| 41 | | * */ |
| 42 | | public function install_plugin($h) |
| 43 | | { |
| 44 | | //$h->vars['cron_settings'] = $h->getSerializedSettings(); |
| 45 | | |
| 46 | | $create = false; |
| 47 | | |
| 48 | | if (!defined('SYS_UPDATES')) { $create = true; } |
| 49 | | if (defined('SYS_UPDATES') && SYS_UPDATES == 'true') { $create = true; } |
| 50 | | |
| 51 | | if ($create) { |
| 52 | | $timestamp = time(); |
| 53 | | $recurrence = "daily"; |
| 54 | | |
| 55 | | $hook = "SystemInfo:hotaru_version"; |
| 56 | | $args = array('timestamp' => $timestamp, 'recurrence' => $recurrence, 'hook' => $hook); |
| 57 | | $this->cron_update_job($h, $args); |
| 58 | | |
| 59 | | $hook = "SystemInfo:plugin_version_getAll"; |
| 60 | | $args = array('timestamp' => $timestamp, 'recurrence' => $recurrence, 'hook' => $hook); |
| 61 | | $this->cron_update_job($h, $args); |
| 62 | | |
| 63 | | $hook = "SystemInfo:hotaru_feedback"; |
| 64 | | $args = array('timestamp' => $timestamp, 'recurrence' => $recurrence, 'hook' => $hook); |
| 65 | | $this->cron_update_job($h, $args); |
| 66 | | } |
| 67 | | |
| 68 | | |
| 69 | | } |
| 70 | | |
| 71 | | public function theme_index_top($h) { |
| 72 | | $this->admin_theme_index_top($h); |
| 73 | | } |
| 74 | | |
| 75 | | public function admin_theme_index_top($h) { |
| 76 | | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| 77 | | $this->checkrunCron($h); |
| 78 | | //$this->run_cron($h); |
| 79 | | } |
| 80 | | |
| 81 | | /** |
| 82 | | * @param int $timestamp Timestamp for when to run the event. |
| 83 | | * @param string $hook Action hook to execute when cron is run. |
| 84 | | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| 85 | | */ |
| 86 | | public function cron_schedule_single_event( $timestamp, $hook, $args = array()) { |
| 87 | | // don't schedule a duplicate if there's already an identical event due in the next 10 minutes |
| 88 | | $next = cron_next_scheduled($hook, $args); |
| 89 | | if ( $next && $next <= $timestamp + 600 ) |
| 90 | | return; |
| 91 | | |
| 92 | | $crons = _get_cron_array(); |
| 93 | | $key = md5(serialize($args)); |
| 94 | | $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args ); |
| 95 | | uksort( $crons, "strnatcasecmp" ); |
| 96 | | _set_cron_array( $crons ); |
| 97 | | } |
| 98 | | |
| 99 | | |
| 100 | | public function cron_schedule_event($h, $timestamp, $recurrence, $hook, $args = array()) { |
| 101 | | $crons = $this->_get_cron_array($h); |
| 102 | | $schedules = $this->cron_get_schedules($h); |
| 103 | | $key = md5(serialize($args)); |
| 104 | | if ( !isset( $schedules[$recurrence] ) ) |
| 105 | | return false; |
| 106 | | |
| 107 | | $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); |
| 108 | | uksort( $crons, "strnatcasecmp" ); |
| 109 | | |
| 110 | | $this->_set_cron_array($h, $crons ); |
| 111 | | } |
| 112 | | |
| 113 | | /** |
| 114 | | * Reschedule a recurring event. |
| 115 | | * |
| 116 | | * @since 2.1.0 |
| 117 | | * |
| 118 | | * @param int $timestamp Timestamp for when to run the event. |
| 119 | | * @param string $recurrence How often the event should recur. |
| 120 | | * @param string $hook Action hook to execute when cron is run. |
| 121 | | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| 122 | | * @return bool|null False on failure. Null when event is rescheduled. |
| 123 | | */ |
| 124 | | public function cron_reschedule_event($h, $timestamp, $recurrence, $hook, $args = array()) { |
| 125 | | $crons = $this->_get_cron_array($h); |
| 126 | | $schedules = $this->cron_get_schedules($h); |
| 127 | | $key = md5(serialize($args)); |
| 128 | | $interval = 0; |
| 129 | | |
| 130 | | // First we try to get it from the schedule |
| 131 | | if ( 0 == $interval ) |
| 132 | | $interval = $schedules[$recurrence]['interval']; |
| 133 | | // Now we try to get it from the saved interval in case the schedule disappears |
| 134 | | if ( 0 == $interval ) |
| 135 | | $interval = $crons[$timestamp][$hook][$key]['interval']; |
| 136 | | // Now we assume something is wrong and fail to schedule |
| 137 | | if ( 0 == $interval ) |
| 138 | | return false; |
| 139 | | |
| 140 | | $now = time(); |
| 141 | | |
| 142 | | if ( $timestamp >= $now ) |
| 143 | | $timestamp = $now + $interval; |
| 144 | | else |
| 145 | | $timestamp = $now + ($interval - (($now - $timestamp) % $interval)); |
| 146 | | $this->cron_schedule_event($h, $timestamp, $recurrence, $hook, $args ); |
| 147 | | } |
| 148 | | |
| 149 | | /** |
| 150 | | * Unschedule a previously scheduled cron job. |
| 151 | | * |
| 152 | | * The $timestamp and $hook parameters are required, so that the event can be |
| 153 | | * identified. |
| 154 | | * |
| 155 | | * @since 2.1.0 |
| 156 | | * |
| 157 | | * @param int $timestamp Timestamp for when to run the event. |
| 158 | | * @param string $hook Action hook, the execution of which will be unscheduled. |
| 159 | | * @param array $args Arguments to pass to the hook's callback function. |
| 160 | | * Although not passed to a callback function, these arguments are used |
| 161 | | * to uniquely identify the scheduled event, so they should be the same |
| 162 | | * as those used when originally scheduling the event. |
| 163 | | */ |
| 164 | | public function cron_unschedule_event($h, $timestamp, $hook, $args = array() ) { |
| 165 | | $crons = $this->_get_cron_array($h); |
| 166 | | |
| 167 | | $key = md5(serialize($args)); |
| 168 | | unset( $crons[$timestamp][$hook][$key] ); |
| 169 | | if ( empty($crons[$timestamp][$hook]) ) |
| 170 | | unset( $crons[$timestamp][$hook] ); |
| 171 | | if ( empty($crons[$timestamp]) ) |
| 172 | | unset( $crons[$timestamp] ); |
| 173 | | $this->_set_cron_array($h, $crons ); |
| 174 | | } |
| 175 | | |
| 176 | | /** |
| 177 | | * Unschedule all cron jobs attached to a specific hook. |
| 178 | | * |
| 179 | | * @since 2.1.0 |
| 180 | | * |
| 181 | | * @param string $hook Action hook, the execution of which will be unscheduled. |
| 182 | | * @param mixed $args,... Optional. Event arguments. |
| 183 | | */ |
| 184 | | function cron_clear_scheduled_hook($h, $hook ) { |
| 185 | | $args = array_slice( func_get_args(), 1 ); |
| 186 | | |
| 187 | | while ( $timestamp = wp_next_scheduled( $hook, $args ) ) |
| 188 | | $this->cron_unschedule_event($h, $timestamp, $hook, $args ); |
| 189 | | } |
| 190 | | |
| 191 | | /** |
| 192 | | * Retrieve the next timestamp for a cron event. |
| 193 | | * |
| 194 | | * @since 2.1.0 |
| 195 | | * |
| 196 | | * @param string $hook Action hook to execute when cron is run. |
| 197 | | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| 198 | | * @return bool|int The UNIX timestamp of the next time the scheduled event will occur. |
| 199 | | */ |
| 200 | | public function cron_next_scheduled( $hook, $args = array() ) { |
| 201 | | $crons = _get_cron_array(); |
| 202 | | $key = md5(serialize($args)); |
| 203 | | if ( empty($crons) ) |
| 204 | | return false; |
| 205 | | foreach ( $crons as $timestamp => $cron ) { |
| 206 | | if ( isset( $cron[$hook][$key] ) ) |
| 207 | | return $timestamp; |
| 208 | | } |
| 209 | | return false; |
| 210 | | } |
| 211 | | |
| 212 | | /** |
| 213 | | * Send request to run cron through HTTP request that doesn't halt page loading. |
| 214 | | * |
| 215 | | * @since 2.1.0 |
| 216 | | * |
| 217 | | * @return null Cron could not be spawned, because it is not needed to run. |
| 218 | | */ |
| 219 | | public function spawn_cron($h, $local_time = 0 ) { |
| 220 | | |
| 221 | | if ( !$local_time ) |
| 222 | | $local_time = time(); |
| 223 | | |
| 224 | | if ( defined('DOING_CRON') || isset($_GET['doing_cron']) ) |
| 225 | | return; |
| 226 | | |
| 227 | | /* |
| 228 | | * do not even start the cron if local server timer has drifted |
| 229 | | * such as due to power failure, or misconfiguration |
| 230 | | */ |
| 231 | | $timer_accurate = $this->check_server_timer( $local_time ); |
| 232 | | if ( !$timer_accurate ) |
| 233 | | return; |
| 234 | | |
| 235 | | /* |
| 236 | | * multiple processes on multiple web servers can run this code concurrently |
| 237 | | * try to make this as atomic as possible by setting doing_cron switch |
| 238 | | */ |
| 239 | | $flag = $h->getSetting('doing_cron'); |
| 240 | | |
| 241 | | if ( $flag > $local_time + 10*60 ) |
| 242 | | $flag = 0; |
| 243 | | |
| 244 | | // don't run if another process is currently running it or more than once every 60 sec. |
| 245 | | if ( $flag + 60 > $local_time ) |
| 246 | | return; |
| 247 | | |
| 248 | | //sanity check |
| 249 | | $crons = $this->_get_cron_array($h); |
| 250 | | if ( !is_array($crons) ) |
| 251 | | return; |
| 252 | | |
| 253 | | $keys = array_keys( $crons ); |
| 254 | | if ( isset($keys[0]) && $keys[0] > $local_time ) |
| 255 | | return; |
| 256 | | |
| 257 | | $h->updateSetting('doing_cron', $local_time); |
| 258 | | |
| 259 | | //$cron_url = get_option( 'siteurl' ) . '/wp-cron.php?doing_wp_cron'; |
| 260 | | //wp_remote_post( $cron_url, array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)) ); |
| 261 | | |
| 262 | | $this->checkrunCron($h); |
| 263 | | } |
| 264 | | |
| 265 | | /** |
| 266 | | * Run scheduled callbacks or spawn cron for all scheduled events. |
| 267 | | * |
| 268 | | * @since 2.1.0 |
| 269 | | * |
| 270 | | * @return null When doesn't need to run Cron. |
| 271 | | */ |
| 272 | | public function run_cron($h) { |
| 273 | | if ( false === $crons = $this->_get_cron_array($h) ) |
| 274 | | return; |
| 275 | | |
| 276 | | $local_time = time(); |
| 277 | | $keys = array_keys( $crons ); |
| 278 | | if ( isset($keys[0]) && $keys[0] > $local_time ) |
| 279 | | return; |
| 280 | | |
| 281 | | $schedules = $this->cron_get_schedules($h); |
| 282 | | foreach ( $crons as $timestamp => $cronhooks ) { |
| 283 | | if ( $timestamp > $local_time ) break; |
| 284 | | foreach ( (array) $cronhooks as $hook => $args ) { |
| 285 | | if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) |
| 286 | | continue; |
| 287 | | $this->spawn_cron($h, $local_time ); |
| 288 | | break 2; |
| 289 | | } |
| 290 | | } |
| 291 | | } |
| 292 | | |
| 293 | | /** |
| 294 | | * Retrieve supported and filtered Cron recurrences. |
| 295 | | * |
| 296 | | * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by |
| 297 | | * hooking into the 'cron_schedules' filter. The filter accepts an array of |
| 298 | | * arrays. The outer array has a key that is the name of the schedule or for |
| 299 | | * example 'weekly'. The value is an array with two keys, one is 'interval' and |
| 300 | | * the other is 'display'. |
| 301 | | * |
| 302 | | * The 'interval' is a number in seconds of when the cron job should run. So for |
| 303 | | * 'hourly', the time is 3600 or 60*60. For weekly, the value would be |
| 304 | | * 60*60*24*7 or 604800. The value of 'interval' would then be 604800. |
| 305 | | * |
| 306 | | * The 'display' is the description. For the 'weekly' key, the 'display' would |
| 307 | | * be <code>__('Once Weekly')</code>. |
| 308 | | * |
| 309 | | * For your plugin, you will be passed an array. you can easily add your |
| 310 | | * schedule by doing the following. |
| 311 | | * <code> |
| 312 | | * // filter parameter variable name is 'array' |
| 313 | | * $array['weekly'] = array( |
| 314 | | * 'interval' => 604800, |
| 315 | | * 'display' => __('Once Weekly') |
| 316 | | * ); |
| 317 | | * </code> |
| 318 | | * |
| 319 | | * @since 2.1.0 |
| 320 | | * |
| 321 | | * @return array |
| 322 | | */ |
| 323 | | public function cron_get_schedules($h) { |
| 324 | | $schedules = array( |
| 325 | | 'hourly' => array( 'interval' => 3600, 'display' => 'Once Hourly' ), |
| 326 | | 'twicedaily' => array( 'interval' => 43200, 'display' => 'Twice Daily' ), |
| 327 | | 'daily' => array( 'interval' => 86400, 'display' => 'Once Daily' ), |
| 328 | | 'weekly' => array( 'interval' => 604800, 'display' => 'Once Weekly' ), |
| 329 | | ); |
| 330 | | |
| 331 | | //return array_merge( apply_filters( 'cron_schedules', array() ), $schedules ); |
| 332 | | return $schedules; |
| 333 | | } |
| 334 | | |
| 335 | | /** |
| 336 | | * Retrieve Cron schedule for hook with arguments. |
| 337 | | * |
| 338 | | * @since 2.1.0 |
| 339 | | * |
| 340 | | * @param string $hook Action hook to execute when cron is run. |
| 341 | | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| 342 | | * @return string|bool False, if no schedule. Schedule on success. |
| 343 | | */ |
| 344 | | public function cron_get_schedule($hook, $args = array()) { |
| 345 | | $crons = _get_cron_array(); |
| 346 | | $key = md5(serialize($args)); |
| 347 | | if ( empty($crons) ) |
| 348 | | return false; |
| 349 | | foreach ( $crons as $timestamp => $cron ) { |
| 350 | | if ( isset( $cron[$hook][$key] ) ) |
| 351 | | return $cron[$hook][$key]['schedule']; |
| 352 | | } |
| 353 | | return false; |
| 354 | | } |
| 355 | | |
| 356 | | // |
| 357 | | // Private functions |
| 358 | | // |
| 359 | | |
| 360 | | /** |
| 361 | | * Retrieve cron info array option. |
| 362 | | * |
| 363 | | */ |
| 364 | | public function _get_cron_array($h) { |
| 365 | | if (isset($h->vars['cron_settings'])) { |
| 366 | | $cron = $h->vars['cron_settings']; |
| 367 | | if ( ! is_array($cron) ) |
| 368 | | return false; |
| 369 | | |
| 370 | | return $cron; |
| 371 | | } |
| 372 | | else { |
| | 43 | * |
| | 44 | * @param Hotaru $h |
| | 45 | */ |
| | 46 | public function install_plugin($h) |
| | 47 | { |
| | 48 | $timestamp = time(); |
| | 49 | $recurrence = "daily"; |
| | 50 | |
| | 51 | $hook = "SystemInfo:hotaru_version"; |
| | 52 | $args = array('timestamp' => $timestamp, 'recurrence' => $recurrence, 'hook' => $hook); |
| | 53 | $this->cron_update_job($h, $args); |
| | 54 | |
| | 55 | $hook = "SystemInfo:plugin_version_getAll"; |
| | 56 | $args = array('timestamp' => $timestamp, 'recurrence' => $recurrence, 'hook' => $hook); |
| | 57 | $this->cron_update_job($h, $args); |
| | 58 | |
| | 59 | $hook = "SystemInfo:hotaru_feedback"; |
| | 60 | $args = array('timestamp' => $timestamp, 'recurrence' => $recurrence, 'hook' => $hook); |
| | 61 | $this->cron_update_job($h, $args); |
| | 62 | } |
| | 63 | |
| | 64 | /** |
| | 65 | * Hook for the theme |
| | 66 | * |
| | 67 | * @param Hotaru $h |
| | 68 | */ |
| | 69 | public function theme_index_top($h) |
| | 70 | { |
| | 71 | $this->admin_theme_index_top($h); |
| | 72 | } |
| | 73 | |
| | 74 | /** |
| | 75 | * Hook for the admin theme |
| | 76 | * |
| | 77 | * @param Hotaru $h |
| | 78 | */ |
| | 79 | public function admin_theme_index_top($h) |
| | 80 | { |
| | 81 | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| | 82 | $this->checkrunCron($h); |
| | 83 | } |
| | 84 | |
| | 85 | /** |
| | 86 | * Schedules a single event |
| | 87 | * |
| | 88 | * @param int $timestamp Timestamp for when to run the event. |
| | 89 | * @param string $hook Action hook to execute when cron is run. |
| | 90 | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| | 91 | * @return null |
| | 92 | */ |
| | 93 | public function cron_schedule_single_event($timestamp, $hook, $args = array()) |
| | 94 | { |
| | 95 | // don't schedule a duplicate if there's already an identical event due in the next 10 minutes |
| | 96 | $next = cron_next_scheduled($hook, $args); |
| | 97 | if ($next && $next <= $timestamp + 600) { |
| | 98 | return; |
| | 99 | } |
| | 100 | |
| | 101 | $crons = _get_cron_array(); |
| | 102 | $key = md5(serialize($args)); |
| | 103 | $crons[$timestamp][$hook][$key] = array('schedule' => false, 'args' => $args); |
| | 104 | uksort($crons, "strnatcasecmp"); |
| | 105 | _set_cron_array($crons); |
| | 106 | } |
| | 107 | |
| | 108 | /** |
| | 109 | * |
| | 110 | * @param Hotaru $h |
| | 111 | * @param int $timestamp |
| | 112 | * @param string $recurrence |
| | 113 | * @param string $hook |
| | 114 | * @param array $args |
| | 115 | * @return bool|null |
| | 116 | */ |
| | 117 | public function cron_schedule_event($h, $timestamp, $recurrence, $hook, $args = array()) |
| | 118 | { |
| | 119 | $crons = $this->_get_cron_array($h); |
| | 120 | $schedules = $this->cron_get_schedules($h); |
| | 121 | $key = md5(serialize($args)); |
| | 122 | if (!isset($schedules[$recurrence])) { |
| | 123 | return false; |
| | 124 | } |
| | 125 | |
| | 126 | $crons[$timestamp][$hook][$key] = array('schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval']); |
| | 127 | uksort($crons, "strnatcasecmp"); |
| | 128 | |
| | 129 | $this->_set_cron_array($h, $crons); |
| | 130 | } |
| | 131 | |
| | 132 | /** |
| | 133 | * Reschedule a recurring event. |
| | 134 | * |
| | 135 | * @since 2.1.0 |
| | 136 | * |
| | 137 | * @param int $timestamp Timestamp for when to run the event. |
| | 138 | * @param string $recurrence How often the event should recur. |
| | 139 | * @param string $hook Action hook to execute when cron is run. |
| | 140 | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| | 141 | * @return bool|null False on failure. Null when event is rescheduled. |
| | 142 | */ |
| | 143 | public function cron_reschedule_event($h, $timestamp, $recurrence, $hook, $args = array()) |
| | 144 | { |
| | 145 | $crons = $this->_get_cron_array($h); |
| | 146 | $schedules = $this->cron_get_schedules($h); |
| | 147 | $key = md5(serialize($args)); |
| | 148 | $interval = 0; |
| | 149 | |
| | 150 | // First we try to get it from the schedule |
| | 151 | if (0 == $interval) { |
| | 152 | $interval = $schedules[$recurrence]['interval']; |
| | 153 | } |
| | 154 | // Now we try to get it from the saved interval in case the schedule disappears |
| | 155 | if (0 == $interval) { |
| | 156 | $interval = $crons[$timestamp][$hook][$key]['interval']; |
| | 157 | } |
| | 158 | // Now we assume something is wrong and fail to schedule |
| | 159 | if (0 == $interval) { |
| | 160 | return false; |
| | 161 | } |
| | 162 | |
| | 163 | $now = time(); |
| | 164 | |
| | 165 | if ($timestamp >= $now) { |
| | 166 | $timestamp = $now + $interval; |
| | 167 | } else { |
| | 168 | $timestamp = $now + ($interval - (($now - $timestamp) % $interval)); |
| | 169 | } |
| | 170 | $this->cron_schedule_event($h, $timestamp, $recurrence, $hook, $args); |
| | 171 | } |
| | 172 | |
| | 173 | /** |
| | 174 | * Unschedule a previously scheduled cron job. |
| | 175 | * |
| | 176 | * The $timestamp and $hook parameters are required, so that the event can be |
| | 177 | * identified. |
| | 178 | * |
| | 179 | * @since 2.1.0 |
| | 180 | * |
| | 181 | * @param int $timestamp Timestamp for when to run the event. |
| | 182 | * @param string $hook Action hook, the execution of which will be unscheduled. |
| | 183 | * @param array $args Arguments to pass to the hook's callback function. |
| | 184 | * Although not passed to a callback function, these arguments are used |
| | 185 | * to uniquely identify the scheduled event, so they should be the same |
| | 186 | * as those used when originally scheduling the event. |
| | 187 | */ |
| | 188 | public function cron_unschedule_event($h, $timestamp, $hook, $args = array()) |
| | 189 | { |
| | 190 | $crons = $this->_get_cron_array($h); |
| | 191 | |
| | 192 | $key = md5(serialize($args)); |
| | 193 | unset($crons[$timestamp][$hook][$key]); |
| | 194 | if (empty($crons[$timestamp][$hook])) { |
| | 195 | unset($crons[$timestamp][$hook]); |
| | 196 | } |
| | 197 | if (empty($crons[$timestamp])) { |
| | 198 | unset($crons[$timestamp]); |
| | 199 | } |
| | 200 | $this->_set_cron_array($h, $crons); |
| | 201 | } |
| | 202 | |
| | 203 | /** |
| | 204 | * Unschedule all cron jobs attached to a specific hook. |
| | 205 | * |
| | 206 | * @since 2.1.0 |
| | 207 | * |
| | 208 | * @param string $hook Action hook, the execution of which will be unscheduled. |
| | 209 | * @param mixed $args,... Optional. Event arguments. |
| | 210 | */ |
| | 211 | function cron_clear_scheduled_hook($h, $hook) |
| | 212 | { |
| | 213 | $args = array_slice(func_get_args(), 1); |
| | 214 | |
| | 215 | while ($timestamp = wp_next_scheduled($hook, $args)) { |
| | 216 | $this->cron_unschedule_event($h, $timestamp, $hook, $args); |
| | 217 | } |
| | 218 | } |
| | 219 | |
| | 220 | /** |
| | 221 | * Retrieve the next timestamp for a cron event. |
| | 222 | * |
| | 223 | * @since 2.1.0 |
| | 224 | * |
| | 225 | * @param string $hook Action hook to execute when cron is run. |
| | 226 | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| | 227 | * @return bool|int The UNIX timestamp of the next time the scheduled event will occur. |
| | 228 | */ |
| | 229 | public function cron_next_scheduled($hook, $args = array()) |
| | 230 | { |
| | 231 | $crons = _get_cron_array(); |
| | 232 | $key = md5(serialize($args)); |
| | 233 | if (empty($crons)) { |
| | 234 | return false; |
| | 235 | } |
| | 236 | foreach ($crons as $timestamp => $cron) { |
| | 237 | if (isset($cron[$hook][$key])) { |
| | 238 | return $timestamp; |
| | 239 | } |
| | 240 | } |
| 374 | | } |
| 375 | | } |
| 376 | | |
| 377 | | /** |
| 378 | | * Updates the CRON settings. |
| 379 | | * |
| 380 | | */ |
| 381 | | public function _set_cron_array($h,$cron) { |
| 382 | | $h->updateSetting('cron_settings', serialize($cron)); |
| 383 | | $h->vars['cron_settings'] = $cron; |
| 384 | | } |
| 385 | | |
| 386 | | // stub for checking server timer accuracy, using outside standard time sources |
| 387 | | public function check_server_timer( $local_time ) { |
| 388 | | return true; |
| 389 | | } |
| 390 | | |
| 391 | | |
| 392 | | public function checkrunCron($h) { |
| 393 | | if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') ) |
| 394 | | return; |
| 395 | | |
| 396 | | define('DOING_CRON', true); |
| 397 | | |
| 398 | | if ( false === $crons = $this->_get_cron_array($h) ) |
| 399 | | return; |
| 400 | | |
| 401 | | $keys = array_keys( $crons ); |
| 402 | | $local_time = time(); |
| 403 | | |
| 404 | | if ( isset($keys[0]) && $keys[0] > $local_time ) |
| 405 | | return; |
| 406 | | |
| 407 | | foreach ($crons as $timestamp => $cronhooks) { |
| 408 | | if ( $timestamp > $local_time ) |
| 409 | | return; |
| 410 | | |
| 411 | | foreach ($cronhooks as $hook => $keys) { |
| 412 | | foreach ($keys as $k => $v) { |
| 413 | | $schedule = $v['schedule']; |
| 414 | | if ($schedule != false) { |
| 415 | | //$new_args = array($timestamp, $schedule, $hook, $v['args']); |
| 416 | | $this->cron_reschedule_event($h, $timestamp, $schedule, $hook, $v['args']); |
| 417 | | } |
| 418 | | $this->cron_unschedule_event($h, $timestamp, $hook, $v['args']); |
| 419 | | //call function to do task required for cron |
| 420 | | //print "running hook..-> " . $hook. " "; |
| 421 | | if (strpos($hook, ':')) { |
| 422 | | $parts = explode(':', $hook); |
| 423 | | if (count($parts) > 1) { |
| 424 | | $cron_call = new $parts[0](); |
| 425 | | $cron_call->$parts[1]($h); |
| 426 | | } |
| 427 | | } else { |
| 428 | | $h->pluginHook($hook, '', $v['args']); |
| 429 | | } |
| 430 | | } |
| 431 | | } |
| 432 | | } |
| 433 | | } |
| 434 | | |
| 435 | | public function cron_update_job($h, $cron_data) |
| 436 | | { |
| 437 | | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| 438 | | |
| 439 | | $timestamp = (isset($cron_data['timestamp'])) ? $cron_data['timestamp'] : array(); |
| 440 | | $recurrence = (isset($cron_data['recurrence'])) ? $cron_data['recurrence'] : array(); |
| 441 | | $hook = (isset($cron_data['hook'])) ? $cron_data['hook'] : array(); |
| 442 | | $args = (isset($cron_data['args'])) ? $cron_data['args'] : array(); |
| 443 | | $cron_exists = false; |
| 444 | | |
| 445 | | // check whether already have existing event for this job |
| 446 | | // match against hook and args. note args must match |
| 447 | | $current_crons = $this->_get_cron_array($h); |
| 448 | | |
| 449 | | if ($current_crons) { |
| 450 | | foreach ($current_crons as $current_timestamp => $current_cronhooks) { |
| 451 | | foreach ($current_cronhooks as $current_hook => $current_keys) { |
| 452 | | if ($current_hook == $hook) { |
| 453 | | foreach ($current_keys as $current_md5 => $current_job) { |
| 454 | | //print_r( $current_job["args"]); print " ** "; print_r($args); |
| 455 | | if ($current_job["args"] == $args) { |
| | 242 | } |
| | 243 | |
| | 244 | /** |
| | 245 | * Send request to run cron through HTTP request that doesn't halt page loading. |
| | 246 | * |
| | 247 | * @since 2.1.0 |
| | 248 | * |
| | 249 | * @return null Cron could not be spawned, because it is not needed to run. |
| | 250 | */ |
| | 251 | public function spawn_cron($h, $local_time = 0) |
| | 252 | { |
| | 253 | |
| | 254 | if (!$local_time) { |
| | 255 | $local_time = time(); |
| | 256 | } |
| | 257 | |
| | 258 | if (defined('DOING_CRON') || isset($_GET['doing_cron'])) { |
| | 259 | return; |
| | 260 | } |
| | 261 | |
| | 262 | /* |
| | 263 | * do not even start the cron if local server timer has drifted |
| | 264 | * such as due to power failure, or misconfiguration |
| | 265 | */ |
| | 266 | $timer_accurate = $this->check_server_timer($local_time); |
| | 267 | if (!$timer_accurate) { |
| | 268 | return; |
| | 269 | } |
| | 270 | |
| | 271 | /* |
| | 272 | * multiple processes on multiple web servers can run this code concurrently |
| | 273 | * try to make this as atomic as possible by setting doing_cron switch |
| | 274 | */ |
| | 275 | $flag = $h->getSetting('doing_cron'); |
| | 276 | |
| | 277 | if ($flag > $local_time + 10 * 60) { |
| | 278 | $flag = 0; |
| | 279 | } |
| | 280 | |
| | 281 | // don't run if another process is currently running it or more than once every 60 sec. |
| | 282 | if ($flag + 60 > $local_time) { |
| | 283 | return; |
| | 284 | } |
| | 285 | |
| | 286 | //sanity check |
| | 287 | $crons = $this->_get_cron_array($h); |
| | 288 | if (!is_array($crons)) { |
| | 289 | return; |
| | 290 | } |
| | 291 | |
| | 292 | $keys = array_keys($crons); |
| | 293 | if (isset($keys[0]) && $keys[0] > $local_time) { |
| | 294 | return; |
| | 295 | } |
| | 296 | |
| | 297 | $h->updateSetting('doing_cron', $local_time); |
| | 298 | |
| | 299 | //$cron_url = get_option( 'siteurl' ) . '/wp-cron.php?doing_wp_cron'; |
| | 300 | //wp_remote_post( $cron_url, array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)) ); |
| | 301 | |
| | 302 | $this->checkrunCron($h); |
| | 303 | } |
| | 304 | |
| | 305 | /** |
| | 306 | * Run scheduled callbacks or spawn cron for all scheduled events. |
| | 307 | * |
| | 308 | * @since 2.1.0 |
| | 309 | * |
| | 310 | * @return null When doesn't need to run Cron. |
| | 311 | */ |
| | 312 | public function run_cron($h) |
| | 313 | { |
| | 314 | if (false === $crons = $this->_get_cron_array($h)) { |
| | 315 | return; |
| | 316 | } |
| | 317 | |
| | 318 | $local_time = time(); |
| | 319 | $keys = array_keys($crons); |
| | 320 | if (isset($keys[0]) && $keys[0] > $local_time) { |
| | 321 | return; |
| | 322 | } |
| | 323 | |
| | 324 | $schedules = $this->cron_get_schedules($h); |
| | 325 | foreach ($crons as $timestamp => $cronhooks) { |
| | 326 | if ($timestamp > $local_time) { |
| | 327 | break; |
| | 328 | } |
| | 329 | foreach ((array) $cronhooks as $hook => $args) { |
| | 330 | if (isset($schedules[$hook]['callback']) && !call_user_func($schedules[$hook]['callback'])) { |
| | 331 | continue; |
| | 332 | } |
| | 333 | $this->spawn_cron($h, $local_time); |
| | 334 | break 2; |
| | 335 | } |
| | 336 | } |
| | 337 | } |
| | 338 | |
| | 339 | /** |
| | 340 | * Retrieve supported and filtered Cron recurrences. |
| | 341 | * |
| | 342 | * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by |
| | 343 | * hooking into the 'cron_schedules' filter. The filter accepts an array of |
| | 344 | * arrays. The outer array has a key that is the name of the schedule or for |
| | 345 | * example 'weekly'. The value is an array with two keys, one is 'interval' and |
| | 346 | * the other is 'display'. |
| | 347 | * |
| | 348 | * The 'interval' is a number in seconds of when the cron job should run. So for |
| | 349 | * 'hourly', the time is 3600 or 60*60. For weekly, the value would be |
| | 350 | * 60*60*24*7 or 604800. The value of 'interval' would then be 604800. |
| | 351 | * |
| | 352 | * The 'display' is the description. For the 'weekly' key, the 'display' would |
| | 353 | * be <code>__('Once Weekly')</code>. |
| | 354 | * |
| | 355 | * For your plugin, you will be passed an array. you can easily add your |
| | 356 | * schedule by doing the following. |
| | 357 | * <code> |
| | 358 | * // filter parameter variable name is 'array' |
| | 359 | * $array['weekly'] = array( |
| | 360 | * 'interval' => 604800, |
| | 361 | * 'display' => __('Once Weekly') |
| | 362 | * ); |
| | 363 | * </code> |
| | 364 | * |
| | 365 | * @since 2.1.0 |
| | 366 | * |
| | 367 | * @return array |
| | 368 | */ |
| | 369 | public function cron_get_schedules($h) |
| | 370 | { |
| | 371 | $schedules = array( |
| | 372 | 'hourly' => array('interval' => 3600, 'display' => 'Once Hourly'), |
| | 373 | 'twicedaily' => array('interval' => 43200, 'display' => 'Twice Daily'), |
| | 374 | 'daily' => array('interval' => 86400, 'display' => 'Once Daily'), |
| | 375 | 'weekly' => array('interval' => 604800, 'display' => 'Once Weekly'), |
| | 376 | ); |
| | 377 | |
| | 378 | //return array_merge( apply_filters( 'cron_schedules', array() ), $schedules ); |
| | 379 | return $schedules; |
| | 380 | } |
| | 381 | |
| | 382 | /** |
| | 383 | * Retrieve Cron schedule for hook with arguments. |
| | 384 | * |
| | 385 | * @since 2.1.0 |
| | 386 | * |
| | 387 | * @param string $hook Action hook to execute when cron is run. |
| | 388 | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| | 389 | * @return string|bool False, if no schedule. Schedule on success. |
| | 390 | */ |
| | 391 | public function cron_get_schedule($hook, $args = array()) |
| | 392 | { |
| | 393 | $crons = _get_cron_array(); |
| | 394 | $key = md5(serialize($args)); |
| | 395 | if (empty($crons)) |
| | 396 | return false; |
| | 397 | foreach ($crons as $timestamp => $cron) { |
| | 398 | if (isset($cron[$hook][$key])) { |
| | 399 | return $cron[$hook][$key]['schedule']; |
| | 400 | } |
| | 401 | } |
| | 402 | return false; |
| | 403 | } |
| | 404 | |
| | 405 | // |
| | 406 | // Private functions |
| | 407 | // |
| | 408 | |
| | 409 | /** |
| | 410 | * Retrieve cron info array option. |
| | 411 | * |
| | 412 | */ |
| | 413 | public function _get_cron_array($h) |
| | 414 | { |
| | 415 | if (isset($h->vars['cron_settings'])) { |
| | 416 | $cron = $h->vars['cron_settings']; |
| | 417 | if (!is_array($cron)) { |
| | 418 | return false; |
| | 419 | } |
| | 420 | |
| | 421 | return $cron; |
| | 422 | } else { |
| | 423 | return false; |
| | 424 | } |
| | 425 | } |
| | 426 | |
| | 427 | /** |
| | 428 | * Updates the CRON settings. |
| | 429 | * |
| | 430 | */ |
| | 431 | public function _set_cron_array($h, $cron) |
| | 432 | { |
| | 433 | $h->updateSetting('cron_settings', serialize($cron)); |
| | 434 | $h->vars['cron_settings'] = $cron; |
| | 435 | } |
| | 436 | |
| | 437 | // stub for checking server timer accuracy, using outside standard time sources |
| | 438 | public function check_server_timer($local_time) |
| | 439 | { |
| | 440 | return true; |
| | 441 | } |
| | 442 | |
| | 443 | public function checkrunCron($h) |
| | 444 | { |
| | 445 | if (!empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON')) { |
| | 446 | return; |
| | 447 | } |
| | 448 | |
| | 449 | define('DOING_CRON', true); |
| | 450 | |
| | 451 | if (false === $crons = $this->_get_cron_array($h)) { |
| | 452 | return; |
| | 453 | } |
| | 454 | |
| | 455 | $keys = array_keys($crons); |
| | 456 | $local_time = time(); |
| | 457 | |
| | 458 | if (isset($keys[0]) && $keys[0] > $local_time) { |
| | 459 | return; |
| | 460 | } |
| | 461 | |
| | 462 | foreach ($crons as $timestamp => $cronhooks) { |
| | 463 | if ($timestamp > $local_time) { |
| | 464 | return; |
| | 465 | } |
| | 466 | |
| | 467 | foreach ($cronhooks as $hook => $keys) { |
| | 468 | foreach ($keys as $k => $v) { |
| | 469 | $schedule = $v['schedule']; |
| | 470 | if ($schedule != false) { |
| | 471 | //$new_args = array($timestamp, $schedule, $hook, $v['args']); |
| | 472 | $this->cron_reschedule_event($h, $timestamp, $schedule, $hook, $v['args']); |
| | 473 | } |
| | 474 | $this->cron_unschedule_event($h, $timestamp, $hook, $v['args']); |
| | 475 | //call function to do task required for cron |
| | 476 | //print "running hook..-> " . $hook. " "; |
| | 477 | if (strpos($hook, ':')) { |
| | 478 | $parts = explode(':', $hook); |
| | 479 | if (count($parts) > 1) { |
| | 480 | $cron_call = new $parts[0](); |
| | 481 | $cron_call->$parts[1]($h); |
| | 482 | } |
| | 483 | } else { |
| | 484 | $h->pluginHook($hook, '', $v['args']); |
| | 485 | } |
| | 486 | } |
| | 487 | } |
| | 488 | } |
| | 489 | } |
| | 490 | |
| | 491 | public function cron_update_job($h, $cron_data) |
| | 492 | { |
| | 493 | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| | 494 | |
| | 495 | $timestamp = (isset($cron_data['timestamp'])) ? $cron_data['timestamp'] : array(); |
| | 496 | $recurrence = (isset($cron_data['recurrence'])) ? $cron_data['recurrence'] : array(); |
| | 497 | $hook = (isset($cron_data['hook'])) ? $cron_data['hook'] : array(); |
| | 498 | $args = (isset($cron_data['args'])) ? $cron_data['args'] : array(); |
| | 499 | $cron_exists = false; |
| | 500 | |
| | 501 | // check whether already have existing event for this job |
| | 502 | // match against hook and args. note args must match |
| | 503 | $current_crons = $this->_get_cron_array($h); |
| | 504 | |
| | 505 | if ($current_crons) { |
| | 506 | foreach ($current_crons as $current_timestamp => $current_cronhooks) { |
| | 507 | foreach ($current_cronhooks as $current_hook => $current_keys) { |
| | 508 | if ($current_hook == $hook) { |
| | 509 | foreach ($current_keys as $current_md5 => $current_job) { |
| | 510 | //print_r( $current_job["args"]); print " ** "; print_r($args); |
| | 511 | if ($current_job["args"] == $args) { |
| | 512 | $this->cron_schedule_event($h, $timestamp, $recurrence, $hook, $args); |
| | 513 | $this->cron_unschedule_event($h, $current_timestamp, $current_hook, $args); |
| | 514 | $cron_exists = true; |
| | 515 | } |
| | 516 | } |
| | 517 | } |
| | 518 | } |
| | 519 | } |
| | 520 | } |
| | 521 | |
| | 522 | if (!$cron_exists) { |
| 457 | | $this->cron_unschedule_event($h, $current_timestamp, $current_hook, $args); |
| 458 | | $cron_exists = true; |
| 459 | | } |
| 460 | | } |
| 461 | | } |
| 462 | | } |
| 463 | | } |
| 464 | | } |
| 465 | | |
| 466 | | if (!$cron_exists) $this->cron_schedule_event($h, $timestamp, $recurrence, $hook, $args); |
| 467 | | } |
| 468 | | |
| 469 | | public function cron_delete_job($h, $cron_data) |
| 470 | | { |
| 471 | | //load current cron jobs from memory space |
| 472 | | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| 473 | | |
| 474 | | $hook = (isset($cron_data['hook'])) ? $cron_data['hook'] : array(); |
| 475 | | $args = (isset($cron_data['args'])) ? $cron_data['args'] : array(); |
| 476 | | |
| 477 | | $current_crons = $this->_get_cron_array($h); |
| 478 | | |
| 479 | | foreach ($current_crons as $current_timestamp => $current_cronhooks) { |
| 480 | | foreach ($current_cronhooks as $current_hook => $current_keys) { |
| 481 | | foreach ($current_keys as $current_md5 => $current_job) { |
| 482 | | foreach ($current_job as $current_set => $current_args) { |
| 483 | | if ($current_hook == $hook && $current_args == $args) { |
| 484 | | $this->cron_unschedule_event($h, $current_timestamp, $current_hook, $current_args); |
| 485 | | } |
| 486 | | } |
| 487 | | } |
| 488 | | } |
| 489 | | } |
| 490 | | } |
| 491 | | |
| 492 | | public function cron_flush_hook($h, $cron_data) |
| 493 | | { |
| 494 | | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| 495 | | |
| 496 | | $hook = (isset($cron_data['hook'])) ? $cron_data['hook'] : array(); |
| 497 | | $flush_count = 0; |
| 498 | | |
| 499 | | $current_crons = $this->_get_cron_array($h); |
| 500 | | foreach ($current_crons as $current_timestamp => $current_cronhooks) { |
| 501 | | foreach ($current_cronhooks as $current_hook => $current_keys) { |
| 502 | | if ($current_hook == $hook) { |
| 503 | | foreach ($current_keys as $current_md5 => $current_job) { |
| 504 | | $flush_count ++; |
| 505 | | //while ( list ($param, $value) = each ( $current_job )) |
| 506 | | $this->cron_unschedule_event($h, $current_timestamp, $current_hook, $current_job["args"]); |
| 507 | | } |
| 508 | | } |
| 509 | | } |
| 510 | | } |
| 511 | | $array = array('count' => $flush_count); |
| 512 | | echo json_encode($array); |
| 513 | | } |
| 514 | | |
| | 524 | } |
| | 525 | } |
| | 526 | |
| | 527 | public function cron_delete_job($h, $cron_data) |
| | 528 | { |
| | 529 | //load current cron jobs from memory space |
| | 530 | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| | 531 | |
| | 532 | $hook = (isset($cron_data['hook'])) ? $cron_data['hook'] : array(); |
| | 533 | $args = (isset($cron_data['args'])) ? $cron_data['args'] : array(); |
| | 534 | |
| | 535 | $current_crons = $this->_get_cron_array($h); |
| | 536 | |
| | 537 | foreach ($current_crons as $current_timestamp => $current_cronhooks) { |
| | 538 | foreach ($current_cronhooks as $current_hook => $current_keys) { |
| | 539 | foreach ($current_keys as $current_md5 => $current_job) { |
| | 540 | foreach ($current_job as $current_set => $current_args) { |
| | 541 | if ($current_hook == $hook && $current_args == $args) { |
| | 542 | $this->cron_unschedule_event($h, $current_timestamp, $current_hook, $current_args); |
| | 543 | } |
| | 544 | } |
| | 545 | } |
| | 546 | } |
| | 547 | } |
| | 548 | } |
| | 549 | |
| | 550 | public function cron_flush_hook($h, $cron_data) |
| | 551 | { |
| | 552 | $h->vars['cron_settings'] = $h->getSerializedSettings(); |
| | 553 | |
| | 554 | $hook = (isset($cron_data['hook'])) ? $cron_data['hook'] : array(); |
| | 555 | $flush_count = 0; |
| | 556 | |
| | 557 | $current_crons = $this->_get_cron_array($h); |
| | 558 | foreach ($current_crons as $current_timestamp => $current_cronhooks) { |
| | 559 | foreach ($current_cronhooks as $current_hook => $current_keys) { |
| | 560 | if ($current_hook == $hook) { |
| | 561 | foreach ($current_keys as $current_md5 => $current_job) { |
| | 562 | $flush_count++; |
| | 563 | //while ( list ($param, $value) = each ( $current_job )) |
| | 564 | $this->cron_unschedule_event($h, $current_timestamp, $current_hook, $current_job["args"]); |
| | 565 | } |
| | 566 | } |
| | 567 | } |
| | 568 | } |
| | 569 | $array = array('count' => $flush_count); |
| | 570 | echo json_encode($array); |
| | 571 | } |
| | 572 | |