-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSubthemaController.php
More file actions
112 lines (89 loc) · 3.15 KB
/
SubthemaController.php
File metadata and controls
112 lines (89 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Controller which handles the (requested) subthema(s).
*/
namespace OWC\PDC\Base\RestAPI\Controllers;
use WP_Error;
use WP_REST_Request;
use OWC\PDC\Base\Repositories\Subthema;
/**
* Controller which handles the (requested) subthema(s).
*/
class SubthemaController extends BaseController
{
/**
* Get a list of all subthemas.
*/
public function getSubthemas(WP_REST_Request $request): array
{
$orderBy = $request->get_param('orderby') ?? 'name';
$order = $request->get_param('order') ?? 'ASC';
$items = (new Subthema())
->query(apply_filters('owc/pdc/rest-api/subthemas/query', $this->getPaginatorParams($request)))
->query($this->getOrderClause($orderBy, $order))
->query([
'post_status' => $this->getPostStatus($request)
]);
if ($this->plugin->settings->useShowOn() && $this->showOnParamIsValid($request)) {
$items->filterSource($request->get_param('source'));
}
if ($language = $request->get_param('language')) {
$items->filterLanguage((string) $language);
}
$data = $items->all();
$query = $items->getQuery();
return $this->addPaginator($data, $query);
}
/**
* Get an individual subthema.
*
* @return array|WP_Error
*/
public function getSubthema(WP_REST_Request $request)
{
$id = (int) $request->get_param('id');
$thema = (new Subthema())
->query(apply_filters('owc/pdc/rest-api/subthemas/query/single', []))
->query(['post_status' => $this->getPostStatus($request)]);
if ($this->plugin->settings->useShowOn() && $this->showOnParamIsValid($request)) {
$thema->filterSource($request->get_param('source'));
}
if ($language = $request->get_param('language')) {
$thema->filterLanguage((string) $language);
}
$thema = $thema->find($id);
if (! $thema) {
return new WP_Error('no_item_found', sprintf('Subthema with ID [%d] not found', $id), [
'status' => 404,
]);
}
return $thema;
}
/**
* Get an individual subtheme by slug.
*
* @return array|WP_Error
*/
public function getSubthemeBySlug(WP_REST_Request $request)
{
$slug = $request->get_param('slug');
$subtheme = (new Subthema())
->query(apply_filters('owc/pdc/rest-api/subthemas/query/single', []))
->query(['post_status' => $this->getPostStatus($request)]);
if ($this->plugin->settings->useShowOn() && $this->showOnParamIsValid($request)) {
$subtheme->filterSource($request->get_param('source'));
}
if ($language = $request->get_param('language')) {
$subtheme->filterLanguage((string) $language);
}
$subtheme = $subtheme->findBySlug($slug);
if (! $subtheme) {
return new WP_Error(
'no_subtheme_found',
sprintf('Subheme with slug [%s] not found', $slug),
['status' => 404]
);
}
return $subtheme;
}
}