WPCOM_JSON_API_Update_Post_v1_1_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Update Post v1 1 Endpoint class.

Defined (1)

The class is defined in the following location(s).

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