Last active
August 30, 2019 04:31
-
-
Save wpmudev-sls/2e71a87636e8609f4a93571839366c61 to your computer and use it in GitHub Desktop.
[Hummingbird Pro] - Fix jQuery undefined after enabled CDN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Hummingbird Pro] - Fix jQuery undefined after enabled CDN | |
* Description: [Hummingbird Pro] - Fix jQuery undefined after enabled CDN - Required 2.1.0 - 1122891649416329 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/profile/tho2757 | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', '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') ){ | |
// only custom for front-end | |
if( is_admin() || ! Hummingbird\Core\Settings::get_setting( 'use_cdn', 'minify' ) ){ | |
return; | |
} | |
class WPMUDEV_HB_Fix_jQuery_Undefined{ | |
private $uncompress_styles = []; | |
private $uncompress_scripts = []; | |
private $uncompress_ids = []; | |
public function __construct(){ | |
$this->uncompress_styles = apply_filters( 'wpmudev_hb_list_uncompress_styles', array( | |
'fonts-awesome' // list handle name | |
) ); | |
$this->uncompress_scripts = apply_filters( 'wpmudev_hb_list_uncompress_scripts', array() ); | |
add_action( 'wp_head', array( $this, 'uncompress_assets'), 2 ); | |
add_action( 'wp_print_scripts', array( $this, 'load_jquery' ), 0 ); | |
} | |
public function get_file_id( $handle_urls, $type='scripts' ){ | |
$post_title = wp_hash( maybe_serialize( $handle_urls ) ) . '-' . $type; | |
$post_ids = get_posts( | |
array( | |
'name' => $post_title, | |
'post_type' => 'wphb_minify_group', | |
'numberposts' => 1, | |
'fields' => 'ids', | |
) | |
); | |
return $post_ids ? end( $post_ids ) : 0; | |
} | |
public function get_cdn_link($handle_urls, $type='scripts'){ | |
$file_id = $this->get_file_id( $handle_urls, $type ); | |
if( $file_id ){ | |
$cdn_link = get_post_meta( $file_id, '_url', true ); | |
}else{ | |
$cdn_link = false; | |
} | |
return $cdn_link; | |
} | |
public function load_jquery() { | |
global $wp_scripts; | |
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { | |
$wp_scripts = wp_scripts(); | |
} | |
// uncompress_lib | |
if( ! empty( $this->list_uncompress_lib['scripts'] ) ){ | |
foreach( $this->list_uncompress_lib['scripts'] as $handle ){ | |
$this->list_uncompress_lib[ 'scripts' ][ $file_id ] = $handle; | |
} | |
} | |
// load jquery | |
$wp_scripts->done[] = 'jquery-core'; | |
$wp_scripts->done[] = 'jquery-migrate'; | |
$wp_scripts->done[] = "jquery"; | |
$jquery_src = "/wp-includes/js/jquery/jquery.js"; | |
$jquery_migrate = "/wp-includes/js/jquery/jquery-migrate.min.js"; | |
if( isset( $wp_scripts->registered['jquery-core'] ) ){ | |
$jquery_src = $wp_scripts->registered['jquery-core']->src; | |
$handle_urls = [$wp_scripts->registered['jquery-core']->handle => $jquery_src]; | |
$cdn_link = $this->get_cdn_link( $handle_urls, 'scripts' ); | |
if( $cdn_link ){ | |
$jquery_src = $cdn_link; | |
}else{ | |
$jquery_src .='?ver='. $wp_scripts->registered['jquery-core']->ver; | |
} | |
if( isset( $wp_scripts->registered['jquery-migrate']) ){ | |
$jquery_migrate = $wp_scripts->registered['jquery-migrate']->src; | |
$handle_urls = [$wp_scripts->registered['jquery-migrate']->handle => $jquery_src]; | |
$cdn_link = $this->get_cdn_link( $handle_urls, 'scripts' ); | |
if( $cdn_link ){ | |
$jquery_migrate = $cdn_link; | |
}else{ | |
$jquery_migrate .='?ver='. $wp_scripts->registered['jquery-core']->ver; | |
} | |
} | |
} | |
if ( ! preg_match( '|^(https?:)?//|', $jquery_src ) && ! ( $wp_scripts->content_url && 0 === strpos( $jquery_src, $wp_scripts->content_url ) ) ) { | |
$jquery_src = $wp_scripts->base_url . $jquery_src; | |
} | |
if ( ! preg_match( '|^(https?:)?//|', $jquery_migrate ) && ! ( $wp_scripts->content_url && 0 === strpos( $jquery_migrate, $wp_scripts->content_url ) ) ) { | |
$jquery_migrate = $wp_scripts->base_url . $jquery_migrate; | |
} | |
echo "<script type='text/javascript' src='{$jquery_src}'></script>"; | |
echo "<script type='text/javascript' src='{$jquery_migrate}'></script>"; | |
} | |
public function uncompress_assets(){ | |
// styles | |
$this->uncompress_styles(); | |
// scripts | |
$this->uncompress_scripts(); | |
if( ! empty( $this->uncompress_ids ) ){ | |
add_filter( 'get_post_metadata', array( $this, 'disable_cdn_link_for_uncompress_assets'), 10, 3 ); | |
} | |
} | |
public function uncompress($handle, $asset_obj, $type ){ | |
if( ! isset( $asset_obj->registered[ $handle ] ) || ( $asset_obj->content_url && false !== strpos( $asset_obj->registered[ $handle ]->src, $asset_obj->content_url ) ) ){ | |
return; | |
} | |
$handle_urls = [$asset_obj->registered[ $handle ]->handle => $asset_obj->registered[ $handle ]->src]; | |
$file_id = $this->get_file_id( $handle_urls, $type ); | |
if( $file_id ){ | |
$this->uncompress_ids[ $file_id ] = $handle; | |
} | |
} | |
public function uncompress_scripts(){ | |
if( empty( $this->uncompress_scripts ) || ! is_array( $this->uncompress_scripts ) ) return; | |
global $wp_scripts; | |
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { | |
$wp_scripts = wp_scripts(); | |
} | |
foreach( $this->uncompress_scripts as $handle ){ | |
// un compress styles | |
$this->uncompress( $handle, $wp_styles, 'styles' ); | |
} | |
} | |
public function uncompress_styles(){ | |
if( empty( $this->uncompress_styles ) || ! is_array( $this->uncompress_styles ) ) return; | |
$wp_styles = wp_styles(); | |
foreach( $this->uncompress_styles as $handle ){ | |
// un compress styles | |
$this->uncompress( $handle, $wp_styles, 'styles' ); | |
} | |
} | |
public function disable_cdn_link_for_uncompress_assets( $val, $post_id, $key){ | |
if( '_url' !== $key ){ | |
return $val; | |
} | |
if( isset( $this->uncompress_ids[ $post_id ] ) ){ | |
$val = false; | |
} | |
return $val; | |
} | |
} | |
add_action( 'init', function(){ | |
$run = new WPMUDEV_HB_Fix_jQuery_Undefined(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment