Make WordPress Core


Ignore:
Timestamp:
11/23/2016 05:33:21 PM (7 years ago)
Author:
westonruter
Message:

Customize: Refactor logic for updating custom_css posts by introducing wp_update_custom_css_post() function and renaming update filter.

  • Moves logic from WP_Customize_Custom_CSS_Setting::update() into a re-usable wp_update_custom_css_post() function, useful for future REST API endpoint, WP-CLI command, or plugin migrations.
  • Renames customize_update_custom_css_post_content_args filter to update_custom_css_data and improves the naming of the parameters. Instead of passing post_content and post_content_filtered the filtered array now contains css and preprocessed respectively.
  • The second context param for the update_custom_css_data filter is now an array of the original args passed to wp_update_custom_css_post() and there is now no more $setting arg since it isn't necessarily being called in the customizer context.

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

File:
1 edited

Legend:

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

    r39209 r39350  
    152152        $this->assertEquals( $updated_css, $this->setting->value() );
    153153        $this->assertEquals( $updated_css, wp_get_custom_css( $this->setting->stylesheet ) );
     154        $this->assertEquals( $updated_css, get_post( $post_id )->post_content );
    154155
    155156        $previewed_css = 'body { color: red; }';
     
    159160        $this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) );
    160161
     162        // Make sure that wp_update_custom_css_post() works as expected for updates.
     163        $r = wp_update_custom_css_post( 'body { color:red; }', array(
     164            'stylesheet' => $this->setting->stylesheet,
     165            'preprocessed' => "body\n\tcolor:red;",
     166        ) );
     167        $this->assertInstanceOf( 'WP_Post', $r );
     168        $this->assertEquals( $post_id, $r->ID );
     169        $this->assertEquals( 'body { color:red; }', get_post( $r )->post_content );
     170        $this->assertEquals( "body\n\tcolor:red;", get_post( $r )->post_content_filtered );
     171        $r = wp_update_custom_css_post( 'body { content: "\o/"; }' );
     172        $this->assertEquals( $this->wp_customize->get_stylesheet(), get_post( $r )->post_name );
     173        $this->assertEquals( 'body { content: "\o/"; }', get_post( $r )->post_content );
     174        $this->assertEquals( '', get_post( $r )->post_content_filtered );
     175
     176        // Make sure that wp_update_custom_css_post() works as expected for insertion.
     177        $r = wp_update_custom_css_post( 'body { background:black; }', array(
     178            'stylesheet' => 'other',
     179        ) );
     180        $this->assertInstanceOf( 'WP_Post', $r );
     181        $this->assertEquals( 'other', get_post( $r )->post_name );
     182        $this->assertEquals( 'body { background:black; }', get_post( $r )->post_content );
     183        $this->assertEquals( 'publish', get_post( $r )->post_status );
     184
     185        // Test deletion.
    161186        wp_delete_post( $post_id );
    162187        $this->assertNull( wp_get_custom_css_post() );
     
    226251        $original_title = $post->post_title;
    227252
    228         add_filter( 'customize_update_custom_css_post_content_args', array( $this, 'filter_update_post_content_args' ), 10, 3 );
     253        add_filter( 'update_custom_css_data', array( $this, 'filter_update_custom_css_data' ), 10, 3 );
    229254        $this->setting->save();
    230255
     
    239264     * Filter `customize_update_custom_css_post_content_args`.
    240265     *
    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 ) {
     266     * @param array  $data Data.
     267     * @param string $args Args.
     268     * @return array Data.
     269     */
     270    function filter_update_custom_css_data( $data, $args ) {
     271        $this->assertInternalType( 'array', $data );
     272        $this->assertEqualSets( array( 'css', 'preprocessed' ), array_keys( $data ) );
     273        $this->assertEquals( '', $data['preprocessed'] );
    247274        $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;
     275        $this->assertEqualSets( array( 'css', 'preprocessed', 'stylesheet' ), array_keys( $args ) );
     276        $this->assertEquals( $args['css'], $data['css'] );
     277        $this->assertEquals( $args['preprocessed'], $data['preprocessed'] );
     278
     279        $data['css'] .= '/* filtered post_content */';
     280        $data['preprocessed'] = '/* filtered post_content_filtered */';
     281        $data['post_title'] = 'Ignored';
     282        return $data;
    257283    }
    258284
Note: See TracChangeset for help on using the changeset viewer.