Skip to content

Commit

Permalink
Pattern API: Add curation parameter to filter out all/core/communit…
Browse files Browse the repository at this point in the history
…y patterns (#583)
  • Loading branch information
ryelle authored Jun 9, 2023
1 parent f227b7e commit 1b14e53
Showing 1 changed file with 34 additions and 0 deletions.
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',
);
} 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

0 comments on commit 1b14e53

Please sign in to comment.