Ensure that other scheduled actions are claimed before WC Admin actions. (https://github.com/woocommerce/woocommerce-admin/pull/1759)

* Use menu_order to lower the claim priority of WC Admin scheduled actions.

* Use a ActionScheduler_Store subclass to modify menu_order.

* Check to make sure we’re overriding only AS’s built in WP Post data store.
This commit is contained in:
Jeff Stieler 2019-03-08 11:59:32 -07:00 committed by GitHub
parent 8802de46d5
commit a50a9bccb1
3 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,36 @@
<?php
/**
* WC Admin Action Scheduler Store.
*
* @package WooCommerce Admin/Classes
*/
/**
* Class WC Admin Action Scheduler Store.
*/
class WC_Admin_ActionScheduler_WPPostStore extends ActionScheduler_wpPostStore {
/**
* Action scheduler job priority (lower numbers are claimed first).
*/
const JOB_PRIORITY = 30;
/**
* Create the post array for storing actions as WP posts.
*
* For WC Admin actions, force a lower action claim
* priority by setting a high value for `menu_order`.
*
* @param ActionScheduler_Action $action Action.
* @param DateTime $scheduled_date Action schedule.
* @return array Post data array for usage in wp_insert_post().
*/
protected function create_post_array( ActionScheduler_Action $action, DateTime $scheduled_date = null ) {
$postdata = parent::create_post_array( $action, $scheduled_date );
if ( 0 === strpos( $postdata['post_title'], 'wc-admin_' ) ) {
$postdata['menu_order'] = self::JOB_PRIORITY;
}
return $postdata;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* Reports Generation Batch Queue Prioritizaion Tests
*
* @package WooCommerce\Tests\Reports
* @since 3.5.0
*/
/**
* Reports Generation Batch Queue Prioritizaion Test Class
*
* @package WooCommerce\Tests\Reports
* @since 3.5.0
*/
class WC_Tests_Reports_Queue_Prioritization extends WC_REST_Unit_Test_Case {
/**
* Set up.
*/
public function setUp() {
parent::setUp();
if ( class_exists( 'ActionScheduler' ) ) {
remove_action( 'action_scheduler_run_queue', array( ActionScheduler::runner(), 'run' ) );
}
}
/**
* Tear down.
*/
public function tearDown() {
parent::tearDown();
if ( class_exists( 'ActionScheduler' ) ) {
add_action( 'action_scheduler_run_queue', array( ActionScheduler::runner(), 'run' ) );
}
}
/**
* Test that we're setting a priority on our actions.
*/
public function test_queue_action_sets_priority() {
WC_Admin_Reports_Sync::queue()->schedule_single( time(), WC_Admin_Reports_Sync::SINGLE_ORDER_ACTION );
$actions = WC_Admin_Reports_Sync::queue()->search(
array(
'status' => 'pending',
'claimed' => false,
'hook' => WC_Admin_Reports_Sync::SINGLE_ORDER_ACTION,
)
);
$this->assertCount( 1, $actions );
$action_ids = array_keys( $actions );
$action_id = $action_ids[0];
$action = get_post( $action_id );
$this->assertEquals( WC_Admin_ActionScheduler_wpPostStore::JOB_PRIORITY, $action->menu_order );
WC_Admin_Reports_Sync::queue()->cancel_all( WC_Admin_Reports_Sync::SINGLE_ORDER_ACTION );
}
}

View File

@ -181,3 +181,26 @@ add_action( 'woocommerce_updated', 'wc_admin_woocommerce_updated' );
* Gutenberg has also disabled emojis. More on that here -> https://github.com/WordPress/gutenberg/pull/6151 * Gutenberg has also disabled emojis. More on that here -> https://github.com/WordPress/gutenberg/pull/6151
*/ */
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
/**
* Filter in our ActionScheduler Store class.
*
* @param string $store_class ActionScheduler Store class name.
* @return string ActionScheduler Store class name.
*/
function wc_admin_set_actionscheduler_store_class( $store_class ) {
// Don't override any other overrides.
if ( 'ActionScheduler_wpPostStore' !== $store_class ) {
return $store_class;
}
// Include our store class here instead of wc_admin_plugins_loaded()
// because ActionScheduler is hooked into `plugins_loaded` at a
// much higher priority.
require_once WC_ADMIN_ABSPATH . '/includes/class-wc-admin-actionscheduler-wppoststore.php';
return 'WC_Admin_ActionScheduler_WPPostStore';
}
// Hook up our modified ActionScheduler Store.
add_filter( 'action_scheduler_store_class', 'wc_admin_set_actionscheduler_store_class' );