Make WordPress Core

Ticket #38672: 38672.wp_update_custom_css_post.2.diff

File 38672.wp_update_custom_css_post.2.diff, 4.9 KB (added by westonruter, 8 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 eadd47c..f0c11b7 100644
    final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting { 
    287287                $args = apply_filters( 'customize_update_custom_css_post_content_args', $args, $css, $setting );
    288288                $args = wp_array_slice_assoc( $args, array( 'post_content', 'post_content_filtered' ) );
    289289
    290                 $args = array_merge(
    291                         $args,
    292                         array(
    293                                 'post_title' => $this->stylesheet,
    294                                 'post_name' => sanitize_title( $this->stylesheet ),
    295                                 'post_type' => 'custom_css',
    296                                 'post_status' => 'publish',
    297                         )
    298                 );
     290                $args['stylesheet'] = $this->stylesheet;
     291                $r = wp_update_custom_css_post( $args );
    299292
    300                 // Update post if it already exists, otherwise create a new one.
    301                 $post = wp_get_custom_css_post( $this->stylesheet );
    302                 if ( $post ) {
    303                         $args['ID'] = $post->ID;
    304                         $post_id = wp_update_post( wp_slash( $args ) );
    305                 } else {
    306                         $post_id = wp_insert_post( wp_slash( $args ) );
    307                 }
    308                 if ( ! $post_id ) {
     293                if ( $r instanceof WP_Error ) {
    309294                        return false;
    310295                }
     296                $post_id = $r->ID;
    311297
    312298                // Cache post ID in theme mod for performance to avoid additional DB query.
    313299                if ( $this->manager->get_stylesheet() === $this->stylesheet ) {
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index eea50a5..37309ea 100644
    function wp_get_custom_css( $stylesheet = '' ) { 
    17081708}
    17091709
    17101710/**
     1711 * Update the `custom_css` post for a given theme.
     1712 *
     1713 * Inserts a `custom_css` post when one doesn't yet exist.
     1714 *
     1715 * @since 4.7.0
     1716 * @access public
     1717 *
     1718 * @param array $args {
     1719 *     Custom CSS post args.
     1720 *
     1721 *     @type string $post_content          CSS.
     1722 *     @type string $post_content_filtered Pre-processed CSS. Normally empty string. Optional.
     1723 *     @type string $stylesheet            Stylesheet (child theme) to update. Optional, defaults to current theme/stylesheet.
     1724 * }
     1725 * @return WP_Post|WP_Error Post on success, error on failure.
     1726 */
     1727function wp_update_custom_css_post( $args ) {
     1728        $args = wp_parse_args( $args, array(
     1729                'post_content' => '',
     1730                'post_content_filtered' => '',
     1731                'stylesheet' => get_stylesheet(),
     1732        ) );
     1733
     1734        $args = array_merge(
     1735                $args,
     1736                array(
     1737                        'post_title' => $args['stylesheet'],
     1738                        'post_name' => sanitize_title( $args['stylesheet'] ),
     1739                        'post_type' => 'custom_css',
     1740                        'post_status' => 'publish',
     1741                )
     1742        );
     1743
     1744        // Update post if it already exists, otherwise create a new one.
     1745        $post = wp_get_custom_css_post( $args['stylesheet'] );
     1746        if ( $post ) {
     1747                $args['ID'] = $post->ID;
     1748                $r = wp_update_post( wp_slash( $args ), true );
     1749        } else {
     1750                $r = wp_insert_post( wp_slash( $args ), true );
     1751        }
     1752
     1753        if ( $r instanceof WP_Error ) {
     1754                return $r;
     1755        }
     1756        return get_post( $r );
     1757}
     1758
     1759/**
    17111760 * Add callback for custom TinyMCE editor stylesheets.
    17121761 *
    17131762 * The parameter $stylesheet is the name of the stylesheet, relative to
  • 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 cd82a72..8478447 100644
    class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 
    158158                $this->assertEquals( $previewed_css, $this->setting->value() );
    159159                $this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) );
    160160
     161                // Make sure that wp_update_custom_css_post() works as expected for updates.
     162                $r = wp_update_custom_css_post( array(
     163                        'stylesheet' => $this->setting->stylesheet,
     164                        'post_content' => 'body { color:red; }',
     165                        'post_content_filtered' => "body\n\tcolor:red;",
     166                ) );
     167                $this->assertInstanceOf( 'WP_Post', $r );
     168                $this->assertEquals( $post_id, $r->ID );
     169                $r = wp_update_custom_css_post( array(
     170                        'post_content' => 'body { content: "\o/"; }',
     171                ) );
     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
     175                // Make sure that wp_update_custom_css_post() works as expected for insertion.
     176                $r = wp_update_custom_css_post( array(
     177                        'stylesheet' => 'other',
     178                        'post_content' => 'body { background:black; }',
     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() );
    163188                $this->assertNull( wp_get_custom_css_post( get_stylesheet() ) );