WordPress.org

Make WordPress Core

Ticket #21072: 0001-Refresh-of-evansolomon-s-patch-for-issue-20172.patch

File 0001-Refresh-of-evansolomon-s-patch-for-issue-20172.patch, 9.6 KB (added by jrf, 4 years ago)

Refresh of the earlier patch.

  • src/wp-includes/cron.php

    From 85ef68165ec7044ecd04d00e40e9df6e3ddc430c Mon Sep 17 00:00:00 2001
    From: jrfnl <github_nospam@adviesenzo.nl>
    Date: Tue, 8 Dec 2015 11:23:04 +0100
    Subject: [PATCH] Refresh of @evansolomon's patch for issue #20172.
    
    Boyscouting: get rid of inline control structures in touched functions.
    ---
     src/wp-includes/cron.php | 84 ++++++++++++++++++++++++++++++++----------------
     1 file changed, 56 insertions(+), 28 deletions(-)
    
    diff --git a/src/wp-includes/cron.php b/src/wp-includes/cron.php
    index 60492c5..c8852f9 100644
    a b  
    1818 * @param int $timestamp Timestamp for when to run the event.
    1919 * @param string $hook Action hook to execute when cron is run.
    2020 * @param array $args Optional. Arguments to pass to the hook's callback function.
    21  * @return false|void False when an event is not scheduled.
     21 * @return bool False on failure, true if the event has been scheduled.
    2222 */
    2323function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
    2424        // Make sure timestamp is a positive integer
    function wp_schedule_single_event( $timestamp, $hook, $args = array()) { 
    4444        $event = apply_filters( 'schedule_event', $event );
    4545
    4646        // A plugin disallowed this event
    47         if ( ! $event )
     47        if ( ! $event ) {
    4848                return false;
     49        }
    4950
    5051        $key = md5(serialize($event->args));
    5152
    5253        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
    5354        uksort( $crons, "strnatcasecmp" );
    54         _set_cron_array( $crons );
     55        return _set_cron_array( $crons );
    5556}
    5657
    5758/**
    function wp_schedule_single_event( $timestamp, $hook, $args = array()) { 
    7273 * @param string $recurrence How often the event should recur.
    7374 * @param string $hook Action hook to execute when cron is run.
    7475 * @param array $args Optional. Arguments to pass to the hook's callback function.
    75  * @return false|void False when an event is not scheduled.
     76 * @return bool False on failure, true if the event has been scheduled.
    7677 */
    7778function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
    7879        // Make sure timestamp is a positive integer
    function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) { 
    8384        $crons = _get_cron_array();
    8485        $schedules = wp_get_schedules();
    8586
    86         if ( !isset( $schedules[$recurrence] ) )
     87        if ( !isset( $schedules[$recurrence] ) ) {
    8788                return false;
     89        }
    8890
    8991        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
    9092        /** This filter is documented in wp-includes/cron.php */
    9193        $event = apply_filters( 'schedule_event', $event );
    9294
    9395        // A plugin disallowed this event
    94         if ( ! $event )
     96        if ( ! $event ) {
    9597                return false;
     98        }
    9699
    97100        $key = md5(serialize($event->args));
    98101
    99102        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
    100103        uksort( $crons, "strnatcasecmp" );
    101         _set_cron_array( $crons );
     104        return _set_cron_array( $crons );
    102105}
    103106
    104107/**
    function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) { 
    110113 * @param string $recurrence How often the event should recur.
    111114 * @param string $hook Action hook to execute when cron is run.
    112115 * @param array $args Optional. Arguments to pass to the hook's callback function.
    113  * @return false|void False when an event is not scheduled.
     116 * @return bool False on failure, true if the event has been rescheduled.
    114117 */
    115118function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
    116119        // Make sure timestamp is a positive integer
    function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) 
    144147                $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
    145148        }
    146149
    147         wp_schedule_event( $timestamp, $recurrence, $hook, $args );
     150        return wp_schedule_event( $timestamp, $recurrence, $hook, $args );
    148151}
    149152
    150153/**
    function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) 
    161164 * Although not passed to a callback function, these arguments are used
    162165 * to uniquely identify the scheduled event, so they should be the same
    163166 * as those used when originally scheduling the event.
    164  * @return false|void False when an event is not unscheduled.
     167 * @return bool False on failure, true if the event has been unscheduled.
    165168 */
    166169function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
    167170        // Make sure timestamp is a positive integer
    function wp_unschedule_event( $timestamp, $hook, $args = array() ) { 
    172175        $crons = _get_cron_array();
    173176        $key = md5(serialize($args));
    174177        unset( $crons[$timestamp][$hook][$key] );
    175         if ( empty($crons[$timestamp][$hook]) )
     178        if ( empty($crons[$timestamp][$hook]) ) {
    176179                unset( $crons[$timestamp][$hook] );
    177         if ( empty($crons[$timestamp]) )
     180        }
     181        if ( empty($crons[$timestamp]) ) {
    178182                unset( $crons[$timestamp] );
    179         _set_cron_array( $crons );
     183        }
     184        return _set_cron_array( $crons );
    180185}
    181186
    182187/**
    function wp_unschedule_event( $timestamp, $hook, $args = array() ) { 
    186191 *
    187192 * @param string $hook Action hook, the execution of which will be unscheduled.
    188193 * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
     194 * @return array Boolean values, for each unscheduled event with timestamps as keys.
    189195 */
    190196function wp_clear_scheduled_hook( $hook, $args = array() ) {
    191197        // Backward compatibility
    function wp_clear_scheduled_hook( $hook, $args = array() ) { 
    199205        // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
    200206        // and, wp_next_scheduled() returns the same schedule in an infinite loop.
    201207        $crons = _get_cron_array();
    202         if ( empty( $crons ) )
     208        if ( empty( $crons ) ) {
    203209                return;
     210        }
    204211
     212        $results = array();
    205213        $key = md5( serialize( $args ) );
    206214        foreach ( $crons as $timestamp => $cron ) {
    207215                if ( isset( $cron[ $hook ][ $key ] ) ) {
    208                         wp_unschedule_event( $timestamp, $hook, $args );
     216                        $results[ $timestamp ] = wp_unschedule_event( $timestamp, $hook, $args );
    209217                }
    210218        }
     219        return $results;
    211220}
    212221
    213222/**
    function wp_next_scheduled( $hook, $args = array() ) { 
    235244 * Send request to run cron through HTTP request that doesn't halt page loading.
    236245 *
    237246 * @since 2.1.0
     247 *
     248 * @return null|WP_Error|array Null when cron could not be spawned, because it is not needed to run.
     249 * When cron runs, return the result of wp_remote_post().
    238250 */
    239251function spawn_cron( $gmt_time = 0 ) {
    240         if ( ! $gmt_time )
     252        if ( ! $gmt_time ) {
    241253                $gmt_time = microtime( true );
     254        }
    242255
    243         if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
     256        if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) {
    244257                return;
     258        }
    245259
    246260        /*
    247261         * Get the cron lock, which is a unix timestamp of when the last cron was spawned
    function spawn_cron( $gmt_time = 0 ) { 
    252266         */
    253267        $lock = get_transient('doing_cron');
    254268
    255         if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
     269        if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) {
    256270                $lock = 0;
     271        }
    257272
    258273        // don't run if another process is currently running it or more than once every 60 sec.
    259         if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
     274        if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
    260275                return;
     276        }
    261277
    262278        //sanity check
    263279        $crons = _get_cron_array();
    264         if ( !is_array($crons) )
     280        if ( !is_array($crons) ) {
    265281                return;
     282        }
    266283
    267284        $keys = array_keys( $crons );
    268         if ( isset($keys[0]) && $keys[0] > $gmt_time )
     285        if ( isset($keys[0]) && $keys[0] > $gmt_time ) {
    269286                return;
     287        }
    270288
    271289        if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
    272290                if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) ||  defined( 'XMLRPC_REQUEST' ) ) {
    function spawn_cron( $gmt_time = 0 ) { 
    322340                )
    323341        ) );
    324342
    325         wp_remote_post( $cron_request['url'], $cron_request['args'] );
     343        return wp_remote_post( $cron_request['url'], $cron_request['args'] );
    326344}
    327345
    328346/**
    329347 * Run scheduled callbacks or spawn cron for all scheduled events.
    330348 *
    331349 * @since 2.1.0
     350 *
     351 * @return null|array Null when doesn't need to run Cron. Array of spawn_cron() results when cron does run.
    332352 */
    333353function wp_cron() {
    334354        // Prevent infinite loops caused by lack of wp-cron.php
    335         if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
     355        if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) {
    336356                return;
     357        }
    337358
    338         if ( false === $crons = _get_cron_array() )
     359        if ( false === $crons = _get_cron_array() ) {
    339360                return;
     361        }
    340362
    341363        $gmt_time = microtime( true );
    342364        $keys = array_keys( $crons );
    343         if ( isset($keys[0]) && $keys[0] > $gmt_time )
     365        if ( isset($keys[0]) && $keys[0] > $gmt_time ) {
    344366                return;
     367        }
    345368
     369        $results = array();
    346370        $schedules = wp_get_schedules();
    347371        foreach ( $crons as $timestamp => $cronhooks ) {
    348372                if ( $timestamp > $gmt_time ) break;
    349373                foreach ( (array) $cronhooks as $hook => $args ) {
    350                         if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
     374                        if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) {
    351375                                continue;
    352                         spawn_cron( $gmt_time );
     376                        }
     377                        $results[] = spawn_cron( $gmt_time );
    353378                        break 2;
    354379                }
    355380        }
     381        return $results;
    356382}
    357383
    358384/**
    function _get_cron_array() { 
    454480 * @access private
    455481 *
    456482 * @param array $cron Cron info array from {@link _get_cron_array()}.
     483 *
     484 * @return bool Whether the update of the cron option succeeded.
    457485 */
    458486function _set_cron_array($cron) {
    459487        $cron['version'] = 2;
    460         update_option( 'cron', $cron );
     488        return update_option( 'cron', $cron );
    461489}
    462490
    463491/**