Completed
Push — update/comments-ignore-author-... ( 4f8a13...0fe3a8 )
by
unknown
11:38
created

class.wpcom-json-api-update-post-v1-1-endpoint.php (3 issues)

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
class WPCOM_JSON_API_Update_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
3
	function __construct( $args ) {
4
		parent::__construct( $args );
5
		if ( $this->api->ends_with( $this->path, '/delete' ) ) {
6
			$this->post_object_format['status']['deleted'] = 'The post has been deleted permanently.';
7
		}
8
	}
9
10
	// /sites/%s/posts/new       -> $blog_id
11
	// /sites/%s/posts/%d        -> $blog_id, $post_id
12
	// /sites/%s/posts/%d/delete -> $blog_id, $post_id
13
	// /sites/%s/posts/%d/restore -> $blog_id, $post_id
14 View Code Duplication
	function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
15
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
16
		if ( is_wp_error( $blog_id ) ) {
17
			return $blog_id;
18
		}
19
20
		if ( $this->api->ends_with( $path, '/delete' ) ) {
21
			return $this->delete_post( $path, $blog_id, $post_id );
22
		} elseif ( $this->api->ends_with( $path, '/restore' ) ) {
23
			return $this->restore_post( $path, $blog_id, $post_id );
24
		} else {
25
			return $this->write_post( $path, $blog_id, $post_id );
26
		}
27
	}
28
29
	// /sites/%s/posts/new       -> $blog_id
30
	// /sites/%s/posts/%d        -> $blog_id, $post_id
31
	function write_post( $path, $blog_id, $post_id ) {
32
		$new  = $this->api->ends_with( $path, '/new' );
33
		$args = $this->query_args();
34
35
		// unhook publicize, it's hooked again later -- without this, skipping services is impossible
36 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
37
			remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100, 2 );
38
			add_action( 'rest_api_inserted_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ) );
39
		}
40
41
		// 'future' is an alias for 'publish' for now
42
		if ( 'future' === $input['status'] ) {
43
			$input['status'] = 'publish';
44
		}
45
46 View Code Duplication
		if ( $new ) {
47
			$input = $this->input( true );
48
49
			if ( 'revision' === $input['type'] ) {
50
				if ( ! isset( $input['parent'] ) ) {
51
					return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
52
				}
53
				$input['status'] = 'inherit'; // force inherit for revision type
54
				$input['slug'] = $input['parent'] . '-autosave-v1';
55
			}
56
			elseif ( !isset( $input['title'] ) && !isset( $input['content'] ) && !isset( $input['excerpt'] ) ) {
57
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
58
			}
59
60
			// default to post
61
			if ( empty( $input['type'] ) )
62
				$input['type'] = 'post';
63
64
			$post_type = get_post_type_object( $input['type'] );
65
66
			if ( ! $this->is_post_type_allowed( $input['type'] ) ) {
67
				return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
68
			}
69
70
			if ( ! empty( $input['author'] ) ) {
71
				$author_id = $this->parse_and_set_author( $input['author'], $input['type'] );
72
				unset( $input['author'] );
73
				if ( is_wp_error( $author_id ) )
74
					return $author_id;
75
			}
76
77
			if ( 'publish' === $input['status'] ) {
78
				if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
79
					if ( current_user_can( $post_type->cap->edit_posts ) ) {
80
						$input['status'] = 'pending';
81
					} else {
82
						return new WP_Error( 'unauthorized', 'User cannot publish posts', 403 );
83
					}
84
				}
85
			} else {
86
				if ( !current_user_can( $post_type->cap->edit_posts ) ) {
87
					return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 );
88
				}
89
			}
90
		} else {
91
			$input = $this->input( false );
92
93
			if ( !is_array( $input ) || !$input ) {
94
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
95
			}
96
97
			$post = get_post( $post_id );
98
			$_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
99
			$post_type = get_post_type_object( $_post_type );
100
			if ( !$post || is_wp_error( $post ) ) {
101
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
102
			}
103
104
			if ( !current_user_can( 'edit_post', $post->ID ) ) {
105
				return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
106
			}
107
108
			if ( ! empty( $input['author'] ) ) {
109
				$author_id = $this->parse_and_set_author( $input['author'], $_post_type );
110
				unset( $input['author'] );
111
				if ( is_wp_error( $author_id ) )
112
					return $author_id;
113
			}
114
115
			if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) {
116
				$input['status'] = 'pending';
117
			}
