NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Cron.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * WP Cron job container.
  4. */
  5. class scbCron {
  6. protected $schedule;
  7. protected $interval;
  8. protected $time;
  9. protected $hook;
  10. protected $callback_args = array();
  11. /**
  12. * Create a new cron job.
  13. *
  14. * @param string|bool $file (optional) Reference to main plugin file
  15. * @param array $args List of args:
  16. * string $action OR callback $callback
  17. * string $schedule OR number $interval
  18. * array $callback_args (optional)
  19. *
  20. * @return void
  21. */
  22. public function __construct( $file = false, $args ) {
  23. // Set time & schedule
  24. if ( isset( $args['time'] ) ) {
  25. $this->time = $args['time'];
  26. }
  27. if ( isset( $args['interval'] ) ) {
  28. $this->schedule = $args['interval'] . 'secs';
  29. $this->interval = $args['interval'];
  30. } elseif ( isset( $args['schedule'] ) ) {
  31. $this->schedule = $args['schedule'];
  32. }
  33. // Set hook
  34. if ( isset( $args['action'] ) ) {
  35. $this->hook = $args['action'];
  36. } else if ( isset( $args['callback'] ) ) {
  37. $this->hook = self::_callback_to_string( $args['callback'] );
  38. add_action( $this->hook, $args['callback'] );
  39. } else if ( method_exists( $this, 'callback' ) ) {
  40. $this->hook = self::_callback_to_string( array( $this, 'callback' ) );
  41. add_action( $this->hook, $args['callback'] );
  42. } else {
  43. trigger_error( '$action OR $callback not set', E_USER_WARNING );
  44. }
  45. if ( isset( $args['callback_args'] ) ) {
  46. $this->callback_args = (array) $args['callback_args'];
  47. }
  48. if ( $file && $this->schedule ) {
  49. scbUtil::add_activation_hook( $file, array( $this, 'reset' ) );
  50. register_deactivation_hook( $file, array( $this, 'unschedule' ) );
  51. }
  52. add_filter( 'cron_schedules', array( $this, '_add_timing' ) );
  53. }
  54. /**
  55. * Change the interval of the cron job.
  56. *
  57. * @param array $args List of args:
  58. * string $schedule OR number $interval
  59. * timestamp $time ( optional )
  60. *
  61. * @return void
  62. */
  63. public function reschedule( $args ) {
  64. if ( $args['schedule'] && $this->schedule != $args['schedule'] ) {
  65. $this->schedule = $args['schedule'];
  66. } elseif ( $args['interval'] && $this->interval != $args['interval'] ) {
  67. $this->schedule = $args['interval'] . 'secs';
  68. $this->interval = $args['interval'];
  69. }
  70. $this->time = $args['time'];
  71. $this->reset();
  72. }
  73. /**
  74. * Reset the schedule.
  75. *
  76. * @return void
  77. */
  78. public function reset() {
  79. $this->unschedule();
  80. $this->schedule();
  81. }
  82. /**
  83. * Clear the cron job.
  84. *
  85. * @return void
  86. */
  87. public function unschedule() {
  88. # wp_clear_scheduled_hook( $this->hook, $this->callback_args );
  89. self::really_clear_scheduled_hook( $this->hook );
  90. }
  91. /**
  92. * Execute the job now.
  93. *
  94. * @param array $args (optional) List of arguments to pass to the callback.
  95. *
  96. * @return void
  97. */
  98. public function do_now( $args = null ) {
  99. if ( is_null( $args ) ) {
  100. $args = $this->callback_args;
  101. }
  102. do_action_ref_array( $this->hook, $args );
  103. }
  104. /**
  105. * Execute the job with a given delay.
  106. *
  107. * @param int $delay (optional) Delay in seconds.
  108. * @param array $args (optional) List of arguments to pass to the callback.
  109. *
  110. * @return void
  111. */
  112. public function do_once( $delay = 0, $args = null ) {
  113. if ( is_null( $args ) ) {
  114. $args = $this->callback_args;
  115. }
  116. wp_clear_scheduled_hook( $this->hook, $args );
  117. wp_schedule_single_event( time() + $delay, $this->hook, $args );
  118. }
  119. //_____INTERNAL METHODS_____
  120. /**
  121. * Adds custom schedule timing.
  122. *
  123. * @param array $schedules
  124. *
  125. * @return array
  126. */
  127. public function _add_timing( $schedules ) {
  128. if ( isset( $schedules[ $this->schedule ] ) ) {
  129. return $schedules;
  130. }
  131. $schedules[ $this->schedule ] = array(
  132. 'interval' => $this->interval,
  133. 'display' => $this->interval . ' seconds',
  134. );
  135. return $schedules;
  136. }
  137. /**
  138. * Schedule the job.
  139. *
  140. * @return void
  141. */
  142. protected function schedule() {
  143. if ( ! $this->time ) {
  144. $this->time = time();
  145. }
  146. wp_schedule_event( $this->time, $this->schedule, $this->hook, $this->callback_args );
  147. }
  148. /**
  149. * Clears scheduled job.
  150. *
  151. * @param string $name
  152. *
  153. * @return void
  154. */
  155. protected static function really_clear_scheduled_hook( $name ) {
  156. $crons = _get_cron_array();
  157. foreach ( $crons as $timestamp => $hooks ) {
  158. foreach ( $hooks as $hook => $args ) {
  159. if ( $hook == $name ) {
  160. unset( $crons[ $timestamp ][ $hook ] );
  161. }
  162. }
  163. if ( empty( $crons[ $timestamp ] ) ) {
  164. unset( $crons[ $timestamp ] );
  165. }
  166. }
  167. _set_cron_array( $crons );
  168. }
  169. /**
  170. * Generates a hook name from given callback.
  171. *
  172. * @param callback $callback
  173. *
  174. * @return string
  175. */
  176. protected static function _callback_to_string( $callback ) {
  177. if ( ! is_array( $callback ) ) {
  178. $str = $callback;
  179. } else if ( ! is_string( $callback[0] ) ) {
  180. $str = get_class( $callback[0] ) . '_' . $callback[1];
  181. } else {
  182. $str = $callback[0] . '::' . $callback[1];
  183. }
  184. $str .= '_hook';
  185. return $str;
  186. }
  187. }