説明なし

class.wpcom-json-api-update-post-v1-1-endpoint.php 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. <?php
  2. new WPCOM_JSON_API_Update_Post_v1_1_Endpoint( array(
  3. 'description' => 'Create a post.',
  4. 'group' => 'posts',
  5. 'stat' => 'posts:new',
  6. 'new_version' => '1.2',
  7. 'min_version' => '1.1',
  8. 'max_version' => '1.1',
  9. 'method' => 'POST',
  10. 'path' => '/sites/%s/posts/new',
  11. 'path_labels' => array(
  12. '$site' => '(int|string) Site ID or domain',
  13. ),
  14. 'request_format' => array(
  15. // explicitly document all input
  16. 'date' => "(ISO 8601 datetime) The post's creation time.",
  17. 'title' => '(HTML) The post title.',
  18. 'content' => '(HTML) The post content.',
  19. 'excerpt' => '(HTML) An optional post excerpt.',
  20. 'slug' => '(string) The name (slug) for the post, used in URLs.',
  21. 'author' => '(string) The username or ID for the user to assign the post to.',
  22. 'publicize' => '(array|bool) True or false if the post be publicized to external services. An array of services if we only want to publicize to a select few. Defaults to true.',
  23. 'publicize_message' => '(string) Custom message to be publicized to external services.',
  24. 'status' => array(
  25. 'publish' => 'Publish the post.',
  26. 'private' => 'Privately publish the post.',
  27. 'draft' => 'Save the post as a draft.',
  28. 'pending' => 'Mark the post as pending editorial approval.',
  29. 'future' => 'Schedule the post (alias for publish; you must also set a future date).',
  30. 'auto-draft' => 'Save a placeholder for a newly created post, with no content.',
  31. ),
  32. 'sticky' => array(
  33. 'false' => 'Post is not marked as sticky.',
  34. 'true' => 'Stick the post to the front page.',
  35. ),
  36. 'password' => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.',
  37. 'parent' => "(int) The post ID of the new post's parent.",
  38. 'type' => "(string) The post type. Defaults to 'post'. Post types besides post and page need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.",
  39. 'terms' => '(object) Mapping of taxonomy to comma-separated list or array of terms (name or id)',
  40. 'categories' => "(array|string) Comma-separated list or array of categories (name or id)",
  41. 'tags' => "(array|string) Comma-separated list or array of tags (name or id)",
  42. 'format' => array_merge( array( 'default' => 'Use default post format' ), get_post_format_strings() ),
  43. 'featured_image' => "(string) The post ID of an existing attachment to set as the featured image. Pass an empty string to delete the existing image.",
  44. 'media' => "(media) An array of files to attach to the post. To upload media, the entire request should be multipart/form-data encoded. Multiple media items will be displayed in a gallery. Accepts jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. Audio and Video may also be available. See <code>allowed_file_types</code> in the options response of the site endpoint. Errors produced by media uploads, if any, will be in `media_errors` in the response. <br /><br /><strong>Example</strong>:<br />" .
  45. "<code>curl \<br />--form 'title=Image Post' \<br />--form 'media[0]=@/path/to/file.jpg' \<br />--form 'media_attrs[0][caption]=My Great Photo' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'</code>",
  46. 'media_urls' => "(array) An array of URLs for images to attach to a post. Sideloads the media in for a post. Errors produced by media sideloading, if any, will be in `media_errors` in the response.",
  47. 'media_attrs' => "(array) An array of attributes (`title`, `description` and `caption`) are supported to assign to the media uploaded via the `media` or `media_urls` properties. You must use a numeric index for the keys of `media_attrs` which follow the same sequence as `media` and `media_urls`. <br /><br /><strong>Example</strong>:<br />" .
  48. "<code>curl \<br />--form 'title=Gallery Post' \<br />--form 'media[]=@/path/to/file1.jpg' \<br />--form 'media_urls[]=http://exapmple.com/file2.jpg' \<br /> \<br />--form 'media_attrs[0][caption]=This will be the caption for file1.jpg' \<br />--form 'media_attrs[1][title]=This will be the title for file2.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'</code>",
  49. 'metadata' => "(array) Array of metadata objects containing the following properties: `key` (metadata key), `id` (meta ID), `previous_value` (if set, the action will only occur for the provided previous value), `value` (the new value to set the meta to), `operation` (the operation to perform: `update` or `add`; defaults to `update`). All unprotected meta keys are available by default for read requests. Both unprotected and protected meta keys are avaiable for authenticated requests with proper capabilities. Protected meta keys can be made available with the <code>rest_api_allowed_public_metadata</code> filter.",
  50. 'discussion' => '(object) A hash containing one or more of the following boolean values, which default to the blog\'s discussion preferences: `comments_open`, `pings_open`',
  51. 'likes_enabled' => "(bool) Should the post be open to likes? Defaults to the blog's preference.",
  52. 'sharing_enabled' => "(bool) Should sharing buttons show on this post? Defaults to true.",
  53. 'menu_order' => "(int) (Pages Only) the order pages should appear in. Use 0 to maintain alphabetical order.",
  54. 'page_template' => '(string) (Pages Only) The page template this page should use.',
  55. ),
  56. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/new/',
  57. 'example_request_data' => array(
  58. 'headers' => array(
  59. 'authorization' => 'Bearer YOUR_API_TOKEN'
  60. ),
  61. 'body' => array(
  62. 'title' => 'Hello World',
  63. 'content' => 'Hello. I am a test post. I was created by the API',
  64. 'tags' => 'tests',
  65. 'categories' => 'API'
  66. )
  67. )
  68. ) );
  69. new WPCOM_JSON_API_Update_Post_v1_1_Endpoint( array(
  70. 'description' => 'Edit a post.',
  71. 'group' => 'posts',
  72. 'stat' => 'posts:1:POST',
  73. 'new_version' => '1.2',
  74. 'min_version' => '1.1',
  75. 'max_version' => '1.1',
  76. 'method' => 'POST',
  77. 'path' => '/sites/%s/posts/%d',
  78. 'path_labels' => array(
  79. '$site' => '(int|string) Site ID or domain',
  80. '$post_ID' => '(int) The post ID',
  81. ),
  82. 'request_format' => array(
  83. 'date' => "(ISO 8601 datetime) The post's creation time.",
  84. 'title' => '(HTML) The post title.',
  85. 'content' => '(HTML) The post content.',
  86. 'excerpt' => '(HTML) An optional post excerpt.',
  87. 'slug' => '(string) The name (slug) for the post, used in URLs.',
  88. 'author' => '(string) The username or ID for the user to assign the post to.',
  89. 'publicize' => '(array|bool) True or false if the post be publicized to external services. An array of services if we only want to publicize to a select few. Defaults to true.',
  90. 'publicize_message' => '(string) Custom message to be publicized to external services.',
  91. 'status' => array(
  92. 'publish' => 'Publish the post.',
  93. 'private' => 'Privately publish the post.',
  94. 'draft' => 'Save the post as a draft.',
  95. 'future' => 'Schedule the post (alias for publish; you must also set a future date).',
  96. 'pending' => 'Mark the post as pending editorial approval.',
  97. 'trash' => 'Set the post as trashed.',
  98. ),
  99. 'sticky' => array(
  100. 'false' => 'Post is not marked as sticky.',
  101. 'true' => 'Stick the post to the front page.',
  102. ),
  103. 'password' => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.',
  104. 'parent' => "(int) The post ID of the new post's parent.",
  105. 'terms' => '(object) Mapping of taxonomy to comma-separated list or array of terms (name or id)',
  106. 'categories' => "(array|string) Comma-separated list or array of categories (name or id)",
  107. 'tags' => "(array|string) Comma-separated list or array of tags (name or id)",
  108. 'format' => array_merge( array( 'default' => 'Use default post format' ), get_post_format_strings() ),
  109. 'discussion' => '(object) A hash containing one or more of the following boolean values, which default to the blog\'s discussion preferences: `comments_open`, `pings_open`',
  110. 'likes_enabled' => "(bool) Should the post be open to likes?",
  111. 'menu_order' => "(int) (Pages only) the order pages should appear in. Use 0 to maintain alphabetical order.",
  112. 'page_template' => '(string) (Pages Only) The page template this page should use.',
  113. 'sharing_enabled' => "(bool) Should sharing buttons show on this post?",
  114. 'featured_image' => "(string) The post ID of an existing attachment to set as the featured image. Pass an empty string to delete the existing image.",
  115. 'media' => "(media) An array of files to attach to the post. To upload media, the entire request should be multipart/form-data encoded. Multiple media items will be displayed in a gallery. Accepts jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. Audio and Video may also be available. See <code>allowed_file_types</code> in the options resposne of the site endpoint. <br /><br /><strong>Example</strong>:<br />" .
  116. "<code>curl \<br />--form 'title=Image' \<br />--form 'media[]=@/path/to/file.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'</code>",
  117. 'media_urls' => "(array) An array of URLs for images to attach to a post. Sideloads the media in for a post.",
  118. 'metadata' => "(array) Array of metadata objects containing the following properties: `key` (metadata key), `id` (meta ID), `previous_value` (if set, the action will only occur for the provided previous value), `value` (the new value to set the meta to), `operation` (the operation to perform: `update` or `add`; defaults to `update`). All unprotected meta keys are available by default for read requests. Both unprotected and protected meta keys are available for authenticated requests with proper capabilities. Protected meta keys can be made available with the <code>rest_api_allowed_public_metadata</code> filter.",
  119. ),
  120. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/881',
  121. 'example_request_data' => array(
  122. 'headers' => array(
  123. 'authorization' => 'Bearer YOUR_API_TOKEN'
  124. ),
  125. 'body' => array(
  126. 'title' => 'Hello World (Again)',
  127. 'content' => 'Hello. I am an edited post. I was edited by the API',
  128. 'tags' => 'tests',
  129. 'categories' => 'API'
  130. )
  131. )
  132. ) );
  133. new WPCOM_JSON_API_Update_Post_v1_1_Endpoint( array(
  134. 'description' => 'Delete a post. Note: If the trash is enabled, this request will send the post to the trash. A second request will permanently delete the post.',
  135. 'group' => 'posts',
  136. 'stat' => 'posts:1:delete',
  137. 'min_version' => '1.1',
  138. 'max_version' => '1.1',
  139. 'method' => 'POST',
  140. 'path' => '/sites/%s/posts/%d/delete',
  141. 'path_labels' => array(
  142. '$site' => '(int|string) Site ID or domain',
  143. '$post_ID' => '(int) The post ID',
  144. ),
  145. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/$post_ID/delete/',
  146. 'example_request_data' => array(
  147. 'headers' => array(
  148. 'authorization' => 'Bearer YOUR_API_TOKEN'
  149. )
  150. )
  151. ) );
  152. new WPCOM_JSON_API_Update_Post_v1_1_Endpoint( array(
  153. 'description' => 'Restore a post or page from the trash to its previous status.',
  154. 'group' => 'posts',
  155. 'stat' => 'posts:1:restore',
  156. 'min_version' => '1.1',
  157. 'max_version' => '1.1',
  158. 'method' => 'POST',
  159. 'path' => '/sites/%s/posts/%d/restore',
  160. 'path_labels' => array(
  161. '$site' => '(int|string) Site ID or domain',
  162. '$post_ID' => '(int) The post ID',
  163. ),
  164. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/$post_ID/restore/',
  165. 'example_request_data' => array(
  166. 'headers' => array(
  167. 'authorization' => 'Bearer YOUR_API_TOKEN'
  168. )
  169. )
  170. ) );
  171. class WPCOM_JSON_API_Update_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
  172. function __construct( $args ) {
  173. parent::__construct( $args );
  174. if ( $this->api->ends_with( $this->path, '/delete' ) ) {
  175. $this->post_object_format['status']['deleted'] = 'The post has been deleted permanently.';
  176. }
  177. }
  178. // /sites/%s/posts/new -> $blog_id
  179. // /sites/%s/posts/%d -> $blog_id, $post_id
  180. // /sites/%s/posts/%d/delete -> $blog_id, $post_id
  181. // /sites/%s/posts/%d/restore -> $blog_id, $post_id
  182. function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
  183. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  184. if ( is_wp_error( $blog_id ) ) {
  185. return $blog_id;
  186. }
  187. if ( $this->api->ends_with( $path, '/delete' ) ) {
  188. return $this->delete_post( $path, $blog_id, $post_id );
  189. } elseif ( $this->api->ends_with( $path, '/restore' ) ) {
  190. return $this->restore_post( $path, $blog_id, $post_id );
  191. } else {
  192. return $this->write_post( $path, $blog_id, $post_id );
  193. }
  194. }
  195. // /sites/%s/posts/new -> $blog_id
  196. // /sites/%s/posts/%d -> $blog_id, $post_id
  197. function write_post( $path, $blog_id, $post_id ) {
  198. global $wpdb;
  199. $new = $this->api->ends_with( $path, '/new' );
  200. $args = $this->query_args();
  201. // unhook publicize, it's hooked again later -- without this, skipping services is impossible
  202. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  203. remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100, 2 );
  204. add_action( 'rest_api_inserted_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ) );
  205. if ( $this->should_load_theme_functions( $post_id ) ) {
  206. $this->load_theme_functions();
  207. }
  208. }
  209. if ( $new ) {
  210. $input = $this->input( true );
  211. // 'future' is an alias for 'publish' for now
  212. if ( 'future' === $input['status'] ) {
  213. $input['status'] = 'publish';
  214. }
  215. if ( 'revision' === $input['type'] ) {
  216. if ( ! isset( $input['parent'] ) ) {
  217. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  218. }
  219. $input['status'] = 'inherit'; // force inherit for revision type
  220. $input['slug'] = $input['parent'] . '-autosave-v1';
  221. }
  222. elseif ( !isset( $input['title'] ) && !isset( $input['content'] ) && !isset( $input['excerpt'] ) ) {
  223. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  224. }
  225. // default to post
  226. if ( empty( $input['type'] ) )
  227. $input['type'] = 'post';
  228. $post_type = get_post_type_object( $input['type'] );
  229. if ( ! $this->is_post_type_allowed( $input['type'] ) ) {
  230. return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
  231. }
  232. if ( ! empty( $input['author'] ) ) {
  233. $author_id = $this->parse_and_set_author( $input['author'], $input['type'] );
  234. unset( $input['author'] );
  235. if ( is_wp_error( $author_id ) )
  236. return $author_id;
  237. }
  238. if ( 'publish' === $input['status'] ) {
  239. if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
  240. if ( current_user_can( $post_type->cap->edit_posts ) ) {
  241. $input['status'] = 'pending';
  242. } else {
  243. return new WP_Error( 'unauthorized', 'User cannot publish posts', 403 );
  244. }
  245. }
  246. } else {
  247. if ( !current_user_can( $post_type->cap->edit_posts ) ) {
  248. return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 );
  249. }
  250. }
  251. } else {
  252. $input = $this->input( false );
  253. if ( !is_array( $input ) || !$input ) {
  254. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  255. }
  256. if ( isset( $input['status'] ) && 'trash' === $input['status'] && ! current_user_can( 'delete_post', $post_id ) ) {
  257. return new WP_Error( 'unauthorized', 'User cannot delete post', 403 );
  258. }
  259. // 'future' is an alias for 'publish' for now
  260. if ( isset( $input['status'] ) && 'future' === $input['status'] ) {
  261. $input['status'] = 'publish';
  262. }
  263. $post = get_post( $post_id );
  264. $_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
  265. $post_type = get_post_type_object( $_post_type );
  266. if ( !$post || is_wp_error( $post ) ) {
  267. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  268. }
  269. if ( !current_user_can( 'edit_post', $post->ID ) ) {
  270. return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
  271. }
  272. if ( ! empty( $input['author'] ) ) {
  273. $author_id = $this->parse_and_set_author( $input['author'], $_post_type );
  274. unset( $input['author'] );
  275. if ( is_wp_error( $author_id ) )
  276. return $author_id;
  277. }
  278. if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) {
  279. $input['status'] = 'pending';
  280. }
  281. $last_status = $post->post_status;
  282. $new_status = isset( $input['status'] ) ? $input['status'] : $last_status;
  283. // Make sure that drafts get the current date when transitioning to publish if not supplied in the post.
  284. // Similarly, scheduled posts that are manually published before their scheduled date should have the date reset.
  285. $date_in_past = ( strtotime($post->post_date_gmt) < time() );
  286. $reset_draft_date = 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past;
  287. $reset_scheduled_date = 'publish' === $new_status && 'future' === $last_status && ! isset( $input['date_gmt'] ) && ! $date_in_past;
  288. if ( $reset_draft_date || $reset_scheduled_date ) {
  289. $input['date_gmt'] = gmdate( 'Y-m-d H:i:s' );
  290. }
  291. // Untrash a post so that the proper hooks get called as well as the comments get untrashed.
  292. if ( $this->should_untrash_post( $last_status, $new_status, $post ) ) {
  293. $input = $this->untrash_post( $post, $input );
  294. }
  295. }
  296. if ( function_exists( 'wpcom_switch_to_blog_locale' ) ) {
  297. // fixes calypso-pre-oss #12476: respect blog locale when creating the post slug
  298. wpcom_switch_to_blog_locale( $blog_id );
  299. }
  300. // If date was set, $this->input will set date_gmt, date still needs to be adjusted for the blog's offset
  301. if ( isset( $input['date_gmt'] ) ) {
  302. $gmt_offset = get_option( 'gmt_offset' );
  303. $time_with_offset = strtotime( $input['date_gmt'] ) + $gmt_offset * HOUR_IN_SECONDS;
  304. $input['date'] = date( 'Y-m-d H:i:s', $time_with_offset );
  305. }
  306. if ( ! empty( $author_id ) && get_current_user_id() != $author_id ) {
  307. if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
  308. return new WP_Error( 'unauthorized', "User is not allowed to publish others' posts.", 403 );
  309. } elseif ( ! user_can( $author_id, $post_type->cap->edit_posts ) ) {
  310. return new WP_Error( 'unauthorized', 'Assigned author cannot publish post.', 403 );
  311. }
  312. }
  313. if ( !is_post_type_hierarchical( $post_type->name ) && 'revision' !== $post_type->name ) {
  314. unset( $input['parent'] );
  315. }
  316. $input['terms'] = isset( $input['terms'] ) ? (array) $input['terms'] : array();
  317. // Convert comma-separated terms to array before attempting to
  318. // merge with hardcoded taxonomies
  319. foreach ( $input['terms'] as $taxonomy => $terms ) {
  320. if ( is_string( $terms ) ) {
  321. $input['terms'][ $taxonomy ] = explode( ',', $terms );
  322. } else if ( ! is_array( $terms ) ) {
  323. $input['terms'][ $taxonomy ] = array();
  324. }
  325. }
  326. // For each hard-coded taxonomy, merge into terms object
  327. foreach ( array( 'categories' => 'category', 'tags' => 'post_tag' ) as $taxonomy_key => $taxonomy ) {
  328. if ( ! isset( $input[ $taxonomy_key ] ) ) {
  329. continue;
  330. }
  331. if ( ! isset( $input['terms'][ $taxonomy ] ) ) {
  332. $input['terms'][ $taxonomy ] = array();
  333. }
  334. $terms = $input[ $taxonomy_key ];
  335. if ( is_string( $terms ) ) {
  336. $terms = explode( ',', $terms );
  337. } else if ( ! is_array( $terms ) ) {
  338. continue;
  339. }
  340. $input['terms'][ $taxonomy ] = array_merge(
  341. $input['terms'][ $taxonomy ],
  342. $terms
  343. );
  344. }
  345. $tax_input = array();
  346. foreach ( $input['terms'] as $taxonomy => $terms ) {
  347. $tax_input[ $taxonomy ] = array();
  348. $is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
  349. foreach ( $terms as $term ) {
  350. /**
  351. * `curl --data 'terms[category][]=123'` should be interpreted as a category ID,
  352. * not a category whose name is '123'.
  353. *
  354. * Consequence: To add a category/tag whose name is '123', the client must
  355. * first look up its ID.
  356. */
  357. $term = (string) $term; // ctype_digit compat
  358. if ( ctype_digit( $term ) ) {
  359. $term = (int) $term;
  360. }
  361. $term_info = term_exists( $term, $taxonomy );
  362. if ( ! $term_info ) {
  363. // A term ID that doesn't already exist. Ignore it: we don't know what name to give it.
  364. if ( is_int( $term ) ){
  365. continue;
  366. }
  367. // only add a new tag/cat if the user has access to
  368. $tax = get_taxonomy( $taxonomy );
  369. // see https://core.trac.wordpress.org/ticket/26409
  370. if ( $is_hierarchical && ! current_user_can( $tax->cap->edit_terms ) ) {
  371. continue;
  372. } else if ( ! current_user_can( $tax->cap->assign_terms ) ) {
  373. continue;
  374. }
  375. $term_info = wp_insert_term( $term, $taxonomy );
  376. }
  377. if ( ! is_wp_error( $term_info ) ) {
  378. if ( $is_hierarchical ) {
  379. // Hierarchical terms must be added by ID
  380. $tax_input[$taxonomy][] = (int) $term_info['term_id'];
  381. } else {
  382. // Non-hierarchical terms must be added by name
  383. if ( is_int( $term ) ) {
  384. $term = get_term( $term, $taxonomy );
  385. $tax_input[$taxonomy][] = $term->name;
  386. } else {
  387. $tax_input[$taxonomy][] = $term;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. if ( isset( $input['terms']['category'] ) && empty( $tax_input['category'] ) && 'revision' !== $post_type->name ) {
  394. $tax_input['category'][] = get_option( 'default_category' );
  395. }
  396. unset( $input['terms'], $input['tags'], $input['categories'] );
  397. $insert = array();
  398. if ( !empty( $input['slug'] ) ) {
  399. $insert['post_name'] = $input['slug'];
  400. unset( $input['slug'] );
  401. }
  402. if ( isset( $input['discussion'] ) ) {
  403. $discussion = (array) $input['discussion'];
  404. foreach ( array( 'comment', 'ping' ) as $discussion_type ) {
  405. $discussion_open = sprintf( '%ss_open', $discussion_type );
  406. $discussion_status = sprintf( '%s_status', $discussion_type );
  407. if ( isset( $discussion[ $discussion_open ] ) ) {
  408. $is_open = WPCOM_JSON_API::is_truthy( $discussion[ $discussion_open ] );
  409. $discussion[ $discussion_status ] = $is_open ? 'open' : 'closed';
  410. }
  411. if ( in_array( $discussion[ $discussion_status ], array( 'open', 'closed' ) ) ) {
  412. $insert[ $discussion_status ] = $discussion[ $discussion_status ];
  413. }
  414. }
  415. }
  416. unset( $input['discussion'] );
  417. if ( isset( $input['menu_order'] ) ) {
  418. $insert['menu_order'] = $input['menu_order'];
  419. unset( $input['menu_order'] );
  420. }
  421. $publicize = isset( $input['publicize'] ) ? $input['publicize'] : null;
  422. unset( $input['publicize'] );
  423. $publicize_custom_message = isset( $input['publicize_message'] ) ? $input['publicize_message'] : null;
  424. unset( $input['publicize_message'] );
  425. if ( isset( $input['featured_image'] ) ) {
  426. $featured_image = trim( $input['featured_image'] );
  427. $delete_featured_image = empty( $featured_image );
  428. unset( $input['featured_image'] );
  429. }
  430. $metadata = isset( $input['metadata'] ) ? $input['metadata'] : null;
  431. unset( $input['metadata'] );
  432. $likes = isset( $input['likes_enabled'] ) ? $input['likes_enabled'] : null;
  433. unset( $input['likes_enabled'] );
  434. $sharing = isset( $input['sharing_enabled'] ) ? $input['sharing_enabled'] : null;
  435. unset( $input['sharing_enabled'] );
  436. $sticky = isset( $input['sticky'] ) ? $input['sticky'] : null;
  437. unset( $input['sticky'] );
  438. foreach ( $input as $key => $value ) {
  439. $insert["post_$key"] = $value;
  440. }
  441. if ( ! empty( $author_id ) ) {
  442. $insert['post_author'] = absint( $author_id );
  443. }
  444. if ( ! empty( $tax_input ) ) {
  445. $insert['tax_input'] = $tax_input;
  446. }
  447. $has_media = ! empty( $input['media'] ) ? count( $input['media'] ) : false;
  448. $has_media_by_url = ! empty( $input['media_urls'] ) ? count( $input['media_urls'] ) : false;
  449. $media_id_string = '';
  450. if ( $has_media || $has_media_by_url ) {
  451. $media_files = ! empty( $input['media'] ) ? $input['media'] : array();
  452. $media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array();
  453. $media_attrs = ! empty( $input['media_attrs'] ) ? $input['media_attrs'] : array();
  454. $media_results = $this->handle_media_creation_v1_1( $media_files, $media_urls, $media_attrs );
  455. $media_id_string = join( ',', array_filter( array_map( 'absint', $media_results['media_ids'] ) ) );
  456. }
  457. if ( $new ) {
  458. if ( isset( $input['content'] ) && ! has_shortcode( $input['content'], 'gallery' ) && ( $has_media || $has_media_by_url ) ) {
  459. switch ( ( $has_media + $has_media_by_url ) ) {
  460. case 0 :
  461. // No images - do nothing.
  462. break;
  463. case 1 :
  464. // 1 image - make it big
  465. $insert['post_content'] = $input['content'] = sprintf(
  466. "[gallery size=full ids='%s' columns=1]\n\n",
  467. $media_id_string
  468. ) . $input['content'];
  469. break;
  470. default :
  471. // Several images - 3 column gallery
  472. $insert['post_content'] = $input['content'] = sprintf(
  473. "[gallery ids='%s']\n\n",
  474. $media_id_string
  475. ) . $input['content'];
  476. break;
  477. }
  478. }
  479. $post_id = wp_insert_post( add_magic_quotes( $insert ), true );
  480. } else {
  481. $insert['ID'] = $post->ID;
  482. // wp_update_post ignores date unless edit_date is set
  483. // See: https://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts
  484. // See: https://core.trac.wordpress.org/browser/tags/3.9.2/src/wp-includes/post.php#L3302
  485. if ( isset( $input['date_gmt'] ) || isset( $input['date'] ) ) {
  486. $insert['edit_date'] = true;
  487. }
  488. // this two-step process ensures any changes submitted along with status=trash get saved before trashing
  489. if ( isset( $input['status'] ) && 'trash' === $input['status'] ) {
  490. // if we insert it with status='trash', it will get double-trashed, so insert it as a draft first
  491. unset( $insert['status'] );
  492. $post_id = wp_update_post( (object) $insert );
  493. // now call wp_trash_post so post_meta gets set and any filters get called
  494. wp_trash_post( $post_id );
  495. } else {
  496. $post_id = wp_update_post( (object) $insert );
  497. }
  498. }
  499. if ( !$post_id || is_wp_error( $post_id ) ) {
  500. return $post_id;
  501. }
  502. // make sure this post actually exists and is not an error of some kind (ie, trying to load media in the posts endpoint)
  503. $post_check = $this->get_post_by( 'ID', $post_id, $args['context'] );
  504. if ( is_wp_error( $post_check ) ) {
  505. return $post_check;
  506. }
  507. if ( $media_id_string ) {
  508. // Yes - this is really how wp-admin does it.
  509. $wpdb->query( $wpdb->prepare(
  510. "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $media_id_string )",
  511. $post_id
  512. ) );
  513. foreach ( $media_results['media_ids'] as $media_id ) {
  514. clean_attachment_cache( $media_id );
  515. }
  516. clean_post_cache( $post_id );
  517. }
  518. // set page template for this post..
  519. if ( isset( $input['page_template'] ) && 'page' == $post_type->name ) {
  520. $page_template = $input['page_template'];
  521. $page_templates = wp_get_theme()->get_page_templates( get_post( $post_id ) );
  522. if ( empty( $page_template ) || 'default' == $page_template || isset( $page_templates[ $page_template ] ) ) {
  523. update_post_meta( $post_id, '_wp_page_template', $page_template );
  524. }
  525. }
  526. // Set like status for the post
  527. /** This filter is documented in modules/likes.php */
  528. $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
  529. if ( $new ) {
  530. if ( $sitewide_likes_enabled ) {
  531. if ( false === $likes ) {
  532. update_post_meta( $post_id, 'switch_like_status', 0 );
  533. } else {
  534. delete_post_meta( $post_id, 'switch_like_status' );
  535. }
  536. } else {
  537. if ( $likes ) {
  538. update_post_meta( $post_id, 'switch_like_status', 1 );
  539. } else {
  540. delete_post_meta( $post_id, 'switch_like_status' );
  541. }
  542. }
  543. } else {
  544. if ( isset( $likes ) ) {
  545. if ( $sitewide_likes_enabled ) {
  546. if ( false === $likes ) {
  547. update_post_meta( $post_id, 'switch_like_status', 0 );
  548. } else {
  549. delete_post_meta( $post_id, 'switch_like_status' );
  550. }
  551. } else {
  552. if ( true === $likes ) {
  553. update_post_meta( $post_id, 'switch_like_status', 1 );
  554. } else {
  555. delete_post_meta( $post_id, 'switch_like_status' );
  556. }
  557. }
  558. }
  559. }
  560. // Set sharing status of the post
  561. if ( $new ) {
  562. $sharing_enabled = isset( $sharing ) ? (bool) $sharing : true;
  563. if ( false === $sharing_enabled ) {
  564. update_post_meta( $post_id, 'sharing_disabled', 1 );
  565. }
  566. }
  567. else {
  568. if ( isset( $sharing ) && true === $sharing ) {
  569. delete_post_meta( $post_id, 'sharing_disabled' );
  570. } else if ( isset( $sharing ) && false == $sharing ) {
  571. update_post_meta( $post_id, 'sharing_disabled', 1 );
  572. }
  573. }
  574. if ( isset( $sticky ) ) {
  575. if ( true === $sticky ) {
  576. stick_post( $post_id );
  577. } else {
  578. unstick_post( $post_id );
  579. }
  580. }
  581. // WPCOM Specific (Jetpack's will get bumped elsewhere
  582. // Tracks how many posts are published and sets meta
  583. // so we can track some other cool stats (like likes & comments on posts published)
  584. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  585. if (
  586. ( $new && 'publish' == $input['status'] )
  587. || (
  588. ! $new && isset( $last_status )
  589. && 'publish' != $last_status
  590. && isset( $new_status )
  591. && 'publish' == $new_status
  592. )
  593. ) {
  594. /** This action is documented in modules/widgets/social-media-icons.php */
  595. do_action( 'jetpack_bump_stats_extras', 'api-insights-posts', $this->api->token_details['client_id'] );
  596. update_post_meta( $post_id, '_rest_api_published', 1 );
  597. update_post_meta( $post_id, '_rest_api_client_id', $this->api->token_details['client_id'] );
  598. }
  599. }
  600. // We ask the user/dev to pass Publicize services he/she wants activated for the post, but Publicize expects us
  601. // to instead flag the ones we don't want to be skipped. proceed with said logic.
  602. // any posts coming from Path (client ID 25952) should also not publicize
  603. if ( $publicize === false || ( isset( $this->api->token_details['client_id'] ) && 25952 == $this->api->token_details['client_id'] ) ) {
  604. // No publicize at all, skip all by ID
  605. foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
  606. delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
  607. $service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
  608. if ( ! $service_connections ) {
  609. continue;
  610. }
  611. foreach ( $service_connections as $service_connection ) {
  612. update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
  613. }
  614. }
  615. } else if ( is_array( $publicize ) && ( count ( $publicize ) > 0 ) ) {
  616. foreach ( $GLOBALS['publicize_ui']->publicize->get_services( 'all' ) as $name => $service ) {
  617. /*
  618. * We support both indexed and associative arrays:
  619. * * indexed are to pass entire services
  620. * * associative are to pass specific connections per service
  621. *
  622. * We do support mixed arrays: mixed integer and string keys (see 3rd example below).
  623. *
  624. * EG: array( 'twitter', 'facebook') will only publicize to those, ignoring the other available services
  625. * Form data: publicize[]=twitter&publicize[]=facebook
  626. * 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.
  627. * Form data: publicize[twitter]=$pub_conn_id_0,$pub_conn_id_3&publicize[facebook]=$pub_conn_id_7
  628. * 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
  629. * Form data: publicize[]=twitter&publicize[facebook]=$pub_conn_id_0,$pub_conn_id_3
  630. */
  631. // Delete any stale SKIP value for the service by name. We'll add it back by ID.
  632. delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $name );
  633. // Get the user's connections
  634. $service_connections = $GLOBALS['publicize_ui']->publicize->get_connections( $name );
  635. // if the user doesn't have any connections for this service, move on
  636. if ( ! $service_connections ) {
  637. continue;
  638. }
  639. if ( !in_array( $name, $publicize ) && !array_key_exists( $name, $publicize ) ) {
  640. // Skip the whole service by adding each connection ID
  641. foreach ( $service_connections as $service_connection ) {
  642. update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
  643. }
  644. } else if ( !empty( $publicize[ $name ] ) ) {
  645. // Seems we're being asked to only push to [a] specific connection[s].
  646. // Explode the list on commas, which will also support a single passed ID
  647. $requested_connections = explode( ',', ( preg_replace( '/[\s]*/', '', $publicize[ $name ] ) ) );
  648. // Flag the connections we can't match with the requested list to be skipped.
  649. foreach ( $service_connections as $service_connection ) {
  650. if ( !in_array( $service_connection->meta['connection_data']->id, $requested_connections ) ) {
  651. update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id, 1 );
  652. } else {
  653. delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
  654. }
  655. }
  656. } else {
  657. // delete all SKIP values; it's okay to publish to all connected IDs for this service
  658. foreach ( $service_connections as $service_connection ) {
  659. delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_SKIP . $service_connection->unique_id );
  660. }
  661. }
  662. }
  663. }
  664. if ( ! is_null( $publicize_custom_message ) ) {
  665. if ( empty( $publicize_custom_message ) ) {
  666. delete_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS );
  667. } else {
  668. update_post_meta( $post_id, $GLOBALS['publicize_ui']->publicize->POST_MESS, trim( $publicize_custom_message ) );
  669. }
  670. }
  671. if ( ! empty( $insert['post_format'] ) ) {
  672. if ( 'default' !== strtolower( $insert['post_format'] ) ) {
  673. set_post_format( $post_id, $insert['post_format'] );
  674. }
  675. else {
  676. set_post_format( $post_id, get_option( 'default_post_format' ) );
  677. }
  678. }
  679. if ( isset( $featured_image ) ) {
  680. $this->parse_and_set_featured_image( $post_id, $delete_featured_image, $featured_image );
  681. }
  682. if ( ! empty( $metadata ) ) {
  683. foreach ( (array) $metadata as $meta ) {
  684. $meta = (object) $meta;
  685. if ( Jetpack_SEO_Posts::DESCRIPTION_META_KEY == $meta->key && ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
  686. return new WP_Error( 'unauthorized', __( 'SEO tools are not enabled for this site.', 'jetpack' ), 403 );
  687. }
  688. $existing_meta_item = new stdClass;
  689. if ( empty( $meta->operation ) )
  690. $meta->operation = 'update';
  691. if ( ! empty( $meta->value ) ) {
  692. if ( 'true' == $meta->value )
  693. $meta->value = true;
  694. if ( 'false' == $meta->value )
  695. $meta->value = false;
  696. }
  697. if ( ! empty( $meta->id ) ) {
  698. $meta->id = absint( $meta->id );
  699. $existing_meta_item = get_metadata_by_mid( 'post', $meta->id );
  700. if ( $post_id !== (int) $existing_meta_item->post_id ) {
  701. // Only allow updates for metadata on this post
  702. continue;
  703. }
  704. }
  705. $unslashed_meta_key = wp_unslash( $meta->key ); // should match what the final key will be
  706. $meta->key = wp_slash( $meta->key );
  707. $unslashed_existing_meta_key = wp_unslash( $existing_meta_item->meta_key );
  708. $existing_meta_item->meta_key = wp_slash( $existing_meta_item->meta_key );
  709. // make sure that the meta id passed matches the existing meta key
  710. if ( ! empty( $meta->id ) && ! empty( $meta->key ) ) {
  711. $meta_by_id = get_metadata_by_mid( 'post', $meta->id );
  712. if ( $meta_by_id->meta_key !== $meta->key ) {
  713. continue; // skip this meta
  714. }
  715. }
  716. switch ( $meta->operation ) {
  717. case 'delete':
  718. if ( ! empty( $meta->id ) && ! empty( $existing_meta_item->meta_key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_existing_meta_key ) ) {
  719. delete_metadata_by_mid( 'post', $meta->id );
  720. } elseif ( ! empty( $meta->key ) && ! empty( $meta->previous_value ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) {
  721. delete_post_meta( $post_id, $meta->key, $meta->previous_value );
  722. } elseif ( ! empty( $meta->key ) && current_user_can( 'delete_post_meta', $post_id, $unslashed_meta_key ) ) {
  723. delete_post_meta( $post_id, $meta->key );
  724. }
  725. break;
  726. case 'add':
  727. if ( ! empty( $meta->id ) || ! empty( $meta->previous_value ) ) {
  728. break;
  729. } elseif ( ! empty( $meta->key ) && ! empty( $meta->value ) && ( current_user_can( 'add_post_meta', $post_id, $unslashed_meta_key ) ) || WPCOM_JSON_API_Metadata::is_public( $meta->key ) ) {
  730. add_post_meta( $post_id, $meta->key, $meta->value );
  731. }
  732. break;
  733. case 'update':
  734. if ( ! isset( $meta->value ) ) {
  735. break;
  736. } elseif ( ! empty( $meta->id ) && ! empty( $existing_meta_item->meta_key ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_existing_meta_key ) || WPCOM_JSON_API_Metadata::is_public( $meta->key ) ) ) {
  737. update_metadata_by_mid( 'post', $meta->id, $meta->value );
  738. } elseif ( ! empty( $meta->key ) && ! empty( $meta->previous_value ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_meta_key ) || WPCOM_JSON_API_Metadata::is_public( $meta->key ) ) ) {
  739. update_post_meta( $post_id, $meta->key,$meta->value, $meta->previous_value );
  740. } elseif ( ! empty( $meta->key ) && ( current_user_can( 'edit_post_meta', $post_id, $unslashed_meta_key ) || WPCOM_JSON_API_Metadata::is_public( $meta->key ) ) ) {
  741. update_post_meta( $post_id, $meta->key, $meta->value );
  742. }
  743. break;
  744. }
  745. }
  746. }
  747. /** This action is documented in json-endpoints/class.wpcom-json-api-update-post-endpoint.php */
  748. do_action( 'rest_api_inserted_post', $post_id, $insert, $new );
  749. $return = $this->get_post_by( 'ID', $post_id, $args['context'] );
  750. if ( !$return || is_wp_error( $return ) ) {
  751. return $return;
  752. }
  753. if ( isset( $input['type'] ) && 'revision' === $input['type'] ) {
  754. $return['preview_nonce'] = wp_create_nonce( 'post_preview_' . $input['parent'] );
  755. }
  756. if ( isset( $sticky ) ) {
  757. // workaround for sticky test occasionally failing, maybe a race condition with stick_post() above
  758. $return['sticky'] = ( true === $sticky );
  759. }
  760. if ( ! empty( $media_results['errors'] ) )
  761. $return['media_errors'] = $media_results['errors'];
  762. if ( 'publish' !== $post->post_status ) {
  763. $sal_site = $this->get_sal_post_by( 'ID', $post_id, $args['context'] );
  764. $return['other_URLs'] = (object) $sal_site->get_permalink_suggestions( $input['title'] );
  765. }
  766. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  767. do_action( 'wpcom_json_api_objects', 'posts' );
  768. return $return;
  769. }
  770. // /sites/%s/posts/%d/delete -> $blog_id, $post_id
  771. function delete_post( $path, $blog_id, $post_id ) {
  772. $post = get_post( $post_id );
  773. if ( !$post || is_wp_error( $post ) ) {
  774. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  775. }
  776. if ( ! $this->is_post_type_allowed( $post->post_type ) ) {
  777. return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
  778. }
  779. if ( !current_user_can( 'delete_post', $post->ID ) ) {
  780. return new WP_Error( 'unauthorized', 'User cannot delete posts', 403 );
  781. }
  782. $args = $this->query_args();
  783. $return = $this->get_post_by( 'ID', $post->ID, $args['context'] );
  784. if ( !$return || is_wp_error( $return ) ) {
  785. return $return;
  786. }
  787. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  788. do_action( 'wpcom_json_api_objects', 'posts' );
  789. // we need to call wp_trash_post so that untrash will work correctly for all post types
  790. if ( 'trash' === $post->post_status )
  791. wp_delete_post( $post->ID );
  792. else
  793. wp_trash_post( $post->ID );
  794. $status = get_post_status( $post->ID );
  795. if ( false === $status ) {
  796. $return['status'] = 'deleted';
  797. return $return;
  798. }
  799. return $this->get_post_by( 'ID', $post->ID, $args['context'] );
  800. }
  801. // /sites/%s/posts/%d/restore -> $blog_id, $post_id
  802. function restore_post( $path, $blog_id, $post_id ) {
  803. $args = $this->query_args();
  804. $post = get_post( $post_id );
  805. if ( !$post || is_wp_error( $post ) ) {
  806. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  807. }
  808. if ( !current_user_can( 'delete_post', $post->ID ) ) {
  809. return new WP_Error( 'unauthorized', 'User cannot restore trashed posts', 403 );
  810. }
  811. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  812. do_action( 'wpcom_json_api_objects', 'posts' );
  813. wp_untrash_post( $post->ID );
  814. return $this->get_post_by( 'ID', $post->ID, $args['context'] );
  815. }
  816. protected function parse_and_set_featured_image( $post_id, $delete_featured_image, $featured_image ) {
  817. if ( $delete_featured_image ) {
  818. delete_post_thumbnail( $post_id );
  819. return;
  820. }
  821. $featured_image = (string) $featured_image;
  822. // if we got a post ID, we can just set it as the thumbnail
  823. if ( ctype_digit( $featured_image ) && 'attachment' == get_post_type( $featured_image ) ) {
  824. set_post_thumbnail( $post_id, $featured_image );
  825. return $featured_image;
  826. }
  827. $featured_image_id = $this->handle_media_sideload( $featured_image, $post_id, 'image' );
  828. if ( empty( $featured_image_id ) || ! is_int( $featured_image_id ) )
  829. return false;
  830. set_post_thumbnail( $post_id, $featured_image_id );
  831. return $featured_image_id;
  832. }
  833. protected function parse_and_set_author( $author = null, $post_type = 'post' ) {
  834. if ( empty( $author ) || ! post_type_supports( $post_type, 'author' ) )
  835. return get_current_user_id();
  836. $author = (string) $author;
  837. if ( ctype_digit( $author ) ) {
  838. $_user = get_user_by( 'id', $author );
  839. if ( ! $_user || is_wp_error( $_user ) )
  840. return new WP_Error( 'invalid_author', 'Invalid author provided' );
  841. return $_user->ID;
  842. }
  843. $_user = get_user_by( 'login', $author );
  844. if ( ! $_user || is_wp_error( $_user ) )
  845. return new WP_Error( 'invalid_author', 'Invalid author provided' );
  846. return $_user->ID;
  847. }
  848. protected function should_untrash_post( $last_status, $new_status, $post ) {
  849. return 'trash' === $last_status && 'trash' !== $new_status && isset( $post->ID );
  850. }
  851. protected function untrash_post( $post, $input ) {
  852. wp_untrash_post( $post->ID );
  853. $untrashed_post = get_post( $post->ID );
  854. // Lets make sure that we use the reverted the slug.
  855. if ( isset( $untrashed_post->post_name ) && $untrashed_post->post_name . '__trashed' === $input['slug'] ) {
  856. unset( $input['slug'] );
  857. }
  858. return $input;
  859. }
  860. protected function should_load_theme_functions( $post_id = null ) {
  861. if ( empty( $post_id ) ) {
  862. $input = $this->input( true );
  863. $type = $input['type'];
  864. } else {
  865. $type = get_post_type( $post_id );
  866. }
  867. return ! empty( $type ) && ! in_array( $type, array( 'post', 'revision' ) );
  868. }
  869. }