VideoPress_Scheduler

VideoPress playback module markup generator.

Defined (1)

The class is defined in the following location(s).

/modules/videopress/class.videopress-scheduler.php  
  1. class VideoPress_Scheduler { 
  2.  
  3. /** 
  4. * The name of the function used to run the cleanup cron. 
  5. */ 
  6. const CLEANUP_CRON_METHOD = 'videopress_cleanup_media_library'; 
  7.  
  8. /** 
  9. * @var VideoPress_Scheduler 
  10. **/ 
  11. private static $instance = null; 
  12.  
  13. /** 
  14. * A list of all of the crons that are to be activated, along with their interval timings. 
  15. * @var array 
  16. */ 
  17. protected $crons = array( 
  18. // 'cleanup' => array( 
  19. // 'method' => self::CLEANUP_CRON_METHOD,  
  20. // 'interval' => 'minutes_30',  
  21. // ),  
  22. ); 
  23.  
  24.  
  25. /** 
  26. * Private VideoPress_Scheduler constructor. 
  27. * Use the VideoPress_Scheduler::init() method to get an instance. 
  28. */ 
  29. private function __construct() { 
  30. add_filter( 'cron_schedules', array( $this, 'add_30_minute_cron_interval' ) ); 
  31.  
  32. // Activate the cleanup cron if videopress is enabled, jetpack is activated, or jetpack is updated. 
  33. add_action( 'jetpack_activate_module_videopress', array( $this, 'activate_all_crons' ) ); 
  34. add_action( 'updating_jetpack_version', array( $this, 'activate_all_crons' ) ); 
  35. add_action( 'activated_plugin', array( $this, 'activate_crons_on_jetpack_activation' ) ); 
  36.  
  37. // Deactivate the cron if either videopress is disabled or Jetpack is disabled. 
  38. add_action( 'jetpack_deactivate_module_videopress', array( $this, 'deactivate_all_crons' ) ); 
  39. register_deactivation_hook( plugin_basename( JETPACK__PLUGIN_FILE ), array( $this, 'deactivate_all_crons' ) ); 
  40.  
  41. /** 
  42. * Initialize the VideoPress_Scheduler and get back a singleton instance. 
  43. * @return VideoPress_Scheduler 
  44. */ 
  45. public static function init() { 
  46. if ( is_null( self::$instance ) ) { 
  47. self::$instance = new VideoPress_Scheduler; 
  48.  
  49. return self::$instance; 
  50.  
  51. /** 
  52. * Adds 30 minute running interval to the cron schedules. 
  53. * @param array $current_schedules Currently defined schedules list. 
  54. * @return array 
  55. */ 
  56. public function add_30_minute_cron_interval( $current_schedules ) { 
  57.  
  58. // Only add the 30 minute interval if it wasn't already set. 
  59. if ( ! isset( $current_schedules['minutes_30'] ) ) { 
  60. $current_schedules['minutes_30'] = array( 
  61. 'interval' => 30 * MINUTE_IN_SECONDS,  
  62. 'display' => 'Every 30 minutes' 
  63. ); 
  64.  
  65. return $current_schedules; 
  66.  
  67. /** 
  68. * Activate a single cron 
  69. * @param string $cron_name 
  70. * @return bool 
  71. */ 
  72. public function activate_cron( $cron_name ) { 
  73.  
  74. if ( ! $this->is_cron_valid( $cron_name ) ) { 
  75. return false; 
  76.  
  77. if ( ! $this->check_cron( $cron_name ) ) { 
  78. wp_schedule_event( time(), $this->crons[ $cron_name ]['interval'], $this->crons[ $cron_name ]['method'] ); 
  79.  
  80. /** 
  81. * Activates widget update cron task. 
  82. */ 
  83. public function activate_all_crons() { 
  84.  
  85. if ( ! Jetpack::is_module_active( 'videopress' ) ) { 
  86. return false; 
  87.  
  88. foreach ( $this->crons as $cron_name => $cron ) { 
  89. if ( ! $this->check_cron( $cron_name ) ) { 
  90. wp_schedule_event( time(), $cron['interval'], $cron['method'] ); 
  91.  
  92. /** 
  93. * Only activate the crons if it is Jetpack that was activated. 
  94. * @param string $plugin_file_name 
  95. */ 
  96. public function activate_crons_on_jetpack_activation( $plugin_file_name ) { 
  97.  
  98. if ( plugin_basename( JETPACK__PLUGIN_FILE ) === $plugin_file_name ) { 
  99. $this->activate_all_crons(); 
  100.  
  101. /** 
  102. * Deactivates any crons associated with the VideoPress module. 
  103. * @return bool 
  104. */ 
  105. public function deactivate_cron( $cron_name ) { 
  106.  
  107. if ( ! $this->is_cron_valid( $cron_name ) ) { 
  108. return false; 
  109.  
  110. $next_scheduled_time = $this->check_cron( $cron_name ); 
  111. wp_unschedule_event( $next_scheduled_time, $this->crons[ $cron_name ]['method'] ); 
  112.  
  113. return true; 
  114.  
  115. /** 
  116. * Deactivates any crons associated with the VideoPress module.. 
  117. */ 
  118. public function deactivate_all_crons() { 
  119.  
  120. foreach ( $this->crons as $cron_name => $cron ) { 
  121. $this->deactivate_cron( $cron_name ); 
  122.  
  123. /** 
  124. * Is the given cron job currently active? 
  125. * If so, return when it will next run,  
  126. * @param string $cron_name 
  127. * @return int|bool Timestamp of the next run time OR false. 
  128. */ 
  129. public function check_cron( $cron_name ) { 
  130. if ( ! $this->is_cron_valid( $cron_name ) ) { 
  131. return false; 
  132.  
  133. return wp_next_scheduled( $this->crons[ $cron_name ]['method'] ); 
  134.  
  135. /** 
  136. * Check that the given cron job name is valid. 
  137. * @param string $cron_name 
  138. * @return bool 
  139. */ 
  140. public function is_cron_valid( $cron_name ) { 
  141.  
  142. if ( ! isset( $this->crons[ $cron_name ]['method'] ) || ! isset( $this->crons[ $cron_name ]['interval'] ) ) { 
  143. return false; 
  144.  
  145. return true; 
  146.  
  147. /** 
  148. * Get a list of all of the crons that are available. 
  149. * @return array 
  150. */ 
  151. public function get_crons() { 
  152. return $this->crons;