Skip to content

Commit

Permalink
Fix marketplace promotions action scheduler (#46630)
Browse files Browse the repository at this point in the history
* Defensive coding for when `as_has_scheduled_action` Action Scheduler function is not defined.

* Changelog.
  • Loading branch information
andfinally authored and nielslange committed Apr 20, 2024
1 parent d2034d7 commit 3363e03
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
@@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: Defensive coding for when Action Scheduler function as_has_scheduled_action is not defined.


Expand Up @@ -38,19 +38,17 @@ class WC_Admin_Marketplace_Promotions {
* @return void
*/
public static function init() {
register_deactivation_hook( WC_PLUGIN_FILE, array( __CLASS__, 'clear_scheduled_event' ) );

/**
* Filter to suppress the requests for and showing of marketplace promotions.
*
* @since 8.8
*/
if ( apply_filters( 'woocommerce_marketplace_suppress_promotions', false ) ) {
add_action( 'init', array( __CLASS__, 'clear_scheduled_event' ), 13 );

return;
}

register_deactivation_hook( WC_PLUGIN_FILE, array( __CLASS__, 'clear_scheduled_event' ) );

// Add the callback for our scheduled action.
if ( ! has_action( self::SCHEDULED_ACTION_HOOK, array( __CLASS__, 'fetch_marketplace_promotions' ) ) ) {
add_action( self::SCHEDULED_ACTION_HOOK, array( __CLASS__, 'fetch_marketplace_promotions' ) );
Expand All @@ -77,7 +75,11 @@ public static function init() {
*/
public static function schedule_promotion_fetch() {
// Schedule the action twice a day using Action Scheduler.
if ( false === as_has_scheduled_action( self::SCHEDULED_ACTION_HOOK ) ) {
if (
function_exists( 'as_has_scheduled_action' )
&& function_exists( 'as_schedule_recurring_action' )
&& false === as_has_scheduled_action( self::SCHEDULED_ACTION_HOOK )
) {
as_schedule_recurring_action( time(), self::SCHEDULED_ACTION_INTERVAL, self::SCHEDULED_ACTION_HOOK );
}
}
Expand Down Expand Up @@ -295,7 +297,9 @@ private static function append_bubble( string $menu_item_text, string $bubble_te
* @return void
*/
public static function clear_scheduled_event() {
as_unschedule_all_actions( self::SCHEDULED_ACTION_HOOK );
if ( function_exists( 'as_unschedule_all_actions' ) ) {
as_unschedule_all_actions( self::SCHEDULED_ACTION_HOOK );
}
}
}

Expand Down

0 comments on commit 3363e03

Please sign in to comment.