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

Allow the style property of block.json to be an array and add support for object-based block styles #2853

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
00bfe03
Allow the style property of block.json to be an array
adamziel Jun 22, 2022
92f6635
Refactor the other use of _register_single_block_style_handle
adamziel Jun 22, 2022
277603c
Filter out arrays of styles before enqueuing them in class-wp-block.php
adamziel Jun 22, 2022
6eb4626
Do not flatten the styles array in block_type_metadata filter
adamziel Jun 23, 2022
2acd65b
Clarify the docstring
adamziel Jun 23, 2022
127aac5
Clarify the intention of not processing arrays
adamziel Jun 23, 2022
b58f2e7
Clarify the intention
adamziel Jun 23, 2022
14116bb
Further clarify the intention
adamziel Jun 23, 2022
61e2828
Handle style arrays in block-editor.php
adamziel Jun 23, 2022
4b668b0
Handle style arrays in script-loader.php
adamziel Jun 23, 2022
eeb8ede
Adjust code style to avoid requesting block name in the test_handle_p…
adamziel Jun 23, 2022
37113d9
Do not assume 'file' is an exisitng key
adamziel Jun 23, 2022
d70e683
Add comments, rename _register_single_block_style_handle to register_…
adamziel Jun 24, 2022
e034805
Register all string style handles, not just the first one
adamziel Jun 27, 2022
7e086ef
Add array sypport to WP_Block_Type and WP_Rest_Block_Types_Controller
adamziel Jun 29, 2022
438a35b
Add support for editor_styles
adamziel Jun 29, 2022
e4fc856
Delete _wp_multiple_block_styles now that multiple styles are handled
adamziel Jun 29, 2022
3bdb544
Normalize styles format to a list
adamziel Jun 29, 2022
da476d2
Add docstring to _wp_normalize_block_styles_from_mixed_format_to_array
adamziel Jun 29, 2022
ce39fb7
Move the argument normalization inside of WP_Block_Type class
adamziel Jun 29, 2022
c9f0d13
Support array-based styles in register_block_style_handle and add uni…
adamziel Jun 29, 2022
81f13cb
Add an empty _wp_multiple_block_styles function
adamziel Jun 29, 2022
994938f
Add debug statement to see the provlem in the CI
adamziel Jun 29, 2022
c44ba77
Update docs
adamziel Jun 29, 2022
b8ab9d0
Fix last test failures
adamziel Jun 29, 2022
124b4ad
Add @since to normalize_block_styles_from_mixed_format_to_array
adamziel Jun 29, 2022
2c396ae
Merge branch 'trunk' into allow-style-arrays-in-block-json
adamziel Jul 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,26 +325,18 @@ function _wp_get_iframed_editor_assets() {

foreach ( $block_registry->get_all_registered() as $block_type ) {
if ( ! empty( $block_type->style ) ) {
if ( is_array( $block_type->style ) ) {
foreach ( $block_type->style as $single_style ) {
if ( is_string( $single_style ) ) {
$style_handles[] = $single_style;
}
foreach ( $block_type->style as $single_style ) {
if ( is_string( $single_style ) ) {
$style_handles[] = $single_style;
}
} else {
$style_handles[] = $block_type->style;
}
}

if ( ! empty( $block_type->editor_style ) ) {
if ( is_array( $block_type->editor_style ) ) {
foreach ( $block_type->editor_style as $single_style ) {
if ( is_string( $single_style ) ) {
$style_handles[] = $single_style;
}
foreach ( $block_type->editor_style as $single_style ) {
if ( is_string( $single_style ) ) {
$style_handles[] = $single_style;
}
} else {
$style_handles[] = $block_type->editor_style;
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,47 @@ public function get_attributes() {
array();
}
}

function _wp_normalize_block_styles_from_mixed_format_to_array( $args ) {
adamziel marked this conversation as resolved.
Show resolved Hide resolved
foreach ( $args as $property_name => $property_value ) {
/*
* The style and editor_style properties may be defined in any of the following ways:
*
* String handle:
* { "style": "my-block" }
*
* Style definition:
* { "style": { "color": { "text": "#fff" } } }
*
* An array with a string handle:
* { "style": [ "my-block" ] }
*
* A mixed array of string handle and style definitions:
* { "style": [ "my-block", { "color": { "text": "#fff" } } ] }
*
* That's a lot of work for the developer to handle, so we'll handle it for them.
* The code below always stores the styles in the mixed array format.
*/
if (
! is_null( $property_value ) &&
in_array( $property_name, array( 'style', 'editor_style' ), true )
) {
/*
* Tests for an associative array.
* * true if the value is like array("color" => ...)
* * false if the value is like array("handle", array())
*/
$is_style_definition = is_array( $property_value ) && count( array_filter( array_keys( $property_value ), 'is_string' ) ) > 0;
$is_style_handle = is_string( $property_value );
$is_style_element = $is_style_definition || $is_style_handle;

if ( $is_style_element ) {
$args[ $property_name ] = array( $property_value );
}
}
}

return $args;
}

add_filter( 'register_block_type_args', '_wp_normalize_block_styles_from_mixed_format_to_array', 10 );
10 changes: 3 additions & 7 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,10 @@ public function render( $options = array() ) {
*
* This code enqueues the stylesheet handles and omit the style arrays.
*/
if ( is_array( $this->block_type->style ) ) {
foreach ( $this->block_type->style as $handle_maybe ) {
if ( is_string( $handle_maybe ) ) {
wp_enqueue_style( $handle_maybe );
}
foreach ( $this->block_type->style as $handle_maybe ) {
if ( is_string( $handle_maybe ) ) {
wp_enqueue_style( $handle_maybe );
}
} else {
wp_enqueue_style( $this->block_type->style );
}
}

Expand Down
20 changes: 6 additions & 14 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2502,14 +2502,10 @@ function wp_enqueue_registered_block_scripts_and_styles() {
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
// Front-end styles.
if ( ! empty( $block_type->style ) ) {
if ( is_array( $block_type->style ) ) {
foreach ( $block_type->style as $single_style ) {
if ( is_string( $single_style ) ) {
wp_enqueue_style( $single_style );
}
foreach ( $block_type->style as $single_style ) {
if ( is_string( $single_style ) ) {
wp_enqueue_style( $single_style );
}
} else {
wp_enqueue_style( $block_type->style );
}
}

Expand All @@ -2520,14 +2516,10 @@ function wp_enqueue_registered_block_scripts_and_styles() {

// Editor styles.
if ( $load_editor_scripts && ! empty( $block_type->editor_style ) ) {
if ( is_array( $block_type->editor_style ) ) {
foreach ( $block_type->editor_style as $single_style ) {
if ( is_string( $single_style ) ) {
wp_enqueue_style( $single_style );
}
foreach ( $block_type->editor_style as $single_style ) {
if ( is_string( $single_style ) ) {
wp_enqueue_style( $single_style );
}
} else {
wp_enqueue_style( $block_type->editor_style );
}
}

Expand Down