118
			$last_status = $post->post_status;
119
			$new_status = isset( $input['status'] ) ? $input['status'] : $last_status;
120
121
			// Make sure that drafts get the current date when transitioning to publish if not supplied in the post.
122
			$date_in_past = ( strtotime($post->post_date_gmt) < time() );
123
			if ( 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past ) {
124
				$input['date_gmt'] = gmdate( 'Y-m-d H:i:s' );
125
			}
126
		}
127
128
		// If date is set, $this->input will set date_gmt, date still needs to be adjusted for the blog's offset
129 View Code Duplication
		if ( isset( $input['date_gmt'] ) ) {
130
			$gmt_offset = get_option( 'gmt_offset' );
131
			$time_with_offset = strtotime( $input['date_gmt'] ) + $gmt_offset * HOUR_IN_SECONDS;
132
			$input['date'] = date( 'Y-m-d H:i:s', $time_with_offset );
133
		}
134
135 View Code Duplication
		if ( ! empty( $author_id ) && get_current_user_id() != $author_id ) {
136
			if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
137
				return new WP_Error( 'unauthorized', "User is not allowed to publish others' posts.", 403 );
138
			} elseif ( ! user_can( $author_id, $post_type->cap->edit_posts ) ) {
139
				return new WP_Error( 'unauthorized', 'Assigned author cannot publish post.', 403 );
140
			}
141
		}
142
143
		if ( !is_post_type_hierarchical( $post_type->name ) && 'revision' !== $post_type->name ) {
144
			unset( $input['parent'] );
145
		}
146
147
		$tax_input = array();
148
149 View Code Duplication
		foreach ( array( 'categories' => 'category', 'tags' => 'post_tag' ) as $key => $taxonomy ) {
150
			if ( ! isset( $input[ $key ] ) ) {
151
				continue;
152
			}
153
154
			$tax_input[ $taxonomy ] = array();
155
156
			$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
157
158
			if ( is_array( $input[$key] ) ) {
159
				$terms = $input[$key];
160
			} else {
161
				$terms = explode( ',', $input[$key] );
162
			}
163
164
			foreach ( $terms as $term ) {
165
				/**
166
				 * `curl --data 'category[]=123'` should be interpreted as a category ID,
167
				 * not a category whose name is '123'.
168
				 *
169
				 * Consequence: To add a category/tag whose name is '123', the client must
170
				 * first look up its ID.
171
				 */
172
				if ( ctype_digit( $term ) ) {
173
					$term = (int) $term;
174
				}
175
176
				$term_info = term_exists( $term, $taxonomy );
177
178
				if ( ! $term_info ) {
179
					// A term ID that doesn't already exist. Ignore it: we don't know what name to give it.
180
					if ( is_int( $term ) ){
181
						continue;
182
					}
183
					// only add a new tag/cat if the user has access to
184
					$tax = get_taxonomy( $taxonomy );
185
186
					// see https://core.trac.wordpress.org/ticket/26409
187
					if ( 'category' === $taxonomy && ! current_user_can( $tax->cap->edit_terms ) ) {
188
						continue;
189
					} else if ( ! current_user_can( $tax->cap->assign_terms ) ) {
190
						continue;
191
					}
192
193
					$term_info = wp_insert_term( $term, $taxonomy );
194
				}
195
196
				if ( ! is_wp_error( $term_info ) ) {
197
					if ( $is_hierarchical ) {
198
						// Categories must be added by ID
199
						$tax_input[$taxonomy][] = (int) $term_info['term_id'];
200
					} else {
201
						// Tags must be added by name
202
						if ( is_int( $term ) ) {
203
							$term = get_term( $term, $taxonomy );
204
							$tax_input[$taxonomy][] = $term->name;
205
						} else {
206
							$tax_input[$taxonomy][] = $term;
207
						}
208
					}
209
				}
210
			}
211
		}
