Make WordPress Core


Ignore:
Timestamp:
10/24/2018 12:35:51 AM (7 years ago)
Author:
pento
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/script-loader.php

    r43803 r43812  
    21792179    }
    21802180}
     2181
     2182/**
     2183 * Handles the enqueueing of block scripts and styles that are common to both
     2184 * the editor and the front-end.
     2185 *
     2186 * @since 5.0.0
     2187 *
     2188 * @global WP_Screen $current_screen
     2189 */
     2190function wp_common_block_scripts_and_styles() {
     2191    global $current_screen;
     2192
     2193    if ( is_admin() && ! $current_screen->is_block_editor() ) {
     2194        return;
     2195    }
     2196
     2197    wp_enqueue_style( 'wp-block-library' );
     2198
     2199    if ( current_theme_supports( 'wp-block-styles' ) ) {
     2200        wp_enqueue_style( 'wp-block-library-theme' );
     2201    }
     2202
     2203    /**
     2204     * Fires after enqueuing block assets for both editor and front-end.
     2205     *
     2206     * Call `add_action` on any hook before 'wp_enqueue_scripts'.
     2207     *
     2208     * In the function call you supply, simply use `wp_enqueue_script` and
     2209     * `wp_enqueue_style` to add your functionality to the Gutenberg editor.
     2210     *
     2211     * @since 5.0.0
     2212     */
     2213      do_action( 'enqueue_block_assets' );
     2214}
     2215
     2216/**
     2217 * Enqueues registered block scripts and styles, depending on current rendered
     2218 * context (only enqueuing editor scripts while in context of the editor).
     2219 *
     2220 * @since 5.0.0
     2221 *
     2222 * @global WP_Screen $current_screen
     2223 */
     2224function wp_enqueue_registered_block_scripts_and_styles() {
     2225    global $current_screen;
     2226
     2227    $is_editor = ( is_admin() && $current_screen->is_block_editor() );
     2228
     2229    $block_registry = WP_Block_Type_Registry::get_instance();
     2230    foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
     2231        // Front-end styles.
     2232        if ( ! empty( $block_type->style ) ) {
     2233            wp_enqueue_style( $block_type->style );
     2234        }
     2235
     2236        // Front-end script.
     2237        if ( ! empty( $block_type->script ) ) {
     2238            wp_enqueue_script( $block_type->script );
     2239        }
     2240
     2241        // Editor styles.
     2242        if ( $is_editor && ! empty( $block_type->editor_style ) ) {
     2243            wp_enqueue_style( $block_type->editor_style );
     2244        }
     2245
     2246        // Editor script.
     2247        if ( $is_editor && ! empty( $block_type->editor_script ) ) {
     2248            wp_enqueue_script( $block_type->editor_script );
     2249        }
     2250    }
     2251}
Note: See TracChangeset for help on using the changeset viewer.