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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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->parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
  48. $this->namespace = 'wp/v2';
  49. $this->rest_base = 'revisions';
  50. $post_type_object = get_post_type_object( $parent_post_type );
  51. $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
  52. }
  53. /**
  54. * Registers routes for revisions based on post types supporting revisions.
  55. *
  56. * @since 4.7.0
  57. *
  58. * @see register_rest_route()
  59. */
  60. public function register_routes() {
  61. register_rest_route( $this->namespace, '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base, array(
  62. 'args' => array(
  63. 'parent' => array(
  64. 'description' => __( 'The ID for the parent of the object.' ),
  65. 'type' => 'integer',
  66. ),
  67. ),
  68. array(
  69. 'methods' => WP_REST_Server::READABLE,
  70. 'callback' => array( $this, 'get_items' ),
  71. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  72. 'args' => $this->get_collection_params(),
  73. ),
  74. 'schema' => array( $this, 'get_public_item_schema' ),
  75. ) );
  76. register_rest_route( $this->namespace, '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)', array(
  77. 'args' => array(
  78. 'parent' => array(
  79. 'description' => __( 'The ID for the parent of the object.' ),
  80. 'type' => 'integer',
  81. ),
  82. 'id' => array(
  83. 'description' => __( 'Unique identifier for the object.' ),
  84. 'type' => 'integer',
  85. ),
  86. ),
  87. array(
  88. 'methods' => WP_REST_Server::READABLE,
  89. 'callback' => array( $this, 'get_item' ),
  90. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  91. 'args' => array(
  92. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  93. ),
  94. ),
  95. array(
  96. 'methods' => WP_REST_Server::DELETABLE,
  97. 'callback' => array( $this, 'delete_item' ),
  98. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  99. 'args' => array(
  100. 'force' => array(
  101. 'type' => 'boolean',
  102. 'default' => false,
  103. 'description' => __( 'Required to be true, as revisions do not support trashing.' ),
  104. ),
  105. ),
  106. ),
  107. 'schema' => array( $this, 'get_public_item_schema' ),
  108. ));
  109. }
  110. /**
  111. * Get the parent post, if the ID is valid.
  112. *
  113. * @since 4.7.2
  114. *
  115. * @param int $id Supplied ID.
  116. * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
  117. */
  118. protected function get_parent( $parent ) {
  119. $error = new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent ID.' ), array( 'status' => 404 ) );
  120. if ( (int) $parent <= 0 ) {
  121. return $error;
  122. }
  123. $parent = get_post( (int) $parent );
  124. if ( empty( $parent ) || empty( $parent->ID ) || $this->parent_post_type !== $parent->post_type ) {
  125. return $error;
  126. }
  127. return $parent;
  128. }
  129. /**
  130. * Checks if a given request has access to get revisions.
  131. *
  132. * @since 4.7.0
  133. *
  134. * @param WP_REST_Request $request Full data about the request.
  135. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  136. */
  137. public function get_items_permissions_check( $request ) {
  138. $parent = $this->get_parent( $request['parent'] );
  139. if ( is_wp_error( $parent ) ) {
  140. return $parent;
  141. }
  142. $parent_post_type_obj = get_post_type_object( $parent->post_type );
  143. if ( ! current_user_can( $parent_post_type_obj->cap->edit_post, $parent->ID ) ) {
  144. return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to view revisions of this post.' ), array( 'status' => rest_authorization_required_code() ) );
  145. }
  146. return true;
  147. }
  148. /**
  149. * Get the revision, if the ID is valid.
  150. *
  151. * @since 4.7.2
  152. *
  153. * @param int $id Supplied ID.
  154. * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise.
  155. */
  156. protected function get_revision( $id ) {
  157. $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision ID.' ), array( 'status' => 404 ) );
  158. if ( (int) $id <= 0 ) {
  159. return $error;
  160. }
  161. $revision = get_post( (int) $id );
  162. if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) {
  163. return $error;
  164. }
  165. return $revision;
  166. }
  167. /**
  168. * Gets a collection of revisions.
  169. *
  170. * @since 4.7.0
  171. *
  172. * @param WP_REST_Request $request Full data about the request.
  173. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  174. */
  175. public function get_items( $request ) {
  176. $parent = $this->get_parent( $request['parent'] );
  177. if ( is_wp_error( $parent ) ) {
  178. return $parent;
  179. }
  180. $revisions = wp_get_post_revisions( $request['parent'] );
  181. $response = array();
  182. foreach ( $revisions as $revision ) {
  183. $data = $this->prepare_item_for_response( $revision, $request );
  184. $response[] = $this->prepare_response_for_collection( $data );
  185. }
  186. return rest_ensure_response( $response );
  187. }
  188. /**
  189. * Checks if a given request has access to get a specific revision.
  190. *
  191. * @since 4.7.0
  192. *
  193. * @param WP_REST_Request $request Full data about the request.
  194. * @return bool|WP_Error True if the request has read access for the item, WP_Error object otherwise.
  195. */
  196. public function get_item_permissions_check( $request ) {
  197. return $this->get_items_permissions_check( $request );
  198. }
  199. /**
  200. * Retrieves one revision from the collection.
  201. *
  202. * @since 4.7.0
  203. *
  204. * @param WP_REST_Request $request Full data about the request.
  205. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  206. */
  207. public function get_item( $request ) {
  208. $parent = $this->get_parent( $request['parent'] );
  209. if ( is_wp_error( $parent ) ) {
  210. return $parent;
  211. }
  212. $revision = $this->get_revision( $request['id'] );
  213. if ( is_wp_error( $revision ) ) {
  214. return $revision;
  215. }
  216. $response = $this->prepare_item_for_response( $revision, $request );
  217. return rest_ensure_response( $response );
  218. }
  219. /**
  220. * Checks if a given request has access to delete a revision.
  221. *
  222. * @since 4.7.0
  223. *
  224. * @param WP_REST_Request $request Full details about the request.
  225. * @return bool|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
  226. */
  227. public function delete_item_permissions_check( $request ) {
  228. $parent = $this->get_parent( $request['parent'] );
  229. if ( is_wp_error( $parent ) ) {
  230. return $parent;
  231. }
  232. $revision = $this->get_revision( $request['id'] );
  233. if ( is_wp_error( $revision ) ) {
  234. return $revision;
  235. }
  236. $response = $this->get_items_permissions_check( $request );
  237. if ( ! $response || is_wp_error( $response ) ) {
  238. return $response;
  239. }
  240. $post_type = get_post_type_object( 'revision' );
  241. return current_user_can( $post_type->cap->delete_post, $revision->ID );
  242. }
  243. /**
  244. * Deletes a single revision.
  245. *
  246. * @since 4.7.0
  247. *
  248. * @param WP_REST_Request $request Full details about the request.
  249. * @return true|WP_Error True on success, or WP_Error object on failure.
  250. */
  251. public function delete_item( $request ) {
  252. $revision = $this->get_revision( $request['id'] );
  253. if ( is_wp_error( $revision ) ) {
  254. return $revision;
  255. }
  256. $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
  257. // We don't support trashing for revisions.
  258. if ( ! $force ) {
  259. /* translators: %s: force=true */
  260. return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Revisions do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
  261. }
  262. $previous = $this->prepare_item_for_response( $revision, $request );
  263. $result = wp_delete_post( $request['id'], true );
  264. /**
  265. * Fires after a revision is deleted via the REST API.
  266. *
  267. * @since 4.7.0
  268. *
  269. * @param (mixed) $result The revision object (if it was deleted or moved to the trash successfully)
  270. * or false (failure). If the revision was moved to the trash, $result represents
  271. * its new state; if it was deleted, $result represents its state before deletion.
  272. * @param WP_REST_Request $request The request sent to the API.
  273. */
  274. do_action( 'rest_delete_revision', $result, $request );
  275. if ( ! $result ) {
  276. return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
  277. }
  278. $response = new WP_REST_Response();
  279. $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) );
  280. return $response;
  281. }
  282. /**
  283. * Prepares the revision for the REST response.
  284. *
  285. * @since 4.7.0
  286. *
  287. * @param WP_Post $post Post revision object.
  288. * @param WP_REST_Request $request Request object.
  289. * @return WP_REST_Response Response object.
  290. */
  291. public function prepare_item_for_response( $post, $request ) {
  292. $GLOBALS['post'] = $post;
  293. setup_postdata( $post );
  294. $fields = $this->get_fields_for_response( $request );
  295. $data = array();
  296. if ( in_array( 'author', $fields, true ) ) {
  297. $data['author'] = (int) $post->post_author;
  298. }
  299. if ( in_array( 'date', $fields, true ) ) {
  300. $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
  301. }
  302. if ( in_array( 'date_gmt', $fields, true ) ) {
  303. $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
  304. }
  305. if ( in_array( 'id', $fields, true ) ) {
  306. $data['id'] = $post->ID;
  307. }
  308. if ( in_array( 'modified', $fields, true ) ) {
  309. $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
  310. }
  311. if ( in_array( 'modified_gmt', $fields, true ) ) {
  312. $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
  313. }
  314. if ( in_array( 'parent', $fields, true ) ) {
  315. $data['parent'] = (int) $post->post_parent;
  316. }
  317. if ( in_array( 'slug', $fields, true ) ) {
  318. $data['slug'] = $post->post_name;
  319. }
  320. if ( in_array( 'guid', $fields, true ) ) {
  321. $data['guid'] = array(
  322. /** This filter is documented in wp-includes/post-template.php */
  323. 'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
  324. 'raw' => $post->guid,
  325. );
  326. }
  327. if ( in_array( 'title', $fields, true ) ) {
  328. $data['title'] = array(
  329. 'raw' => $post->post_title,
  330. 'rendered' => get_the_title( $post->ID ),
  331. );
  332. }
  333. if ( in_array( 'content', $fields, true ) ) {
  334. $data['content'] = array(
  335. 'raw' => $post->post_content,
  336. /** This filter is documented in wp-includes/post-template.php */
  337. 'rendered' => apply_filters( 'the_content', $post->post_content ),
  338. );
  339. }
  340. if ( in_array( 'excerpt', $fields, true ) ) {
  341. $data['excerpt'] = array(
  342. 'raw' => $post->post_excerpt,
  343. 'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
  344. );
  345. }
  346. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  347. $data = $this->add_additional_fields_to_object( $data, $request );
  348. $data = $this->filter_response_by_context( $data, $context );
  349. $response = rest_ensure_response( $data );
  350. if ( ! empty( $data['parent'] ) ) {
  351. $response->add_link( 'parent', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
  352. }
  353. /**
  354. * Filters a revision returned from the API.
  355. *
  356. * Allows modification of the revision right before it is returned.
  357. *
  358. * @since 4.7.0
  359. *
  360. * @param WP_REST_Response $response The response object.
  361. * @param WP_Post $post The original revision object.
  362. * @param WP_REST_Request $request Request used to generate the response.
  363. */
  364. return apply_filters( 'rest_prepare_revision', $response, $post, $request );
  365. }
  366. /**
  367. * Checks the post_date_gmt or modified_gmt and prepare any post or
  368. * modified date for single post output.
  369. *
  370. * @since 4.7.0
  371. *
  372. * @param string $date_gmt GMT publication time.
  373. * @param string|null $date Optional. Local publication time. Default null.
  374. * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
  375. */
  376. protected function prepare_date_response( $date_gmt, $date = null ) {
  377. if ( '0000-00-00 00:00:00' === $date_gmt ) {
  378. return null;
  379. }
  380. if ( isset( $date ) ) {
  381. return mysql_to_rfc3339( $date );
  382. }
  383. return mysql_to_rfc3339( $date_gmt );
  384. }
  385. /**
  386. * Retrieves the revision's schema, conforming to JSON Schema.
  387. *
  388. * @since 4.7.0
  389. *
  390. * @return array Item schema data.
  391. */
  392. public function get_item_schema() {
  393. $schema = array(
  394. '$schema' => 'http://json-schema.org/draft-04/schema#',
  395. 'title' => "{$this->parent_post_type}-revision",
  396. 'type' => 'object',
  397. // Base properties for every Revision.
  398. 'properties' => array(
  399. 'author' => array(
  400. 'description' => __( 'The ID for the author of the object.' ),
  401. 'type' => 'integer',
  402. 'context' => array( 'view', 'edit', 'embed' ),
  403. ),
  404. 'date' => array(
  405. 'description' => __( "The date the object was published, in the site's timezone." ),
  406. 'type' => 'string',
  407. 'format' => 'date-time',
  408. 'context' => array( 'view', 'edit', 'embed' ),
  409. ),
  410. 'date_gmt' => array(
  411. 'description' => __( 'The date the object was published, as GMT.' ),
  412. 'type' => 'string',
  413. 'format' => 'date-time',
  414. 'context' => array( 'view', 'edit' ),
  415. ),
  416. 'guid' => array(
  417. 'description' => __( 'GUID for the object, as it exists in the database.' ),
  418. 'type' => 'string',
  419. 'context' => array( 'view', 'edit' ),
  420. ),
  421. 'id' => array(
  422. 'description' => __( 'Unique identifier for the object.' ),
  423. 'type' => 'integer',
  424. 'context' => array( 'view', 'edit', 'embed' ),
  425. ),
  426. 'modified' => array(
  427. 'description' => __( "The date the object was last modified, in the site's timezone." ),
  428. 'type' => 'string',
  429. 'format' => 'date-time',
  430. 'context' => array( 'view', 'edit' ),
  431. ),
  432. 'modified_gmt' => array(
  433. 'description' => __( 'The date the object was last modified, as GMT.' ),
  434. 'type' => 'string',
  435. 'format' => 'date-time',
  436. 'context' => array( 'view', 'edit' ),
  437. ),
  438. 'parent' => array(
  439. 'description' => __( 'The ID for the parent of the object.' ),
  440. 'type' => 'integer',
  441. 'context' => array( 'view', 'edit', 'embed' ),
  442. ),
  443. 'slug' => array(
  444. 'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
  445. 'type' => 'string',
  446. 'context' => array( 'view', 'edit', 'embed' ),
  447. ),
  448. ),
  449. );
  450. $parent_schema = $this->parent_controller->get_item_schema();
  451. if ( ! empty( $parent_schema['properties']['title'] ) ) {
  452. $schema['properties']['title'] = $parent_schema['properties']['title'];
  453. }
  454. if ( ! empty( $parent_schema['properties']['content'] ) ) {
  455. $schema['properties']['content'] = $parent_schema['properties']['content'];
  456. }
  457. if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
  458. $schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
  459. }
  460. if ( ! empty( $parent_schema['properties']['guid'] ) ) {
  461. $schema['properties']['guid'] = $parent_schema['properties']['guid'];
  462. }
  463. return $this->add_additional_fields_schema( $schema );
  464. }
  465. /**
  466. * Retrieves the query params for collections.
  467. *
  468. * @since 4.7.0
  469. *
  470. * @return array Collection parameters.
  471. */
  472. public function get_collection_params() {
  473. return array(
  474. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  475. );
  476. }
  477. /**
  478. * Checks the post excerpt and prepare it for single post output.
  479. *
  480. * @since 4.7.0
  481. *
  482. * @param string $excerpt The post excerpt.
  483. * @param WP_Post $post Post revision object.
  484. * @return string Prepared excerpt or empty string.
  485. */
  486. protected function prepare_excerpt_response( $excerpt, $post ) {
  487. /** This filter is documented in wp-includes/post-template.php */
  488. $excerpt = apply_filters( 'the_excerpt', $excerpt, $post );
  489. if ( empty( $excerpt ) ) {
  490. return '';
  491. }
  492. return $excerpt;
  493. }
  494. }