class.videopress-scheduler.php 4.6 KB

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