Changeset 42343 for trunk/src/wp-includes/cron.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/cron.php
r42216 r42343 29 29 * @return false|void False if the event did not get scheduled. 30 30 */ 31 function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {31 function wp_schedule_single_event( $timestamp, $hook, $args = array() ) { 32 32 // Make sure timestamp is a positive integer 33 33 if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { … … 36 36 37 37 // Don't schedule a duplicate if there's already an identical event due within 10 minutes of it 38 $next = wp_next_scheduled( $hook, $args);38 $next = wp_next_scheduled( $hook, $args ); 39 39 if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) { 40 40 return false; … … 42 42 43 43 $crons = _get_cron_array(); 44 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args ); 44 $event = (object) array( 45 'hook' => $hook, 46 'timestamp' => $timestamp, 47 'schedule' => false, 48 'args' => $args, 49 ); 45 50 46 51 /** … … 62 67 63 68 // A plugin disallowed this event 64 if ( ! $event ) 65 return false; 66 67 $key = md5(serialize($event->args)); 68 69 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args ); 70 uksort( $crons, "strnatcasecmp" ); 69 if ( ! $event ) { 70 return false; 71 } 72 73 $key = md5( serialize( $event->args ) ); 74 75 $crons[ $event->timestamp ][ $event->hook ][ $key ] = array( 76 'schedule' => $event->schedule, 77 'args' => $event->args, 78 ); 79 uksort( $crons, 'strnatcasecmp' ); 71 80 _set_cron_array( $crons ); 72 81 } … … 99 108 * @return false|void False if the event did not get scheduled. 100 109 */ 101 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {110 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) { 102 111 // Make sure timestamp is a positive integer 103 112 if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { … … 105 114 } 106 115 107 $crons = _get_cron_array();116 $crons = _get_cron_array(); 108 117 $schedules = wp_get_schedules(); 109 118 110 if ( !isset( $schedules[$recurrence] ) ) 111 return false; 112 113 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); 119 if ( ! isset( $schedules[ $recurrence ] ) ) { 120 return false; 121 } 122 123 $event = (object) array( 124 'hook' => $hook, 125 'timestamp' => $timestamp, 126 'schedule' => $recurrence, 127 'args' => $args, 128 'interval' => $schedules[ $recurrence ]['interval'], 129 ); 114 130 /** This filter is documented in wp-includes/cron.php */ 115 131 $event = apply_filters( 'schedule_event', $event ); 116 132 117 133 // A plugin disallowed this event 118 if ( ! $event ) 119 return false; 120 121 $key = md5(serialize($event->args)); 122 123 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval ); 124 uksort( $crons, "strnatcasecmp" ); 134 if ( ! $event ) { 135 return false; 136 } 137 138 $key = md5( serialize( $event->args ) ); 139 140 $crons[ $event->timestamp ][ $event->hook ][ $key ] = array( 141 'schedule' => $event->schedule, 142 'args' => $event->args, 143 'interval' => $event->interval, 144 ); 145 uksort( $crons, 'strnatcasecmp' ); 125 146 _set_cron_array( $crons ); 126 147 } … … 143 164 } 144 165 145 $crons = _get_cron_array();166 $crons = _get_cron_array(); 146 167 $schedules = wp_get_schedules(); 147 $key = md5( serialize( $args ) );148 $interval = 0;168 $key = md5( serialize( $args ) ); 169 $interval = 0; 149 170 150 171 // First we try to get it from the schedule … … 194 215 195 216 $crons = _get_cron_array(); 196 $key = md5(serialize($args)); 197 unset( $crons[$timestamp][$hook][$key] ); 198 if ( empty($crons[$timestamp][$hook]) ) 199 unset( $crons[$timestamp][$hook] ); 200 if ( empty($crons[$timestamp]) ) 201 unset( $crons[$timestamp] ); 217 $key = md5( serialize( $args ) ); 218 unset( $crons[ $timestamp ][ $hook ][ $key ] ); 219 if ( empty( $crons[ $timestamp ][ $hook ] ) ) { 220 unset( $crons[ $timestamp ][ $hook ] ); 221 } 222 if ( empty( $crons[ $timestamp ] ) ) { 223 unset( $crons[ $timestamp ] ); 224 } 202 225 _set_cron_array( $crons ); 203 226 } … … 214 237 // Backward compatibility 215 238 // Previously this function took the arguments as discrete vars rather than an array like the rest of the API 216 if ( ! is_array($args) ) {217 _deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.') );239 if ( ! is_array( $args ) ) { 240 _deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) ); 218 241 $args = array_slice( func_get_args(), 1 ); 219 242 } … … 223 246 // and, wp_next_scheduled() returns the same schedule in an infinite loop. 224 247 $crons = _get_cron_array(); 225 if ( empty( $crons ) ) 226 return; 248 if ( empty( $crons ) ) { 249 return; 250 } 227 251 228 252 $key = md5( serialize( $args ) ); … … 246 270 $crons = _get_cron_array(); 247 271 248 foreach ( $crons as $timestamp => $args ) {272 foreach ( $crons as $timestamp => $args ) { 249 273 unset( $crons[ $timestamp ][ $hook ] ); 250 274 … … 270 294 function wp_next_scheduled( $hook, $args = array() ) { 271 295 $crons = _get_cron_array(); 272 $key = md5(serialize($args)); 273 if ( empty($crons) ) 274 return false; 296 $key = md5( serialize( $args ) ); 297 if ( empty( $crons ) ) { 298 return false; 299 } 275 300 foreach ( $crons as $timestamp => $cron ) { 276 if ( isset( $cron[ $hook][$key] ) )301 if ( isset( $cron[ $hook ][ $key ] ) ) { 277 302 return $timestamp; 303 } 278 304 } 279 305 return false; … … 288 314 */ 289 315 function spawn_cron( $gmt_time = 0 ) { 290 if ( ! $gmt_time ) 316 if ( ! $gmt_time ) { 291 317 $gmt_time = microtime( true ); 292 293 if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) 294 return; 318 } 319 320 if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) { 321 return; 322 } 295 323 296 324 /* … … 301 329 * this lock attempts to make spawning as atomic as possible. 302 330 */ 303 $lock = get_transient( 'doing_cron');304 305 if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) 331 $lock = get_transient( 'doing_cron' ); 332 333 if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) { 306 334 $lock = 0; 335 } 307 336 308 337 // don't run if another process is currently running it or more than once every 60 sec. 309 if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) 310 return; 338 if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) { 339 return; 340 } 311 341 312 342 //sanity check 313 343 $crons = _get_cron_array(); 314 if ( !is_array($crons) ) 315 return; 344 if ( ! is_array( $crons ) ) { 345 return; 346 } 316 347 317 348 $keys = array_keys( $crons ); 318 if ( isset($keys[0]) && $keys[0] > $gmt_time ) 319 return; 349 if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { 350 return; 351 } 320 352 321 353 if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { 322 if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || 354 if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { 323 355 return; 324 356 } … … 332 364 333 365 // flush any buffers and send the headers 334 while ( @ob_end_flush() ); 366 while ( @ob_end_flush() ) { 367 } 335 368 flush(); 336 369 … … 364 397 * @param string $doing_wp_cron The unix timestamp of the cron lock. 365 398 */ 366 $cron_request = apply_filters( 'cron_request', array( 367 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ), 368 'key' => $doing_wp_cron, 369 'args' => array( 370 'timeout' => 0.01, 371 'blocking' => false, 372 /** This filter is documented in wp-includes/class-wp-http-streams.php */ 373 'sslverify' => apply_filters( 'https_local_ssl_verify', false ) 374 ) 375 ), $doing_wp_cron ); 399 $cron_request = apply_filters( 400 'cron_request', array( 401 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ), 402 'key' => $doing_wp_cron, 403 'args' => array( 404 'timeout' => 0.01, 405 'blocking' => false, 406 /** This filter is documented in wp-includes/class-wp-http-streams.php */ 407 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), 408 ), 409 ), $doing_wp_cron 410 ); 376 411 377 412 wp_remote_post( $cron_request['url'], $cron_request['args'] ); … … 385 420 function wp_cron() { 386 421 // Prevent infinite loops caused by lack of wp-cron.php 387 if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) 388 return; 389 390 if ( false === $crons = _get_cron_array() ) 391 return; 422 if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) { 423 return; 424 } 425 426 if ( false === $crons = _get_cron_array() ) { 427 return; 428 } 392 429 393 430 $gmt_time = microtime( true ); 394 $keys = array_keys( $crons ); 395 if ( isset($keys[0]) && $keys[0] > $gmt_time ) 396 return; 431 $keys = array_keys( $crons ); 432 if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { 433 return; 434 } 397 435 398 436 $schedules = wp_get_schedules(); 399 437 foreach ( $crons as $timestamp => $cronhooks ) { 400 if ( $timestamp > $gmt_time ) break; 438 if ( $timestamp > $gmt_time ) { 439 break; 440 } 401 441 foreach ( (array) $cronhooks as $hook => $args ) { 402 if ( isset( $schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )442 if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) { 403 443 continue; 444 } 404 445 spawn_cron( $gmt_time ); 405 446 break 2; … … 430 471 * $array['weekly'] = array( 431 472 * 'interval' => 604800, 432 * 473 * 'display' => __( 'Once Weekly' ) 433 474 * ); 434 *435 475 * 436 476 * @since 2.1.0 … … 440 480 function wp_get_schedules() { 441 481 $schedules = array( 442 'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ), 443 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ), 444 'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ), 482 'hourly' => array( 483 'interval' => HOUR_IN_SECONDS, 484 'display' => __( 'Once Hourly' ), 485 ), 486 'twicedaily' => array( 487 'interval' => 12 * HOUR_IN_SECONDS, 488 'display' => __( 'Twice Daily' ), 489 ), 490 'daily' => array( 491 'interval' => DAY_IN_SECONDS, 492 'display' => __( 'Once Daily' ), 493 ), 445 494 ); 446 495 /** … … 465 514 * @return string|false False, if no schedule. Schedule name on success. 466 515 */ 467 function wp_get_schedule($hook, $args = array()) { 468 $crons = _get_cron_array(); 469 $key = md5(serialize($args)); 470 if ( empty($crons) ) 471 return false; 516 function wp_get_schedule( $hook, $args = array() ) { 517 $crons = _get_cron_array(); 518 $key = md5( serialize( $args ) ); 519 if ( empty( $crons ) ) { 520 return false; 521 } 472 522 foreach ( $crons as $timestamp => $cron ) { 473 if ( isset( $cron[$hook][$key] ) ) 474 return $cron[$hook][$key]['schedule']; 523 if ( isset( $cron[ $hook ][ $key ] ) ) { 524 return $cron[ $hook ][ $key ]['schedule']; 525 } 475 526 } 476 527 return false; … … 489 540 * @return false|array CRON info array. 490 541 */ 491 function _get_cron_array() { 492 $cron = get_option('cron'); 493 if ( ! is_array($cron) ) 494 return false; 495 496 if ( !isset($cron['version']) ) 497 $cron = _upgrade_cron_array($cron); 498 499 unset($cron['version']); 542 function _get_cron_array() { 543 $cron = get_option( 'cron' ); 544 if ( ! is_array( $cron ) ) { 545 return false; 546 } 547 548 if ( ! isset( $cron['version'] ) ) { 549 $cron = _upgrade_cron_array( $cron ); 550 } 551 552 unset( $cron['version'] ); 500 553 501 554 return $cron; … … 510 563 * @param array $cron Cron info array from _get_cron_array(). 511 564 */ 512 function _set_cron_array( $cron) {565 function _set_cron_array( $cron ) { 513 566 $cron['version'] = 2; 514 567 update_option( 'cron', $cron ); … … 526 579 * @return array An upgraded Cron info array. 527 580 */ 528 function _upgrade_cron_array( $cron) {529 if ( isset( $cron['version']) && 2 == $cron['version'])581 function _upgrade_cron_array( $cron ) { 582 if ( isset( $cron['version'] ) && 2 == $cron['version'] ) { 530 583 return $cron; 584 } 531 585 532 586 $new_cron = array(); 533 587 534 foreach ( (array) $cron as $timestamp => $hooks ) {588 foreach ( (array) $cron as $timestamp => $hooks ) { 535 589 foreach ( (array) $hooks as $hook => $args ) { 536 $key = md5(serialize($args['args']));537 $new_cron[ $timestamp][$hook][$key] = $args;590 $key = md5( serialize( $args['args'] ) ); 591 $new_cron[ $timestamp ][ $hook ][ $key ] = $args; 538 592 } 539 593 }
Note: See TracChangeset
for help on using the changeset viewer.