reconcile bower dependency versions with CDN url
This commit is contained in:
@@ -38,7 +38,6 @@ function asset_path($filename) {
|
||||
$dist_path = get_template_directory_uri() . '/dist/';
|
||||
$directory = dirname($filename) . '/';
|
||||
$file = basename($filename);
|
||||
|
||||
static $manifest;
|
||||
|
||||
if (empty($manifest)) {
|
||||
@@ -54,6 +53,31 @@ function asset_path($filename) {
|
||||
}
|
||||
}
|
||||
|
||||
function bower_map_to_cdn($dependency, $fallback) {
|
||||
static $bower;
|
||||
|
||||
if (empty($bower)) {
|
||||
$bower_path = get_template_directory() . '/bower.json';
|
||||
$bower = new JsonManifest($bower_path);
|
||||
$bower = $bower->get();
|
||||
}
|
||||
|
||||
$templates = [
|
||||
'google' => '//ajax.googleapis.com/ajax/libs/%name%/%version%/%file%'
|
||||
];
|
||||
|
||||
$version = $bower['dependencies'][$dependency['name']];
|
||||
|
||||
if (isset($version) && preg_match('/^(\d+\.){2}\d+$/', $version)) {
|
||||
$search = ['%name%', '%version%', '%file%'];
|
||||
$replace = [$dependency['name'], $version, $dependency['file']];
|
||||
return str_replace($search, $replace, $templates[$dependency['cdn']]);
|
||||
} else {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function assets() {
|
||||
wp_enqueue_style('sage_css', asset_path('styles/main.css'), false, null);
|
||||
|
||||
@@ -66,7 +90,11 @@ function assets() {
|
||||
if (!is_admin() && current_theme_supports('jquery-cdn')) {
|
||||
wp_deregister_script('jquery');
|
||||
|
||||
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', [], null, true);
|
||||
wp_register_script('jquery', bower_map_to_cdn([
|
||||
'name' => 'jquery',
|
||||
'cdn' => 'google',
|
||||
'file' => 'jquery.min.js'
|
||||
], asset_path('scripts/jquery.js')), [], null, true);
|
||||
|
||||
add_filter('script_loader_src', __NAMESPACE__ . '\\jquery_local_fallback', 10, 2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user