Changeset 43050
- Timestamp:
- 05/01/2018 02:04:25 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/cron.php
r42343 r43050 22 22 * 23 23 * @since 2.1.0 24 * @since 5.0.0 Return value modified to boolean indicating success or failure. 25 * 24 26 * @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event 25 27 * … … 27 29 * @param string $hook Action hook to execute when the event is run. 28 30 * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. 29 * @return false|void False if the event did not get scheduled.31 * @return bool True if event successfully scheduled. False for failure. 30 32 */ 31 33 function wp_schedule_single_event( $timestamp, $hook, $args = array() ) { … … 78 80 ); 79 81 uksort( $crons, 'strnatcasecmp' ); 80 _set_cron_array( $crons );82 return _set_cron_array( $crons ); 81 83 } 82 84 … … 100 102 * 101 103 * @since 2.1.0 104 * @since 5.0.0 Return value modified to boolean indicating success or failure. 105 * 102 106 * @link https://codex.wordpress.org/Function_Reference/wp_schedule_event 103 107 * … … 106 110 * @param string $hook Action hook to execute when the event is run. 107 111 * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. 108 * @return false|void False if the event did not get scheduled.112 * @return bool True if event successfully scheduled. False for failure. 109 113 */ 110 114 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) { … … 144 148 ); 145 149 uksort( $crons, 'strnatcasecmp' ); 146 _set_cron_array( $crons );150 return _set_cron_array( $crons ); 147 151 } 148 152 … … 151 155 * 152 156 * @since 2.1.0 157 * @since 5.0.0 Return value modified to boolean indicating success or failure. 153 158 * 154 159 * @param int $timestamp Unix timestamp (UTC) for when to next run the event. … … 156 161 * @param string $hook Action hook to execute when the event is run. 157 162 * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. 158 * @return false|void False if the event did not get rescheduled.163 * @return bool True if event successfully rescheduled. False for failure. 159 164 */ 160 165 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) { … … 190 195 } 191 196 192 wp_schedule_event( $timestamp, $recurrence, $hook, $args );197 return wp_schedule_event( $timestamp, $recurrence, $hook, $args ); 193 198 } 194 199 … … 200 205 * 201 206 * @since 2.1.0 207 * @since 5.0.0 Return value modified to boolean indicating success or failure. 202 208 * 203 209 * @param int $timestamp Unix timestamp (UTC) of the event. … … 206 212 * Although not passed to a callback, these arguments are used to uniquely identify the 207 213 * event, so they should be the same as those used when originally scheduling the event. 208 * @return false|void False if the event did not get unscheduled.214 * @return bool True if event successfully unscheduled. False for failure. 209 215 */ 210 216 function wp_unschedule_event( $timestamp, $hook, $args = array() ) { … … 223 229 unset( $crons[ $timestamp ] ); 224 230 } 225 _set_cron_array( $crons );231 return _set_cron_array( $crons ); 226 232 } 227 233 … … 229 235 * Unschedules all events attached to the hook with the specified arguments. 230 236 * 231 * @since 2.1.0 237 * Warning: This function may return Boolean FALSE, but may also return a non-Boolean 238 * value which evaluates to FALSE. For information about casting to booleans see the 239 * {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use 240 * the `===` operator for testing the return value of this function. 241 * 242 * @since 2.1.0 243 * @since 5.0.0 Return value modified to indicate success or failure. 232 244 * 233 245 * @param string $hook Action hook, the execution of which will be unscheduled. 234 246 * @param array $args Optional. Arguments that were to be passed to the hook's callback function. 247 * @return bool|int On success an integer indicating number of events unscheduled (0 indicates no 248 * events were registered with the hook and arguments combination), false if 249 * unscheduling one or more events fail. 235 250 */ 236 251 function wp_clear_scheduled_hook( $hook, $args = array() ) { … … 247 262 $crons = _get_cron_array(); 248 263 if ( empty( $crons ) ) { 249 return; 250 } 251 252 $key = md5( serialize( $args ) ); 264 return 0; 265 } 266 267 $results = array(); 268 $key = md5( serialize( $args ) ); 253 269 foreach ( $crons as $timestamp => $cron ) { 254 270 if ( isset( $cron[ $hook ][ $key ] ) ) { 255 wp_unschedule_event( $timestamp, $hook, $args ); 256 } 257 } 271 $results[] = wp_unschedule_event( $timestamp, $hook, $args ); 272 } 273 } 274 if ( in_array( false, $results, true ) ) { 275 return false; 276 } 277 return count( $results ); 258 278 } 259 279 … … 263 283 * Can be useful for plugins when deactivating to clean up the cron queue. 264 284 * 285 * Warning: This function may return Boolean FALSE, but may also return a non-Boolean 286 * value which evaluates to FALSE. For information about casting to booleans see the 287 * {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use 288 * the `===` operator for testing the return value of this function. 289 * 265 290 * @since 4.9.0 291 * @since 5.0.0 Return value added to indicate success or failure. 266 292 * 267 293 * @param string $hook Action hook, the execution of which will be unscheduled. 294 * @return bool|int On success an integer indicating number of events unscheduled (0 indicates no 295 * events were registered on the hook), false if unscheduling fails. 268 296 */ 269 297 function wp_unschedule_hook( $hook ) { 270 298 $crons = _get_cron_array(); 271 299 if ( empty( $crons ) ) { 300 return 0; 301 } 302 303 $results = array(); 272 304 foreach ( $crons as $timestamp => $args ) { 305 if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) { 306 $results[] = count( $crons[ $timestamp ][ $hook ] ); 307 } 273 308 unset( $crons[ $timestamp ][ $hook ] ); 274 309 … … 278 313 } 279 314 280 _set_cron_array( $crons ); 315 /* 316 * If the results are empty (zero events to unschedule), no attempt 317 * to update the cron array is required. 318 */ 319 if ( empty( $results ) ) { 320 return 0; 321 } 322 if ( _set_cron_array( $crons ) ) { 323 return array_sum( $results ); 324 } 325 return false; 281 326 } 282 327 … … 310 355 * 311 356 * @since 2.1.0 357 * @since 5.0.0 Return values added. 312 358 * 313 359 * @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used). 360 * @return bool True if spawned, false if no events spawned. 314 361 */ 315 362 function spawn_cron( $gmt_time = 0 ) { … … 319 366 320 367 if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) { 321 return ;368 return false; 322 369 } 323 370 … … 337 384 // don't run if another process is currently running it or more than once every 60 sec. 338 385 if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) { 339 return ;386 return false; 340 387 } 341 388 … … 343 390 $crons = _get_cron_array(); 344 391 if ( ! is_array( $crons ) ) { 345 return ;392 return false; 346 393 } 347 394 348 395 $keys = array_keys( $crons ); 349 396 if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { 350 return ;397 return false; 351 398 } 352 399 353 400 if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { 354 401 if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { 355 return ;402 return false; 356 403 } 357 404 … … 369 416 370 417 WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' ); 371 return ;418 return true; 372 419 } 373 420 … … 410 457 ); 411 458 412 wp_remote_post( $cron_request['url'], $cron_request['args'] ); 459 $result = wp_remote_post( $cron_request['url'], $cron_request['args'] ); 460 return ! is_wp_error( $result ); 413 461 } 414 462 … … 416 464 * Run scheduled callbacks or spawn cron for all scheduled events. 417 465 * 418 * @since 2.1.0 466 * Warning: This function may return Boolean FALSE, but may also return a non-Boolean 467 * value which evaluates to FALSE. For information about casting to booleans see the 468 * {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use 469 * the `===` operator for testing the return value of this function. 470 * 471 * @since 2.1.0 472 * @since 5.0.0 Return value added to indicate success or failure. 473 * 474 * @return bool|int On success an integer indicating number of events spawned (0 indicates no 475 * events needed to be spawned), false if spawning fails for one or more events. 419 476 */ 420 477 function wp_cron() { 421 478 // Prevent infinite loops caused by lack of wp-cron.php 422 479 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; 480 return 0; 481 } 482 483 $crons = _get_cron_array(); 484 if ( false === $crons ) { 485 return 0; 428 486 } 429 487 … … 431 489 $keys = array_keys( $crons ); 432 490 if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { 433 return ;491 return 0; 434 492 } 435 493 436 494 $schedules = wp_get_schedules(); 495 $results = array(); 437 496 foreach ( $crons as $timestamp => $cronhooks ) { 438 497 if ( $timestamp > $gmt_time ) { … … 443 502 continue; 444 503 } 445 spawn_cron( $gmt_time );504 $results[] = spawn_cron( $gmt_time ); 446 505 break 2; 447 506 } 448 507 } 508 509 if ( in_array( false, $results, true ) ) { 510 return false; 511 } 512 return count( $results ); 449 513 } 450 514 … … 559 623 * 560 624 * @since 2.1.0 625 * @since 5.0.0 Return value modified to outcome of {@see update_option}. 626 * 561 627 * @access private 562 628 * 563 629 * @param array $cron Cron info array from _get_cron_array(). 630 * @return bool True if cron array updated, false on failure. 564 631 */ 565 632 function _set_cron_array( $cron ) { 566 633 $cron['version'] = 2; 567 update_option( 'cron', $cron );634 return update_option( 'cron', $cron ); 568 635 } 569 636 -
trunk/tests/phpunit/tests/cron.php
r42343 r43050 30 30 $timestamp = strtotime( '+1 hour' ); 31 31 32 wp_schedule_single_event( $timestamp, $hook ); 32 $scheduled = wp_schedule_single_event( $timestamp, $hook ); 33 $this->assertTrue( $scheduled ); 33 34 $this->assertEquals( $timestamp, wp_next_scheduled( $hook ) ); 34 35 … … 44 45 $args = array( 'foo' ); 45 46 46 wp_schedule_single_event( $timestamp, $hook, $args ); 47 $scheduled = wp_schedule_single_event( $timestamp, $hook, $args ); 48 $this->assertTrue( $scheduled ); 47 49 // this returns the timestamp only if we provide matching args 48 50 $this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) ); … … 61 63 $timestamp = strtotime( '+1 hour' ); 62 64 63 wp_schedule_event( $timestamp, $recur, $hook ); 65 $scheduled = wp_schedule_event( $timestamp, $recur, $hook ); 66 $this->assertTrue( $scheduled ); 64 67 // it's scheduled for the right time 65 68 $this->assertEquals( $timestamp, wp_next_scheduled( $hook ) ); … … 75 78 $args = array( 'foo' ); 76 79 77 wp_schedule_event( $timestamp, 'hourly', $hook, $args ); 80 $scheduled = wp_schedule_event( $timestamp, 'hourly', $hook, $args ); 81 $this->assertTrue( $scheduled ); 78 82 // this returns the timestamp only if we provide matching args 79 83 $this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) ); … … 95 99 96 100 // now unschedule it and make sure it's gone 97 wp_unschedule_event( $timestamp, $hook ); 101 $unscheduled = wp_unschedule_event( $timestamp, $hook ); 102 $this->assertTrue( $unscheduled ); 98 103 $this->assertEquals( false, wp_next_scheduled( $hook ) ); 99 104 } … … 114 119 115 120 // clear the schedule for the no args events and make sure it's gone 116 wp_clear_scheduled_hook( $hook ); 121 $hook_unscheduled = wp_clear_scheduled_hook( $hook ); 122 $this->assertSame( 2, $hook_unscheduled ); 117 123 $this->assertFalse( wp_next_scheduled( $hook ) ); 118 124 // the args events should still be there … … 123 129 wp_clear_scheduled_hook( $hook, $args ); 124 130 $this->assertFalse( wp_next_scheduled( $hook, $args ) ); 131 } 132 133 function test_clear_undefined_schedule() { 134 $hook = __FUNCTION__; 135 $args = array( 'arg1' ); 136 137 wp_schedule_single_event( strtotime( '+1 hour' ), $hook, $args ); 138 wp_schedule_single_event( strtotime( '+2 hour' ), $hook, $args ); 139 140 // clear the schedule for no args events and ensure no events are cleared. 141 $hook_unscheduled = wp_clear_scheduled_hook( $hook ); 142 $this->assertSame( 0, $hook_unscheduled ); 125 143 } 126 144 … … 207 225 208 226 // clear the schedule and make sure it's gone. 209 wp_unschedule_hook( $hook ); 227 $unschedule_hook = wp_unschedule_hook( $hook ); 228 $this->assertSame( 4, $unschedule_hook ); 229 $this->assertFalse( wp_next_scheduled( $hook ) ); 230 } 231 232 function test_unschedule_undefined_hook() { 233 $hook = __FUNCTION__; 234 $unrelated_hook = __FUNCTION__ . '_two'; 235 236 // Attempt to clear schedule on non-existant hook. 237 $unschedule_hook = wp_unschedule_hook( $hook ); 238 $this->assertSame( 0, $unschedule_hook ); 239 $this->assertFalse( wp_next_scheduled( $hook ) ); 240 241 // Repeat tests with populated cron array. 242 wp_schedule_single_event( strtotime( '+1 hour' ), $unrelated_hook ); 243 wp_schedule_single_event( strtotime( '+2 hour' ), $unrelated_hook ); 244 245 $unschedule_hook = wp_unschedule_hook( $hook ); 246 $this->assertSame( 0, $unschedule_hook ); 210 247 $this->assertFalse( wp_next_scheduled( $hook ) ); 211 248 } … … 222 259 223 260 // first one works 224 wp_schedule_single_event( $ts1, $hook, $args);261 $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) ); 225 262 // second one is ignored 226 wp_schedule_single_event( $ts2, $hook, $args);263 $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) ); 227 264 228 265 // the next event should be at +5 minutes, not +3 … … 241 278 242 279 // first one works 243 wp_schedule_single_event( $ts1, $hook, $args);280 $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) ); 244 281 // second works too 245 wp_schedule_single_event( $ts2, $hook, $args);282 $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) ); 246 283 247 284 // the next event should be at +3 minutes, even though that one was scheduled second … … 260 297 261 298 // first one works 262 wp_schedule_single_event( $ts1, $hook, $args);299 $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) ); 263 300 // second works too 264 wp_schedule_single_event( $ts2, $hook, $args);301 $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) ); 265 302 266 303 // the next event should be at +3 minutes
Note: See TracChangeset
for help on using the changeset viewer.