-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Style engine: extend block support style definitions #45296
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c647880
initial commit:
ramonjd 6dd326c
No such thing as wp_json_decode
ramonjd ffdc7da
Rename to styles metadata
ramonjd 596862f
Switching to class instantiation instead of a static class
ramonjd cb0a710
Changelog
ramonjd 8ede035
Fixing up comments and README.md
ramonjd ba1c6d5
- only allow adding new styles, not overwriting original styles
ramonjd 1850436
Readme needed some love
ramonjd 466127f
Replace array_key_exists with isset for speed
ramonjd eb569b9
Remove readme doc for now since it won't be in core for a while.
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
packages/style-engine/class-wp-style-engine-block-style-metadata.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| <?php | ||
| /** | ||
| * WP_Style_Engine_Block_Style_Metadata | ||
| * | ||
| * Stores block support metadata and associated rules. | ||
| * | ||
| * @package Gutenberg | ||
| */ | ||
|
|
||
| if ( class_exists( 'WP_Style_Engine_Block_Style_Metadata' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| /** | ||
| * Stores block support metadata and associated rules. | ||
| * | ||
| * @access private | ||
| */ | ||
| class WP_Style_Engine_Block_Style_Metadata { | ||
| /** | ||
| * A variable to cache original metadata. | ||
| * | ||
| * @var array | ||
| */ | ||
| protected $base_metadata = array(); | ||
|
|
||
| /** | ||
| * The merged metadata. | ||
| * | ||
| * @var array | ||
| */ | ||
| protected $merged_block_support_metadata = array(); | ||
|
|
||
| /** | ||
| * Constructor for this object. | ||
| * | ||
| * @param array $base_metadata An associative array of block style metadata to extend. | ||
| */ | ||
| public function __construct( $base_metadata = array() ) { | ||
| $this->base_metadata = $base_metadata; | ||
| $this->reset_metadata(); | ||
| } | ||
|
|
||
| /** | ||
| * Adds block style metadata. | ||
| * | ||
| * @param array $new_metadata The $metadata to be added. | ||
| * | ||
| * @return WP_Style_Engine_Block_Style_Metadata Returns the object to allow chaining methods. | ||
| */ | ||
| public function add_metadata( $new_metadata = array() ) { | ||
| if ( empty( $new_metadata ) ) { | ||
| return $this; | ||
| } | ||
|
|
||
| foreach ( $new_metadata as $definition_group_key => $definition_group_style ) { | ||
| if ( ! is_array( $definition_group_style ) || empty( $definition_group_style ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Adds a new top-level group if it doesn't exist already. | ||
| if ( ! isset( $this->merged_block_support_metadata[ $definition_group_key ] ) ) { | ||
| $this->merged_block_support_metadata[ $definition_group_key ] = array(); | ||
| } | ||
|
|
||
| foreach ( $definition_group_style as $style_definition_key => $style_definition ) { | ||
| // Bails early if merging metadata is attempting to overwrite existing, original style metadata. | ||
| if ( isset( $this->base_metadata[ $definition_group_key ] ) | ||
| && isset( $this->base_metadata[ $definition_group_key ][ $style_definition_key ] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( ! is_array( $style_definition ) || empty( $style_definition ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $array_to_extend = isset( $this->merged_block_support_metadata[ $definition_group_key ][ $style_definition_key ] ) | ||
| ? $this->merged_block_support_metadata[ $definition_group_key ][ $style_definition_key ] : array(); | ||
| $merged_style_definition = $this->merge_custom_style_definitions_metadata( $array_to_extend, $style_definition ); | ||
|
|
||
| if ( $merged_style_definition ) { | ||
| $this->merged_block_support_metadata[ $definition_group_key ][ $style_definition_key ] = $merged_style_definition; | ||
| } | ||
| } | ||
| } | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns merged metadata. | ||
| * | ||
| * @param array $path A path to an array item in static::$merged_block_support_metadata. | ||
| * @return array | ||
| */ | ||
| public function get_metadata( $path = array() ) { | ||
| if ( ! empty( $path ) ) { | ||
| return _wp_array_get( $this->merged_block_support_metadata, $path, null ); | ||
| } | ||
| return $this->merged_block_support_metadata; | ||
| } | ||
|
|
||
| /** | ||
| * Resets the de-referenced metadata array. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function reset_metadata() { | ||
| $this->merged_block_support_metadata = json_decode( wp_json_encode( $this->base_metadata ), true ); | ||
| } | ||
|
|
||
| /** | ||
| * Merges single style definitions with incoming custom style definitions. | ||
| * | ||
| * @param array $style_definition The internal style definition metadata. | ||
| * @param array $custom_definition The custom style definition metadata to be merged. | ||
| * | ||
| * @return array|void The merged definition metadata. | ||
| */ | ||
| protected function merge_custom_style_definitions_metadata( $style_definition, $custom_definition = array() ) { | ||
| // Required metadata. | ||
| if ( ! isset( $style_definition['path'] ) && ! isset( $custom_definition['path'] ) && ! is_array( $custom_definition['path'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Only allow strings for valid property keys. | ||
| if ( ! isset( $custom_definition['property_keys']['default'] ) && ! is_string( $custom_definition['property_keys']['default'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Only allow strings for valid property keys. | ||
| if ( isset( $custom_definition['property_keys']['individual'] ) && ! is_string( $custom_definition['property_keys']['individual'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $custom_definition['property_keys']['default'] = sanitize_key( $custom_definition['property_keys']['default'] ); | ||
|
|
||
| // A white list of keys that may be merged. Note the absence of the callable `value_func`. | ||
| $valid_keys = array( 'path', 'property_keys', 'css_vars', 'classnames' ); | ||
| foreach ( $valid_keys as $key ) { | ||
| if ( isset( $custom_definition[ $key ] ) && is_array( $custom_definition[ $key ] ) ) { | ||
| if ( ! isset( $style_definition[ $key ] ) ) { | ||
| $style_definition[ $key ] = array(); | ||
| } | ||
| $style_definition[ $key ] = array_merge( $style_definition[ $key ], $custom_definition[ $key ] ); | ||
| } | ||
| } | ||
|
|
||
| return $style_definition; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
phpunit/style-engine/class-wp-style-engine-block-style-metadata-test.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| <?php | ||
| /** | ||
| * Tests the Style Engine Block Style Metadata class. | ||
| * | ||
| * @package Gutenberg | ||
| * @subpackage style-engine | ||
| */ | ||
|
|
||
| /** | ||
| * Tests fetching, extending and otherwise manipulation block supports style definitions. | ||
| * | ||
| * @coversDefaultClass WP_Style_Engine_Block_Style_Metadata_Gutenberg | ||
| */ | ||
| class WP_Style_Engine_Block_Style_Metadata_Test extends WP_UnitTestCase { | ||
| /** | ||
| * Tests getting metadata. | ||
| * | ||
| * @covers ::get_metadata | ||
| */ | ||
| public function test_should_get_metadata() { | ||
| $block_style_metadata = new WP_Style_Engine_Block_Style_Metadata_Gutenberg( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA ); | ||
|
|
||
| $full_metadata = $block_style_metadata->get_metadata(); | ||
| $this->assertEquals( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA, $full_metadata, 'Returning all default definitions' ); | ||
|
|
||
| $color_metadata = $block_style_metadata->get_metadata( array( 'color' ) ); | ||
| $this->assertEquals( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA['color'], $color_metadata, 'Returning top-level color definition' ); | ||
|
|
||
| $color_metadata = $block_style_metadata->get_metadata( array( 'color', 'background' ) ); | ||
| $this->assertEquals( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA['color']['background'], $color_metadata, 'Returning second-level color > background definition' ); | ||
|
|
||
| $null_metadata = $block_style_metadata->get_metadata( array( 'something', 'background' ) ); | ||
| $this->assertNull( $null_metadata, 'Returning `null` where the path is invalid' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests adding metadata to the block styles definition. | ||
| * | ||
| * @covers ::add_metadata | ||
| */ | ||
| public function test_should_add_new_top_level_metadata() { | ||
| $block_style_metadata = new WP_Style_Engine_Block_Style_Metadata_Gutenberg( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA ); | ||
| $new_metadata = array( | ||
| 'layout' => array( | ||
| 'float' => array( | ||
| 'property_keys' => array( | ||
| 'default' => 'float', | ||
| ), | ||
| 'path' => array( 'layout', 'float' ), | ||
| 'css_vars' => array( | ||
| 'layout' => '--wp--preset--float--$slug', | ||
| ), | ||
| 'classnames' => array( | ||
| 'has-float-layout' => true, | ||
| 'has-$slug-float' => 'layout', | ||
| ), | ||
| ), | ||
| 'width' => array( | ||
| 'property_keys' => array( | ||
| 'default' => 'width', | ||
| 'individual' => '%s-width', | ||
| ), | ||
| 'path' => array( 'layout', 'width' ), | ||
| 'classnames' => array( | ||
| 'has-$slug-width' => 'layout', | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| $this->assertEquals( | ||
| $new_metadata['layout'], | ||
| $block_style_metadata->add_metadata( $new_metadata )->get_metadata( array( 'layout' ) ), | ||
| 'A new style definition for `layout` should be registered' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests adding new second-level property metadata to the block styles definition and ignore `value_func` values. | ||
| * | ||
| * @covers ::add_metadata | ||
| */ | ||
| public function test_should_add_new_style_property_metadata_keys() { | ||
| $block_style_metadata = new WP_Style_Engine_Block_Style_Metadata_Gutenberg( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA ); | ||
| $new_metadata = array( | ||
| 'typography' => array( | ||
| 'textIndent' => array( | ||
| 'property_keys' => array( | ||
| 'default' => 'text-indent', | ||
| ), | ||
| 'css_vars' => array( | ||
| 'spacing' => '--wp--preset--spacing--$slug', | ||
| ), | ||
| 'path' => array( 'typography', 'textIndent' ), | ||
| 'classnames' => array( | ||
| 'has-text-indent' => true, | ||
| ), | ||
| 'value_func' => 'Test::function', | ||
| ), | ||
| ), | ||
| ); | ||
| $block_style_metadata->add_metadata( $new_metadata ); | ||
|
|
||
| // Remove ignored property keys. | ||
| unset( $new_metadata['typography']['textIndent']['value_func'] ); | ||
|
|
||
| $this->assertEquals( | ||
| $new_metadata['typography']['textIndent'], | ||
| $block_style_metadata->get_metadata( array( 'typography', 'textIndent' ) ), | ||
| 'The new style property should match expected.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that merging style metadata to the block styles definitions does not work. | ||
| * | ||
| * @covers ::add_metadata | ||
| */ | ||
| public function test_should_not_overwrite_style_property_metadata() { | ||
| $block_style_metadata = new WP_Style_Engine_Block_Style_Metadata_Gutenberg( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA ); | ||
| $new_metadata = array( | ||
| 'spacing' => array( | ||
| 'padding' => array( | ||
| 'property_keys' => array( | ||
| 'default' => 'columns', | ||
| ), | ||
| 'css_vars' => array( | ||
| 'spacing' => '--wp--preset--column--$slug', | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| $block_style_metadata->add_metadata( $new_metadata ); | ||
| $this->assertEquals( | ||
| WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA['spacing']['padding'], | ||
| $block_style_metadata->get_metadata( array( 'spacing', 'padding' ) ), | ||
| 'The newly-merged property metadata should be present' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests resetting metadata to the original block styles definition. | ||
| * | ||
| * @covers ::reset_metadata | ||
| */ | ||
| public function test_should_reset_metadata() { | ||
| $block_style_metadata = new WP_Style_Engine_Block_Style_Metadata_Gutenberg( WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA ); | ||
| $new_metadata = array( | ||
| 'spacing' => array( | ||
| 'gap' => array( | ||
| 'property_keys' => array( | ||
| 'default' => 'gap', | ||
| 'individual' => 'gap-%', | ||
| ), | ||
| 'path' => array( 'spacing', 'gap' ), | ||
| 'css_vars' => array( | ||
| 'spacing' => '--wp--preset--spacing--$slug', | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->assertEquals( | ||
| $new_metadata['spacing']['gap'], | ||
| $block_style_metadata->add_metadata( $new_metadata )->get_metadata( array( 'spacing', 'gap' ) ), | ||
| 'Should successfully merge metadata' | ||
| ); | ||
|
|
||
| $block_style_metadata->reset_metadata(); | ||
| $this->assertEquals( | ||
| WP_Style_Engine_Gutenberg::BLOCK_STYLE_DEFINITIONS_METADATA['spacing'], | ||
| $block_style_metadata->get_metadata( array( 'spacing' ) ), | ||
| 'Should be equal to original' | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.