Make WordPress Core

Changeset 39185


Ignore:
Timestamp:
11/09/2016 08:42:22 PM (7 years ago)
Author:
westonruter
Message:

Customize: Split out custom_css query logic from wp_get_custom_css() into a re-usable wp_get_custom_css_post() function to also be used when updating.

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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/customize/class-wp-customize-custom-css-setting.php

    r39175 r39185  
    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 {
  • trunk/src/wp-includes/theme.php

    r39181 r39185  
    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
     
    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.
    1588  */
    1589 function wp_get_custom_css( $stylesheet = '' ) {
    1590     $css = '';
    1591 
     1583 * @return WP_Post|null The custom_css post or null if none exists.
     1584 */
     1585function wp_get_custom_css_post( $stylesheet = '' ) {
    15921586    if ( empty( $stylesheet ) ) {
    15931587        $stylesheet = get_stylesheet();
     
    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,
     
    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().
     
    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;
  • trunk/tests/phpunit/tests/customize/custom-css-setting.php

    r39175 r39185  
    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    }
     
    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
     
    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,
    121125            'post_status' => 'publish',
    122126            'post_type' => 'custom_css',
    123127        ) );
     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,
     133            'post_status' => 'publish',
     134            'post_type' => 'custom_css',
     135        ) );
     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; }';
     
    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
Note: See TracChangeset for help on using the changeset viewer.