Make WordPress Core


Ignore:
Timestamp:
04/20/2022 01:51:21 PM (2 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/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 *
Note: See TracChangeset for help on using the changeset viewer.