|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Register Newspack Blocks rest fields |
|
4
|
|
|
* |
|
5
|
|
|
* @package Newspack_Blocks |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* `Newspack_Blocks_API` is a wrapper for `register_rest_fields()` |
|
10
|
|
|
*/ |
|
11
|
|
|
class Newspack_Blocks_API { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Register Newspack REST fields. |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function register_rest_fields() { |
|
17
|
|
|
register_rest_field( |
|
18
|
|
|
array( 'post', 'page' ), |
|
19
|
|
|
'newspack_featured_image_src', |
|
20
|
|
|
array( |
|
21
|
|
|
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_image_src' ), |
|
22
|
|
|
'schema' => array( |
|
23
|
|
|
'context' => array( |
|
24
|
|
|
'edit', |
|
25
|
|
|
), |
|
26
|
|
|
'type' => 'array', |
|
27
|
|
|
), |
|
28
|
|
|
) |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
register_rest_field( |
|
32
|
|
|
array( 'post', 'page' ), |
|
33
|
|
|
'newspack_featured_image_caption', |
|
34
|
|
|
array( |
|
35
|
|
|
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_image_caption' ), |
|
36
|
|
|
'schema' => array( |
|
37
|
|
|
'context' => array( |
|
38
|
|
|
'edit', |
|
39
|
|
|
), |
|
40
|
|
|
'type' => 'string', |
|
41
|
|
|
), |
|
42
|
|
|
) |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
/* Add author info source */ |
|
46
|
|
|
register_rest_field( |
|
47
|
|
|
'post', |
|
48
|
|
|
'newspack_author_info', |
|
49
|
|
|
array( |
|
50
|
|
|
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_author_info' ), |
|
51
|
|
|
'schema' => array( |
|
52
|
|
|
'context' => array( |
|
53
|
|
|
'edit', |
|
54
|
|
|
), |
|
55
|
|
|
'type' => 'array', |
|
56
|
|
|
), |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
/* Add first category source */ |
|
61
|
|
|
register_rest_field( |
|
62
|
|
|
'post', |
|
63
|
|
|
'newspack_category_info', |
|
64
|
|
|
array( |
|
65
|
|
|
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_primary_category' ), |
|
66
|
|
|
'schema' => array( |
|
67
|
|
|
'context' => array( |
|
68
|
|
|
'edit', |
|
69
|
|
|
), |
|
70
|
|
|
'type' => 'string', |
|
71
|
|
|
), |
|
72
|
|
|
) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
/* Add list of categories for CSS classes */ |
|
76
|
|
|
register_rest_field( |
|
77
|
|
|
'post', |
|
78
|
|
|
'newspack_article_classes', |
|
79
|
|
|
array( |
|
80
|
|
|
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_get_cat_tag_classes' ), |
|
81
|
|
|
'schema' => array( |
|
82
|
|
|
'context' => array( |
|
83
|
|
|
'edit', |
|
84
|
|
|
), |
|
85
|
|
|
'type' => 'string', |
|
86
|
|
|
), |
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
/* Sponsors */ |
|
91
|
|
|
register_rest_field( |
|
92
|
|
|
'post', |
|
93
|
|
|
'newspack_post_sponsors', |
|
94
|
|
|
array( |
|
95
|
|
|
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_sponsor_info' ), |
|
96
|
|
|
'schema' => array( |
|
97
|
|
|
'context' => array( |
|
98
|
|
|
'edit', |
|
99
|
|
|
), |
|
100
|
|
|
'type' => 'array', |
|
101
|
|
|
), |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
/* Post format */ |
|
106
|
|
|
register_rest_field( |
|
107
|
|
|
'post', |
|
108
|
|
|
'newspack_post_format', |
|
109
|
|
|
array( |
|
110
|
|
|
'get_callback' => array( 'Newspack_Blocks_API', 'newspack_blocks_post_format' ), |
|
111
|
|
|
'schema' => array( |
|
112
|
|
|
'context' => array( |
|
113
|
|
|
'edit', |
|
114
|
|
|
), |
|
115
|
|
|
'type' => 'string', |
|
116
|
|
|
), |
|
117
|
|
|
) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get thumbnail featured image source for the rest field. |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $object The object info. |
|
125
|
|
|
* @return array | bool Featured image if available, false if not. |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function newspack_blocks_get_image_src( $object ) { |
|
128
|
|
|
$featured_image_set = array(); |
|
129
|
|
|
|
|
130
|
|
|
if ( 0 === $object['featured_media'] ) { |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// Large image. |
|
135
|
|
|
$feat_img_array_large = wp_get_attachment_image_src( |
|
136
|
|
|
$object['featured_media'], |
|
137
|
|
|
'large', |
|
138
|
|
|
false |
|
139
|
|
|
); |
|
140
|
|
|
$featured_image_set['large'] = $feat_img_array_large[0]; |
|
141
|
|
|
|
|
142
|
|
|
// Landscape image. |
|
143
|
|
|
$landscape_size = Newspack_Blocks::image_size_for_orientation( 'landscape' ); |
|
144
|
|
|
|
|
145
|
|
|
$feat_img_array_landscape = wp_get_attachment_image_src( |
|
146
|
|
|
$object['featured_media'], |
|
147
|
|
|
$landscape_size, |
|
148
|
|
|
false |
|
149
|
|
|
); |
|
150
|
|
|
$featured_image_set['landscape'] = $feat_img_array_landscape[0]; |
|
151
|
|
|
|
|
152
|
|
|
// Portrait image. |
|
153
|
|
|
$portrait_size = Newspack_Blocks::image_size_for_orientation( 'portrait' ); |
|
154
|
|
|
|
|
155
|
|
|
$feat_img_array_portrait = wp_get_attachment_image_src( |
|
156
|
|
|
$object['featured_media'], |
|
157
|
|
|
$portrait_size, |
|
158
|
|
|
false |
|
159
|
|
|
); |
|
160
|
|
|
$featured_image_set['portrait'] = $feat_img_array_portrait[0]; |
|
161
|
|
|
|
|
162
|
|
|
// Square image. |
|
163
|
|
|
$square_size = Newspack_Blocks::image_size_for_orientation( 'square' ); |
|
164
|
|
|
|
|
165
|
|
|
$feat_img_array_square = wp_get_attachment_image_src( |
|
166
|
|
|
$object['featured_media'], |
|
167
|
|
|
$square_size, |
|
168
|
|
|
false |
|
169
|
|
|
); |
|
170
|
|
|
$featured_image_set['square'] = $feat_img_array_square[0]; |
|
171
|
|
|
|
|
172
|
|
|
// Uncropped image. |
|
173
|
|
|
$uncropped_size = 'newspack-article-block-uncropped'; |
|
174
|
|
|
|
|
175
|
|
|
$feat_img_array_uncropped = wp_get_attachment_image_src( |
|
176
|
|
|
$object['featured_media'], |
|
177
|
|
|
$uncropped_size, |
|
178
|
|
|
false |
|
179
|
|
|
); |
|
180
|
|
|
$featured_image_set['uncropped'] = $feat_img_array_uncropped[0]; |
|
181
|
|
|
|
|
182
|
|
|
return $featured_image_set; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get thumbnail featured image captions for the rest field. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $object The object info. |
|
189
|
|
|
* @return string|null Image caption on success, null on failure. |
|
190
|
|
|
*/ |
|
191
|
|
|
public static function newspack_blocks_get_image_caption( $object ) { |
|
192
|
|
|
return (int) $object['featured_media'] > 0 ? trim( wp_get_attachment_caption( $object['featured_media'] ) ) : null; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Get author info for the rest field. |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $object The object info. |
|
199
|
|
|
* @return array Author data. |
|
200
|
|
|
*/ |
|
201
|
|
|
public static function newspack_blocks_get_author_info( $object ) { |
|
202
|
|
|
$author_data = array(); |
|
203
|
|
|
|
|
204
|
|
|
if ( function_exists( 'coauthors_posts_links' ) && ! empty( get_coauthors() ) ) : |
|
205
|
|
|
$authors = get_coauthors(); |
|
206
|
|
|
|
|
207
|
|
|
foreach ( $authors as $author ) { |
|
208
|
|
|
// Check if this is a guest author post type. |
|
209
|
|
View Code Duplication |
if ( 'guest-author' === get_post_type( $author->ID ) ) { |
|
210
|
|
|
// If yes, make sure the author actually has an avatar set; otherwise, coauthors_get_avatar returns a featured image. |
|
211
|
|
|
if ( get_post_thumbnail_id( $author->ID ) ) { |
|
212
|
|
|
$author_avatar = coauthors_get_avatar( $author, 48 ); |
|
213
|
|
|
} else { |
|
214
|
|
|
// If there is no avatar, force it to return the current fallback image. |
|
215
|
|
|
$author_avatar = get_avatar( ' ' ); |
|
216
|
|
|
} |
|
217
|
|
|
} else { |
|
218
|
|
|
$author_avatar = coauthors_get_avatar( $author, 48 ); |
|
219
|
|
|
} |
|
220
|
|
|
$author_link = null; |
|
221
|
|
|
if ( function_exists( 'coauthors_posts_links' ) ) { |
|
222
|
|
|
$author_link = get_author_posts_url( $author->ID, $author->user_nicename ); |
|
223
|
|
|
} |
|
224
|
|
|
$author_data[] = array( |
|
225
|
|
|
/* Get the author name */ |
|
226
|
|
|
'display_name' => esc_html( $author->display_name ), |
|
227
|
|
|
/* Get the author avatar */ |
|
228
|
|
|
'avatar' => wp_kses_post( $author_avatar ), |
|
229
|
|
|
/* Get the author ID */ |
|
230
|
|
|
'id' => $author->ID, |
|
231
|
|
|
/* Get the author Link */ |
|
232
|
|
|
'author_link' => $author_link, |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
else : |
|
236
|
|
|
$author_data[] = array( |
|
237
|
|
|
/* Get the author name */ |
|
238
|
|
|
'display_name' => get_the_author_meta( 'display_name', $object['author'] ), |
|
239
|
|
|
/* Get the author avatar */ |
|
240
|
|
|
'avatar' => get_avatar( $object['author'], 48 ), |
|
241
|
|
|
/* Get the author ID */ |
|
242
|
|
|
'id' => $object['author'], |
|
243
|
|
|
/* Get the author Link */ |
|
244
|
|
|
'author_link' => get_author_posts_url( $object['author'] ), |
|
245
|
|
|
); |
|
246
|
|
|
endif; |
|
247
|
|
|
|
|
248
|
|
|
/* Return the author data */ |
|
249
|
|
|
return $author_data; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Get primary category for the rest field. |
|
254
|
|
|
* |
|
255
|
|
|
* @param array $object The object info. |
|
256
|
|
|
* @return string Category name. |
|
257
|
|
|
*/ |
|
258
|
|
|
public static function newspack_blocks_get_primary_category( $object ) { |
|
259
|
|
|
$category = false; |
|
260
|
|
|
|
|
261
|
|
|
// Use Yoast primary category if set. |
|
262
|
|
View Code Duplication |
if ( class_exists( 'WPSEO_Primary_Term' ) ) { |
|
263
|
|
|
$primary_term = new WPSEO_Primary_Term( 'category', $object['id'] ); |
|
264
|
|
|
$category_id = $primary_term->get_primary_term(); |
|
265
|
|
|
if ( $category_id ) { |
|
266
|
|
|
$category = get_term( $category_id ); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
View Code Duplication |
if ( ! $category ) { |
|
271
|
|
|
$categories_list = get_the_category( $object['id'] ); |
|
272
|
|
|
if ( ! empty( $categories_list ) ) { |
|
273
|
|
|
$category = $categories_list[0]; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
if ( ! $category ) { |
|
278
|
|
|
return ''; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return $category->name; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Get a list of category, tag classes for the rest field. |
|
286
|
|
|
* |
|
287
|
|
|
* @param array $object The object info. |
|
288
|
|
|
* @return string classes from assigned categories and tags. |
|
289
|
|
|
*/ |
|
290
|
|
|
public static function newspack_blocks_get_cat_tag_classes( $object ) { |
|
291
|
|
|
return Newspack_Blocks::get_term_classes( $object['id'] ); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Get all sponsor information for the rest field. |
|
296
|
|
|
* |
|
297
|
|
|
* @param array $object The object info. |
|
298
|
|
|
* @return array sponsor information. |
|
299
|
|
|
*/ |
|
300
|
|
|
public static function newspack_blocks_sponsor_info( $object ) { |
|
301
|
|
|
$sponsors = Newspack_Blocks::get_all_sponsors( |
|
302
|
|
|
$object['id'], |
|
303
|
|
|
'native', |
|
304
|
|
|
'post', |
|
305
|
|
|
array( |
|
306
|
|
|
'maxwidth' => 80, |
|
307
|
|
|
'maxheight' => 40, |
|
308
|
|
|
) |
|
309
|
|
|
); |
|
310
|
|
|
if ( ! empty( $sponsors ) ) { |
|
311
|
|
|
foreach ( $sponsors as $sponsor ) { |
|
312
|
|
|
$sponsor_info = array(); |
|
313
|
|
|
$sponsor_info_item = array( |
|
314
|
|
|
'flag' => $sponsor['sponsor_flag'], |
|
315
|
|
|
'sponsor_name' => $sponsor['sponsor_name'], |
|
316
|
|
|
'sponsor_url' => $sponsor['sponsor_url'], |
|
317
|
|
|
'byline_prefix' => $sponsor['sponsor_byline'], |
|
318
|
|
|
'id' => $sponsor['sponsor_id'], |
|
319
|
|
|
'scope' => $sponsor['sponsor_scope'], |
|
320
|
|
|
); |
|
321
|
|
|
if ( ! empty( $sponsor['sponsor_logo'] ) ) { |
|
322
|
|
|
$sponsor_info_item['src'] = $sponsor['sponsor_logo']['src']; |
|
323
|
|
|
$sponsor_info_item['img_width'] = $sponsor['sponsor_logo']['img_width']; |
|
324
|
|
|
$sponsor_info_item['img_height'] = $sponsor['sponsor_logo']['img_height']; |
|
325
|
|
|
} |
|
326
|
|
|
$sponsor_info[] = $sponsor_info_item; |
|
327
|
|
|
} |
|
328
|
|
|
return $sponsor_info; |
|
|
|
|
|
|
329
|
|
|
} else { |
|
330
|
|
|
return false; |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Pass post format to editor. |
|
336
|
|
|
* |
|
337
|
|
|
* @param array $object The object info. |
|
338
|
|
|
* @return string post format. |
|
339
|
|
|
*/ |
|
340
|
|
|
public static function newspack_blocks_post_format( $object ) { |
|
341
|
|
|
$post_format = get_post_format( $object['id'] ); |
|
342
|
|
|
return $post_format; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Register the video-playlist endpoint. |
|
347
|
|
|
*/ |
|
348
|
|
|
public static function register_video_playlist_endpoint() { |
|
349
|
|
|
register_rest_route( |
|
350
|
|
|
'newspack-blocks/v1', |
|
351
|
|
|
'/video-playlist', |
|
352
|
|
|
array( |
|
353
|
|
|
'methods' => 'GET', |
|
354
|
|
|
'callback' => array( 'Newspack_Blocks_API', 'video_playlist_endpoint' ), |
|
355
|
|
|
'permission_callback' => function () { |
|
356
|
|
|
return current_user_can( 'edit_posts' ); |
|
357
|
|
|
}, |
|
358
|
|
|
) |
|
359
|
|
|
); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* Register specific posts endpoint. |
|
364
|
|
|
*/ |
|
365
|
|
|
public static function register_post_lookup_endpoint() { |
|
366
|
|
|
if ( ! Newspack_Blocks::use_experimental() ) { |
|
367
|
|
|
return; |
|
368
|
|
|
} |
|
369
|
|
|
register_rest_route( |
|
370
|
|
|
'newspack-blocks/v1', |
|
371
|
|
|
'/specific-posts', |
|
372
|
|
|
array( |
|
373
|
|
|
'methods' => \WP_REST_Server::READABLE, |
|
374
|
|
|
'callback' => array( 'Newspack_Blocks_API', 'specific_posts_endpoint' ), |
|
375
|
|
|
'args' => array( |
|
376
|
|
|
'search' => array( |
|
377
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
|
378
|
|
|
), |
|
379
|
|
|
'per_page' => array( |
|
380
|
|
|
'sanitize_callback' => 'absint', |
|
381
|
|
|
), |
|
382
|
|
|
), |
|
383
|
|
|
'permission_callback' => function () { |
|
384
|
|
|
return current_user_can( 'edit_posts' ); |
|
385
|
|
|
}, |
|
386
|
|
|
) |
|
387
|
|
|
); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* Process requests to the video-playlist endpoint. |
|
392
|
|
|
* |
|
393
|
|
|
* @param WP_REST_Request $request Request object. |
|
394
|
|
|
* @return WP_REST_Response. |
|
|
|
|
|
|
395
|
|
|
*/ |
|
396
|
|
|
public static function video_playlist_endpoint( $request ) { |
|
397
|
|
|
$args = $request->get_params(); |
|
398
|
|
|
return new \WP_REST_Response( newspack_blocks_get_video_playlist( $args ), 200 ); |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Lookup individual posts by title only. |
|
403
|
|
|
* |
|
404
|
|
|
* @param WP_REST_Request $request Request object. |
|
405
|
|
|
* @return WP_REST_Response. |
|
|
|
|
|
|
406
|
|
|
*/ |
|
407
|
|
|
public static function specific_posts_endpoint( $request ) { |
|
408
|
|
|
$params = $request->get_params(); |
|
409
|
|
|
if ( empty( $params['search'] ) ) { |
|
410
|
|
|
return new \WP_REST_Response( array() ); |
|
411
|
|
|
} |
|
412
|
|
|
add_filter( 'posts_where', array( 'Newspack_Blocks_API', 'add_post_title_wildcard_search' ), 10, 2 ); |
|
413
|
|
|
|
|
414
|
|
|
$args = array( |
|
415
|
|
|
'post_type' => 'post', |
|
416
|
|
|
'post_status' => 'publish', |
|
417
|
|
|
'title_wildcard_search' => esc_sql( $params['search'] ), |
|
418
|
|
|
'posts_per_page' => $params['per_page'], |
|
419
|
|
|
); |
|
420
|
|
|
|
|
421
|
|
|
$query = new WP_Query( $args ); |
|
422
|
|
|
remove_filter( 'posts_where', array( 'Newspack_Blocks_API', 'add_post_title_wildcard_search' ), 10, 2 ); |
|
423
|
|
|
return new \WP_REST_Response( |
|
424
|
|
|
array_map( |
|
425
|
|
|
function ( $post ) { |
|
426
|
|
|
return array( |
|
427
|
|
|
'id' => $post->ID, |
|
428
|
|
|
'title' => $post->post_title, |
|
429
|
|
|
); |
|
430
|
|
|
}, |
|
431
|
|
|
$query->posts |
|
432
|
|
|
), |
|
433
|
|
|
200 |
|
434
|
|
|
); |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* Add title wildcard search to post lookup query. |
|
439
|
|
|
* |
|
440
|
|
|
* @param String $where Where clause. |
|
441
|
|
|
* @param WP_Query $query The query. |
|
442
|
|
|
*/ |
|
443
|
|
|
public static function add_post_title_wildcard_search( $where, $query ) { |
|
444
|
|
|
$search = ! empty( $query->query['title_wildcard_search'] ) ? $query->query['title_wildcard_search'] : null; |
|
445
|
|
|
$where .= ' AND post_title LIKE "%' . $search . '%" '; |
|
446
|
|
|
return $where; |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* Adds meta query support to API rest endpoint. |
|
451
|
|
|
* |
|
452
|
|
|
* @param array $args Key value array of query var to query value. |
|
453
|
|
|
* @param WP_REST_Request $request The request used. |
|
454
|
|
|
* @return array $args Filtered request parameters. |
|
455
|
|
|
*/ |
|
456
|
|
|
public static function post_meta_request_params( $args, $request ) { |
|
457
|
|
|
$params = $request->get_params(); |
|
458
|
|
|
|
|
459
|
|
|
if ( |
|
460
|
|
|
isset( $params['meta_key'], $params['meta_value_num'], $params['meta_compare'] ) && |
|
461
|
|
|
'_thumbnail_id' === $params['meta_key'] && |
|
462
|
|
|
'0' === $params['meta_value_num'] && |
|
463
|
|
|
'>' === $params['meta_compare'] |
|
464
|
|
|
) { |
|
465
|
|
|
// phpcs:disable WordPress.DB.SlowDBQuery |
|
466
|
|
|
$args['meta_key'] = $params['meta_key']; |
|
467
|
|
|
$args['meta_value_num'] = $params['meta_value_num']; |
|
468
|
|
|
$args['meta_compare'] = $params['meta_compare']; |
|
469
|
|
|
// phpcs:enable WordPress.DB.SlowDBQuery |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
return $args; |
|
473
|
|
|
} |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_rest_fields' ) ); |
|
477
|
|
|
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_video_playlist_endpoint' ) ); |
|
478
|
|
|
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_post_lookup_endpoint' ) ); |
|
479
|
|
|
add_filter( 'rest_post_query', array( 'Newspack_Blocks_API', 'post_meta_request_params' ), 10, 2 ); |
|
480
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: