Make WordPress Core

Ticket #38672: 38672.extensibility.2.diff

File 38672.extensibility.2.diff, 8.9 KB (added by westonruter, 8 years ago)

https://github.com/xwp/wordpress-develop/pull/199/commits/7502bf44d85472f88802d85e75bfeeb02f60f0da

  • 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 ba4a58c..4693ace 100644
    final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting { 
    100100        }
    101101
    102102        /**
    103          * Filter wp_get_custom_css for applying customized value to return value.
     103         * Filter `wp_get_custom_css` for applying the customized value.
     104         *
     105         * This is used in the preview when `wp_get_custom_css()` is called for rendering the styles.
    104106         *
    105107         * @since 4.7.0
    106108         * @access private
     109         * @see wp_get_custom_css()
    107110         *
    108111         * @param string $css        Original CSS.
    109112         * @param string $stylesheet Current stylesheet.
    final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting { 
    120123        }
    121124
    122125        /**
    123          * Fetch the value of the setting.
     126         * Fetch the value of the setting. Will return the previewed value when `preview()` is called.
    124127         *
    125128         * @since 4.7.0
    126129         * @access public
     130         * @see WP_Customize_Setting::value()
    127131         *
    128132         * @return string
    129133         */
    130134        public function value() {
    131                 $value = wp_get_custom_css( $this->stylesheet );
     135                $id_base = $this->id_data['base'];
     136                if ( $this->is_previewed && null !== $this->post_value( null ) ) {
     137                        return $this->post_value();
     138                }
     139                $value = '';
     140                $post = wp_get_custom_css_post( $this->stylesheet );
     141                if ( $post ) {
     142                        $value = $post->post_content;
     143                }
    132144                if ( empty( $value ) ) {
    133145                        $value = $this->default;
    134146                }
     147
     148                /** This filter is documented in wp-includes/class-wp-customize-setting.php */
     149                $value = apply_filters( "customize_value_{$id_base}", $value, $this );
     150
    135151                return $value;
    136152        }
    137153
    final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting { 
    226242         * @return int|false The post ID or false if the value could not be saved.
    227243         */
    228244        public function update( $css ) {
     245                $setting = $this;
     246
     247                if ( empty( $css ) ) {
     248                        $css = '';
     249                }
    229250
    230251                $args = array(
    231                         'post_content' => $css ? $css : '',
    232                         'post_title' => $this->stylesheet,
    233                         'post_name' => sanitize_title( $this->stylesheet ),
    234                         'post_type' => 'custom_css',
    235                         'post_status' => 'publish',
     252                        'post_content' => $css,
     253                        'post_content_filtered' => '',
     254                );
     255
     256                /**
     257                 * Filters the `post_content` and `post_content_filtered` args for a `custom_css` post being updated.
     258                 *
     259                 * This filter can be used by plugin that offer CSS pre-processors, to store the original
     260                 * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
     261                 * When used in this way, the `post_content_filtered` should be supplied as the setting value
     262                 * instead of `post_content` via a the `customize_value_custom_css` filter, for example:
     263                 *
     264                 * <code>
     265                 * add_filter( 'customize_value_custom_css', function( $value, $setting ) {
     266                 *     $post = wp_get_custom_css_post( $setting->stylesheet );
     267                 *     if ( $post && ! empty( $post->post_content_filtered ) ) {
     268                 *         $css = $post->post_content_filtered;
     269                 *     }
     270                 *     return $css;
     271                 * }
     272                 * </code>
     273                 *
     274                 * @since 4.7.0
     275                 * @param array  $args {
     276                 *     Content post args (unslashed) for wp_update_post()/wp_insert_post().
     277                 *
     278                 *     @type string $post_content          CSS.
     279                 *     @type string $post_content_filtered Pre-processed CSS. Normally empty string.
     280                 * }
     281                 * @param string                          $css     Original CSS being updated.
     282                 * @param WP_Customize_Custom_CSS_Setting $setting Custom CSS Setting.
     283                 */
     284                $args = apply_filters( 'customize_update_custom_css_post_content_args', $args, $css, $setting );
     285                $args = wp_array_slice_assoc( $args, array( 'post_content', 'post_content_filtered' ) );
     286
     287                $args = array_merge(
     288                        $args,
     289                        array(
     290                                'post_title' => $this->stylesheet,
     291                                'post_name' => sanitize_title( $this->stylesheet ),
     292                                'post_type' => 'custom_css',
     293                                'post_status' => 'publish',
     294                        )
    236295                );
    237296
    238297                // Update post if it already exists, otherwise create a new one.
  • src/wp-includes/js/customize-preview.js

    diff --git src/wp-includes/js/customize-preview.js src/wp-includes/js/customize-preview.js
    index be24e41..9a3944f 100644
     
    594594                };
    595595        } )();
    596596
     597        api.settingPreviewHandlers = {
     598
     599                /**
     600                 * Preview changes to custom logo.
     601                 *
     602                 * @param {number} attachmentId Attachment ID for custom logo.
     603                 * @returns {void}
     604                 */
     605                custom_logo: function( attachmentId ) {
     606                        $( 'body' ).toggleClass( 'wp-custom-logo', !! attachmentId );
     607                },
     608
     609                /**
     610                 * Preview changes to custom css.
     611                 *
     612                 * @param {string} value Custom CSS..
     613                 * @returns {void}
     614                 */
     615                custom_css: function( value ) {
     616                        $( '#wp-custom-css' ).text( value );
     617                },
     618
     619                /**
     620                 * Preview changes to any of the background settings.
     621                 *
     622                 * @returns {void}
     623                 */
     624                background: function() {
     625                        var css = '', settings = {};
     626
     627                        _.each( ['color', 'image', 'preset', 'position_x', 'position_y', 'size', 'repeat', 'attachment'], function( prop ) {
     628                                settings[ prop ] = api( 'background_' + prop );
     629                        } );
     630
     631                        /*
     632                         * The body will support custom backgrounds if either the color or image are set.
     633                         *
     634                         * See get_body_class() in /wp-includes/post-template.php
     635                         */
     636                        $( document.body ).toggleClass( 'custom-background', !! ( settings.color() || settings.image() ) );
     637
     638                        if ( settings.color() ) {
     639                                css += 'background-color: ' + settings.color() + ';';
     640                        }
     641
     642                        if ( settings.image() ) {
     643                                css += 'background-image: url("' + settings.image() + '");';
     644                                css += 'background-size: ' + settings.size() + ';';
     645                                css += 'background-position: ' + settings.position_x() + ' ' + settings.position_y() + ';';
     646                                css += 'background-repeat: ' + settings.repeat() + ';';
     647                                css += 'background-attachment: ' + settings.attachment() + ';';
     648                        }
     649
     650                        $( '#custom-background-css' ).text( 'body.custom-background { ' + css + ' }' );
     651                }
     652        };
     653
    597654        $( function() {
    598655                var bg, setValue;
    599656
     
    759816                        return 'background_' + prop;
    760817                } );
    761818
    762                 api.when.apply( api, bg ).done( function( color, image, preset, positionX, positionY, size, repeat, attachment ) {
    763                         var body = $(document.body),
    764                                 head = $('head'),
    765                                 style = $('#custom-background-css'),
    766                                 update;
    767 
    768                         update = function() {
    769                                 var css = '';
    770 
    771                                 // The body will support custom backgrounds if either
    772                                 // the color or image are set.
    773                                 //
    774                                 // See get_body_class() in /wp-includes/post-template.php
    775                                 body.toggleClass( 'custom-background', !! ( color() || image() ) );
    776 
    777                                 if ( color() )
    778                                         css += 'background-color: ' + color() + ';';
    779 
    780                                 if ( image() ) {
    781                                         css += 'background-image: url("' + image() + '");';
    782                                         css += 'background-size: ' + size() + ';';
    783                                         css += 'background-position: ' + positionX() + ' ' + positionY() + ';';
    784                                         css += 'background-repeat: ' + repeat() + ';';
    785                                         css += 'background-attachment: ' + attachment() + ';';
    786                                 }
    787 
    788                                 // Refresh the stylesheet by removing and recreating it.
    789                                 style.remove();
    790                                 style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
    791                         };
    792 
     819                api.when.apply( api, bg ).done( function() {
    793820                        $.each( arguments, function() {
    794                                 this.bind( update );
     821                                this.bind( api.settingPreviewHandlers.background );
    795822                        });
    796823                });
    797824
     
    802829                 *
    803830                 * @since 4.5.0
    804831                 */
    805                 api( 'custom_logo', function( setting ) {
    806                         $( 'body' ).toggleClass( 'wp-custom-logo', !! setting.get() );
    807                         setting.bind( function( attachmentId ) {
    808                                 $( 'body' ).toggleClass( 'wp-custom-logo', !! attachmentId );
    809                         });
    810                 });
     832                api( 'custom_logo', function ( setting ) {
     833                        api.settingPreviewHandlers.custom_logo.call( setting, setting.get() );
     834                        setting.bind( api.settingPreviewHandlers.custom_logo );
     835                } );
    811836
    812                 api( 'custom_css[' + api.settings.theme.stylesheet + ']', function( value ) {
    813                         value.bind( function( to ) {
    814                                 $( '#wp-custom-css' ).text( to );
    815                         } );
     837                api( 'custom_css[' + api.settings.theme.stylesheet + ']', function( setting ) {
     838                        setting.bind( api.settingPreviewHandlers.custom_css );
    816839                } );
    817840
    818841                api.trigger( 'preview-ready' );
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index baad12f..bf1c55c 100644
    function _custom_background_cb() { 
    15001500                $color = false;
    15011501        }
    15021502
    1503         if ( ! $background && ! $color )
     1503        if ( ! $background && ! $color ) {
     1504                if ( is_customize_preview() ) {
     1505                        echo '<style type="text/css" id="custom-background-css"></style>';
     1506                }
    15041507                return;
     1508        }
    15051509
    15061510        $style = $color ? "background-color: #$color;" : '';
    15071511
    function wp_get_custom_css_post( $stylesheet = '' ) { 
    16211625}
    16221626
    16231627/**
    1624  * Fetch the saved Custom CSS content.
     1628 * Fetch the saved Custom CSS content for rendering.
    16251629 *
    16261630 * @since 4.7.0
    16271631 * @access public