From 3e6b65153877ff42cca1938f23994914c0822297 Mon Sep 17 00:00:00 2001 From: And Finally Date: Mon, 18 Mar 2024 12:19:20 +0000 Subject: [PATCH] Calling `WC_Admin_Marketplace_Promotions::init` immediately from `WC_Admin` constructor. `WC_Admin` is only instantiated in an admin request. If we init marketplace promotions from `woocommerce_init`, we'll be too late to add the callback for the scheduled action that fetches promotion data. --- .../class-wc-admin-marketplace-promotions.php | 59 ++++++++----------- .../includes/admin/class-wc-admin.php | 2 +- 2 files changed, 25 insertions(+), 36 deletions(-) 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 e744fdd56f255..d10ce8c01bac2 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-marketplace-promotions.php @@ -26,55 +26,43 @@ class WC_Admin_Marketplace_Promotions { /** * On all admin pages, schedule an action to fetch promotions data. - * Add menu badge to WooCommerce Extensions item if the promotions - * API requests one. + * Shows notice and adds menu badge to WooCommerce Extensions item + * if the promotions API requests them. + * + * This method is called from WC_Admin when it is instantiated in + * is_admin requests. * * @return void */ - public static function init_marketplace_promotions() { - add_action( 'init', array( __CLASS__, 'schedule_promotion_fetch' ) ); + public static function init() { + // 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' ) ); + } + + add_action( 'init', array( __CLASS__, 'schedule_promotion_fetch' ), 10 ); register_deactivation_hook( WC_PLUGIN_FILE, array( __CLASS__, 'clear_scheduled_event' ) ); + + if ( defined( 'DOING_AJAX' ) && DOING_AJAX + || defined( 'DOING_CRON' ) && DOING_CRON + || ! is_admin() ) { + return; + } + + self::$locale = ( self::$locale ?? get_user_locale() ) ?? 'en_US'; + self::maybe_show_bubble_promotions(); } /** * Schedule the action to fetch promotions data. */ public static function schedule_promotion_fetch() { - // 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' ) ); - } - // Schedule the action twice a day using Action Scheduler - if ( false === as_next_scheduled_action( self::SCHEDULED_ACTION_HOOK ) ) { + if ( false === as_has_scheduled_action( self::SCHEDULED_ACTION_HOOK ) ) { as_schedule_recurring_action( time(), 12 * HOUR_IN_SECONDS, self::SCHEDULED_ACTION_HOOK ); } - - if ( self::is_admin_page() ) { - self::$locale = ( self::$locale ?? get_user_locale() ) ?? 'en_US'; - self::maybe_show_bubble_promotions(); - } } - /** - * Check if the request is for an admin page, and not ajax. - * We may want to add a menu bubble to WooCommerce Extensions - * on any admin page, as the user may view the WooCommerce flyout - * menu. - * - * @return bool - */ - private static function is_admin_page(): bool { - if ( - ( defined( 'DOING_AJAX' ) && DOING_AJAX ) - || ! is_admin() - ) { - return false; - } - - return true; - } - /** * Get promotions to show in the Woo in-app marketplace. * Only run on selected pages in the main WooCommerce menu in wp-admin. @@ -252,7 +240,7 @@ function ( $a, $b ) { * * @return array */ - public static function filter_marketplace_menu_items( $menu_items, $promotion = array() ) { + public static function filter_marketplace_menu_items( $menu_items, $promotion = array() ): array { if ( ! isset( $promotion['menu_item_id'] ) || ! isset( $promotion['content'] ) ) { return $menu_items; } @@ -291,3 +279,4 @@ public static function clear_scheduled_event() { as_unschedule_all_actions( self::SCHEDULED_ACTION_HOOK ); } } + diff --git a/plugins/woocommerce/includes/admin/class-wc-admin.php b/plugins/woocommerce/includes/admin/class-wc-admin.php index a8f3c777dbd04..42b5d5e0bd9ee 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin.php @@ -41,7 +41,7 @@ public function __construct() { } // Fetch list of promotions from Woo.com for WooCommerce admin UI. We need to fire earlier than admin_init so we can filter menu items. - add_action( 'woocommerce_init', array( 'WC_Admin_Marketplace_Promotions', 'init_marketplace_promotions' ) ); + WC_Admin_Marketplace_Promotions::init(); } /**