Skip to content

Instantly share code, notes, and snippets.

@jdcauley
Last active May 4, 2016 15:47
Show Gist options
  • Save jdcauley/d5f7dabae0295ba84f702b597a4d46c5 to your computer and use it in GitHub Desktop.
Save jdcauley/d5f7dabae0295ba84f702b597a4d46c5 to your computer and use it in GitHub Desktop.
assets
<?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. /theme/dist/scripts/modernizr.js
* 2. /theme/dist/scripts/main.js
*/
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_DIR;
$directory = dirname($filename) . '/';
$file = basename($filename);
static $manifest;
if (empty($manifest)) {
$manifest_path = get_template_directory() . DIST_DIR . 'assets.json';
$manifest = new JsonManifest($manifest_path);
}
if (array_key_exists($file, $manifest->get())) {
return $dist_path . $directory . $manifest->get()[$file];
} else {
return $dist_path . $directory . $file;
}
}
function assets() {
wp_enqueue_style('web_fonts', 'http://fonts.googleapis.com/css?family=Josefin+Sans:300,400,600,700|Roboto:400,300,500', false, null);
wp_enqueue_style('icons', '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css', false, null);
wp_enqueue_style('sage_css', asset_path('styles/main.css'), false, null);
wp_enqueue_style('style_css', get_template_directory_uri() . '/style.css', false, null);
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_12', 'http://code.jquery.com/jquery-1.12.3.min.js', [], null, true);
wp_enqueue_script( 'maps', 'http://maps.google.com/maps/api/js?sensor=true', [], null, true);
wp_enqueue_script( 'infobox', 'http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js', [], null, true);
wp_enqueue_script( 'handlebars', 'http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.min.js', [], null, true);
wp_enqueue_script('sage_js', asset_path('scripts/main.js'), ['jquery'], null, true);
wp_enqueue_script('local_map_js', get_template_directory_uri() . '/assets/scripts/maps.js', ['jquery'], null, true);
// wp_enqueue_script('appjs', get_template_directory_uri() . '/assets/scripts/app.js', ['jquery'], null, true);
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\assets', 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment