Make WordPress Core

Changeset 46111


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.

Location:
trunk/src
Files:
1 added
4 edited

Legend:

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

    r45393 r46111  
    357357    return has_blocks( $content ) ? 1 : 0;
    358358}
     359
     360/**
     361 * Registers a new block style.
     362 *
     363 * @since 5.3.0
     364 *
     365 * @param string $block_name       Block type name including namespace.
     366 * @param array  $style_properties Array containing the properties of the style name, label, style (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added).
     367 *
     368 * @return boolean True if the block style was registered with success and false otherwise.
     369 */
     370function register_block_style( $block_name, $style_properties ) {
     371    return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties );
     372}
     373
     374/**
     375 * Unregisters a block style.
     376 *
     377 * @since 5.3.0
     378 *
     379 * @param string $block_name       Block type name including namespace.
     380 * @param array  $block_style_name Block style name.
     381 *
     382 * @return boolean True if the block style was unregistered with success and false otherwise.
     383 */
     384function unregister_block_style( $block_name, $block_style_name ) {
     385    return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
     386}
  • trunk/src/wp-includes/default-filters.php

    r46066 r46111  
    502502add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
    503503add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 );
     504add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
     505add_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' );
    504506
    505507add_action( 'wp_default_styles', 'wp_default_styles' );
  • 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}
  • trunk/src/wp-settings.php

    r45739 r46111  
    257257require( ABSPATH . WPINC . '/rest-api/search/class-wp-rest-post-search-handler.php' );
    258258require( ABSPATH . WPINC . '/class-wp-block-type.php' );
     259require( ABSPATH . WPINC . '/class-wp-block-styles-registry.php' );
    259260require( ABSPATH . WPINC . '/class-wp-block-type-registry.php' );
    260261require( ABSPATH . WPINC . '/class-wp-block-parser.php' );
Note: See TracChangeset for help on using the changeset viewer.