Skip to content

Commit 9aafa93

Browse files
committed
Block visibility: add theme json opt out
Adds a setting to theme.json - `blockVisibility.allowEditing` - allowing themes to disable block visibility editing in the editor. With `blockVisibility.allowEditing` set to `false`, the toolbar button and block options menu items in the Block Editor are hidden. Props ramonopoly, mukesh27, isabel_brison, andrewserong. Fixes #65592. git-svn-id: https://develop.svn.wordpress.org/trunk@62725 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f670c8d commit 9aafa93

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class WP_Theme_JSON {
415415
* Added support for `dimensions.width` and `dimensions.height`.
416416
* Added support for `typography.textIndent`.
417417
* @since 7.1.0 Added `viewport` property.
418-
* Added support for `background.gradient`.
418+
* Added support for `background.gradient` and `blockVisibility.allowEditing`.
419419
* @var array
420420
*/
421421
const VALID_SETTINGS = array(
@@ -474,6 +474,9 @@ class WP_Theme_JSON {
474474
'fixed' => null,
475475
'sticky' => null,
476476
),
477+
'blockVisibility' => array(
478+
'allowEditing' => true,
479+
),
477480
'spacing' => array(
478481
'customSpacingSize' => null,
479482
'defaultSpacingSizes' => null,
@@ -1330,7 +1333,9 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
13301333
*/
13311334
foreach ( $valid_block_names as $block ) {
13321335
$schema_settings_blocks[ $block ] = static::VALID_SETTINGS;
1336+
// `viewport` and `blockVisibility` are global-only settings and cannot be set per block for now.
13331337
unset( $schema_settings_blocks[ $block ]['viewport'] );
1338+
unset( $schema_settings_blocks[ $block ]['blockVisibility'] );
13341339
$schema_styles_blocks[ $block ] = $styles_non_top_level;
13351340
$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
13361341

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,36 @@ public function test_get_settings() {
103103
$this->assertEqualSetsWithIndex( $expected, $actual );
104104
}
105105

106+
/**
107+
* @ticket 65592
108+
*/
109+
public function test_get_block_visibility_settings() {
110+
// Test that the value passes through the full sanitization pipeline,
111+
// including remove_insecure_properties (called when saving global styles).
112+
$theme_json_data = array(
113+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
114+
'settings' => array(
115+
'blockVisibility' => array(
116+
'allowEditing' => false,
117+
),
118+
'blocks' => array(
119+
'core/group' => array(
120+
'blockVisibility' => array(
121+
'allowEditing' => true,
122+
),
123+
),
124+
),
125+
),
126+
);
127+
$sanitized = WP_Theme_JSON::remove_insecure_properties( $theme_json_data );
128+
$theme_json = new WP_Theme_JSON( $sanitized );
129+
$actual = $theme_json->get_settings();
130+
131+
$this->assertFalse( $actual['blockVisibility']['allowEditing'] );
132+
// The setting is global-only: block-scoped values are stripped during sanitization.
133+
$this->assertArrayNotHasKey( 'blockVisibility', $actual['blocks']['core/group'] ?? array() );
134+
}
135+
106136
/**
107137
* @ticket 53397
108138
*/

0 commit comments

Comments
 (0)