Make WordPress Core

Changeset 39431


Ignore:
Timestamp:
12/02/2016 07:10:56 AM (8 years ago)
Author:
pento
Message:

Plugins: Add a current_priority() method to WP_Hook.

This allows plugins to determine the currently running priority of a filter.

Merges [39430] to the 4.7 branch.

Fixes #39007.

Location:
branches/4.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

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

    r38573 r39431  
    353353
    354354    /**
     355     * Return the current priority level of the currently running iteration of the hook.
     356     *
     357     * @since 4.7.0
     358     * @access public
     359     *
     360     * @return int|false If the hook is running, return the current priority level. If it isn't running, return false.
     361     */
     362    public function current_priority() {
     363        if ( false === current( $this->iterations ) ) {
     364            return false;
     365        }
     366
     367        return current( current( $this->iterations ) );
     368    }
     369
     370    /**
    355371     * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
    356372     *
  • branches/4.7/tests/phpunit/tests/filters.php

    r38762 r39431  
    348348        $this->assertSame( $val, apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $val ), '4.6' ) );
    349349    }
     350
     351    private $current_priority;
     352    /**
     353     * @ticket 39007
     354     */
     355    public function test_current_priority() {
     356        add_action( 'test_current_priority', array( $this, '_current_priority_action' ), 99 );
     357        do_action( 'test_current_priority' );
     358        remove_action( 'test_current_priority', array( $this, '_current_priority_action' ), 99 );
     359
     360        $this->assertSame( 99, $this->current_priority );
     361    }
     362
     363    public function _current_priority_action() {
     364        global $wp_filter;
     365        $this->current_priority = $wp_filter[ current_filter() ]->current_priority();
     366    }
     367
     368    /**
     369     * @ticket 39007
     370     */
     371    public function test_other_priority() {
     372        add_action( 'test_current_priority', array( $this, '_other_priority_action' ), 99 );
     373        do_action( 'test_current_priority' );
     374        remove_action( 'test_current_priority', array( $this, '_other_priority_action' ), 99 );
     375
     376        $this->assertSame( false, $this->current_priority );
     377    }
     378
     379    public function _other_priority_action() {
     380        global $wp_filter;
     381        $this->current_priority = $wp_filter[ 'the_content' ]->current_priority();
     382    }
    350383}
Note: See TracChangeset for help on using the changeset viewer.