Make WordPress Core


Ignore:
Timestamp:
04/20/2022 01:51:21 PM (3 years ago)
Author:
audrasjb
Message:

Editor: Move wp_enqueue_block_style() to wp-includes/script-loader.php, for better consistency.

As a result of [52741] and [52743], wp_enqueue_block_support_styles() was moved to wp-includes/script-loader.php. However, wp_enqueue_block_style() was still defined in wp-includes/blocks.php. This changeset also moves wp_enqueue_block_support_styles() to wp-includes/script-loader.php for better consistency.

Follow-up to [52741], [52743].

Props SergeyBiryukov, mukesh27, audrasjb.
Fixes #55182.
See #55148.

File:
1 edited

Legend:

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

    r53173 r53234  
    29222922    );
    29232923}
     2924
     2925/**
     2926 * Enqueues a stylesheet for a specific block.
     2927 *
     2928 * If the theme has opted-in to separate-styles loading,
     2929 * then the stylesheet will be enqueued on-render,
     2930 * otherwise when the block inits.
     2931 *
     2932 * @since 5.9.0
     2933 *
     2934 * @param string $block_name The block-name, including namespace.
     2935 * @param array  $args       An array of arguments [handle,src,deps,ver,media].
     2936 * @return void
     2937 */
     2938function wp_enqueue_block_style( $block_name, $args ) {
     2939    $args = wp_parse_args(
     2940        $args,
     2941        array(
     2942            'handle' => '',
     2943            'src'    => '',
     2944            'deps'   => array(),
     2945            'ver'    => false,
     2946            'media'  => 'all',
     2947        )
     2948    );
     2949
     2950    /**
     2951     * Callback function to register and enqueue styles.
     2952     *
     2953     * @param string $content When the callback is used for the render_block filter,
     2954     *                        the content needs to be returned so the function parameter
     2955     *                        is to ensure the content exists.
     2956     * @return string Block content.
     2957     */
     2958    $callback = static function( $content ) use ( $args ) {
     2959        // Register the stylesheet.
     2960        if ( ! empty( $args['src'] ) ) {
     2961            wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );
     2962        }
     2963
     2964        // Add `path` data if provided.
     2965        if ( isset( $args['path'] ) ) {
     2966            wp_style_add_data( $args['handle'], 'path', $args['path'] );
     2967
     2968            // Get the RTL file path.
     2969            $rtl_file_path = str_replace( '.css', '-rtl.css', $args['path'] );
     2970
     2971            // Add RTL stylesheet.
     2972            if ( file_exists( $rtl_file_path ) ) {
     2973                wp_style_add_data( $args['handle'], 'rtl', 'replace' );
     2974
     2975                if ( is_rtl() ) {
     2976                    wp_style_add_data( $args['handle'], 'path', $rtl_file_path );
     2977                }
     2978            }
     2979        }
     2980
     2981        // Enqueue the stylesheet.
     2982        wp_enqueue_style( $args['handle'] );
     2983
     2984        return $content;
     2985    };
     2986
     2987    $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
     2988    if ( wp_should_load_separate_core_block_assets() ) {
     2989        /**
     2990         * Callback function to register and enqueue styles.
     2991         *
     2992         * @param string $content The block content.
     2993         * @param array  $block   The full block, including name and attributes.
     2994         * @return string Block content.
     2995         */
     2996        $callback_separate = static function( $content, $block ) use ( $block_name, $callback ) {
     2997            if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {
     2998                return $callback( $content );
     2999            }
     3000            return $content;
     3001        };
     3002
     3003        /*
     3004         * The filter's callback here is an anonymous function because
     3005         * using a named function in this case is not possible.
     3006         *
     3007         * The function cannot be unhooked, however, users are still able
     3008         * to dequeue the stylesheets registered/enqueued by the callback
     3009         * which is why in this case, using an anonymous function
     3010         * was deemed acceptable.
     3011         */
     3012        add_filter( 'render_block', $callback_separate, 10, 2 );
     3013        return;
     3014    }
     3015
     3016    /*
     3017     * The filter's callback here is an anonymous function because
     3018     * using a named function in this case is not possible.
     3019     *
     3020     * The function cannot be unhooked, however, users are still able
     3021     * to dequeue the stylesheets registered/enqueued by the callback
     3022     * which is why in this case, using an anonymous function
     3023     * was deemed acceptable.
     3024     */
     3025    add_filter( $hook, $callback );
     3026
     3027    // Enqueue assets in the editor.
     3028    add_action( 'enqueue_block_assets', $callback );
     3029}
Note: See TracChangeset for help on using the changeset viewer.