Skip to content

Commit d4d2016

Browse files
committed
Refactor SAFE_SETTINGS in WP_Theme_JSON to be private, ensuring it is used only internally. Update unit tests to validate the removal of insecure properties and ensure safe settings are not allowed to be unsafe.
1 parent e102c6b commit d4d2016

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

src/wp-includes/class-wp-theme-json.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,13 @@ class WP_Theme_JSON {
487487
*
488488
* These are non-preset, non-CSS settings that control behavior rather than styling.
489489
* Each entry defines the setting path and its expected type for validation.
490-
* Each entry should also be present in ::VALID_SETTINGS.
490+
*
491+
* The constant is deliberately private to prevent external usage by plugins.
492+
* Like the class itself, it is intended for internal core usage.
491493
*
492494
* @since 7.0.0
493495
*/
494-
const SAFE_SETTINGS = array(
496+
private const SAFE_SETTINGS = array(
495497
array(
496498
'path' => array( 'lightbox', 'allowEditing' ),
497499
'type' => 'boolean',

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6630,24 +6630,25 @@ public function test_merge_incoming_data_unique_slugs_always_preserved() {
66306630
* @ticket 64280
66316631
*/
66326632
public function test_remove_insecure_properties_should_allow_safe_settings() {
6633-
$actual = WP_Theme_JSON::remove_insecure_properties(
6633+
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties(
66346634
array(
6635-
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6635+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
66366636
'settings' => array(
66376637
'blocks' => array(
66386638
'core/image' => array(
6639-
'lightbox' => array(
6639+
'lightbox' => array(
66406640
'enabled' => false,
66416641
'allowEditing' => true,
66426642
),
6643+
'unsupported' => 'value',
66436644
),
66446645
),
66456646
),
66466647
)
66476648
);
66486649

66496650
$expected = array(
6650-
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6651+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
66516652
'settings' => array(
66526653
'blocks' => array(
66536654
'core/image' => array(
@@ -6662,35 +6663,41 @@ public function test_remove_insecure_properties_should_allow_safe_settings() {
66626663

66636664
$this->assertEqualSetsWithIndex( $expected, $actual );
66646665
}
6665-
66666666
/**
66676667
* @covers WP_Theme_JSON::remove_insecure_properties
66686668
*
66696669
* @ticket 64280
66706670
*/
6671-
public function test_safe_settings_paths_should_exist_in_valid_settings() {
6672-
// Verify all paths in SAFE_SETTINGS exist in VALID_SETTINGS.
6673-
foreach ( WP_Theme_JSON::SAFE_SETTINGS as $safe_setting ) {
6674-
$path = $safe_setting['path'];
6675-
$data = WP_Theme_JSON::VALID_SETTINGS;
6676-
6677-
// Check if path exists by traversing the nested structure.
6678-
$exists = true;
6679-
foreach ( $path as $key ) {
6680-
if ( ! is_array( $data ) || ! array_key_exists( $key, $data ) ) {
6681-
$exists = false;
6682-
break;
6683-
}
6684-
$data = $data[ $key ];
6685-
}
6686-
6687-
$this->assertTrue(
6688-
$exists,
6689-
sprintf(
6690-
'Path %s from SAFE_SETTINGS should exist in VALID_SETTINGS',
6691-
implode( '.', $path )
6692-
)
6693-
);
6694-
}
6671+
public function test_remove_insecure_properties_should_not_allow_unsafe_settings() {
6672+
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties(
6673+
array(
6674+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
6675+
'settings' => array(
6676+
'blocks' => array(
6677+
'core/image' => array(
6678+
'lightbox' => array(
6679+
'enabled' => 'false',
6680+
'allowEditing' => true,
6681+
),
6682+
),
6683+
),
6684+
),
6685+
)
6686+
);
6687+
6688+
$expected = array(
6689+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
6690+
'settings' => array(
6691+
'blocks' => array(
6692+
'core/image' => array(
6693+
'lightbox' => array(
6694+
'allowEditing' => true,
6695+
),
6696+
),
6697+
),
6698+
),
6699+
);
6700+
6701+
$this->assertEqualSetsWithIndex( $expected, $actual );
66956702
}
66966703
}

0 commit comments

Comments
 (0)