From 3083bf6083d670a62544e5e0fc3689ecba255639 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Fri, 7 May 2021 17:01:54 +1000 Subject: [PATCH 1/5] Remove the block that filters out core blocks from the patterns inserter --- .../block-patterns/class-block-patterns-from-api.php | 9 --------- 1 file changed, 9 deletions(-) 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 7b112bed82cac..53b6e32bf8b69 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 @@ -62,15 +62,6 @@ public function __construct( $patterns_sources, Block_Patterns_Utils $utils = nu * @return array Results of pattern registration. */ public function register_patterns() { - if ( class_exists( 'WP_Block_Patterns_Registry' ) ) { - // Remove core patterns. - foreach ( \WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $pattern ) { - if ( 'core/' === substr( $pattern['name'], 0, 5 ) ) { - unregister_block_pattern( $pattern['name'] ); - } - } - } - // Used to track which patterns we successfully register. $results = array(); From 7e7fbc4e845094cf4d67e40093f529200db2b837 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Fri, 14 May 2021 13:26:49 +1000 Subject: [PATCH 2/5] This commit deregisters all patterns including those added by Gutenberg. We've also added a method to reregister core patterns when we want to update their categories to reflect WPCOM categories. Also removing the unused get_block_patterns_locale() method, which was abstracted into Block_Patterns_Utils in #51423 --- .../class-block-patterns-from-api.php | 84 ++++++++++++++++--- 1 file changed, 72 insertions(+), 12 deletions(-) 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 53b6e32bf8b69..7c8f4476e8b9a 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 @@ -44,6 +44,15 @@ class Block_Patterns_From_API { */ private $utils; + /** + * A dictionary to map existing WPCOM pattern categories to core patterns. + * These should match the categories in $patterns_sources, + * which are registered in $this->register_patterns() + * + * @var array + */ + private $core_to_wpcom_categories_dictionary; + /** * Block_Patterns constructor. * @@ -51,9 +60,17 @@ class Block_Patterns_From_API { * @param Block_Patterns_Utils $utils A class dependency containing utils methods. */ public function __construct( $patterns_sources, Block_Patterns_Utils $utils = null ) { - $patterns_sources = empty( $patterns_sources ) ? array( 'block_patterns' ) : $patterns_sources; - $this->patterns_sources = empty( array_diff( $patterns_sources, $this->valid_patterns_sources ) ) ? $patterns_sources : array( 'block_patterns' ); - $this->utils = empty( $utils ) ? new \A8C\FSE\Block_Patterns_Utils() : $utils; + $patterns_sources = empty( $patterns_sources ) ? array( 'block_patterns' ) : $patterns_sources; + $this->patterns_sources = empty( array_diff( $patterns_sources, $this->valid_patterns_sources ) ) ? $patterns_sources : array( 'block_patterns' ); + $this->utils = empty( $utils ) ? new \A8C\FSE\Block_Patterns_Utils() : $utils; + // Add categories to this array using the core pattern name as the key + // for core patterns we wish to "recategorize" + $this->core_to_wpcom_categories_dictionary = array( + 'core/quote' => array( + 'quotes' => __( 'Quotes', 'full-site-editing' ), + 'text' => __( 'Text', 'full-site-editing' ), + ), + ); } /** @@ -62,6 +79,8 @@ public function __construct( $patterns_sources, Block_Patterns_Utils $utils = nu * @return array Results of pattern registration. */ public function register_patterns() { + $this->reregister_core_patterns(); + // Used to track which patterns we successfully register. $results = array(); @@ -132,6 +151,9 @@ function ( $a, $b ) { } } } + + $this->update_core_patterns_with_wpcom_categories(); + return $results; } @@ -179,21 +201,13 @@ private function get_patterns( $patterns_cache_key, $patterns_source ) { ); $block_patterns = $this->utils->remote_get( $request_url ); + $this->utils->cache_add( $patterns_cache_key, $block_patterns, 'ptk_patterns', DAY_IN_SECONDS ); } return $block_patterns; } - /** - * Get the locale to be used for fetching block patterns - */ - private function get_block_patterns_locale() { - // Make sure to get blog locale, not user locale. - $language = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_locale(); - return \A8C\FSE\Common\get_iso_639_locale( $language ); - } - /** * Check that the pattern is allowed to be registered. * @@ -227,5 +241,51 @@ private function can_register_pattern( $pattern ) { return true; } + + /** + * Unregister all core patterns, then reregister core patterns in core Wordpress only, + * that is those in wp-includes/block-patterns.php + * Gutenberg adds new and overrides existing core patterns. We don't want these for now. + */ + private function reregister_core_patterns() { + if ( class_exists( 'WP_Block_Patterns_Registry' ) ) { + foreach ( \WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $pattern ) { + // Gutenberg registers patterns with varying prefixes, but categorizes them using `core/*` in a blockTypes array. + // This will ensure we remove `query/*` blocks for example. + $pattern_block_type_or_name = ! empty( $pattern['blockTypes'][0] ) ? $pattern['blockTypes'][0] : $pattern['name']; + if ( 'core/' === substr( $pattern_block_type_or_name, 0, 5 ) ) { + unregister_block_pattern( $pattern['name'] ); + } + + } + if ( function_exists( '_register_core_block_patterns_and_categories' ) ) { + switch_to_locale( $this->utils->get_block_patterns_locale() ); + _register_core_block_patterns_and_categories(); + } + } + } + // + /** + * Update categories for core patterns if a records exists in $this->core_to_wpcom_categories_dictionary + * and reregister them. + */ + private function update_core_patterns_with_wpcom_categories() { + if ( class_exists( 'WP_Block_Patterns_Registry' ) ) { + foreach ( \WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $pattern ) { + $wpcom_categories = $this->core_to_wpcom_categories_dictionary[ $pattern['name'] ]; + if ( isset( $wpcom_categories ) ) { + unregister_block_pattern( $pattern['name'] ); + $pattern_properties = array_merge( $pattern, array( 'categories' => array_keys( + $wpcom_categories + ) ) ); + unset( $pattern_properties['name'] ); + register_block_pattern( + $pattern['name'], + $pattern_properties + ); + } + } + } + } } From e16d3cf036aaa9df914d33481ebda0d49d7a7ac7 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Mon, 17 May 2021 12:21:22 +1000 Subject: [PATCH 3/5] Formatting --- .../class-block-patterns-from-api.php | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) 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 7c8f4476e8b9a..72089ef5fe383 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 @@ -60,11 +60,10 @@ class Block_Patterns_From_API { * @param Block_Patterns_Utils $utils A class dependency containing utils methods. */ public function __construct( $patterns_sources, Block_Patterns_Utils $utils = null ) { - $patterns_sources = empty( $patterns_sources ) ? array( 'block_patterns' ) : $patterns_sources; - $this->patterns_sources = empty( array_diff( $patterns_sources, $this->valid_patterns_sources ) ) ? $patterns_sources : array( 'block_patterns' ); - $this->utils = empty( $utils ) ? new \A8C\FSE\Block_Patterns_Utils() : $utils; - // Add categories to this array using the core pattern name as the key - // for core patterns we wish to "recategorize" + $patterns_sources = empty( $patterns_sources ) ? array( 'block_patterns' ) : $patterns_sources; + $this->patterns_sources = empty( array_diff( $patterns_sources, $this->valid_patterns_sources ) ) ? $patterns_sources : array( 'block_patterns' ); + $this->utils = empty( $utils ) ? new \A8C\FSE\Block_Patterns_Utils() : $utils; + // Add categories to this array using the core pattern name as the key for core patterns we wish to "recategorize". $this->core_to_wpcom_categories_dictionary = array( 'core/quote' => array( 'quotes' => __( 'Quotes', 'full-site-editing' ), @@ -243,7 +242,7 @@ private function can_register_pattern( $pattern ) { } /** - * Unregister all core patterns, then reregister core patterns in core Wordpress only, + * Unregister all core patterns, then reregister core patterns in core WordPress only, * that is those in wp-includes/block-patterns.php * Gutenberg adds new and overrides existing core patterns. We don't want these for now. */ @@ -256,7 +255,6 @@ private function reregister_core_patterns() { if ( 'core/' === substr( $pattern_block_type_or_name, 0, 5 ) ) { unregister_block_pattern( $pattern['name'] ); } - } if ( function_exists( '_register_core_block_patterns_and_categories' ) ) { switch_to_locale( $this->utils->get_block_patterns_locale() ); @@ -264,7 +262,7 @@ private function reregister_core_patterns() { } } } - // + /** * Update categories for core patterns if a records exists in $this->core_to_wpcom_categories_dictionary * and reregister them. @@ -275,13 +273,14 @@ private function update_core_patterns_with_wpcom_categories() { $wpcom_categories = $this->core_to_wpcom_categories_dictionary[ $pattern['name'] ]; if ( isset( $wpcom_categories ) ) { unregister_block_pattern( $pattern['name'] ); - $pattern_properties = array_merge( $pattern, array( 'categories' => array_keys( - $wpcom_categories - ) ) ); + $pattern_properties = array_merge( + $pattern, + array( 'categories' => array_keys( $wpcom_categories ) ) + ); unset( $pattern_properties['name'] ); register_block_pattern( $pattern['name'], - $pattern_properties + $pattern_properties, ); } } From bff926780c1b88cd18e146afb86b25cff97b73cf Mon Sep 17 00:00:00 2001 From: ramonjd Date: Mon, 17 May 2021 12:38:25 +1000 Subject: [PATCH 4/5] Added a todo to revisit the use of `blockTypes` Restoring current locale if needed --- .../block-patterns/class-block-patterns-from-api.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 72089ef5fe383..d605014561484 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 @@ -251,14 +251,19 @@ private function reregister_core_patterns() { foreach ( \WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $pattern ) { // Gutenberg registers patterns with varying prefixes, but categorizes them using `core/*` in a blockTypes array. // This will ensure we remove `query/*` blocks for example. + // TODO: We need to revisit our usage or $pattern['blockTypes']: they are currently an experimental feature and not guaranteed to reference `core/*` blocks. $pattern_block_type_or_name = ! empty( $pattern['blockTypes'][0] ) ? $pattern['blockTypes'][0] : $pattern['name']; if ( 'core/' === substr( $pattern_block_type_or_name, 0, 5 ) ) { unregister_block_pattern( $pattern['name'] ); } } if ( function_exists( '_register_core_block_patterns_and_categories' ) ) { - switch_to_locale( $this->utils->get_block_patterns_locale() ); + $did_switch_locale = switch_to_locale( $this->utils->get_block_patterns_locale() ); _register_core_block_patterns_and_categories(); + // The site locale might be the same as the current locale so switching could have failed in such instances. + if ( $did_switch_locale !== false ) { + restore_previous_locale(); + } } } } @@ -280,7 +285,7 @@ private function update_core_patterns_with_wpcom_categories() { unset( $pattern_properties['name'] ); register_block_pattern( $pattern['name'], - $pattern_properties, + $pattern_properties ); } } From a28ad6721b154c0afaec7016ab5f3fb52a82eefd Mon Sep 17 00:00:00 2001 From: ramonjd Date: Mon, 17 May 2021 12:46:59 +1000 Subject: [PATCH 5/5] YODA checks the linter still wants. --- .../block-patterns/class-block-patterns-from-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d605014561484..abb178524de24 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 @@ -261,7 +261,7 @@ private function reregister_core_patterns() { $did_switch_locale = switch_to_locale( $this->utils->get_block_patterns_locale() ); _register_core_block_patterns_and_categories(); // The site locale might be the same as the current locale so switching could have failed in such instances. - if ( $did_switch_locale !== false ) { + if ( false !== $did_switch_locale ) { restore_previous_locale(); } }