From 6e0a565f78c2032d79e0c5f9be2d73c48655c596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 5 Aug 2022 12:55:50 +0200 Subject: [PATCH 01/14] POC about registering patterns --- src/Domain/Bootstrap.php | 8 +++++++ src/Patterns/FilterPatterns.php | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/Patterns/FilterPatterns.php diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 186c18c52cc..b9ca3a33f0d 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -13,6 +13,7 @@ use Automattic\WooCommerce\Blocks\Domain\Services\GoogleAnalytics; use Automattic\WooCommerce\Blocks\InboxNotifications; use Automattic\WooCommerce\Blocks\Installer; +use Automattic\WooCommerce\Blocks\Patterns\FilterPatterns; use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate; use Automattic\WooCommerce\Blocks\Templates\ClassicTemplatesCompatibility; use Automattic\WooCommerce\Blocks\Payments\Api as PaymentsApi; @@ -119,6 +120,7 @@ function() { $this->container->get( ProductSearchResultsTemplate::class ); $this->container->get( ProductAttributeTemplate::class ); $this->container->get( ClassicTemplatesCompatibility::class ); + $this->container->get( FilterPatterns::class ); if ( $this->package->feature()->is_feature_plugin_build() ) { $this->container->get( PaymentsApi::class ); } @@ -332,6 +334,12 @@ function( Container $container ) { return $container->get( StoreApi::class )::container()->get( RoutesController::class ); } ); + $this->container->register( + FilterPatterns::class, + function () { + return new FilterPatterns(); + } + ); } /** diff --git a/src/Patterns/FilterPatterns.php b/src/Patterns/FilterPatterns.php new file mode 100644 index 00000000000..ab4201d4eb1 --- /dev/null +++ b/src/Patterns/FilterPatterns.php @@ -0,0 +1,41 @@ + __( 'WooCommerce', 'woo-gutenberg-products-block' ) ) + ); + + register_block_pattern( + 'my-plugin/powered-by-wordpress', + array( + 'title' => __( 'Powered by WordPress', 'woo-gutenberg-products-block' ), + 'categories' => array( 'woocommerce' ), + 'blockTypes' => array( 'woocommerce/stock-filter', 'woocommerce/price-filter' ), + 'content' => ' +
+ + + +
+', + ) + ); + } +} From 36384e8926f7bd5fe61b7620593bec11239b7b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Mon, 8 Aug 2022 13:48:33 +0200 Subject: [PATCH 02/14] POC load patterns from `/patterns` folder --- patterns/example.php | 12 ++ patterns/filters.php | 15 +++ phpcs.xml | 5 + src/Domain/Bootstrap.php | 2 +- src/Patterns/FilterPatterns.php | 203 ++++++++++++++++++++++++++++---- 5 files changed, 214 insertions(+), 23 deletions(-) create mode 100644 patterns/example.php create mode 100644 patterns/filters.php diff --git a/patterns/example.php b/patterns/example.php new file mode 100644 index 00000000000..a147264cb93 --- /dev/null +++ b/patterns/example.php @@ -0,0 +1,12 @@ + + + +

I am a pattern

+ diff --git a/patterns/filters.php b/patterns/filters.php new file mode 100644 index 00000000000..f65a13caab5 --- /dev/null +++ b/patterns/filters.php @@ -0,0 +1,15 @@ + + + +
+ + +
+ diff --git a/phpcs.xml b/phpcs.xml index 70199828a4b..eac8154c883 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -43,12 +43,17 @@ src/ tests/php + patterns src/ tests/php + + patterns + + tests/ diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index b9ca3a33f0d..c8b42252881 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -337,7 +337,7 @@ function( Container $container ) { $this->container->register( FilterPatterns::class, function () { - return new FilterPatterns(); + return new FilterPatterns( $this->package ); } ); } diff --git a/src/Patterns/FilterPatterns.php b/src/Patterns/FilterPatterns.php index ab4201d4eb1..374d27a1ced 100644 --- a/src/Patterns/FilterPatterns.php +++ b/src/Patterns/FilterPatterns.php @@ -1,41 +1,200 @@ patterns_path = $package->get_path( 'patterns' ); + + add_action( 'init', array( $this, 'register_block_patterns' ) ); } /** - * Register the patterns and categories. + * Register patterns under the `./patterns/` directory. + * Each pattern is defined as a PHP file and defines its metadata using plugin-style headers. + * The minimum required definition is: + * + * /** + * * Title: My Pattern + * * Slug: my-theme/my-pattern + * * + * + * The output of the PHP source corresponds to the content of the pattern, e.g.: + * + *

+ * + * Other settable fields include: + * + * - Description + * - Viewport Width + * - Categories (comma-separated values) + * - Keywords (comma-separated values) + * - Block Types (comma-separated values) + * - Inserter (yes/no) + * + * @since 6.0.0 */ - public function add_filter_patterns() { - register_block_pattern_category( - 'woocommerce', - array( 'label' => __( 'WooCommerce', 'woo-gutenberg-products-block' ) ) - ); + public function register_block_patterns() { + if ( ! class_exists( 'WP_Block_Patterns_Registry' ) ) { + return; + } - register_block_pattern( - 'my-plugin/powered-by-wordpress', - array( - 'title' => __( 'Powered by WordPress', 'woo-gutenberg-products-block' ), - 'categories' => array( 'woocommerce' ), - 'blockTypes' => array( 'woocommerce/stock-filter', 'woocommerce/price-filter' ), - 'content' => ' -
- - - -
-', - ) + $default_headers = array( + 'title' => 'Title', + 'slug' => 'Slug', + 'description' => 'Description', + 'viewportWidth' => 'Viewport Width', + 'categories' => 'Categories', + 'keywords' => 'Keywords', + 'blockTypes' => 'Block Types', + 'inserter' => 'Inserter', ); + + if ( file_exists( $this->patterns_path ) ) { + $files = glob( $this->patterns_path . '/*.php' ); + if ( $files ) { + foreach ( $files as $file ) { + $pattern_data = get_file_data( $file, $default_headers ); + + if ( empty( $pattern_data['slug'] ) ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %s: file name. */ + __( 'Could not register file "%s" as a block pattern ("Slug" field missing)', 'woo-gutenberg-products-block' ), + $file + ) + ), + '6.0.0' + ); + continue; + } + + if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern_data['slug'] ) ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %1s: file name; %2s: slug value found. */ + __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")', 'woo-gutenberg-products-block' ), + $file, + $pattern_data['slug'] + ) + ), + '6.0.0' + ); + } + + if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) { + continue; + } + + // Title is a required property. + if ( ! $pattern_data['title'] ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %1s: file name; %2s: slug value found. */ + __( 'Could not register file "%s" as a block pattern ("Title" field missing)', 'woo-gutenberg-products-block' ), + $file + ) + ), + '6.0.0' + ); + continue; + } + + // For properties of type array, parse data as comma-separated. + foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = array_filter( + preg_split( + '/[\s,]+/', + (string) $pattern_data[ $property ] + ) + ); + } else { + unset( $pattern_data[ $property ] ); + } + } + + // Parse properties of type int. + foreach ( array( 'viewportWidth' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = (int) $pattern_data[ $property ]; + } else { + unset( $pattern_data[ $property ] ); + } + } + + // Parse properties of type bool. + foreach ( array( 'inserter' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = in_array( + strtolower( $pattern_data[ $property ] ), + array( 'yes', 'true' ), + true + ); + } else { + unset( $pattern_data[ $property ] ); + } + } + + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction + $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woo-gutenberg-products-block' ); + if ( ! empty( $pattern_data['description'] ) ) { + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction + $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woo-gutenberg-products-block' ); + } + + // The actual pattern content is the output of the file. + ob_start(); + include $file; + $pattern_data['content'] = ob_get_clean(); + if ( ! $pattern_data['content'] ) { + continue; + } + + foreach ( $pattern_data['categories'] as $key => $category ) { + $category_slug = _wp_to_kebab_case( $category ); + + $pattern_data['categories'][ $key ] = $category_slug; + + register_block_pattern_category( + $category_slug, + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText + array( 'label' => __( $category, 'woo-gutenberg-products-block' ) ) + ); + } + + register_block_pattern( $pattern_data['slug'], $pattern_data ); + } + } + } } } From 945d20da325edb323febccfaddb35da192b77612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Mon, 8 Aug 2022 14:33:38 +0200 Subject: [PATCH 03/14] Change class name and remove unnecessary comment --- src/Domain/Bootstrap.php | 8 ++++---- src/Patterns/{FilterPatterns.php => Patterns.php} | 11 ++--------- 2 files changed, 6 insertions(+), 13 deletions(-) rename src/Patterns/{FilterPatterns.php => Patterns.php} (96%) diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index c8b42252881..97c454bb1e2 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -13,7 +13,7 @@ use Automattic\WooCommerce\Blocks\Domain\Services\GoogleAnalytics; use Automattic\WooCommerce\Blocks\InboxNotifications; use Automattic\WooCommerce\Blocks\Installer; -use Automattic\WooCommerce\Blocks\Patterns\FilterPatterns; +use Automattic\WooCommerce\Blocks\Patterns\Patterns; use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate; use Automattic\WooCommerce\Blocks\Templates\ClassicTemplatesCompatibility; use Automattic\WooCommerce\Blocks\Payments\Api as PaymentsApi; @@ -120,7 +120,7 @@ function() { $this->container->get( ProductSearchResultsTemplate::class ); $this->container->get( ProductAttributeTemplate::class ); $this->container->get( ClassicTemplatesCompatibility::class ); - $this->container->get( FilterPatterns::class ); + $this->container->get( Patterns::class ); if ( $this->package->feature()->is_feature_plugin_build() ) { $this->container->get( PaymentsApi::class ); } @@ -335,9 +335,9 @@ function( Container $container ) { } ); $this->container->register( - FilterPatterns::class, + Patterns::class, function () { - return new FilterPatterns( $this->package ); + return new Patterns( $this->package ); } ); } diff --git a/src/Patterns/FilterPatterns.php b/src/Patterns/Patterns.php similarity index 96% rename from src/Patterns/FilterPatterns.php rename to src/Patterns/Patterns.php index 374d27a1ced..a1a87691f37 100644 --- a/src/Patterns/FilterPatterns.php +++ b/src/Patterns/Patterns.php @@ -1,19 +1,12 @@ Date: Tue, 9 Aug 2022 15:07:00 +0200 Subject: [PATCH 04/14] Add all four blocks to the filters pattern --- patterns/example.php | 12 ------------ patterns/filters.php | 9 +++++++++ 2 files changed, 9 insertions(+), 12 deletions(-) delete mode 100644 patterns/example.php diff --git a/patterns/example.php b/patterns/example.php deleted file mode 100644 index a147264cb93..00000000000 --- a/patterns/example.php +++ /dev/null @@ -1,12 +0,0 @@ - - - -

