Make WordPress Core


Ignore:
Timestamp:
11/21/2015 02:51:57 AM (9 years ago)
Author:
westonruter
Message:

Customize: Ensure that a setting (especially a multidimensional one) can still be previewed when the post value to preview is set after preview() is invoked.

  • Introduce customize_post_value_set_{$setting_id} and customize_post_value_set actions which are done when WP_Customize_Manager::set_post_value() is called.
  • Clear the preview_applied flag for aggregated multidimensional settings when a post value is set. This ensures the new value is used instead of a previously-cached previewed value.
  • Move $is_preview property from subclasses to WP_Customize_Setting parent class.
  • Deferred preview: Ensure that when preview() short-circuits due to not being applicable that it will be called again later when the post value is set.
  • Populate post value for updated-widget with the (unsanitized) JS-value in WP_Customize_Widgets::call_widget_update() so that value will be properly sanitized when accessed in WP_Customize_Manager::post_value().

Includes unit tests with assertions to check the reported issues and validate the fixes.

Fixes defect introduced in [35007].
See #32103.
Fixes #34738.

File:
1 edited

Legend:

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

    r35635 r35724  
    3333        parent::setUp();
    3434        require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
    35         $GLOBALS['wp_customize'] = new WP_Customize_Manager();
    36         $this->manager = $GLOBALS['wp_customize'];
     35        $this->manager = $this->instantiate();
    3736        $this->undefined = new stdClass();
    3837    }
     
    6766        }
    6867
    69         $manager = $this->instantiate();
     68        $manager = $this->manager;
    7069        $this->assertTrue( $manager->doing_ajax() );
    7170
     
    8382        }
    8483
    85         $manager = $this->instantiate();
     84        $manager = $this->manager;
    8685        $this->assertFalse( $manager->doing_ajax() );
    8786    }
     
    9392     */
    9493    function test_unsanitized_post_values() {
    95         $manager = $this->instantiate();
     94        $manager = $this->manager;
    9695
    9796        $customized = array(
     
    115114        $_POST['customized'] = wp_slash( wp_json_encode( $posted_settings ) );
    116115
    117         $manager = $this->instantiate();
     116        $manager = $this->manager;
    118117
    119118        $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
     
    128127
    129128    /**
     129     * Test WP_Customize_Manager::set_post_value().
     130     *
     131     * @see WP_Customize_Manager::set_post_value()
     132     */
     133    function test_set_post_value() {
     134        $this->manager->add_setting( 'foo', array(
     135            'sanitize_callback' => array( $this, 'sanitize_foo_for_test_set_post_value' ),
     136        ) );
     137        $setting = $this->manager->get_setting( 'foo' );
     138
     139        $this->assertEmpty( $this->captured_customize_post_value_set_actions );
     140        add_action( 'customize_post_value_set', array( $this, 'capture_customize_post_value_set_actions' ), 10, 3 );
     141        add_action( 'customize_post_value_set_foo', array( $this, 'capture_customize_post_value_set_actions' ), 10, 2 );
     142        $this->manager->set_post_value( $setting->id, '123abc' );
     143        $this->assertCount( 2, $this->captured_customize_post_value_set_actions );
     144        $this->assertEquals( 'customize_post_value_set_foo', $this->captured_customize_post_value_set_actions[0]['action'] );
     145        $this->assertEquals( 'customize_post_value_set', $this->captured_customize_post_value_set_actions[1]['action'] );
     146        $this->assertEquals( array( '123abc', $this->manager ), $this->captured_customize_post_value_set_actions[0]['args'] );
     147        $this->assertEquals( array( $setting->id, '123abc', $this->manager ), $this->captured_customize_post_value_set_actions[1]['args'] );
     148
     149        $unsanitized = $this->manager->unsanitized_post_values();
     150        $this->assertArrayHasKey( $setting->id, $unsanitized );
     151
     152        $this->assertEquals( '123abc', $unsanitized[ $setting->id ] );
     153        $this->assertEquals( 123, $setting->post_value() );
     154    }
     155
     156    /**
     157     * Sanitize a value for Tests_WP_Customize_Manager::test_set_post_value().
     158     *
     159     * @see Tests_WP_Customize_Manager::test_set_post_value()
     160     *
     161     * @param mixed $value Value.
     162     * @return int Value.
     163     */
     164    function sanitize_foo_for_test_set_post_value( $value ) {
     165        return intval( $value );
     166    }
     167
     168    /**
     169     * Store data coming from customize_post_value_set action calls.
     170     *
     171     * @see Tests_WP_Customize_Manager::capture_customize_post_value_set_actions()
     172     * @var array
     173     */
     174    protected $captured_customize_post_value_set_actions = array();
     175
     176    /**
     177     * Capture the actions fired when calling WP_Customize_Manager::set_post_value().
     178     *
     179     * @see Tests_WP_Customize_Manager::test_set_post_value()
     180     */
     181    function capture_customize_post_value_set_actions() {
     182        $action = current_action();
     183        $args = func_get_args();
     184        $this->captured_customize_post_value_set_actions[] = compact( 'action', 'args' );
     185    }
     186
     187    /**
    130188     * Test the WP_Customize_Manager::add_dynamic_settings() method.
    131189     *
     
    133191     */
    134192    function test_add_dynamic_settings() {
    135         $manager = $this->instantiate();
     193        $manager = $this->manager;
    136194        $setting_ids = array( 'foo', 'bar' );
    137195        $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
     
    163221        add_action( 'customize_register', array( $this, 'action_customize_register_for_dynamic_settings' ) );
    164222
    165         $manager = $this->instantiate();
     223        $manager = $this->manager;
    166224        $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
    167225
Note: See TracChangeset for help on using the changeset viewer.