Nessuna descrizione

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 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 = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) );
  299. if ( $page > 1 ) {
  300. $prev_page = $page - 1;
  301. if ( $prev_page > $max_pages ) {
  302. $prev_page = $max_pages;
  303. }
  304. $prev_link = add_query_arg( 'page', $prev_page, $base );
  305. $response->link_header( 'prev', $prev_link );
  306. }
  307. if ( $max_pages > $page ) {
  308. $next_page = $page + 1;
  309. $next_link = add_query_arg( 'page', $next_page, $base );
  310. $response->link_header( 'next', $next_link );
  311. }
  312. return $response;
  313. }
  314. /**
  315. * Checks if a given request has access to get a specific revision.
  316. *
  317. * @since 4.7.0
  318. *
  319. * @param WP_REST_Request $request Full details about the request.
  320. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
  321. */
  322. public function get_item_permissions_check( $request ) {
  323. return $this->get_items_permissions_check( $request );
  324. }
  325. /**
  326. * Retrieves one revision from the collection.
  327. *
  328. * @since 4.7.0
  329. *
  330. * @param WP_REST_Request $request Full details about the request.
  331. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  332. */
  333. public function get_item( $request ) {
  334. $parent = $this->get_parent( $request['parent'] );
  335. if ( is_wp_error( $parent ) ) {
  336. return $parent;
  337. }
  338. $revision = $this->get_revision( $request['id'] );
  339. if ( is_wp_error( $revision ) ) {
  340. return $revision;
  341. }
  342. $response = $this->prepare_item_for_response( $revision, $request );
  343. return rest_ensure_response( $response );
  344. }
  345. /**
  346. * Checks if a given request has access to delete a revision.
  347. *
  348. * @since 4.7.0
  349. *
  350. * @param WP_REST_Request $request Full details about the request.
  351. * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
  352. */
  353. public function delete_item_permissions_check( $request ) {
  354. $parent = $this->get_parent( $request['parent'] );
  355. if ( is_wp_error( $parent ) ) {
  356. return $parent;
  357. }
  358. $parent_post_type = get_post_type_object( $parent->post_type );
  359. if ( ! current_user_can( 'delete_post', $parent->ID ) ) {
  360. return new WP_Error(
  361. 'rest_cannot_delete',
  362. __( 'Sorry, you are not allowed to delete revisions of this post.' ),
  363. array( 'status' => rest_authorization_required_code() )
  364. );
  365. }
  366. $revision = $this->get_revision( $request['id'] );
  367. if ( is_wp_error( $revision ) ) {
  368. return $revision;
  369. }
  370. $response = $this->get_items_permissions_check( $request );
  371. if ( ! $response || is_wp_error( $response ) ) {
  372. return $response;
  373. }
  374. if ( ! current_user_can( 'delete_post', $revision->ID ) ) {
  375. return new WP_Error(
  376. 'rest_cannot_delete',
  377. __( 'Sorry, you are not allowed to delete this revision.' ),
  378. array( 'status' => rest_authorization_required_code() )
  379. );
  380. }
  381. return true;
  382. }
  383. /**
  384. * Deletes a single revision.
  385. *
  386. * @since 4.7.0
  387. *
  388. * @param WP_REST_Request $request Full details about the request.
  389. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  390. */
  391. public function delete_item( $request ) {
  392. $revision = $this->get_revision( $request['id'] );
  393. if ( is_wp_error( $revision ) ) {
  394. return $revision;
  395. }
  396. $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
  397. // We don't support trashing for revisions.
  398. if ( ! $force ) {
  399. return new WP_Error(
  400. 'rest_trash_not_supported',
  401. /* translators: %s: force=true */
  402. sprintf( __( "Revisions do not support trashing. Set '%s' to delete." ), 'force=true' ),
  403. array( 'status' => 501 )
  404. );
  405. }
  406. $previous = $this->prepare_item_for_response( $revision, $request );
  407. $result = wp_delete_post( $request['id'], true );
  408. /**
  409. * Fires after a revision is deleted via the REST API.
  410. *
  411. * @since 4.7.0
  412. *
  413. * @param WP_Post|false|null $result The revision object (if it was deleted or moved to the Trash successfully)
  414. * or false or null (failure). If the revision was moved to the Trash, $result represents
  415. * its new state; if it was deleted, $result represents its state before deletion.
  416. * @param WP_REST_Request $request The request sent to the API.
  417. */
  418. do_action( 'rest_delete_revision', $result, $request );
  419. if ( ! $result ) {
  420. return new WP_Error(
  421. 'rest_cannot_delete',
  422. __( 'The post cannot be deleted.' ),
  423. array( 'status' => 500 )
  424. );
  425. }
  426. $response = new WP_REST_Response();
  427. $response->set_data(
  428. array(
  429. 'deleted' => true,
  430. 'previous' => $previous->get_data(),
  431. )
  432. );
  433. return $response;
  434. }
  435. /**
  436. * Determines the allowed query_vars for a get_items() response and prepares
  437. * them for WP_Query.
  438. *
  439. * @since 5.0.0
  440. *
  441. * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
  442. * @param WP_REST_Request $request Optional. Full details about the request.
  443. * @return array Items query arguments.
  444. */
  445. protected function prepare_items_query( $prepared_args = array(), $request = null ) {
  446. $query_args = array();
  447. foreach ( $prepared_args as $key => $value ) {
  448. /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
  449. $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  450. }
  451. // Map to proper WP_Query orderby param.
  452. if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
  453. $orderby_mappings = array(
  454. 'id' => 'ID',
  455. 'include' => 'post__in',
  456. 'slug' => 'post_name',
  457. 'include_slugs' => 'post_name__in',
  458. );
  459. if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
  460. $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
  461. }
  462. }
  463. return $query_args;
  464. }
  465. /**
  466. * Prepares the revision for the REST response.
  467. *
  468. * @since 4.7.0
  469. *
  470. * @param WP_Post $post Post revision object.
  471. * @param WP_REST_Request $request Request object.
  472. * @return WP_REST_Response Response object.
  473. */
  474. public function prepare_item_for_response( $post, $request ) {
  475. $GLOBALS['post'] = $post;
  476. setup_postdata( $post );
  477. $fields = $this->get_fields_for_response( $request );
  478. $data = array();
  479. if ( in_array( 'author', $fields, true ) ) {
  480. $data['author'] = (int) $post->post_author;
  481. }
  482. if ( in_array( 'date', $fields, true ) ) {
  483. $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
  484. }
  485. if ( in_array( 'date_gmt', $fields, true ) ) {
  486. $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
  487. }
  488. if ( in_array( 'id', $fields, true ) ) {
  489. $data['id'] = $post->ID;
  490. }
  491. if ( in_array( 'modified', $fields, true ) ) {
  492. $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
  493. }
  494. if ( in_array( 'modified_gmt', $fields, true ) ) {
  495. $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
  496. }
  497. if ( in_array( 'parent', $fields, true ) ) {
  498. $data['parent'] = (int) $post->post_parent;
  499. }
  500. if ( in_array( 'slug', $fields, true ) ) {
  501. $data['slug'] = $post->post_name;
  502. }
  503. if ( in_array( 'guid', $fields, true ) ) {
  504. $data['guid'] = array(
  505. /** This filter is documented in wp-includes/post-template.php */
  506. 'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
  507. 'raw' => $post->guid,
  508. );
  509. }
  510. if ( in_array( 'title', $fields, true ) ) {
  511. $data['title'] = array(
  512. 'raw' => $post->post_title,
  513. 'rendered' => get_the_title( $post->ID ),
  514. );
  515. }
  516. if ( in_array( 'content', $fields, true ) ) {
  517. $data['content'] = array(
  518. 'raw' => $post->post_content,
  519. /** This filter is documented in wp-includes/post-template.php */
  520. 'rendered' => apply_filters( 'the_content', $post->post_content ),
  521. );
  522. }
  523. if ( in_array( 'excerpt', $fields, true ) ) {
  524. $data['excerpt'] = array(
  525. 'raw' => $post->post_excerpt,
  526. 'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
  527. );
  528. }
  529. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  530. $data = $this->add_additional_fields_to_object( $data, $request );
  531. $data = $this->filter_response_by_context( $data, $context );
  532. $response = rest_ensure_response( $data );
  533. if ( ! empty( $data['parent'] ) ) {
  534. $response->add_link( 'parent', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
  535. }
  536. /**
  537. * Filters a revision returned from the REST API.
  538. *
  539. * Allows modification of the revision right before it is returned.
  540. *
  541. * @since 4.7.0
  542. *
  543. * @param WP_REST_Response $response The response object.
  544. * @param WP_Post $post The original revision object.
  545. * @param WP_REST_Request $request Request used to generate the response.
  546. */
  547. return apply_filters( 'rest_prepare_revision', $response, $post, $request );
  548. }
  549. /**
  550. * Checks the post_date_gmt or modified_gmt and prepare any post or
  551. * modified date for single post output.
  552. *
  553. * @since 4.7.0
  554. *
  555. * @param string $date_gmt GMT publication time.
  556. * @param string|null $date Optional. Local publication time. Default null.
  557. * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
  558. */
  559. protected function prepare_date_response( $date_gmt, $date = null ) {
  560. if ( '0000-00-00 00:00:00' === $date_gmt ) {
  561. return null;
  562. }
  563. if ( isset( $date ) ) {
  564. return mysql_to_rfc3339( $date );
  565. }
  566. return mysql_to_rfc3339( $date_gmt );
  567. }
  568. /**
  569. * Retrieves the revision's schema, conforming to JSON Schema.
  570. *
  571. * @since 4.7.0
  572. *
  573. * @return array Item schema data.
  574. */
  575. public function get_item_schema() {
  576. if ( $this->schema ) {
  577. return $this->add_additional_fields_schema( $this->schema );
  578. }
  579. $schema = array(
  580. '$schema' => 'http://json-schema.org/draft-04/schema#',
  581. 'title' => "{$this->parent_post_type}-revision",
  582. 'type' => 'object',
  583. // Base properties for every Revision.
  584. 'properties' => array(
  585. 'author' => array(
  586. 'description' => __( 'The ID for the author of the revision.' ),
  587. 'type' => 'integer',
  588. 'context' => array( 'view', 'edit', 'embed' ),
  589. ),
  590. 'date' => array(
  591. 'description' => __( "The date the revision was published, in the site's timezone." ),
  592. 'type' => 'string',
  593. 'format' => 'date-time',
  594. 'context' => array( 'view', 'edit', 'embed' ),
  595. ),
  596. 'date_gmt' => array(
  597. 'description' => __( 'The date the revision was published, as GMT.' ),
  598. 'type' => 'string',
  599. 'format' => 'date-time',
  600. 'context' => array( 'view', 'edit' ),
  601. ),
  602. 'guid' => array(
  603. 'description' => __( 'GUID for the revision, as it exists in the database.' ),
  604. 'type' => 'string',
  605. 'context' => array( 'view', 'edit' ),
  606. ),
  607. 'id' => array(
  608. 'description' => __( 'Unique identifier for the revision.' ),
  609. 'type' => 'integer',
  610. 'context' => array( 'view', 'edit', 'embed' ),
  611. ),
  612. 'modified' => array(
  613. 'description' => __( "The date the revision was last modified, in the site's timezone." ),
  614. 'type' => 'string',
  615. 'format' => 'date-time',
  616. 'context' => array( 'view', 'edit' ),
  617. ),
  618. 'modified_gmt' => array(
  619. 'description' => __( 'The date the revision was last modified, as GMT.' ),
  620. 'type' => 'string',
  621. 'format' => 'date-time',
  622. 'context' => array( 'view', 'edit' ),
  623. ),
  624. 'parent' => array(
  625. 'description' => __( 'The ID for the parent of the revision.' ),
  626. 'type' => 'integer',
  627. 'context' => array( 'view', 'edit', 'embed' ),
  628. ),
  629. 'slug' => array(
  630. 'description' => __( 'An alphanumeric identifier for the revision unique to its type.' ),
  631. 'type' => 'string',
  632. 'context' => array( 'view', 'edit', 'embed' ),
  633. ),
  634. ),
  635. );
  636. $parent_schema = $this->parent_controller->get_item_schema();
  637. if ( ! empty( $parent_schema['properties']['title'] ) ) {
  638. $schema['properties']['title'] = $parent_schema['properties']['title'];
  639. }
  640. if ( ! empty( $parent_schema['properties']['content'] ) ) {
  641. $schema['properties']['content'] = $parent_schema['properties']['content'];
  642. }
  643. if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
  644. $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
  645. }
  646. if ( ! empty( $parent_schema['properties']['guid'] ) ) {
  647. $schema['properties']['guid'] = $parent_schema['properties']['guid'];
  648. }
  649. $this->schema = $schema;
  650. return $this->add_additional_fields_schema( $this->schema );
  651. }
  652. /**
  653. * Retrieves the query params for collections.
  654. *
  655. * @since 4.7.0
  656. *
  657. * @return array Collection parameters.
  658. */
  659. public function get_collection_params() {
  660. $query_params = parent::get_collection_params();
  661. $query_params['context']['default'] = 'view';
  662. unset( $query_params['per_page']['default'] );
  663. $query_params['exclude'] = array(
  664. 'description' => __( 'Ensure result set excludes specific IDs.' ),
  665. 'type' => 'array',
  666. 'items' => array(
  667. 'type' => 'integer',
  668. ),
  669. 'default' => array(),
  670. );
  671. $query_params['include'] = array(
  672. 'description' => __( 'Limit result set to specific IDs.' ),
  673. 'type' => 'array',
  674. 'items' => array(
  675. 'type' => 'integer',
  676. ),
  677. 'default' => array(),
  678. );
  679. $query_params['offset'] = array(
  680. 'description' => __( 'Offset the result set by a specific number of items.' ),
  681. 'type' => 'integer',
  682. );
  683. $query_params['order'] = array(
  684. 'description' => __( 'Order sort attribute ascending or descending.' ),
  685. 'type' => 'string',
  686. 'default' => 'desc',
  687. 'enum' => array( 'asc', 'desc' ),
  688. );
  689. $query_params['orderby'] = array(
  690. 'description' => __( 'Sort collection by object attribute.' ),
  691. 'type' => 'string',
  692. 'default' => 'date',
  693. 'enum' => array(
  694. 'date',
  695. 'id',
  696. 'include',
  697. 'relevance',
  698. 'slug',
  699. 'include_slugs',
  700. 'title',
  701. ),
  702. );
  703. return $query_params;
  704. }
  705. /**
  706. * Checks the post excerpt and prepare it for single post output.
  707. *
  708. * @since 4.7.0
  709. *
  710. * @param string $excerpt The post excerpt.
  711. * @param WP_Post $post Post revision object.
  712. * @return string Prepared excerpt or empty string.
  713. */
  714. protected function prepare_excerpt_response( $excerpt, $post ) {
  715. /** This filter is documented in wp-includes/post-template.php */
  716. $excerpt = apply_filters( 'the_excerpt', $excerpt, $post );
  717. if ( empty( $excerpt ) ) {
  718. return '';
  719. }
  720. return $excerpt;
  721. }
  722. }