From 244a774aa39ccbd06b664afdc554c002032886b8 Mon Sep 17 00:00:00 2001 From: Luigi Date: Fri, 21 Jan 2022 11:10:02 +0100 Subject: [PATCH] Enable Mini Cart template-parts only for experimental builds #5598 Enable Mini Cart template-parts only for experimental builds --- src/BlockTemplatesController.php | 6 +++-- src/Domain/Services/FeatureGating.php | 28 +++++++++++++++++++++ src/Utils/BlockTemplateUtils.php | 35 +++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index fb3c65215db..c203bbe34f7 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -362,12 +362,14 @@ function ( $template ) use ( $template_slug ) { * @param array $slugs An array of slugs to retrieve templates for. * @param array $template_type wp_template or wp_template_part. * - * @return array + * @return array WP_Block_Template[] An array of block template objects. */ public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) { $templates_from_db = $this->get_block_templates_from_db( $slugs, $template_type ); $templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type ); - return array_merge( $templates_from_db, $templates_from_woo ); + $templates = array_merge( $templates_from_db, $templates_from_woo ); + return BlockTemplateUtils::filter_block_templates_by_feature_flag( $templates ); + } /** diff --git a/src/Domain/Services/FeatureGating.php b/src/Domain/Services/FeatureGating.php index 4431fdfbc0d..ee9fdb400aa 100644 --- a/src/Domain/Services/FeatureGating.php +++ b/src/Domain/Services/FeatureGating.php @@ -137,4 +137,32 @@ public function is_production_environment() { public function is_test_environment() { return self::TEST_ENVIRONMENT === $this->environment; } + + /** + * Returns core flag value. + * + * @return number + */ + public static function get_core_flag() { + return self::CORE_FLAG; + } + + /** + * Returns feature plugin flag value. + * + * @return number + */ + public static function get_feature_plugin_flag() { + return self::FEATURE_PLUGIN_FLAG; + } + + /** + * Returns experimental flag value. + * + * @return number + */ + public static function get_experimental_flag() { + return self::EXPERIMENTAL_FLAG; + } + } diff --git a/src/Utils/BlockTemplateUtils.php b/src/Utils/BlockTemplateUtils.php index 4e8414f489a..33b04569b78 100644 --- a/src/Utils/BlockTemplateUtils.php +++ b/src/Utils/BlockTemplateUtils.php @@ -1,6 +1,9 @@ get_flag(); + + /** + * An array of block templates with slug as key and flag as value. + * + * @var array + */ + $block_templates_with_feature_gate = array( + 'mini-cart' => $feature_gating->get_experimental_flag(), + ); + + return array_filter( + $block_templates, + function( $block_template ) use ( $flag, $block_templates_with_feature_gate ) { + if ( isset( $block_templates_with_feature_gate[ $block_template->slug ] ) ) { + return $block_templates_with_feature_gate[ $block_template->slug ] <= $flag; + } + return true; + } + ); + } + }