An updated theme (based on Atticus Finch) for ClassicPress
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.1 KiB

3 years ago
  1. <?php
  2. if ( !class_exists('Puc_v4p1_Scheduler', false) ):
  3. /**
  4. * The scheduler decides when and how often to check for updates.
  5. * It calls @see Puc_v4p1_UpdateChecker::checkForUpdates() to perform the actual checks.
  6. */
  7. class Puc_v4p1_Scheduler {
  8. public $checkPeriod = 12; //How often to check for updates (in hours).
  9. public $throttleRedundantChecks = false; //Check less often if we already know that an update is available.
  10. public $throttledCheckPeriod = 72;
  11. protected $hourlyCheckHooks = array('load-update.php');
  12. /**
  13. * @var Puc_v4p1_UpdateChecker
  14. */
  15. protected $updateChecker;
  16. private $cronHook = null;
  17. /**
  18. * Scheduler constructor.
  19. *
  20. * @param Puc_v4p1_UpdateChecker $updateChecker
  21. * @param int $checkPeriod How often to check for updates (in hours).
  22. * @param array $hourlyHooks
  23. */
  24. public function __construct($updateChecker, $checkPeriod, $hourlyHooks = array('load-plugins.php')) {
  25. $this->updateChecker = $updateChecker;
  26. $this->checkPeriod = $checkPeriod;
  27. //Set up the periodic update checks
  28. $this->cronHook = $this->updateChecker->getUniqueName('cron_check_updates');
  29. if ( $this->checkPeriod > 0 ){
  30. //Trigger the check via Cron.
  31. //Try to use one of the default schedules if possible as it's less likely to conflict
  32. //with other plugins and their custom schedules.
  33. $defaultSchedules = array(
  34. 1 => 'hourly',
  35. 12 => 'twicedaily',
  36. 24 => 'daily',
  37. );
  38. if ( array_key_exists($this->checkPeriod, $defaultSchedules) ) {
  39. $scheduleName = $defaultSchedules[$this->checkPeriod];
  40. } else {
  41. //Use a custom cron schedule.
  42. $scheduleName = 'every' . $this->checkPeriod . 'hours';
  43. add_filter('cron_schedules', array($this, '_addCustomSchedule'));
  44. }
  45. if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) {
  46. wp_schedule_event(time(), $scheduleName, $this->cronHook);
  47. }
  48. add_action($this->cronHook, array($this, 'maybeCheckForUpdates'));
  49. //In case Cron is disabled or unreliable, we also manually trigger
  50. //the periodic checks while the user is browsing the Dashboard.
  51. add_action( 'admin_init', array($this, 'maybeCheckForUpdates') );
  52. //Like WordPress itself, we check more often on certain pages.
  53. /** @see wp_update_plugins */
  54. add_action('load-update-core.php', array($this, 'maybeCheckForUpdates'));
  55. //"load-update.php" and "load-plugins.php" or "load-themes.php".
  56. $this->hourlyCheckHooks = array_merge($this->hourlyCheckHooks, $hourlyHooks);
  57. foreach($this->hourlyCheckHooks as $hook) {
  58. add_action($hook, array($this, 'maybeCheckForUpdates'));
  59. }
  60. //This hook fires after a bulk update is complete.
  61. add_action('upgrader_process_complete', array($this, 'maybeCheckForUpdates'), 11, 0);
  62. } else {
  63. //Periodic checks are disabled.
  64. wp_clear_scheduled_hook($this->cronHook);
  65. }
  66. }
  67. /**
  68. * Check for updates if the configured check interval has already elapsed.
  69. * Will use a shorter check interval on certain admin pages like "Dashboard -> Updates" or when doing cron.
  70. *
  71. * You can override the default behaviour by using the "puc_check_now-$slug" filter.
  72. * The filter callback will be passed three parameters:
  73. * - Current decision. TRUE = check updates now, FALSE = don't check now.
  74. * - Last check time as a Unix timestamp.
  75. * - Configured check period in hours.
  76. * Return TRUE to check for updates immediately, or FALSE to cancel.
  77. *
  78. * This method is declared public because it's a hook callback. Calling it directly is not recommended.
  79. */
  80. public function maybeCheckForUpdates(){
  81. if ( empty($this->checkPeriod) ){
  82. return;
  83. }
  84. $state = $this->updateChecker->getUpdateState();
  85. $shouldCheck = ($state->timeSinceLastCheck() >= $this->getEffectiveCheckPeriod());
  86. //Let plugin authors substitute their own algorithm.
  87. $shouldCheck = apply_filters(
  88. $this->updateChecker->getUniqueName('check_now'),
  89. $shouldCheck,
  90. $state->getLastCheck(),
  91. $this->checkPeriod
  92. );
  93. if ( $shouldCheck ) {
  94. $this->updateChecker->checkForUpdates();
  95. }
  96. }
  97. /**
  98. * Calculate the actual check period based on the current status and environment.
  99. *
  100. * @return int Check period in seconds.
  101. */
  102. protected function getEffectiveCheckPeriod() {
  103. $currentFilter = current_filter();
  104. if ( in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete')) ) {
  105. //Check more often when the user visits "Dashboard -> Updates" or does a bulk update.
  106. $period = 60;
  107. } else if ( in_array($currentFilter, $this->hourlyCheckHooks) ) {
  108. //Also check more often on /wp-admin/update.php and the "Plugins" or "Themes" page.
  109. $period = 3600;
  110. } else if ( $this->throttleRedundantChecks && ($this->updateChecker->getUpdate() !== null) ) {
  111. //Check less frequently if it's already known that an update is available.
  112. $period = $this->throttledCheckPeriod * 3600;
  113. } else if ( defined('DOING_CRON') && constant('DOING_CRON') ) {
  114. //WordPress cron schedules are not exact, so lets do an update check even
  115. //if slightly less than $checkPeriod hours have elapsed since the last check.
  116. $cronFuzziness = 20 * 60;
  117. $period = $this->checkPeriod * 3600 - $cronFuzziness;
  118. } else {
  119. $period = $this->checkPeriod * 3600;
  120. }
  121. return $period;
  122. }
  123. /**
  124. * Add our custom schedule to the array of Cron schedules used by WP.
  125. *
  126. * @param array $schedules
  127. * @return array
  128. */
  129. public function _addCustomSchedule($schedules){
  130. if ( $this->checkPeriod && ($this->checkPeriod > 0) ){
  131. $scheduleName = 'every' . $this->checkPeriod . 'hours';
  132. $schedules[$scheduleName] = array(
  133. 'interval' => $this->checkPeriod * 3600,
  134. 'display' => sprintf('Every %d hours', $this->checkPeriod),
  135. );
  136. }
  137. return $schedules;
  138. }
  139. /**
  140. * Remove the scheduled cron event that the library uses to check for updates.
  141. *
  142. * @return void
  143. */
  144. public function removeUpdaterCron(){
  145. wp_clear_scheduled_hook($this->cronHook);
  146. }
  147. /**
  148. * Get the name of the update checker's WP-cron hook. Mostly useful for debugging.
  149. *
  150. * @return string
  151. */
  152. public function getCronHookName() {
  153. return $this->cronHook;
  154. }
  155. }
  156. endif;