/** * Test crud methods on WP_Customize_Custom_CSS_Setting. * * @covers wp_get_custom_css() * @covers WP_Customize_Custom_CSS_Setting::value() * @covers WP_Customize_Custom_CSS_Setting::preview() * @covers WP_Customize_Custom_CSS_Setting::update() */ function test_crud() { $this->setting->default = '/* Hello World */'; $this->assertEquals($this->setting->default, $this->setting->value()); $this->assertNull(wp_get_custom_css_post()); $this->assertNull(wp_get_custom_css_post($this->setting->stylesheet)); $this->assertNull(wp_get_custom_css_post('twentyten')); $original_css = 'body { color: black; }'; $post_id = $this->factory()->post->create(array('post_title' => $this->setting->stylesheet, 'post_name' => $this->setting->stylesheet, 'post_content' => $original_css, 'post_status' => 'publish', 'post_type' => 'custom_css')); $twentyten_css = 'body { color: red; }'; $twentyten_post_id = $this->factory()->post->create(array('post_title' => 'twentyten', 'post_name' => 'twentyten', 'post_content' => $twentyten_css, 'post_status' => 'publish', 'post_type' => 'custom_css')); $twentyten_setting = new WP_Customize_Custom_CSS_Setting($this->wp_customize, 'custom_css[twentyten]'); $this->assertEquals($post_id, wp_get_custom_css_post()->ID); $this->assertEquals($post_id, wp_get_custom_css_post($this->setting->stylesheet)->ID); $this->assertEquals($twentyten_post_id, wp_get_custom_css_post('twentyten')->ID); $this->assertEquals($original_css, wp_get_custom_css($this->setting->stylesheet)); $this->assertEquals($original_css, $this->setting->value()); $this->assertEquals($twentyten_css, wp_get_custom_css('twentyten')); $this->assertEquals($twentyten_css, $twentyten_setting->value()); $updated_css = 'body { color: blue; }'; $this->wp_customize->set_post_value($this->setting->id, $updated_css); $saved = $this->setting->save(); $this->assertTrue(false !== $saved); $this->assertEquals($updated_css, $this->setting->value()); $this->assertEquals($updated_css, wp_get_custom_css($this->setting->stylesheet)); $this->assertEquals($updated_css, get_post($post_id)->post_content); $previewed_css = 'body { color: red; }'; $this->wp_customize->set_post_value($this->setting->id, $previewed_css); $this->setting->preview(); $this->assertEquals($previewed_css, $this->setting->value()); $this->assertEquals($previewed_css, wp_get_custom_css($this->setting->stylesheet)); // Make sure that wp_update_custom_css_post() works as expected for updates. $r = wp_update_custom_css_post('body { color:red; }', array('stylesheet' => $this->setting->stylesheet, 'preprocessed' => "body\n\tcolor:red;")); $this->assertInstanceOf('WP_Post', $r); $this->assertEquals($post_id, $r->ID); $this->assertEquals('body { color:red; }', get_post($r)->post_content); $this->assertEquals("body\n\tcolor:red;", get_post($r)->post_content_filtered); $r = wp_update_custom_css_post('body { content: "\\o/"; }'); $this->assertEquals($this->wp_customize->get_stylesheet(), get_post($r)->post_name); $this->assertEquals('body { content: "\\o/"; }', get_post($r)->post_content); $this->assertEquals('', get_post($r)->post_content_filtered); // Make sure that wp_update_custom_css_post() works as expected for insertion. $r = wp_update_custom_css_post('body { background:black; }', array('stylesheet' => 'other')); $this->assertInstanceOf('WP_Post', $r); $this->assertEquals('other', get_post($r)->post_name); $this->assertEquals('body { background:black; }', get_post($r)->post_content); $this->assertEquals('publish', get_post($r)->post_status); // Test deletion. wp_delete_post($post_id); $this->assertNull(wp_get_custom_css_post()); $this->assertNull(wp_get_custom_css_post(get_stylesheet())); $this->assertEquals($previewed_css, wp_get_custom_css(get_stylesheet()), 'Previewed value remains in spite of deleted post.'); wp_delete_post($twentyten_post_id); $this->assertNull(wp_get_custom_css_post('twentyten')); $this->assertEquals('', wp_get_custom_css('twentyten')); }
/** * Import Custom CSS. * * @since 3.5.0 */ function blue_planet_import_custom_css() { // Bail if not WP 4.7. if (!function_exists('wp_get_custom_css_post')) { return; } $custom_css = blue_planet_get_option('custom_css'); // Bail if there is no Custom CSS. if (empty($custom_css)) { return; } $core_css = wp_get_custom_css(); $return = wp_update_custom_css_post($core_css . $custom_css); if (!is_wp_error($return)) { // Remove from theme. $options = blue_planet_get_option_all(); $options['custom_css'] = ''; set_theme_mod('blueplanet_options', $options); } }
/** * Partial for use in the Customizer. */ public static function echo_custom_css_partial() { echo wp_get_custom_css(); }
/** * Render the Custom CSS style element. * * @since 4.7.0 * @access public */ function wp_custom_css_cb() { $styles = wp_get_custom_css(); if ($styles || is_customize_preview()) { ?> <style type="text/css" id="wp-custom-css"> <?php echo strip_tags($styles); // Note that esc_html() cannot be used because `div > span` is not interpreted properly. ?> </style> <?php } }
/** * Test crud methods on WP_Customize_Custom_CSS_Setting. * * @covers wp_get_custom_css() * @covers WP_Customize_Custom_CSS_Setting::value() * @covers WP_Customize_Custom_CSS_Setting::preview() * @covers WP_Customize_Custom_CSS_Setting::update() */ function test_crud() { $this->setting->default = '/* Hello World */'; $this->assertEquals($this->setting->default, $this->setting->value()); $this->assertNull(wp_get_custom_css_post()); $this->assertNull(wp_get_custom_css_post($this->setting->stylesheet)); $this->assertNull(wp_get_custom_css_post('twentyten')); $original_css = 'body { color: black; }'; $post_id = $this->factory()->post->create(array('post_title' => $this->setting->stylesheet, 'post_name' => $this->setting->stylesheet, 'post_content' => $original_css, 'post_status' => 'publish', 'post_type' => 'custom_css')); $twentyten_css = 'body { color: red; }'; $twentyten_post_id = $this->factory()->post->create(array('post_title' => 'twentyten', 'post_name' => 'twentyten', 'post_content' => $twentyten_css, 'post_status' => 'publish', 'post_type' => 'custom_css')); $twentyten_setting = new WP_Customize_Custom_CSS_Setting($this->wp_customize, 'custom_css[twentyten]'); $this->assertEquals($post_id, wp_get_custom_css_post()->ID); $this->assertEquals($post_id, wp_get_custom_css_post($this->setting->stylesheet)->ID); $this->assertEquals($twentyten_post_id, wp_get_custom_css_post('twentyten')->ID); $this->assertEquals($original_css, wp_get_custom_css($this->setting->stylesheet)); $this->assertEquals($original_css, $this->setting->value()); $this->assertEquals($twentyten_css, wp_get_custom_css('twentyten')); $this->assertEquals($twentyten_css, $twentyten_setting->value()); $updated_css = 'body { color: blue; }'; $this->wp_customize->set_post_value($this->setting->id, $updated_css); $saved = $this->setting->save(); $this->assertTrue(false !== $saved); $this->assertEquals($updated_css, $this->setting->value()); $this->assertEquals($updated_css, wp_get_custom_css($this->setting->stylesheet)); $previewed_css = 'body { color: red; }'; $this->wp_customize->set_post_value($this->setting->id, $previewed_css); $this->setting->preview(); $this->assertEquals($previewed_css, $this->setting->value()); $this->assertEquals($previewed_css, wp_get_custom_css($this->setting->stylesheet)); wp_delete_post($post_id); $this->assertNull(wp_get_custom_css_post()); $this->assertNull(wp_get_custom_css_post(get_stylesheet())); $this->assertEquals($previewed_css, wp_get_custom_css(get_stylesheet()), 'Previewed value remains in spite of deleted post.'); wp_delete_post($twentyten_post_id); $this->assertNull(wp_get_custom_css_post('twentyten')); $this->assertEquals('', wp_get_custom_css('twentyten')); }