Make WordPress Core

Changeset 35810


Ignore:
Timestamp:
12/06/2015 11:21:46 PM (9 years ago)
Author:
westonruter
Message:

Customizer: Apply customize_dynamic_setting_class and customize_dynamic_setting_args filters in calls to WP_Customize_Manager::add_setting( $id, $args ).

Ensure that plugin filters apply as expected for any settings registered statically without passing in an explicit class instance to WP_Customize_Manager::add_setting().

Fixes #34597.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r35781 r35810  
    10501050            $setting = $id;
    10511051        } else {
    1052             $setting = new WP_Customize_Setting( $this, $id, $args );
     1052            $class = 'WP_Customize_Setting';
     1053
     1054            /** This filter is documented in wp-includes/class-wp-customize-manager.php */
     1055            $args = apply_filters( 'customize_dynamic_setting_args', $args, $id );
     1056
     1057            /** This filter is documented in wp-includes/class-wp-customize-manager.php */
     1058            $class = apply_filters( 'customize_dynamic_setting_class', $class, $id, $args );
     1059
     1060            $setting = new $class( $this, $id, $args );
    10531061        }
    10541062
  • trunk/tests/phpunit/tests/customize/manager.php

    r35781 r35810  
    246246     */
    247247    function filter_customize_dynamic_setting_args_for_test_dynamic_settings( $setting_args, $setting_id ) {
    248         $this->assertEquals( false, $setting_args, 'Expected $setting_args to be false by default.' );
    249248        $this->assertInternalType( 'string', $setting_id );
    250249        if ( in_array( $setting_id, array( 'foo', 'bar' ) ) ) {
     
    543542
    544543    /**
     544     * @ticket 34597
     545     */
     546    function test_add_setting_honoring_dynamic() {
     547        $manager = new WP_Customize_Manager();
     548
     549        $setting_id = 'dynamic';
     550        $setting = $manager->add_setting( $setting_id );
     551        $this->assertEquals( 'WP_Customize_Setting', get_class( $setting ) );
     552        $this->assertObjectNotHasAttribute( 'custom', $setting );
     553        $manager->remove_setting( $setting_id );
     554
     555        add_filter( 'customize_dynamic_setting_class', array( $this, 'return_dynamic_customize_setting_class' ), 10, 3 );
     556        add_filter( 'customize_dynamic_setting_args', array( $this, 'return_dynamic_customize_setting_args' ), 10, 2 );
     557        $setting = $manager->add_setting( $setting_id );
     558        $this->assertEquals( 'Test_Dynamic_Customize_Setting', get_class( $setting ) );
     559        $this->assertObjectHasAttribute( 'custom', $setting );
     560        $this->assertEquals( 'foo', $setting->custom );
     561    }
     562
     563    /**
     564     * Return 'Test_Dynamic_Customize_Setting' in 'customize_dynamic_setting_class.
     565     *
     566     * @param string $class Setting class.
     567     * @param array  $args  Setting args.
     568     * @param string $id    Setting ID.
     569     * @return string       Setting class.
     570     */
     571    function return_dynamic_customize_setting_class( $class, $id, $args ) {
     572        unset( $args );
     573        if ( 0 === strpos( $id, 'dynamic' ) ) {
     574            $class = 'Test_Dynamic_Customize_Setting';
     575        }
     576        return $class;
     577    }
     578
     579    /**
     580     * Return 'Test_Dynamic_Customize_Setting' in 'customize_dynamic_setting_class.
     581     *
     582     * @param array  $args Setting args.
     583     * @param string $id   Setting ID.
     584     * @return string      Setting args.
     585     */
     586    function return_dynamic_customize_setting_args( $args, $id ) {
     587        if ( 0 === strpos( $id, 'dynamic' ) ) {
     588            $args['custom'] = 'foo';
     589        }
     590        return $args;
     591    }
     592
     593    /**
    545594     * @ticket 34596
    546595     */
     
    603652    }
    604653}
     654
     655require_once ABSPATH . WPINC . '/class-wp-customize-setting.php';
     656
     657/**
     658 * Class Test_Dynamic_Customize_Setting
     659 *
     660 * @see Tests_WP_Customize_Manager::test_add_setting_honoring_dynamic()
     661 */
     662class Test_Dynamic_Customize_Setting extends WP_Customize_Setting {
     663    public $type = 'dynamic';
     664    public $custom;
     665}
Note: See TracChangeset for help on using the changeset viewer.