From 3363e03efdb3acd74b76af7320440ebf869dd40e Mon Sep 17 00:00:00 2001 From: And Finally Date: Tue, 16 Apr 2024 14:08:39 +0100 Subject: [PATCH] Fix marketplace promotions action scheduler (#46630) * Defensive coding for when `as_has_scheduled_action` Action Scheduler function is not defined. * Changelog. --- .../fix-marketplace-promotions-action-scheduler | 5 +++++ .../class-wc-admin-marketplace-promotions.php | 16 ++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-marketplace-promotions-action-scheduler diff --git a/plugins/woocommerce/changelog/fix-marketplace-promotions-action-scheduler b/plugins/woocommerce/changelog/fix-marketplace-promotions-action-scheduler new file mode 100644 index 0000000000000..392cff3aba9a7 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-marketplace-promotions-action-scheduler @@ -0,0 +1,5 @@ +Significance: patch +Type: fix +Comment: Defensive coding for when Action Scheduler function as_has_scheduled_action is not defined. + + diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php b/plugins/woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php index ca887b87e2366..756f4cdc8fceb 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php @@ -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' ) ); @@ -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 ); } } @@ -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 ); + } } }