Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active September 14, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/3022a069c83fa298877f83968ba02fd1 to your computer and use it in GitHub Desktop.
Save westonruter/3022a069c83fa298877f83968ba02fd1 to your computer and use it in GitHub Desktop.
<?php
/**
* Generate ordered list of scripts according to their dependencies and whether they are enqueued for the header or footer.
* This may be called in a Grunt task to obtain the list of assets to concatenate and minify.
* Usage: wp eval-file wp-generate-script-bundle-manifest.php
* Author: Weston Ruter, XWP
*/
wp_enqueue_scripts();
wp_scripts()->all_deps( wp_scripts()->queue );
$groups = array(
0 => 'head',
1 => 'footer',
);
$group_assets = array();
foreach ( $groups as $group => $group_name ) {
$assets = array();
foreach ( wp_scripts()->to_do as $key => $handle ) {
if ( ! in_array( $handle, wp_scripts()->done, true ) && isset( wp_scripts()->registered[ $handle ] ) ) {
if ( 0 === $group && wp_scripts()->groups[ $handle ] > 0 ) {
wp_scripts()->in_footer[] = $handle;
continue;
}
if ( false === $group && in_array( $handle, wp_scripts()->in_footer, true ) ) {
wp_scripts()->in_footer = array_diff( wp_scripts()->in_footer, (array) $handle );
}
unset( wp_scripts()->to_do[ $key ] );
wp_scripts()->done[] = $handle;
$assets[ $handle ] = wp_scripts()->registered[ $handle ]->src;
}
}
$group_assets[ $group_name ] = array_filter( $assets );
}
echo json_encode( $group_assets, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment