Ticket #32656: with-return-codes.diff
File with-return-codes.diff, 12.5 KB (added by , 8 years ago) |
---|
-
wp-includes/cron.php
18 18 * @param int $timestamp Timestamp for when to run the event. 19 19 * @param string $hook Action hook to execute when cron is run. 20 20 * @param array $args Optional. Arguments to pass to the hook's callback function. 21 * @return false|void False when an event is notscheduled.21 * @return bool Whether or not the requested event has been scheduled. 22 22 */ 23 23 function wp_schedule_single_event( $timestamp, $hook, $args = array()) { 24 24 // Make sure timestamp is a positive integer … … 26 26 return false; 27 27 } 28 28 29 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args ); 30 31 /** 32 * Filter to preflight or hijack scheduling an event. 33 * 34 * Passing a non-null value will short-circuit adding the event to the cron 35 * array, returning the passed value instead. 36 * 37 * Both single events and recurring events are passed through this filter; 38 * single events have `$event->schedule` as false, whereas recurring events 39 * have this set to a recurrence from {@see wp_get_schedules}. Recurring 40 * events also have the integer recurrence interval set as `$event->interval` 41 * 42 * @param null|bool $pre Value to return instead. Default null to continue adding the event. 43 * @param object $event An object containing the event's data. 44 */ 45 $pre = apply_filters( 'pre_schedule_event', null, $event ); 46 if ( null !== $pre ) { 47 return $pre; 48 } 49 29 50 // Don't schedule a duplicate if there's already an identical event due within 10 minutes of it 30 51 $next = wp_next_scheduled($hook, $args); 31 52 if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) { … … 32 53 return false; 33 54 } 34 55 35 $crons = _get_cron_array();36 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );37 56 /** 38 57 * Filter a single event before it is scheduled. 39 58 * … … 49 68 50 69 $key = md5(serialize($event->args)); 51 70 71 $crons = _get_cron_array(); 52 72 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args ); 53 73 uksort( $crons, "strnatcasecmp" ); 54 _set_cron_array( $crons );74 return _set_cron_array( $crons ); 55 75 } 56 76 57 77 /** … … 72 92 * @param string $recurrence How often the event should recur. 73 93 * @param string $hook Action hook to execute when cron is run. 74 94 * @param array $args Optional. Arguments to pass to the hook's callback function. 75 * @return false|void False when an event is notscheduled.95 * @return bool Whether or not the requested event has been scheduled. 76 96 */ 77 97 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) { 78 98 // Make sure timestamp is a positive integer … … 80 100 return false; 81 101 } 82 102 83 $crons = _get_cron_array();84 103 $schedules = wp_get_schedules(); 85 104 86 105 if ( !isset( $schedules[$recurrence] ) ) … … 87 106 return false; 88 107 89 108 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); 109 90 110 /** This filter is documented in wp-includes/cron.php */ 111 $pre = apply_filters( 'pre_schedule_event', null, $event ); 112 if ( null !== $pre ) { 113 return $pre; 114 } 115 116 /** This filter is documented in wp-includes/cron.php */ 91 117 $event = apply_filters( 'schedule_event', $event ); 92 118 93 119 // A plugin disallowed this event … … 96 122 97 123 $key = md5(serialize($event->args)); 98 124 125 $crons = _get_cron_array(); 99 126 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval ); 100 127 uksort( $crons, "strnatcasecmp" ); 101 _set_cron_array( $crons );128 return _set_cron_array( $crons ); 102 129 } 103 130 104 131 /** … … 110 137 * @param string $recurrence How often the event should recur. 111 138 * @param string $hook Action hook to execute when cron is run. 112 139 * @param array $args Optional. Arguments to pass to the hook's callback function. 113 * @return false|void False when an event is notscheduled.140 * @return bool Whether or not the requested event has been rescheduled. 114 141 */ 115 142 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) { 116 143 // Make sure timestamp is a positive integer … … 118 145 return false; 119 146 } 120 147 148 /** 149 * Filter to preflight or hijack rescheduling of events. 150 * 151 * Passing a non-null value will short-circuit the normal rescheduling 152 * process, returning the passed value instead. 153 * 154 * Note that `$recurrence` is the original stored value. This is not 155 * guaranteed to exist when the event is rescheduled, so plugins should 156 * store the interval value and fall back to this if needed. 157 * 158 * @param null|bool $pre Value to return instead. Default null to continue rescheduling the event. 159 * @param int $timestamp Timestamp for when the event originally ran. 160 * @param string $recurrence How often the event should recur. 161 * @param string $hook Action hook to execute when cron is run. 162 * @param array $args Optional. Arguments to pass to the hook's callback function. 163 */ 164 $pre = apply_filters( 'pre_reschedule_event', null, $timestamp, $recurrence, $hook, $args ); 165 if ( $pre !== null ) { 166 return $pre; 167 } 168 121 169 $crons = _get_cron_array(); 122 170 $schedules = wp_get_schedules(); 123 171 $key = md5( serialize( $args ) ); … … 144 192 $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) ); 145 193 } 146 194 147 wp_schedule_event( $timestamp, $recurrence, $hook, $args );195 return wp_schedule_event( $timestamp, $recurrence, $hook, $args ); 148 196 } 149 197 150 198 /** … … 161 209 * Although not passed to a callback function, these arguments are used 162 210 * to uniquely identify the scheduled event, so they should be the same 163 211 * as those used when originally scheduling the event. 164 * @return false|void False when an event is not unscheduled.212 * @return bool Whether or not the requested event has been rescheduled. 165 213 */ 166 214 function wp_unschedule_event( $timestamp, $hook, $args = array() ) { 167 215 // Make sure timestamp is a positive integer … … 169 217 return false; 170 218 } 171 219 220 /** 221 * Filter to preflight or hijack unscheduling of events. 222 * 223 * Passing a non-null value will short-circuit the normal unscheduling 224 * process, returning the passed value instead. 225 * 226 * @param null|bool $pre Value to return instead. Default null to continue unscheduling the event. 227 * @param int $timestamp Timestamp for when to run the event. 228 * @param string $hook Action hook, the execution of which will be unscheduled. 229 * @param array $args Arguments to pass to the hook's callback function. 230 */ 231 $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args ); 232 if ( $pre !== null ) { 233 return $pre; 234 } 235 172 236 $crons = _get_cron_array(); 173 237 $key = md5(serialize($args)); 174 238 unset( $crons[$timestamp][$hook][$key] ); … … 176 240 unset( $crons[$timestamp][$hook] ); 177 241 if ( empty($crons[$timestamp]) ) 178 242 unset( $crons[$timestamp] ); 179 _set_cron_array( $crons );243 return _set_cron_array( $crons ); 180 244 } 181 245 182 246 /** … … 186 250 * 187 251 * @param string $hook Action hook, the execution of which will be unscheduled. 188 252 * @param array $args Optional. Arguments that were to be pass to the hook's callback function. 253 * @return array Boolean values, indicating the result of attempting to unschedule each indicated event, with timestamps as keys. 189 254 */ 190 255 function wp_clear_scheduled_hook( $hook, $args = array() ) { 191 256 // Backward compatibility … … 195 260 $args = array_slice( func_get_args(), 1 ); 196 261 } 197 262 263 /** 264 * Filter to preflight or hijack clearing a scheduled hook. 265 * 266 * Passing a non-null value will short-circuit the normal unscheduling 267 * process, returning the passed value instead. 268 * 269 * @param null|array $pre Value to return instead. Default null to continue unscheduling the event. 270 * @param string $hook Action hook, the execution of which will be unscheduled. 271 * @param array $args Arguments to pass to the hook's callback function. 272 */ 273 $pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args ); 274 if ( $pre !== null ) { 275 return $pre; 276 } 277 198 278 // This logic duplicates wp_next_scheduled() 199 279 // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing, 200 280 // and, wp_next_scheduled() returns the same schedule in an infinite loop. 201 281 $crons = _get_cron_array(); 202 282 if ( empty( $crons ) ) 203 return ;283 return array(); 204 284 285 $results = array(); 205 286 $key = md5( serialize( $args ) ); 206 287 foreach ( $crons as $timestamp => $cron ) { 207 288 if ( isset( $cron[ $hook ][ $key ] ) ) { 208 wp_unschedule_event( $timestamp, $hook, $args );289 $results[ $timestamp ] = wp_unschedule_event( $timestamp, $hook, $args ); 209 290 } 210 291 } 292 return $results; 211 293 } 212 294 213 295 /** … … 222 304 function wp_next_scheduled( $hook, $args = array() ) { 223 305 $crons = _get_cron_array(); 224 306 $key = md5(serialize($args)); 225 if ( empty($crons) ) 226 return false; 227 foreach ( $crons as $timestamp => $cron ) { 228 if ( isset( $cron[$hook][$key] ) ) 229 return $timestamp; 307 $next = false; 308 309 if ( ! empty($crons) ) { 310 foreach ( $crons as $timestamp => $cron ) { 311 if ( isset( $cron[$hook][$key] ) ) { 312 $next = $timestamp; 313 break; 314 } 315 } 230 316 } 231 return false; 317 318 /** 319 * Filter the next scheduled event timestamp. 320 * 321 * @param int|boolean The UNIX timestamp when the scheduled event will next occur, or false if not found. 322 * @param string $hook Action hook to execute when cron is run. 323 * @param array $args Arguments to be passed to the callback function. Used for deduplicating events. 324 */ 325 return apply_filters( 'next_scheduled', $next, $hook, $args ); 232 326 } 233 327 234 328 /** … … 237 331 * @since 2.1.0 238 332 * 239 333 * @param int $gmt_time Optional. Unix timestamp. Default 0 (current time is used). 334 * @return null|WP_Error|array Null when cron could not be spawned, because it is not needed to run. 335 * When cron runs, return the result of {@see wp_remote_post} 240 336 */ 241 337 function spawn_cron( $gmt_time = 0 ) { 242 338 if ( ! $gmt_time ) … … 326 422 ) 327 423 ), $doing_wp_cron ); 328 424 329 wp_remote_post( $cron_request['url'], $cron_request['args'] );425 return wp_remote_post( $cron_request['url'], $cron_request['args'] ); 330 426 } 331 427 332 428 /** … … 333 429 * Run scheduled callbacks or spawn cron for all scheduled events. 334 430 * 335 431 * @since 2.1.0 432 * @return array Array of spawn_cron() results for any cron jobs that were run, with cron timestamps as keys. 336 433 */ 337 434 function wp_cron() { 338 435 // Prevent infinite loops caused by lack of wp-cron.php 339 436 if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) 340 return ;437 return array(); 341 438 342 439 if ( false === $crons = _get_cron_array() ) 343 return ;440 return array(); 344 441 345 442 $gmt_time = microtime( true ); 346 443 $keys = array_keys( $crons ); 347 444 if ( isset($keys[0]) && $keys[0] > $gmt_time ) 348 return ;445 return array(); 349 446 447 $results = array(); 350 448 $schedules = wp_get_schedules(); 351 449 foreach ( $crons as $timestamp => $cronhooks ) { 352 450 if ( $timestamp > $gmt_time ) break; … … 353 451 foreach ( (array) $cronhooks as $hook => $args ) { 354 452 if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) 355 453 continue; 356 spawn_cron( $gmt_time );454 $results[$timestamp] = spawn_cron( $gmt_time ); 357 455 break 2; 358 456 } 359 457 } 458 return $results; 360 459 } 361 460 362 461 /** … … 417 516 function wp_get_schedule($hook, $args = array()) { 418 517 $crons = _get_cron_array(); 419 518 $key = md5(serialize($args)); 420 if ( empty($crons) ) 421 return false; 422 foreach ( $crons as $timestamp => $cron ) { 423 if ( isset( $cron[$hook][$key] ) ) 424 return $cron[$hook][$key]['schedule']; 519 $schedule = false; 520 if ( ! empty($crons) ) { 521 foreach ( $crons as $timestamp => $cron ) { 522 if ( isset( $cron[$hook][$key] ) ) { 523 $schedule = $cron[$hook][$key]['schedule']; 524 break; 525 } 526 } 425 527 } 426 return false; 528 529 /** 530 * Filter the schedule for a hook. 531 * 532 * @param string|boolean $schedule Schedule for the hook. False if not found. 533 * @param string $hook Action hook to execute when cron is run. 534 * @param array $args Optional. Arguments to pass to the hook's callback function. 535 */ 536 return apply_filters( 'get_schedule', $schedule, $hook, $args ); 427 537 } 428 538 429 539 // … … 458 568 * @access private 459 569 * 460 570 * @param array $cron Cron info array from {@link _get_cron_array()}. 571 * @return bool Whether the update of the cron option succeeded (according to {@see update_option}) 461 572 */ 462 573 function _set_cron_array($cron) { 463 574 $cron['version'] = 2; 464 update_option( 'cron', $cron );575 return update_option( 'cron', $cron ); 465 576 } 466 577 467 578 /**