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 12, 2022
1 parent 9b78470 commit b99cb16
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 49 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
9 changes: 5 additions & 4 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,16 @@ 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_core_block_first_style = $is_core_block && 0 === $index;
if ( $style_handle === $style_path && ! $is_core_block_first_style ) {
return $style_handle;
}

// Check whether styles should have a ".min" suffix or not.
$suffix = SCRIPT_DEBUG ? '' : '.min';
$style_uri = plugins_url( $style_path, $metadata['file'] );
if ( $is_core_block ) {
if ( $is_core_block_first_style ) {
$style_path = "style$suffix.css";
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
}
Expand All @@ -219,7 +220,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
$block_dir = dirname( $metadata['file'] );
$style_file = realpath( "$block_dir/$style_path" );
$has_style_file = false !== $style_file;
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$version = ! $is_core_block_first_style && isset( $metadata['version'] ) ? $metadata['version'] : false;
$style_uri = $has_style_file ? $style_uri : false;
$result = wp_register_style(
$style_handle,
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 @@ -259,21 +259,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 b99cb16

Please sign in to comment.