Completed
Push — renovate/history-4.x ( 8706da...6c1ea7 )
by
unknown
17:57 queued 11:18
created

class.wpcom-json-api-list-posts-v1-1-endpoint.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
new WPCOM_JSON_API_List_Posts_v1_1_Endpoint( array(
4
	'description' => 'Get a list of matching posts.',
5
	'min_version' => '1.1',
6
	'max_version' => '1.1',
7
8
	'group'       => 'posts',
9
	'stat'        => 'posts',
10
11
	'method'      => 'GET',
12
	'path'        => '/sites/%s/posts/',
13
	'path_labels' => array(
14
		'$site' => '(int|string) Site ID or domain',
15
	),
16
17
	'query_parameters' => array(
18
		'number'   => '(int=20) The number of posts to return. Limit: 100.',
19
		'offset'   => '(int=0) 0-indexed offset.',
20
		'page'     => '(int) Return the Nth 1-indexed page of posts. Takes precedence over the <code>offset</code> parameter.',
21
		'page_handle' => '(string) A page handle, returned from a previous API call as a <code>meta.next_page</code> property. This is the most efficient way to fetch the next page of results.',
22
		'order'    => array(
23
			'DESC' => 'Return posts in descending order. For dates, that means newest to oldest.',
24
			'ASC'  => 'Return posts in ascending order. For dates, that means oldest to newest.',
25
		),
26
		'order_by' => array(
27
			'date'          => 'Order by the created time of each post.',
28
			'modified'      => 'Order by the modified time of each post.',
29
			'title'         => "Order lexicographically by the posts' titles.",
30
			'comment_count' => 'Order by the number of comments for each post.',
31
			'ID'            => 'Order by post ID.',
32
		),
33
		'after'    => '(ISO 8601 datetime) Return posts dated after the specified datetime.',
34
		'before'   => '(ISO 8601 datetime) Return posts dated before the specified datetime.',
35
		'modified_after'    => '(ISO 8601 datetime) Return posts modified after the specified datetime.',
36
		'modified_before'   => '(ISO 8601 datetime) Return posts modified before the specified datetime.',
37
		'tag'      => '(string) Specify the tag name or slug.',
38
		'category' => '(string) Specify the category name or slug.',
39
		'term'     => '(object:string) Specify comma-separated term slugs to search within, indexed by taxonomy slug.',
40
		'type'     => "(string) Specify the post type. Defaults to 'post', use 'any' to query for both posts and pages. Post types besides post and page need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.",
41
		'parent_id' => '(int) Returns only posts which are children of the specified post. Applies only to hierarchical post types.',
42
		'exclude'  => '(array:int|int) Excludes the specified post ID(s) from the response',
43
		'exclude_tree' => '(int) Excludes the specified post and all of its descendants from the response. Applies only to hierarchical post types.',
44
		'status'   => '(string) Comma-separated list of statuses for which to query, including any of: "publish", "private", "draft", "pending", "future", and "trash", or simply "any". Defaults to "publish"',
45
		'sticky'    => array(
46
			'include'   => 'Sticky posts are not excluded from the list.',
47
			'exclude'   => 'Sticky posts are excluded from the list.',
48
			'require'   => 'Only include sticky posts',
49
		),
50
		'author'   => "(int) Author's user ID",
51
		'search'   => '(string) Search query',
52
		'meta_key'   => '(string) Metadata key that the post should contain',
53
		'meta_value'   => '(string) Metadata value that the post should contain. Will only be applied if a `meta_key` is also given',
54
	),
55
56
	'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/?number=2'
57
) );
58
59
class WPCOM_JSON_API_List_Posts_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
60
	public $date_range = array();
61
	public $modified_range = array();
62
	public $page_handle = array();
63
	public $performed_query = null;
64
65
	public $response_format = array(
66
		'found'    => '(int) The total number of posts found that match the request (ignoring limits, offsets, and pagination).',
67
		'posts'    => '(array:post) An array of post objects.',
68
		'meta'     => '(object) Meta data',
69
	);
70
71
	// /sites/%s/posts/ -> $blog_id
