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

Commit

Permalink
Schedule action for updating the patterns content on plugin update (f…
Browse files Browse the repository at this point in the history
…or both WooCommerce and WooCommerce Blocks.)
  • Loading branch information
nefeline committed Oct 13, 2023
1 parent 0ef86a0 commit bef4459
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/BlockPatterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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 ) );
}

/**
Expand Down

0 comments on commit bef4459

Please sign in to comment.