WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/08/2015 11:10:05 PM (5 years ago)
Author:
ocean90
Message:

Customizer: Introduce an API to create WP_Customize_Settings for dynamically-created settings.

  • Introduce WP_Customize_Manager::add_dynamic_settings() to register dynamically-created settings.
  • Introduce customize_dynamic_setting_args filter to pass an array of args to a dynamic setting's constructor.
  • Add unit tests for WP_Customize_Manager and WP_Customize_Widgets.
  • See WP_Customize_Widgets as an example.

props westonruter.
fixes #30936.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/manager.php

    r31329 r31370  
    3333
    3434    /**
    35      * Test WP_Customize_Manager::unsanitized_post_values()
     35     * Test WP_Customize_Manager::doing_ajax().
     36     *
     37     * @group ajax
     38     */
     39    function test_doing_ajax() {
     40        if ( ! defined( 'DOING_AJAX' ) ) {
     41            define( 'DOING_AJAX', true );
     42        }
     43
     44        $manager = $this->instantiate();
     45        $this->assertTrue( $manager->doing_ajax() );
     46
     47        $_REQUEST['action'] = 'customize_save';
     48        $this->assertTrue( $manager->doing_ajax( 'customize_save' ) );
     49        $this->assertFalse( $manager->doing_ajax( 'update-widget' ) );
     50    }
     51
     52    /**
     53     * Test ! WP_Customize_Manager::doing_ajax().
     54     */
     55    function test_not_doing_ajax() {
     56        if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
     57            $this->markTestSkipped( 'Cannot test when DOING_AJAX' );
     58        }
     59
     60        $manager = $this->instantiate();
     61        $this->assertFalse( $manager->doing_ajax() );
     62    }
     63
     64    /**
     65     * Test WP_Customize_Manager::unsanitized_post_values().
    3666     *
    3767     * @ticket 30988
     
    5080
    5181    /**
    52      * Test the WP_Customize_Manager::post_value() method
     82     * Test the WP_Customize_Manager::post_value() method.
    5383     *
    5484     * @ticket 30988
     
    72102    }
    73103
     104    /**
     105     * Test the WP_Customize_Manager::add_dynamic_settings() method.
     106     *
     107     * @ticket 30936
     108     */
     109    function test_add_dynamic_settings() {
     110        $manager = $this->instantiate();
     111        $setting_ids = array( 'foo', 'bar' );
     112        $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
     113        $this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected there to not be a bar setting up front.' );
     114        $manager->add_dynamic_settings( $setting_ids );
     115        $this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected the bar setting to remain absent since filters not added.' );
     116
     117        $this->action_customize_register_for_dynamic_settings();
     118        $manager->add_dynamic_settings( $setting_ids );
     119        $this->assertNotEmpty( $manager->get_setting( 'bar' ), 'Expected bar setting to be created since filters were added.' );
     120        $this->assertEquals( 'foo_default', $manager->get_setting( 'foo' )->default, 'Expected static foo setting to not get overridden by dynamic setting.' );
     121        $this->assertEquals( 'dynamic_bar_default', $manager->get_setting( 'bar' )->default, 'Expected dynamic setting bar to have default providd by filter.' );
     122    }
     123
     124    /**
     125     * Test the WP_Customize_Manager::register_dynamic_settings() method.
     126     *
     127     * This is similar to test_add_dynamic_settings, except the settings are passed via $_POST['customized'].
     128     *
     129     * @ticket 30936
     130     */
     131    function test_register_dynamic_settings() {
     132        $posted_settings = array(
     133            'foo' => 'OOF',
     134            'bar' => 'RAB',
     135        );
     136        $_POST['customized'] = wp_slash( wp_json_encode( $posted_settings ) );
     137
     138        add_action( 'customize_register', array( $this, 'action_customize_register_for_dynamic_settings' ) );
     139
     140        $manager = $this->instantiate();
     141        $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
     142
     143        $this->assertEmpty( $manager->get_setting( 'bar' ), 'Expected dynamic setting "bar" to not be registered.' );
     144        do_action( 'customize_register', $manager );
     145        $this->assertNotEmpty( $manager->get_setting( 'bar' ), 'Expected dynamic setting "bar" to be automatically registered after customize_register action.' );
     146        $this->assertEmpty( $manager->get_setting( 'baz' ), 'Expected unrecognized dynamic setting "baz" to remain unregistered.' );
     147    }
     148
     149    /**
     150     * In lieu of closures, callback for customize_register action added in test_register_dynamic_settings().
     151     */
     152    function action_customize_register_for_dynamic_settings() {
     153        add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args_for_test_dynamic_settings' ), 10, 2 );
     154        add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_customize_dynamic_setting_class_for_test_dynamic_settings' ), 10, 3 );
     155    }
     156
     157    /**
     158     * In lieu of closures, callback for customize_dynamic_setting_args filter added for test_register_dynamic_settings().
     159     */
     160    function filter_customize_dynamic_setting_args_for_test_dynamic_settings( $setting_args, $setting_id ) {
     161        $this->assertEquals( false, $setting_args, 'Expected $setting_args to be false by default.' );
     162        $this->assertInternalType( 'string', $setting_id );
     163        if ( in_array( $setting_id, array( 'foo', 'bar' ) ) ) {
     164            $setting_args = array( 'default' => "dynamic_{$setting_id}_default" );
     165        }
     166        return $setting_args;
     167    }
     168
     169    /**
     170     * In lieu of closures, callback for customize_dynamic_setting_class filter added for test_register_dynamic_settings().
     171     */
     172    function filter_customize_dynamic_setting_class_for_test_dynamic_settings( $setting_class, $setting_id, $setting_args ) {
     173        $this->assertEquals( 'WP_Customize_Setting', $setting_class );
     174        $this->assertInternalType( 'string', $setting_id );
     175        $this->assertInternalType( 'array', $setting_args );
     176        return $setting_class;
     177    }
    74178}
    75 
Note: See TracChangeset for help on using the changeset viewer.