72
	function callback( $path = '', $blog_id = 0 ) {
73
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
74
		if ( is_wp_error( $blog_id ) ) {
75
			return $blog_id;
76
		}
77
78
		$args = $this->query_args();
79
		$is_eligible_for_page_handle = true;
80
		$site = $this->get_platform()->get_site( $blog_id );
81
82
		if ( $args['number'] < 1 ) {
83
			$args['number'] = 20;
84
		} elseif ( 100 < $args['number'] ) {
85
			return new WP_Error( 'invalid_number',  'The NUMBER parameter must be less than or equal to 100.', 400 );
86
		}
87
88 View Code Duplication
		if ( isset( $args['type'] ) &&
89
			   ! in_array( $args['type'], array( 'post', 'revision', 'page', 'any' ) ) &&
90
			   defined( 'IS_WPCOM' ) && IS_WPCOM ) {
91
			$this->load_theme_functions();
92
		}
93
94 View Code Duplication
		if ( isset( $args['type'] ) && ! $site->is_post_type_allowed( $args['type'] ) ) {
95
			return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
96
		}
97
98
		// Normalize post_type
99 View Code Duplication
		if ( isset( $args['type'] ) && 'any' == $args['type'] ) {
100
			if ( version_compare( $this->api->version, '1.1', '<' ) ) {
101
				$args['type'] = array( 'post', 'page' );
102
			} else { // 1.1+
103
				$args['type'] = $site->get_whitelisted_post_types();
104
			}
105
		}
106
107
		// determine statuses
108
		$status = ( ! empty( $args['status'] ) ) ? explode( ',', $args['status'] ) : array( 'publish' );
109 View Code Duplication
		if ( is_user_logged_in() ) {
110
			$statuses_whitelist = array(
111
				'publish',
112
				'pending',
113
				'draft',
114
				'future',
115
				'private',
116
				'trash',
117
				'any',
118
			);
119
			$status = array_intersect( $status, $statuses_whitelist );
120
		} else {
121
			// logged-out users can see only published posts
122
			$statuses_whitelist = array( 'publish', 'any' );
123
			$status = array_intersect( $status, $statuses_whitelist );
124
125
			if ( empty( $status ) ) {
126
				// requested only protected statuses? nothing for you here
127
				return array( 'found' => 0, 'posts' => array() );
128
			}
129
			// clear it (AKA published only) because "any" includes protected
130
			$status = array();
131
		}
132
133
		// let's be explicit about defaulting to 'post'
134
		$args['type'] = isset( $args['type'] ) ? $args['type'] : 'post';
135
136
		// make sure the user can read or edit the requested post type(s)
137 View Code Duplication
		if ( is_array( $args['type'] ) ) {
138
			$allowed_types = array();
139
			foreach ( $args['type'] as $post_type ) {
140
				if ( $site->current_user_can_access_post_type( $post_type, $args['context'] ) ) {
141
				   	$allowed_types[] = $post_type;
142
				}
143
			}
144
145
			if ( empty( $allowed_types ) ) {
146
				return array( 'found' => 0, 'posts' => array() );
147
			}
148
			$args['type'] = $allowed_types;
149
		}
150
		else {
151
			if ( ! $site->current_user_can_access_post_type( $args['type'], $args['context'] ) ) {
152
				return array( 'found' => 0, 'posts' => array() );
153
			}
154
		}
155
156
157
		$query = array(
158
			'posts_per_page' => $args['number'],
159
			'order'          => $args['order'],
160
			'orderby'        => $args['order_by'],
161
			'post_type'      => $args['type'],
162
			'post_status'    => $status,
163
			'post_parent'    => isset( $args['parent_id'] ) ? $args['parent_id'] : null,
164
			'author'         => isset( $args['author'] ) && 0 < $args['author'] ? $args['author'] : null,
165
			's'              => isset( $args['search'] ) && '' !== $args['search'] ? $args['search'] : null,
166
			'fields'         => 'ids',
167
		);
168
169
		if ( ! is_user_logged_in () ) {
170
			$query['has_password'] = false;
171
		}
172
173 View Code Duplication
		if ( isset( $args['meta_key'] ) ) {
174
			$show = false;
175
			if ( WPCOM_JSON_API_Metadata::is_public( $args['meta_key'] ) )
176
				$show = true;
177
			if ( current_user_can( 'edit_post_meta', $query['post_type'], $args['meta_key'] ) )
178
				$show = true;
179
180
			if ( is_protected_meta( $args['meta_key'], 'post' ) && ! $show )
181
				return new WP_Error( 'invalid_meta_key', 'Invalid meta key', 404 );
182
183
			$meta = array( 'key' => $args['meta_key'] );
184
			if ( isset( $args['meta_value'] ) )
185
				$meta['value'] = $args['meta_value'];
186
187
			$query['meta_query'] = array( $meta );
188
		}
189
190 View Code Duplication
		if ( $args['sticky'] === 'include' ) {
191
			$query['ignore_sticky_posts'] = 1;
192
		} else if ( $args['sticky'] === 'exclude' ) {
193
			$sticky = get_option( 'sticky_posts' );
194
			if ( is_array( $sticky ) ) {
195
				$query['post__not_in'] = $sticky;
196
			}
197
		} else if ( $args['sticky'] === 'require' ) {
198
			$sticky = get_option( 'sticky_posts' );
199
			if ( is_array( $sticky ) && ! empty( $sticky ) ) {
200
				$query['post__in'] = $sticky;
201
			} else {
202
				// no sticky posts exist
203
				return array( 'found' => 0, 'posts' => array() );
204
			}
205
		}
206
207 View Code Duplication
		if ( isset( $args['exclude'] ) ) {
208
			$excluded_ids = (array) $args['exclude'];
209
			$query['post__not_in'] = isset( $query['post__not_in'] ) ? array_merge( $query['post__not_in'], $excluded_ids ) : $excluded_ids;
210
		}
211
212 View Code Duplication
		if ( isset( $args['exclude_tree'] ) && is_post_type_hierarchical( $args['type'] ) ) {
213
			// get_page_children is a misnomer; it supports all hierarchical post types
214
			$page_args = array(
215
					'child_of' => $args['exclude_tree'],
216
					'post_type' => $args['type'],
217
					// since we're looking for things to exclude, be aggressive
218
					'post_status' => 'publish,draft,pending,private,future,trash',
219
				);
220
			$post_descendants = get_pages( $page_args );
221
222
			$exclude_tree = array( $args['exclude_tree'] );
223
			foreach ( $post_descendants as $child ) {
224
				$exclude_tree[] = $child->ID;
225
			}
226
227
			$query['post__not_in'] = isset( $query['post__not_in'] ) ? array_merge( $query['post__not_in'], $exclude_tree ) : $exclude_tree;
228
		}
229
230 View Code Duplication
		if ( isset( $args['category'] ) ) {
231
			$category = get_term_by( 'slug', $args['category'], 'category' );
232
			if ( $category === false) {
233
				$query['category_name'] = $args['category'];
234
			} else {
235
				$query['cat'] = $category->term_id;
236
			}
237
		}
238
239
		if ( isset( $args['tag'] ) ) {
240
			$query['tag'] = $args['tag'];
241
		}
242
243 View Code Duplication
		if ( ! empty( $args['term'] ) ) {
244
			$query['tax_query'] = array();
245
			foreach ( $args['term'] as $taxonomy => $slug ) {
246
				$taxonomy_object = get_taxonomy( $taxonomy );
247
				if ( false === $taxonomy_object || ( ! $taxonomy_object->public && 
248
						! current_user_can( $taxonomy_object->cap->assign_terms ) ) ) {
249
					continue;
250
				}
251
252
				$query['tax_query'][] = array(
253
					'taxonomy' => $taxonomy,
254
					'field' => 'slug',
255
					'terms' => explode( ',', $slug )
256
				);				
257
			}
258
		}
259
260 View Code Duplication
		if ( isset( $args['page'] ) ) {
261
			if ( $args['page'] < 1 ) {
262
				$args['page'] = 1;
263
			}
264
265
			$query['paged'] = $args['page'];
266
			if ( $query['paged'] !== 1 ) {
267
				$is_eligible_for_page_handle = false;
268
			}
269
		} else {
270
			if ( $args['offset'] < 0 ) {
271
				$args['offset'] = 0;
272
			}
273
274
			$query['offset'] = $args['offset'];
275
			if ( $query['offset'] !== 0 ) {
276
				$is_eligible_for_page_handle = false;
277
			}
278
		}
279
280
		if ( isset( $args['before_gmt'] ) ) {
281
			$this->date_range['before'] = $args['before_gmt'];
282
		}
283
		if ( isset( $args['after_gmt'] ) ) {
284
			$this->date_range['after'] = $args['after_gmt'];
285
		}
286
287
		if ( isset( $args['modified_before_gmt'] ) ) {
288
			$this->modified_range['before'] = $args['modified_before_gmt'];
289
		}
290
		if ( isset( $args['modified_after_gmt'] ) ) {
291
			$this->modified_range['after'] = $args['modified_after_gmt'];
292
		}
293
294
		if ( $this->date_range ) {
295
			add_filter( 'posts_where', array( $this, 'handle_date_range' ) );
296
		}
297
298
		if ( $this->modified_range ) {
299
			add_filter( 'posts_where', array( $this, 'handle_modified_range' ) );
300
		}
301
302 View Code Duplication
		if ( isset( $args['page_handle'] ) ) {
303
			$page_handle = wp_parse_args( $args['page_handle'] );
304
			if ( isset( $page_handle['value'] ) && isset( $page_handle['id'] ) ) {
305
				// we have a valid looking page handle
306
				$this->page_handle = $page_handle;
307
				add_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
308
			}
309
		}
310
311
		/**
312
		 * 'column' necessary for the me/posts endpoint (which extends sites/$site/posts).
313
		 * Would need to be added to the sites/$site/posts definition if we ever want to
314
		 * use it there.
315
		 */
316
		$column_whitelist = array( 'post_modified_gmt' );
317 View Code Duplication
		if ( isset( $args['column'] ) && in_array( $args['column'], $column_whitelist ) ) {
318
			$query['column'] = $args['column'];
319
		}
320
321
		$this->performed_query = $query;
322
		add_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
323
324
		$wp_query = new WP_Query( $query );
325
326
		remove_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
327
328
		if ( $this->date_range ) {
329
			remove_filter( 'posts_where', array( $this, 'handle_date_range' ) );
330
			$this->date_range = array();
331
		}
332
333
		if ( $this->modified_range ) {
334
			remove_filter( 'posts_where', array( $this, 'handle_modified_range' ) );
335
			$this->modified_range = array();
336
		}
337
338
		if ( $this->page_handle ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->page_handle of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
339
			remove_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
340
341
		}
342
343
		$return = array();
344
		$excluded_count = 0;
345 View Code Duplication
		foreach ( array_keys( $this->response_format ) as $key ) {
346
			switch ( $key ) {
347
			case 'found' :
348
				$return[$key] = (int) $wp_query->found_posts;
349
				break;
350
			case 'posts' :
351
				$posts = array();
352
				foreach ( $wp_query->posts as $post_ID ) {
353
					$the_post = $this->get_post_by( 'ID', $post_ID, $args['context'] );
354
					if ( $the_post && ! is_wp_error( $the_post ) ) {
355
						$posts[] = $the_post;
356
					} else {
357
						$excluded_count++;
358
					}
359
				}
360
361
				if ( $posts ) {
362
					/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
363
					do_action( 'wpcom_json_api_objects', 'posts', count( $posts ) );
364
				}
365
366
				$return[$key] = $posts;
367
				break;
368
369
			case 'meta' :
370
				if ( ! is_array( $args['type'] ) ) {
371
					$return[$key] = (object) array(
372
						'links' => (object) array(
373
							'counts' => (string) $this->links->get_site_link( $blog_id, 'post-counts/' . $args['type'] ),
374
						)
375
					);
376
				}
377
378
				if ( $is_eligible_for_page_handle && $return['posts'] ) {
379
					$last_post = end( $return['posts'] );
380
					reset( $return['posts'] );
381
					if ( ( $return['found'] > count( $return['posts'] ) ) && $last_post ) {
382
						if ( ! isset( $return[$key] ) ) {
383
							$return[$key] = (object) array();
384
						}
385
						$return[$key]->next_page = $this->build_page_handle( $last_post, $query );
386
					}
387
				}
388
389
				if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
390
					if ( !isset( $return[$key] ) )
391
						$return[$key] = new stdClass;
392
					$return[$key]->wpcom = true;
393
				}
394
395
				break;
396
			}
397
		}
