WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-cron.php
Go to the documentation of this file.
1 <?php
2 
15 
16  const HALF_HOUR = 'wpdk_half_hour';
17  const TWO_MINUTES = 'wpdk_two_minutes';
18 
26  public static function init()
27  {
28  static $instance = null;
29  if ( is_null( $instance ) ) {
30  $instance = new WPDKCronSchedules();
31  }
32  return $instance;
33  }
34 
42  public function __construct()
43  {
44  // Custom periodic interval
45  add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) );
46  }
47 
57  public function cron_schedules( $schedules )
58  {
59  $new_schedules = array(
60  self::HALF_HOUR => array(
61  'interval' => HOUR_IN_SECONDS / 2,
62  'display' => __( 'Half hour', WPDK_TEXTDOMAIN )
63  ),
64  self::TWO_MINUTES => array(
65  'interval' => MINUTE_IN_SECONDS * 2,
66  'display' => __( 'Two minutes', WPDK_TEXTDOMAIN )
67  ),
68  );
69 
70  return array_merge( $schedules, $new_schedules );
71  }
72 
73 }
74 
75 
87 
95  public $cron = array();
96 
104  public function __construct()
105  {
106  $this->cron = _get_cron_array();
107  }
108 
116  public static function remove( $name )
117  {
118  $name = sanitize_title( $name );
119 
120  // Get time of next scheduled run
121  $timestamp = wp_next_scheduled( $name );
122 
123  // Unschedule custom action hook
124  wp_unschedule_event( $timestamp, $name );
125  }
126 
134  public static function removeAll( $name )
135  {
136  $name = sanitize_title( $name );
137  wp_clear_scheduled_hook( $name );
138  }
139 
140 }
141 
142 
154 class WPDKCron {
155 
156  public $name = '';
157  public $timestamp = 0;
158  public $recurrence = 0;
159 
171  public function __construct( $name, $timestap = false, $recurrence = 0 )
172  {
173  $this->name = sanitize_title( $name );
174  $this->timestamp = empty( $timestap ) ? time() : $timestap;
175  $this->recurrence = $recurrence;
176 
177  // Add action
178  add_action( $this->name, array( $this, 'cron' ) );
179 
180  // If this cron jobs is not scheduled then add to the WP list
181  if ( !wp_next_scheduled( $this->name ) ) {
182 
183  // Recurring
184  if ( empty( $timestap ) && !empty( $recurrence ) ) {
185  wp_schedule_event( time(), $recurrence, $this->name );
186  }
187  // Single event
188  elseif ( !empty( $timestap ) ) {
189  wp_schedule_single_event( $timestap, $this->name );
190  }
191  }
192  }
193 
200  public function cron( )
201  {
202  // Override
203  }
204 
212  public function remove()
213  {
214  WPDKCronController::remove( $this->name );
215  }
216 
217 }
218 
219 
232 
243  public function __construct( $name, $recurrence = 0 )
244  {
245  parent::__construct( $name, false, $recurrence );
246  }
247 }
248 
260 class WPDKSingleCron extends WPDKCron {
261 
272  public function __construct( $name, $timestap )
273  {
274  parent::__construct( $name, $timestap );
275  }
276 
277 }