Completed
Push — master-stable ( 3751a6...e73511 )
by
unknown
40:58 queued 32:02
created

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