|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WP_REST_Newspack_Articles_Controller file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package WordPress |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class WP_REST_Newspack_Articles_Controller. |
|
10
|
|
|
*/ |
|
11
|
|
|
class WP_REST_Newspack_Articles_Controller extends WP_REST_Controller { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Attribute schema. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
public $attribute_schema; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructs the controller. |
|
22
|
|
|
* |
|
23
|
|
|
* @access public |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() { |
|
26
|
|
|
$this->namespace = 'newspack-blocks/v1'; |
|
27
|
|
|
$this->rest_base = 'articles'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Registers the necessary REST API routes. |
|
32
|
|
|
* |
|
33
|
|
|
* @access public |
|
34
|
|
|
*/ |
|
35
|
|
|
public function register_routes() { |
|
36
|
|
|
register_rest_route( |
|
37
|
|
|
$this->namespace, |
|
38
|
|
|
'/' . $this->rest_base, |
|
39
|
|
|
array( |
|
40
|
|
|
array( |
|
41
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
42
|
|
|
'callback' => array( $this, 'get_items' ), |
|
43
|
|
|
'args' => $this->get_attribute_schema(), |
|
44
|
|
|
'permission_callback' => '__return_true', |
|
45
|
|
|
), |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns a list of rendered posts. |
|
52
|
|
|
* |
|
53
|
|
|
* @param WP_REST_Request $request Request object. |
|
54
|
|
|
* @return WP_REST_Response |
|
55
|
|
|
*/ |
|
56
|
|
|
public function get_items( $request ) { |
|
57
|
|
|
$page = $request->get_param( 'page' ); |
|
58
|
|
|
if ( ! isset( $page ) ) { |
|
59
|
|
|
$page = 1; |
|
60
|
|
|
} |
|
61
|
|
|
$next_page = $page + 1; |
|
62
|
|
|
$exclude_ids = $request->get_param( 'exclude_ids' ); |
|
63
|
|
|
if ( ! isset( $exclude_ids ) ) { |
|
64
|
|
|
$exclude_ids = array(); |
|
65
|
|
|
} |
|
66
|
|
|
$params = $request->get_params(); |
|
67
|
|
|
if ( ! isset( $params ) ) { |
|
68
|
|
|
$params = array(); |
|
69
|
|
|
} |
|
70
|
|
|
$attributes = wp_parse_args( |
|
71
|
|
|
$params, |
|
72
|
|
|
wp_list_pluck( $this->get_attribute_schema(), 'default' ) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$article_query_args = Newspack_Blocks::build_articles_query( $attributes ); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$query = array_merge( |
|
78
|
|
|
$article_query_args, |
|
79
|
|
|
array( |
|
80
|
|
|
'post__not_in' => $exclude_ids, |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
// Run Query. |
|
85
|
|
|
$article_query = new WP_Query( $query ); |
|
86
|
|
|
|
|
87
|
|
|
// Defaults. |
|
88
|
|
|
$items = array(); |
|
89
|
|
|
$ids = array(); |
|
90
|
|
|
$next_url = ''; |
|
91
|
|
|
|
|
92
|
|
|
// The Loop. |
|
93
|
|
|
while ( $article_query->have_posts() ) { |
|
94
|
|
|
$article_query->the_post(); |
|
95
|
|
|
$html = Newspack_Blocks::template_inc( |
|
96
|
|
|
__DIR__ . '/templates/article.php', |
|
97
|
|
|
array( |
|
|
|
|
|
|
98
|
|
|
'attributes' => $attributes, |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
if ( $request->get_param( 'amp' ) ) { |
|
103
|
|
|
$html = $this->generate_amp_partial( $html ); |
|
104
|
|
|
} |
|
105
|
|
|
$items[]['html'] = $html; |
|
106
|
|
|
$ids[] = get_the_ID(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Provide next URL if there are more pages. |
|
110
|
|
|
if ( $next_page <= $article_query->max_num_pages ) { |
|
111
|
|
|
$next_url = add_query_arg( |
|
112
|
|
|
array_merge( |
|
113
|
|
|
array_map( |
|
114
|
|
|
function ( $attribute ) { |
|
115
|
|
|
return false === $attribute ? '0' : str_replace( '#', '%23', $attribute ); |
|
116
|
|
|
}, |
|
117
|
|
|
$attributes |
|
118
|
|
|
), |
|
119
|
|
|
array( |
|
120
|
|
|
'exclude_ids' => false, |
|
121
|
|
|
'page' => $next_page, |
|
122
|
|
|
'amp' => $request->get_param( 'amp' ), |
|
123
|
|
|
) |
|
124
|
|
|
), |
|
125
|
|
|
rest_url( '/newspack-blocks/v1/articles' ) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return rest_ensure_response( |
|
130
|
|
|
array( |
|
131
|
|
|
'items' => $items, |
|
132
|
|
|
'ids' => $ids, |
|
133
|
|
|
'next' => $next_url, |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Sets up and returns attribute schema. |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
public function get_attribute_schema() { |
|
144
|
|
|
if ( empty( $this->attribute_schema ) ) { |
|
145
|
|
|
$block_json = json_decode( |
|
146
|
|
|
file_get_contents( __DIR__ . '/block.json' ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
147
|
|
|
true |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
$this->attribute_schema = array_merge( |
|
151
|
|
|
$block_json['attributes'], |
|
152
|
|
|
array( |
|
153
|
|
|
'exclude_ids' => array( |
|
154
|
|
|
'type' => 'array', |
|
155
|
|
|
'default' => array(), |
|
156
|
|
|
'items' => array( |
|
157
|
|
|
'type' => 'integer', |
|
158
|
|
|
), |
|
159
|
|
|
), |
|
160
|
|
|
) |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
return $this->attribute_schema; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Use AMP Plugin functions to render markup as valid AMP. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $html Markup to convert to AMP. |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public function generate_amp_partial( $html ) { |
|
173
|
|
|
$dom = AMP_DOM_Utils::get_dom_from_content( $html ); |
|
174
|
|
|
|
|
175
|
|
|
AMP_Content_Sanitizer::sanitize_document( |
|
176
|
|
|
$dom, |
|
177
|
|
|
amp_get_content_sanitizers(), |
|
178
|
|
|
array( |
|
179
|
|
|
'use_document_element' => false, |
|
180
|
|
|
) |
|
181
|
|
|
); |
|
182
|
|
|
$xpath = new DOMXPath( $dom ); |
|
183
|
|
|
foreach ( iterator_to_array( $xpath->query( '//noscript | //comment()' ) ) as $node ) { |
|
184
|
|
|
$node->parentNode->removeChild( $node ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
185
|
|
|
} |
|
186
|
|
|
return AMP_DOM_Utils::get_content_from_dom( $dom ); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.