Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #39 from synapticism/develop
Browse files Browse the repository at this point in the history
Develop updates
  • Loading branch information
xsynaptic authored Aug 2, 2016
2 parents f6a3774 + 4e73ae0 commit b63da55
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"supernew": true,
"globals": {
"_": true,
"jQuery": true
"jQuery": true,
"svg4everybody": true
}
}
17 changes: 11 additions & 6 deletions gulpconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ module.exports = {

scripts: {
bundles: { // Bundles are defined by a name and an array of chunks (below) to concatenate; warning: this method offers no dependency management!
core: ['core']
, pageloader: ['pageloader', 'core']
footer: ['footer']
, header: ['header']
, pageloader: ['pageloader', 'footer']
}
, chunks: { // Chunks are arrays of paths or globs matching a set of source files; this way you can organize a bunch of scripts that go together into pieces that can then be bundled (above)
// The core chunk is loaded no matter what; put essential scripts that you want loaded by your theme in here
core: [
// The core footer chunk is loaded no matter what; put essential scripts that you want loaded by your theme in here
footer: [
modules+'timeago/jquery.timeago.js' // The modules directory contains packages downloaded via npm
, src+'js/responsive-menu.js'
, src+'js/core.js'
, src+'js/footer.js'
]
, header: [
modules+'svg4everybody/dist/svg4everybody.js'
, src+'js/header.js'
]
// The pageloader chunk provides an example of how you would add a user-configurable feature to your theme; you can delete this if you wish
// Have a look at the `src/inc/assets.php` to see how script bundles could be conditionally loaded by a theme
Expand All @@ -76,7 +81,7 @@ module.exports = {
, uglify: {} // Default options
, dest: build+'js/'
}
, namespace: 'wp-' // Script filenames will be prefaced with this (optional; leave blank if you have no need for it but be sure to change the corresponding value in `src/inc/assets.php` if you use it)
, namespace: 'x-' // Script filenames will be prefaced with this (optional; leave blank if you have no need for it but be sure to change the corresponding value in `src/inc/assets.php` if you use it)
},

styles: {
Expand Down
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wordpress-gulp-starter-kit",
"version": "0.2.5",
"version": "0.3.0",
"description": "A starter kit for developing WordPress themes with Gulp",
"homepage": "https://github.com/synapticism/wordpress-gulp-starter-kit",
"repository": {
Expand All @@ -25,29 +25,30 @@
"main": "gulpfile.js",
"private": true,
"devDependencies": {
"browser-sync": "^2.13.0",
"del": "^2.2.0",
"browser-sync": "^2.14.0",
"del": "^2.2.1",
"gulp": "^3.9.1",
"gulp-changed": "^1.3.0",
"gulp-changed": "^1.3.1",
"gulp-concat": "^2.6.0",
"gulp-cssnano": "^2.1.2",
"gulp-imagemin": "^3.0.1",
"gulp-imagemin": "^3.0.2",
"gulp-jshint": "^2.0.1",
"gulp-livereload": "^3.8.1",
"gulp-load-plugins": "^1.2.4",
"gulp-rename": "^1.2.2",
"gulp-ruby-sass": "^2.0.6",
"gulp-sass": "^2.3.1",
"gulp-sass": "^2.3.2",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^1.5.3",
"gulp-uglify": "^2.0.0",
"gulp-util": "^3.0.7",
"html5-history-api": "^4.2.5",
"merge-stream": "^1.0.0",
"normalize.css": "^4.1.1",
"normalize.css": "^4.2.0",
"require-dir": "^0.3.0",
"scut": "^1.4.0",
"spin.js": "^2.3.2",
"timeago": "^1.5.2",
"svg4everybody": "^2.1.0",
"timeago": "^1.5.3",
"wp-ajax-page-loader": "^0.2.1"
},
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ Like images, PHP (and language) files can go anywhere under `src` and will be co

## EXAMPLE INTEGRATIONS

The bare bones theme included in this starter kit integrates two external JavaScript projects: [timeago](http://timeago.yarp.com/) (which facilitates human-readable timestamps) and [WP AJAX Page Loader](https://github.com/synapticism/wp-ajax-page-loader) (a lightweight and intelligent infinite scroll script). Have a look at the `/src/inc/assets.php` file and `gulpconfig.js` to see how this was accomplished. Timeago required only `npm install -d timeago`, an extra line in `gulpconfig.js`, and initialization in `/src/js/core.js`. Integrating the other script was much more involved; you'll have to browse the source and see for yourself.
The bare bones theme included in this starter kit integrates three external JavaScript projects: [timeago](http://timeago.yarp.com/) (which facilitates human-readable timestamps), [SVG for Everybody](https://github.com/jonathantneal/svg4everybody), and [WP AJAX Page Loader](https://github.com/synapticism/wp-ajax-page-loader) (a lightweight and intelligent infinite scroll script). Have a look at `/src/inc/assets.php` file and `gulpconfig.js` to see how this works. Timeago requires only `npm install -d timeago`, an extra line in `gulpconfig.js`, and initialization in `/src/js/footer.js`. SVG for Everybody is provided as an example of a script that must be loaded in the header; hence you can see it in action at `/src/js/header.js`. The page loading script is a more involved integraton; browse the source and see for yourself.

Let's say you run across a cool project like [Headroom.js](http://wicky.nillia.ms/headroom.js/) and decide you'd like to try it out. Here's how you would do that with the tools and workflow outlined in this repo:
Now for one that you can try yourself! Let's say you run across a cool project like [Headroom.js](http://wicky.nillia.ms/headroom.js/) and decide you'd like to try it out. Here's how you would do that with the tools and workflow outlined in this repo:

* `npm install headroom.js --save-dev`. This will save the package to the `devDependencies` field of your `package.json` file.
* Look up the path to the script and add the appropriate entries to `scripts.bundles` and `scripts.chunks` in `gulpconfig.js`. The key name of `scripts.bundles` should match `$script_name` (below). Since this script is meant to be loaded on every page it is safe to bundle it with the `core` script.
* Look up the path to the script and add the appropriate entries to `scripts.bundles` and `scripts.chunks` in `gulpconfig.js`. The key name of `scripts.bundles` should match `$script_name` (below). Since this script is meant to be loaded on every page it is safe to bundle it with the `footer` script.
* To make this script *optional* requires a bit more work:
* Add an option to `functions-config-defaults.php`: `defined( 'VOIDX_SCRIPTS_HEADROOM' ) || define( 'VOIDX_SCRIPTS_HEADROOM', true );`.
* Add the switch to `inc/assets.php`: `if ( VOIDX_SCRIPTS_HEADROOM ) : $script_name .= '-hr';`.
* Add the switch to the `voidx_assets_footer` functon in `inc/assets.php`: `if ( VOIDX_SCRIPTS_HEADROOM ) : $file .= '-hr';`.
* Add an option to `scss/_config.scss` to allow for the styling to be turned on or off: `$plugin-headroom: true;`.
* Add the necessary styling to `scss/_plugins.scss` wrapped in a conditional check: `@if ($plugin-headroom) { // Style }`.
* Create an additional script at `src/js/headroom.js` to invoke the main script:
Expand Down
104 changes: 49 additions & 55 deletions src/inc/assets.php
Original file line number Diff line number Diff line change
@@ -1,88 +1,82 @@
<?php // ==== ASSETS ==== //

// Now that you have efficiently generated scripts and stylesheets for your theme, how should they be integrated?
// This file walks you through the approach I use but you are free to do this any way you like
// This file walks you through an approach I use but you are free to do this any way you like

// Enqueue front-end scripts and styles
function voidx_enqueue_scripts() {
// Load header assets; this should include the main stylesheet as well as any mission critical scripts
function voidx_assets_header() {

$script_name = ''; // Empty by default, may be populated by conditionals below; this is used to generate the script filename
$script_vars = array(); // An empty array that can be filled with variables to send to front-end scripts
$script_handle = 'voidx'; // A generic script handle used internally by WordPress
$ns = 'wp'; // Namespace for scripts; this should match what is specified in your `gulpconfig.js` (and it's safe to leave alone)
// Header script loading is simplistic in this starter kit but you may want to change what file is loaded based on various conditions; check out the footer asset loader for an example
$file = 'x-header';
wp_enqueue_script( 'voidx-header', get_stylesheet_directory_uri() . '/js/' . $file . '.js', $deps = array(), filemtime( get_template_directory() . '/js/' . $file . '.js' ), false );

// Figure out which script bundle to load based on various options set in `src/functions-config-defaults.php`
// Note: bundles require fewer HTTP requests at the expense of addition caching hits when different scripts are requested on different pages of your site
// You could also load one main bundle on every page with supplementary scripts as needed (e.g. for commenting or a contact page); it's up to you!
// Register and enqueue our main stylesheet with versioning based on last modified time
wp_register_style( 'voidx-style', get_stylesheet_uri(), $dependencies = array(), filemtime( get_template_directory() . '/style.css' ) );
wp_enqueue_style( 'voidx-style' );
}
add_action( 'wp_enqueue_scripts', 'voidx_assets_header' );



// == EXAMPLE INTEGRATION == //
// Load footer assets; a more complex example of a smooth asset-loading approach for WordPress themes
function voidx_assets_footer() {

// An example integration using WP AJAX Page Loader; this script requires a bit more setup as outlined in the documentation: https://github.com/synapticism/wp-ajax-page-loader
$script_vars_page_loader = '';
// Initialize variables
$name = 'voidx-footer'; // This is the script handle
$file = 'x'; // The beginning of the filename; "x" is the namespace set in `gulpconfig.js`
$vars = array(); // An empty array that may be populated by script variables for output with `wp_localize_script` after the footer script is enqueued

// This conditional establishes whether the page loader bundle is loaded or not; you can turn this off completely from the theme configuration file if you wish (or just remove the code)
// This approach allows for conditional loading of various script bundles based on options set in `src/functions-config-defaults.php`
// Note: bundles require fewer HTTP requests at the expense of addition caching hits when different scripts are requested on different pages of your site
// You could also load one main bundle on every page with supplementary scripts as needed (e.g. for commenting or a contact page); it's up to you!

// Example integraton: WP AJAX Page Loader (similar to Infinite Scroll); this script is only loaded when the conditions below are met
// This script must be provisioned with some extra data via the `wp_localize_script` function as outlined in the documentation: https://github.com/synapticism/wp-ajax-page-loader
if ( VOIDX_SCRIPTS_PAGELOAD && ( is_archive() || is_home() || is_search() ) ) {

// The script name is used to specify the file that the theme will serve to users
// Script names are designed to be additive (meaning you can append more script names to the end in other conditional blocks using `.= '-anotherscript'` etc.) to allow for multiple feature toggles in the theme configuration
$script_name .= '-pageloader';
// Script filenames are designed to be additive (meaning you can append more script names to the end in other conditional blocks using `.= '-anotherscript'` etc.) to allow for multiple feature toggles in the theme configuration
// Have a look at `gulpconfig.js` to see where these script names are defined
$file .= '-pageloader';

// This chunk of code provisions the WP AJAX Page Loader script with some important information it needs: What page are we on? And what is the page limit?
// This chunk of code provisions the script with vital info: What page are we on? And what is the page limit?
global $wp_query;
$max = $wp_query->max_num_pages;
$paged = ( get_query_var( 'paged' ) > 1 ) ? get_query_var( 'paged' ) : 1;

// Prepare script variables; note that these are separate from the rest of the script variables as the WP AJAX Page Loader script requires everything in a specific object named `PG8Data`
$script_vars_page_loader = array(
// Prepare script variables that will be output after the footer script is enqueued
$vars['PG8Data'] = array(
'startPage' => $paged,
'maxPages' => $max,
'nextLink' => next_posts( $max, false )
);
} // WP AJAX Page Loader configuration ends


}

// Default script name; used when conditional blocks (above) aren't triggered
if ( empty( $script_name ) )
$script_name = '-core';
// If none of the conditons were matched (above) let's output the default script name
if ( $file === 'x' )
$file .= '-footer';

// Load theme-specific JavaScript bundles with versioning based on last modified time; http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/
// The handle is the same for each bundle since we're only loading one script; if you load others be sure to provide a new handle
wp_enqueue_script( $script_handle, get_stylesheet_directory_uri() . '/js/' . $ns . $script_name . '.js', array( 'jquery' ), filemtime( get_template_directory() . '/js/' . $ns . $script_name . '.js' ), true );

// Pass variables to JavaScript at runtime; see: http://codex.wordpress.org/Function_Reference/wp_localize_script
$script_vars = apply_filters( 'voidx_script_vars', $script_vars );
if ( !empty( $script_vars ) )
wp_localize_script( $script_handle, 'voidxVars', $script_vars );

// Script variables specific to WP AJAX Page Loader (these are separate from the main theme script variables due to the naming requirement; the object *must* be `PG8Data`)
// This appears here and NOT in the conditional block (above) because these variables will be attached to the main script handle (which may be modified after the page loader block)
if ( !empty( $script_vars_page_loader ) )
wp_localize_script( $script_handle, 'PG8Data', $script_vars_page_loader );

// Register and enqueue our main stylesheet with versioning based on last modified time
wp_register_style( 'voidx-style', get_stylesheet_uri(), $dependencies = array(), filemtime( get_template_directory() . '/style.css' ) );
wp_enqueue_style( 'voidx-style' );
wp_enqueue_script( $name, get_stylesheet_directory_uri() . '/js/' . $file . '.js', array( 'jquery' ), filemtime( get_template_directory() . '/js/' . $file . '.js' ), true ); // This last `true` is what loads the script in the footer

// Pass variables to scripts at runtime; must be triggered after the script is enqueued; see: http://codex.wordpress.org/Function_Reference/wp_localize_script
if ( !empty( $vars ) ) {
foreach ( $vars as $var => $data )
wp_localize_script( $name, $var, $data );
}
}
add_action( 'wp_enqueue_scripts', 'voidx_enqueue_scripts' );
add_action( 'wp_enqueue_scripts', 'voidx_assets_footer' );



// Provision the front-end with the appropriate script variables
function voidx_update_script_vars( $script_vars = array() ) {
// Load assets on single content; useful for conditional loading of the core comments script, for example
function voidx_assets_singular() {
if ( !is_singular() )
return;

// Non-destructively merge script variables if a particular condition is met (e.g. `is_archive()` or whatever); useful for managing many different kinds of script variables
if ( 1 == 1 ) {
$script_vars = array_merge( $script_vars, array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nameSpaced' => array(
'test1' => __( 'Testing 1, 2, 3!', 'voidx' ),
'test2' => __( 'This is easier than it looks :)', 'voidx' )
) ) );
}
return $script_vars;
// Load core WordPress script for handling threaded comments where appropriate
// This isn't really useful since comments aren't a feature of this simple theme but you get the idea
if ( comments_open() && get_option('thread_comments') )
wp_enqueue_script( 'comment-reply' );
}
add_filter( 'voidx_script_vars', 'voidx_update_script_vars' );
add_action( 'wp_enqueue_scripts', 'voidx_assets_singular' );
4 changes: 2 additions & 2 deletions src/js/core.js → src/js/footer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==== CORE ==== //
// ==== FOOTER ==== //

// A simple wrapper for all your custom jQuery; everything in this file will be run on every page
// A simple wrapper for all your custom jQuery that belongs in the footer
;(function($){
$(function(){
// Example integration: JavaScript-based human-readable timestamps
Expand Down
9 changes: 9 additions & 0 deletions src/js/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// ==== HEADER ==== //

// A simple wrapper for all your custom jQuery that belongs in the header
;(function($){
$(function(){
// Initialize svg4everybody 2.0.0+, which is supposed to be done in the header
svg4everybody();
});
}(jQuery));
2 changes: 1 addition & 1 deletion src/scss/_config.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $_metadata: (
, theme-author: "Alexander Synaptic"
, theme-author-uri: "http://synapticism.com"
, theme-description: "A minimally viable theme made with the WordPress/Gulp starter kit"
, theme-version: "0.2.5"
, theme-version: "0.3.0"
, theme-license: "GPLv3"
, theme-license-uri: "http://www.gnu.org/licenses/gpl.txt"
, theme-tags: (light, responsive-layout, right-sidebar, starter-theme, two-columns, translation-ready, white)
Expand Down

0 comments on commit b63da55

Please sign in to comment.