-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathThemeController.php
More file actions
89 lines (74 loc) · 2.2 KB
/
ThemeController.php
File metadata and controls
89 lines (74 loc) · 2.2 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
<?php
namespace OWC\OpenPub\Base\RestAPI\Controllers;
use OWC\OpenPub\Base\Repositories\Theme;
use WP_Error;
use WP_Post;
use WP_REST_Request;
class ThemeController extends BaseController
{
/**
* Get a list of all items.
*
* @param WP_REST_Request $request
*
* @return array
* @throws \OWC\OpenPub\Base\Exceptions\PropertyNotExistsException
* @throws \ReflectionException
*/
public function getThemes(WP_REST_Request $request)
{
$themes = (new Theme())->query(apply_filters(
'owc/openpub/rest-api/items/query',
$this->getPaginatorParams($request)
));
return $this->response($themes);
}
/**
* Get an individual post theme by slug.
*
* @return array|WP_Error
*/
public function getThemeBySlug(WP_REST_Request $request)
{
$slug = $request->get_param('slug');
$theme = $this->singleThemeQueryBuilder($request);
$theme = $theme->findBySlug($slug);
if (! $theme) {
return new WP_Error(
'no_theme_found',
sprintf('Theme with slug "%s" not found', sanitize_text_field($slug)),
['status' => 404]
);
}
return $theme;
}
public function singleThemeQueryBuilder(WP_REST_Request $request): Theme
{
$theme = (new Theme())
->query(apply_filters('owc/openpub/rest-api/themes/query/single', []));
$preview = filter_var($request->get_param('draft-preview'), FILTER_VALIDATE_BOOLEAN);
if (true === $preview) {
$theme->query(['post_status' => ['publish', 'draft', 'future']]);
}
return $theme;
}
/**
* Transform a single WP_Post item.
*
* @param WP_Post $post
*
* @return array
*/
public function transform(WP_Post $post)
{
$data = [
'id' => $post->ID,
'title' => $post->post_title,
'content' => apply_filters('the_content', $post->post_content),
'excerpt' => $post->post_excerpt,
'date' => $post->post_date,
];
$data = $this->assignFields($data, $post);
return $data;
}
}