class-wp-rest-revisions-controller.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. <?php
  2. /**
  3. * REST API: WP_REST_Revisions_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Core class used to access revisions via the REST API.
  11. *
  12. * @since 4.7.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Revisions_Controller extends WP_REST_Controller {
  17. /**
  18. * Parent post type.
  19. *
  20. * @since 4.7.0
  21. * @var string
  22. */
  23. private $parent_post_type;
  24. /**
  25. * Parent controller.
  26. *
  27. * @since 4.7.0
  28. * @var WP_REST_Controller
  29. */
  30. private $parent_controller;
  31. /**
  32. * The base of the parent controller's route.
  33. *
  34. * @since 4.7.0
  35. * @var string
  36. */
  37. private $parent_base;
  38. /**
  39. * Constructor.
  40. *
  41. * @since 4.7.0
  42. *
  43. * @param string $parent_post_type Post type of the parent.
  44. */
  45. public function __construct( $parent_post_type ) {
  46. $this->parent_post_type = $parent_post_type;
  47. $this->namespace = 'wp/v2';
  48. $this->rest_base = 'revisions';
  49. $post_type_object = get_post_type_object( $parent_post_type );
  50. $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
  51. $this->parent_controller = $post_type_object->get_rest_controller();
  52. if ( ! $this->parent_controller ) {
  53. $this->parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
  54. }
  55. }
  56. /**
  57. * Registers routes for revisions based on post types supporting revisions.
  58. *
  59. * @since 4.7.0
  60. *
  61. * @see register_rest_route()
  62. */
  63. public function register_routes() {
  64. register_rest_route(
  65. $this->namespace,
  66. '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base,
  67. array(
  68. 'args' => array(
  69. 'parent' => array(
  70. 'description' => __( 'The ID for the parent of the object.' ),
  71. 'type' => 'integer',
  72. ),
  73. ),
  74. array(
  75. 'methods' => WP_REST_Server::READABLE,
  76. 'callback' => array( $this, 'get_items' ),
  77. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  78. 'args' => $this->get_collection_params(),
  79. ),
  80. 'schema' => array( $this, 'get_public_item_schema' ),
  81. )
  82. );
  83. register_rest_route(
  84. $this->namespace,
  85. '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)',
  86. array(
  87. 'args' => array(
  88. 'parent' => array(
  89. 'description' => __( 'The ID for the parent of the object.' ),
  90. 'type' => 'integer',
  91. ),
  92. 'id' => array(
  93. 'description' => __( 'Unique identifier for the object.' ),
  94. 'type' => 'integer',
  95. ),
  96. ),
  97. array(
  98. 'methods' => WP_REST_Server::READABLE,
  99. 'callback' => array( $this, 'get_item' ),
  100. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  101. 'args' => array(
  102. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  103. ),
  104. ),
  105. array(
  106. 'methods' => WP_REST_Server::DELETABLE,
  107. 'callback' => array( $this, 'delete_item' ),
  108. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  109. 'args' => array(
  110. 'force' => array(
  111. 'type' => 'boolean',
  112. 'default' => false,
  113. 'description' => __( 'Required to be true, as revisions do not support trashing.' ),
  114. ),
  115. ),
  116. ),
  117. 'schema' => array( $this, 'get_public_item_schema' ),
  118. )
  119. );
  120. }
  121. /**
  122. * Get the parent post, if the ID is valid.
  123. *
  124. * @since 4.7.2
  125. *
  126. * @param int $parent Supplied ID.
  127. * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
  128. */
  129. protected function get_parent( $parent ) {
  130. $error = new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent ID.' ), array( 'status' => 404 ) );
  131. if ( (int) $parent <= 0 ) {
  132. return $error;
  133. }
  134. $parent = get_post( (int) $parent );
  135. if ( empty( $parent ) || empty( $parent->ID ) || $this->parent_post_type !== $parent->post_type ) {
  136. return $error;
  137. }
  138. return $parent;
  139. }
  140. /**
  141. * Checks if a given request has access to get revisions.
  142. *
  143. * @since 4.7.0
  144. *
  145. * @param WP_REST_Request $request Full data about the request.
  146. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  147. */
  148. public function get_items_permissions_check( $request ) {
  149. $parent = $this->get_parent( $request['parent'] );
  150. if ( is_wp_error( $parent ) ) {
  151. return $parent;
  152. }
  153. $parent_post_type_obj = get_post_type_object( $parent->post_type );
  154. if ( ! current_user_can( $parent_post_type_obj->cap->edit_post, $parent->ID ) ) {
  155. return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to view revisions of this post.' ), array( 'status' => rest_authorization_required_code() ) );
  156. }
  157. return true;
  158. }
  159. /**
  160. * Get the revision, if the ID is valid.
  161. *
  162. * @since 4.7.2
  163. *
  164. * @param int $id Supplied ID.
  165. * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise.
  166. */
  167. protected function get_revision( $id ) {
  168. $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision ID.' ), array( 'status' => 404 ) );
  169. if ( (int) $id <= 0 ) {
  170. return $error;
  171. }
  172. $revision = get_post( (int) $id );
  173. if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) {
  174. return $error;
  175. }
  176. return $revision;
  177. }
  178. /**
  179. * Gets a collection of revisions.
  180. *
  181. * @since 4.7.0
  182. *
  183. * @param WP_REST_Request $request Full data about the request.
  184. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  185. */
  186. public function get_items( $request ) {
  187. $parent = $this->get_parent( $request['parent'] );
  188. if ( is_wp_error( $parent ) ) {
  189. return $parent;
  190. }
  191. // Ensure a search string is set in case the orderby is set to 'relevance'.
  192. if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
  193. return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
  194. }
  195. // Ensure an include parameter is set in case the orderby is set to 'include'.
  196. if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
  197. return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.' ), array( 'status' => 400 ) );
  198. }
  199. if ( wp_revisions_enabled( $parent ) ) {
  200. $registered = $this->get_collection_params();
  201. $args = array(
  202. 'post_parent' => $parent->ID,
  203. 'post_type' => 'revision',
  204. 'post_status' => 'inherit',
  205. 'posts_per_page' => -1,
  206. 'orderby' => 'date ID',
  207. 'order' => 'DESC',
  208. 'suppress_filters' => true,
  209. );
  210. $parameter_mappings = array(
  211. 'exclude' => 'post__not_in',
  212. 'include' => 'post__in',
  213. 'offset' => 'offset',
  214. 'order' => 'order',
  215. 'orderby' => 'orderby',
  216. 'page' => 'paged',
  217. 'per_page' => 'posts_per_page',
  218. 'search' => 's',
  219. );
  220. foreach ( $parameter_mappings as $api_param => $wp_param ) {
  221. if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
  222. $args[ $wp_param ] = $request[ $api_param ];
  223. }
  224. }
  225. // For backward-compatibility, 'date' needs to resolve to 'date ID'.
  226. if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) {
  227. $args['orderby'] = 'date ID';
  228. }
  229. /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
  230. $args = apply_filters( 'rest_revision_query', $args, $request );
  231. $query_args = $this->prepare_items_query( $args, $request );
  232. $revisions_query = new WP_Query();
  233. $revisions = $revisions_query->query( $query_args );
  234. $offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
  235. $page = (int) $query_args['paged'];
  236. $total_revisions = $revisions_query->found_posts;
  237. if ( $total_revisions < 1 ) {
  238. // Out-of-bounds, run the query again without LIMIT for total count.
  239. unset( $query_args['paged'], $query_args['offset'] );
  240. $count_query = new WP_Query();
  241. $count_query->query( $query_args );
  242. $total_revisions = $count_query->found_posts;
  243. }
  244. if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
  245. $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
  246. } else {
  247. $max_pages = $total_revisions > 0 ? 1 : 0;
  248. }
  249. if ( $total_revisions > 0 ) {
  250. if ( $offset >= $total_revisions ) {
  251. return new WP_Error( 'rest_revision_invalid_offset_number', __( 'The offset number requested is larger than or equal to the number of available revisions.' ), array( 'status' => 400 ) );
  252. } elseif ( ! $offset && $page > $max_pages ) {
  253. return new WP_Error( 'rest_revision_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) );
  254. }
  255. }
  256. } else {
  257. $revisions = array();
  258. $total_revisions = 0;
  259. $max_pages = 0;
  260. $page = (int) $request['page'];
  261. }
  262. $response = array();
  263. foreach ( $revisions as $revision ) {
  264. $data = $this->prepare_item_for_response( $revision, $request );
  265. $response[] = $this->prepare_response_for_collection( $data );
  266. }
  267. $response = rest_ensure_response( $response );
  268. $response->header( 'X-WP-Total', (int) $total_revisions );
  269. $response->header( 'X-WP-TotalPages', (int) $max_pages );
  270. $request_params = $request->get_query_params();
  271. $base = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) );
  272. if ( $page > 1 ) {
  273. $prev_page = $page - 1;
  274. if ( $prev_page > $max_pages ) {
  275. $prev_page = $max_pages;
  276. }
  277. $prev_link = add_query_arg( 'page', $prev_page, $base );
  278. $response->link_header( 'prev', $prev_link );
  279. }
  280. if ( $max_pages > $page ) {
  281. $next_page = $page + 1;
  282. $next_link = add_query_arg( 'page', $next_page, $base );
  283. $response->link_header( 'next', $next_link );
  284. }
  285. return $response;
  286. }
  287. /**
  288. * Checks if a given request has access to get a specific revision.
  289. *
  290. * @since 4.7.0
  291. *
  292. * @param WP_REST_Request $request Full data about the request.
  293. * @return bool|WP_Error True if the request has read access for the item, WP_Error object otherwise.
  294. */
  295. public function get_item_permissions_check( $request ) {
  296. return $this->get_items_permissions_check( $request );
  297. }
  298. /**
  299. * Retrieves one revision from the collection.
  300. *
  301. * @since 4.7.0
  302. *
  303. * @param WP_REST_Request $request Full data about the request.
  304. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  305. */
  306. public function get_item( $request ) {
  307. $parent = $this->get_parent( $request['parent'] );
  308. if ( is_wp_error( $parent ) ) {
  309. return $parent;
  310. }
  311. $parent_post_type = get_post_type_object( $parent->post_type );
  312. if ( ! current_user_can( $parent_post_type->cap->delete_post, $parent->ID ) ) {
  313. return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete revisions of this post.' ), array( 'status' => rest_authorization_required_code() ) );
  314. }
  315. $revision = $this->get_revision( $request['id'] );
  316. if ( is_wp_error( $revision ) ) {
  317. return $revision;
  318. }
  319. $response = $this->prepare_item_for_response( $revision, $request );
  320. return rest_ensure_response( $response );
  321. }
  322. /**
  323. * Checks if a given request has access to delete a revision.
  324. *
  325. * @since 4.7.0
  326. *
  327. * @param WP_REST_Request $request Full details about the request.
  328. * @return bool|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
  329. */
  330. public function delete_item_permissions_check( $request ) {
  331. $parent = $this->get_parent( $request['parent'] );
  332. if ( is_wp_error( $parent ) ) {
  333. return $parent;
  334. }
  335. $revision = $this->get_revision( $request['id'] );
  336. if ( is_wp_error( $revision ) ) {
  337. return $revision;
  338. }
  339. $response = $this->get_items_permissions_check( $request );
  340. if ( ! $response || is_wp_error( $response ) ) {
  341. return $response;
  342. }
  343. $post_type = get_post_type_object( 'revision' );
  344. if ( ! current_user_can( $post_type->cap->delete_post, $revision->ID ) ) {
  345. return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete this revision.' ), array( 'status' => rest_authorization_required_code() ) );
  346. }
  347. return true;
  348. }
  349. /**
  350. * Deletes a single revision.
  351. *
  352. * @since 4.7.0
  353. *
  354. * @param WP_REST_Request $request Full details about the request.
  355. * @return true|WP_Error True on success, or WP_Error object on failure.
  356. */
  357. public function delete_item( $request ) {
  358. $revision = $this->get_revision( $request['id'] );
  359. if ( is_wp_error( $revision ) ) {
  360. return $revision;
  361. }
  362. $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
  363. // We don't support trashing for revisions.
  364. if ( ! $force ) {
  365. /* translators: %s: force=true */
  366. return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Revisions do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
  367. }
  368. $previous = $this->prepare_item_for_response( $revision, $request );
  369. $result = wp_delete_post( $request['id'], true );
  370. /**
  371. * Fires after a revision is deleted via the REST API.
  372. *
  373. * @since 4.7.0
  374. *
  375. * @param (mixed) $result The revision object (if it was deleted or moved to the trash successfully)
  376. * or false (failure). If the revision was moved to the trash, $result represents
  377. * its new state; if it was deleted, $result represents its state before deletion.
  378. * @param WP_REST_Request $request The request sent to the API.
  379. */
  380. do_action( 'rest_delete_revision', $result, $request );
  381. if ( ! $result ) {
  382. return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
  383. }
  384. $response = new WP_REST_Response();
  385. $response->set_data(
  386. array(
  387. 'deleted' => true,
  388. 'previous' => $previous->get_data(),
  389. )
  390. );
  391. return $response;
  392. }
  393. /**
  394. * Determines the allowed query_vars for a get_items() response and prepares
  395. * them for WP_Query.
  396. *
  397. * @since 5.0.0
  398. *
  399. * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
  400. * @param WP_REST_Request $request Optional. Full details about the request.
  401. * @return array Items query arguments.
  402. */
  403. protected function prepare_items_query( $prepared_args = array(), $request = null ) {
  404. $query_args = array();
  405. foreach ( $prepared_args as $key => $value ) {
  406. /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
  407. $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  408. }
  409. // Map to proper WP_Query orderby param.
  410. if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
  411. $orderby_mappings = array(
  412. 'id' => 'ID',
  413. 'include' => 'post__in',
  414. 'slug' => 'post_name',
  415. 'include_slugs' => 'post_name__in',
  416. );
  417. if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
  418. $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
  419. }
  420. }
  421. return $query_args;
  422. }
  423. /**
  424. * Prepares the revision for the REST response.
  425. *
  426. * @since 4.7.0
  427. *
  428. * @param WP_Post $post Post revision object.
  429. * @param WP_REST_Request $request Request object.
  430. * @return WP_REST_Response Response object.
  431. */
  432. public function prepare_item_for_response( $post, $request ) {
  433. $GLOBALS['post'] = $post;
  434. setup_postdata( $post );
  435. $fields = $this->get_fields_for_response( $request );
  436. $data = array();
  437. if ( in_array( 'author', $fields, true ) ) {
  438. $data['author'] = (int) $post->post_author;
  439. }
  440. if ( in_array( 'date', $fields, true ) ) {
  441. $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
  442. }
  443. if ( in_array( 'date_gmt', $fields, true ) ) {
  444. $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
  445. }
  446. if ( in_array( 'id', $fields, true ) ) {
  447. $data['id'] = $post->ID;
  448. }
  449. if ( in_array( 'modified', $fields, true ) ) {
  450. $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
  451. }
  452. if ( in_array( 'modified_gmt', $fields, true ) ) {
  453. $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
  454. }
  455. if ( in_array( 'parent', $fields, true ) ) {
  456. $data['parent'] = (int) $post->post_parent;
  457. }
  458. if ( in_array( 'slug', $fields, true ) ) {
  459. $data['slug'] = $post->post_name;
  460. }
  461. if ( in_array( 'guid', $fields, true ) ) {
  462. $data['guid'] = array(
  463. /** This filter is documented in wp-includes/post-template.php */
  464. 'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
  465. 'raw' => $post->guid,
  466. );
  467. }
  468. if ( in_array( 'title', $fields, true ) ) {
  469. $data['title'] = array(
  470. 'raw' => $post->post_title,
  471. 'rendered' => get_the_title( $post->ID ),
  472. );
  473. }
  474. if ( in_array( 'content', $fields, true ) ) {
  475. $data['content'] = array(
  476. 'raw' => $post->post_content,
  477. /** This filter is documented in wp-includes/post-template.php */
  478. 'rendered' => apply_filters( 'the_content', $post->post_content ),
  479. );
  480. }
  481. if ( in_array( 'excerpt', $fields, true ) ) {
  482. $data['excerpt'] = array(
  483. 'raw' => $post->post_excerpt,
  484. 'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
  485. );
  486. }
  487. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  488. $data = $this->add_additional_fields_to_object( $data, $request );
  489. $data = $this->filter_response_by_context( $data, $context );
  490. $response = rest_ensure_response( $data );
  491. if ( ! empty( $data['parent'] ) ) {
  492. $response->add_link( 'parent', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
  493. }
  494. /**
  495. * Filters a revision returned from the API.
  496. *
  497. * Allows modification of the revision right before it is returned.
  498. *
  499. * @since 4.7.0
  500. *
  501. * @param WP_REST_Response $response The response object.
  502. * @param WP_Post $post The original revision object.
  503. * @param WP_REST_Request $request Request used to generate the response.
  504. */
  505. return apply_filters( 'rest_prepare_revision', $response, $post, $request );
  506. }
  507. /**
  508. * Checks the post_date_gmt or modified_gmt and prepare any post or
  509. * modified date for single post output.
  510. *
  511. * @since 4.7.0
  512. *
  513. * @param string $date_gmt GMT publication time.
  514. * @param string|null $date Optional. Local publication time. Default null.
  515. * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
  516. */
  517. protected function prepare_date_response( $date_gmt, $date = null ) {
  518. if ( '0000-00-00 00:00:00' === $date_gmt ) {
  519. return null;
  520. }
  521. if ( isset( $date ) ) {
  522. return mysql_to_rfc3339( $date );
  523. }
  524. return mysql_to_rfc3339( $date_gmt );
  525. }
  526. /**
  527. * Retrieves the revision's schema, conforming to JSON Schema.
  528. *
  529. * @since 4.7.0
  530. *
  531. * @return array Item schema data.
  532. */
  533. public function get_item_schema() {
  534. if ( $this->schema ) {
  535. return $this->add_additional_fields_schema( $this->schema );
  536. }
  537. $schema = array(
  538. '$schema' => 'http://json-schema.org/draft-04/schema#',
  539. 'title' => "{$this->parent_post_type}-revision",
  540. 'type' => 'object',
  541. // Base properties for every Revision.
  542. 'properties' => array(
  543. 'author' => array(
  544. 'description' => __( 'The ID for the author of the object.' ),
  545. 'type' => 'integer',
  546. 'context' => array( 'view', 'edit', 'embed' ),
  547. ),
  548. 'date' => array(
  549. 'description' => __( "The date the object was published, in the site's timezone." ),
  550. 'type' => 'string',
  551. 'format' => 'date-time',
  552. 'context' => array( 'view', 'edit', 'embed' ),
  553. ),
  554. 'date_gmt' => array(
  555. 'description' => __( 'The date the object was published, as GMT.' ),
  556. 'type' => 'string',
  557. 'format' => 'date-time',
  558. 'context' => array( 'view', 'edit' ),
  559. ),
  560. 'guid' => array(
  561. 'description' => __( 'GUID for the object, as it exists in the database.' ),
  562. 'type' => 'string',
  563. 'context' => array( 'view', 'edit' ),
  564. ),
  565. 'id' => array(
  566. 'description' => __( 'Unique identifier for the object.' ),
  567. 'type' => 'integer',
  568. 'context' => array( 'view', 'edit', 'embed' ),
  569. ),
  570. 'modified' => array(
  571. 'description' => __( "The date the object was last modified, in the site's timezone." ),
  572. 'type' => 'string',
  573. 'format' => 'date-time',
  574. 'context' => array( 'view', 'edit' ),
  575. ),
  576. 'modified_gmt' => array(
  577. 'description' => __( 'The date the object was last modified, as GMT.' ),
  578. 'type' => 'string',
  579. 'format' => 'date-time',
  580. 'context' => array( 'view', 'edit' ),
  581. ),
  582. 'parent' => array(
  583. 'description' => __( 'The ID for the parent of the object.' ),
  584. 'type' => 'integer',
  585. 'context' => array( 'view', 'edit', 'embed' ),
  586. ),
  587. 'slug' => array(
  588. 'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
  589. 'type' => 'string',
  590. 'context' => array( 'view', 'edit', 'embed' ),
  591. ),
  592. ),
  593. );
  594. $parent_schema = $this->parent_controller->get_item_schema();
  595. if ( ! empty( $parent_schema['properties']['title'] ) ) {
  596. $schema['properties']['title'] = $parent_schema['properties']['title'];
  597. }
  598. if ( ! empty( $parent_schema['properties']['content'] ) ) {
  599. $schema['properties']['content'] = $parent_schema['properties']['content'];
  600. }
  601. if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
  602. $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
  603. }
  604. if ( ! empty( $parent_schema['properties']['guid'] ) ) {
  605. $schema['properties']['guid'] = $parent_schema['properties']['guid'];
  606. }
  607. $this->schema = $schema;
  608. return $this->add_additional_fields_schema( $this->schema );
  609. }
  610. /**
  611. * Retrieves the query params for collections.
  612. *
  613. * @since 4.7.0
  614. *
  615. * @return array Collection parameters.
  616. */
  617. public function get_collection_params() {
  618. $query_params = parent::get_collection_params();
  619. $query_params['context']['default'] = 'view';
  620. unset( $query_params['per_page']['default'] );
  621. $query_params['exclude'] = array(
  622. 'description' => __( 'Ensure result set excludes specific IDs.' ),
  623. 'type' => 'array',
  624. 'items' => array(
  625. 'type' => 'integer',
  626. ),
  627. 'default' => array(),
  628. );
  629. $query_params['include'] = array(
  630. 'description' => __( 'Limit result set to specific IDs.' ),
  631. 'type' => 'array',
  632. 'items' => array(
  633. 'type' => 'integer',
  634. ),
  635. 'default' => array(),
  636. );
  637. $query_params['offset'] = array(
  638. 'description' => __( 'Offset the result set by a specific number of items.' ),
  639. 'type' => 'integer',
  640. );
  641. $query_params['order'] = array(
  642. 'description' => __( 'Order sort attribute ascending or descending.' ),
  643. 'type' => 'string',
  644. 'default' => 'desc',
  645. 'enum' => array( 'asc', 'desc' ),
  646. );
  647. $query_params['orderby'] = array(
  648. 'description' => __( 'Sort collection by object attribute.' ),
  649. 'type' => 'string',
  650. 'default' => 'date',
  651. 'enum' => array(
  652. 'date',
  653. 'id',
  654. 'include',
  655. 'relevance',
  656. 'slug',
  657. 'include_slugs',
  658. 'title',
  659. ),
  660. );
  661. return $query_params;
  662. }
  663. /**
  664. * Checks the post excerpt and prepare it for single post output.
  665. *
  666. * @since 4.7.0
  667. *
  668. * @param string $excerpt The post excerpt.
  669. * @param WP_Post $post Post revision object.
  670. * @return string Prepared excerpt or empty string.
  671. */
  672. protected function prepare_excerpt_response( $excerpt, $post ) {
  673. /** This filter is documented in wp-includes/post-template.php */
  674. $excerpt = apply_filters( 'the_excerpt', $excerpt, $post );
  675. if ( empty( $excerpt ) ) {
  676. return '';
  677. }
  678. return $excerpt;
  679. }
  680. }