WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/13/2016 02:42:04 AM (3 years ago)
Author:
westonruter
Message:

Customize: Improve extensibility of Custom CSS.

  • Add customize_value_custom_css filter to WP_Customize_Custom_CSS::value() method.
  • Introduce customize_update_custom_css_post_content_args filter in WP_Customize_Custom_CSS::update() method.
  • Make clear that wp_get_custom_css() and wp_get_custom_css filter are specifically for obtaining the value to render/display. Eliminate use of wp_get_custom_css() when getting the setting value. Use the underlying post_value directly when is_previewed.
  • Move anonymous functions handing JS previewing for custom_logo, custom_css, and background into named functions on the wp.customize.settingPreviewHandlers to allow plugins to override/extend preview logic.
  • Update _custom_background_cb to always print a style tag wen in the customizer preview, and update background preview logic to replace existing style element instead of appending a new style to the head so that background changes don't unexpectedly override any Custom CSS in the preview's stylesheet cascade.

Props westonruter, georgestephanis.
See #22058.
Fixes #38672.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/custom-css-setting.php

    r39185 r39209  
    169169
    170170    /**
     171     * Test crud methods on WP_Customize_Custom_CSS_Setting.
     172     *
     173     * @covers WP_Customize_Custom_CSS_Setting::value()
     174     */
     175    function test_value_filter() {
     176        add_filter( 'customize_value_custom_css', array( $this, 'filter_value' ), 10, 2 );
     177        $this->setting->default = '/*default*/';
     178        $this->assertEquals( '/*default*//*filtered*/', $this->setting->value() );
     179
     180        $this->factory()->post->create( array(
     181            'post_title' => $this->setting->stylesheet,
     182            'post_name' => $this->setting->stylesheet,
     183            'post_content' => '/*custom*/',
     184            'post_status' => 'publish',
     185            'post_type' => 'custom_css',
     186        ) );
     187        $this->assertEquals( '/*custom*//*filtered*/', $this->setting->value() );
     188
     189        $this->wp_customize->set_post_value( $this->setting->id, '/*overridden*/' );
     190        $this->setting->preview();
     191        $this->assertEquals( '/*overridden*/', $this->setting->value(), 'Expected value to not be filtered since post value is present.' );
     192    }
     193
     194    /**
     195     * Filter value.
     196     *
     197     * @param string $value                 Value.
     198     * @param WP_Customize_Setting $setting Setting.
     199     * @return string
     200     */
     201    function filter_value( $value, $setting ) {
     202        $this->assertInstanceOf( 'WP_Customize_Custom_CSS_Setting', $setting );
     203        $value .= '/*filtered*/';
     204        return $value;
     205    }
     206
     207    /**
     208     * Test update filter on WP_Customize_Custom_CSS_Setting.
     209     *
     210     * @covers WP_Customize_Custom_CSS_Setting::update()
     211     */
     212    function test_update_filter() {
     213        $original_css = 'body { color:red; }';
     214        $post_id = $this->factory()->post->create( array(
     215            'post_title' => $this->setting->stylesheet,
     216            'post_name' => $this->setting->stylesheet,
     217            'post_content' => $original_css,
     218            'post_status' => 'publish',
     219            'post_type' => 'custom_css',
     220        ) );
     221
     222        $overridden_css = 'body { color:green; }';
     223        $this->wp_customize->set_post_value( $this->setting->id, $overridden_css );
     224
     225        $post = get_post( $post_id );
     226        $original_title = $post->post_title;
     227
     228        add_filter( 'customize_update_custom_css_post_content_args', array( $this, 'filter_update_post_content_args' ), 10, 3 );
     229        $this->setting->save();
     230
     231        $post = get_post( $post_id );
     232        $this->assertEquals( $original_title, $post->post_title );
     233        $this->assertContains( $overridden_css, $post->post_content );
     234        $this->assertContains( '/* filtered post_content */', $post->post_content );
     235        $this->assertContains( '/* filtered post_content_filtered */', $post->post_content_filtered );
     236    }
     237
     238    /**
     239     * Filter `customize_update_custom_css_post_content_args`.
     240     *
     241     * @param array                $args    Post array.
     242     * @param string               $css     CSS.
     243     * @param WP_Customize_Setting $setting Setting.
     244     * @return array Args.
     245     */
     246    function filter_update_post_content_args( $args, $css, $setting ) {
     247        $this->assertInternalType( 'array', $args );
     248        $this->assertEqualSets( array( 'post_content', 'post_content_filtered' ), array_keys( $args ) );
     249        $this->assertEquals( $css, $args['post_content'] );
     250        $this->assertEquals( '', $args['post_content_filtered'] );
     251        $this->assertInstanceOf( 'WP_Customize_Custom_CSS_Setting', $setting );
     252
     253        $args['post_content'] .= '/* filtered post_content */';
     254        $args['post_content_filtered'] = '/* filtered post_content_filtered */';
     255        $args['post_title'] = 'Ignored';
     256        return $args;
     257    }
     258
     259    /**
    171260     * Tests that validation errors are caught appropriately.
    172261     *
Note: See TracChangeset for help on using the changeset viewer.