Skip to content
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

Pattern API: Add curation parameter #583

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ function disable_block_directory() {
* Filter the collection parameters:
* - set a new default for per_page.
* - add a new parameter, `author_name`, for a user's nicename slug.
* - add a new parameter, `curation`, to filter between curated, community, and all patterns.
*
* @param array $query_params JSON Schema-formatted collection parameters.
* @return array Filtered parameters.
Expand All @@ -665,6 +666,17 @@ function filter_patterns_collection_params( $query_params ) {
},
);

$query_params['curation'] = array(
'description' => __( 'Limit result to either curated core, community, or all patterns.', 'wporg-patterns' ),
'type' => 'string',
'default' => 'core',
'enum' => array(
'all',
'core',
'community',
),
);

if ( isset( $query_params['orderby'] ) ) {
$query_params['orderby']['enum'][] = 'favorite_count';
}
Expand Down Expand Up @@ -716,6 +728,28 @@ function filter_patterns_rest_query( $args, $request ) {
}
}

// If `curation` is passed and either `core` or `community`, we should
// filter the result. If `curation=all`, no filtering is needed.
if ( isset( $request['curation'] ) ) {
if ( 'core' === $request['curation'] ) {
// Patterns with the core keyword.
$args['tax_query']['core_keyword'] = array(
'taxonomy' => 'wporg-pattern-keyword',
'field' => 'slug',
'terms' => 'core',
'operator' => 'IN',
StevenDufresne marked this conversation as resolved.
Show resolved Hide resolved
);
} else if ( 'community' === $request['curation'] ) {
// Patterns without the core keyword.
$args['tax_query']['core_keyword'] = array(
'taxonomy' => 'wporg-pattern-keyword',
'field' => 'slug',
'terms' => 'core',
'operator' => 'NOT IN',
);
}
}

$orderby = $request->get_param( 'orderby' );
if ( 'favorite_count' === $orderby ) {
$args['orderby'] = 'meta_value_num';
Expand Down