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 Directory API: Add a keyword filter parameter #28794

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
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
21 changes: 16 additions & 5 deletions lib/class-wp-rest-pattern-directory-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,17 @@ public function get_items_permissions_check( $request ) { // phpcs:ignore Variab
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
$query_args = array();
$category_ids = $request['category'];
$search_term = $request['search'];
$query_args = array();
$category_id = $request['category'];
$keyword_id = $request['keyword'];
$search_term = $request['search'];

if ( $category_ids ) {
$query_args['pattern-categories'] = $category_ids;
if ( $category_id ) {
$query_args['pattern-categories'] = $category_id;
}

if ( $keyword_id ) {
$query_args['pattern-keywords'] = $keyword_id;
}

if ( $search_term ) {
Expand Down Expand Up @@ -276,6 +281,12 @@ public function get_collection_params() {
'minimum' => 1,
);

$query_params['keyword'] = array(
'description' => __( 'Limit results to those matching a keyword ID.', 'gutenberg' ),
'type' => 'integer',
'minimum' => 1,
);

/**
* Filter collection parameters for the pattern directory controller.
*
Expand Down
30 changes: 30 additions & 0 deletions phpunit/class-wp-rest-pattern-directory-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ public function test_get_items_by_category() {
}
}

/**
* @covers WP_REST_Pattern_Directory_Controller::get_items
*
* @since 5.8.0
*/
public function test_get_items_by_keyword() {
wp_set_current_user( self::$contributor_id );
self::mock_successful_response( 'browse-keyword', true );

$request = new WP_REST_Request( 'GET', '/__experimental/pattern-directory/patterns' );
$request->set_query_params( array( 'keyword' => 11 ) );
$response = rest_do_request( $request );
$patterns = $response->get_data();

$this->assertNotWPError( $response->as_error() );
$this->assertSame( 200, $response->status );
$this->assertGreaterThan( 0, count( $patterns ) );

array_walk( $patterns, array( $this, 'assertPatternMatchesSchema' ) );

foreach ( $patterns as $pattern ) {
$this->assertContains( 'core', $pattern['keywords'] );
}
}

/**
* @covers WP_REST_Pattern_Directory_Controller::get_items
*
Expand Down Expand Up @@ -304,6 +329,11 @@ private static function get_raw_response( $action ) {
$response = file_get_contents( __DIR__ . '/fixtures/rest-api/pattern-directory/browse-category-2.json' );
break;

case 'browse-keyword':
// Response from https://api.wordpress.org/patterns/1.0/?pattern-keywords=11.
$response = file_get_contents( __DIR__ . '/fixtures/rest-api/pattern-directory/browse-keyword-11.json' );
break;

case 'search':
// Response from https://api.wordpress.org/patterns/1.0/?search=button.
$response = file_get_contents( __DIR__ . '/fixtures/rest-api/pattern-directory/search-button.json' );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":31,"title":{"rendered":"Heading and paragraph"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<h2 class=\"has-large-font-size\"><span style=\"color:#ba0c49\" class=\"has-inline-color\"><strong>2<\/strong>.<\/span><br>Which treats of the first sally the ingenious Don Quixote made from home<\/h2>\n\n\n\n<p>These preliminaries settled, he did not care to put off any longer the execution of his design, urged on to it by the thought of all the world was losing by his delay, seeing what wrongs he intended to right, grievances to redress, injustices to repair, abuses to remove, and duties to discharge.<\/p>\n<\/div><\/div>\n","protected":false},"meta":{"spay_email":"","wpop_description":"A heading preceded by a chapter number, and followed by a paragraph.","wpop_viewport_width":1000},"category_slugs":["text"],"keyword_slugs":["core"]},{"id":25,"title":{"rendered":"Large header with a heading"},"content":{"rendered":"\n<div class=\"wp-block-cover alignwide has-background-dim-20 has-background-dim is-position-center-center\" style=\"background-image:url(https:\/\/s.w.org\/images\/core\/5.5\/don-quixote-06.jpg);min-height:375px;background-position:40% 26%\"><div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-text-color\" style=\"color:#fffffa;font-size:74px;line-height:1.1\"><strong>Don Quixote<\/strong><\/p>\n<\/div><\/div>\n","protected":false},"meta":{"spay_email":"","wpop_description":"A large hero section with an example background image and a heading in the center.","wpop_viewport_width":1000},"category_slugs":["header"],"keyword_slugs":["core"]},{"id":26,"title":{"rendered":"Large header with a heading and a button"},"content":{"rendered":"\n<div class=\"wp-block-cover alignwide has-background-dim has-background-gradient is-position-center-center\" style=\"background:linear-gradient(135deg,rgb(249,72,72) 1%,rgb(179,22,22) 100%);min-height:575px\"><div class=\"wp-block-cover__inner-container\">\n<div class=\"wp-block-columns alignwide\">\n<div class=\"wp-block-column\" style=\"flex-basis:12%\">\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column\">\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-text-align-left has-text-color\" style=\"color:#fffffa;font-size:68px;line-height:1.2\"><strong>Thou hast seen<\/strong><br><strong>nothing yet<\/strong><\/p>\n\n\n\n<div class=\"wp-block-buttons\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-text-color has-background\" style=\"border-radius:3px;background-color:#fffffa;color:#00000a\">Read now<\/a><\/div>\n<\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column\" style=\"flex-basis:12%\">\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"meta":{"spay_email":"","wpop_description":"A large hero section with a bright gradient background, a big heading and a filled button.","wpop_viewport_width":1000},"category_slugs":["header"],"keyword_slugs":["core"]}]