Make WordPress Core


Ignore:
Timestamp:
11/13/2016 02:42:04 AM (10 years ago)
Author:
westonruter
Message:

Customize: Improve extensibility of Custom CSS.

  • Add customize_value_custom_css filter to WP_Customize_Custom_CSS::value() method.
  • Introduce customize_update_custom_css_post_content_args filter in WP_Customize_Custom_CSS::update() method.
  • Make clear that wp_get_custom_css() and wp_get_custom_css filter are specifically for obtaining the value to render/display. Eliminate use of wp_get_custom_css() when getting the setting value. Use the underlying post_value directly when is_previewed.
  • Move anonymous functions handing JS previewing for custom_logo, custom_css, and background into named functions on the wp.customize.settingPreviewHandlers to allow plugins to override/extend preview logic.
  • Update _custom_background_cb to always print a style tag wen in the customizer preview, and update background preview logic to replace existing style element instead of appending a new style to the head so that background changes don't unexpectedly override any Custom CSS in the preview's stylesheet cascade.

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

File:
1 edited

Legend:

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

    r39185 r39209  
    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.
    … …  
    121124
    122125        /**
    123          * Fetch the value of the setting.
    124          *
    125          * @since 4.7.0
    126          * @access public
     126         * Fetch the value of the setting. Will return the previewed value when `preview()` is called.
     127         *
     128         * @since 4.7.0
     129         * @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        }
    … …  
    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                 * }, 10, 2 );
     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
Note: See TracChangeset for help on using the changeset viewer.