From 23984141ce5b36322b49563d6c3a77bfdaf38f33 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 24 Oct 2018 00:36:39 +0000 Subject: [PATCH] Styles: Add helper functions for loading block styles. Blocks are able to register styles that used in the editor and the frontend, or only in the editor. These functions ensure the correct styles are loaded in the correct place. See #45065. Built from https://develop.svn.wordpress.org/branches/5.0@43812 git-svn-id: http://core.svn.wordpress.org/branches/5.0@43641 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/default-filters.php | 4 ++ wp-includes/script-loader.php | 71 +++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 56c9b6b75c..dfc0508f05 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -495,6 +495,10 @@ add_action( 'wp_default_scripts', 'wp_default_packages' ); add_action( 'wp_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 ); add_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 ); +add_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' ); +add_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' ); +add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); +add_action( 'enqueue_block_editor_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); add_action( 'admin_print_scripts-index.php', 'wp_localize_community_events' ); add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' ); add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' ); diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index b5f66b5cee..b1718c2602 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -2178,3 +2178,74 @@ function script_concat_settings() { $compress_css = false; } } + +/** + * Handles the enqueueing of block scripts and styles that are common to both + * the editor and the front-end. + * + * @since 5.0.0 + * + * @global WP_Screen $current_screen + */ +function wp_common_block_scripts_and_styles() { + global $current_screen; + + if ( is_admin() && ! $current_screen->is_block_editor() ) { + return; + } + + wp_enqueue_style( 'wp-block-library' ); + + if ( current_theme_supports( 'wp-block-styles' ) ) { + wp_enqueue_style( 'wp-block-library-theme' ); + } + + /** + * Fires after enqueuing block assets for both editor and front-end. + * + * Call `add_action` on any hook before 'wp_enqueue_scripts'. + * + * In the function call you supply, simply use `wp_enqueue_script` and + * `wp_enqueue_style` to add your functionality to the Gutenberg editor. + * + * @since 5.0.0 + */ + do_action( 'enqueue_block_assets' ); +} + +/** + * Enqueues registered block scripts and styles, depending on current rendered + * context (only enqueuing editor scripts while in context of the editor). + * + * @since 5.0.0 + * + * @global WP_Screen $current_screen + */ +function wp_enqueue_registered_block_scripts_and_styles() { + global $current_screen; + + $is_editor = ( is_admin() && $current_screen->is_block_editor() ); + + $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 ) ) { + wp_enqueue_style( $block_type->style ); + } + + // Front-end script. + if ( ! empty( $block_type->script ) ) { + wp_enqueue_script( $block_type->script ); + } + + // Editor styles. + if ( $is_editor && ! empty( $block_type->editor_style ) ) { + wp_enqueue_style( $block_type->editor_style ); + } + + // Editor script. + if ( $is_editor && ! empty( $block_type->editor_script ) ) { + wp_enqueue_script( $block_type->editor_script ); + } + } +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 17d71d1452..84803e2b4f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '5.0-alpha-43811'; +$wp_version = '5.0-alpha-43812'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.