212
213 View Code Duplication
		if ( isset( $input['categories'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) {
214
			$tax_input['category'][] = get_option( 'default_category' );
215
		}
216
217
		unset( $input['tags'], $input['categories'] );
218
219
		$insert = array();
220
221 View Code Duplication
		if ( !empty( $input['slug'] ) ) {
222
			$insert['post_name'] = $input['slug'];
223
			unset( $input['slug'] );
224
		}
225
226 View Code Duplication
		if ( isset( $input['discussion'] ) ) {
227
			$discussion = (array) $input['discussion'];
228
			foreach ( array( 'comment', 'ping' ) as $discussion_type ) {
229
				$discussion_open = sprintf( '%ss_open', $discussion_type );
230
				$discussion_status = sprintf( '%s_status', $discussion_type );
231
232
				if ( isset( $discussion[ $discussion_open ] ) ) {
233
					$is_open = WPCOM_JSON_API::is_truthy( $discussion[ $discussion_open ] );
234
 					$discussion[ $discussion_status ] = $is_open ? 'open' : 'closed';
235
				}
236
237
				if ( in_array( $discussion[ $discussion_status ], array( 'open', 'closed' ) ) ) {
238
					$insert[ $discussion_status ] = $discussion[ $discussion_status ];
239
				}
240
			}
241
		}
242
243
		unset( $input['discussion'] );
244
245 View Code Duplication
		if ( isset( $input['menu_order'] ) ) {
246
			$insert['menu_order'] = $input['menu_order'];
247
			unset( $input['menu_order'] );
248
		}
249
250
		$publicize = isset( $input['publicize'] ) ? $input['publicize'] : null;
251
		unset( $input['publicize'] );
252
253
		$publicize_custom_message = isset( $input['publicize_message'] ) ? $input['publicize_message'] : null;
254
		unset( $input['publicize_message'] );
255
256 View Code Duplication
		if ( isset( $input['featured_image'] ) ) {
257
			$featured_image = trim( $input['featured_image'] );
258
			$delete_featured_image = empty( $featured_image );
259
			unset( $input['featured_image'] );
260
		}
261
262
		$metadata = isset( $input['metadata'] ) ? $input['metadata'] : null;
263
		unset( $input['metadata'] );
264
265
		$likes = isset( $input['likes_enabled'] ) ? $input['likes_enabled'] : null;
266
		unset( $input['likes_enabled'] );
267
268
		$sharing = isset( $input['sharing_enabled'] ) ? $input['sharing_enabled'] : null;
269
		unset( $input['sharing_enabled'] );
270
271
		$sticky = isset( $input['sticky'] ) ? $input['sticky'] : null;
272
		unset( $input['sticky'] );
273
274
		foreach ( $input as $key => $value ) {
275
			$insert["post_$key"] = $value;
276
		}
277
278
		if ( ! empty( $author_id ) ) {
279
			$insert['post_author'] = absint( $author_id );
280
		}
281
282
		if ( ! empty( $tax_input ) ) {
283
			$insert['tax_input'] = $tax_input;
284
		}
285
286
		$has_media = ! empty( $input['media'] ) ? count( $input['media'] ) : false;
287
		$has_media_by_url = ! empty( $input['media_urls'] ) ? count( $input['media_urls'] ) : false;
288
289 View Code Duplication
		if ( $new ) {
290
291
			if ( isset( $input['content'] ) && ! has_shortcode( $input['content'], 'gallery' ) && ( $has_media || $has_media_by_url ) ) {
292
				switch ( ( $has_media + $has_media_by_url ) ) {
293
				case 0 :
294
					// No images - do nothing.
295
					break;
296
				case 1 :
297
					// 1 image - make it big
298
					$insert['post_content'] = $input['content'] = "[gallery size=full columns=1]\n\n" . $input['content'];
299
					break;
300
				default :
301
					// Several images - 3 column gallery
302
					$insert['post_content'] = $input['content'] = "[gallery]\n\n" . $input['content'];
303
					break;
304
				}
305
			}
306
307
			$post_id = wp_insert_post( add_magic_quotes( $insert ), true );
308
		} else {
309
			$insert['ID'] = $post->ID;
310
311
			// wp_update_post ignores date unless edit_date is set
312
			// See: http://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts
313
			// See: https://core.trac.wordpress.org/browser/tags/3.9.2/src/wp-includes/post.php#L3302
314
			if ( isset( $input['date_gmt'] ) || isset( $input['date'] ) ) {
315
				$insert['edit_date'] = true;
316
			}
317
318
			$post_id = wp_update_post( (object) $insert );
319
		}
320
321
322
		if ( !$post_id || is_wp_error( $post_id ) ) {
323
			return $post_id;
324
		}
325
326
		// make sure this post actually exists and is not an error of some kind (ie, trying to load media in the posts endpoint)
327
		$post_check = $this->get_post_by( 'ID', $post_id, $args['context'] );
328
		if ( is_wp_error( $post_check ) ) {
329
			return $post_check;
330
		}
331
332 View Code Duplication
		if ( $has_media || $has_media_by_url ) {
333
			$media_files = ! empty( $input['media'] ) ? $input['media'] : array();
334
			$media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array();
335
			$media_attrs = ! empty( $input['media_attrs'] ) ? $input['media_attrs'] : array();
336
			$force_parent_id = $post_id;
337
			$media_results = $this->handle_media_creation_v1_1( $media_files, $media_urls, $media_attrs, $force_parent_id );
338
		}
339
340
		// set page template for this post..
341 View Code Duplication
		if ( isset( $input['page_template'] ) && 'page' == $post_type->name ) {
342
			$page_template = $input['page_template'];
343
			$page_templates = wp_get_theme()->get_page_templates( get_post( $post_id ) );
344
			if ( empty( $page_template ) || 'default' == $page_template || isset( $page_templates[ $page_template ] ) ) {
345
				update_post_meta( $post_id, '_wp_page_template', $page_template );
346
			}
347
		}
348
349
		// Set like status for the post
350
		/** This filter is documented in modules/likes.php */
351
		$sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
352 View Code Duplication
		if ( $new ) {
353
			if ( $sitewide_likes_enabled ) {
354
				if ( false === $likes ) {
355
					update_post_meta( $post_id, 'switch_like_status', 1 );
356
				} else {
357
					delete_post_meta( $post_id, 'switch_like_status' );
358
				}
359
			} else {
360
				if ( $likes ) {
361
					update_post_meta( $post_id, 'switch_like_status', 1 );
362
				} else {
363
					delete_post_meta( $post_id, 'switch_like_status' );
364
				}
365
			}
366
		} else {
367
			if ( isset( $likes ) ) {
368
				if ( $sitewide_likes_enabled ) {
369
					if ( false === $likes ) {
370
						update_post_meta( $post_id, 'switch_like_status', 1 );
371
					} else {
372
						delete_post_meta( $post_id, 'switch_like_status' );
373
					}
374
				} else {
375
					if ( true === $likes ) {
376
						update_post_meta( $post_id, 'switch_like_status', 1 );
377
					} else {
378
						delete_post_meta( $post_id, 'switch_like_status' );
379
					}
380
				}
381
			}
382
		}
383
384
		// Set sharing status of the post
385 View Code Duplication
		if ( $new ) {
386
			$sharing_enabled = isset( $sharing ) ? (bool) $sharing : true;
387
			if ( false === $sharing_enabled ) {
388
				update_post_meta( $post_id, 'sharing_disabled', 1 );
389
			}
390
		}
391
		else {
392
			if ( isset( $sharing ) && true === $sharing ) {
393
				delete_post_meta( $post_id, 'sharing_disabled' );
394
			} else if ( isset( $sharing ) && false == $sharing ) {
395
				update_post_meta( $post_id, 'sharing_disabled', 1 );
396
			}
397
		}
398
399
		if ( isset( $sticky ) ) {
400
			if ( true === $sticky ) {
401
				stick_post( $post_id );
402
			} else {
403
				unstick_post( $post_id );
404
			}
405
		}
406
407
		// WPCOM Specific (Jetpack's will get bumped elsewhere
408
		// Tracks how many posts are published and sets meta
409
		// so we can track some other cool stats (like likes & comments on posts published)
410 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
411
			if (
412
				( $new && 'publish' == $input['status'] )
413
				|| (
414
					! $new && isset( $last_status )
415
					&& 'publish' != $last_status
416
					&& isset( $new_status )
417
					&& 'publish' == $new_status
418
				)
419
			) {
420
				/** This action is documented in modules/widgets/social-media-icons.php */
421
				do_action( 'jetpack_bump_stats_extras', 'api-insights-posts', $this->api->token_details['client_id'] );
422
				update_post_meta( $post_id, '_rest_api_published', 1 );
423
				update_post_meta( $post_id, '_rest_api_client_id', $this->api->token_details['client_id'] );
424
			}
425
		}
426
427
428
		// We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
429
		// to instead flag the ones we don't want to be skipped. proceed with said logic.
430
		// any posts coming from Path (client ID 25952) should also not publicize
431 View Code Duplication
		if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
432
			// No publicize at all, skip all by ID
433
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
434
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
435
				$service_connections   = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
436
				if ( ! $service_connections ) {
437
					continue;
438
				}
439
				foreach ( $service_connections as $service_connection ) {
440
					update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
441
				}
442
			}
