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

Add migration strategy and load blockified templates #6538

Merged
merged 52 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
8b9983a
Increase `schema_version` to add the new `wc_blocks_use_blockified_te…
albarin Jun 8, 2022
0bb7058
After the theme is switched we check if we need to update the flag to
albarin Jun 8, 2022
e399e3a
Get the templates from the blockified folder if the flag is set to tr…
albarin Jun 8, 2022
0233df0
Add temporary blockified template for testing purposes
albarin Jun 8, 2022
7f80a30
Inline variable
albarin Jun 9, 2022
5a51078
Improve comment
albarin Jun 9, 2022
aecf645
Use blockified templates on new installs with block themes only
albarin Jun 9, 2022
ffe9587
Don't use blockified templates when switching to a non FSE theme
albarin Jun 9, 2022
784f51e
Fix condition
albarin Jun 9, 2022
a0769dc
Add tests for the option value
albarin Jun 14, 2022
2a188a1
Move the check to use blockified templates
albarin Jun 14, 2022
214fc55
WIP
gigitux Jun 15, 2022
2301500
WIP
gigitux Jun 16, 2022
545efaf
Add migration strategy
gigitux Jun 16, 2022
a135804
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Jun 16, 2022
e7f78bc
Move the blockified templates to the templates folder and filter them…
albarin Jun 17, 2022
e4c463c
Fix tests and start using the Options constants
albarin Jun 17, 2022
456758d
Fix migration, the `should_use_blockified_product_grid_templates` and…
albarin Jun 17, 2022
2ddddfc
Rename tests and move to Utils folder
albarin Jun 17, 2022
0cd4b7d
Merge branch 'trunk' into feature/6513-load-blockified-templates
albarin Jun 17, 2022
73e4e0b
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Jun 17, 2022
1d79fdd
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Jun 21, 2022
3b7fb60
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Jun 22, 2022
e6c65a1
add Migration class test
gigitux Jun 22, 2022
def35e8
try
gigitux Jun 22, 2022
0e71aa1
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Jun 22, 2022
9e13267
remove PHP
gigitux Jun 22, 2022
982fa15
add composer
gigitux Jun 22, 2022
0ff6a09
Replace the blockified templates with a temporary version
albarin Jun 22, 2022
f18e15a
Fix tests
albarin Jun 22, 2022
e2312a6
add comment
gigitux Jun 22, 2022
eec5b1c
Merge branch 'feature/6513-load-blockified-templates' of https://gith…
gigitux Jun 22, 2022
b1561b4
Merge branch 'trunk' into feature/6513-load-blockified-templates
gigitux Jun 22, 2022
d788fc6
Add feature gating for experimental builds
albarin Jun 23, 2022
7b16ec1
Inject the package to the controller
albarin Jun 23, 2022
eb82886
test
albarin Jun 23, 2022
ffd2008
Change blocks.ini
albarin Jun 23, 2022
75f23c3
debug
albarin Jun 23, 2022
e7ff823
Remove debug info
albarin Jun 23, 2022
d560b64
Merge branch 'trunk' into feature/6513-load-blockified-templates
gigitux Jun 24, 2022
cc22647
fix test
gigitux Jun 24, 2022
dccc858
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Jun 24, 2022
584c1dc
fix tests
gigitux Jun 24, 2022
4665888
try now
gigitux Jun 24, 2022
ec2d8d6
using composer cache
gigitux Jun 24, 2022
4159ff0
install deps
gigitux Jun 24, 2022
0cad66f
test
albarin Jun 23, 2022
b004cab
Remove unnecessary extra key
albarin Jun 27, 2022
f0b8569
Add cache actions
albarin Jun 27, 2022
9cd0595
Undo tests change
albarin Jun 27, 2022
76c2354
Fix readme format
albarin Jun 27, 2022
3e265d0
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Jul 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can optimize by creating a more abstract method which accepts the option key so we are able to use it for future templates we are going to convert to block templates to avoid creating separate methods for each scenario?

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