Skip to content

Commit c647b15

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents 623f529 + d6b4669 commit c647b15

3 files changed

Lines changed: 67 additions & 4 deletions

File tree

src/wp-includes/class-wp-customize-manager.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,6 @@ public function customize_preview_init() {
19361936
'<code>customize_messenger_channel</code>'
19371937
)
19381938
);
1939-
return;
19401939
}
19411940

19421941
$this->prepare_controls();
@@ -3166,7 +3165,6 @@ public function handle_changeset_trash_request() {
31663165
'code' => 'non_existent_changeset',
31673166
)
31683167
);
3169-
return;
31703168
}
31713169

31723170
if ( ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->delete_post, $changeset_post_id ) ) {
@@ -3197,7 +3195,6 @@ public function handle_changeset_trash_request() {
31973195
'code' => 'changeset_already_trashed',
31983196
)
31993197
);
3200-
return;
32013198
}
32023199

32033200
$r = $this->trash_changeset_post( $changeset_post_id );

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,22 @@ public function get_theme_items( $request ) {
673673
* @since 7.0.0 Only restricts contents which risk prematurely closing the STYLE element,
674674
* either through a STYLE end tag or a prefix of one which might become a
675675
* full end tag when combined with the contents of other styles.
676+
* @since 7.1.0 Rejects non-string values with a WP_Error instead of a fatal error.
676677
*
677678
* @see WP_Customize_Custom_CSS_Setting::validate()
678679
*
679-
* @param string $css CSS to validate.
680+
* @param mixed $css CSS to validate.
680681
* @return true|WP_Error True if the input was validated, otherwise WP_Error.
681682
*/
682683
protected function validate_custom_css( $css ) {
684+
if ( ! is_string( $css ) ) {
685+
return new WP_Error(
686+
'rest_custom_css_invalid_type',
687+
__( 'CSS must be a string.' ),
688+
array( 'status' => 400 )
689+
);
690+
}
691+
683692
$length = strlen( $css );
684693
for (
685694
$at = strcspn( $css, '<' );

tests/phpunit/tests/rest-api/rest-global-styles-controller.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,25 @@ public function test_update_item_invalid_styles_css() {
667667
$this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
668668
}
669669

670+
/**
671+
* @covers WP_REST_Global_Styles_Controller::update_item
672+
* @ticket 65640
673+
*/
674+
public function test_update_item_non_string_styles_css() {
675+
wp_set_current_user( self::$admin_id );
676+
if ( is_multisite() ) {
677+
grant_super_admin( self::$admin_id );
678+
}
679+
$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
680+
$request->set_body_params(
681+
array(
682+
'styles' => array( 'css' => array( 'body { color: red; }' ) ),
683+
)
684+
);
685+
$response = rest_get_server()->dispatch( $request );
686+
$this->assertErrorResponse( 'rest_custom_css_invalid_type', $response, 400 );
687+
}
688+
670689
/**
671690
* Tests the submission of a custom block style variation that was defined
672691
* within a theme style variation and wouldn't be registered at the time
@@ -946,4 +965,42 @@ public static function data_custom_css_disallowed(): array {
946965
'truncated "</stYle"' => array( '</stYle', 'The CSS must not end in "&lt;/stYle".' ),
947966
);
948967
}
968+
969+
/**
970+
* @covers WP_REST_Global_Styles_Controller::validate_custom_css
971+
* @ticket 65640
972+
*
973+
* @dataProvider data_custom_css_non_string
974+
*
975+
* @param mixed $custom_css Non-string value to validate.
976+
*/
977+
public function test_validate_custom_css_non_string( $custom_css ) {
978+
$controller = new WP_REST_Global_Styles_Controller();
979+
$validate = Closure::bind(
980+
function ( $css ) {
981+
return $this->validate_custom_css( $css );
982+
},
983+
$controller,
984+
$controller
985+
);
986+
987+
$result = $validate( $custom_css );
988+
$this->assertWPError( $result );
989+
$this->assertSame( 'rest_custom_css_invalid_type', $result->get_error_code() );
990+
}
991+
992+
/**
993+
* Data provider.
994+
*
995+
* @return array<string, mixed[]>
996+
*/
997+
public static function data_custom_css_non_string(): array {
998+
return array(
999+
'array' => array( array( 'body { color: red; }' ) ),
1000+
'integer' => array( 123 ),
1001+
'boolean' => array( true ),
1002+
'null' => array( null ),
1003+
'object' => array( new stdClass() ),
1004+
);
1005+
}
9491006
}

0 commit comments

Comments
 (0)