Make WordPress Core


Ignore:
Timestamp:
12/14/2018 03:35:55 AM (7 years ago)
Author:
desrosj
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.

Props pento.

Merges [43812] to trunk.

See #45065.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/script-loader.php

    r44143 r44157  
    24162416    }
    24172417}
     2418
     2419/**
     2420 * Handles the enqueueing of block scripts and styles that are common to both
     2421 * the editor and the front-end.
     2422 *
     2423 * @since 5.0.0
     2424 *
     2425 * @global WP_Screen $current_screen
     2426 */
     2427function wp_common_block_scripts_and_styles() {
     2428    global $current_screen;
     2429
     2430    if ( is_admin() && ! $current_screen->is_block_editor() ) {
     2431        return;
     2432    }
     2433
     2434    wp_enqueue_style( 'wp-block-library' );
     2435
     2436    if ( current_theme_supports( 'wp-block-styles' ) ) {
     2437        wp_enqueue_style( 'wp-block-library-theme' );
     2438    }
     2439
     2440    /**
     2441     * Fires after enqueuing block assets for both editor and front-end.
     2442     *
     2443     * Call `add_action` on any hook before 'wp_enqueue_scripts'.
     2444     *
     2445     * In the function call you supply, simply use `wp_enqueue_script` and
     2446     * `wp_enqueue_style` to add your functionality to the Gutenberg editor.
     2447     *
     2448     * @since 5.0.0
     2449     */
     2450      do_action( 'enqueue_block_assets' );
     2451}
     2452
     2453/**
     2454 * Enqueues registered block scripts and styles, depending on current rendered
     2455 * context (only enqueuing editor scripts while in context of the editor).
     2456 *
     2457 * @since 5.0.0
     2458 *
     2459 * @global WP_Screen $current_screen
     2460 */
     2461function wp_enqueue_registered_block_scripts_and_styles() {
     2462    global $current_screen;
     2463
     2464    $is_editor = ( is_admin() && $current_screen->is_block_editor() );
     2465
     2466    $block_registry = WP_Block_Type_Registry::get_instance();
     2467    foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
     2468        // Front-end styles.
     2469        if ( ! empty( $block_type->style ) ) {
     2470            wp_enqueue_style( $block_type->style );
     2471        }
     2472
     2473        // Front-end script.
     2474        if ( ! empty( $block_type->script ) ) {
     2475            wp_enqueue_script( $block_type->script );
     2476        }
     2477
     2478        // Editor styles.
     2479        if ( $is_editor && ! empty( $block_type->editor_style ) ) {
     2480            wp_enqueue_style( $block_type->editor_style );
     2481        }
     2482
     2483        // Editor script.
     2484        if ( $is_editor && ! empty( $block_type->editor_script ) ) {
     2485            wp_enqueue_script( $block_type->editor_script );
     2486        }
     2487    }
     2488}
Note: See TracChangeset for help on using the changeset viewer.