Allow action and filter hooks to be deprecated.

When a filter or action hook is deprecated, the corresponding `apply_filters()`
or `do_action()` calls should be switched out with `apply_filters_deprecated()`
or `do_action_deprecated()`. The latter functions will throw a deprecation
before invoking the original hook.

Props solarissmoke, SergeyBiryukov, DrewAPicture.
Fixes #10441.

git-svn-id: https://develop.svn.wordpress.org/trunk@37861 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-06-25 19:56:19 +00:00
parent 255aaa471e
commit c97f234e6f
4 changed files with 189 additions and 0 deletions

View File

@@ -309,9 +309,11 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
add_action( 'deprecated_argument_run', array( $this, 'deprecated_function_run' ) );
add_action( 'deprecated_hook_run', array( $this, 'deprecated_function_run' ) );
add_action( 'doing_it_wrong_run', array( $this, 'doing_it_wrong_run' ) );
add_action( 'deprecated_function_trigger_error', '__return_false' );
add_action( 'deprecated_argument_trigger_error', '__return_false' );
add_action( 'deprecated_hook_trigger_error', '__return_false' );
add_action( 'doing_it_wrong_trigger_error', '__return_false' );
}

View File

@@ -419,4 +419,87 @@ class Tests_Actions extends WP_UnitTestCase {
$this->assertTrue( doing_filter( 'testing_nested' ) );
$this->assertFalse( doing_filter( 'something_else' ) );
}
/**
* @ticket 10441
* @expectedDeprecated tests_do_action_deprecated
*/
public function test_do_action_deprecated() {
$p = new WP_Post( (object) array( 'post_title' => 'Foo' ) );
add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
do_action_deprecated( 'tests_do_action_deprecated', array( $p ), '4.6' );
remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
$this->assertSame( 'Bar', $p->post_title );
}
public static function deprecated_action_callback( $p ) {
$p->post_title = 'Bar';
}
/**
* @ticket 10441
* @expectedDeprecated tests_do_action_deprecated
*/
public function test_do_action_deprecated_with_multiple_params() {
$p1 = new WP_Post( (object) array( 'post_title' => 'Foo1' ) );
$p2 = new WP_Post( (object) array( 'post_title' => 'Foo2' ) );
add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
do_action_deprecated( 'tests_do_action_deprecated', array( $p1, $p2 ), '4.6' );
remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
$this->assertSame( 'Bar1', $p1->post_title );
$this->assertSame( 'Bar2', $p2->post_title );
}
public static function deprecated_action_callback_multiple_params( $p1, $p2 ) {
$p1->post_title = 'Bar1';
$p2->post_title = 'Bar2';
}
/**
* @ticket 10441
* @expectedDeprecated tests_apply_filters_deprecated
*/
public function test_apply_filters_deprecated() {
$p = 'Foo';
add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
$p = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p ), '4.6' );
remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
$this->assertSame( 'Bar', $p );
}
public static function deprecated_filter_callback( $p ) {
$p = 'Bar';
return $p;
}
/**
* @ticket 10441
* @expectedDeprecated tests_apply_filters_deprecated
*/
public function test_apply_filters_deprecated_with_multiple_params() {
$p1 = 'Foo1';
$p2 = 'Foo2';
add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
$p1 = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p1, $p2 ), '4.6' );
remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
$this->assertSame( 'Bar1', $p1 );
// Not passed by reference, so not modified.
$this->assertSame( 'Foo2', $p2 );
}
public static function deprecated_filter_callback_multiple_params( $p1, $p2 ) {
$p1 = 'Bar1';
$p2 = 'Bar2';
return $p1;
}
}