2020-01-02 16:00:37 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Import related functions and actions.
|
|
|
|
*/
|
|
|
|
|
2022-02-17 18:48:31 +00:00
|
|
|
namespace Automattic\WooCommerce\Internal\Admin\Schedulers;
|
2020-01-02 16:00:37 +00:00
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
use \Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
|
|
|
|
use \Automattic\WooCommerce\Admin\Schedulers\SchedulerTraits;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ImportScheduler class.
|
|
|
|
*/
|
2020-01-20 12:19:53 +00:00
|
|
|
abstract class ImportScheduler implements ImportInterface {
|
2020-01-17 00:08:29 +00:00
|
|
|
/**
|
|
|
|
* Import stats option name.
|
|
|
|
*/
|
|
|
|
const IMPORT_STATS_OPTION = 'woocommerce_admin_import_stats';
|
|
|
|
|
2020-01-02 16:00:37 +00:00
|
|
|
/**
|
|
|
|
* Scheduler traits.
|
|
|
|
*/
|
|
|
|
use SchedulerTraits {
|
|
|
|
get_batch_sizes as get_scheduler_batch_sizes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if an import is in progress.
|
|
|
|
*
|
2022-02-17 18:48:31 +00:00
|
|
|
* @internal
|
2020-01-02 16:00:37 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function is_importing() {
|
|
|
|
$pending_jobs = self::queue()->search(
|
|
|
|
array(
|
|
|
|
'status' => 'pending',
|
|
|
|
'per_page' => 1,
|
|
|
|
'claimed' => false,
|
|
|
|
'search' => 'import',
|
|
|
|
'group' => self::$group,
|
|
|
|
)
|
|
|
|
);
|
2020-01-08 13:53:05 +00:00
|
|
|
if ( empty( $pending_jobs ) ) {
|
|
|
|
$in_progress = self::queue()->search(
|
|
|
|
array(
|
|
|
|
'status' => 'in-progress',
|
|
|
|
'per_page' => 1,
|
|
|
|
'search' => 'import',
|
|
|
|
'group' => self::$group,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2020-01-02 16:00:37 +00:00
|
|
|
|
2020-01-08 13:53:05 +00:00
|
|
|
return ! empty( $pending_jobs ) || ! empty( $in_progress );
|
2020-01-02 16:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get batch sizes.
|
|
|
|
*
|
2022-02-17 18:48:31 +00:00
|
|
|
* @internal
|
2020-01-02 16:00:37 +00:00
|
|
|
* @retun array
|
|
|
|
*/
|
|
|
|
public static function get_batch_sizes() {
|
|
|
|
return array_merge(
|
|
|
|
self::get_scheduler_batch_sizes(),
|
|
|
|
array(
|
|
|
|
'delete' => 10,
|
|
|
|
'import' => 25,
|
|
|
|
'queue' => 100,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all available scheduling actions.
|
|
|
|
* Used to determine action hook names and clear events.
|
|
|
|
*
|
2022-02-17 18:48:31 +00:00
|
|
|
* @internal
|
2020-01-02 16:00:37 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_scheduler_actions() {
|
|
|
|
return array(
|
|
|
|
'import_batch_init' => 'wc-admin_import_batch_init_' . static::$name,
|
|
|
|
'import_batch' => 'wc-admin_import_batch_' . static::$name,
|
|
|
|
'delete_batch_init' => 'wc-admin_delete_batch_init_' . static::$name,
|
|
|
|
'delete_batch' => 'wc-admin_delete_batch_' . static::$name,
|
|
|
|
'import' => 'wc-admin_import_' . static::$name,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue the imports into multiple batches.
|
|
|
|
*
|
2022-02-17 18:48:31 +00:00
|
|
|
* @internal
|
2020-01-02 16:00:37 +00:00
|
|
|
* @param integer|boolean $days Number of days to import.
|
|
|
|
* @param boolean $skip_existing Skip exisiting records.
|
|
|
|
*/
|
|
|
|
public static function import_batch_init( $days, $skip_existing ) {
|
|
|
|
$batch_size = static::get_batch_size( 'import' );
|
|
|
|
$items = static::get_items( 1, 1, $days, $skip_existing );
|
|
|
|
|
|
|
|
if ( 0 === $items->total ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_batches = ceil( $items->total / $batch_size );
|
|
|
|
|
|
|
|
self::queue_batches( 1, $num_batches, 'import_batch', array( $days, $skip_existing ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports a batch of items to update.
|
|
|
|
*
|
2022-02-17 18:48:31 +00:00
|
|
|
* @internal
|
2020-01-02 16:00:37 +00:00
|
|
|
* @param int $batch_number Batch number to import (essentially a query page number).
|
|
|
|
* @param int|bool $days Number of days to import.
|
|
|
|
* @param bool $skip_existing Skip exisiting records.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function import_batch( $batch_number, $days, $skip_existing ) {
|
|
|
|
$batch_size = static::get_batch_size( 'import' );
|
|
|
|
|
|
|
|
$properties = array(
|
|
|
|
'batch_number' => $batch_number,
|
|
|
|
'batch_size' => $batch_size,
|
|
|
|
'type' => static::$name,
|
|
|
|
);
|
|
|
|
wc_admin_record_tracks_event( 'import_job_start', $properties );
|
|
|
|
|
|
|
|
// When we are skipping already imported items, the table of items to import gets smaller in
|
|
|
|
// every batch, so we want to always import the first page.
|
|
|
|
$page = $skip_existing ? 1 : $batch_number;
|
|
|
|
$items = static::get_items( $batch_size, $page, $days, $skip_existing );
|
|
|
|
|
|
|
|
foreach ( $items->ids as $id ) {
|
|
|
|
static::import( $id );
|
|
|
|
}
|
|
|
|
|
2020-01-17 00:08:29 +00:00
|
|
|
$import_stats = get_option( self::IMPORT_STATS_OPTION, array() );
|
2020-01-02 16:00:37 +00:00
|
|
|
$imported_count = absint( $import_stats[ static::$name ]['imported'] ) + count( $items->ids );
|
|
|
|
$import_stats[ static::$name ]['imported'] = $imported_count;
|
2020-01-17 00:08:29 +00:00
|
|
|
update_option( self::IMPORT_STATS_OPTION, $import_stats );
|
2020-01-02 16:00:37 +00:00
|
|
|
|
|
|
|
$properties['imported_count'] = $imported_count;
|
|
|
|
|
|
|
|
wc_admin_record_tracks_event( 'import_job_complete', $properties );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue item deletion in batches.
|
2022-02-17 18:48:31 +00:00
|
|
|
*
|
|
|
|
* @internal
|
2020-01-02 16:00:37 +00:00
|
|
|
*/
|
|
|
|
public static function delete_batch_init() {
|
|
|
|
global $wpdb;
|
|
|
|
$batch_size = static::get_batch_size( 'delete' );
|
|
|
|
$count = static::get_total_imported();
|
|
|
|
|
|
|
|
if ( 0 === $count ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_batches = ceil( $count / $batch_size );
|
|
|
|
|
|
|
|
self::queue_batches( 1, $num_batches, 'delete_batch' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a batch by passing the count to be deleted to the child delete method.
|
|
|
|
*
|
2022-02-17 18:48:31 +00:00
|
|
|
* @internal
|
2020-01-02 16:00:37 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function delete_batch() {
|
|
|
|
wc_admin_record_tracks_event( 'delete_import_data_job_start', array( 'type' => static::$name ) );
|
|
|
|
|
|
|
|
$batch_size = static::get_batch_size( 'delete' );
|
|
|
|
static::delete( $batch_size );
|
|
|
|
|
|
|
|
ReportsCache::invalidate();
|
|
|
|
|
|
|
|
wc_admin_record_tracks_event( 'delete_import_data_job_complete', array( 'type' => static::$name ) );
|
|
|
|
}
|
|
|
|
}
|