class-wp-rest-autosaves-controller.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * REST API: WP_REST_Autosaves_Controller class.
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Core class used to access autosaves via the REST API.
  11. *
  12. * @since 5.0.0
  13. *
  14. * @see WP_REST_Revisions_Controller
  15. * @see WP_REST_Controller
  16. */
  17. class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
  18. /**
  19. * Parent post type.
  20. *
  21. * @since 5.0.0
  22. * @var string
  23. */
  24. private $parent_post_type;
  25. /**
  26. * Parent post controller.
  27. *
  28. * @since 5.0.0
  29. * @var WP_REST_Controller
  30. */
  31. private $parent_controller;
  32. /**
  33. * Revision controller.
  34. *
  35. * @since 5.0.0
  36. * @var WP_REST_Controller
  37. */
  38. private $revisions_controller;
  39. /**
  40. * The base of the parent controller's route.
  41. *
  42. * @since 5.0.0
  43. * @var string
  44. */
  45. private $parent_base;
  46. /**
  47. * Constructor.
  48. *
  49. * @since 5.0.0
  50. *
  51. * @param string $parent_post_type Post type of the parent.
  52. */
  53. public function __construct( $parent_post_type ) {
  54. $this->parent_post_type = $parent_post_type;
  55. $post_type_object = get_post_type_object( $parent_post_type );
  56. $parent_controller = $post_type_object->get_rest_controller();
  57. if ( ! $parent_controller ) {
  58. $parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
  59. }
  60. $this->parent_controller = $parent_controller;
  61. $this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
  62. $this->rest_namespace = 'wp/v2';
  63. $this->rest_base = 'autosaves';
  64. $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
  65. }
  66. /**
  67. * Registers routes for autosaves.
  68. *
  69. * @since 5.0.0
  70. *
  71. * @see register_rest_route()
  72. */
  73. public function register_routes() {
  74. register_rest_route(
  75. $this->rest_namespace,
  76. '/' . $this->parent_base . '/(?P<id>[\d]+)/' . $this->rest_base,
  77. array(
  78. 'args' => array(
  79. 'parent' => array(
  80. 'description' => __( 'The ID for the parent of the object.' ),
  81. 'type' => 'integer',
  82. ),
  83. ),
  84. array(
  85. 'methods' => WP_REST_Server::READABLE,
  86. 'callback' => array( $this, 'get_items' ),
  87. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  88. 'args' => $this->get_collection_params(),
  89. ),
  90. array(
  91. 'methods' => WP_REST_Server::CREATABLE,
  92. 'callback' => array( $this, 'create_item' ),
  93. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  94. 'args' => $this->parent_controller->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  95. ),
  96. 'schema' => array( $this, 'get_public_item_schema' ),
  97. )
  98. );
  99. register_rest_route(
  100. $this->rest_namespace,
  101. '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)',
  102. array(
  103. 'args' => array(
  104. 'parent' => array(
  105. 'description' => __( 'The ID for the parent of the object.' ),
  106. 'type' => 'integer',
  107. ),
  108. 'id' => array(
  109. 'description' => __( 'The ID for the object.' ),
  110. 'type' => 'integer',
  111. ),
  112. ),
  113. array(
  114. 'methods' => WP_REST_Server::READABLE,
  115. 'callback' => array( $this, 'get_item' ),
  116. 'permission_callback' => array( $this->revisions_controller, 'get_item_permissions_check' ),
  117. 'args' => array(
  118. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  119. ),
  120. ),
  121. 'schema' => array( $this, 'get_public_item_schema' ),
  122. )
  123. );
  124. }
  125. /**
  126. * Get the parent post.
  127. *
  128. * @since 5.0.0
  129. *
  130. * @param int $parent_id Supplied ID.
  131. * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
  132. */
  133. protected function get_parent( $parent_id ) {
  134. return $this->revisions_controller->get_parent( $parent_id );
  135. }
  136. /**
  137. * Checks if a given request has access to get autosaves.
  138. *
  139. * @since 5.0.0
  140. *
  141. * @param WP_REST_Request $request Full data about the request.
  142. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  143. */
  144. public function get_items_permissions_check( $request ) {
  145. $parent = $this->get_parent( $request['id'] );
  146. if ( is_wp_error( $parent ) ) {
  147. return $parent;
  148. }
  149. $parent_post_type_obj = get_post_type_object( $parent->post_type );
  150. if ( ! current_user_can( $parent_post_type_obj->cap->edit_post, $parent->ID ) ) {
  151. return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to view autosaves of this post.' ), array( 'status' => rest_authorization_required_code() ) );
  152. }
  153. return true;
  154. }
  155. /**
  156. * Checks if a given request has access to create an autosave revision.
  157. *
  158. * Autosave revisions inherit permissions from the parent post,
  159. * check if the current user has permission to edit the post.
  160. *
  161. * @since 5.0.0
  162. *
  163. * @param WP_REST_Request $request Full details about the request.
  164. * @return true|WP_Error True if the request has access to create the item, WP_Error object otherwise.
  165. */
  166. public function create_item_permissions_check( $request ) {
  167. $id = $request->get_param( 'id' );
  168. if ( empty( $id ) ) {
  169. return new WP_Error( 'rest_post_invalid_id', __( 'Invalid item ID.' ), array( 'status' => 404 ) );
  170. }
  171. return $this->parent_controller->update_item_permissions_check( $request );
  172. }
  173. /**
  174. * Creates, updates or deletes an autosave revision.
  175. *
  176. * @since 5.0.0
  177. *
  178. * @param WP_REST_Request $request Full details about the request.
  179. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  180. */
  181. public function create_item( $request ) {
  182. if ( ! defined( 'DOING_AUTOSAVE' ) ) {
  183. define( 'DOING_AUTOSAVE', true );
  184. }
  185. $post = get_post( $request['id'] );
  186. if ( is_wp_error( $post ) ) {
  187. return $post;
  188. }
  189. $prepared_post = $this->parent_controller->prepare_item_for_database( $request );
  190. $prepared_post->ID = $post->ID;
  191. $user_id = get_current_user_id();
  192. if ( ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status ) && $post->post_author == $user_id ) {
  193. // Draft posts for the same author: autosaving updates the post and does not create a revision.
  194. // Convert the post object to an array and add slashes, wp_update_post expects escaped array.
  195. $autosave_id = wp_update_post( wp_slash( (array) $prepared_post ), true );
  196. } else {
  197. // Non-draft posts: create or update the post autosave.
  198. $autosave_id = $this->create_post_autosave( (array) $prepared_post );
  199. }
  200. if ( is_wp_error( $autosave_id ) ) {
  201. return $autosave_id;
  202. }
  203. $autosave = get_post( $autosave_id );
  204. $request->set_param( 'context', 'edit' );
  205. $response = $this->prepare_item_for_response( $autosave, $request );
  206. $response = rest_ensure_response( $response );
  207. return $response;
  208. }
  209. /**
  210. * Get the autosave, if the ID is valid.
  211. *
  212. * @since 5.0.0
  213. *
  214. * @param WP_REST_Request $request Full data about the request.
  215. * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise.
  216. */
  217. public function get_item( $request ) {
  218. $parent_id = (int) $request->get_param( 'parent' );
  219. if ( $parent_id <= 0 ) {
  220. return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent ID.' ), array( 'status' => 404 ) );
  221. }
  222. $autosave = wp_get_post_autosave( $parent_id );
  223. if ( ! $autosave ) {
  224. return new WP_Error( 'rest_post_no_autosave', __( 'There is no autosave revision for this post.' ), array( 'status' => 404 ) );
  225. }
  226. $response = $this->prepare_item_for_response( $autosave, $request );
  227. return $response;
  228. }
  229. /**
  230. * Gets a collection of autosaves using wp_get_post_autosave.
  231. *
  232. * Contains the user's autosave, for empty if it doesn't exist.
  233. *
  234. * @since 5.0.0
  235. *
  236. * @param WP_REST_Request $request Full data about the request.
  237. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  238. */
  239. public function get_items( $request ) {
  240. $parent = $this->get_parent( $request['id'] );
  241. if ( is_wp_error( $parent ) ) {
  242. return $parent;
  243. }
  244. $response = array();
  245. $parent_id = $parent->ID;
  246. $revisions = wp_get_post_revisions( $parent_id, array( 'check_enabled' => false ) );
  247. foreach ( $revisions as $revision ) {
  248. if ( false !== strpos( $revision->post_name, "{$parent_id}-autosave" ) ) {
  249. $data = $this->prepare_item_for_response( $revision, $request );
  250. $response[] = $this->prepare_response_for_collection( $data );
  251. }
  252. }
  253. return rest_ensure_response( $response );
  254. }
  255. /**
  256. * Retrieves the autosave's schema, conforming to JSON Schema.
  257. *
  258. * @since 5.0.0
  259. *
  260. * @return array Item schema data.
  261. */
  262. public function get_item_schema() {
  263. if ( $this->schema ) {
  264. return $this->add_additional_fields_schema( $this->schema );
  265. }
  266. $schema = $this->revisions_controller->get_item_schema();
  267. $schema['properties']['preview_link'] = array(
  268. 'description' => __( 'Preview link for the post.' ),
  269. 'type' => 'string',
  270. 'format' => 'uri',
  271. 'context' => array( 'edit' ),
  272. 'readonly' => true,
  273. );
  274. $this->schema = $schema;
  275. return $this->add_additional_fields_schema( $this->schema );
  276. }
  277. /**
  278. * Creates autosave for the specified post.
  279. *
  280. * From wp-admin/post.php.
  281. *
  282. * @since 5.0.0
  283. *
  284. * @param mixed $post_data Associative array containing the post data.
  285. * @return mixed The autosave revision ID or WP_Error.
  286. */
  287. public function create_post_autosave( $post_data ) {
  288. $post_id = (int) $post_data['ID'];
  289. $post = get_post( $post_id );
  290. if ( is_wp_error( $post ) ) {
  291. return $post;
  292. }
  293. $user_id = get_current_user_id();
  294. // Store one autosave per author. If there is already an autosave, overwrite it.
  295. $old_autosave = wp_get_post_autosave( $post_id, $user_id );
  296. if ( $old_autosave ) {
  297. $new_autosave = _wp_post_revision_data( $post_data, true );
  298. $new_autosave['ID'] = $old_autosave->ID;
  299. $new_autosave['post_author'] = $user_id;
  300. // If the new autosave has the same content as the post, delete the autosave.
  301. $autosave_is_different = false;
  302. foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
  303. if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
  304. $autosave_is_different = true;
  305. break;
  306. }
  307. }
  308. if ( ! $autosave_is_different ) {
  309. wp_delete_post_revision( $old_autosave->ID );
  310. return new WP_Error( 'rest_autosave_no_changes', __( 'There is nothing to save. The autosave and the post content are the same.' ), array( 'status' => 400 ) );
  311. }
  312. /** This filter is documented in wp-admin/post.php */
  313. do_action( 'wp_creating_autosave', $new_autosave );
  314. // wp_update_post expects escaped array.
  315. return wp_update_post( wp_slash( $new_autosave ) );
  316. }
  317. // Create the new autosave as a special post revision.
  318. return _wp_put_post_revision( $post_data, true );
  319. }
  320. /**
  321. * Prepares the revision for the REST response.
  322. *
  323. * @since 5.0.0
  324. *
  325. * @param WP_Post $post Post revision object.
  326. * @param WP_REST_Request $request Request object.
  327. *
  328. * @return WP_REST_Response Response object.
  329. */
  330. public function prepare_item_for_response( $post, $request ) {
  331. $response = $this->revisions_controller->prepare_item_for_response( $post, $request );
  332. $fields = $this->get_fields_for_response( $request );
  333. if ( in_array( 'preview_link', $fields, true ) ) {
  334. $parent_id = wp_is_post_autosave( $post );
  335. $preview_post_id = false === $parent_id ? $post->ID : $parent_id;
  336. $preview_query_args = array();
  337. if ( false !== $parent_id ) {
  338. $preview_query_args['preview_id'] = $parent_id;
  339. $preview_query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $parent_id );
  340. }
  341. $response->data['preview_link'] = get_preview_post_link( $preview_post_id, $preview_query_args );
  342. }
  343. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  344. $response->data = $this->add_additional_fields_to_object( $response->data, $request );
  345. $response->data = $this->filter_response_by_context( $response->data, $context );
  346. /**
  347. * Filters a revision returned from the API.
  348. *
  349. * Allows modification of the revision right before it is returned.
  350. *
  351. * @since 5.0.0
  352. *
  353. * @param WP_REST_Response $response The response object.
  354. * @param WP_Post $post The original revision object.
  355. * @param WP_REST_Request $request Request used to generate the response.
  356. */
  357. return apply_filters( 'rest_prepare_autosave', $response, $post, $request );
  358. }
  359. /**
  360. * Retrieves the query params for the autosaves collection.
  361. *
  362. * @since 5.0.0
  363. *
  364. * @return array Collection parameters.
  365. */
  366. public function get_collection_params() {
  367. return array(
  368. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  369. );
  370. }
  371. }