Skip to content

Commit

Permalink
Use new fields when enqueuing styles and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Sep 13, 2022
1 parent d56a061 commit 772fb62
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
25 changes: 9 additions & 16 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,16 @@ function _wp_get_iframed_editor_assets() {
$block_registry = WP_Block_Type_Registry::get_instance();

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

if ( ! empty( $block_type->editor_style ) ) {
foreach ( (array) $block_type->editor_style as $single_editor_style ) {
$style_handles[] = $single_editor_style;
}
}
$style_handles = array_merge(
$style_handles,
$block_type->style_handles,
$block_type->editor_style_handles
);

if ( ! empty( $block_type->script ) ) {
foreach ( (array) $block_type->script as $single_script ) {
$script_handles[] = $single_script;
}
}
$script_handles = array_merge(
$script_handles,
$block_type->script_handles
);
}

$style_handles = array_unique( $style_handles );
Expand Down
13 changes: 10 additions & 3 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
// Skip registering individual styles for each core block when a bundled version provided.
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
return false;
}
Expand All @@ -195,8 +196,14 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
$style_handle = $style_handle[ $index ];
}

$style_path = remove_block_asset_path_prefix( $style_handle );
if ( $style_handle === $style_path && ! $is_core_block ) {
$style_path = remove_block_asset_path_prefix( $style_handle );
$is_style_handle = $style_handle === $style_path;
// Allow only passing style handles for core blocks.
if ( $is_core_block && ! $is_style_handle ) {
return false;
}
// Return the style handle unless it's the first item for every core block that requires special treatment.
if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) {
return $style_handle;
}

Expand All @@ -217,7 +224,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {

$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
$block_dir = dirname( $metadata['file'] );
$style_file = realpath( "$block_dir/$style_path" );
$style_file = wp_normalize_path( realpath( "$block_dir/$style_path" ) );
$has_style_file = false !== $style_file;
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$style_uri = $has_style_file ? $style_uri : false;
Expand Down
18 changes: 9 additions & 9 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,21 @@ public function render( $options = array() ) {
$post = $global_post;
}

if ( ! empty( $this->block_type->script ) ) {
foreach ( (array) $this->block_type->script as $single_script ) {
wp_enqueue_script( $single_script );
if ( ( ! empty( $this->block_type->script_handles ) ) ) {
foreach ( $this->block_type->script_handles as $script_handle ) {
wp_enqueue_script( $script_handle );
}
}

if ( ! empty( $this->block_type->view_script ) && empty( $this->block_type->render_callback ) ) {
foreach ( (array) $this->block_type->view_script as $single_view_script ) {
wp_enqueue_script( $single_view_script );
if ( ! empty( $this->block_type->view_script_handles ) && empty( $this->block_type->render_callback ) ) {
foreach ( $this->block_type->view_script_handles as $view_script_handle ) {
wp_enqueue_script( $view_script_handle );
}
}

if ( ! empty( $this->block_type->style ) ) {
foreach ( (array) $this->block_type->style as $single_style ) {
wp_enqueue_style( $single_style );
if ( ( ! empty( $this->block_type->style_handles ) ) ) {
foreach ( $this->block_type->style_handles as $style_handle ) {
wp_enqueue_style( $style_handle );
}
}

Expand Down
34 changes: 14 additions & 20 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2531,35 +2531,29 @@ function wp_enqueue_registered_block_scripts_and_styles() {
return;
}

$load_editor_scripts = is_admin() && wp_should_load_block_editor_scripts_and_styles();
$load_editor_scripts_and_styles = is_admin() && wp_should_load_block_editor_scripts_and_styles();

$block_registry = WP_Block_Type_Registry::get_instance();
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
// Front-end styles.
if ( ! empty( $block_type->style ) ) {
foreach ( (array) $block_type->style as $single_style ) {
wp_enqueue_style( $single_style );
}
// Front-end and editor styles.
foreach ( $block_type->style_handles as $style_handle ) {
wp_enqueue_style( $style_handle );
}

// Front-end scripts.
if ( ! empty( $block_type->script ) ) {
foreach ( (array) $block_type->script as $single_script ) {
wp_enqueue_script( $single_script );
}
// Front-end and editor scripts.
foreach ( $block_type->script_handles as $script_handle ) {
wp_enqueue_script( $script_handle );
}

// Editor styles.
if ( $load_editor_scripts && ! empty( $block_type->editor_style ) ) {
foreach ( (array) $block_type->editor_style as $single_editor_style ) {
wp_enqueue_style( $single_editor_style );
if ( $load_editor_scripts_and_styles ) {
// Editor styles.
foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
wp_enqueue_style( $editor_style_handle );
}
}

// Editor scripts.
if ( $load_editor_scripts && ! empty( $block_type->editor_script ) ) {
foreach ( (array) $block_type->editor_script as $single_editor_script ) {
wp_enqueue_script( $single_editor_script );
// Editor scripts.
foreach ( $block_type->editor_script_handles as $editor_script_handle ) {
wp_enqueue_script( $editor_script_handle );
}
}
}
Expand Down

0 comments on commit 772fb62

Please sign in to comment.