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. Built from https://develop.svn.wordpress.org/trunk@39350 git-svn-id: http://core.svn.wordpress.org/trunk@39290 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
6f189ddbc8
commit
456941a018
|
@ -241,69 +241,18 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
|
|||
* @return int|false The post ID or false if the value could not be saved.
|
||||
*/
|
||||
public function update( $css ) {
|
||||
$setting = $this;
|
||||
|
||||
if ( empty( $css ) ) {
|
||||
$css = '';
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'post_content' => $css,
|
||||
'post_content_filtered' => '',
|
||||
);
|
||||
$r = wp_update_custom_css_post( $css, array(
|
||||
'stylesheet' => $this->stylesheet,
|
||||
) );
|
||||
|
||||
/**
|
||||
* Filters the `post_content` and `post_content_filtered` args for a `custom_css` post being updated.
|
||||
*
|
||||
* This filter can be used by plugin that offer CSS pre-processors, to store the original
|
||||
* pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
|
||||
* When used in this way, the `post_content_filtered` should be supplied as the setting value
|
||||
* instead of `post_content` via a the `customize_value_custom_css` filter, for example:
|
||||
*
|
||||
* <code>
|
||||
* add_filter( 'customize_value_custom_css', function( $value, $setting ) {
|
||||
* $post = wp_get_custom_css_post( $setting->stylesheet );
|
||||
* if ( $post && ! empty( $post->post_content_filtered ) ) {
|
||||
* $css = $post->post_content_filtered;
|
||||
* }
|
||||
* return $css;
|
||||
* }, 10, 2 );
|
||||
* </code>
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @param array $args {
|
||||
* Content post args (unslashed) for `wp_update_post()`/`wp_insert_post()`.
|
||||
*
|
||||
* @type string $post_content CSS.
|
||||
* @type string $post_content_filtered Pre-processed CSS. Normally empty string.
|
||||
* }
|
||||
* @param string $css Original CSS being updated.
|
||||
* @param WP_Customize_Custom_CSS_Setting $setting Custom CSS Setting.
|
||||
*/
|
||||
$args = apply_filters( 'customize_update_custom_css_post_content_args', $args, $css, $setting );
|
||||
$args = wp_array_slice_assoc( $args, array( 'post_content', 'post_content_filtered' ) );
|
||||
|
||||
$args = array_merge(
|
||||
$args,
|
||||
array(
|
||||
'post_title' => $this->stylesheet,
|
||||
'post_name' => sanitize_title( $this->stylesheet ),
|
||||
'post_type' => 'custom_css',
|
||||
'post_status' => 'publish',
|
||||
)
|
||||
);
|
||||
|
||||
// Update post if it already exists, otherwise create a new one.
|
||||
$post = wp_get_custom_css_post( $this->stylesheet );
|
||||
if ( $post ) {
|
||||
$args['ID'] = $post->ID;
|
||||
$post_id = wp_update_post( wp_slash( $args ) );
|
||||
} else {
|
||||
$post_id = wp_insert_post( wp_slash( $args ) );
|
||||
}
|
||||
if ( ! $post_id ) {
|
||||
if ( $r instanceof WP_Error ) {
|
||||
return false;
|
||||
}
|
||||
$post_id = $r->ID;
|
||||
|
||||
// Cache post ID in theme mod for performance to avoid additional DB query.
|
||||
if ( $this->manager->get_stylesheet() === $this->stylesheet ) {
|
||||
|
|
|
@ -1707,6 +1707,93 @@ function wp_get_custom_css( $stylesheet = '' ) {
|
|||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the `custom_css` post for a given theme.
|
||||
*
|
||||
* Inserts a `custom_css` post when one doesn't yet exist.
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $css CSS, stored in `post_content`.
|
||||
* @param array $args {
|
||||
* Args.
|
||||
*
|
||||
* @type string $preprocessed Pre-processed CSS, stored in `post_content_filtered`. Normally empty string. Optional.
|
||||
* @type string $stylesheet Stylesheet (child theme) to update. Optional, defaults to current theme/stylesheet.
|
||||
* }
|
||||
* @return WP_Post|WP_Error Post on success, error on failure.
|
||||
*/
|
||||
function wp_update_custom_css_post( $css, $args = array() ) {
|
||||
$args = wp_parse_args( $args, array(
|
||||
'preprocessed' => '',
|
||||
'stylesheet' => get_stylesheet(),
|
||||
) );
|
||||
|
||||
$data = array(
|
||||
'css' => $css,
|
||||
'preprocessed' => $args['preprocessed'],
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated.
|
||||
*
|
||||
* This filter can be used by plugin that offer CSS pre-processors, to store the original
|
||||
* pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
|
||||
* When used in this way, the `post_content_filtered` should be supplied as the setting value
|
||||
* instead of `post_content` via a the `customize_value_custom_css` filter, for example:
|
||||
*
|
||||
* <code>
|
||||
* add_filter( 'customize_value_custom_css', function( $value, $setting ) {
|
||||
* $post = wp_get_custom_css_post( $setting->stylesheet );
|
||||
* if ( $post && ! empty( $post->post_content_filtered ) ) {
|
||||
* $css = $post->post_content_filtered;
|
||||
* }
|
||||
* return $css;
|
||||
* }, 10, 2 );
|
||||
* </code>
|
||||
*
|
||||
* @since 4.7.0
|
||||
* @param array $data {
|
||||
* Custom CSS data.
|
||||
*
|
||||
* @type string $css CSS stored in `post_content`.
|
||||
* @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. Normally empty string.
|
||||
* }
|
||||
* @param array $args {
|
||||
* The args passed into `wp_update_custom_css_post()` merged with defaults.
|
||||
*
|
||||
* @type string $css The original CSS passed in to be updated.
|
||||
* @type string $preprocessed The original preprocessed CSS passed in to be updated.
|
||||
* @type string $stylesheet The stylesheet (theme) being updated.
|
||||
* }
|
||||
*/
|
||||
$data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) );
|
||||
|
||||
$post_data = array(
|
||||
'post_title' => $args['stylesheet'],
|
||||
'post_name' => sanitize_title( $args['stylesheet'] ),
|
||||
'post_type' => 'custom_css',
|
||||
'post_status' => 'publish',
|
||||
'post_content' => $data['css'],
|
||||
'post_content_filtered' => $data['preprocessed'],
|
||||
);
|
||||
|
||||
// Update post if it already exists, otherwise create a new one.
|
||||
$post = wp_get_custom_css_post( $args['stylesheet'] );
|
||||
if ( $post ) {
|
||||
$post_data['ID'] = $post->ID;
|
||||
$r = wp_update_post( wp_slash( $post_data ), true );
|
||||
} else {
|
||||
$r = wp_insert_post( wp_slash( $post_data ), true );
|
||||
}
|
||||
|
||||
if ( $r instanceof WP_Error ) {
|
||||
return $r;
|
||||
}
|
||||
return get_post( $r );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add callback for custom TinyMCE editor stylesheets.
|
||||
*
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.7-beta4-39349';
|
||||
$wp_version = '4.7-beta4-39350';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue