Make WordPress Core


Ignore:
Timestamp:
09/14/2019 06:20:58 PM (5 years ago)
Author:
jorgefilipecosta
Message:

Block Editor: Backport block styles server functions from block editor.

This commit backports the block styles functionality added to the block editor in https://github.com/WordPress/gutenberg/pull/16356.

Props: youknowriad, aduth, swissspidy.
Fixes #48039.

File:
1 edited

Legend:

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

    r46095 r46111  
    27812781    }
    27822782}
     2783
     2784/**
     2785 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
     2786 *
     2787 * @since 5.3.0
     2788 */
     2789function enqueue_block_styles_assets() {
     2790    $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
     2791
     2792    foreach ( $block_styles as $styles ) {
     2793        foreach ( $styles as $style_properties ) {
     2794            if ( isset( $style_properties['style_handle'] ) ) {
     2795                wp_enqueue_style( $style_properties['style_handle'] );
     2796            }
     2797            if ( isset( $style_properties['inline_style'] ) ) {
     2798                wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] );
     2799            }
     2800        }
     2801    }
     2802}
     2803
     2804/**
     2805 * Function responsible for enqueuing the assets required for block styles functionality on the editor.
     2806 *
     2807 * @since 5.3.0
     2808 */
     2809function enqueue_editor_block_styles_assets() {
     2810    $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
     2811
     2812    $register_script_lines = array( '( function() {' );
     2813    foreach ( $block_styles as $block_name => $styles ) {
     2814        foreach ( $styles as $style_properties ) {
     2815            $register_script_lines[] = sprintf(
     2816                '   wp.blocks.registerBlockStyle( \'%s\', %s );',
     2817                $block_name,
     2818                wp_json_encode(
     2819                    array(
     2820                        'name'  => $style_properties['name'],
     2821                        'label' => $style_properties['label'],
     2822                    )
     2823                )
     2824            );
     2825        }
     2826    }
     2827    $register_script_lines[] = '} )();';
     2828    $inline_script           = implode( "\n", $register_script_lines );
     2829
     2830    wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true );
     2831    wp_add_inline_script( 'wp-block-styles', $inline_script );
     2832    wp_enqueue_script( 'wp-block-styles' );
     2833}
Note: See TracChangeset for help on using the changeset viewer.