From eb955bab6dfa2fc33d0704a7c128436befae6617 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Fri, 21 May 2021 16:02:46 +1000 Subject: [PATCH] Initial commit. Checking pattern meta for block type values and adding them to the options array when registering blocks with `register_block_pattern` Added tests --- .../class-block-patterns-from-api.php | 2 + .../class-block-patterns-utils.php | 29 ++++++ .../class-block-patterns-from-api-test.php | 10 ++- .../test/class-block-patterns-utils-test.php | 90 +++++++++++++++++++ 4 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 apps/editing-toolkit/editing-toolkit-plugin/block-patterns/test/class-block-patterns-utils-test.php diff --git a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php index 23093085cc7ab..d59128447c295 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-from-api.php @@ -133,6 +133,7 @@ function ( $a, $b ) { $viewport_width = isset( $pattern['pattern_meta']['viewport_width'] ) ? intval( $pattern['pattern_meta']['viewport_width'] ) : 1280; $viewport_width = $viewport_width < 320 ? 320 : $viewport_width; $pattern_name = self::PATTERN_NAMESPACE . $pattern['name']; + $block_types = $this->utils->maybe_get_pattern_block_types_from_pattern_meta( $pattern ); $results[ $pattern_name ] = register_block_pattern( $pattern_name, @@ -145,6 +146,7 @@ function ( $a, $b ) { $pattern['categories'] ), 'isPremium' => $is_premium, + 'blockTypes' => $block_types, ) ); } diff --git a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php index 498478d523811..55934c7dc49a1 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/class-block-patterns-utils.php @@ -86,4 +86,33 @@ public function get_block_patterns_locale() { $language = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_locale(); return \A8C\FSE\Common\get_iso_639_locale( $language ); } + + /** + * Check for block type values in the pattern_meta tag. + * When tags have a prefix of `block_type_`, we expect the remaining suffix to be a blockType value. + * We'll add these values to the `(array) blockType` options property when registering the pattern + * via `register_block_pattern`. + * + * @param array $pattern A pattern with a 'pattern_meta' array. + * + * @return array An array of block types defined in pattern meta. + */ + public function maybe_get_pattern_block_types_from_pattern_meta( $pattern ) { + $block_types = array(); + + if ( ! isset( $pattern['pattern_meta'] ) || empty( $pattern['pattern_meta'] ) ) { + return $block_types; + } + + foreach ( $pattern['pattern_meta'] as $pattern_meta => $value ) { + // Match against tags starting with `block_type_`. + $split_slug = preg_split( '/^block_type_/', $pattern_meta ); + + if ( isset( $split_slug[1] ) ) { + $block_types[] = $split_slug[1]; + } + } + + return $block_types; + } } diff --git a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/test/class-block-patterns-from-api-test.php b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/test/class-block-patterns-from-api-test.php index 33d1d8c23eda2..a62e51f0fba77 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/test/class-block-patterns-from-api-test.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/block-patterns/test/class-block-patterns-from-api-test.php @@ -1,7 +1,9 @@ utils = new Block_Patterns_Utils(); + } + + /** + * Tests that we receive an empty block_types array where there are no block types in pattern_meta + */ + public function test_should_return_empty_array_from_block_types_check() { + $test_pattern = $this->get_test_pattern(); + $block_types = $this->utils->maybe_get_pattern_block_types_from_pattern_meta( $test_pattern ); + + $this->assertEmpty( $block_types ); + } + + /** + * Tests that we can parse block types from pattern_meta. + */ + public function test_should_return_block_types_from_patterns_meta() { + $test_pattern = $this->get_test_pattern( + array( + 'pattern_meta' => array( + 'block_type_core/template-part/footer' => true, + ), + ) + ); + $block_types = $this->utils->maybe_get_pattern_block_types_from_pattern_meta( $test_pattern ); + + $this->assertEquals( array( 'core/template-part/footer' ), $block_types ); + } + + /** + * Util function from grabbing a test pattern. + * + * @param array $new_pattern_values Values to merge into the default array. + * @return array A test pattern. + */ + private function get_test_pattern( $new_pattern_values = array() ) { + $default_pattern = array( + 'ID' => '1', + 'site_id' => '2', + 'title' => 'test title', + 'name' => 'test pattern name', + 'description' => 'test description', + 'html' => '

test

', + 'source_url' => 'http;//test', + 'modified_date' => 'dd:mm:YY', + 'categories' => array( + array( + 'title' => 'test-category', + ), + ), + 'pattern_meta' => array( + 'is_web' => true, + ), + ); + + return array_merge( $default_pattern, $new_pattern_values ); + } +}