Make WordPress Core


Ignore:
Timestamp:
11/23/2016 05:33:21 PM (8 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/theme.php

    r39346 r39350  
    17061706
    17071707    return $css;
     1708}
     1709
     1710/**
     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 string $css CSS, stored in `post_content`.
     1719 * @param array  $args {
     1720 *     Args.
     1721 *
     1722 *     @type string $preprocessed Pre-processed CSS, stored in `post_content_filtered`. 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( $css, $args = array() ) {
     1728    $args = wp_parse_args( $args, array(
     1729        'preprocessed' => '',
     1730        'stylesheet' => get_stylesheet(),
     1731    ) );
     1732
     1733    $data = array(
     1734        'css' => $css,
     1735        'preprocessed' => $args['preprocessed'],
     1736    );
     1737
     1738    /**
     1739     * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated.
     1740     *
     1741     * This filter can be used by plugin that offer CSS pre-processors, to store the original
     1742     * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
     1743     * When used in this way, the `post_content_filtered` should be supplied as the setting value
     1744     * instead of `post_content` via a the `customize_value_custom_css` filter, for example:
     1745     *
     1746     * <code>
     1747     * add_filter( 'customize_value_custom_css', function( $value, $setting ) {
     1748     *     $post = wp_get_custom_css_post( $setting->stylesheet );
     1749     *     if ( $post && ! empty( $post->post_content_filtered ) ) {
     1750     *         $css = $post->post_content_filtered;
     1751     *     }
     1752     *     return $css;
     1753     * }, 10, 2 );
     1754     * </code>
     1755     *
     1756     * @since 4.7.0
     1757     * @param array $data {
     1758     *     Custom CSS data.
     1759     *
     1760     *     @type string $css          CSS stored in `post_content`.
     1761     *     @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. Normally empty string.
     1762     * }
     1763     * @param array $args {
     1764     *     The args passed into `wp_update_custom_css_post()` merged with defaults.
     1765     *
     1766     *     @type string $css          The original CSS passed in to be updated.
     1767     *     @type string $preprocessed The original preprocessed CSS passed in to be updated.
     1768     *     @type string $stylesheet   The stylesheet (theme) being updated.
     1769     * }
     1770     */
     1771    $data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) );
     1772
     1773    $post_data = array(
     1774        'post_title' => $args['stylesheet'],
     1775        'post_name' => sanitize_title( $args['stylesheet'] ),
     1776        'post_type' => 'custom_css',
     1777        'post_status' => 'publish',
     1778        'post_content' => $data['css'],
     1779        'post_content_filtered' => $data['preprocessed'],
     1780    );
     1781
     1782    // Update post if it already exists, otherwise create a new one.
     1783    $post = wp_get_custom_css_post( $args['stylesheet'] );
     1784    if ( $post ) {
     1785        $post_data['ID'] = $post->ID;
     1786        $r = wp_update_post( wp_slash( $post_data ), true );
     1787    } else {
     1788        $r = wp_insert_post( wp_slash( $post_data ), true );
     1789    }
     1790
     1791    if ( $r instanceof WP_Error ) {
     1792        return $r;
     1793    }
     1794    return get_post( $r );
    17081795}
    17091796
Note: See TracChangeset for help on using the changeset viewer.