class-wp-rest-block-patterns-controller.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * REST API: WP_REST_Block_Patterns_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 6.0.0
  8. */
  9. /**
  10. * Core class used to access block patterns via the REST API.
  11. *
  12. * @since 6.0.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
  17. /**
  18. * Defines whether remote patterns should be loaded.
  19. *
  20. * @since 6.0.0
  21. * @var bool
  22. */
  23. private $remote_patterns_loaded;
  24. /**
  25. * Constructs the controller.
  26. *
  27. * @since 6.0.0
  28. */
  29. public function __construct() {
  30. $this->namespace = 'wp/v2';
  31. $this->rest_base = 'block-patterns/patterns';
  32. }
  33. /**
  34. * Registers the routes for the objects of the controller.
  35. *
  36. * @since 6.0.0
  37. */
  38. public function register_routes() {
  39. register_rest_route(
  40. $this->namespace,
  41. '/' . $this->rest_base,
  42. array(
  43. array(
  44. 'methods' => WP_REST_Server::READABLE,
  45. 'callback' => array( $this, 'get_items' ),
  46. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  47. ),
  48. 'schema' => array( $this, 'get_public_item_schema' ),
  49. )
  50. );
  51. }
  52. /**
  53. * Checks whether a given request has permission to read block patterns.
  54. *
  55. * @since 6.0.0
  56. *
  57. * @param WP_REST_Request $request Full details about the request.
  58. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  59. */
  60. public function get_items_permissions_check( $request ) {
  61. if ( current_user_can( 'edit_posts' ) ) {
  62. return true;
  63. }
  64. foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  65. if ( current_user_can( $post_type->cap->edit_posts ) ) {
  66. return true;
  67. }
  68. }
  69. return new WP_Error(
  70. 'rest_cannot_view',
  71. __( 'Sorry, you are not allowed to view the registered block patterns.' ),
  72. array( 'status' => rest_authorization_required_code() )
  73. );
  74. }
  75. /**
  76. * Retrieves all block patterns.
  77. *
  78. * @since 6.0.0
  79. *
  80. * @param WP_REST_Request $request Full details about the request.
  81. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  82. */
  83. public function get_items( $request ) {
  84. if ( ! $this->remote_patterns_loaded ) {
  85. // Load block patterns from w.org.
  86. _load_remote_block_patterns(); // Patterns with the `core` keyword.
  87. _load_remote_featured_patterns(); // Patterns in the `featured` category.
  88. _register_remote_theme_patterns(); // Patterns requested by current theme.
  89. $this->remote_patterns_loaded = true;
  90. }
  91. $response = array();
  92. $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
  93. foreach ( $patterns as $pattern ) {
  94. $prepared_pattern = $this->prepare_item_for_response( $pattern, $request );
  95. $response[] = $this->prepare_response_for_collection( $prepared_pattern );
  96. }
  97. return rest_ensure_response( $response );
  98. }
  99. /**
  100. * Prepare a raw block pattern before it gets output in a REST API response.
  101. *
  102. * @since 6.0.0
  103. *
  104. * @param array $item Raw pattern as registered, before any changes.
  105. * @param WP_REST_Request $request Request object.
  106. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  107. */
  108. public function prepare_item_for_response( $item, $request ) {
  109. $fields = $this->get_fields_for_response( $request );
  110. $keys = array(
  111. 'name' => 'name',
  112. 'title' => 'title',
  113. 'description' => 'description',
  114. 'viewportWidth' => 'viewport_width',
  115. 'blockTypes' => 'block_types',
  116. 'postTypes' => 'post_types',
  117. 'categories' => 'categories',
  118. 'keywords' => 'keywords',
  119. 'content' => 'content',
  120. 'inserter' => 'inserter',
  121. );
  122. $data = array();
  123. foreach ( $keys as $item_key => $rest_key ) {
  124. if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) {
  125. $data[ $rest_key ] = $item[ $item_key ];
  126. }
  127. }
  128. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  129. $data = $this->add_additional_fields_to_object( $data, $request );
  130. $data = $this->filter_response_by_context( $data, $context );
  131. return rest_ensure_response( $data );
  132. }
  133. /**
  134. * Retrieves the block pattern schema, conforming to JSON Schema.
  135. *
  136. * @since 6.0.0
  137. *
  138. * @return array Item schema data.
  139. */
  140. public function get_item_schema() {
  141. $schema = array(
  142. '$schema' => 'http://json-schema.org/draft-04/schema#',
  143. 'title' => 'block-pattern',
  144. 'type' => 'object',
  145. 'properties' => array(
  146. 'name' => array(
  147. 'description' => __( 'The pattern name.' ),
  148. 'type' => 'string',
  149. 'readonly' => true,
  150. 'context' => array( 'view', 'edit', 'embed' ),
  151. ),
  152. 'title' => array(
  153. 'description' => __( 'The pattern title, in human readable format.' ),
  154. 'type' => 'string',
  155. 'readonly' => true,
  156. 'context' => array( 'view', 'edit', 'embed' ),
  157. ),
  158. 'description' => array(
  159. 'description' => __( 'The pattern detailed description.' ),
  160. 'type' => 'string',
  161. 'readonly' => true,
  162. 'context' => array( 'view', 'edit', 'embed' ),
  163. ),
  164. 'viewport_width' => array(
  165. 'description' => __( 'The pattern viewport width for inserter preview.' ),
  166. 'type' => 'number',
  167. 'readonly' => true,
  168. 'context' => array( 'view', 'edit', 'embed' ),
  169. ),
  170. 'block_types' => array(
  171. 'description' => __( 'Block types that the pattern is intended to be used with.' ),
  172. 'type' => 'array',
  173. 'readonly' => true,
  174. 'context' => array( 'view', 'edit', 'embed' ),
  175. ),
  176. 'post_types' => array(
  177. 'description' => __( 'An array of post types that the pattern is restricted to be used with.' ),
  178. 'type' => 'array',
  179. 'readonly' => true,
  180. 'context' => array( 'view', 'edit', 'embed' ),
  181. ),
  182. 'categories' => array(
  183. 'description' => __( 'The pattern category slugs.' ),
  184. 'type' => 'array',
  185. 'readonly' => true,
  186. 'context' => array( 'view', 'edit', 'embed' ),
  187. ),
  188. 'keywords' => array(
  189. 'description' => __( 'The pattern keywords.' ),
  190. 'type' => 'array',
  191. 'readonly' => true,
  192. 'context' => array( 'view', 'edit', 'embed' ),
  193. ),
  194. 'content' => array(
  195. 'description' => __( 'The pattern content.' ),
  196. 'type' => 'string',
  197. 'readonly' => true,
  198. 'context' => array( 'view', 'edit', 'embed' ),
  199. ),
  200. 'inserter' => array(
  201. 'description' => __( 'Determines whether the pattern is visible in inserter.' ),
  202. 'type' => 'boolean',
  203. 'readonly' => true,
  204. 'context' => array( 'view', 'edit', 'embed' ),
  205. ),
  206. ),
  207. );
  208. return $this->add_additional_fields_schema( $schema );
  209. }
  210. }