WordPress.org

Make WordPress Core

Ticket #10441: 10441.2.diff

File 10441.2.diff, 8.5 KB (added by boonebgorges, 2 years ago)
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 7710ab3..f94320a 100644
    function _deprecated_argument( $function, $version, $message = null ) { 
    38653865}
    38663866
    38673867/**
     3868 * Marks an action or filter hook as deprecated and informs when it has been used.
     3869 *
     3870 * There is a hook deprecated_hook_run that will be called that can be used
     3871 * to get the backtrace up to what file and function was used for the callback.
     3872 *
     3873 * The current behavior is to trigger a user error if WP_DEBUG is true.
     3874 *
     3875 * This function is to be used for every hook that is deprecated, when any callback is
     3876 * attacked to the hook, as determined by has_action() or has_filter(), and shall be
     3877 * called before the hook is fired.
     3878 *
     3879 * @since 4.6.0
     3880 *
     3881 * @param string $hook        The hook that was used.
     3882 * @param string $version     The version of WordPress that deprecated the hook.
     3883 * @param string $replacement Optional. The hook that should have been used.
     3884 * @param string $message     Optional. A message regarding the change.
     3885 */
     3886function _deprecated_hook( $hook, $version, $replacement = null, $message = null ) {
     3887        /**
     3888         * Fires when a deprecated hook is called.
     3889         *
     3890         * @since 4.6.0
     3891         *
     3892         * @param string $hook        The hook that was called.
     3893         * @param string $replacement The hook that should be used as a replacement.
     3894         * @param string $version     The version of WordPress that deprecated the argument used.
     3895         * @param string $message     A message regarding the change.
     3896         */
     3897        do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message );
     3898
     3899        /**
     3900         * Filter whether to trigger deprecated hook errors.
     3901         *
     3902         * @since 4.6.0
     3903         *
     3904         * @param bool $trigger Whether to trigger deprecated hook errors. Requires
     3905         *                      `WP_DEBUG` to be defined true.
     3906         */
     3907        if ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) {
     3908                $message = empty( $message ) ? '' : ' ' . $message;
     3909                if ( ! is_null( $replacement ) ) {
     3910                        trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $hook, $version, $replacement ) . $message );
     3911                } else {
     3912                        trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $hook, $version ) . $message );
     3913                }
     3914        }
     3915}
     3916
     3917/**
    38683918 * Mark something as being incorrectly called.
    38693919 *
    38703920 * There is a hook doing_it_wrong_run that will be called that can be used
  • src/wp-includes/plugin.php

    diff --git src/wp-includes/plugin.php src/wp-includes/plugin.php
    index dd8798a..d2ebeac 100644
    function remove_all_actions($tag, $priority = false) { 
    655655        return remove_all_filters($tag, $priority);
    656656}
    657657
     658/**
     659 * Fires functions attached to a deprecated filter hook.
     660 *
     661 * Wraps apply_filters().
     662 *
     663 * @since 4.4.0
     664 *
     665 * @see _deprecated_hook()
     666 *
     667 * @param string $tag         The name of the filter hook.
     668 * @param array  $args        Array of additional function arguments to be passed to apply_filters().
     669 * @param string $version     The version of WordPress that deprecated the hook.
     670 * @param string $replacement Optional. The hook that should have been used.
     671 * @param string $message     Optional. A message regarding the change.
     672 */
     673function apply_filters_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
     674        if ( ! has_filter( $tag ) ) {
     675                return;
     676        }
     677
     678        _deprecated_hook( $tag, $version, $replacement, $message );
     679
     680        return apply_filters_ref_array( $tag, $args );
     681}
     682
     683/**
     684 * Fires functions attached to a deprecated action hook.
     685 *
     686 * Wraps do_action().
     687 *
     688 * @since 4.6.0
     689 *
     690 * @see _deprecated_hook()
     691 *
     692 * @param string $tag         The name of the filter hook.
     693 * @param array  $args        Array of additional function arguments to be passed to do_action().
     694 * @param string $version     The version of WordPress that deprecated the hook.
     695 * @param string $replacement Optional. The hook that should have been used.
     696 * @param string $message     Optional. A message regarding the change.
     697 */
     698function do_action_deprecated( $tag, $args, $version, $replacement = false, $message = null ) {
     699        if ( ! has_action( $tag ) ) {
     700                return;
     701        }
     702
     703        _deprecated_hook( $tag, $version, $replacement, $message );
     704
     705        do_action_ref_array( $tag, $args );
     706}
     707
    658708//
    659709// Functions for handling plugins.
    660710//
  • tests/phpunit/includes/testcase.php

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index 6c893ce..6eed90a 100644
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    309309                }
    310310                add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
    311311                add_action( 'deprecated_argument_run', array( $this, 'deprecated_function_run' ) );
     312                add_action( 'deprecated_hook_run', array( $this, 'deprecated_function_run' ) );
    312313                add_action( 'doing_it_wrong_run', array( $this, 'doing_it_wrong_run' ) );
    313314                add_action( 'deprecated_function_trigger_error', '__return_false' );
    314315                add_action( 'deprecated_argument_trigger_error', '__return_false' );
     316                add_action( 'deprecated_hook_trigger_error',     '__return_false' );
    315317                add_action( 'doing_it_wrong_trigger_error',      '__return_false' );
    316318        }
    317319
  • tests/phpunit/tests/actions.php

    diff --git tests/phpunit/tests/actions.php tests/phpunit/tests/actions.php
    index 583c8ce..5b9cf6c 100644
    class Tests_Actions extends WP_UnitTestCase { 
    356356                $this->assertTrue( doing_filter( 'testing_nested' ) );
    357357                $this->assertFalse( doing_filter( 'something_else' ) );
    358358        }
     359
     360        /**
     361         * @ticket 10441
     362         * @expectedDeprecated tests_do_action_deprecated
     363         */
     364        public function test_do_action_deprecated() {
     365                $p = new WP_Post( (object) array( 'post_title' => 'Foo' ) );
     366
     367                add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
     368                do_action_deprecated( 'tests_do_action_deprecated', array( $p ), '4.6' );
     369                remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
     370
     371                $this->assertSame( 'Bar', $p->post_title );
     372        }
     373
     374        public static function deprecated_action_callback( $p ) {
     375                $p->post_title = 'Bar';
     376        }
     377
     378        /**
     379         * @ticket 10441
     380         * @expectedDeprecated tests_do_action_deprecated
     381         */
     382        public function test_do_action_deprecated_with_multiple_params() {
     383                $p1 = new WP_Post( (object) array( 'post_title' => 'Foo1' ) );
     384                $p2 = new WP_Post( (object) array( 'post_title' => 'Foo2' ) );
     385
     386                add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
     387                do_action_deprecated( 'tests_do_action_deprecated', array( $p1, $p2 ), '4.6' );
     388                remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
     389
     390                $this->assertSame( 'Bar1', $p1->post_title );
     391                $this->assertSame( 'Bar2', $p2->post_title );
     392        }
     393
     394        public static function deprecated_action_callback_multiple_params( $p1, $p2 ) {
     395                $p1->post_title = 'Bar1';
     396                $p2->post_title = 'Bar2';
     397        }
     398
     399        /**
     400         * @ticket 10441
     401         * @expectedDeprecated tests_apply_filters_deprecated
     402         */
     403        public function test_apply_filters_deprecated() {
     404                $p = 'Foo';
     405
     406                add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
     407                $p = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p ), '4.6' );
     408                remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
     409
     410                $this->assertSame( 'Bar', $p );
     411        }
     412
     413        public static function deprecated_filter_callback( $p ) {
     414                $p = 'Bar';
     415                return $p;
     416        }
     417
     418        /**
     419         * @ticket 10441
     420         * @expectedDeprecated tests_apply_filters_deprecated
     421         */
     422        public function test_apply_filters_deprecated_with_multiple_params() {
     423                $p1 = 'Foo1';
     424                $p2 = 'Foo2';
     425
     426                add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
     427                $p1 = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p1, $p2 ), '4.6' );
     428                remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
     429
     430                $this->assertSame( 'Bar1', $p1 );
     431
     432                // Not passed by reference, so not modified.
     433                $this->assertSame( 'Foo2', $p2 );
     434        }
     435
     436        public static function deprecated_filter_callback_multiple_params( $p1, $p2 ) {
     437                $p1 = 'Bar1';
     438                $p2 = 'Bar2';
     439
     440                return $p1;
     441        }
    359442}