Skip to content

Commit

Permalink
Allow themes to add inline styles for all blocks when using lazy styl…
Browse files Browse the repository at this point in the history
…es loading (#32275)

* register empty styles for blocks that don't have any

* add style & editorStyle in block.json where missing

* only do this if the file doesn't exist

* meh, missed the 2nd arg here

* Use a filter instead of modifying each block.json file.

* Use the register_block_type_args filter

* Move filter to compat files
  • Loading branch information
aristath authored Jun 8, 2021
1 parent a3443bc commit fb481b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ function gutenberg_register_core_block_styles( $block_name ) {

// Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $style_path );
} else {
wp_register_style( "wp-block-{$block_name}", false );
}

if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
Expand All @@ -210,6 +212,8 @@ function gutenberg_register_core_block_styles( $block_name ) {
filemtime( gutenberg_dir_path() . $editor_style_path )
);
wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' );
} else {
wp_register_style( "wp-block-{$block_name}-editor", false );
}
}

Expand Down Expand Up @@ -466,5 +470,4 @@ function gutenberg_migrate_old_typography_shape( $metadata ) {
return $metadata;
}


add_filter( 'block_type_metadata', 'gutenberg_migrate_old_typography_shape' );
24 changes: 24 additions & 0 deletions lib/compat/wordpress-5.8/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,27 @@ function gutenberg_block_editor_rest_api_preload( array $preload_paths, $block_e
'after'
);
}

/**
* Filters the arguments for registering a block type.
*
* @todo Remove from the Gutenberg plugin when WordPress 5.8 is the minimum required version.
*
* @param array $args Array of arguments for registering a block type.
*
* @return array Returns the $metadata with any missing `style` and `editorStyle` added.
*/
function gutenberg_add_missing_styles_to_core_block_json( $args ) {
if ( ! empty( $args['name'] ) && 0 === strpos( $args['name'], 'core/' ) ) {
$block_name = str_replace( 'core/', '', $args['name'] );

if ( ! isset( $args['style'] ) ) {
$args['style'] = "wp-block-$block_name";
}
if ( ! isset( $args['editor_style'] ) ) {
$args['editor_style'] = "wp-block-$block_name-editor";
}
}
return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_add_missing_styles_to_core_block_json' );

0 comments on commit fb481b7

Please sign in to comment.