Skip to content

Commit

Permalink
Read block styles from parent and child themes
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed May 14, 2018
1 parent 7785f33 commit 5553e15
Showing 1 changed file with 49 additions and 39 deletions.
88 changes: 49 additions & 39 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,48 +850,58 @@ function gutenberg_enqueue_registered_block_scripts_and_styles() {
* @since 2.9.0
*/
function gutenberg_enqueue_theme_block_styles() {
$is_editor = ( 'enqueue_block_editor_assets' === current_action() );
$is_editor = 'enqueue_block_editor_assets' === current_action();
$core_block_style_dependencies = $is_editor ?
array( 'wp-core-blocks', 'wp-edit-blocks' ) :
array( 'wp-core-blocks' );
$block_registry = WP_Block_Type_Registry::get_instance();

$block_registry = WP_Block_Type_Registry::get_instance();
$block_themes_directory = get_template_directory() . '/blocks/';
$block_themes_uri = get_template_directory_uri() . '/blocks/';

if (
file_exists( $block_themes_directory ) && (
$block_themes_handle = opendir( $block_themes_directory )
) ) {
$core_block_style_dependencies = $is_editor ?
array( 'wp-core-blocks', 'wp-edit-blocks' ) :
array( 'wp-core-blocks' );

while ( $filename = readdir( $block_themes_handle ) ) {
if (
is_file( $block_themes_directory . $filename ) &&
pathinfo( $filename, PATHINFO_EXTENSION ) === 'css'
) {
$block_name = str_replace( '.', '/', pathinfo( $filename, PATHINFO_FILENAME ) );
$block_type = $block_registry->get_registered( $block_name );
$block_style_handle = 'theme-block-style-' . $block_name;
$block_style_uri = $block_themes_uri . $filename;

if ( $block_type ) {
$theme_style_dependencies = array();

if ( ! empty( $block_type->style ) ) {
$theme_style_dependencies += $block_type->style;
}
// Add child theme first to give precedence over parent theme.
$theme_directories = array( get_stylesheet_directory() => get_stylesheet_directory_uri() );

if ( $is_editor && ! empty( $block_style->editor_style ) ) {
$theme_style_dependencies += $block_type->editor_style;
}
if ( get_template_directory() !== get_stylesheet_directory() ) {
$theme_directories[ get_template_directory() ] = get_template_directory_uri();
}

wp_enqueue_style(
$block_style_handle, $block_style_uri, $theme_style_dependencies
);
} elseif ( preg_match( '/^core\//', $block_name ) ) {
wp_enqueue_style(
$block_style_handle, $block_style_uri, $core_block_style_dependencies
);
foreach ( $theme_directories as $directory => $directory_uri ) {
$block_themes_directory = $directory . '/blocks/';
$block_themes_uri = $directory_uri . '/blocks/';

if (
file_exists( $block_themes_directory ) && (
$block_themes_handle = opendir( $block_themes_directory )
) ) {
while ( $filename = readdir( $block_themes_handle ) ) {
if (
is_file( $block_themes_directory . $filename ) &&
pathinfo( $filename, PATHINFO_EXTENSION ) === 'css'
) {
$block_name = str_replace( '.', '/', pathinfo( $filename, PATHINFO_FILENAME ) );
$block_type = $block_registry->get_registered( $block_name );
$block_style_handle = 'theme-block-style-' . $block_name;
$block_style_uri = $block_themes_uri . $filename;

if ( ! wp_style_is( $block_style_handle ) ) {
if ( $block_type ) {
$theme_style_dependencies = array();

if ( ! empty( $block_type->style ) ) {
$theme_style_dependencies += $block_type->style;
}

if ( $is_editor && ! empty( $block_style->editor_style ) ) {
$theme_style_dependencies += $block_type->editor_style;
}

wp_enqueue_style(
$block_style_handle, $block_style_uri, $theme_style_dependencies
);
} elseif ( preg_match( '/^core\//', $block_name ) ) {
wp_enqueue_style(
$block_style_handle, $block_style_uri, $core_block_style_dependencies
);
}
}
}
}
}
Expand Down

0 comments on commit 5553e15

Please sign in to comment.