443
		} else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
444
			foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
445
				/*
446
				 * We support both indexed and associative arrays:
447
				 * * indexed are to pass entire services
448
				 * * associative are to pass specific connections per service
449
				 *
450
				 * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
451
				 *
452
				 * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
453
				 * 		Form data: publicize[]=twitter&publicize[]=facebook
454
				 * EG: array( 'twitter' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3', 'facebook' => (int) $pub_conn_id_7 ) will publicize to two Twitter accounts, and one Facebook connection, of potentially many.
455
				 * 		Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
456
				 * EG: array( 'twitter', 'facebook' => '(int) $pub_conn_id_0, (int) $pub_conn_id_3' ) will publicize to all available Twitter accounts, but only 2 of potentially many Facebook connections
457
				 * 		Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
458
				 */
459
460
				// Delete any stale SKIP value for the service by name. We'll add it back by ID.
461
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
462
463
				// Get the user's connections
464
				$service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
465
466
				// if the user doesn't have any connections for this service, move on
467
				if ( ! $service_connections ) {
468
					continue;
469
				}
470
471
				if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
472
					// Skip the whole service by adding each connection ID
473
					foreach ( $service_connections as $service_connection ) {
474
						update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
475
					}
476
				} else if ( !empty( $publicize[ $name ] ) ) {
477
					// Seems we're being asked to only push to [a] specific connection[s].
478
					// Explode the list on commas, which will also support a single passed ID
479
					$requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
480
481
					// Flag the connections we can't match with the requested list to be skipped.
482
					foreach ( $service_connections as $service_connection ) {
483
						if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
484
							update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
485
						} else {
486
							delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
487
						}
488
					}
