Merge pull request #1301 from austinpray/fix1286CDN
[8.0.0] reconcile bower dependency versions with CDN url
This commit is contained in:
@@ -17,26 +17,83 @@ namespace Roots\Sage\Assets;
|
||||
* - An ID has been defined in config.php
|
||||
* - You're not logged in as an administrator
|
||||
*/
|
||||
|
||||
class JsonManifest {
|
||||
private $manifest;
|
||||
|
||||
public function __construct($manifest_path) {
|
||||
if (file_exists($manifest_path)) {
|
||||
$this->manifest = json_decode(file_get_contents($manifest_path), true);
|
||||
} else {
|
||||
$this->manifest = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function get() {
|
||||
return $this->manifest;
|
||||
}
|
||||
|
||||
public function getPath($key = '', $default = null) {
|
||||
$collection = $this->manifest;
|
||||
if (is_null($key)) {
|
||||
return $collection;
|
||||
}
|
||||
if (isset($collection[$key])) {
|
||||
return $collection[$key];
|
||||
}
|
||||
foreach (explode('.', $key) as $segment) {
|
||||
if (!isset($collection[$segment])) {
|
||||
return $default;
|
||||
} else {
|
||||
$collection = $collection[$segment];
|
||||
}
|
||||
}
|
||||
return $collection;
|
||||
}
|
||||
}
|
||||
|
||||
function asset_path($filename) {
|
||||
$dist_path = get_template_directory_uri() . '/dist/';
|
||||
$directory = dirname($filename) . '/';
|
||||
$file = basename($filename);
|
||||
static $manifest;
|
||||
|
||||
$manifest_path = get_template_directory() . '/dist/assets.json';
|
||||
|
||||
if (file_exists($manifest_path)) {
|
||||
$manifest = json_decode(file_get_contents($manifest_path), true);
|
||||
} else {
|
||||
$manifest = [];
|
||||
if (empty($manifest)) {
|
||||
$manifest_path = get_template_directory() . '/dist/assets.json';
|
||||
$manifest = new JsonManifest($manifest_path);
|
||||
}
|
||||
|
||||
if (WP_ENV !== 'development' && array_key_exists($file, $manifest)) {
|
||||
return $dist_path . $directory . $manifest[$file];
|
||||
if (WP_ENV !== 'development' && array_key_exists($file, $manifest->get())) {
|
||||
return $dist_path . $directory . $manifest->get()[$file];
|
||||
} else {
|
||||
return $dist_path . $directory . $file;
|
||||
}
|
||||
}
|
||||
|
||||
function bower_map_to_cdn($dependency, $fallback) {
|
||||
static $bower;
|
||||
|
||||
if (empty($bower)) {
|
||||
$bower_path = get_template_directory() . '/bower.json';
|
||||
$bower = new JsonManifest($bower_path);
|
||||
}
|
||||
|
||||
$templates = [
|
||||
'google' => '//ajax.googleapis.com/ajax/libs/%name%/%version%/%file%'
|
||||
];
|
||||
|
||||
$version = $bower->getPath('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);
|
||||
|
||||
@@ -49,7 +106,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