Skip to content

Commit

Permalink
Merge pull request #6 from sixach/bugfix/remove-requires
Browse files Browse the repository at this point in the history
Remove `should_load_extension_assets`
  • Loading branch information
mahdiyazdani authored Mar 16, 2022
2 parents 47d2a0a + 0aa23d5 commit 64e527a
Showing 1 changed file with 2 additions and 29 deletions.
31 changes: 2 additions & 29 deletions src/class-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,33 +256,8 @@ public static function enqueue_frontend_assets(): void {
self::enqueue_styles_by_type( Asset_Type::FRONTEND_STYLE );
}

/**
* Returns if at least one of the blocks from the `requires` field
* is used or if the current screen is the editor screen.
*
* @see https://developer.wordpress.org/reference/functions/wp_should_load_block_editor_scripts_and_styles/
* @since 1.0.0
* @param array $extension Extension data array.
* @return bool
*/
private static function should_load_extension_assets( $extension ) {
if ( empty( $extension['requires'] ) ) {
return true;
}

// Bail early if at least one matching block is found.
foreach ( $extension['requires'] as $required_block ) {
if ( has_block( $required_block ) ) {
return true;
}
}
return wp_should_load_block_editor_scripts_and_styles();
}

/**
* Enqueue all scripts of the given type.
* Assets in the frontend are only enqueued if at least one
* block from `requires` is used.
*
* @since 1.0.0
* @param string $type Type of the asset (frontend, editor, both).
Expand All @@ -292,16 +267,14 @@ private static function should_load_extension_assets( $extension ) {
private static function enqueue_scripts_by_type( string $type ): void {
$extension_registry = Extension_Registry::get_instance();
foreach ( $extension_registry->get_registered_extensions() as $extension ) {
if ( ! empty( $extension[ $type ] ) && self::should_load_extension_assets( $extension ) ) {
if ( ! empty( $extension[ $type ] ) ) {
wp_enqueue_script( $extension[ $type ] );
}
}
}

/**
* Enqueue all styles of the given type.
* Assets in the frontend are only enqueued if at least one
* block from `requires` is used.
*
* @since 1.0.0
* @param string $type Type of the asset (frontend, editor, both).
Expand All @@ -311,7 +284,7 @@ private static function enqueue_scripts_by_type( string $type ): void {
private static function enqueue_styles_by_type( string $type ): void {
$extension_registry = Extension_Registry::get_instance();
foreach ( $extension_registry->get_registered_extensions() as $extension ) {
if ( ! empty( $extension[ $type ] ) && self::should_load_extension_assets( $extension ) ) {
if ( ! empty( $extension[ $type ] ) ) {
wp_enqueue_style( $extension[ $type ] );
}
}
Expand Down

0 comments on commit 64e527a

Please sign in to comment.