489
				} else {
490
					// delete all SKIP values; it's okay to publish to all connected IDs for this service
491
					foreach ( $service_connections as $service_connection ) {
492
						delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
493
					}
494
				}
495
			}
496
		}
497
498 View Code Duplication
		if ( ! is_null( $publicize_custom_message ) ) {
499
			if ( empty( $publicize_custom_message ) ) {
500
				delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS );
501
			} else {
502
				update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS, trim( $publicize_custom_message ) );
503
			}
504
		}
505
506 View Code Duplication
		if ( ! empty( $insert['post_format'] ) ) {
507
			if ( 'default' !== strtolower( $insert['post_format'] ) ) {
508
				set_post_format( $post_id, $insert['post_format'] );
509
			}
510
			else {
511
				set_post_format( $post_id, get_option( 'default_post_format' ) );
512
			}
513
		}
514
515
		if ( isset( $featured_image ) ) {
516
			$this->parse_and_set_featured_image( $post_id, $delete_featured_image, $featured_image );
517
		}
518
519 View Code Duplication
		if ( ! empty( $metadata ) ) {
520
			foreach ( (array) $metadata as $meta ) {
521
522
				$meta = (object) $meta;
523
524
				$existing_meta_item = new stdClass;
525
526
				if ( empty( $meta->operation ) )
527
					$meta->operation = 'update';
528
529
				if ( ! empty( $meta->value ) ) {
530
					if ( 'true' == $meta->value )
531
						$meta->value = true;
532
					if ( 'false' == $meta->value )
533
						$meta->value = false;
534
				}
535
536
				if ( ! empty( $meta->id ) ) {
537
					$meta->id = absint( $meta->id );
538
					$existing_meta_item = get_metadata_by_mid( 'post', $meta->id );
539
				}
540
541
				$unslashed_meta_key = wp_unslash( $meta->key ); // should match what the final key will be
542
				$meta->key = wp_slash( $meta->key );
543
				$unslashed_existing_meta_key = wp_unslash( $existing_meta_item->meta_key );
544
				$existing_meta_item->meta_key = wp_slash( $existing_meta_item->meta_key );
545
546
				// make sure that the meta id passed matches the existing meta key
547
				if ( ! empty( $meta->id ) && ! empty( $meta->key ) ) {
548
					$meta_by_id = get_metadata_by_mid( 'post', $meta->id );
549
					if ( $meta_by_id->meta_key !== $meta->key ) {
550
						continue; // skip this meta
551
					}
552
				}
553
554
				switch ( $meta->operation ) {
555
					case 'delete':
0 ignored issues
show
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
556
557
						if ( ! empty( $meta->id ) && ! empty( $existing_meta_item->meta_key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_existing_meta_key ) ) {
558
							delete_metadata_by_mid( 'post', $meta->id );
559
						} elseif ( ! empty( $meta->key ) && ! empty( $meta->previous_value ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) {
560
							delete_post_meta( $post_id, $meta->key, $meta->previous_value );
561
						} elseif ( ! empty( $meta->key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) {
562
							delete_post_meta( $post_id, $meta->key );
563
						}
564
565
						break;
566
					case 'add':
0 ignored issues
show
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
567
568
						if ( ! empty( $meta->id ) || ! empty( $meta->previous_value ) ) {
569
							continue;
570
						} elseif ( ! empty( $meta->key ) && ! empty( $meta->value ) && ( current_user_can( 'add_post_meta', $post_id, $unslashed_meta_key ) ) || $this->is_metadata_public( $meta->key ) ) {
571
							add_post_meta( $post_id, $meta->key, $meta->value );
572
						}
573
574
						break;
575
					case 'update':
0 ignored issues
show
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
576
577
						if ( ! isset( $meta->value ) ) {
578
							continue;
579
						} elseif ( ! empty( $meta->id ) && ! empty( $existing_meta_item->meta_key ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_existing_meta_key ) || $this->is_metadata_public( $meta->key ) ) ) {
580
							update_metadata_by_mid( 'post', $meta->id, $meta->value );
581
						} elseif ( ! empty( $meta->key ) && ! empty( $meta->previous_value ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_meta_key ) || $this->is_metadata_public( $meta->key ) ) ) {
582
							update_post_meta( $post_id, $meta->key,$meta->value, $meta->previous_value );
583
						} elseif ( ! empty( $meta->key ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_meta_key ) || $this->is_metadata_public( $meta->key ) ) ) {
584
							update_post_meta( $post_id, $meta->key, $meta->value );
585
						}
586
587
						break;
588
				}
589
590
			}
