forked from woocommerce/woocommerce-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that other scheduled actions are claimed before WC Admin actio…
…ns. (woocommerce#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.
- Loading branch information
1 parent
4a806b6
commit 72b6655
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters