WordPress.org

Make WordPress Core

Ticket #38672: 38672.wp_get_custom_css_post.2.diff

File 38672.wp_get_custom_css_post.2.diff, 7.3 KB (added by westonruter, 3 years ago)
  • src/wp-includes/customize/class-wp-customize-custom-css-setting.php

    diff --git src/wp-includes/customize/class-wp-customize-custom-css-setting.php src/wp-includes/customize/class-wp-customize-custom-css-setting.php
    index 034e081..ba4a58c 100644
    final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting { 
    236236                );
    237237
    238238                // Update post if it already exists, otherwise create a new one.
    239                 $post_id = null;
    240                 $query = new WP_Query( array(
    241                         'post_type' => 'custom_css',
    242                         'post_status' => get_post_stati(),
    243                         'name' => sanitize_title( $this->stylesheet ),
    244                         'number' => 1,
    245                         'no_found_rows' => true,
    246                         'cache_results' => true,
    247                         'update_post_meta_cache' => false,
    248                         'update_term_meta_cache' => false,
    249                         'suppress_filters' => true,
    250                 ) );
    251                 if ( ! empty( $query->post ) ) {
    252                         $args['ID'] = $query->post->ID;
     239                $post = wp_get_custom_css_post( $this->stylesheet );
     240                if ( $post ) {
     241                        $args['ID'] = $post->ID;
    253242                        $post_id = wp_update_post( wp_slash( $args ) );
    254243                } else {
    255244                        $post_id = wp_insert_post( wp_slash( $args ) );
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index 646b2e4..baad12f 100644
    function wp_custom_css_cb() { 
    15741574}
    15751575
    15761576/**
    1577  * Fetch the saved Custom CSS content.
    1578  *
    1579  * Gets the content of a Custom CSS post that matches the
    1580  * current theme.
     1577 * Fetch the `custom_css` post for a given theme.
    15811578 *
    15821579 * @since 4.7.0
    15831580 * @access public
    15841581 *
    15851582 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
    1586  *
    1587  * @return string The Custom CSS Post content.
     1583 * @return WP_Post|null The custom_css post or null if none exists.
    15881584 */
    1589 function wp_get_custom_css( $stylesheet = '' ) {
    1590         $css = '';
    1591 
     1585function wp_get_custom_css_post( $stylesheet = '' ) {
    15921586        if ( empty( $stylesheet ) ) {
    15931587                $stylesheet = get_stylesheet();
    15941588        }
    15951589
    15961590        $custom_css_query_vars = array(
    1597                 'post_type' => 'custom_css',
    1598                 'post_status' => get_post_stati(),
    1599                 'name' => sanitize_title( $stylesheet ),
    1600                 'number' => 1,
    1601                 'no_found_rows' => true,
    1602                 'cache_results' => true,
     1591                'post_type'              => 'custom_css',
     1592                'post_status'            => get_post_stati(),
     1593                'name'                   => sanitize_title( $stylesheet ),
     1594                'number'                 => 1,
     1595                'no_found_rows'          => true,
     1596                'cache_results'          => true,
    16031597                'update_post_meta_cache' => false,
    16041598                'update_term_meta_cache' => false,
    16051599        );
    function wp_get_custom_css( $stylesheet = '' ) { 
    16071601        $post = null;
    16081602        if ( get_stylesheet() === $stylesheet ) {
    16091603                $post_id = get_theme_mod( 'custom_css_post_id' );
    1610                 if ( ! $post_id ) {
     1604                if ( ! $post_id || ! get_post( $post_id ) ) {
    16111605                        $query = new WP_Query( $custom_css_query_vars );
    16121606                        $post = $query->post;
    1613 
    16141607                        /*
    16151608                         * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update().
    16161609                         * @todo This should get cleared if a custom_css post is added/removed.
    function wp_get_custom_css( $stylesheet = '' ) { 
    16241617                $post = $query->post;
    16251618        }
    16261619
     1620        return $post;
     1621}
     1622
     1623/**
     1624 * Fetch the saved Custom CSS content.
     1625 *
     1626 * @since 4.7.0
     1627 * @access public
     1628 *
     1629 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme.
     1630 * @return string The Custom CSS Post content.
     1631 */
     1632function wp_get_custom_css( $stylesheet = '' ) {
     1633        $css = '';
     1634
     1635        if ( empty( $stylesheet ) ) {
     1636                $stylesheet = get_stylesheet();
     1637        }
     1638
     1639        $post = wp_get_custom_css_post( $stylesheet );
    16271640        if ( $post ) {
    16281641                $css = $post->post_content;
    16291642        }
  • tests/phpunit/tests/customize/custom-css-setting.php

    diff --git tests/phpunit/tests/customize/custom-css-setting.php tests/phpunit/tests/customize/custom-css-setting.php
    index fea0976..63c7c81 100644
    class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 
    4545                $wp_customize = $this->wp_customize;
    4646
    4747                do_action( 'customize_register', $this->wp_customize );
    48                 $this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentysixteen]' );
     48                $this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[' . get_stylesheet() . ']' );
    4949                $this->wp_customize->add_setting( $this->setting );
    5050        }
    5151
    class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 
    7878        function test_construct() {
    7979                $this->assertTrue( post_type_exists( 'custom_css' ) );
    8080                $this->assertEquals( 'custom_css', $this->setting->type );
    81                 $this->assertEquals( 'twentysixteen', $this->setting->stylesheet );
     81                $this->assertEquals( get_stylesheet(), $this->setting->stylesheet );
    8282                $this->assertEquals( 'edit_css', $this->setting->capability );
    8383
    8484                $exception = null;
    class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 
    113113                $this->setting->default = '/* Hello World */';
    114114                $this->assertEquals( $this->setting->default, $this->setting->value() );
    115115
     116                $this->assertNull( wp_get_custom_css_post() );
     117                $this->assertNull( wp_get_custom_css_post( $this->setting->stylesheet ) );
     118                $this->assertNull( wp_get_custom_css_post( 'twentyten' ) );
     119
    116120                $original_css = 'body { color: black; }';
    117                 $this->factory()->post->create( array(
     121                $post_id = $this->factory()->post->create( array(
    118122                        'post_title' => $this->setting->stylesheet,
    119123                        'post_name' => $this->setting->stylesheet,
    120                         'post_content' => 'body { color: black; }',
     124                        'post_content' => $original_css,
     125                        'post_status' => 'publish',
     126                        'post_type' => 'custom_css',
     127                ) );
     128                $twentyten_css = 'body { color: red; }';
     129                $twentyten_post_id = $this->factory()->post->create( array(
     130                        'post_title' => 'twentyten',
     131                        'post_name' => 'twentyten',
     132                        'post_content' => $twentyten_css,
    121133                        'post_status' => 'publish',
    122134                        'post_type' => 'custom_css',
    123135                ) );
     136                $twentyten_setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentyten]' );
     137
     138                $this->assertEquals( $post_id, wp_get_custom_css_post()->ID );
     139                $this->assertEquals( $post_id, wp_get_custom_css_post( $this->setting->stylesheet )->ID );
     140                $this->assertEquals( $twentyten_post_id, wp_get_custom_css_post( 'twentyten' )->ID );
    124141
    125142                $this->assertEquals( $original_css, wp_get_custom_css( $this->setting->stylesheet ) );
    126143                $this->assertEquals( $original_css, $this->setting->value() );
     144                $this->assertEquals( $twentyten_css, wp_get_custom_css( 'twentyten' ) );
     145                $this->assertEquals( $twentyten_css, $twentyten_setting->value() );
    127146
    128147                $updated_css = 'body { color: blue; }';
    129148                $this->wp_customize->set_post_value( $this->setting->id, $updated_css );
    class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 
    138157                $this->setting->preview();
    139158                $this->assertEquals( $previewed_css, $this->setting->value() );
    140159                $this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) );
     160
     161                wp_delete_post( $post_id );
     162                $this->assertNull( wp_get_custom_css_post() );
     163                $this->assertNull( wp_get_custom_css_post( get_stylesheet() ) );
     164                $this->assertEquals( $previewed_css, wp_get_custom_css( get_stylesheet() ), 'Previewed value remains in spite of deleted post.' );
     165                wp_delete_post( $twentyten_post_id );
     166                $this->assertNull( wp_get_custom_css_post( 'twentyten' ) );
     167                $this->assertEquals( '', wp_get_custom_css( 'twentyten' ) );
    141168        }
    142169
    143170        /**