From a71bf79d647e7d7ed9ec1df5c5a0c3fb85032e37 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 5 Jul 2022 16:03:10 +0000 Subject: [PATCH] Block Patterns: Update the value used for keywords. Patterns on the [https://wordpress.org/patterns/ Pattern Directory] can have keywords for better discoverability while searching. The way these are stored [https://github.com/WordPress/pattern-directory/commit/69548ff1f0326c4fd4d9ea7390c48b6d9d227055 was changed from a taxonomy to meta value], but the `/wp/v2/pattern-directory/patterns` endpoint was still pulling from that old value. The correct property to use for this field is `meta.wpop_keywords`, which returns a single string with comma-separated keywords. Props ryelle, TimothyBlynJacobs. Merges [53665] to the 6.0 branch. See #56126. git-svn-id: https://develop.svn.wordpress.org/branches/6.0@53666 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-pattern-directory-controller.php | 4 ++-- .../phpunit/data/blocks/pattern-directory/browse-all.json | 3 +++ .../data/blocks/pattern-directory/browse-category-2.json | 2 ++ .../data/blocks/pattern-directory/browse-keyword-11.json | 3 +++ .../data/blocks/pattern-directory/search-button.json | 4 ++++ .../tests/rest-api/rest-pattern-directory-controller.php | 7 +++---- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php index 64b4ce28f1..b0ba186a02 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php @@ -206,7 +206,7 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { 'title' => sanitize_text_field( $raw_pattern->title->rendered ), 'content' => wp_kses_post( $raw_pattern->pattern_content ), 'categories' => array_map( 'sanitize_title', $raw_pattern->category_slugs ), - 'keywords' => array_map( 'sanitize_title', $raw_pattern->keyword_slugs ), + 'keywords' => array_map( 'sanitize_text_field', explode( ',', $raw_pattern->meta->wpop_keywords ) ), 'description' => sanitize_text_field( $raw_pattern->meta->wpop_description ), 'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ), ); @@ -274,7 +274,7 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { ), 'keywords' => array( - 'description' => __( "The pattern's keyword slugs." ), + 'description' => __( "The pattern's keywords." ), 'type' => 'array', 'uniqueItems' => true, 'items' => array( 'type' => 'string' ), diff --git a/tests/phpunit/data/blocks/pattern-directory/browse-all.json b/tests/phpunit/data/blocks/pattern-directory/browse-all.json index 26131e33bb..8d0d2d4ba3 100644 --- a/tests/phpunit/data/blocks/pattern-directory/browse-all.json +++ b/tests/phpunit/data/blocks/pattern-directory/browse-all.json @@ -9,6 +9,7 @@ "meta": { "spay_email": "", "wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.", + "wpop_keywords": "blog post", "wpop_viewport_width": 1000 }, "category_slugs": [ "text" ], @@ -25,6 +26,7 @@ "meta": { "spay_email": "", "wpop_description": "A large hero section with an example background image and a heading in the center.", + "wpop_keywords": "header, hero", "wpop_viewport_width": 1000 }, "category_slugs": [ "header" ], @@ -41,6 +43,7 @@ "meta": { "spay_email": "", "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.", + "wpop_keywords": "call to action, hero section", "wpop_viewport_width": 1000 }, "category_slugs": [ "header" ], diff --git a/tests/phpunit/data/blocks/pattern-directory/browse-category-2.json b/tests/phpunit/data/blocks/pattern-directory/browse-category-2.json index 947da16bfd..1fce1a0f80 100644 --- a/tests/phpunit/data/blocks/pattern-directory/browse-category-2.json +++ b/tests/phpunit/data/blocks/pattern-directory/browse-category-2.json @@ -9,6 +9,7 @@ "meta": { "spay_email": "", "wpop_description": "Three filled buttons with rounded corners, side by side.", + "wpop_keywords": "", "wpop_viewport_width": 600 }, "category_slugs": [ "buttons" ], @@ -25,6 +26,7 @@ "meta": { "spay_email": "", "wpop_description": "Two buttons, one filled and one outlined, side by side.", + "wpop_keywords": "", "wpop_viewport_width": 500 }, "category_slugs": [ "buttons" ], diff --git a/tests/phpunit/data/blocks/pattern-directory/browse-keyword-11.json b/tests/phpunit/data/blocks/pattern-directory/browse-keyword-11.json index 26131e33bb..383d7eff61 100644 --- a/tests/phpunit/data/blocks/pattern-directory/browse-keyword-11.json +++ b/tests/phpunit/data/blocks/pattern-directory/browse-keyword-11.json @@ -9,6 +9,7 @@ "meta": { "spay_email": "", "wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.", + "wpop_keywords": "", "wpop_viewport_width": 1000 }, "category_slugs": [ "text" ], @@ -25,6 +26,7 @@ "meta": { "spay_email": "", "wpop_description": "A large hero section with an example background image and a heading in the center.", + "wpop_keywords": "", "wpop_viewport_width": 1000 }, "category_slugs": [ "header" ], @@ -41,6 +43,7 @@ "meta": { "spay_email": "", "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.", + "wpop_keywords": "", "wpop_viewport_width": 1000 }, "category_slugs": [ "header" ], diff --git a/tests/phpunit/data/blocks/pattern-directory/search-button.json b/tests/phpunit/data/blocks/pattern-directory/search-button.json index b135e0ac5a..10e5c6036f 100644 --- a/tests/phpunit/data/blocks/pattern-directory/search-button.json +++ b/tests/phpunit/data/blocks/pattern-directory/search-button.json @@ -9,6 +9,7 @@ "meta": { "spay_email": "", "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.", + "wpop_keywords": "", "wpop_viewport_width": 1000 }, "category_slugs": [ "header" ], @@ -25,6 +26,7 @@ "meta": { "spay_email": "", "wpop_description": "Three small columns of text, each with an outlined button with rounded corners at the bottom.", + "wpop_keywords": "", "wpop_viewport_width": 1000 }, "category_slugs": [ "columns" ], @@ -41,6 +43,7 @@ "meta": { "spay_email": "", "wpop_description": "Three filled buttons with rounded corners, side by side.", + "wpop_keywords": "", "wpop_viewport_width": 600 }, "category_slugs": [ "buttons" ], @@ -57,6 +60,7 @@ "meta": { "spay_email": "", "wpop_description": "Two buttons, one filled and one outlined, side by side.", + "wpop_keywords": "", "wpop_viewport_width": 500 }, "category_slugs": [ "buttons" ], diff --git a/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php b/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php index 43ca497edb..8c6e79f7a8 100644 --- a/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php +++ b/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php @@ -111,6 +111,9 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_ $this->assertGreaterThan( 0, count( $patterns ) ); array_walk( $patterns, array( $this, 'assertPatternMatchesSchema' ) ); + $this->assertSame( array( 'blog post' ), $patterns[0]['keywords'] ); + $this->assertSame( array( 'header', 'hero' ), $patterns[1]['keywords'] ); + $this->assertSame( array( 'call to action', 'hero section' ), $patterns[2]['keywords'] ); } /** @@ -157,10 +160,6 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_ $this->assertGreaterThan( 0, count( $patterns ) ); array_walk( $patterns, array( $this, 'assertPatternMatchesSchema' ) ); - - foreach ( $patterns as $pattern ) { - $this->assertContains( 'core', $pattern['keywords'] ); - } } /**