Automattic\WooCommerce\Internal\ProductAttributesLookup
CLIRunner::regenerate_core
Core method for the "regenerate" command.
Method of the class: CLIRunner{}
No Hooks.
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->regenerate_core( $args, $assoc_args );
- $args(array)
- Positional arguments passed to the command.
Default: array() - $assoc_args(array)
- Associative arguments (options) passed to the command.
Default: array()
CLIRunner::regenerate_core() CLIRunner::regenerate core code WC 10.3.3
private function regenerate_core( array $args = array(), array $assoc_args = array() ) {
global $wpdb;
$table_name = $this->lookup_data_store->get_lookup_table_name();
$batch_size = $assoc_args['batch-size'] ?? DataRegenerator::PRODUCTS_PER_GENERATION_STEP;
if ( ! is_numeric( $batch_size ) || $batch_size < 1 ) {
throw new \Exception( 'batch_size must be a number bigger than 0' );
}
$was_enabled = 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' );
// phpcs:ignore Generic.Commenting.Todo.TaskFound
// TODO: adjust for non-CPT datastores (this is only used for the progress bar, though).
$products_count = wp_count_posts( 'product' );
$products_count = intval( $products_count->publish ) + intval( $products_count->pending ) + intval( $products_count->draft );
if ( ! $this->lookup_data_store->regeneration_is_in_progress() || array_key_exists( 'from-scratch', $assoc_args ) ) {
$info = $this->get_lookup_table_info();
if ( $info['total_rows'] > 0 && ! array_key_exists( 'force', $assoc_args ) ) {
$this->warning( "The %W{$table_name}%n table contains %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
WP_CLI::confirm( 'Triggering the regeneration will first delete the data. Are you sure?' );
}
$this->data_regenerator->finalize_regeneration( false );
$last_product_id = $this->data_regenerator->initiate_regeneration( false );
if ( 0 === $last_product_id ) {
$this->data_regenerator->finalize_regeneration( $was_enabled );
WP_CLI::log( 'No products exist in the database, the table is left empty.' );
return;
}
$processed_count = 0;
} else {
$last_product_id = get_option( 'woocommerce_attribute_lookup_last_product_id_to_process' );
if ( false === $last_product_id ) {
WP_CLI::error( 'Regeneration seems to be already in progress, but the woocommerce_attribute_lookup_last_product_id_to_process option isn\'t there. Try %9wp cli palt cleanup_regeneration_progress%n first." );' );
return 1;
}
$processed_count = get_option( 'woocommerce_attribute_lookup_processed_count', 0 );
$this->log( "Resuming regeneration, %C{$processed_count}%n products have been processed already" );
$this->lookup_data_store->set_regeneration_in_progress_flag();
}
$this->data_regenerator->cancel_regeneration_scheduled_action();
$use_db_optimization = ! array_key_exists( 'disable-db-optimization', $assoc_args );
$this->check_can_use_db_optimization( $use_db_optimization );
$progress = WP_CLI\Utils\make_progress_bar( '', $products_count );
$this->log( "Regenerating %W{$table_name}%n..." );
$progress->tick( $processed_count );
$regeneration_step_failed = false;
while ( $this->data_regenerator->do_regeneration_step( $batch_size, $use_db_optimization ) ) {
$progress->tick( $batch_size );
$regeneration_step_failed = $regeneration_step_failed || $this->data_regenerator->get_last_regeneration_step_failed();
}
$this->data_regenerator->finalize_regeneration( $was_enabled );
$time = $progress->formatTime( $progress->elapsed() );
$progress->finish();
if ( $regeneration_step_failed ) {
$this->warning( "Lookup data regeneration failed for at least one product.\nSee the WooCommerce logs (source is %9palt-updates%n) for details.\n" );
$this->log( "Table %W{$table_name}%n regenerated in {$time}." );
} else {
$this->log( "%GSuccess:%n Table %W{$table_name}%n regenerated in {$time}." );
}
$info = $this->get_lookup_table_info();
$this->log( "The table contains now %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
}