Fix/10530 Inconsistent new install behaviour for templates when using block themes (https://github.com/woocommerce/woocommerce-blocks/pull/10608)

* Skipped requests regarding WP/WC setup, to avoid issues with the pages not existing yet when migrating page content to templates.

* Skipped requests regarding WP-CLI to avoid issues with the pages not existing yet when migrating page content to templates.

* Added check for woocommerce_db_version option

* Add check for occurred migration to skip template injection

* corrected if clause

* Update src/BlockTemplatesController.php

Co-authored-by: Mike Jolley <mike.jolley@me.com>

* Added maintenance mode to migration exception. Added trace value to has_migrated options

* Merge with new migration logic.

* Set correct post terms for wp_template content

* updated comment

---------

Co-authored-by: Mike Jolley <mike.jolley@me.com>
This commit is contained in:
Paulo Arromba 2023-08-21 14:48:51 +01:00 committed by GitHub
parent 85ae77d10d
commit efb6136a70
4 changed files with 31 additions and 2 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Automattic\WooCommerce\Blocks; namespace Automattic\WooCommerce\Blocks;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Blocks\Domain\Package; use Automattic\WooCommerce\Blocks\Domain\Package;
use Automattic\WooCommerce\Blocks\Templates\CartTemplate; use Automattic\WooCommerce\Blocks\Templates\CartTemplate;
use Automattic\WooCommerce\Blocks\Templates\CheckoutTemplate; use Automattic\WooCommerce\Blocks\Templates\CheckoutTemplate;
@ -767,6 +768,12 @@ class BlockTemplatesController {
* Migrates page content to templates if needed. * Migrates page content to templates if needed.
*/ */
public function maybe_migrate_content() { public function maybe_migrate_content() {
// Migration should occur on a normal request to ensure every requirement is met.
// We are postponing it if WP is in maintenance mode, installing, WC installing or if the request is part of a WP-CLI command.
if ( wp_is_maintenance_mode() || ! get_option( 'woocommerce_db_version', false ) || Constants::is_defined( 'WP_SETUP_CONFIG' ) || Constants::is_defined( 'WC_INSTALLING' ) || Constants::is_defined( 'WP_CLI' ) ) {
return;
}
if ( ! BlockTemplateMigrationUtils::has_migrated_page( 'cart' ) ) { if ( ! BlockTemplateMigrationUtils::has_migrated_page( 'cart' ) ) {
BlockTemplateMigrationUtils::migrate_page( 'cart', CartTemplate::get_placeholder_page() ); BlockTemplateMigrationUtils::migrate_page( 'cart', CartTemplate::get_placeholder_page() );
} }

View File

@ -1,6 +1,8 @@
<?php <?php
namespace Automattic\WooCommerce\Blocks\Templates; namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateMigrationUtils;
/** /**
* CartTemplate class. * CartTemplate class.
* *
@ -32,6 +34,11 @@ class CartTemplate extends AbstractPageTemplate {
* @return boolean * @return boolean
*/ */
protected function is_active_template() { protected function is_active_template() {
if ( ! BlockTemplateMigrationUtils::has_migrated_page( 'cart' ) ) {
return false;
}
global $post; global $post;
$placeholder = $this->get_placeholder_page(); $placeholder = $this->get_placeholder_page();
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name; return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;

View File

@ -1,6 +1,8 @@
<?php <?php
namespace Automattic\WooCommerce\Blocks\Templates; namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateMigrationUtils;
/** /**
* CheckoutTemplate class. * CheckoutTemplate class.
* *
@ -41,6 +43,11 @@ class CheckoutTemplate extends AbstractPageTemplate {
* @return boolean * @return boolean
*/ */
public function is_active_template() { public function is_active_template() {
if ( ! BlockTemplateMigrationUtils::has_migrated_page( 'checkout' ) ) {
return false;
}
global $post; global $post;
$placeholder = $this->get_placeholder_page(); $placeholder = $this->get_placeholder_page();
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name; return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;

View File

@ -1,8 +1,6 @@
<?php <?php
namespace Automattic\WooCommerce\Blocks\Utils; namespace Automattic\WooCommerce\Blocks\Utils;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/** /**
* Utility methods used for migrating pages to block templates. * Utility methods used for migrating pages to block templates.
* {@internal This class and its methods should only be used within the BlockTemplateController.php and is not intended for public use.} * {@internal This class and its methods should only be used within the BlockTemplateController.php and is not intended for public use.}
@ -135,6 +133,13 @@ class BlockTemplateMigrationUtils {
* @return boolean Success. * @return boolean Success.
*/ */
protected static function create_custom_template( $template, $content ) { protected static function create_custom_template( $template, $content ) {
$term = get_term_by( 'slug', $template->theme, 'wp_theme', ARRAY_A );
if ( ! $term ) {
$term = wp_insert_term( $template->theme, 'wp_theme' );
}
$template_id = wp_insert_post( $template_id = wp_insert_post(
[ [
'post_name' => $template->slug, 'post_name' => $template->slug,
@ -150,6 +155,9 @@ class BlockTemplateMigrationUtils {
], ],
true true
); );
wp_set_post_terms( $template_id, array( $term['term_id'] ), 'wp_theme' );
return $template_id && ! is_wp_error( $template_id ); return $template_id && ! is_wp_error( $template_id );
} }