Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Enable Mini Cart template-parts only for experimental builds #5598
Browse files Browse the repository at this point in the history
Enable Mini Cart template-parts only for experimental builds
  • Loading branch information
gigitux committed Jan 21, 2022
1 parent c56757f commit 244a774
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Domain/Services/FeatureGating.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
35 changes: 35 additions & 0 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Automattic\WooCommerce\Blocks\Utils;

use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;


/**
* BlockTemplateUtils class used for serving block templates from Woo Blocks.
* IMPORTANT: These methods have been duplicated from Gutenberg/lib/full-site-editing/block-templates.php as those functions are not for public usage.
Expand Down Expand Up @@ -433,4 +436,36 @@ public static function set_has_theme_file_if_fallback_is_available( $query_resul

return false;
}

/**
* Filter block templates by feature flag.
*
* @param WP_Block_Template[] $block_templates An array of block template objects.
*
* @return WP_Block_Template[] An array of block template objects.
*/
public static function filter_block_templates_by_feature_flag( $block_templates ) {
$feature_gating = new FeatureGating();
$flag = $feature_gating->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;
}
);
}

}

0 comments on commit 244a774

Please sign in to comment.