Skip to content
Browse files

Added an option to check updates less frequently if there's an update…

… already available.

This feature is disabled by default. Turn it on by setting `$checker->throttleRedundantChecks` to `true` and use `$checker->throttledCheckPeriod` to set the alternate check period (default = every 72 hours).
  • Loading branch information...
1 parent 3adbcde commit d921ed91e8fcfcd8304e174cc7b7ae9cb75991c6 @YahnisElsts committed
Showing with 38 additions and 10 deletions.
  1. +15 −0 debug-bar-panel.php
  2. +23 −10 plugin-update-checker.php
View
15 debug-bar-panel.php
@@ -42,6 +42,21 @@ public function render() {
} else {
$this->row('Automatic checks', 'Disabled');
}
+
+ if ( isset($this->updateChecker->throttleRedundantChecks) ) {
+ if ( $this->updateChecker->throttleRedundantChecks && ($this->updateChecker->checkPeriod > 0) ) {
+ $this->row(
+ 'Throttling',
+ sprintf(
+ 'Enabled. If an update is already available, check for updates every %1$d hours instead of every %2$d hours.',
+ $this->updateChecker->throttledCheckPeriod,
+ $this->updateChecker->checkPeriod
+ )
+ );
+ } else {
+ $this->row('Throttling', 'Disabled');
+ }
+ }
echo '</table>';
echo '<h3>Status</h3>';
View
33 plugin-update-checker.php
@@ -8,17 +8,17 @@
* http://www.gnu.org/licenses/gpl.html
*/
-if ( !class_exists('PluginUpdateChecker_1_3_2') ):
+if ( !class_exists('PluginUpdateChecker_1_4') ):
/**
* A custom plugin update checker.
*
* @author Janis Elsts
* @copyright 2013
- * @version 1.3.1
+ * @version 1.4
* @access public
*/
-class PluginUpdateChecker_1_3_2 {
+class PluginUpdateChecker_1_4 {
public $metadataUrl = ''; //The URL of the plugin's metadata file.
public $pluginAbsolutePath = ''; //Full path of the main plugin file.
public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins.
@@ -29,6 +29,9 @@ class PluginUpdateChecker_1_3_2 {
public $debugMode = false; //Set to TRUE to enable error reporting. Errors are raised using trigger_error()
//and should be logged to the standard PHP error log.
+ public $throttleRedundantChecks = false; //Check less often if we already know that an update is available.
+ public $throttledCheckPeriod = 72;
+
private $cronHook = null;
private $debugBarPlugin = null;
private $cachedInstalledVersion = null;
@@ -109,7 +112,7 @@ protected function installHooks(){
if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) {
wp_schedule_event(time(), $scheduleName, $this->cronHook);
}
- add_action($this->cronHook, array($this, 'checkForUpdates'));
+ add_action($this->cronHook, array($this, 'maybeCheckForUpdates'));
register_deactivation_hook($this->pluginFile, array($this, '_removeUpdaterCron'));
@@ -324,23 +327,33 @@ public function checkForUpdates(){
/**
* Check for updates if the configured check interval has already elapsed.
- * Will use a shorter check interval on certain admin pages like "Dashboard -> Updates".
- *
+ * Will use a shorter check interval on certain admin pages like "Dashboard -> Updates" or when doing cron.
+ *
+ * This method must be declared public to be usable as a hook callback, but calling it directly is not recommended.
+ *
* @return void
*/
public function maybeCheckForUpdates(){
if ( empty($this->checkPeriod) ){
return;
}
- $state = $this->getUpdateState();
- //Check more often when the user visits Dashboard -> Updates.
if ( current_filter() == 'load-update-core.php' ) {
+ //Check more often when the user visits Dashboard -> Updates.
$timeout = 60;
+ } else if ( $this->throttleRedundantChecks && ($this->getUpdate() !== null) ) {
+ //Check less frequently if it's already known that an update is available.
+ $timeout = $this->throttledCheckPeriod * 3600;
+ } else if ( defined('DOING_CRON') && constant('DOING_CRON') ) {
+ //Check every time if triggered by cron and throttling is disabled. Our cron event is
+ //scheduled to run every $checkPeriod hours, so we don't need to check how much time
+ //has passed since the last check.
+ $timeout = 0;
} else {
$timeout = $this->checkPeriod * 3600;
}
+ $state = $this->getUpdateState();
$shouldCheck =
empty($state) ||
!isset($state->lastCheck) ||
@@ -954,7 +967,7 @@ public static function addVersion($generalClass, $versionedClass, $version) {
endif;
//Register classes defined in this file with the factory.
-PucFactory::addVersion('PluginUpdateChecker', 'PluginUpdateChecker_1_3_2', '1.3.2');
+PucFactory::addVersion('PluginUpdateChecker', 'PluginUpdateChecker_1_4', '1.4');
PucFactory::addVersion('PluginUpdate', 'PluginUpdate_1_3', '1.3');
PucFactory::addVersion('PluginInfo', 'PluginInfo_1_3', '1.3');
@@ -963,7 +976,7 @@ public static function addVersion($generalClass, $versionedClass, $version) {
* compatibility with versions that did not use a factory, and it simplifies doc-comments.
*/
if ( !class_exists('PluginUpdateChecker') ) {
- class PluginUpdateChecker extends PluginUpdateChecker_1_3_2 { }
+ class PluginUpdateChecker extends PluginUpdateChecker_1_4 { }
}
if ( !class_exists('PluginUpdate') ) {

0 comments on commit d921ed9

Please sign in to comment.
Something went wrong with that request. Please try again.