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.


Built from https://develop.svn.wordpress.org/trunk@42343


git-svn-id: http://core.svn.wordpress.org/trunk@42172 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast
2017-11-30 23:11:00 +00:00
parent 62be7ad218
commit aaf99e6913
610 changed files with 55731 additions and 43711 deletions

View File

@@ -28,20 +28,25 @@
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
* @return false|void False if the event did not get scheduled.
*/
function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
// Make sure timestamp is a positive integer
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
return false;
}
// Don't schedule a duplicate if there's already an identical event due within 10 minutes of it
$next = wp_next_scheduled($hook, $args);
$next = wp_next_scheduled( $hook, $args );
if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
return false;
}
$crons = _get_cron_array();
$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
$event = (object) array(
'hook' => $hook,
'timestamp' => $timestamp,
'schedule' => false,
'args' => $args,
);
/**
* Filters a single event before it is scheduled.
@@ -61,13 +66,17 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
$event = apply_filters( 'schedule_event', $event );
// A plugin disallowed this event
if ( ! $event )
if ( ! $event ) {
return false;
}
$key = md5(serialize($event->args));
$key = md5( serialize( $event->args ) );
$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
uksort( $crons, "strnatcasecmp" );
$crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
'schedule' => $event->schedule,
'args' => $event->args,
);
uksort( $crons, 'strnatcasecmp' );
_set_cron_array( $crons );
}
@@ -98,30 +107,42 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
* @return false|void False if the event did not get scheduled.
*/
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
// Make sure timestamp is a positive integer
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
return false;
}
$crons = _get_cron_array();
$crons = _get_cron_array();
$schedules = wp_get_schedules();
if ( !isset( $schedules[$recurrence] ) )
if ( ! isset( $schedules[ $recurrence ] ) ) {
return false;
}
$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
$event = (object) array(
'hook' => $hook,
'timestamp' => $timestamp,
'schedule' => $recurrence,
'args' => $args,
'interval' => $schedules[ $recurrence ]['interval'],
);
/** This filter is documented in wp-includes/cron.php */
$event = apply_filters( 'schedule_event', $event );
// A plugin disallowed this event
if ( ! $event )
if ( ! $event ) {
return false;
}
$key = md5(serialize($event->args));
$key = md5( serialize( $event->args ) );
$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
uksort( $crons, "strnatcasecmp" );
$crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
'schedule' => $event->schedule,
'args' => $event->args,
'interval' => $event->interval,
);
uksort( $crons, 'strnatcasecmp' );
_set_cron_array( $crons );
}
@@ -142,10 +163,10 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
return false;
}
$crons = _get_cron_array();
$crons = _get_cron_array();
$schedules = wp_get_schedules();
$key = md5( serialize( $args ) );
$interval = 0;
$key = md5( serialize( $args ) );
$interval = 0;
// First we try to get it from the schedule
if ( isset( $schedules[ $recurrence ] ) ) {
@@ -193,12 +214,14 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
}
$crons = _get_cron_array();
$key = md5(serialize($args));
unset( $crons[$timestamp][$hook][$key] );
if ( empty($crons[$timestamp][$hook]) )
unset( $crons[$timestamp][$hook] );
if ( empty($crons[$timestamp]) )
unset( $crons[$timestamp] );
$key = md5( serialize( $args ) );
unset( $crons[ $timestamp ][ $hook ][ $key ] );
if ( empty( $crons[ $timestamp ][ $hook ] ) ) {
unset( $crons[ $timestamp ][ $hook ] );
}
if ( empty( $crons[ $timestamp ] ) ) {
unset( $crons[ $timestamp ] );
}
_set_cron_array( $crons );
}
@@ -213,8 +236,8 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
function wp_clear_scheduled_hook( $hook, $args = array() ) {
// Backward compatibility
// Previously this function took the arguments as discrete vars rather than an array like the rest of the API
if ( !is_array($args) ) {
_deprecated_argument( __FUNCTION__, '3.0.0', __('This argument has changed to an array to match the behavior of the other cron functions.') );
if ( ! is_array( $args ) ) {
_deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) );
$args = array_slice( func_get_args(), 1 );
}
@@ -222,8 +245,9 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
// It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
// and, wp_next_scheduled() returns the same schedule in an infinite loop.
$crons = _get_cron_array();
if ( empty( $crons ) )
if ( empty( $crons ) ) {
return;
}
$key = md5( serialize( $args ) );
foreach ( $crons as $timestamp => $cron ) {
@@ -245,7 +269,7 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
function wp_unschedule_hook( $hook ) {
$crons = _get_cron_array();
foreach( $crons as $timestamp => $args ) {
foreach ( $crons as $timestamp => $args ) {
unset( $crons[ $timestamp ][ $hook ] );
if ( empty( $crons[ $timestamp ] ) ) {
@@ -269,12 +293,14 @@ function wp_unschedule_hook( $hook ) {
*/
function wp_next_scheduled( $hook, $args = array() ) {
$crons = _get_cron_array();
$key = md5(serialize($args));
if ( empty($crons) )
$key = md5( serialize( $args ) );
if ( empty( $crons ) ) {
return false;
}
foreach ( $crons as $timestamp => $cron ) {
if ( isset( $cron[$hook][$key] ) )
if ( isset( $cron[ $hook ][ $key ] ) ) {
return $timestamp;
}
}
return false;
}
@@ -287,11 +313,13 @@ function wp_next_scheduled( $hook, $args = array() ) {
* @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used).
*/
function spawn_cron( $gmt_time = 0 ) {
if ( ! $gmt_time )
if ( ! $gmt_time ) {
$gmt_time = microtime( true );
}
if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) {
return;
}
/*
* Get the cron lock, which is a Unix timestamp of when the last cron was spawned
@@ -300,26 +328,30 @@ function spawn_cron( $gmt_time = 0 ) {
* Multiple processes on multiple web servers can run this code concurrently,
* this lock attempts to make spawning as atomic as possible.
*/
$lock = get_transient('doing_cron');
$lock = get_transient( 'doing_cron' );
if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) {
$lock = 0;
}
// don't run if another process is currently running it or more than once every 60 sec.
if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
return;
}
//sanity check
$crons = _get_cron_array();
if ( !is_array($crons) )
if ( ! is_array( $crons ) ) {
return;
}
$keys = array_keys( $crons );
if ( isset($keys[0]) && $keys[0] > $gmt_time )
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
return;
}
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
return;
}
@@ -331,7 +363,8 @@ function spawn_cron( $gmt_time = 0 ) {
echo ' ';
// flush any buffers and send the headers
while ( @ob_end_flush() );
while ( @ob_end_flush() ) {
}
flush();
WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
@@ -363,16 +396,18 @@ function spawn_cron( $gmt_time = 0 ) {
* }
* @param string $doing_wp_cron The unix timestamp of the cron lock.
*/
$cron_request = apply_filters( 'cron_request', array(
'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
'key' => $doing_wp_cron,
'args' => array(
'timeout' => 0.01,
'blocking' => false,
/** This filter is documented in wp-includes/class-wp-http-streams.php */
'sslverify' => apply_filters( 'https_local_ssl_verify', false )
)
), $doing_wp_cron );
$cron_request = apply_filters(
'cron_request', array(
'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
'key' => $doing_wp_cron,
'args' => array(
'timeout' => 0.01,
'blocking' => false,
/** This filter is documented in wp-includes/class-wp-http-streams.php */
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
),
), $doing_wp_cron
);
wp_remote_post( $cron_request['url'], $cron_request['args'] );
}
@@ -384,23 +419,29 @@ function spawn_cron( $gmt_time = 0 ) {
*/
function wp_cron() {
// Prevent infinite loops caused by lack of wp-cron.php
if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) {
return;
}
if ( false === $crons = _get_cron_array() )
if ( false === $crons = _get_cron_array() ) {
return;
}
$gmt_time = microtime( true );
$keys = array_keys( $crons );
if ( isset($keys[0]) && $keys[0] > $gmt_time )
$keys = array_keys( $crons );
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
return;
}
$schedules = wp_get_schedules();
foreach ( $crons as $timestamp => $cronhooks ) {
if ( $timestamp > $gmt_time ) break;
if ( $timestamp > $gmt_time ) {
break;
}
foreach ( (array) $cronhooks as $hook => $args ) {
if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) {
continue;
}
spawn_cron( $gmt_time );
break 2;
}
@@ -429,19 +470,27 @@ function wp_cron() {
* // Filter parameter variable name is 'array'.
* $array['weekly'] = array(
* 'interval' => 604800,
* 'display' => __( 'Once Weekly' )
* 'display' => __( 'Once Weekly' )
* );
*
*
* @since 2.1.0
*
* @return array
*/
function wp_get_schedules() {
$schedules = array(
'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ),
'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ),
'hourly' => array(
'interval' => HOUR_IN_SECONDS,
'display' => __( 'Once Hourly' ),
),
'twicedaily' => array(
'interval' => 12 * HOUR_IN_SECONDS,
'display' => __( 'Twice Daily' ),
),
'daily' => array(
'interval' => DAY_IN_SECONDS,
'display' => __( 'Once Daily' ),
),
);
/**
* Filters the non-default cron schedules.
@@ -464,14 +513,16 @@ function wp_get_schedules() {
* @param array $args Optional. Arguments passed to the event's callback function.
* @return string|false False, if no schedule. Schedule name on success.
*/
function wp_get_schedule($hook, $args = array()) {
function wp_get_schedule( $hook, $args = array() ) {
$crons = _get_cron_array();
$key = md5(serialize($args));
if ( empty($crons) )
$key = md5( serialize( $args ) );
if ( empty( $crons ) ) {
return false;
}
foreach ( $crons as $timestamp => $cron ) {
if ( isset( $cron[$hook][$key] ) )
return $cron[$hook][$key]['schedule'];
if ( isset( $cron[ $hook ][ $key ] ) ) {
return $cron[ $hook ][ $key ]['schedule'];
}
}
return false;
}
@@ -488,15 +539,17 @@ function wp_get_schedule($hook, $args = array()) {
*
* @return false|array CRON info array.
*/
function _get_cron_array() {
$cron = get_option('cron');
if ( ! is_array($cron) )
function _get_cron_array() {
$cron = get_option( 'cron' );
if ( ! is_array( $cron ) ) {
return false;
}
if ( !isset($cron['version']) )
$cron = _upgrade_cron_array($cron);
if ( ! isset( $cron['version'] ) ) {
$cron = _upgrade_cron_array( $cron );
}
unset($cron['version']);
unset( $cron['version'] );
return $cron;
}
@@ -509,7 +562,7 @@ function _get_cron_array() {
*
* @param array $cron Cron info array from _get_cron_array().
*/
function _set_cron_array($cron) {
function _set_cron_array( $cron ) {
$cron['version'] = 2;
update_option( 'cron', $cron );
}
@@ -525,16 +578,17 @@ function _set_cron_array($cron) {
* @param array $cron Cron info array from _get_cron_array().
* @return array An upgraded Cron info array.
*/
function _upgrade_cron_array($cron) {
if ( isset($cron['version']) && 2 == $cron['version'])
function _upgrade_cron_array( $cron ) {
if ( isset( $cron['version'] ) && 2 == $cron['version'] ) {
return $cron;
}
$new_cron = array();
foreach ( (array) $cron as $timestamp => $hooks) {
foreach ( (array) $cron as $timestamp => $hooks ) {
foreach ( (array) $hooks as $hook => $args ) {
$key = md5(serialize($args['args']));
$new_cron[$timestamp][$hook][$key] = $args;
$key = md5( serialize( $args['args'] ) );
$new_cron[ $timestamp ][ $hook ][ $key ] = $args;
}
}