From b794b9ee40d2c3a08c05962656e682d50d93a69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Thu, 12 Oct 2023 13:38:27 +0200 Subject: [PATCH 1/8] Update the patterns content after updating the plugin --- src/Domain/Bootstrap.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 9448126ab1a..5578efce8d5 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -16,6 +16,7 @@ use Automattic\WooCommerce\Blocks\InboxNotifications; use Automattic\WooCommerce\Blocks\Installer; use Automattic\WooCommerce\Blocks\Migration; +use Automattic\WooCommerce\Blocks\Patterns\PatternUpdater; use Automattic\WooCommerce\Blocks\Payments\Api as PaymentsApi; use Automattic\WooCommerce\Blocks\Payments\Integrations\BankTransfer; use Automattic\WooCommerce\Blocks\Payments\Integrations\CashOnDelivery; @@ -30,6 +31,7 @@ use Automattic\WooCommerce\Blocks\Templates\OrderConfirmationTemplate; use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate; use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate; +use Automattic\WooCommerce\Blocks\Verticals\VerticalsSelector; use Automattic\WooCommerce\StoreApi\RoutesController; use Automattic\WooCommerce\StoreApi\SchemaController; use Automattic\WooCommerce\StoreApi\StoreApi; @@ -58,7 +60,6 @@ class Bootstrap { */ private $package; - /** * Holds the Migration instance * @@ -66,6 +67,13 @@ class Bootstrap { */ private $migration; + /** + * Holds the Block Patterns instance + * + * @var BlockPatterns + */ + private $block_patterns; + /** * Constructor * @@ -106,6 +114,7 @@ protected function init() { if ( $this->package->get_version() !== $this->package->get_version_stored_on_db() ) { $this->migration->run_migrations(); $this->package->set_version_stored_on_db(); + $this->update_patterns_content(); } add_action( @@ -529,4 +538,20 @@ function( Container $container ) { } ); } + + /** + * Update the patterns content if the store description is present. + * + * @return void + */ + private function update_patterns_content() { + $block_patterns = $this->container->get( BlockPatterns::class ); + + $store_description = get_option( VerticalsSelector::STORE_DESCRIPTION_OPTION_KEY ); + if ( ! $store_description ) { + return; + } + + $block_patterns->update_patterns_content( $store_description ); + } } From f3a5262a86834e9227d14cb5c5c2e7d09a66ca40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Thu, 12 Oct 2023 14:12:40 +0200 Subject: [PATCH 2/8] Remove unused variable and use statement --- src/Domain/Bootstrap.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 5578efce8d5..6923c1fe50e 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -16,7 +16,6 @@ use Automattic\WooCommerce\Blocks\InboxNotifications; use Automattic\WooCommerce\Blocks\Installer; use Automattic\WooCommerce\Blocks\Migration; -use Automattic\WooCommerce\Blocks\Patterns\PatternUpdater; use Automattic\WooCommerce\Blocks\Payments\Api as PaymentsApi; use Automattic\WooCommerce\Blocks\Payments\Integrations\BankTransfer; use Automattic\WooCommerce\Blocks\Payments\Integrations\CashOnDelivery; @@ -67,13 +66,6 @@ class Bootstrap { */ private $migration; - /** - * Holds the Block Patterns instance - * - * @var BlockPatterns - */ - private $block_patterns; - /** * Constructor * From bef445954dda02aecaf22b1440157cae909dd83e Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Fri, 13 Oct 2023 07:13:42 +0200 Subject: [PATCH 3/8] Schedule action for updating the patterns content on plugin update (for both WooCommerce and WooCommerce Blocks.) --- src/BlockPatterns.php | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/BlockPatterns.php b/src/BlockPatterns.php index b3673fa745e..aaf61ff02f2 100644 --- a/src/BlockPatterns.php +++ b/src/BlockPatterns.php @@ -51,7 +51,8 @@ public function __construct( Package $package ) { $this->patterns_path = $package->get_path( 'patterns' ); add_action( 'init', array( $this, 'register_block_patterns' ) ); - add_action( 'update_option_woo_ai_describe_store_description', array( $this, 'schedule_patterns_content_update' ), 10, 2 ); + add_action( 'update_option_woo_ai_describe_store_description', array( $this, 'schedule_on_option_update' ), 10, 2 ); + add_action( 'upgrader_process_complete', array( $this, 'schedule_on_plugin_update' ), 10, 2 ); add_action( 'woocommerce_update_patterns_content', array( $this, 'update_patterns_content' ) ); } @@ -210,7 +211,34 @@ public function register_block_patterns() { * @param string $option The option name. * @param string $value The option value. */ - public function schedule_patterns_content_update( $option, $value ) { + public function schedule_on_option_update( $option, $value ) { + $this->schedule_patterns_content_update( $value ); + } + + /** + * Update the patterns content when the WooCommerce Blocks plugin is updated. + * + * @param \WP_Upgrader $upgrader_object WP_Upgrader instance. + * @param array $options Array of bulk item update data. + */ + public function schedule_on_plugin_update( $upgrader_object, $options ) { + if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) { + if ( isset( $options['plugin'] ) && ( 'woocommerce-blocks/woocommerce-gutenberg-products-block.php' === $options['plugin'] || 'woocommerce/woocommerce.php' === $options['plugin'] ) ) { + $business_description = get_option( 'woo_ai_describe_store_description' ); + + if ( $business_description ) { + $this->schedule_patterns_content_update( $business_description ); + } + } + } + } + + /** + * Update the patterns content when the store description is changed. + * + * @param string $business_description The business description. + */ + public function schedule_patterns_content_update( $business_description ) { if ( ! class_exists( 'WooCommerce' ) ) { return; } @@ -223,7 +251,7 @@ public function schedule_patterns_content_update( $option, $value ) { require_once $action_scheduler; - as_schedule_single_action( time(), 'woocommerce_update_patterns_content', array( $value ) ); + as_schedule_single_action( time(), 'woocommerce_update_patterns_content', array( $business_description ) ); } /** From 207e733b0e4f88dc6e8ec04d484876e415162b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 13 Oct 2023 14:00:54 +0200 Subject: [PATCH 4/8] Update condition to remove the plugin folder name --- src/BlockPatterns.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockPatterns.php b/src/BlockPatterns.php index aaf61ff02f2..db4a9d4e49b 100644 --- a/src/BlockPatterns.php +++ b/src/BlockPatterns.php @@ -223,7 +223,7 @@ public function schedule_on_option_update( $option, $value ) { */ public function schedule_on_plugin_update( $upgrader_object, $options ) { if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) { - if ( isset( $options['plugin'] ) && ( 'woocommerce-blocks/woocommerce-gutenberg-products-block.php' === $options['plugin'] || 'woocommerce/woocommerce.php' === $options['plugin'] ) ) { + if ( isset( $options['plugin'] ) && ( str_contains( $options['plugin'], 'woocommerce-gutenberg-products-block.php' ) || str_contains( $options['plugin'], 'woocommerce.php' ) ) ) { $business_description = get_option( 'woo_ai_describe_store_description' ); if ( $business_description ) { From 4046f58e60b57462c7c97547f7088259e6586d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 13 Oct 2023 14:01:44 +0200 Subject: [PATCH 5/8] Remove the update function from bootstrap.php --- src/Domain/Bootstrap.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 6923c1fe50e..244b4745ae8 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -106,7 +106,6 @@ protected function init() { if ( $this->package->get_version() !== $this->package->get_version_stored_on_db() ) { $this->migration->run_migrations(); $this->package->set_version_stored_on_db(); - $this->update_patterns_content(); } add_action( @@ -530,20 +529,4 @@ function( Container $container ) { } ); } - - /** - * Update the patterns content if the store description is present. - * - * @return void - */ - private function update_patterns_content() { - $block_patterns = $this->container->get( BlockPatterns::class ); - - $store_description = get_option( VerticalsSelector::STORE_DESCRIPTION_OPTION_KEY ); - if ( ! $store_description ) { - return; - } - - $block_patterns->update_patterns_content( $store_description ); - } } From 66007b4f3ecbd20d51fd9273c6bb2c4de62424cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 13 Oct 2023 14:02:37 +0200 Subject: [PATCH 6/8] Remove unused use --- src/Domain/Bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 244b4745ae8..9448126ab1a 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -30,7 +30,6 @@ use Automattic\WooCommerce\Blocks\Templates\OrderConfirmationTemplate; use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate; use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate; -use Automattic\WooCommerce\Blocks\Verticals\VerticalsSelector; use Automattic\WooCommerce\StoreApi\RoutesController; use Automattic\WooCommerce\StoreApi\SchemaController; use Automattic\WooCommerce\StoreApi\StoreApi; @@ -59,6 +58,7 @@ class Bootstrap { */ private $package; + /** * Holds the Migration instance * From 55f8bbcc0961c0b746d6391a4b918c272e273cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 13 Oct 2023 14:32:56 +0200 Subject: [PATCH 7/8] Fix the condition, since $options['plugin'] is an array --- src/BlockPatterns.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/BlockPatterns.php b/src/BlockPatterns.php index db4a9d4e49b..2358ced388f 100644 --- a/src/BlockPatterns.php +++ b/src/BlockPatterns.php @@ -223,11 +223,13 @@ public function schedule_on_option_update( $option, $value ) { */ public function schedule_on_plugin_update( $upgrader_object, $options ) { if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) { - if ( isset( $options['plugin'] ) && ( str_contains( $options['plugin'], 'woocommerce-gutenberg-products-block.php' ) || str_contains( $options['plugin'], 'woocommerce.php' ) ) ) { - $business_description = get_option( 'woo_ai_describe_store_description' ); + foreach ( $options['plugins'] as $plugin ) { + if ( str_contains( $plugin, 'woocommerce-gutenberg-products-block.php' ) || str_contains( $plugin, 'woocommerce.php' ) ) { + $business_description = get_option( 'woo_ai_describe_store_description' ); - if ( $business_description ) { - $this->schedule_patterns_content_update( $business_description ); + if ( $business_description ) { + $this->schedule_patterns_content_update( $business_description ); + } } } } From fba0778c65685a3050f35f7b5b97db66d602a3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 13 Oct 2023 15:01:43 +0200 Subject: [PATCH 8/8] Remove pattern file This was left empty by mistake in a merge, we need to remove the file or we get an error --- patterns/product-hero-2-col-2-row.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 patterns/product-hero-2-col-2-row.php diff --git a/patterns/product-hero-2-col-2-row.php b/patterns/product-hero-2-col-2-row.php deleted file mode 100644 index e69de29bb2d..00000000000