Make WordPress Core


Ignore:
Timestamp:
11/23/2016 05:33:21 PM (7 years ago)
Author:
westonruter
Message:

Customize: Refactor logic for updating custom_css posts by introducing wp_update_custom_css_post() function and renaming update filter.

  • Moves logic from WP_Customize_Custom_CSS_Setting::update() into a re-usable wp_update_custom_css_post() function, useful for future REST API endpoint, WP-CLI command, or plugin migrations.
  • Renames customize_update_custom_css_post_content_args filter to update_custom_css_data and improves the naming of the parameters. Instead of passing post_content and post_content_filtered the filtered array now contains css and preprocessed respectively.
  • The second context param for the update_custom_css_data filter is now an array of the original args passed to wp_update_custom_css_post() and there is now no more $setting arg since it isn't necessarily being called in the customizer context.

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

File:
1 edited

Legend:

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

    r39345 r39350  
    242242     */
    243243    public function update( $css ) {
    244         $setting = $this;
    245 
    246244        if ( empty( $css ) ) {
    247245            $css = '';
    248246        }
    249247
    250         $args = array(
    251             'post_content' => $css,
    252             'post_content_filtered' => '',
    253         );
    254 
    255         /**
    256          * Filters the `post_content` and `post_content_filtered` args for a `custom_css` post being updated.
    257          *
    258          * This filter can be used by plugin that offer CSS pre-processors, to store the original
    259          * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
    260          * When used in this way, the `post_content_filtered` should be supplied as the setting value
    261          * instead of `post_content` via a the `customize_value_custom_css` filter, for example:
    262          *
    263          * <code>
    264          * add_filter( 'customize_value_custom_css', function( $value, $setting ) {
    265          *     $post = wp_get_custom_css_post( $setting->stylesheet );
    266          *     if ( $post && ! empty( $post->post_content_filtered ) ) {
    267          *         $css = $post->post_content_filtered;
    268          *     }
    269          *     return $css;
    270          * }, 10, 2 );
    271          * </code>
    272          *
    273          * @since 4.7.0
    274          * @param array  $args {
    275          *     Content post args (unslashed) for `wp_update_post()`/`wp_insert_post()`.
    276          *
    277          *     @type string $post_content          CSS.
    278          *     @type string $post_content_filtered Pre-processed CSS. Normally empty string.
    279          * }
    280          * @param string                          $css     Original CSS being updated.
    281          * @param WP_Customize_Custom_CSS_Setting $setting Custom CSS Setting.
    282          */
    283         $args = apply_filters( 'customize_update_custom_css_post_content_args', $args, $css, $setting );
    284         $args = wp_array_slice_assoc( $args, array( 'post_content', 'post_content_filtered' ) );
    285 
    286         $args = array_merge(
    287             $args,
    288             array(
    289                 'post_title' => $this->stylesheet,
    290                 'post_name' => sanitize_title( $this->stylesheet ),
    291                 'post_type' => 'custom_css',
    292                 'post_status' => 'publish',
    293             )
    294         );
    295 
    296         // Update post if it already exists, otherwise create a new one.
    297         $post = wp_get_custom_css_post( $this->stylesheet );
    298         if ( $post ) {
    299             $args['ID'] = $post->ID;
    300             $post_id = wp_update_post( wp_slash( $args ) );
    301         } else {
    302             $post_id = wp_insert_post( wp_slash( $args ) );
    303         }
    304         if ( ! $post_id ) {
     248        $r = wp_update_custom_css_post( $css, array(
     249            'stylesheet' => $this->stylesheet,
     250        ) );
     251
     252        if ( $r instanceof WP_Error ) {
    305253            return false;
    306254        }
     255        $post_id = $r->ID;
    307256
    308257        // Cache post ID in theme mod for performance to avoid additional DB query.
Note: See TracChangeset for help on using the changeset viewer.