Files
sage/lib/assets.php
2015-01-31 00:33:07 -06:00

159 lines
4.7 KiB
PHP

<?php
namespace Roots\Sage\Assets;
/**
* Scripts and stylesheets
*
* Enqueue stylesheets in the following order:
* 1. /theme/dist/styles/main.css
*
* Enqueue scripts in the following order:
* 1. Latest jQuery via Google CDN (if enabled in config.php)
* 2. /theme/dist/scripts/modernizr.js
* 3. /theme/dist/scripts/main.js
*
* Google Analytics is loaded after enqueued scripts if:
* - 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;
}
}
function asset_path($filename) {
$dist_path = get_template_directory_uri() . '/dist/';
$directory = dirname($filename) . '/';
$file = basename($filename);
static $manifest;
if (empty($manifest)) {
$manifest_path = get_template_directory() . '/dist/assets.json';
$manifest = new JsonManifest($manifest_path);
$manifest = $manifest->get();
}
if (WP_ENV !== 'development' && array_key_exists($file, $manifest)) {
return $dist_path . $directory . $manifest[$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);
$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);
/**
* Grab Google CDN's latest jQuery with a protocol relative URL; fallback to local if offline
* jQuery & Modernizr load in the footer per HTML5 Boilerplate's recommendation: http://goo.gl/nMGR7P
* If a plugin enqueues jQuery-dependent scripts in the head, jQuery will load in the head to meet the plugin's dependencies
* To explicitly load jQuery in the head, change the last wp_enqueue_script parameter to false
*/
if (!is_admin() && current_theme_supports('jquery-cdn')) {
wp_deregister_script('jquery');
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);
}
if (is_single() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
wp_enqueue_script('modernizr', asset_path('scripts/modernizr.js'), [], null, true);
wp_enqueue_script('jquery');
wp_enqueue_script('sage_js', asset_path('scripts/main.js'), [], null, true);
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\assets', 100);
// http://wordpress.stackexchange.com/a/12450
function jquery_local_fallback($src, $handle = null) {
static $add_jquery_fallback = false;
if ($add_jquery_fallback) {
echo '<script>window.jQuery || document.write(\'<script src="' . get_template_directory_uri() . '/dist/scripts/jquery.js"><\/script>\')</script>' . "\n";
$add_jquery_fallback = false;
}
if ($handle === 'jquery') {
$add_jquery_fallback = true;
}
return $src;
}
add_action('wp_head', __NAMESPACE__ . '\\jquery_local_fallback');
/**
* Google Analytics snippet from HTML5 Boilerplate
*
* Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
*/
function google_analytics() {
?>
<script>
<?php if (WP_ENV === 'production' && !current_user_can('manage_options')) : ?>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
<?php else : ?>
function ga() {
if (window.console) {
console.log('Google Analytics: ' + [].slice.call(arguments));
}
}
<?php endif; ?>
ga('create','<?= GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
</script>
<?php
}
if (GOOGLE_ANALYTICS_ID) {
add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20);
}