From 5ff3f14d78ddf77a27651fa224abee32fd16b72e Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 2 Dec 2016 07:09:33 +0000 Subject: [PATCH] Plugins: Add a `current_priority()` method to `WP_Hook`. This allows plugins to determine the currently running priority of a filter. Fixes #39007. git-svn-id: https://develop.svn.wordpress.org/trunk@39430 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-hook.php | 16 +++++++++++++++ tests/phpunit/tests/filters.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/wp-includes/class-wp-hook.php b/src/wp-includes/class-wp-hook.php index 823b4b5e58..80b93ef1e6 100755 --- a/src/wp-includes/class-wp-hook.php +++ b/src/wp-includes/class-wp-hook.php @@ -351,6 +351,22 @@ final class WP_Hook implements Iterator, ArrayAccess { $this->nesting_level--; } + /** + * Return the current priority level of the currently running iteration of the hook. + * + * @since 4.7.0 + * @access public + * + * @return int|false If the hook is running, return the current priority level. If it isn't running, return false. + */ + public function current_priority() { + if ( false === current( $this->iterations ) ) { + return false; + } + + return current( current( $this->iterations ) ); + } + /** * Normalizes filters set up before WordPress has initialized to WP_Hook objects. * diff --git a/tests/phpunit/tests/filters.php b/tests/phpunit/tests/filters.php index fddcf4aade..151b4ac8cd 100644 --- a/tests/phpunit/tests/filters.php +++ b/tests/phpunit/tests/filters.php @@ -347,4 +347,37 @@ class Tests_Filters extends WP_UnitTestCase { $this->assertSame( $val, apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $val ), '4.6' ) ); } + + private $current_priority; + /** + * @ticket 39007 + */ + public function test_current_priority() { + add_action( 'test_current_priority', array( $this, '_current_priority_action' ), 99 ); + do_action( 'test_current_priority' ); + remove_action( 'test_current_priority', array( $this, '_current_priority_action' ), 99 ); + + $this->assertSame( 99, $this->current_priority ); + } + + public function _current_priority_action() { + global $wp_filter; + $this->current_priority = $wp_filter[ current_filter() ]->current_priority(); + } + + /** + * @ticket 39007 + */ + public function test_other_priority() { + add_action( 'test_current_priority', array( $this, '_other_priority_action' ), 99 ); + do_action( 'test_current_priority' ); + remove_action( 'test_current_priority', array( $this, '_other_priority_action' ), 99 ); + + $this->assertSame( false, $this->current_priority ); + } + + public function _other_priority_action() { + global $wp_filter; + $this->current_priority = $wp_filter[ 'the_content' ]->current_priority(); + } }