Helper: Cache update count fetch and clear cache after an update.

This commit is contained in:
Konstantin Kovshenin 2017-08-28 10:23:33 +03:00
parent a41d5da411
commit d854f92ee1
1 changed files with 42 additions and 3 deletions

View File

@ -17,6 +17,7 @@ class WC_Helper_Updater {
public static function load() { 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_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( '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. * @return int The number of products with updates.
*/ */
public static function get_updates_count() { 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. // Don't fetch any new data since this function in high-frequency.
if ( ! get_transient( '_woocommerce_helper_subscriptions' ) ) { if ( ! get_transient( '_woocommerce_helper_subscriptions' ) ) {
return 0; return 0;
@ -244,12 +250,37 @@ class WC_Helper_Updater {
return 0; return 0;
} }
$data = self::get_update_data(); $update_data = self::get_update_data();
if ( empty( $data ) ) { if ( empty( $update_data ) ) {
return 0; 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() { public static function flush_updates_cache() {
delete_transient( '_woocommerce_helper_updates' ); 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' );
} }
} }