Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/cron.php

    r42216 r42343  
    2929 * @return false|void False if the event did not get scheduled.
    3030 */
    31 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
     31function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
    3232        // Make sure timestamp is a positive integer
    3333        if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
     
    3636
    3737        // 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 );
    3939        if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
    4040                return false;
     
    4242
    4343        $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        );
    4550
    4651        /**
     
    6267
    6368        // 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' );
    7180        _set_cron_array( $crons );
    7281}
     
    99108 * @return false|void False if the event did not get scheduled.
    100109 */
    101 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
     110function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
    102111        // Make sure timestamp is a positive integer
    103112        if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
     
    105114        }
    106115
    107         $crons = _get_cron_array();
     116        $crons     = _get_cron_array();
    108117        $schedules = wp_get_schedules();
    109118
    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        );
    114130        /** This filter is documented in wp-includes/cron.php */
    115131        $event = apply_filters( 'schedule_event', $event );
    116132
    117133        // 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' );
    125146        _set_cron_array( $crons );
    126147}
     
    143164        }
    144165
    145         $crons = _get_cron_array();
     166        $crons     = _get_cron_array();
    146167        $schedules = wp_get_schedules();
    147         $key = md5( serialize( $args ) );
    148         $interval = 0;
     168        $key       = md5( serialize( $args ) );
     169        $interval  = 0;
    149170
    150171        // First we try to get it from the schedule
     
    194215
    195216        $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        }
    202225        _set_cron_array( $crons );
    203226}
     
    214237        // Backward compatibility
    215238        // 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.' ) );
    218241                $args = array_slice( func_get_args(), 1 );
    219242        }
     
    223246        // and, wp_next_scheduled() returns the same schedule in an infinite loop.
    224247        $crons = _get_cron_array();
    225         if ( empty( $crons ) )
    226                 return;
     248        if ( empty( $crons ) ) {
     249                return;
     250        }
    227251
    228252        $key = md5( serialize( $args ) );
     
    246270        $crons = _get_cron_array();
    247271
    248         foreach( $crons as $timestamp => $args ) {
     272        foreach ( $crons as $timestamp => $args ) {
    249273                unset( $crons[ $timestamp ][ $hook ] );
    250274
     
    270294function wp_next_scheduled( $hook, $args = array() ) {
    271295        $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        }
    275300        foreach ( $crons as $timestamp => $cron ) {
    276                 if ( isset( $cron[$hook][$key] ) )
     301                if ( isset( $cron[ $hook ][ $key ] ) ) {
    277302                        return $timestamp;
     303                }
    278304        }
    279305        return false;
     
    288314 */
    289315function spawn_cron( $gmt_time = 0 ) {
    290         if ( ! $gmt_time )
     316        if ( ! $gmt_time ) {
    291317                $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        }
    295323
    296324        /*
     
    301329         * this lock attempts to make spawning as atomic as possible.
    302330         */
    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 ) {
    306334                $lock = 0;
     335        }
    307336
    308337        // 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        }
    311341
    312342        //sanity check
    313343        $crons = _get_cron_array();
    314         if ( !is_array($crons) )
    315                 return;
     344        if ( ! is_array( $crons ) ) {
     345                return;
     346        }
    316347
    317348        $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        }
    320352
    321353        if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
    322                 if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) ||  defined( 'XMLRPC_REQUEST' ) ) {
     354                if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
    323355                        return;
    324356                }
     
    332364
    333365                // flush any buffers and send the headers
    334                 while ( @ob_end_flush() );
     366                while ( @ob_end_flush() ) {
     367                }
    335368                flush();
    336369
     
    364397         * @param string $doing_wp_cron The unix timestamp of the cron lock.
    365398         */
    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        );
    376411
    377412        wp_remote_post( $cron_request['url'], $cron_request['args'] );
     
    385420function wp_cron() {
    386421        // 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        }
    392429
    393430        $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        }
    397435
    398436        $schedules = wp_get_schedules();
    399437        foreach ( $crons as $timestamp => $cronhooks ) {
    400                 if ( $timestamp > $gmt_time ) break;
     438                if ( $timestamp > $gmt_time ) {
     439                        break;
     440                }
    401441                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'] ) ) {
    403443                                continue;
     444                        }
    404445                        spawn_cron( $gmt_time );
    405446                        break 2;
     
    430471 *     $array['weekly'] = array(
    431472 *         'interval' => 604800,
    432  *         'display'  => __( 'Once Weekly' )
     473 *         'display'  => __( 'Once Weekly' )
    433474 *     );
    434  *
    435475 *
    436476 * @since 2.1.0
     
    440480function wp_get_schedules() {
    441481        $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                ),
    445494        );
    446495        /**
     
    465514 * @return string|false False, if no schedule. Schedule name on success.
    466515 */
    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;
     516function wp_get_schedule( $hook, $args = array() ) {
     517        $crons = _get_cron_array();
     518        $key   = md5( serialize( $args ) );
     519        if ( empty( $crons ) ) {
     520                return false;
     521        }
    472522        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                }
    475526        }
    476527        return false;
     
    489540 * @return false|array CRON info array.
    490541 */
    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']);
     542function _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'] );
    500553
    501554        return $cron;
     
    510563 * @param array $cron Cron info array from _get_cron_array().
    511564 */
    512 function _set_cron_array($cron) {
     565function _set_cron_array( $cron ) {
    513566        $cron['version'] = 2;
    514567        update_option( 'cron', $cron );
     
    526579 * @return array An upgraded Cron info array.
    527580 */
    528 function _upgrade_cron_array($cron) {
    529         if ( isset($cron['version']) && 2 == $cron['version'])
     581function _upgrade_cron_array( $cron ) {
     582        if ( isset( $cron['version'] ) && 2 == $cron['version'] ) {
    530583                return $cron;
     584        }
    531585
    532586        $new_cron = array();
    533587
    534         foreach ( (array) $cron as $timestamp => $hooks) {
     588        foreach ( (array) $cron as $timestamp => $hooks ) {
    535589                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;
    538592                }
    539593        }
Note: See TracChangeset for help on using the changeset viewer.