Make WordPress Core

Ticket #43621: 43621.2.diff

File 43621.2.diff, 4.9 KB (added by soulseekah, 7 years ago)

Refresh

  • src/wp-includes/class-wp-hook.php

    diff --git src/wp-includes/class-wp-hook.php src/wp-includes/class-wp-hook.php
    index efb1360..ebdcecc 100644
    final class WP_Hook implements Iterator, ArrayAccess { 
    4242        private $current_priority = array();
    4343
    4444        /**
     45         * The current callback that is running.
     46         *
     47         * @since 5.0
     48         * @var callable
     49         */
     50        private $current_callback = null;
     51        /**
     52
     53        /**
     54         * The previous callback that ran before the current one.
     55         *
     56         * @since 5.0
     57         * @var callable
     58         */
     59        private $previous_callback = null;
     60
     61        /**
    4562         * Number of levels this hook can be recursively called.
    4663         *
    4764         * @since 4.7.0
    final class WP_Hook implements Iterator, ArrayAccess { 
    279296                                        $args[0] = $value;
    280297                                }
    281298
     299                                $this->current_callback = $the_;
     300
    282301                                // Avoid the array_slice if possible.
    283302                                if ( $the_['accepted_args'] == 0 ) {
    284303                                        $value = call_user_func_array( $the_['function'], array() );
    final class WP_Hook implements Iterator, ArrayAccess { 
    287306                                } else {
    288307                                        $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) );
    289308                                }
     309
     310                                $this->current_callback = null;
     311                                $this->previous_callback = $the_;
    290312                        }
    291313                } while ( false !== next( $this->iterations[ $nesting_level ] ) );
    292314
    final class WP_Hook implements Iterator, ArrayAccess { 
    353375        }
    354376
    355377        /**
     378         * Return the current callback that is running.
     379         *
     380         * @since 5.0
     381         *
     382         * @return callable|null Callback that is running or null if no callback running.
     383         */
     384        public function current_callback() {
     385                return $this->current_callback;
     386        }
     387
     388        /**
     389         * Return the callback that has just run, or ran before the current callback.
     390         *
     391         * @since 5.0
     392         *
     393         * @return callable|null Callback that ran previously or null if no callback ran previously.
     394         */
     395        public function previous_callback() {
     396                return $this->previous_callback;
     397        }
     398
     399        /**
    356400         * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
    357401         *
    358402         * @since 4.7.0
  • src/wp-includes/plugin.php

    diff --git src/wp-includes/plugin.php src/wp-includes/plugin.php
    index 879bb12..5da1805 100644
    function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 
    115115}
    116116
    117117/**
     118 * Hook a function or method to a specific filter callback that is only run once.
     119 *
     120 * The added $function_to_add will only be run once and removed from the filter.
     121 *
     122 * @since 5.0
     123 * @see add_filter
     124 */
     125function add_filter_once( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
     126        $result = add_filter( $tag, $function_to_add, $priority, $accepted_args );
     127
     128        global $wp_filter;
     129
     130        $idx = _wp_filter_build_unique_id( null, $function_to_add, null );
     131        $wp_filter[ $tag ]->callbacks[ $priority ][ "_remove_filter_once_$idx" ] = array(
     132                'function'      => '_remove_filter_once',
     133                'accepted_args' => $accepted_args
     134        );
     135
     136        // Add a remove action immediately after our $function_to_add has run.
     137        add_filter( $tag, '_remove_filter_once', $priority );
     138
     139        return $result;
     140}
     141
     142/**
     143 * Callback that removes an ephemeral filter (via add_filter_once)
     144 * immediately after its execution.
     145 *
     146 * This can be substituted for anoymous callbacks once WordPress
     147 * reaches PHP 5.3 minimum maturity.
     148 *
     149 * @since 5.0
     150 * @see add_filter_once
     151 * @internal
     152 */
     153function _remove_filter_once() {
     154        global $wp_filter;
     155
     156        $tag               = current_filter();
     157        $current_filter    = $wp_filter[ $tag ];
     158        $current_priority  = $current_filter->current_priority();
     159        $previous_callback = $current_filter->previous_callback();
     160
     161        // Remove the callback that has to be run once.
     162        remove_filter( $tag, $previous_callback['function'], $current_priority );
     163
     164        $idx = _wp_filter_build_unique_id( null, $previous_callback['function'], null );
     165
     166        /**
     167         * Remove the remove filter :) ... Filterception.
     168         *
     169         * `remove_filter` removes a callback by unique ID.
     170         * This means that we can precaluculate and pass the
     171         * unique ID as is.
     172         */
     173        remove_filter( $tag, "_remove_filter_once_$idx", $current_priority );
     174
     175        if ( func_num_args() ) {
     176                // If this is a filter return the result.
     177                return func_get_arg( 0 );
     178        }
     179}
     180
     181/**
    118182 * Check if any filter has been registered for a hook.
    119183 *
    120184 * @since 2.5.0
    function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 
    407471}
    408472
    409473/**
     474 * Hook a function or method to a specific action callback that is only run once.
     475 *
     476 * The added $function_to_add will only be run once and removed from the action.
     477 *
     478 * @since 5.0
     479 * @see add_action
     480 */
     481function add_action_once( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
     482        return add_filter_once( $tag, $function_to_add, $priority, $accepted_args );
     483}
     484
     485/**
    410486 * Execute functions hooked on a specific action hook.
    411487 *
    412488 * This function invokes all functions attached to action hook `$tag`. It is