591
		}
592
593
		/** This action is documented in json-endpoints/class.wpcom-json-api-update-post-endpoint.php */
594
		do_action( 'rest_api_inserted_post', $post_id, $insert, $new );
595
596
		$return = $this->get_post_by( 'ID', $post_id, $args['context'] );
597
		if ( !$return || is_wp_error( $return ) ) {
598
			return $return;
599
		}
600
601 View Code Duplication
		if ( isset( $input['type'] ) && 'revision' === $input['type'] ) {
602
			$return['preview_nonce'] = wp_create_nonce( 'post_preview_' . $input['parent'] );
603
		}
604
605
		if ( isset( $sticky ) ) {
606
			// workaround for sticky test occasionally failing, maybe a race condition with stick_post() above
607
			$return['sticky'] = ( true === $sticky );
608
		}
609
610
		if ( ! empty( $media_results['errors'] ) )
611
			$return['media_errors'] = $media_results['errors'];
612
613 View Code Duplication
		if ( 'publish' !== $post->post_status ) {
614
			$return['other_URLs'] = (object) $this->get_post_permalink_suggestions( $post_id, $input['title'] );
615
		}
616
617
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
618
		do_action( 'wpcom_json_api_objects', 'posts' );
619
620
		return $return;
621
	}
622
623
	// /sites/%s/posts/%d/delete -> $blog_id, $post_id
