-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
GlobalStyles: extract sanitize method #30809
Conversation
Size Change: 0 B Total Size: 1.43 MB ℹ️ View Unchanged
|
@@ -485,12 +436,14 @@ private static function remove_keys_not_in_schema( $tree, $schema ) { | |||
$tree = array_intersect_key( $tree, $schema ); | |||
|
|||
foreach ( $schema as $key => $data ) { | |||
if ( is_array( $schema[ $key ] ) && isset( $tree[ $key ] ) ) { | |||
if ( is_array( $schema[ $key ] ) && is_array( $tree[ $key ] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isset
accounts also for the case when the $key
in the $tree
is not set. Is it safe to replace it with is_array
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not, good catch! Pushed another check above as well.
Would you know how to make unit tests fail if there are warnings? Somehow they're not reported for me when running npm run test-unit-php
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that the env that runs phpunit has error reporting disabled?
03fe91e
to
3959695
Compare
foreach ( $valid_block_names as $block_name ) { | ||
$schema['styles'][ $block_name ] = self::VALID_STYLES; | ||
$schema['settings'][ $block_name ] = self::VALID_SETTINGS; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For each $block_name
, we're creating an identical copy of self::VALID_STYLES
and self::VALID_SETTINGS
, respectively. Would it be worth considering having just
foreach ( $valid_block_names as $block_name ) { | |
$schema['styles'][ $block_name ] = self::VALID_STYLES; | |
$schema['settings'][ $block_name ] = self::VALID_SETTINGS; | |
} | |
$schema['styles'] = self::VALID_STYLES; | |
$schema['settings'] = self::VALID_SETTINGS; |
and changing remove_keys_not_in_schema()
accordingly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the struggles of this class has been how to make it adaptable to theme.json shape changes. The idea I explored in the latest rounds is to separate the logic that iterates over the tree from the logic to do something with a particular set of nodes. A visitor pattern of sorts. The hope is that, by centralizing the iteration logic, changes can be made easily in the future.
For example, in this case, remove_keys_not_in_schema
is agnostic about the shape of theme.json, it just cares about removing nodes in the given tree that don't follow the given schema. The same logic applies to some other methods in this class after the latest changes landed in master: most of them become "visitors" of nodes, they know how to operate on a node or set of nodes (take styles out of them, process presets, etc) but are theme.json-shape agnostic. Then there're a couple of methods that are focused on building the paths to iterate over, so those are the only ones that need updating when theme.json changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted from #30541 so it can land in pieces and make reviews easier/quicker.
This PR extracts the sanitization to its own method and simplifies its logic so that changing the structure at #30541 is easier.
How to test
npm run test-unit-php /var/www/html/wp-content/plugins/gutenberg/phpunit/class-wp-theme-json-test.php