398
399
		$return['found'] -= $excluded_count;
400
401
		return $return;
402
	}
403
404 View Code Duplication
	function build_page_handle( $post, $query ) {
405
		$column = $query['orderby'];
406
		if ( ! $column ) {
407
			$column = 'date';
408
		}
409
		return build_query( array( 'value' => urlencode($post[$column]), 'id' => $post['ID'] ) );
410
	}
411
412 View Code Duplication
	function _build_date_range_query( $column, $range, $where ) {
413
		global $wpdb;
414
415
		switch ( count( $range ) ) {
416
			case 2 :
417
				$where .= $wpdb->prepare(
418
					" AND `$wpdb->posts`.$column >= CAST( %s AS DATETIME ) AND `$wpdb->posts`.$column < CAST( %s AS DATETIME ) ",
419
					$range['after'],
420
					$range['before']
421
				);
422
				break;
423
			case 1 :
424
				if ( isset( $range['before'] ) ) {
425
					$where .= $wpdb->prepare(
426
						" AND `$wpdb->posts`.$column < CAST( %s AS DATETIME ) ",
427
						$range['before']
428
					);
429
				} else {
430
					$where .= $wpdb->prepare(
431
						" AND `$wpdb->posts`.$column > CAST( %s AS DATETIME ) ",
432
						$range['after']
433
					);
434
				}
435
				break;
436
		}
437
438
		return $where;
439
	}
