Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use allow-list for mobile editor settings endpoint #56424

Draft
wants to merge 11 commits into
base: trunk
Choose a base branch
from
138 changes: 138 additions & 0 deletions lib/experimental/block-editor-settings-mobile-allowed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* Filters settings for the mobile block editor.
*
* @package gutenberg
*/

if ( ! function_exists( 'filter_by_supported_block_editor_settings_mobile' ) ) {
/**
* Recursively filters for supported settings.
*
* This is used to control the editor settings payload. Keys can be specified
* as `true` to be allowed, which will also allow entire nested structures.
* Alternatively, nested structures can have nested allow-lists for their keys.
*
* @param array $initial_array Existing block editor settings.
*
* @param array $allow_list_array Structured allow-list.
*
* @return array New block editor settings.
*/
function filter_by_supported_block_editor_settings_mobile( $initial_array, $allow_list_array ) {
$result = array();

foreach ( $allow_list_array as $key => $value ) {
if ( ! array_key_exists( $key, $initial_array ) ) {
continue;
}

$initial_value = $initial_array[ $key ];

if ( array_key_exists( $key, $initial_array ) ) {
if ( is_array( $value ) && is_array( $initial_value ) ) {
$result[ $key ] = filter_by_supported_block_editor_settings_mobile( $initial_value, $value );
} else {
$result[ $key ] = $initial_value;
}
}
}

return $result;
}
}

if ( ! function_exists( 'gutenberg_get_allowed_block_editor_settings_mobile' ) ) {
/**
* Keeps only supported settings for the mobile block editor.
*
* @param array $settings Existing block editor settings.
*
* @return array New block editor settings.
*/
function gutenberg_get_allowed_block_editor_settings_mobile( $settings ) {

if (
defined( 'REST_REQUEST' ) &&
REST_REQUEST &&
isset( $_GET['context'] ) &&
'mobile' === $_GET['context']
) {
return filter_by_supported_block_editor_settings_mobile(
$settings,
BLOCK_EDITOR_SETTINGS_MOBILE_ALLOW_LIST
);
} else {
return $settings;
}
}
}

if ( ! defined( 'BLOCK_EDITOR_SETTINGS_MOBILE_ALLOW_LIST' ) ) {
define(
'BLOCK_EDITOR_SETTINGS_MOBILE_ALLOW_LIST',
array(
'alignWide' => true,
'allowedBlockTypes' => true,
'allowedMimeTypes' => true,
'defaultEditorStyles' => true,
'blockCategories' => true,
'isRTL' => true,
'imageDefaultSize' => true,
'imageDimensions' => true,
'imageEditing' => true,
'imageSizes' => true,
'maxUploadFileSize' => true,
'__unstableGalleryWithImageBlocks' => true,
'disableCustomColors' => true,
'disableCustomFontSizes' => true,
'disableCustomGradients' => true,
'disableLayoutStyles' => true,
'enableCustomLineHeight' => true,
'enableCustomSpacing' => true,
'enableCustomUnits' => true,
'colors' => true,
'fontSizes' => true,
'__experimentalStyles' => array(
'elements' => true,
'spacing' => true,
'blocks' => true,
'color' => true,
'typography' => true,
),
'__experimentalFeatures' => array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add the custom attribute which is used for custom variables or other settings. The app uses it to get values for custom variables.

'appearanceTools' => true,
'useRootPaddingAwareAlignments' => true,
'border' => true,
'color' => true,
'shadow' => true,
'spacing' => true,
'typography' => array(
'dropCap' => true,
'fontSizes' => true,
'fontStyle' => true,
'fontWeight' => true,
'letterSpacing' => true,
'textColumns' => true,
'textDecoration' => true,
'textTransform' => true,
'writingMode' => true,
),
'blocks' => true,
'background' => true,
'dimensions' => true,
'position' => true,
),
'gradients' => true,
'disableCustomSpacingSizes' => true,
'spacingSizes' => true,
'__unstableIsBlockBasedTheme' => true,
'localAutosaveInterval' => true,
'__experimentalDiscussionSettings' => true,
'__experimentalEnableQuoteBlockV2' => true,
'__experimentalEnableListBlockV2' => true,
)
);
}

add_filter( 'block_editor_settings_all', 'gutenberg_get_allowed_block_editor_settings_mobile', PHP_INT_MAX );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function gutenberg_is_experiment_enabled( $name ) {

// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/block-editor-settings-mobile-allowed.php';
require __DIR__ . '/experimental/blocks.php';
require __DIR__ . '/experimental/navigation-theme-opt-in.php';
require __DIR__ . '/experimental/kses.php';
Expand Down
Loading