From e409cb4765c090284990578f8754ba8dd846f24d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Wed, 25 Oct 2023 12:29:34 +0200 Subject: [PATCH] [Store Customization MVP] Ensure the AI-generated content in patterns is updated on plugin update (https://github.com/woocommerce/woocommerce-blocks/pull/11210) * Update the patterns content after updating the plugin * Remove unused variable and use statement * Schedule action for updating the patterns content on plugin update (for both WooCommerce and WooCommerce Blocks.) * Update condition to remove the plugin folder name * Remove the update function from bootstrap.php * Remove unused use * Fix the condition, since $options['plugin'] is an array * Remove pattern file This was left empty by mistake in a merge, we need to remove the file or we get an error --------- Co-authored-by: Patricia Hillebrandt --- .../woocommerce-blocks/src/BlockPatterns.php | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/plugins/woocommerce-blocks/src/BlockPatterns.php b/plugins/woocommerce-blocks/src/BlockPatterns.php index 9895b3696ca..c99b46905e8 100644 --- a/plugins/woocommerce-blocks/src/BlockPatterns.php +++ b/plugins/woocommerce-blocks/src/BlockPatterns.php @@ -52,7 +52,8 @@ class BlockPatterns { $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' ) ); } @@ -211,7 +212,36 @@ class BlockPatterns { * @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'] ) { + 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 ); + } + } + } + } + } + + /** + * 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; } @@ -224,7 +254,7 @@ class BlockPatterns { 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 ) ); } /**