I am a pattern

- diff --git a/patterns/filters.php b/patterns/filters.php index f65a13caab5..3b31f431bb8 100644 --- a/patterns/filters.php +++ b/patterns/filters.php @@ -7,9 +7,18 @@ */ ?> + +
+ +
+ + +
+ +
From 04f8903590f56989a7e58ec4b16612e077702d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Tue, 9 Aug 2022 15:13:03 +0200 Subject: [PATCH 05/14] Add all 4 blocks to the block type to allow for transformations --- patterns/filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patterns/filters.php b/patterns/filters.php index 3b31f431bb8..1805d410f76 100644 --- a/patterns/filters.php +++ b/patterns/filters.php @@ -3,7 +3,7 @@ * Title: WooCommerce Filters * Slug: woocommerce-blocks/all-filters * Categories: WooCommerce - * Block Types: woocommerce/stock-filter, woocommerce/price-filter + * Block Types: woocommerce/active-filters, woocommerce/price-filter, wp:woocommerce/attribute-filter, woocommerce/stock-filter */ ?> From 37f15a891edb8c4d936dab820b44024650a0f766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Wed, 10 Aug 2022 10:09:09 +0200 Subject: [PATCH 06/14] Add internal --- src/Patterns/Patterns.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Patterns/Patterns.php b/src/Patterns/Patterns.php index a1a87691f37..468ed53b071 100644 --- a/src/Patterns/Patterns.php +++ b/src/Patterns/Patterns.php @@ -5,6 +5,8 @@ /** * Patterns class + * + * @internal */ class Patterns { /** From e151104de81c080852825ffeb183042f362a8423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Wed, 10 Aug 2022 10:11:47 +0200 Subject: [PATCH 07/14] Improve filters name --- patterns/filters.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/patterns/filters.php b/patterns/filters.php index 1805d410f76..b7331459f7a 100644 --- a/patterns/filters.php +++ b/patterns/filters.php @@ -1,7 +1,7 @@ Date: Wed, 10 Aug 2022 10:20:48 +0200 Subject: [PATCH 08/14] Extract regex to constants --- src/Patterns/Patterns.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Patterns/Patterns.php b/src/Patterns/Patterns.php index 468ed53b071..4db21dbb31b 100644 --- a/src/Patterns/Patterns.php +++ b/src/Patterns/Patterns.php @@ -9,6 +9,9 @@ * @internal */ class Patterns { + const SLUG_REGEX = '/^[A-z0-9\/_-]+$/'; + const COMMA_SEPARATED_REGEX = '/[\s,]+/'; + /** * Path to the patterns directory. * @@ -89,7 +92,7 @@ public function register_block_patterns() { continue; } - if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern_data['slug'] ) ) { + if ( ! preg_match( self::SLUG_REGEX, $pattern_data['slug'] ) ) { _doing_it_wrong( 'register_block_patterns', esc_html( @@ -129,7 +132,7 @@ public function register_block_patterns() { if ( ! empty( $pattern_data[ $property ] ) ) { $pattern_data[ $property ] = array_filter( preg_split( - '/[\s,]+/', + self::COMMA_SEPARATED_REGEX, (string) $pattern_data[ $property ] ) ); From b355122173fc0676446a427fb80ba171c24e0c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Wed, 10 Aug 2022 10:40:10 +0200 Subject: [PATCH 09/14] Add class description --- src/Patterns/Patterns.php | 46 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/Patterns/Patterns.php b/src/Patterns/Patterns.php index 4db21dbb31b..3cfa39bc1a7 100644 --- a/src/Patterns/Patterns.php +++ b/src/Patterns/Patterns.php @@ -4,7 +4,27 @@ use Automattic\WooCommerce\Blocks\Domain\Package; /** - * Patterns class + * Registers patterns under the `./patterns/` directory. + * Each pattern is defined as a PHP file and defines its metadata using plugin-style headers. + * The minimum required definition is: + * + * /** + * * Title: My Pattern + * * Slug: my-theme/my-pattern + * * + * + * The output of the PHP source corresponds to the content of the pattern, e.g.: + * + *

+ * + * Other settable fields include: + * + * - Description + * - Viewport Width + * - Categories (comma-separated values) + * - Keywords (comma-separated values) + * - Block Types (comma-separated values) + * - Inserter (yes/no) * * @internal */ @@ -31,29 +51,7 @@ public function __construct( Package $package ) { } /** - * Register patterns under the `./patterns/` directory. - * Each pattern is defined as a PHP file and defines its metadata using plugin-style headers. - * The minimum required definition is: - * - * /** - * * Title: My Pattern - * * Slug: my-theme/my-pattern - * * - * - * The output of the PHP source corresponds to the content of the pattern, e.g.: - * - *

- * - * Other settable fields include: - * - * - Description - * - Viewport Width - * - Categories (comma-separated values) - * - Keywords (comma-separated values) - * - Block Types (comma-separated values) - * - Inserter (yes/no) - * - * @since 6.0.0 + * Registers the block patterns and categories under `./patterns/`. */ public function register_block_patterns() { if ( ! class_exists( 'WP_Block_Patterns_Registry' ) ) { From 496a3a5ca4daa6e167846677c8f969ff11045b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Wed, 10 Aug 2022 14:55:50 +0200 Subject: [PATCH 10/14] Rename and move class --- src/{Patterns/Patterns.php => BlockPatterns.php} | 11 +++++++++-- src/Domain/Bootstrap.php | 16 ++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) rename src/{Patterns/Patterns.php => BlockPatterns.php} (90%) diff --git a/src/Patterns/Patterns.php b/src/BlockPatterns.php similarity index 90% rename from src/Patterns/Patterns.php rename to src/BlockPatterns.php index 3cfa39bc1a7..3a7da71c164 100644 --- a/src/Patterns/Patterns.php +++ b/src/BlockPatterns.php @@ -1,7 +1,14 @@ container->get( ProductSearchResultsTemplate::class ); $this->container->get( ProductAttributeTemplate::class ); $this->container->get( ClassicTemplatesCompatibility::class ); - $this->container->get( Patterns::class ); + $this->container->get( BlockPatterns::class ); if ( $this->package->feature()->is_feature_plugin_build() ) { $this->container->get( PaymentsApi::class ); } @@ -335,9 +335,9 @@ function( Container $container ) { } ); $this->container->register( - Patterns::class, + BlockPatterns::class, function () { - return new Patterns( $this->package ); + return new BlockPatterns( $this->package ); } ); } From 3be0bb6e49d43bdf74ee0ed546a914c4c48324b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Wed, 10 Aug 2022 14:57:13 +0200 Subject: [PATCH 11/14] Fix wrong imports --- src/BlockPatterns.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/BlockPatterns.php b/src/BlockPatterns.php index 3a7da71c164..42081e00af4 100644 --- a/src/BlockPatterns.php +++ b/src/BlockPatterns.php @@ -2,13 +2,6 @@ namespace Automattic\WooCommerce\Blocks; use Automattic\WooCommerce\Blocks\Domain\Package; -use function Automattic\WooCommerce\Blocks\Patterns\_doing_it_wrong; -use function Automattic\WooCommerce\Blocks\Patterns\_wp_to_kebab_case; -use function Automattic\WooCommerce\Blocks\Patterns\add_action; -use function Automattic\WooCommerce\Blocks\Patterns\get_file_data; -use function Automattic\WooCommerce\Blocks\Patterns\register_block_pattern; -use function Automattic\WooCommerce\Blocks\Patterns\register_block_pattern_category; -use function Automattic\WooCommerce\Blocks\Patterns\translate_with_gettext_context; /** * Registers patterns under the `./patterns/` directory. From 8b1d0e8cb12b9d331a8741c6b6f7ab9e52aab898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Thu, 11 Aug 2022 09:46:14 +0200 Subject: [PATCH 12/14] Address feedback --- src/BlockPatterns.php | 209 +++++++++++++++++++++--------------------- 1 file changed, 106 insertions(+), 103 deletions(-) diff --git a/src/BlockPatterns.php b/src/BlockPatterns.php index 42081e00af4..f5ac03901dc 100644 --- a/src/BlockPatterns.php +++ b/src/BlockPatterns.php @@ -71,125 +71,128 @@ public function register_block_patterns() { if ( file_exists( $this->patterns_path ) ) { $files = glob( $this->patterns_path . '/*.php' ); - if ( $files ) { - foreach ( $files as $file ) { - $pattern_data = get_file_data( $file, $default_headers ); - - if ( empty( $pattern_data['slug'] ) ) { - _doing_it_wrong( - 'register_block_patterns', - esc_html( - sprintf( - /* translators: %s: file name. */ - __( 'Could not register file "%s" as a block pattern ("Slug" field missing)', 'woo-gutenberg-products-block' ), - $file - ) - ), - '6.0.0' - ); - continue; - } + if ( ! $files ) { + return; + } - if ( ! preg_match( self::SLUG_REGEX, $pattern_data['slug'] ) ) { - _doing_it_wrong( - 'register_block_patterns', - esc_html( - sprintf( - /* translators: %1s: file name; %2s: slug value found. */ - __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")', 'woo-gutenberg-products-block' ), - $file, - $pattern_data['slug'] - ) - ), - '6.0.0' - ); - } + foreach ( $files as $file ) { + $pattern_data = get_file_data( $file, $default_headers ); + + if ( empty( $pattern_data['slug'] ) ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %s: file name. */ + __( 'Could not register file "%s" as a block pattern ("Slug" field missing)', 'woo-gutenberg-products-block' ), + $file + ) + ), + '6.0.0' + ); + continue; + } - if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) { - continue; - } + if ( ! preg_match( self::SLUG_REGEX, $pattern_data['slug'] ) ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %1s: file name; %2s: slug value found. */ + __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")', 'woo-gutenberg-products-block' ), + $file, + $pattern_data['slug'] + ) + ), + '6.0.0' + ); + continue; + } - // Title is a required property. - if ( ! $pattern_data['title'] ) { - _doing_it_wrong( - 'register_block_patterns', - esc_html( - sprintf( - /* translators: %1s: file name; %2s: slug value found. */ - __( 'Could not register file "%s" as a block pattern ("Title" field missing)', 'woo-gutenberg-products-block' ), - $file - ) - ), - '6.0.0' - ); - continue; - } + if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) { + continue; + } - // For properties of type array, parse data as comma-separated. - foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { - if ( ! empty( $pattern_data[ $property ] ) ) { - $pattern_data[ $property ] = array_filter( - preg_split( - self::COMMA_SEPARATED_REGEX, - (string) $pattern_data[ $property ] - ) - ); - } else { - unset( $pattern_data[ $property ] ); - } - } + // Title is a required property. + if ( ! $pattern_data['title'] ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %1s: file name; %2s: slug value found. */ + __( 'Could not register file "%s" as a block pattern ("Title" field missing)', 'woo-gutenberg-products-block' ), + $file + ) + ), + '6.0.0' + ); + continue; + } - // Parse properties of type int. - foreach ( array( 'viewportWidth' ) as $property ) { - if ( ! empty( $pattern_data[ $property ] ) ) { - $pattern_data[ $property ] = (int) $pattern_data[ $property ]; - } else { - unset( $pattern_data[ $property ] ); - } + // For properties of type array, parse data as comma-separated. + foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = array_filter( + preg_split( + self::COMMA_SEPARATED_REGEX, + (string) $pattern_data[ $property ] + ) + ); + } else { + unset( $pattern_data[ $property ] ); } + } - // Parse properties of type bool. - foreach ( array( 'inserter' ) as $property ) { - if ( ! empty( $pattern_data[ $property ] ) ) { - $pattern_data[ $property ] = in_array( - strtolower( $pattern_data[ $property ] ), - array( 'yes', 'true' ), - true - ); - } else { - unset( $pattern_data[ $property ] ); - } + // Parse properties of type int. + foreach ( array( 'viewportWidth' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = (int) $pattern_data[ $property ]; + } else { + unset( $pattern_data[ $property ] ); } + } - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction - $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woo-gutenberg-products-block' ); - if ( ! empty( $pattern_data['description'] ) ) { - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction - $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woo-gutenberg-products-block' ); + // Parse properties of type bool. + foreach ( array( 'inserter' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = in_array( + strtolower( $pattern_data[ $property ] ), + array( 'yes', 'true' ), + true + ); + } else { + unset( $pattern_data[ $property ] ); } + } - // The actual pattern content is the output of the file. - ob_start(); - include $file; - $pattern_data['content'] = ob_get_clean(); - if ( ! $pattern_data['content'] ) { - continue; - } + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction + $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woo-gutenberg-products-block' ); + if ( ! empty( $pattern_data['description'] ) ) { + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction + $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woo-gutenberg-products-block' ); + } - foreach ( $pattern_data['categories'] as $key => $category ) { - $category_slug = _wp_to_kebab_case( $category ); + // The actual pattern content is the output of the file. + ob_start(); + include $file; + $pattern_data['content'] = ob_get_clean(); + if ( ! $pattern_data['content'] ) { + continue; + } - $pattern_data['categories'][ $key ] = $category_slug; + foreach ( $pattern_data['categories'] as $key => $category ) { + $category_slug = _wp_to_kebab_case( $category ); - register_block_pattern_category( - $category_slug, - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText - array( 'label' => __( $category, 'woo-gutenberg-products-block' ) ) - ); - } + $pattern_data['categories'][ $key ] = $category_slug; - register_block_pattern( $pattern_data['slug'], $pattern_data ); + register_block_pattern_category( + $category_slug, + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText + array( 'label' => __( $category, 'woo-gutenberg-products-block' ) ) + ); } + + register_block_pattern( $pattern_data['slug'], $pattern_data ); } } } From e221bb3bb07207b2f2941897e813ba201f211112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Thu, 11 Aug 2022 11:36:01 +0200 Subject: [PATCH 13/14] Early return from condition --- src/BlockPatterns.php | 216 +++++++++++++++++++++--------------------- 1 file changed, 109 insertions(+), 107 deletions(-) diff --git a/src/BlockPatterns.php b/src/BlockPatterns.php index f5ac03901dc..290ad440837 100644 --- a/src/BlockPatterns.php +++ b/src/BlockPatterns.php @@ -69,131 +69,133 @@ public function register_block_patterns() { 'inserter' => 'Inserter', ); - if ( file_exists( $this->patterns_path ) ) { - $files = glob( $this->patterns_path . '/*.php' ); - if ( ! $files ) { - return; + if ( ! file_exists( $this->patterns_path ) ) { + return; + } + + $files = glob( $this->patterns_path . '/*.php' ); + if ( ! $files ) { + return; + } + + foreach ( $files as $file ) { + $pattern_data = get_file_data( $file, $default_headers ); + + if ( empty( $pattern_data['slug'] ) ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %s: file name. */ + __( 'Could not register file "%s" as a block pattern ("Slug" field missing)', 'woo-gutenberg-products-block' ), + $file + ) + ), + '6.0.0' + ); + continue; } - foreach ( $files as $file ) { - $pattern_data = get_file_data( $file, $default_headers ); - - if ( empty( $pattern_data['slug'] ) ) { - _doing_it_wrong( - 'register_block_patterns', - esc_html( - sprintf( - /* translators: %s: file name. */ - __( 'Could not register file "%s" as a block pattern ("Slug" field missing)', 'woo-gutenberg-products-block' ), - $file - ) - ), - '6.0.0' - ); - continue; - } + if ( ! preg_match( self::SLUG_REGEX, $pattern_data['slug'] ) ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %1s: file name; %2s: slug value found. */ + __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")', 'woo-gutenberg-products-block' ), + $file, + $pattern_data['slug'] + ) + ), + '6.0.0' + ); + continue; + } - if ( ! preg_match( self::SLUG_REGEX, $pattern_data['slug'] ) ) { - _doing_it_wrong( - 'register_block_patterns', - esc_html( - sprintf( - /* translators: %1s: file name; %2s: slug value found. */ - __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")', 'woo-gutenberg-products-block' ), - $file, - $pattern_data['slug'] - ) - ), - '6.0.0' - ); - continue; - } + if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) { + continue; + } - if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) { - continue; - } + // Title is a required property. + if ( ! $pattern_data['title'] ) { + _doing_it_wrong( + 'register_block_patterns', + esc_html( + sprintf( + /* translators: %1s: file name; %2s: slug value found. */ + __( 'Could not register file "%s" as a block pattern ("Title" field missing)', 'woo-gutenberg-products-block' ), + $file + ) + ), + '6.0.0' + ); + continue; + } - // Title is a required property. - if ( ! $pattern_data['title'] ) { - _doing_it_wrong( - 'register_block_patterns', - esc_html( - sprintf( - /* translators: %1s: file name; %2s: slug value found. */ - __( 'Could not register file "%s" as a block pattern ("Title" field missing)', 'woo-gutenberg-products-block' ), - $file - ) - ), - '6.0.0' + // For properties of type array, parse data as comma-separated. + foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = array_filter( + preg_split( + self::COMMA_SEPARATED_REGEX, + (string) $pattern_data[ $property ] + ) ); - continue; - } - - // For properties of type array, parse data as comma-separated. - foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { - if ( ! empty( $pattern_data[ $property ] ) ) { - $pattern_data[ $property ] = array_filter( - preg_split( - self::COMMA_SEPARATED_REGEX, - (string) $pattern_data[ $property ] - ) - ); - } else { - unset( $pattern_data[ $property ] ); - } + } else { + unset( $pattern_data[ $property ] ); } + } - // Parse properties of type int. - foreach ( array( 'viewportWidth' ) as $property ) { - if ( ! empty( $pattern_data[ $property ] ) ) { - $pattern_data[ $property ] = (int) $pattern_data[ $property ]; - } else { - unset( $pattern_data[ $property ] ); - } + // Parse properties of type int. + foreach ( array( 'viewportWidth' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = (int) $pattern_data[ $property ]; + } else { + unset( $pattern_data[ $property ] ); } + } - // Parse properties of type bool. - foreach ( array( 'inserter' ) as $property ) { - if ( ! empty( $pattern_data[ $property ] ) ) { - $pattern_data[ $property ] = in_array( - strtolower( $pattern_data[ $property ] ), - array( 'yes', 'true' ), - true - ); - } else { - unset( $pattern_data[ $property ] ); - } + // Parse properties of type bool. + foreach ( array( 'inserter' ) as $property ) { + if ( ! empty( $pattern_data[ $property ] ) ) { + $pattern_data[ $property ] = in_array( + strtolower( $pattern_data[ $property ] ), + array( 'yes', 'true' ), + true + ); + } else { + unset( $pattern_data[ $property ] ); } + } + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction + $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woo-gutenberg-products-block' ); + if ( ! empty( $pattern_data['description'] ) ) { // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction - $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woo-gutenberg-products-block' ); - if ( ! empty( $pattern_data['description'] ) ) { - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction - $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woo-gutenberg-products-block' ); - } + $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woo-gutenberg-products-block' ); + } - // The actual pattern content is the output of the file. - ob_start(); - include $file; - $pattern_data['content'] = ob_get_clean(); - if ( ! $pattern_data['content'] ) { - continue; - } + // The actual pattern content is the output of the file. + ob_start(); + include $file; + $pattern_data['content'] = ob_get_clean(); + if ( ! $pattern_data['content'] ) { + continue; + } - foreach ( $pattern_data['categories'] as $key => $category ) { - $category_slug = _wp_to_kebab_case( $category ); + foreach ( $pattern_data['categories'] as $key => $category ) { + $category_slug = _wp_to_kebab_case( $category ); - $pattern_data['categories'][ $key ] = $category_slug; + $pattern_data['categories'][ $key ] = $category_slug; - register_block_pattern_category( - $category_slug, - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText - array( 'label' => __( $category, 'woo-gutenberg-products-block' ) ) - ); - } - - register_block_pattern( $pattern_data['slug'], $pattern_data ); + register_block_pattern_category( + $category_slug, + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText + array( 'label' => __( $category, 'woo-gutenberg-products-block' ) ) + ); } + + register_block_pattern( $pattern_data['slug'], $pattern_data ); } } } From 4bab173aeb44e96b6f9ab74a293e691f36355fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 12 Aug 2022 09:25:09 +0200 Subject: [PATCH 14/14] Fix wrong block type --- patterns/filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patterns/filters.php b/patterns/filters.php index b7331459f7a..66d6c44162e 100644 --- a/patterns/filters.php +++ b/patterns/filters.php @@ -3,7 +3,7 @@ * Title: WooCommerce Product Filters * Slug: woocommerce-blocks/product-filters * Categories: WooCommerce - * Block Types: woocommerce/active-filters, woocommerce/price-filter, wp:woocommerce/attribute-filter, woocommerce/stock-filter + * Block Types: woocommerce/active-filters, woocommerce/price-filter, woocommerce/attribute-filter, woocommerce/stock-filter */ ?>