Make WordPress Core

Changeset 53234


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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

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

    r53214 r53234  
    12301230
    12311231/**
    1232  * Enqueues a stylesheet for a specific block.
    1233  *
    1234  * If the theme has opted-in to separate-styles loading,
    1235  * then the stylesheet will be enqueued on-render,
    1236  * otherwise when the block inits.
    1237  *
    1238  * @since 5.9.0
    1239  *
    1240  * @param string $block_name The block-name, including namespace.
    1241  * @param array  $args       An array of arguments [handle,src,deps,ver,media].
    1242  * @return void
    1243  */
    1244 function wp_enqueue_block_style( $block_name, $args ) {
    1245     $args = wp_parse_args(
    1246         $args,
    1247         array(
    1248             'handle' => '',
    1249             'src'    => '',
    1250             'deps'   => array(),
    1251             'ver'    => false,
    1252             'media'  => 'all',
    1253         )
    1254     );
    1255 
    1256     /**
    1257      * Callback function to register and enqueue styles.
    1258      *
    1259      * @param string $content When the callback is used for the render_block filter,
    1260      *                        the content needs to be returned so the function parameter
    1261      *                        is to ensure the content exists.
    1262      * @return string Block content.
    1263      */
    1264     $callback = static function( $content ) use ( $args ) {
    1265         // Register the stylesheet.
    1266         if ( ! empty( $args['src'] ) ) {
    1267             wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );
    1268         }
    1269 
    1270         // Add `path` data if provided.
    1271         if ( isset( $args['path'] ) ) {
    1272             wp_style_add_data( $args['handle'], 'path', $args['path'] );
    1273 
    1274             // Get the RTL file path.
    1275             $rtl_file_path = str_replace( '.css', '-rtl.css', $args['path'] );
    1276 
    1277             // Add RTL stylesheet.
    1278             if ( file_exists( $rtl_file_path ) ) {
    1279                 wp_style_add_data( $args['handle'], 'rtl', 'replace' );
    1280 
    1281                 if ( is_rtl() ) {
    1282                     wp_style_add_data( $args['handle'], 'path', $rtl_file_path );
    1283                 }
    1284             }
    1285         }
    1286 
    1287         // Enqueue the stylesheet.
    1288         wp_enqueue_style( $args['handle'] );
    1289 
    1290         return $content;
    1291     };
    1292 
    1293     $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
    1294     if ( wp_should_load_separate_core_block_assets() ) {
    1295         /**
    1296          * Callback function to register and enqueue styles.
    1297          *
    1298          * @param string $content The block content.
    1299          * @param array  $block   The full block, including name and attributes.
    1300          * @return string Block content.
    1301          */
    1302         $callback_separate = static function( $content, $block ) use ( $block_name, $callback ) {
    1303             if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {
    1304                 return $callback( $content );
    1305             }
    1306             return $content;
    1307         };
    1308 
    1309         /*
    1310          * The filter's callback here is an anonymous function because
    1311          * using a named function in this case is not possible.
    1312          *
    1313          * The function cannot be unhooked, however, users are still able
    1314          * to dequeue the stylesheets registered/enqueued by the callback
    1315          * which is why in this case, using an anonymous function
    1316          * was deemed acceptable.
    1317          */
    1318         add_filter( 'render_block', $callback_separate, 10, 2 );
    1319         return;
    1320     }
    1321 
    1322     /*
    1323      * The filter's callback here is an anonymous function because
    1324      * using a named function in this case is not possible.
    1325      *
    1326      * The function cannot be unhooked, however, users are still able
    1327      * to dequeue the stylesheets registered/enqueued by the callback
    1328      * which is why in this case, using an anonymous function
    1329      * was deemed acceptable.
    1330      */
    1331     add_filter( $hook, $callback );
    1332 
    1333     // Enqueue assets in the editor.
    1334     add_action( 'enqueue_block_assets', $callback );
    1335 }
    1336 
    1337 /**
    13381232 * Allow multiple block styles.
    13391233 *
  • 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.