ification (needed when running the tool from the tools page). * @throws \Exception The lookup table doesn't exist, or there's no regeneration process in progress to abort. */ public function abort_regeneration( bool $verify_nonce ) { if ( $verify_nonce ) { $this->verify_tool_execution_nonce(); } if ( ! $this->data_store->check_lookup_table_exists() ) { throw new \Exception( "Can't abort the product attribute lookup data regeneration process: lookup table doesn't exist" ); } if ( ! $this->data_store->regeneration_is_in_progress() ) { throw new \Exception( "Can't abort the product attribute lookup data regeneration process since it's not currently in progress" ); } $this->cancel_regeneration_scheduled_action(); $this->data_store->unset_regeneration_in_progress_flag(); $this->data_store->set_regeneration_aborted_flag(); $this->enable_or_disable_lookup_table_usage( false ); // Note that we are NOT deleting the options that track the regeneration progress (processed count, last product id to process). // This is on purpose so that the regeneration can be resumed where it stopped. } /** * Cancel any existing regeneration step scheduled action. */ public function cancel_regeneration_scheduled_action() { $queue = WC()->get_instance_of( \WC_Queue::class ); $queue->cancel_all( 'woocommerce_run_product_attribute_lookup_regeneration_callback' ); } /** * Check if any pending regeneration step scheduled action exists. * * @return bool True if any pending regeneration step scheduled action exists. */ public function has_scheduled_action_for_regeneration_step(): bool { $queue = WC()->get_instance_of( \WC_Queue::class ); $actions = $queue->search( array( 'hook' => 'woocommerce_run_product_attribute_lookup_regeneration_callback', 'status' => \ActionScheduler_Store::STATUS_PENDING, ), 'ids' ); return ! empty( $actions ); } /** * Callback to resume the regeneration process from the Status - Tools page or from CLI. * * @param bool $verify_nonce True to perform nonce verification (needed when running the tool from the tools page). * @throws \Exception The lookup table doesn't exist, or a regeneration process is already in place or hasn't been aborted. */ public function resume_regeneration( bool $verify_nonce ) { if ( $verify_nonce ) { $this->verify_tool_execution_nonce(); } if ( ! $this->data_store->check_lookup_table_exists() ) { throw new \Exception( "Can't resume the product attribute lookup data regeneration process: lookup table doesn't exist" ); } if ( $this->data_store->regeneration_is_in_progress() ) { throw new \Exception( "Can't resume the product attribute lookup data regeneration process: regeneration is already in progress" ); } if ( ! $this->data_store->regeneration_was_aborted() ) { throw new \Exception( "Can't resume the product attribute lookup data regeneration process: no aborted regeneration process exists" ); } $this->data_store->unset_regeneration_aborted_flag(); $this->data_store->set_regeneration_in_progress_flag(); $this->enqueue_regeneration_step_run(); } /** * Verify the validity of the nonce received when executing a tool from the Status - Tools page. * * @throws \Exception Missing or invalid nonce received. */ private function verify_tool_execution_nonce() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput if ( ! isset( $_REQUEST['_wpnonce'] ) || wp_verify_nonce( $_REQUEST['_wpnonce'], 'debug_action' ) === false ) { throw new \Exception( 'Invalid nonce' ); } } /** * Get the name of the product attributes lookup table. * * @return string */ public function get_lookup_table_name() { return $this->lookup_table_name; } /** * Get the SQL statement that creates the product attributes lookup table, including the indices. * * @return string */ public function get_table_creation_sql() { global $wpdb; $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : ''; return "CREATE TABLE {$this->lookup_table_name} ( product_id bigint(20) NOT NULL, product_or_parent_id bigint(20) NOT NULL, taxonomy varchar(32) NOT NULL, term_id bigint(20) NOT NULL, is_variation_attribute tinyint(1) NOT NULL, in_stock tinyint(1) NOT NULL, INDEX is_variation_attribute_term_id (is_variation_attribute, term_id), PRIMARY KEY ( `product_or_parent_id`, `term_id`, `product_id`, `taxonomy` ) ) $collate;"; } /** * Create the primary key for the table if it doesn't exist already. * It also deletes the product_or_parent_id_term_id index if it exists, since it's now redundant. * * @return void */ public function create_table_primary_index() { $database_util = wc_get_container()->get( DatabaseUtil::class ); $database_util->create_primary_key( $this->lookup_table_name, array( 'product_or_parent_id', 'term_id', 'product_id', 'taxonomy' ) ); $database_util->drop_table_index( $this->lookup_table_name, 'product_or_parent_id_term_id' ); if ( empty( $database_util->get_index_columns( $this->lookup_table_name ) ) ) { wc_get_logger()->error( "The creation of the primary key for the {$this->lookup_table_name} table failed" ); } if ( ! empty( $database_util->get_index_columns( $this->lookup_table_name, 'product_or_parent_id_term_id' ) ) ) { wc_get_logger()->error( "Dropping the product_or_parent_id_term_id index from the {$this->lookup_table_name} table failed" ); } } /** * Run additional setup needed after a WooCommerce install or update finishes. * * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. */ public function run_woocommerce_installed_callback() { // The table must exist at this point (created via dbDelta), but we check just in case. if ( ! $this->data_store->check_lookup_table_exists() ) { return; } // If a table regeneration is in progress, leave it alone. if ( $this->data_store->regeneration_is_in_progress() ) { return; } // If the lookup table has data, or if it's empty because there are no products yet, we're good. // Otherwise (lookup table is empty but products exist) we need to initiate a regeneration if one isn't already in progress. if ( $this->data_store->lookup_table_has_data() || ! $this->get_last_existing_product_id() ) { $must_enable = get_option( 'woocommerce_attribute_lookup_enabled' ) !== 'no'; $this->delete_all_attributes_lookup_data( false ); update_option( 'woocommerce_attribute_lookup_enabled', $must_enable ? 'yes' : 'no' ); } else { $this->initiate_regeneration(); } } }