Ticket #38672: 38672.wp_update_custom_css_post.3.diff
File 38672.wp_update_custom_css_post.3.diff, 9.7 KB (added by , 8 years ago) |
---|
-
src/wp-includes/customize/class-wp-customize-custom-css-setting.php
diff --git src/wp-includes/customize/class-wp-customize-custom-css-setting.php src/wp-includes/customize/class-wp-customize-custom-css-setting.php index eadd47c..5a7897a 100644
final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting { 245 245 * @return int|false The post ID or false if the value could not be saved. 246 246 */ 247 247 public function update( $css ) { 248 $setting = $this;249 250 248 if ( empty( $css ) ) { 251 249 $css = ''; 252 250 } … … final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting { 256 254 'post_content_filtered' => '', 257 255 ); 258 256 259 /** 260 * Filters the `post_content` and `post_content_filtered` args for a `custom_css` post being updated. 261 * 262 * This filter can be used by plugin that offer CSS pre-processors, to store the original 263 * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`. 264 * When used in this way, the `post_content_filtered` should be supplied as the setting value 265 * instead of `post_content` via a the `customize_value_custom_css` filter, for example: 266 * 267 * <code> 268 * add_filter( 'customize_value_custom_css', function( $value, $setting ) { 269 * $post = wp_get_custom_css_post( $setting->stylesheet ); 270 * if ( $post && ! empty( $post->post_content_filtered ) ) { 271 * $css = $post->post_content_filtered; 272 * } 273 * return $css; 274 * }, 10, 2 ); 275 * </code> 276 * 277 * @since 4.7.0 278 * @param array $args { 279 * Content post args (unslashed) for `wp_update_post()`/`wp_insert_post()`. 280 * 281 * @type string $post_content CSS. 282 * @type string $post_content_filtered Pre-processed CSS. Normally empty string. 283 * } 284 * @param string $css Original CSS being updated. 285 * @param WP_Customize_Custom_CSS_Setting $setting Custom CSS Setting. 286 */ 287 $args = apply_filters( 'customize_update_custom_css_post_content_args', $args, $css, $setting ); 288 $args = wp_array_slice_assoc( $args, array( 'post_content', 'post_content_filtered' ) ); 289 290 $args = array_merge( 291 $args, 292 array( 293 'post_title' => $this->stylesheet, 294 'post_name' => sanitize_title( $this->stylesheet ), 295 'post_type' => 'custom_css', 296 'post_status' => 'publish', 297 ) 298 ); 257 $args['stylesheet'] = $this->stylesheet; 258 $r = wp_update_custom_css_post( $css, $args ); 299 259 300 // Update post if it already exists, otherwise create a new one. 301 $post = wp_get_custom_css_post( $this->stylesheet ); 302 if ( $post ) { 303 $args['ID'] = $post->ID; 304 $post_id = wp_update_post( wp_slash( $args ) ); 305 } else { 306 $post_id = wp_insert_post( wp_slash( $args ) ); 307 } 308 if ( ! $post_id ) { 260 if ( $r instanceof WP_Error ) { 309 261 return false; 310 262 } 263 $post_id = $r->ID; 311 264 312 265 // Cache post ID in theme mod for performance to avoid additional DB query. 313 266 if ( $this->manager->get_stylesheet() === $this->stylesheet ) { -
src/wp-includes/theme.php
diff --git src/wp-includes/theme.php src/wp-includes/theme.php index eea50a5..36633f4 100644
function wp_get_custom_css( $stylesheet = '' ) { 1708 1708 } 1709 1709 1710 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 */ 1727 function 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 string $stylesheet The stylesheet (theme) being updated. 1764 */ 1765 $data = apply_filters( 'update_custom_css_data', $data, $args['stylesheet'] ); 1766 1767 $post_data = array( 1768 'post_title' => $args['stylesheet'], 1769 'post_name' => sanitize_title( $args['stylesheet'] ), 1770 'post_type' => 'custom_css', 1771 'post_status' => 'publish', 1772 'post_content' => $data['css'], 1773 'post_content_filtered' => $data['preprocessed'], 1774 ); 1775 1776 // Update post if it already exists, otherwise create a new one. 1777 $post = wp_get_custom_css_post( $args['stylesheet'] ); 1778 if ( $post ) { 1779 $post_data['ID'] = $post->ID; 1780 $r = wp_update_post( wp_slash( $post_data ), true ); 1781 } else { 1782 $r = wp_insert_post( wp_slash( $post_data ), true ); 1783 } 1784 1785 if ( $r instanceof WP_Error ) { 1786 return $r; 1787 } 1788 return get_post( $r ); 1789 } 1790 1791 /** 1711 1792 * Add callback for custom TinyMCE editor stylesheets. 1712 1793 * 1713 1794 * The parameter $stylesheet is the name of the stylesheet, relative to -
tests/phpunit/tests/customize/custom-css-setting.php
diff --git tests/phpunit/tests/customize/custom-css-setting.php tests/phpunit/tests/customize/custom-css-setting.php index cd82a72..cd03817 100644
class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 158 158 $this->assertEquals( $previewed_css, $this->setting->value() ); 159 159 $this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) ); 160 160 161 // Make sure that wp_update_custom_css_post() works as expected for updates. 162 $r = wp_update_custom_css_post( 'body { color:red; }', array( 163 'stylesheet' => $this->setting->stylesheet, 164 'preprocessed' => "body\n\tcolor:red;", 165 ) ); 166 $this->assertInstanceOf( 'WP_Post', $r ); 167 $this->assertEquals( $post_id, $r->ID ); 168 $r = wp_update_custom_css_post( 'body { content: "\o/"; }' ); 169 $this->assertEquals( $this->wp_customize->get_stylesheet(), get_post( $r )->post_name ); 170 $this->assertEquals( 'body { content: "\o/"; }', get_post( $r )->post_content ); 171 172 // Make sure that wp_update_custom_css_post() works as expected for insertion. 173 $r = wp_update_custom_css_post( 'body { background:black; }', array( 174 'stylesheet' => 'other', 175 ) ); 176 $this->assertInstanceOf( 'WP_Post', $r ); 177 $this->assertEquals( 'other', get_post( $r )->post_name ); 178 $this->assertEquals( 'body { background:black; }', get_post( $r )->post_content ); 179 $this->assertEquals( 'publish', get_post( $r )->post_status ); 180 181 // Test deletion. 161 182 wp_delete_post( $post_id ); 162 183 $this->assertNull( wp_get_custom_css_post() ); 163 184 $this->assertNull( wp_get_custom_css_post( get_stylesheet() ) ); … … class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 225 246 $post = get_post( $post_id ); 226 247 $original_title = $post->post_title; 227 248 228 add_filter( ' customize_update_custom_css_post_content_args', array( $this, 'filter_update_post_content_args' ), 10, 3 );249 add_filter( 'update_custom_css_data', array( $this, 'filter_update_custom_css_data' ), 10, 3 ); 229 250 $this->setting->save(); 230 251 231 252 $post = get_post( $post_id ); … … class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 238 259 /** 239 260 * Filter `customize_update_custom_css_post_content_args`. 240 261 * 241 * @param array $args Post array. 242 * @param string $css CSS. 243 * @param WP_Customize_Setting $setting Setting. 244 * @return array Args. 262 * @param array $data Data. 263 * @param string $stylesheet Stylesheet. 264 * @return array Data. 245 265 */ 246 function filter_update_post_content_args( $args, $css, $setting ) { 247 $this->assertInternalType( 'array', $args ); 248 $this->assertEqualSets( array( 'post_content', 'post_content_filtered' ), array_keys( $args ) ); 249 $this->assertEquals( $css, $args['post_content'] ); 250 $this->assertEquals( '', $args['post_content_filtered'] ); 251 $this->assertInstanceOf( 'WP_Customize_Custom_CSS_Setting', $setting ); 252 253 $args['post_content'] .= '/* filtered post_content */'; 254 $args['post_content_filtered'] = '/* filtered post_content_filtered */'; 255 $args['post_title'] = 'Ignored'; 256 return $args; 266 function filter_update_custom_css_data( $data, $stylesheet ) { 267 $this->assertInternalType( 'array', $data ); 268 $this->assertEqualSets( array( 'css', 'preprocessed' ), array_keys( $data ) ); 269 $this->assertEquals( '', $data['preprocessed'] ); 270 $this->assertInternalType( 'string', $stylesheet ); 271 272 $data['css'] .= '/* filtered post_content */'; 273 $data['preprocessed'] = '/* filtered post_content_filtered */'; 274 $data['post_title'] = 'Ignored'; 275 return $data; 257 276 } 258 277 259 278 /**