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

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