| 1 |
<?php // phpcs:ignore WordPress.Files.FileName |
| 2 |
/** |
| 3 |
* VideoPress cron scheduler. |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit( 0 ); |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* VideoPress Cron Scheduler. |
| 14 |
*/ |
| 15 |
class VideoPress_Scheduler { |
| 16 |
|
| 17 |
/** |
| 18 |
* The name of the function used to run the cleanup cron. |
| 19 |
*/ |
| 20 |
const CLEANUP_CRON_METHOD = 'videopress_cleanup_media_library'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Singleton instance. |
| 24 |
* |
| 25 |
* @var VideoPress_Scheduler $instance A VideoPress_Scheduler singleton instance (default null) |
| 26 |
**/ |
| 27 |
private static $instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* A list of all of the crons that are to be activated, along with their interval timings. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $crons = array( |
| 35 |
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
| 36 |
// 'cleanup' => array( |
| 37 |
// 'method' => self::CLEANUP_CRON_METHOD, |
| 38 |
// 'interval' => 'minutes_30', |
| 39 |
// ), |
| 40 |
); |
| 41 |
|
| 42 |
/** |
| 43 |
* Private VideoPress_Scheduler constructor. |
| 44 |
* |
| 45 |
* Use the VideoPress_Scheduler::init() method to get an instance. |
| 46 |
*/ |
| 47 |
private function __construct() { |
| 48 |
add_filter( 'cron_schedules', array( $this, 'add_30_minute_cron_interval' ) ); |
| 49 |
|
| 50 |
// Activate the cleanup cron if videopress is enabled, jetpack is activated, or jetpack is updated. |
| 51 |
add_action( 'jetpack_activate_module_videopress', array( $this, 'activate_all_crons' ) ); |
| 52 |
add_action( 'updating_jetpack_version', array( $this, 'activate_all_crons' ) ); |
| 53 |
add_action( 'activated_plugin', array( $this, 'activate_crons_on_jetpack_activation' ) ); |
| 54 |
|
| 55 |
// Deactivate the cron if either videopress is disabled or Jetpack is disabled. |
| 56 |
add_action( 'jetpack_deactivate_module_videopress', array( $this, 'deactivate_all_crons' ) ); |
| 57 |
register_deactivation_hook( plugin_basename( JETPACK__PLUGIN_FILE ), array( $this, 'deactivate_all_crons' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Initialize the VideoPress_Scheduler and get back a singleton instance. |
| 62 |
* |
| 63 |
* @return VideoPress_Scheduler |
| 64 |
*/ |
| 65 |
public static function init() { |
| 66 |
if ( self::$instance === null ) { |
| 67 |
self::$instance = new VideoPress_Scheduler(); |
| 68 |
} |
| 69 |
|
| 70 |
return self::$instance; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Adds 30 minute running interval to the cron schedules. |
| 75 |
* |
| 76 |
* @param array $current_schedules Currently defined schedules list. |
| 77 |
* |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
public function add_30_minute_cron_interval( $current_schedules ) { |
| 81 |
|
| 82 |
// Only add the 30 minute interval if it wasn't already set. |
| 83 |
if ( ! isset( $current_schedules['minutes_30'] ) ) { |
| 84 |
$current_schedules['minutes_30'] = array( |
| 85 |
'interval' => 30 * MINUTE_IN_SECONDS, |
| 86 |
'display' => 'Every 30 minutes', |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
return $current_schedules; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Activate a single cron |
| 95 |
* |
| 96 |
* @param string $cron_name The name of the cron to activate. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function activate_cron( $cron_name ) { |
| 101 |
|
| 102 |
if ( ! $this->is_cron_valid( $cron_name ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! $this->check_cron( $cron_name ) ) { |
| 107 |
wp_schedule_event( time(), $this->crons[ $cron_name ]['interval'], $this->crons[ $cron_name ]['method'] ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Activates widget update cron task. |
| 113 |
*/ |
| 114 |
public function activate_all_crons() { |
| 115 |
|
| 116 |
if ( ! Jetpack::is_module_active( 'videopress' ) ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
foreach ( $this->crons as $cron_name => $cron ) { |
| 121 |
if ( ! $this->check_cron( $cron_name ) ) { |
| 122 |
wp_schedule_event( time(), $cron['interval'], $cron['method'] ); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Only activate the crons if it is Jetpack that was activated. |
| 129 |
* |
| 130 |
* @param string $plugin_file_name The name of the plugin that was activated. |
| 131 |
*/ |
| 132 |
public function activate_crons_on_jetpack_activation( $plugin_file_name ) { |
| 133 |
|
| 134 |
if ( plugin_basename( JETPACK__PLUGIN_FILE ) === $plugin_file_name ) { |
| 135 |
$this->activate_all_crons(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Deactivates any crons associated with the VideoPress module. |
| 141 |
* |
| 142 |
* @param string $cron_name Name of the cron to deactivate. |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
public function deactivate_cron( $cron_name ) { |
| 147 |
|
| 148 |
if ( ! $this->is_cron_valid( $cron_name ) ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
$next_scheduled_time = $this->check_cron( $cron_name ); |
| 153 |
wp_unschedule_event( $next_scheduled_time, $this->crons[ $cron_name ]['method'] ); |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Deactivates any crons associated with the VideoPress module.. |
| 160 |
*/ |
| 161 |
public function deactivate_all_crons() { |
| 162 |
|
| 163 |
foreach ( $this->crons as $cron_name => $cron ) { |
| 164 |
$this->deactivate_cron( $cron_name ); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Is the given cron job currently active? |
| 170 |
* |
| 171 |
* If so, return when it will next run, |
| 172 |
* |
| 173 |
* @param string $cron_name Cron job name. |
| 174 |
* |
| 175 |
* @return int|bool Timestamp of the next run time OR false. |
| 176 |
*/ |
| 177 |
public function check_cron( $cron_name ) { |
| 178 |
if ( ! $this->is_cron_valid( $cron_name ) ) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
return wp_next_scheduled( $this->crons[ $cron_name ]['method'] ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Check that the given cron job name is valid. |
| 187 |
* |
| 188 |
* @param string $cron_name Cron job name. |
| 189 |
* |
| 190 |
* @return bool |
| 191 |
*/ |
| 192 |
public function is_cron_valid( $cron_name ) { |
| 193 |
|
| 194 |
if ( ! isset( $this->crons[ $cron_name ]['method'] ) || ! isset( $this->crons[ $cron_name ]['interval'] ) ) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get a list of all of the crons that are available. |
| 203 |
* |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function get_crons() { |
| 207 |
return $this->crons; |
| 208 |
} |
| 209 |
} |
| 210 |
|