/json-endpoints/class.wpcom-json-api-update-post-v1-2-endpoint.php

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