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

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