選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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

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