diff --git a/includes/admin/helper/class-wc-helper-updater.php b/includes/admin/helper/class-wc-helper-updater.php index 19ad4f097ec..ff52dcf3a5c 100644 --- a/includes/admin/helper/class-wc-helper-updater.php +++ b/includes/admin/helper/class-wc-helper-updater.php @@ -17,6 +17,7 @@ class WC_Helper_Updater { public static function load() { add_action( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'transient_update_plugins' ), 21, 1 ); add_action( 'pre_set_site_transient_update_themes', array( __CLASS__, 'transient_update_themes' ), 21, 1 ); + add_action( 'upgrader_process_complete', array( __CLASS__, 'upgrader_process_complete' ) ); } /** @@ -235,6 +236,11 @@ class WC_Helper_Updater { * @return int The number of products with updates. */ public static function get_updates_count() { + $cache_key = '_woocommerce_helper_updates_count'; + if ( false !== ( $count = get_transient( $cache_key ) ) ) { + return $count; + } + // Don't fetch any new data since this function in high-frequency. if ( ! get_transient( '_woocommerce_helper_subscriptions' ) ) { return 0; @@ -244,12 +250,37 @@ class WC_Helper_Updater { return 0; } - $data = self::get_update_data(); - if ( empty( $data ) ) { + $update_data = self::get_update_data(); + if ( empty( $update_data ) ) { return 0; } - return count( $data ); + $count = 0; + + // Scan local plugins. + foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) { + if ( empty( $update_data[ $plugin['_product_id'] ] ) ) { + continue; + } + + if ( version_compare( $plugin['Version'], $update_data[ $plugin['_product_id'] ]['version'], '<' ) ) { + $count++; + } + } + + // Scan local themes. + foreach ( WC_Helper::get_local_woo_themes() as $theme ) { + if ( empty( $update_data[ $theme['_product_id'] ] ) ) { + continue; + } + + if ( version_compare( $theme['Version'], $update_data[ $theme['_product_id'] ]['version'], '<' ) ) { + $count++; + } + } + + set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS ); + return $count; } /** @@ -257,6 +288,14 @@ class WC_Helper_Updater { */ public static function flush_updates_cache() { delete_transient( '_woocommerce_helper_updates' ); + delete_transient( '_woocommerce_helper_updates_count' ); + } + + /** + * Fires when a user successfully updated a theme or a plugin. + */ + public static function upgrader_process_complete() { + delete_transient( '_woocommerce_helper_updates_count' ); } }