|
17 | 17 | * Enqueue stylesheets for blocks. Each stylesheet will be enqueued on-render. |
18 | 18 | * |
19 | 19 | * @see https://developer.wordpress.org/reference/functions/wp_enqueue_block_style/ |
20 | | - * @return void |
21 | 20 | */ |
22 | 21 | function enqueue_block_styles(): void { |
23 | 22 | $folder_path = get_entry_dir_path( 'block-styles', true ); |
@@ -50,35 +49,42 @@ function enqueue_block_styles(): void { |
50 | 49 | return; |
51 | 50 | } |
52 | 51 |
|
53 | | - $css_files = array_filter( |
| 52 | + $block_directories = array_filter( |
54 | 53 | $folder_contents, |
55 | | - fn ( $item ): bool => pathinfo( (string) $item, PATHINFO_EXTENSION ) === 'css' |
| 54 | + fn ( string $item ): bool => is_dir( $folder . '/' . $item ) && ! in_array( $item, [ '.', '..' ], true ) |
56 | 55 | ); |
57 | 56 |
|
58 | | - // Create entry details. |
59 | | - foreach ( $css_files as $css_file ) { |
60 | | - $block_name = preg_replace( '/\.css$/', '', $css_file ); |
61 | | - $block_entries[] = [ |
62 | | - 'block_name' => $block_name, |
63 | | - 'block_namespace' => $dir . '/' . $block_name, |
64 | | - 'file_name' => $css_file, |
65 | | - 'file_path' => $folder . '/', |
66 | | - 'handle' => get_template() . '-' . $dir . '-' . $block_name, |
67 | | - ]; |
| 57 | + // Create entry details for each block directory. |
| 58 | + foreach ( $block_directories as $block_dir ) { |
| 59 | + $block_folder = $folder . '/' . $block_dir; |
| 60 | + $index_css_path = $block_folder . '/index.css'; |
| 61 | + |
| 62 | + // Check if index.css exists in the block directory. |
| 63 | + if ( file_exists( $index_css_path ) ) { |
| 64 | + $block_entries[] = [ |
| 65 | + 'block_name' => $block_dir, |
| 66 | + 'block_namespace' => $dir . '/' . $block_dir, |
| 67 | + 'file_name' => 'index.css', |
| 68 | + 'file_path' => $block_folder, |
| 69 | + 'handle' => get_template() . '-' . $dir . '-' . $block_dir, |
| 70 | + ]; |
| 71 | + } |
68 | 72 | } |
69 | 73 | } |
70 | 74 |
|
71 | 75 | foreach ( $block_entries as $block ) { |
| 76 | + $entry_dir = 'block-styles/' . $block['block_namespace']; |
| 77 | + |
72 | 78 | wp_enqueue_block_style( |
73 | 79 | $block['block_namespace'], |
74 | 80 | [ |
75 | 81 | 'handle' => $block['handle'] . '-styles', // Ex: `create-wordpress-theme-core-paragraph-styles`. |
76 | | - 'src' => get_template_directory_uri() . '/build/block-styles/' . $block['block_namespace'] . '.css', |
77 | | - 'deps' => get_asset_dependency_array( $block['block_namespace'] ), |
78 | | - 'ver' => get_asset_version( $block['block_namespace'] ), |
| 82 | + 'src' => get_template_directory_uri() . '/build/' . trailingslashit( $entry_dir ) . $block['file_name'], |
| 83 | + 'deps' => get_asset_dependency_array( $entry_dir ), |
| 84 | + 'ver' => get_asset_version( $entry_dir ), |
79 | 85 |
|
80 | 86 | // Adding "path" allows inlining of block styles on the frontend when possible. |
81 | | - 'path' => $block['file_path'] . $block['file_name'], |
| 87 | + 'path' => $block['file_path'] . '/' . $block['file_name'], |
82 | 88 | ] |
83 | 89 | ); |
84 | 90 | } |
|
0 commit comments