Skip to content

Commit

Permalink
Global Styles: Add slashes back to the Theme JSON (#33919)
Browse files Browse the repository at this point in the history
* Global Styles: Add slashes back to the Theme JSON

* Change PHP strip/add slashes to use the WordPress functions

* Add a unit test for global styles

* fix typo
  • Loading branch information
scruffian authored Aug 12, 2021
1 parent ae56763 commit 1e50cab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function gutenberg_experimental_global_styles_register_user_cpt() {
* @return string Filtered post content with unsafe rules removed.
*/
function gutenberg_global_styles_filter_post( $content ) {
$decoded_data = json_decode( stripslashes( $content ), true );
$decoded_data = json_decode( wp_unslash( $content ), true );
$json_decoding_error = json_last_error();
if (
JSON_ERROR_NONE === $json_decoding_error &&
Expand All @@ -245,7 +245,7 @@ function gutenberg_global_styles_filter_post( $content ) {
$data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data );

$data_to_encode['isGlobalStylesUserThemeJSON'] = true;
return wp_json_encode( $data_to_encode );
return wp_slash( wp_json_encode( $data_to_encode ) );
}
return $content;
}
Expand Down
28 changes: 28 additions & 0 deletions phpunit/global-styles-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* Test the Global Styles lib
*
* @package Gutenberg
*/

class WP_Global_Styles_Test extends WP_UnitTestCase {
function test_gutenberg_global_styles_filter_post() {
$user_theme_json = '{
"isGlobalStylesUserThemeJSON": 1,
"version": 1,
"settings": {
"typography": {
"fontFamilies": {
"fontFamily": "\"DM Sans\", sans-serif",
"slug": "dm-sans",
"name": "DM Sans",
}
}
}
}';

$filtered_user_theme_json = gutenberg_global_styles_filter_post( $user_theme_json );
$this->assertEquals( $user_theme_json, $filtered_user_theme_json );
}
}

0 comments on commit 1e50cab

Please sign in to comment.