Make WordPress Core


Ignore:
Timestamp:
06/25/2016 07:56:19 PM (8 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/actions.php

    r37588 r37861  
    420420        $this->assertFalse( doing_filter( 'something_else' ) );
    421421    }
     422
     423    /**
     424     * @ticket 10441
     425     * @expectedDeprecated tests_do_action_deprecated
     426     */
     427    public function test_do_action_deprecated() {
     428        $p = new WP_Post( (object) array( 'post_title' => 'Foo' ) );
     429
     430        add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
     431        do_action_deprecated( 'tests_do_action_deprecated', array( $p ), '4.6' );
     432        remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
     433
     434        $this->assertSame( 'Bar', $p->post_title );
     435    }
     436
     437    public static function deprecated_action_callback( $p ) {
     438        $p->post_title = 'Bar';
     439    }
     440
     441    /**
     442     * @ticket 10441
     443     * @expectedDeprecated tests_do_action_deprecated
     444     */
     445    public function test_do_action_deprecated_with_multiple_params() {
     446        $p1 = new WP_Post( (object) array( 'post_title' => 'Foo1' ) );
     447        $p2 = new WP_Post( (object) array( 'post_title' => 'Foo2' ) );
     448
     449        add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
     450        do_action_deprecated( 'tests_do_action_deprecated', array( $p1, $p2 ), '4.6' );
     451        remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
     452
     453        $this->assertSame( 'Bar1', $p1->post_title );
     454        $this->assertSame( 'Bar2', $p2->post_title );
     455    }
     456
     457    public static function deprecated_action_callback_multiple_params( $p1, $p2 ) {
     458        $p1->post_title = 'Bar1';
     459        $p2->post_title = 'Bar2';
     460    }
     461
     462    /**
     463     * @ticket 10441
     464     * @expectedDeprecated tests_apply_filters_deprecated
     465     */
     466    public function test_apply_filters_deprecated() {
     467        $p = 'Foo';
     468
     469        add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
     470        $p = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p ), '4.6' );
     471        remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
     472
     473        $this->assertSame( 'Bar', $p );
     474    }
     475
     476    public static function deprecated_filter_callback( $p ) {
     477        $p = 'Bar';
     478        return $p;
     479    }
     480
     481    /**
     482     * @ticket 10441
     483     * @expectedDeprecated tests_apply_filters_deprecated
     484     */
     485    public function test_apply_filters_deprecated_with_multiple_params() {
     486        $p1 = 'Foo1';
     487        $p2 = 'Foo2';
     488
     489        add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
     490        $p1 = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p1, $p2 ), '4.6' );
     491        remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
     492
     493        $this->assertSame( 'Bar1', $p1 );
     494
     495        // Not passed by reference, so not modified.
     496        $this->assertSame( 'Foo2', $p2 );
     497    }
     498
     499    public static function deprecated_filter_callback_multiple_params( $p1, $p2 ) {
     500        $p1 = 'Bar1';
     501        $p2 = 'Bar2';
     502
     503        return $p1;
     504    }
    422505}
Note: See TracChangeset for help on using the changeset viewer.