624 View Code Duplication
	function delete_post( $path, $blog_id, $post_id ) {
625
		$post = get_post( $post_id );
626
		if ( !$post || is_wp_error( $post ) ) {
627
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
628
		}
629
630
		if ( ! $this->is_post_type_allowed( $post->post_type ) ) {
631
			return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
632
		}
633
634
		if ( !current_user_can( 'delete_post', $post->ID ) ) {
635
			return new WP_Error( 'unauthorized', 'User cannot delete posts', 403 );
636
		}
637
638
		$args  = $this->query_args();
639
		$return = $this->get_post_by( 'ID', $post->ID, $args['context'] );
640
		if ( !$return || is_wp_error( $return ) ) {
641
			return $return;
642
		}
643
644
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
645
		do_action( 'wpcom_json_api_objects', 'posts' );
646
647
		wp_delete_post( $post->ID );
648
649
		$status = get_post_status( $post->ID );
650
		if ( false === $status ) {
651
			$return['status'] = 'deleted';
652
			return $return;
653
		}
654
655
		return $this->get_post_by( 'ID', $post->ID, $args['context'] );
656
	}
657
658
	// /sites/%s/posts/%d/restore -> $blog_id, $post_id
659 View Code Duplication
	function restore_post( $path, $blog_id, $post_id ) {
660
		$args  = $this->query_args();
661
		$post = get_post( $post_id );
662
663
		if ( !$post || is_wp_error( $post ) ) {
664
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
665
		}
666
667
		if ( !current_user_can( 'delete_post', $post->ID ) ) {
668
			return new WP_Error( 'unauthorized', 'User cannot restore trashed posts', 403 );
669
		}
670
671
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
672
		do_action( 'wpcom_json_api_objects', 'posts' );
673
674
		wp_untrash_post( $post->ID );
675
676
		return $this->get_post_by( 'ID', $post->ID, $args['context'] );
677
	}
678
679 View Code Duplication
	protected function parse_and_set_featured_image( $post_id, $delete_featured_image, $featured_image ) {
680
		if ( $delete_featured_image ) {
681
			delete_post_thumbnail( $post_id );
682
			return;
683
		}
684
685
		$featured_image = (string) $featured_image;
686
687
		// if we got a post ID, we can just set it as the thumbnail
688
		if ( ctype_digit( $featured_image ) && 'attachment' == get_post_type( $featured_image ) ) {
689
			set_post_thumbnail( $post_id, $featured_image );
690
			return $featured_image;
691
		}
692
693
		$featured_image_id = $this->handle_media_sideload( $featured_image, $post_id );
694
695
		if ( empty( $featured_image_id ) || ! is_int( $featured_image_id ) )
696
			return false;
697
698
		set_post_thumbnail( $post_id, $featured_image_id );
699
		return $featured_image_id;
700
	}
701
702 View Code Duplication
	protected function parse_and_set_author( $author = null, $post_type = 'post' ) {
703
		if ( empty( $author ) || ! post_type_supports( $post_type, 'author' ) )
704
			return get_current_user_id();
705
706
		if ( ctype_digit( $author ) ) {
707
			$_user = get_user_by( 'id', $author );
708
			if ( ! $_user || is_wp_error( $_user ) )
709
				return new WP_Error( 'invalid_author', 'Invalid author provided' );
710
711
			return $_user->ID;
712
		}
713
714
		$_user = get_user_by( 'login', $author );
715
		if ( ! $_user || is_wp_error( $_user ) )
716
			return new WP_Error( 'invalid_author', 'Invalid author provided' );
717
718
		return $_user->ID;
719
	}
720
}
721