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

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