2019-01-30 16:59:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Helper code for wc-admin unit tests.
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\Framework\Helpers
|
2019-01-30 16:59:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Helper_Queue.
|
|
|
|
*
|
|
|
|
* This helper class should ONLY be used for unit tests!.
|
|
|
|
*/
|
|
|
|
class WC_Helper_Queue {
|
|
|
|
/**
|
2019-04-30 16:38:07 +00:00
|
|
|
* Get all pending queued actions.
|
2019-01-30 16:59:45 +00:00
|
|
|
*
|
2019-04-30 16:38:07 +00:00
|
|
|
* @return array Pending jobs.
|
2019-01-30 16:59:45 +00:00
|
|
|
*/
|
2019-04-30 16:38:07 +00:00
|
|
|
public static function get_all_pending() {
|
2019-01-30 16:59:45 +00:00
|
|
|
$jobs = WC()->queue()->search(
|
|
|
|
array(
|
|
|
|
'per_page' => -1,
|
|
|
|
'status' => 'pending',
|
|
|
|
'claimed' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-04-30 16:38:07 +00:00
|
|
|
return $jobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run all pending queued actions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function run_all_pending() {
|
2019-05-13 02:30:07 +00:00
|
|
|
$queue_runner = new ActionScheduler_QueueRunner();
|
2019-08-21 23:39:09 +00:00
|
|
|
|
|
|
|
while ( $jobs = self::get_all_pending() ) {
|
|
|
|
foreach ( $jobs as $job_id => $job ) {
|
|
|
|
$queue_runner->process_action( $job_id );
|
|
|
|
}
|
2019-05-13 02:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-12 13:46:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel all pending actions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function cancel_all_pending() {
|
|
|
|
// Force immediate hard delete for Action Scheduler < 3.0.
|
|
|
|
global $wpdb;
|
|
|
|
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'scheduled-action'" );
|
|
|
|
|
2020-03-19 16:34:09 +00:00
|
|
|
// Delete actions for Action Scheduler >= 3.0.
|
|
|
|
$store = ActionScheduler_Store::instance();
|
|
|
|
|
|
|
|
if ( is_callable( array( $store, 'cancel_actions_by_group' ) ) ) {
|
|
|
|
$store->cancel_actions_by_group( 'wc-admin-data' );
|
|
|
|
}
|
2020-03-12 13:46:13 +00:00
|
|
|
}
|
2019-01-30 16:59:45 +00:00
|
|
|
}
|