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

Commit

Permalink
Add migration strategy and load blockified templates (#6538)
Browse files Browse the repository at this point in the history
* Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag

If the schema version stored on the db is <= 260 means the plugin is being updated
to the new version and we should keep using the old templates, not the blockified ones.

* After the theme is switched we check if we need to update the flag to
start loading the blockified templates or not.

* Get the templates from the blockified folder if the flag is set to true on the db

* Add temporary blockified template for testing purposes

* Inline variable

* Improve comment

* Use blockified templates on new installs with block themes only

* Don't use blockified templates when switching to a non FSE theme

* Fix condition

* Add tests for the option value

* Move the check to use blockified templates

* WIP

* WIP

* Add migration strategy

* Move the blockified templates to the templates folder and filter them depending on the option

* Fix tests and start using the Options constants

* Fix migration, the `should_use_blockified_product_grid_templates` and tests

* Rename tests and move to Utils folder

* add Migration class test

* try

* remove PHP

* add composer

* Replace the blockified templates with a temporary version

* Fix tests

* add comment

* Add feature gating for experimental builds

* Inject the package to the controller

* test

* Change blocks.ini

* debug

* Remove debug info

* fix test

* fix tests

* try now

* using composer cache

* install deps

* test

* Remove unnecessary extra key

* Add cache actions

* Undo tests change

* Fix readme format

Co-authored-by: Luigi <gigitux@gmail.com>
  • Loading branch information
albarin and gigitux authored Jul 6, 2022
1 parent aaee302 commit e59611b
Show file tree
Hide file tree
Showing 20 changed files with 440 additions and 7 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ jobs:
- name: blocks.ini setup
run: echo 'woocommerce_blocks_phase = 3' > blocks.ini

- name: Get Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Set up Composer caching
uses: actions/cache@v2
env:
cache-name: cache-composer-dependencies
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install PHP deps
run: composer install

- name: Setup Environment
run: |
chmod -R 767 ./ #TODO: Might get implemented in wp-env
Expand Down
6 changes: 5 additions & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
},
"config": {
"JETPACK_AUTOLOAD_DEV": true,
"SCRIPT_DEBUG": false
"SCRIPT_DEBUG": false,
"WP_TESTS_DOMAIN": "http://localhost:8889",
"WP_TESTS_EMAIL": "admin@example.org",
"WP_TESTS_TITLE": "Test Blog",
"WP_PHP_BINARY": "php"
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"test:e2e-dev-watch": "npm run wp-env:config && cross-env JEST_PUPPETEER_CONFIG=tests/e2e/config/jest-puppeteer.config.js NODE_CONFIG_DIR=tests/e2e/config wp-scripts test-e2e --config tests/e2e/config/jest.config.js --watch --puppeteer-interactive",
"test:e2e:update": "npm run wp-env:config && cross-env JEST_PUPPETEER_CONFIG=tests/e2e/config/jest-puppeteer.config.js NODE_CONFIG_DIR=tests/e2e/config wp-scripts test-e2e --config tests/e2e/config/jest.config.js --updateSnapshot",
"test:help": "wp-scripts test-unit-js --help",
"pretest:php": "npm run wp-env run composer 'install --no-interaction'",
"test:performance": "npm run wp-env:config && cross-env NODE_CONFIG_DIR=tests/e2e/config wp-scripts test-e2e --config tests/e2e/config/jest.performance.config.js -- performance",
"test:php": "npm run wp-env run phpunit 'phpunit -c /var/www/html/wp-content/plugins/$(basename $(pwd))/phpunit.xml.dist'",
"test:update": "wp-scripts test-unit-js --updateSnapshot --config tests/js/jest.config.json",
Expand Down
50 changes: 49 additions & 1 deletion src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Automattic\WooCommerce\Blocks;

use Automattic\WooCommerce\Blocks\Domain\Package;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;

/**
Expand All @@ -10,6 +11,13 @@
*/
class BlockTemplatesController {

/**
* Holds the Package instance
*
* @var Package
*/
private $package;

/**
* Holds the path for the directory where the block templates will be kept.
*
Expand All @@ -33,8 +41,12 @@ class BlockTemplatesController {

/**
* Constructor.
*
* @param Package $package An instance of Package.
*/
public function __construct() {
public function __construct( Package $package ) {
$this->package = $package;

// This feature is gated for WooCommerce versions 6.0.0 and above.
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '6.0.0', '>=' ) ) {
$root_path = plugin_dir_path( __DIR__ ) . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR;
Expand All @@ -52,6 +64,30 @@ protected function init() {
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
add_filter( 'current_theme_supports-block-templates', array( $this, 'remove_block_template_support_for_shop_page' ) );

if ( $this->package->is_experimental_build() ) {
add_action( 'after_switch_theme', array( $this, 'check_should_use_blockified_product_grid_templates' ), 10, 2 );
}
}

/**
* Checks the old and current themes and determines if the "wc_blocks_use_blockified_product_grid_block_as_template"
* option need to be updated accordingly.
*
* @param string $old_name Old theme name.
* @param \WP_Theme $old_theme Instance of the old theme.
* @return void
*/
public function check_should_use_blockified_product_grid_templates( $old_name, $old_theme ) {
if ( ! wc_current_theme_is_fse_theme() ) {
update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( false ) );
return;
}

if ( ! $old_theme->is_block_theme() && wc_current_theme_is_fse_theme() ) {
update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( true ) );
return;
}
}

/**
Expand Down Expand Up @@ -262,6 +298,13 @@ public function get_block_templates_from_woocommerce( $slugs, $already_found_tem
$templates = array();

foreach ( $template_files as $template_file ) {
// Skip the template if it's blockified, and we should only use classic ones.
if ( $this->package->is_experimental_build() &&
! BlockTemplateUtils::should_use_blockified_product_grid_templates() &&
strpos( $template_file, 'blockified' ) !== false ) {
continue;
}

$template_slug = BlockTemplateUtils::generate_template_slug_from_path( $template_file );

// This template does not have a slug we're looking for. Skip it.
Expand Down Expand Up @@ -327,6 +370,11 @@ protected function get_templates_directory( $template_type = 'wp_template' ) {
if ( 'wp_template_part' === $template_type ) {
return $this->template_parts_directory;
}

if ( $this->package->is_experimental_build() && BlockTemplateUtils::should_use_blockified_product_grid_templates() ) {
return $this->templates_directory . '/blockified';
}

return $this->templates_directory;
}

Expand Down
22 changes: 20 additions & 2 deletions src/Domain/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use Automattic\WooCommerce\Blocks\Migration;
use Automattic\WooCommerce\Blocks\AssetsController;
use Automattic\WooCommerce\Blocks\BlockTemplatesController;
use Automattic\WooCommerce\Blocks\BlockTypesController;
Expand Down Expand Up @@ -46,6 +47,14 @@ class Bootstrap {
*/
private $package;


/**
* Holds the Migration instance
*
* @var Migration
*/
private $migration;

/**
* Constructor
*
Expand All @@ -54,6 +63,8 @@ class Bootstrap {
public function __construct( Container $container ) {
$this->container = $container;
$this->package = $container->get( Package::class );
$this->migration = $container->get( Migration::class );

if ( $this->has_core_dependencies() ) {
$this->init();
/**
Expand All @@ -73,6 +84,13 @@ protected function init() {
$this->register_dependencies();
$this->register_payment_methods();

if ( $this->package->is_experimental_build() && is_admin() ) {
if ( $this->package->get_version() !== $this->package->get_version_stored_on_db() ) {
$this->migration->run_migrations();
$this->package->set_version_stored_on_db();
}
}

add_action(
'admin_init',
function() {
Expand Down Expand Up @@ -221,8 +239,8 @@ function ( Container $container ) {
);
$this->container->register(
BlockTemplatesController::class,
function () {
return new BlockTemplatesController();
function ( Container $container ) {
return new BlockTemplatesController( $container->get( Package::class ) );
}
);
$this->container->register(
Expand Down
21 changes: 20 additions & 1 deletion src/Domain/Package.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace Automattic\WooCommerce\Blocks\Domain;

use Automattic\WooCommerce\Blocks\Package as NewPackage;
use Automattic\WooCommerce\Blocks\Options;
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;


/**
* Main package class.
*
Expand Down Expand Up @@ -63,6 +64,24 @@ public function get_version() {
return $this->version;
}

/**
* Returns the version of the plugin stored in the database.
*
* @return string
*/
public function get_version_stored_on_db() {
return get_option( Options::WC_BLOCK_VERSION, '' );
}

/**
* Set the version of the plugin stored in the database.
* This is useful during the first installation or after the upgrade process.
*/
public function set_version_stored_on_db() {
update_option( Options::WC_BLOCK_VERSION, $this->get_version() );

}

/**
* Returns the path to the plugin directory.
*
Expand Down
1 change: 0 additions & 1 deletion src/Domain/Services/FeatureGating.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function __construct( $flag = 0, $environment = 'unset' ) {
public function load_flag() {
if ( 0 === $this->flag ) {
$default_flag = defined( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) ? self::FEATURE_PLUGIN_FLAG : self::CORE_FLAG;

if ( file_exists( __DIR__ . '/../../../blocks.ini' ) ) {
$allowed_flags = [ self::EXPERIMENTAL_FLAG, self::FEATURE_PLUGIN_FLAG, self::CORE_FLAG ];
$woo_options = parse_ini_file( __DIR__ . '/../../../blocks.ini' );
Expand Down
1 change: 1 addition & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @internal
*/
class Installer {

/**
* Constructor
*/
Expand Down
55 changes: 55 additions & 0 deletions src/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Automattic\WooCommerce\Blocks;

use Automattic\WooCommerce\Blocks\Options;

/**
* Takes care of the migrations.
*
* @since 2.5.0
*/
class Migration {

/**
* DB updates and callbacks that need to be run per version.
*
* Please note that these functions are invoked when WooCommerce Blocks is updated from a previous version,
* but NOT when WooCommerce Blocks is newly installed.
*
* @var array
*/
private $db_upgrades = array(
// We don't need to do the following migration yet, but we'll keep it here for future use.
// '7.10.0' => array(
// 'wc_blocks_update_710_blockified_product_grid_block',
// ).
);

/**
* Runs all the necessary migrations.
*
* @var array
*/
public function run_migrations() {
$current_db_version = get_option( Options::WC_BLOCK_VERSION, '' );

if ( empty( $current_db_version ) ) {
return;
}

foreach ( $this->db_upgrades as $version => $update_callbacks ) {
if ( version_compare( $current_db_version, $version, '<' ) ) {
foreach ( $update_callbacks as $update_callback ) {
$this->{$update_callback}();
}
}
}
}

/**
* Set a flag to indicate if the blockified Product Grid Block should be rendered by default.
*/
public static function wc_blocks_update_710_blockified_product_grid_block() {
update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( false ) );
}
}
11 changes: 11 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Automattic\WooCommerce\Blocks;

/**
* Contains all the option names used by the plugin.
*/
class Options {

const WC_BLOCK_VERSION = 'wc_blocks_version';
const WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE = 'wc_blocks_use_blockified_product_grid_block_as_template';
}
8 changes: 8 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ function ( $container ) {
);
}
);
// register Bootstrap.
$container->register(
Migration::class,
function () {
return new Migration(
);
}
);
}
return $container;
}
Expand Down
17 changes: 17 additions & 0 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
use Automattic\WooCommerce\Blocks\Options;
use Automattic\WooCommerce\Blocks\Templates\MiniCartTemplate;

/**
Expand Down Expand Up @@ -564,4 +565,20 @@ function( $template ) use ( $customised_template_slugs ) {
)
);
}

/**
* Returns whether the blockified templates should be used or not.
* If the option is not stored on the db, we need to check if the current theme is a block one or not.
*
* @return boolean
*/
public static function should_use_blockified_product_grid_templates() {
$use_blockified_templates = get_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );

if ( false === $use_blockified_templates ) {
return wc_current_theme_is_fse_theme();
}

return wc_string_to_bool( $use_blockified_templates );
}
}
5 changes: 5 additions & 0 deletions templates/templates/blockified/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Blockified templates

This folder contains the blockified versions of the WooCommerce block templates.
Currently, the content of these templates is temporary and should be replaced by the actual blockified version
of each template when it's available.
8 changes: 8 additions & 0 deletions templates/templates/blockified/archive-product.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Archive product blockified</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer"} /-->
8 changes: 8 additions & 0 deletions templates/templates/blockified/product-search-results.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Product search results blockified</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer"} /-->
8 changes: 8 additions & 0 deletions templates/templates/blockified/single-product.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p>Single product blockified</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer"} /-->
Loading

0 comments on commit e59611b

Please sign in to comment.