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:
@@ -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 ) {
|
||||
|
||||
Reference in New Issue
Block a user