Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent style assets being enqueued at head with 'wp_footer' load hook set #20

Open
wants to merge 1 commit into
base: production
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions php/class-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,6 @@ public function add_asset( $args ) {

$args = $this->pre_add_asset( $args );

// Enqueue asset if applicable.
if ( in_array( $args['load_method'], $this->wp_enqueue_methods, true ) && empty( $args['loaded'] ) ) {
if ( function_exists( $wp_enqueue_function ) ) {
$wp_enqueue_function(
$args['handle'],
$args['src'],
$args['deps'],
$args['version'],
'style' === $args['type'] ? $args['media'] : $args['in_footer']
);
$args['loaded'] = true;
} else {
echo wp_kses_post( $this->format_error( $this->generate_asset_error( 'invalid_enqueue_function', false, $wp_enqueue_function ) ) );
}
}

// Add to asset arrays.
// phpcs:disable Generic.Formatting.MultipleStatementAlignment
$this->assets[] = $args;
Expand All @@ -320,9 +304,29 @@ public function add_asset( $args ) {
*/
public function load_assets() {
foreach ( $this->assets as $idx => $asset ) {
if ( $this->asset_should_load( $asset ) ) {
$this->print_asset( $asset );
$this->assets[ $idx ]['loaded'] = true;
if ( !$this->asset_should_load( $asset ) ) {
return;
}

// Enqueue asset if applicable.
if ( in_array( $asset['load_method'], $this->wp_enqueue_methods, true ) && empty( $args['loaded'] ) ) {
$wp_enqueue_function = $this->wp_enqueue_function;

if ( function_exists( $wp_enqueue_function ) ) {
$wp_enqueue_function(
$asset['handle'],
$asset['src'],
$asset['deps'],
$asset['version'],
'style' === $asset['type'] ? $asset['media'] : $asset['in_footer']
);
$this->assets[ $idx ]['loaded'] = true;
} else {
$this->generate_asset_error( 'invalid_enqueue_function', [], $wp_enqueue_function );
}
} else {
$this->print_asset($asset);
$this->assets[$idx]['loaded'] = true;
}
}
}
Expand Down Expand Up @@ -600,7 +604,7 @@ public function asset_should_load( $asset ) {
/**
* Generate and echo a WP_Error based on a provided error code
*
* @param array $code Error code.
* @param string $code Error code.
* @param array $asset Offending asset.
* @param array|string $info Additional information about a dependency or dependent.
*/
Expand Down