Skip to content

Commit

Permalink
Calling WC_Admin_Marketplace_Promotions::init immediately from `WC_…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
andfinally committed Mar 18, 2024
1 parent 7066cd5 commit 3e6b651
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -291,3 +279,4 @@ public static function clear_scheduled_event() {
as_unschedule_all_actions( self::SCHEDULED_ACTION_HOOK );
}
}

2 changes: 1 addition & 1 deletion plugins/woocommerce/includes/admin/class-wc-admin.php
Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit 3e6b651

Please sign in to comment.