-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add user pattern categories to the pattern categories API response #5621
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,16 +76,50 @@ public function get_items_permissions_check( $request ) { | |
* Retrieves all block pattern categories. | ||
* | ||
* @since 6.0.0 | ||
* @since 6.5.0 Includes user categories from the wp_pattern_category taxonomy in the response if `user` sepecified in the request `source` param. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. | ||
*/ | ||
public function get_items( $request ) { | ||
$response = array(); | ||
$categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); | ||
foreach ( $categories as $category ) { | ||
$prepared_category = $this->prepare_item_for_response( $category, $request ); | ||
$response[] = $this->prepare_response_for_collection( $prepared_category ); | ||
$valid_query_args = array( | ||
'source' => true, | ||
); | ||
$query_args = array_intersect_key( $request->get_params(), $valid_query_args ); | ||
$response = array(); | ||
$unique_categories = array(); | ||
|
||
if ( is_array( $query_args['source'] ) && in_array( 'user', $query_args['source'], true ) ) { | ||
$user_categories = get_terms( | ||
array( | ||
'taxonomy' => 'wp_pattern_category', | ||
'hide_empty' => false, | ||
) | ||
); | ||
foreach ( $user_categories as $user_category ) { | ||
$prepared_category = $this->prepare_item_for_response( | ||
array( | ||
'name' => $user_category->slug, | ||
'label' => $user_category->name, | ||
'description' => $user_category->description, | ||
'id' => $user_category->term_id, | ||
), | ||
$request | ||
); | ||
$response[] = $this->prepare_response_for_collection( $prepared_category ); | ||
$unique_categories[] = $user_category->name; | ||
} | ||
} | ||
|
||
if ( ! isset( $query_args['source'] ) || in_array( 'core', $query_args['source'], true ) ) { | ||
$categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); | ||
foreach ( $categories as $category ) { | ||
if ( in_array( $category['label'], $unique_categories, true ) ) { | ||
continue; | ||
} | ||
$prepared_category = $this->prepare_item_for_response( $category, $request ); | ||
$response[] = $this->prepare_response_for_collection( $prepared_category ); | ||
} | ||
} | ||
|
||
return rest_ensure_response( $response ); | ||
|
@@ -95,6 +129,7 @@ public function get_items( $request ) { | |
* Prepare a raw block pattern category before it gets output in a REST API response. | ||
* | ||
* @since 6.0.0 | ||
* @since 6.5 Added `id` field for identifying user categories | ||
* | ||
* @param array $item Raw category as registered, before any changes. | ||
* @param WP_REST_Request $request Request object. | ||
|
@@ -110,6 +145,11 @@ public function prepare_item_for_response( $item, $request ) { | |
} | ||
} | ||
|
||
// For backwards compatibility we only want to include the id if the field is explicitly requested. | ||
if ( rest_is_field_included( 'id', $fields ) && isset( $item['id'] ) ) { | ||
$data['id'] = $item['id']; | ||
} | ||
Comment on lines
+149
to
+151
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. Generally, introducing a new field would not be seen as a BC break. Is there a specific issue we are trying to work around here? 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. ok, good to know, I thought additional fields would be seen as breaking change, I can remove this if not. |
||
|
||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | ||
$data = $this->add_additional_fields_to_object( $data, $request ); | ||
$data = $this->filter_response_by_context( $data, $context ); | ||
|
@@ -152,11 +192,45 @@ public function get_item_schema() { | |
'readonly' => true, | ||
'context' => array( 'view', 'edit', 'embed' ), | ||
), | ||
'id' => array( | ||
'description' => __( 'An optional category id, currently used to provide id for user wp_pattern_category terms' ), | ||
'type' => 'number', | ||
'readonly' => true, | ||
'context' => array( 'view', 'edit', 'embed' ), | ||
), | ||
), | ||
); | ||
|
||
$this->schema = $schema; | ||
|
||
return $this->add_additional_fields_schema( $this->schema ); | ||
} | ||
|
||
/** | ||
* Retrieves the search parameters for the block pattern categories. | ||
* | ||
* @since 6.5.0 Added source param to request. | ||
* | ||
* @return array Collection parameters. | ||
*/ | ||
public function get_collection_params() { | ||
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. The result of this method should be called in |
||
$query_params = parent::get_collection_params(); | ||
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. We should |
||
|
||
$query_params['source'] = array( | ||
'description' => __( 'Limit result set to specific sources, `core` and/or `user`' ), | ||
'type' => 'array', | ||
'items' => array( | ||
'type' => 'string', | ||
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. We can specify the allowed sources using 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. ok, will add that. |
||
), | ||
); | ||
|
||
/** | ||
* Filter collection parameters for the block pattern categories controller. | ||
* | ||
* @since 5.8.0 | ||
* | ||
* @param array $query_params JSON Schema-formatted collection parameters. | ||
*/ | ||
return apply_filters( 'rest_pattern_directory_collection_params', $query_params ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of this code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach was copied from here, and I assumed it was a way of ensuring that any non-valid query args where dropped 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but in that case it's because we are forwarding those query args wholesale to another API. Here, we are just consuming a single query parameter. So we can just do
$request['source']
.