-
Notifications
You must be signed in to change notification settings - Fork 3.2k
6.0 Backport to add support for custom taxonomies filtering for Query Loop (from Gutenberg) #2488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d453c19
3163816
e51fd0c
b081331
cde5c14
fef0000
c658eb8
e22f662
ceedc0a
a3d71fb
a7d1560
d7b77aa
fec3207
6fbf8e5
0af2058
a3e5009
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ller.php.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| <?php | ||
| /** | ||
| * REST API: WP_REST_Block_Patterns_Controller class | ||
| * | ||
| * @package WordPress | ||
| * @subpackage REST_API | ||
| * @since 6.0.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Core class used to access block patterns via the REST API. | ||
| * | ||
| * @since 6.0.0 | ||
| * | ||
| * @see WP_REST_Controller | ||
| */ | ||
| class WP_REST_Block_Patterns_Controller extends WP_REST_Controller { | ||
|
|
||
| /** | ||
| * Constructs the controller. | ||
| * | ||
| * @since 6.0.0 | ||
| */ | ||
| public function __construct() { | ||
| $this->namespace = '__experimental'; | ||
| $this->rest_base = 'block-patterns/patterns'; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the routes for the objects of the controller. | ||
| * | ||
| * @since 6.0.0 | ||
| */ | ||
| public function register_routes() { | ||
| register_rest_route( | ||
| $this->namespace, | ||
| '/' . $this->rest_base, | ||
| array( | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_items' ), | ||
| 'permission_callback' => array( $this, 'get_items_permissions_check' ), | ||
| ), | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a given request has permission to read block patterns. | ||
| * | ||
| * @since 6.0.0 | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return true|WP_Error True if the request has read access, WP_Error object otherwise. | ||
| */ | ||
| public function get_items_permissions_check( $request ) { | ||
| if ( current_user_can( 'edit_posts' ) ) { | ||
| return true; | ||
| } | ||
|
|
||
| foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { | ||
| if ( current_user_can( $post_type->cap->edit_posts ) ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return new WP_Error( | ||
| 'rest_cannot_view', | ||
| __( 'Sorry, you are not allowed to view the registered block patterns.' ), | ||
| array( 'status' => rest_authorization_required_code() ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves all block patterns. | ||
| * | ||
| * @since 6.0.0 | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
| */ | ||
| public function get_items( $request ) { | ||
| // Load block patterns from w.org. | ||
| _load_remote_block_patterns(); // Patterns with the `core` keyword. | ||
| _load_remote_featured_patterns(); // Patterns in the `featured` category. | ||
| _register_remote_theme_patterns(); // Patterns requested by current theme. | ||
|
Comment on lines
+85
to
+87
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these methods guarded against running more than once in a request? Looking at
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are not guarded. When writing them, I assumed that the handler can ever run only once. But that was wrong. What would be a good way to prevent the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent, let's wait for confirmation from @TimothyBJacobs on the changes proposed.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
During regular requests they probably never are, but what @TimothyBJacobs probably had in mind is that the handler can be called multiple times when doing special things: batching requests, executing the endpoints when preloading, embedding linked requests into one response... In these cases, multiple requests are being processed within one parent one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, @jsnajdr. |
||
|
|
||
| $response = array(); | ||
| $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered(); | ||
| foreach ( $patterns as $pattern ) { | ||
| $prepared_pattern = $this->prepare_item_for_response( $pattern, $request ); | ||
| $response[] = $this->prepare_response_for_collection( $prepared_pattern ); | ||
| } | ||
| return rest_ensure_response( $response ); | ||
| } | ||
|
|
||
| /** | ||
| * Prepare a raw block pattern before it gets output in a REST API response. | ||
| * | ||
| * @since 6.0.0 | ||
| * | ||
| * @param object $item Raw pattern as registered, before any changes. | ||
| * @param WP_REST_Request $request Request object. | ||
| * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
| */ | ||
| public function prepare_item_for_response( $item, $request ) { | ||
| $fields = $this->get_fields_for_response( $request ); | ||
| $keys = array( | ||
| 'name', | ||
| 'title', | ||
| 'description', | ||
| 'viewportWidth', | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'blockTypes', | ||
| 'categories', | ||
| 'keywords', | ||
| 'content', | ||
| ); | ||
| $data = array(); | ||
| foreach ( $keys as $key ) { | ||
| if ( isset( $item[ $key ] ) && rest_is_field_included( $key, $fields ) ) { | ||
| $data[ $key ] = $item[ $key ]; | ||
| } | ||
| } | ||
|
|
||
| $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | ||
| $data = $this->add_additional_fields_to_object( $data, $request ); | ||
| $data = $this->filter_response_by_context( $data, $context ); | ||
| return rest_ensure_response( $data ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing about filter support. |
||
| } | ||
|
|
||
| /** | ||
| * Retrieves the block pattern schema, conforming to JSON Schema. | ||
| * | ||
| * @since 6.0.0 | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
| public function get_item_schema() { | ||
| $schema = array( | ||
| '$schema' => 'http://json-schema.org/draft-04/schema#', | ||
| 'title' => 'block-pattern', | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'name' => array( | ||
| 'description' => __( 'The pattern name.' ), | ||
| 'type' => 'string', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ), | ||
| 'title' => array( | ||
| 'description' => __( 'The pattern title, in human readable format.' ), | ||
| 'type' => 'string', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
| ), | ||
| 'description' => array( | ||
| 'description' => __( 'The pattern detailed description.' ), | ||
| 'type' => 'string', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
| ), | ||
| 'viewportWidth' => array( | ||
| 'description' => __( 'The pattern viewport width for inserter preview.' ), | ||
| 'type' => 'number', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
| ), | ||
| 'blockTypes' => array( | ||
| 'description' => __( 'Block types that the pattern is intended to be used with.' ), | ||
| 'type' => 'array', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
| ), | ||
| 'categories' => array( | ||
| 'description' => __( 'The pattern category slugs.' ), | ||
| 'type' => 'array', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
| ), | ||
| 'keywords' => array( | ||
| 'description' => __( 'The pattern keywords.' ), | ||
| 'type' => 'array', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
| ), | ||
| 'content' => array( | ||
| 'description' => __( 'The pattern content.' ), | ||
| 'type' => 'string', | ||
| 'readonly' => true, | ||
| 'context' => array( 'view', 'embed' ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| return $this->add_additional_fields_schema( $schema ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.