WordPress.org

Make WordPress Core

Ticket #28635: 28635.2.patch

File 28635.2.patch, 7.9 KB (added by jrtashjian, 4 years ago)
  • src/wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    24562456                $title = '';
    24572457        }
    24582458
    2459         if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
     2459        if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
    24602460                /**
    24612461                 * Filter callback for killing WordPress execution for AJAX requests.
    24622462                 *
  • src/wp-cron.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    99 * @package WordPress
    1010 */
    1111
    12 ignore_user_abort(true);
     12ignore_user_abort( true );
    1313
    14 if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') )
    15         die();
     14if ( defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
     15        die( 'doing_ajax_or_doing_cron' );
     16}
    1617
    1718/**
    1819 * Tell WordPress we are doing the CRON task.
    1920 *
    2021 * @var bool
    2122 */
    22 define('DOING_CRON', true);
     23define( 'DOING_CRON', true );
    2324
    24 if ( !defined('ABSPATH') ) {
     25if ( ! defined( 'ABSPATH' ) ) {
    2526        /** Set up WordPress environment */
    2627        require_once( dirname( __FILE__ ) . '/wp-load.php' );
    2728}
    2829
    2930/**
     31 * Default handler for a WP Cron exit
     32 *
     33 * @param string $code
     34 */
     35function wp_cron_default_exit_handler( $code ) {
     36        do_action( 'wp_cron_response_close', $code );
     37        if ( 'bad_post_request_or_doing_ajax_or_doing_cron' === $code ) {
     38                $response = 400;
     39        } elseif ( 'empty_cron_array' === $code ) {
     40                $response = 204;
     41        } elseif ( 'no_scheduled_actions_due' === $code ) {
     42                $response = 204;
     43        } elseif ( 'cron_locked' === $code ) {
     44                $response = 403;
     45        } elseif ( 'cron_lock_check_fail' === $code ) {
     46                $response = 400;
     47        } elseif ( 'ok_exit_prematurely' === $code ) {
     48                $response = 200;
     49        } elseif ( 'ok' === $code ) {
     50                $response = 200;
     51        } else {
     52                $response = 500;
     53        }
     54        if ( ! headers_sent() ) {
     55                header( 'Content-Type: text/plain' );
     56                status_header( $response );
     57        }
     58        wp_die( $code, '', compact( $response ) );
     59}
     60
     61/**
     62 * Filter callback for killing WordPress execution of WP_Cron.
     63 *
     64 * @param callable $wp_cron_exit_handler Callback function name.
     65 */
     66$wp_cron_exit_handler = apply_filters( 'wp_cron_exit_handler', 'wp_cron_default_exit_handler' );
     67
     68if ( ! empty( $_POST ) ) {
     69        $wp_cron_exit_handler( 'bad_post_request' );
     70}
     71
     72/**
    3073 * Retrieves the cron lock.
    3174 *
    3275 * Returns the uncached `doing_cron` transient.
     
    4891                $value = wp_cache_get( 'doing_cron', 'transient', true );
    4992        } else {
    5093                $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
    51                 if ( is_object( $row ) )
     94                if ( is_object( $row ) ) {
    5295                        $value = $row->option_value;
    53         }
     96                }
     97        }
    5498
    5599        return $value;
    56100}
    57101
    58 if ( false === $crons = _get_cron_array() )
    59         die();
     102if ( false === $crons = _get_cron_array() ) {
     103        $wp_cron_exit_handler( 'empty_cron_array' );
     104}
    60105
    61106$keys = array_keys( $crons );
    62107$gmt_time = microtime( true );
    63108
    64 if ( isset($keys[0]) && $keys[0] > $gmt_time )
    65         die();
     109if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
     110        $wp_cron_exit_handler( 'no_scheduled_actions_due' );
     111}
    66112
    67113
    68114// The cron lock: a unix timestamp from when the cron was spawned.
     
    70116
    71117// Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
    72118if ( empty( $doing_wp_cron ) ) {
    73         if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
     119        if ( empty( $_GET['doing_wp_cron'] ) ) {
    74120                // Called from external script/job. Try setting a lock.
    75                 if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) )
    76                         return;
     121                if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) {
     122                        $wp_cron_exit_handler( 'cron_locked' );
     123                }
    77124                $doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
    78125                set_transient( 'doing_cron', $doing_wp_cron );
    79126        } else {
    80                 $doing_wp_cron = $_GET[ 'doing_wp_cron' ];
     127                $doing_wp_cron = $_GET['doing_wp_cron'];
    81128        }
    82129}
    83130
     
    85132 * The cron lock (a unix timestamp set when the cron was spawned),
    86133 * must match $doing_wp_cron (the "key").
    87134 */
    88 if ( $doing_cron_transient != $doing_wp_cron )
    89         return;
     135if ( $doing_cron_transient != $doing_wp_cron ) {
     136        $wp_cron_exit_handler( 'cron_lock_check_fail' );
     137}
    90138
     139/**
     140 * Fires before looping through all the scheduled hooks to filter out those that don't need to run yet.
     141 *
     142 * @param array $crons The scheduled hooks.
     143 */
     144do_action( 'wp_cron_before_crons_loop', $crons );
    91145foreach ( $crons as $timestamp => $cronhooks ) {
    92         if ( $timestamp > $gmt_time )
     146        if ( $timestamp > $gmt_time ) {
    93147                break;
     148        }
    94149
     150        /**
     151         * Fires before looping through the scheduled hooks of a timestamp.
     152         *
     153         * @param array $cronhooks The scheduled hooks of a specific timestamp.
     154         */
     155        do_action( 'wp_cron_before_cronhooks_loop', $cronhooks );
    95156        foreach ( $cronhooks as $hook => $keys ) {
    96157
     158                /**
     159                 * Fires before looping through the scheduled hook's keys.
     160                 *
     161                 * @param array $keys The scheduled hook's keys.
     162                 */
     163                do_action( 'wp_cron_before_keys_loop', $keys );
    97164                foreach ( $keys as $k => $v ) {
    98165
    99166                        $schedule = $v['schedule'];
    100167
    101168                        if ( $schedule != false ) {
    102                                 $new_args = array($timestamp, $schedule, $hook, $v['args']);
    103                                 call_user_func_array('wp_reschedule_event', $new_args);
     169                                $new_args = array( $timestamp, $schedule, $hook, $v['args'] );
     170                                call_user_func_array( 'wp_reschedule_event', $new_args );
    104171                        }
    105172
    106173                        wp_unschedule_event( $timestamp, $hook, $v['args'] );
    107174
    108175                        /**
     176                         * Fires before a scheduled hook is fired.
     177                         *
     178                         * @param string $hook Name of the hook that was scheduled to be fired.
     179                         * @param array $args The arguments to be passed to the hook.
     180                         * @param string $schedule The interval which the scheduled hook runs at.
     181                         * @param int $timestamp The timestamp of when the hook was scheduled to fire.
     182                         */
     183                        do_action( 'wp_cron_before_hook', $hook, $v['args'], $schedule, $timestamp );
     184
     185                        /**
    109186                         * Fires scheduled events.
    110187                         *
    111188                         * @ignore
     
    114191                         * @param string $hook Name of the hook that was scheduled to be fired.
    115192                         * @param array  $args The arguments to be passed to the hook.
    116193                         */
    117                         do_action_ref_array( $hook, $v['args'] );
     194                        do_action_ref_array( $hook, $v['args'] );
    118195
     196                        /**
     197                         * Fires after a scheduled hook is fired.
     198                         *
     199                         * @param string $hook Name of the scheduled hook that fired.
     200                         * @param array $args The arguments passed to the hook.
     201                         * @param string $schedule The interval which the scheduled hook ran at.
     202                         * @param int $timestamp The timestamp of when the hook was scheduled to fire.
     203                         */
     204                        do_action( 'wp_cron_after_hook', $hook, $v['args'], $schedule, $timestamp );
     205
    119206                        // If the hook ran too long and another cron process stole the lock, quit.
    120                         if ( _get_cron_lock() != $doing_wp_cron )
    121                                 return;
    122                 }
    123         }
    124 }
     207                        if ( _get_cron_lock() != $doing_wp_cron ) {
     208                                $wp_cron_exit_handler( 'ok_exit_prematurely' );
     209                        }
     210                }
     211
     212                /**
     213                 * Fires after looping through the scheduled hook's keys.
     214                 *
     215                 * @param array $keys The scheduled hook's keys.
     216                 */
     217                do_action( 'wp_cron_after_keys_loop', $keys );
     218        }
    125219
    126 if ( _get_cron_lock() == $doing_wp_cron )
     220        /**
     221         * Fires after looping through the scheduled hooks of a timestamp.
     222         *
     223         * @param array $cronhooks The scheduled hooks of a specific timestamp.
     224         */
     225        do_action( 'wp_cron_after_cronhooks_loop', $cronhooks );
     226}
     227/**
     228 * Fires before looping through all the scheduled hooks to filter out those that don't need to run yet.
     229 *
     230 * @param array $crons The scheduled hooks.
     231 */
     232do_action( 'wp_cron_after_crons_loop', $crons );
     233
     234if ( _get_cron_lock() == $doing_wp_cron ) {
    127235        delete_transient( 'doing_cron' );
     236}
    128237
    129 die();
     238$wp_cron_exit_handler( 'ok' );