440
441
	function handle_date_range( $where ) {
442
		return $this->_build_date_range_query( 'post_date_gmt', $this->date_range, $where );
443
	}
444
445
	function handle_modified_range( $where ) {
446
		return $this->_build_date_range_query( 'post_modified_gmt', $this->modified_range, $where );
447
	}
448
449 View Code Duplication
	function handle_where_for_page_handle( $where ) {
450
		global $wpdb;
451
452
		$column = $this->performed_query['orderby'];
453
		if ( ! $column ) {
454
			$column = 'date';
455
		}
456
		$order = $this->performed_query['order'];
457
		if ( ! $order ) {
458
			$order = 'DESC';
459
		}
460
461
		if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
462
			return $where;
463
		}
464
465
		if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
466
			return $where;
467
		}
468
469
		$db_column = '';
470
		$db_value = '';
471
		switch( $column ) {
472
			case 'ID':
473
				$db_column = 'ID';
474
				$db_value = '%d';
475
				break;
476
			case 'title':
477
				$db_column = 'post_title';
478
				$db_value = '%s';
479
				break;
480
			case 'date':
481
				$db_column = 'post_date';
482
				$db_value = 'CAST( %s as DATETIME )';
483
				break;
484
			case 'modified':
485
				$db_column = 'post_modified';
486
				$db_value = 'CAST( %s as DATETIME )';
487
				break;
488
			case 'comment_count':
489
				$db_column = 'comment_count';
490
				$db_value = '%d';
491
				break;
492
		}
493
494
		if ( 'DESC'=== $order ) {
495
			$db_order = '<';
496
		} else {
497
			$db_order = '>';
498
		}
499
500
		// Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
501
		// but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
502
		// beyond the passed item.
503
		$where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
504
		if ( $db_column !== 'ID' ) {
505
			$where .= $wpdb->prepare( "OR ( `$wpdb->posts`.`$db_column` = $db_value AND `$wpdb->posts`.ID $db_order %d )", $this->page_handle['value'], $this->page_handle['id'] );
506
		}
507
		$where .= ' )';
508
509
		return $where;
510
	}
511
512 View Code Duplication
	function handle_orderby_for_page_handle( $orderby ) {
513
		global $wpdb;
514
		if ( $this->performed_query['orderby'] === 'ID' ) {
515
			// bail if we're already ordering by ID
516
			return $orderby;
517
		}
518
519
		if ( $orderby ) {
520
			$orderby .= ' ,';
521
		}
522
		$order = $this->performed_query['order'];
523
		if ( ! $order ) {
524
			$order = 'DESC';
525
		}
526
		$orderby .= " `$wpdb->posts`.ID $order";
527
		return $orderby;
528
	}
529
}
530