Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active September 27, 2020 14:50
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 wpmudev-sls/a6e9acadf27ab097620d104f0944dbc6 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/a6e9acadf27ab097620d104f0944dbc6 to your computer and use it in GitHub Desktop.
[Hummingbird Pro] - Fix priority on midified assets
<?php
/**
* Plugin Name: [Hummingbird Pro] - Fix priority on midified assets
* Description: [Hummingbird Pro] - Fix priority on midified assets - 1127222222819939/1138336721259729/1122891649416329
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
add_action( 'after_setup_theme', 'wpmudev_hmbp_fix_jquery_undefined_after_enabled_cdn_func', 100 );
function wpmudev_hmbp_fix_jquery_undefined_after_enabled_cdn_func() {
if( defined('WPHB_VERSION') && class_exists( 'Hummingbird\WP_Hummingbird' ) && class_exists('Hummingbird\Core\Settings') ){
if( is_admin() || ! Hummingbird\Core\Settings::get_setting( 'enabled', 'minify' ) ){
return;
}
class WPMUDEV_HMBP_Reoder_Assets{
public function __construct(){
add_filter( 'print_styles_array', array( $this, 'save_priority_styles' ), 4 );
add_filter( 'print_styles_array', array( $this, 'resort_priority_assets' ), 6 );
add_filter( 'print_scripts_array', array( $this, 'save_priority_scripts' ), 4 );
add_filter( 'print_scripts_array', array( $this, 'resort_priority_assets' ), 6 );
}
public function save_priority_styles( $handles ){
return $this->save_priority_assets( $handles, 'styles' );
}
public function save_priority_scripts( $handles ){
return $this->save_priority_assets( $handles, 'scripts' );
}
public function save_priority_assets( $handles, $type ){
global $wpmudev_hmbp_handles_assets;
if( $handles ){
if( $type === 'scripts' ){
global $wpmudev_hmbp_scrips_move_to_footer;
if( ! Hummingbird\Core\Modules\Minify::is_in_footer() ){
global $wp_scripts;
$wpmudev_hmbp_scrips_move_to_footer = [];
$have_change = 0;
foreach( $handles as $key => $handle ){
// if has deps
if( ! empty( $wp_scripts->registered[ $handle ]->deps ) ){
$move_to_footer = false;
foreach( $wp_scripts->registered[ $handle ]->deps as $dep ){
if( isset( $wpmudev_hmbp_scrips_move_to_footer[ $dep ] ) ){
$move_to_footer = true;
}elseif( ! empty( $wp_scripts->registered[ $dep ]->extra['group'] ) ){
// this will move to footer so we should move the child lib to footer too
$wpmudev_hmbp_scrips_move_to_footer[$dep] = $dep;
$move_to_footer = true;
$parent_key = array_search( $dep, $handles );
unset( $handles[ $parent_key ] );
}
}
if( $move_to_footer ){
$wpmudev_hmbp_scrips_move_to_footer[ $handle ] = $handle;
unset( $handles[ $key ] );
$have_change = 1;
}
}
}
if( $have_change ){
$handles = array_values( $handles );
}
}elseif( $wpmudev_hmbp_scrips_move_to_footer ){
$handles = array_unique( array_merge( array_values( $wpmudev_hmbp_scrips_move_to_footer ), $handles ) );
}
}
$wpmudev_hmbp_handles_assets = $handles;
}
return $handles;
}
public function resort_priority_assets( $handles ){
global $wpmudev_hmbp_handles_assets;
if( $wpmudev_hmbp_handles_assets && $handles && isset( $wpmudev_hmbp_handles_assets[0], $handles[0] ) ){
if( $wpmudev_hmbp_handles_assets[0] !== $handles[0] ){
// if have error handle assets maybe we need reorder priority
$wpmudev_hmbp_handles_assets = array_flip($wpmudev_hmbp_handles_assets);
usort($handles, function( $handle1, $handle2 ){
global $wpmudev_hmbp_handles_assets;
if( isset( $wpmudev_hmbp_handles_assets[ $handle1 ], $wpmudev_hmbp_handles_assets[ $handle2 ] ) ){
return $wpmudev_hmbp_handles_assets[ $handle1 ] < $wpmudev_hmbp_handles_assets[ $handle2 ] ? -1 : 1;
}
return 0;
});
}else{
// look like all fine return here
}
// unset
unset( $wpmudev_hmbp_handles_assets );
}
return $handles;
}
}
$run = new WPMUDEV_HMBP_Reoder_Assets;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment