diff --git src/wp-includes/theme.php src/wp-includes/theme.php
index 646b2e4..baad12f 100644
|
|
function wp_custom_css_cb() { |
1574 | 1574 | } |
1575 | 1575 | |
1576 | 1576 | /** |
1577 | | * Fetch the saved Custom CSS content. |
1578 | | * |
1579 | | * Gets the content of a Custom CSS post that matches the |
1580 | | * current theme. |
| 1577 | * Fetch the `custom_css` post for a given theme. |
1581 | 1578 | * |
1582 | 1579 | * @since 4.7.0 |
1583 | 1580 | * @access public |
1584 | 1581 | * |
1585 | 1582 | * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme. |
1586 | | * |
1587 | | * @return string The Custom CSS Post content. |
| 1583 | * @return WP_Post|null The custom_css post or null if none exists. |
1588 | 1584 | */ |
1589 | | function wp_get_custom_css( $stylesheet = '' ) { |
1590 | | $css = ''; |
1591 | | |
| 1585 | function wp_get_custom_css_post( $stylesheet = '' ) { |
1592 | 1586 | if ( empty( $stylesheet ) ) { |
1593 | 1587 | $stylesheet = get_stylesheet(); |
1594 | 1588 | } |
1595 | 1589 | |
1596 | 1590 | $custom_css_query_vars = array( |
1597 | | 'post_type' => 'custom_css', |
1598 | | 'post_status' => get_post_stati(), |
1599 | | 'name' => sanitize_title( $stylesheet ), |
1600 | | 'number' => 1, |
1601 | | 'no_found_rows' => true, |
1602 | | 'cache_results' => true, |
| 1591 | 'post_type' => 'custom_css', |
| 1592 | 'post_status' => get_post_stati(), |
| 1593 | 'name' => sanitize_title( $stylesheet ), |
| 1594 | 'number' => 1, |
| 1595 | 'no_found_rows' => true, |
| 1596 | 'cache_results' => true, |
1603 | 1597 | 'update_post_meta_cache' => false, |
1604 | 1598 | 'update_term_meta_cache' => false, |
1605 | 1599 | ); |
… |
… |
function wp_get_custom_css( $stylesheet = '' ) { |
1607 | 1601 | $post = null; |
1608 | 1602 | if ( get_stylesheet() === $stylesheet ) { |
1609 | 1603 | $post_id = get_theme_mod( 'custom_css_post_id' ); |
1610 | | if ( ! $post_id ) { |
| 1604 | if ( ! $post_id || ! get_post( $post_id ) ) { |
1611 | 1605 | $query = new WP_Query( $custom_css_query_vars ); |
1612 | 1606 | $post = $query->post; |
1613 | | |
1614 | 1607 | /* |
1615 | 1608 | * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update(). |
1616 | 1609 | * @todo This should get cleared if a custom_css post is added/removed. |
… |
… |
function wp_get_custom_css( $stylesheet = '' ) { |
1624 | 1617 | $post = $query->post; |
1625 | 1618 | } |
1626 | 1619 | |
| 1620 | return $post; |
| 1621 | } |
| 1622 | |
| 1623 | /** |
| 1624 | * Fetch the saved Custom CSS content. |
| 1625 | * |
| 1626 | * @since 4.7.0 |
| 1627 | * @access public |
| 1628 | * |
| 1629 | * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the current theme. |
| 1630 | * @return string The Custom CSS Post content. |
| 1631 | */ |
| 1632 | function wp_get_custom_css( $stylesheet = '' ) { |
| 1633 | $css = ''; |
| 1634 | |
| 1635 | if ( empty( $stylesheet ) ) { |
| 1636 | $stylesheet = get_stylesheet(); |
| 1637 | } |
| 1638 | |
| 1639 | $post = wp_get_custom_css_post( $stylesheet ); |
1627 | 1640 | if ( $post ) { |
1628 | 1641 | $css = $post->post_content; |
1629 | 1642 | } |
diff --git tests/phpunit/tests/customize/custom-css-setting.php tests/phpunit/tests/customize/custom-css-setting.php
index fea0976..63c7c81 100644
|
|
class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { |
45 | 45 | $wp_customize = $this->wp_customize; |
46 | 46 | |
47 | 47 | do_action( 'customize_register', $this->wp_customize ); |
48 | | $this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentysixteen]' ); |
| 48 | $this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[' . get_stylesheet() . ']' ); |
49 | 49 | $this->wp_customize->add_setting( $this->setting ); |
50 | 50 | } |
51 | 51 | |
… |
… |
class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { |
78 | 78 | function test_construct() { |
79 | 79 | $this->assertTrue( post_type_exists( 'custom_css' ) ); |
80 | 80 | $this->assertEquals( 'custom_css', $this->setting->type ); |
81 | | $this->assertEquals( 'twentysixteen', $this->setting->stylesheet ); |
| 81 | $this->assertEquals( get_stylesheet(), $this->setting->stylesheet ); |
82 | 82 | $this->assertEquals( 'edit_css', $this->setting->capability ); |
83 | 83 | |
84 | 84 | $exception = null; |
… |
… |
class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { |
113 | 113 | $this->setting->default = '/* Hello World */'; |
114 | 114 | $this->assertEquals( $this->setting->default, $this->setting->value() ); |
115 | 115 | |
| 116 | $this->assertNull( wp_get_custom_css_post() ); |
| 117 | $this->assertNull( wp_get_custom_css_post( $this->setting->stylesheet ) ); |
| 118 | $this->assertNull( wp_get_custom_css_post( 'twentyten' ) ); |
| 119 | |
116 | 120 | $original_css = 'body { color: black; }'; |
117 | | $this->factory()->post->create( array( |
| 121 | $post_id = $this->factory()->post->create( array( |
118 | 122 | 'post_title' => $this->setting->stylesheet, |
119 | 123 | 'post_name' => $this->setting->stylesheet, |
120 | | 'post_content' => 'body { color: black; }', |
| 124 | 'post_content' => $original_css, |
| 125 | 'post_status' => 'publish', |
| 126 | 'post_type' => 'custom_css', |
| 127 | ) ); |
| 128 | $twentyten_css = 'body { color: red; }'; |
| 129 | $twentyten_post_id = $this->factory()->post->create( array( |
| 130 | 'post_title' => 'twentyten', |
| 131 | 'post_name' => 'twentyten', |
| 132 | 'post_content' => $twentyten_css, |
121 | 133 | 'post_status' => 'publish', |
122 | 134 | 'post_type' => 'custom_css', |
123 | 135 | ) ); |
| 136 | $twentyten_setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentyten]' ); |
| 137 | |
| 138 | $this->assertEquals( $post_id, wp_get_custom_css_post()->ID ); |
| 139 | $this->assertEquals( $post_id, wp_get_custom_css_post( $this->setting->stylesheet )->ID ); |
| 140 | $this->assertEquals( $twentyten_post_id, wp_get_custom_css_post( 'twentyten' )->ID ); |
124 | 141 | |
125 | 142 | $this->assertEquals( $original_css, wp_get_custom_css( $this->setting->stylesheet ) ); |
126 | 143 | $this->assertEquals( $original_css, $this->setting->value() ); |
| 144 | $this->assertEquals( $twentyten_css, wp_get_custom_css( 'twentyten' ) ); |
| 145 | $this->assertEquals( $twentyten_css, $twentyten_setting->value() ); |
127 | 146 | |
128 | 147 | $updated_css = 'body { color: blue; }'; |
129 | 148 | $this->wp_customize->set_post_value( $this->setting->id, $updated_css ); |
… |
… |
class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { |
138 | 157 | $this->setting->preview(); |
139 | 158 | $this->assertEquals( $previewed_css, $this->setting->value() ); |
140 | 159 | $this->assertEquals( $previewed_css, wp_get_custom_css( $this->setting->stylesheet ) ); |
| 160 | |
| 161 | wp_delete_post( $post_id ); |
| 162 | $this->assertNull( wp_get_custom_css_post() ); |
| 163 | $this->assertNull( wp_get_custom_css_post( get_stylesheet() ) ); |
| 164 | $this->assertEquals( $previewed_css, wp_get_custom_css( get_stylesheet() ), 'Previewed value remains in spite of deleted post.' ); |
| 165 | wp_delete_post( $twentyten_post_id ); |
| 166 | $this->assertNull( wp_get_custom_css_post( 'twentyten' ) ); |
| 167 | $this->assertEquals( '', wp_get_custom_css( 'twentyten' ) ); |
141 | 168 | } |
142 | 169 | |
143 | 170 | /** |