class-wp-rest-post-statuses-controller.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * REST API: WP_REST_Post_Statuses_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Core class used to access post statuses via the REST API.
  11. *
  12. * @since 4.7.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Post_Statuses_Controller extends WP_REST_Controller {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 4.7.0
  21. */
  22. public function __construct() {
  23. $this->namespace = 'wp/v2';
  24. $this->rest_base = 'statuses';
  25. }
  26. /**
  27. * Registers the routes for the objects of the controller.
  28. *
  29. * @since 4.7.0
  30. *
  31. * @see register_rest_route()
  32. */
  33. public function register_routes() {
  34. register_rest_route( $this->namespace, '/' . $this->rest_base, array(
  35. array(
  36. 'methods' => WP_REST_Server::READABLE,
  37. 'callback' => array( $this, 'get_items' ),
  38. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  39. 'args' => $this->get_collection_params(),
  40. ),
  41. 'schema' => array( $this, 'get_public_item_schema' ),
  42. ) );
  43. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<status>[\w-]+)', array(
  44. 'args' => array(
  45. 'status' => array(
  46. 'description' => __( 'An alphanumeric identifier for the status.' ),
  47. 'type' => 'string',
  48. ),
  49. ),
  50. array(
  51. 'methods' => WP_REST_Server::READABLE,
  52. 'callback' => array( $this, 'get_item' ),
  53. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  54. 'args' => array(
  55. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  56. ),
  57. ),
  58. 'schema' => array( $this, 'get_public_item_schema' ),
  59. ) );
  60. }
  61. /**
  62. * Checks whether a given request has permission to read post statuses.
  63. *
  64. * @since 4.7.0
  65. *
  66. * @param WP_REST_Request $request Full details about the request.
  67. * @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
  68. */
  69. public function get_items_permissions_check( $request ) {
  70. if ( 'edit' === $request['context'] ) {
  71. $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
  72. foreach ( $types as $type ) {
  73. if ( current_user_can( $type->cap->edit_posts ) ) {
  74. return true;
  75. }
  76. }
  77. return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to edit posts in this post type.' ), array( 'status' => rest_authorization_required_code() ) );
  78. }
  79. return true;
  80. }
  81. /**
  82. * Retrieves all post statuses, depending on user context.
  83. *
  84. * @since 4.7.0
  85. *
  86. * @param WP_REST_Request $request Full details about the request.
  87. * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
  88. */
  89. public function get_items( $request ) {
  90. $data = array();
  91. $statuses = get_post_stati( array( 'internal' => false ), 'object' );
  92. $statuses['trash'] = get_post_status_object( 'trash' );
  93. foreach ( $statuses as $slug => $obj ) {
  94. $ret = $this->check_read_permission( $obj );
  95. if ( ! $ret ) {
  96. continue;
  97. }
  98. $status = $this->prepare_item_for_response( $obj, $request );
  99. $data[ $obj->name ] = $this->prepare_response_for_collection( $status );
  100. }
  101. return rest_ensure_response( $data );
  102. }
  103. /**
  104. * Checks if a given request has access to read a post status.
  105. *
  106. * @since 4.7.0
  107. *
  108. * @param WP_REST_Request $request Full details about the request.
  109. * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise.
  110. */
  111. public function get_item_permissions_check( $request ) {
  112. $status = get_post_status_object( $request['status'] );
  113. if ( empty( $status ) ) {
  114. return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) );
  115. }
  116. $check = $this->check_read_permission( $status );
  117. if ( ! $check ) {
  118. return new WP_Error( 'rest_cannot_read_status', __( 'Cannot view status.' ), array( 'status' => rest_authorization_required_code() ) );
  119. }
  120. return true;
  121. }
  122. /**
  123. * Checks whether a given post status should be visible.
  124. *
  125. * @since 4.7.0
  126. *
  127. * @param object $status Post status.
  128. * @return bool True if the post status is visible, otherwise false.
  129. */
  130. protected function check_read_permission( $status ) {
  131. if ( true === $status->public ) {
  132. return true;
  133. }
  134. if ( false === $status->internal || 'trash' === $status->name ) {
  135. $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
  136. foreach ( $types as $type ) {
  137. if ( current_user_can( $type->cap->edit_posts ) ) {
  138. return true;
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * Retrieves a specific post status.
  146. *
  147. * @since 4.7.0
  148. *
  149. * @param WP_REST_Request $request Full details about the request.
  150. * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
  151. */
  152. public function get_item( $request ) {
  153. $obj = get_post_status_object( $request['status'] );
  154. if ( empty( $obj ) ) {
  155. return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) );
  156. }
  157. $data = $this->prepare_item_for_response( $obj, $request );
  158. return rest_ensure_response( $data );
  159. }
  160. /**
  161. * Prepares a post status object for serialization.
  162. *
  163. * @since 4.7.0
  164. *
  165. * @param stdClass $status Post status data.
  166. * @param WP_REST_Request $request Full details about the request.
  167. * @return WP_REST_Response Post status data.
  168. */
  169. public function prepare_item_for_response( $status, $request ) {
  170. $fields = $this->get_fields_for_response( $request );
  171. $data = array();
  172. if ( in_array( 'name', $fields, true ) ) {
  173. $data['name'] = $status->label;
  174. }
  175. if ( in_array( 'private', $fields, true ) ) {
  176. $data['private'] = (bool) $status->private;
  177. }
  178. if ( in_array( 'protected', $fields, true ) ) {
  179. $data['protected'] = (bool) $status->protected;
  180. }
  181. if ( in_array( 'public', $fields, true ) ) {
  182. $data['public'] = (bool) $status->public;
  183. }
  184. if ( in_array( 'queryable', $fields, true ) ) {
  185. $data['queryable'] = (bool) $status->publicly_queryable;
  186. }
  187. if ( in_array( 'show_in_list', $fields, true ) ) {
  188. $data['show_in_list'] = (bool) $status->show_in_admin_all_list;
  189. }
  190. if ( in_array( 'slug', $fields, true ) ) {
  191. $data['slug'] = $status->name;
  192. }
  193. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  194. $data = $this->add_additional_fields_to_object( $data, $request );
  195. $data = $this->filter_response_by_context( $data, $context );
  196. $response = rest_ensure_response( $data );
  197. if ( 'publish' === $status->name ) {
  198. $response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
  199. } else {
  200. $response->add_link( 'archives', add_query_arg( 'status', $status->name, rest_url( 'wp/v2/posts' ) ) );
  201. }
  202. /**
  203. * Filters a status returned from the REST API.
  204. *
  205. * Allows modification of the status data right before it is returned.
  206. *
  207. * @since 4.7.0
  208. *
  209. * @param WP_REST_Response $response The response object.
  210. * @param object $status The original status object.
  211. * @param WP_REST_Request $request Request used to generate the response.
  212. */
  213. return apply_filters( 'rest_prepare_status', $response, $status, $request );
  214. }
  215. /**
  216. * Retrieves the post status' schema, conforming to JSON Schema.
  217. *
  218. * @since 4.7.0
  219. *
  220. * @return array Item schema data.
  221. */
  222. public function get_item_schema() {
  223. $schema = array(
  224. '$schema' => 'http://json-schema.org/draft-04/schema#',
  225. 'title' => 'status',
  226. 'type' => 'object',
  227. 'properties' => array(
  228. 'name' => array(
  229. 'description' => __( 'The title for the status.' ),
  230. 'type' => 'string',
  231. 'context' => array( 'embed', 'view', 'edit' ),
  232. 'readonly' => true,
  233. ),
  234. 'private' => array(
  235. 'description' => __( 'Whether posts with this status should be private.' ),
  236. 'type' => 'boolean',
  237. 'context' => array( 'edit' ),
  238. 'readonly' => true,
  239. ),
  240. 'protected' => array(
  241. 'description' => __( 'Whether posts with this status should be protected.' ),
  242. 'type' => 'boolean',
  243. 'context' => array( 'edit' ),
  244. 'readonly' => true,
  245. ),
  246. 'public' => array(
  247. 'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ),
  248. 'type' => 'boolean',
  249. 'context' => array( 'view', 'edit' ),
  250. 'readonly' => true,
  251. ),
  252. 'queryable' => array(
  253. 'description' => __( 'Whether posts with this status should be publicly-queryable.' ),
  254. 'type' => 'boolean',
  255. 'context' => array( 'view', 'edit' ),
  256. 'readonly' => true,
  257. ),
  258. 'show_in_list' => array(
  259. 'description' => __( 'Whether to include posts in the edit listing for their post type.' ),
  260. 'type' => 'boolean',
  261. 'context' => array( 'edit' ),
  262. 'readonly' => true,
  263. ),
  264. 'slug' => array(
  265. 'description' => __( 'An alphanumeric identifier for the status.' ),
  266. 'type' => 'string',
  267. 'context' => array( 'embed', 'view', 'edit' ),
  268. 'readonly' => true,
  269. ),
  270. ),
  271. );
  272. return $this->add_additional_fields_schema( $schema );
  273. }
  274. /**
  275. * Retrieves the query params for collections.
  276. *
  277. * @since 4.7.0
  278. *
  279. * @return array Collection parameters.
  280. */
  281. public function get_collection_params() {
  282. return array(
  283. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  284. );
  285. }
  286. }