├── .gitignore ├── Gruntfile.js ├── README.md ├── examples ├── wp-magic-version-011.php └── wp-magic.php ├── loader.php └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/osx,phpunit,node 3 | 4 | ### OSX ### 5 | .DS_Store 6 | .AppleDouble 7 | .LSOverride 8 | 9 | # Icon must end with two \r 10 | Icon 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear in the root of a volume 16 | .DocumentRevisions-V100 17 | .fseventsd 18 | .Spotlight-V100 19 | .TemporaryItems 20 | .Trashes 21 | .VolumeIcon.icns 22 | 23 | # Directories potentially created on remote AFP share 24 | .AppleDB 25 | .AppleDesktop 26 | Network Trash Folder 27 | Temporary Items 28 | .apdisk 29 | 30 | 31 | #!! ERROR: phpunit is undefined. Use list command to see defined gitignore types !!# 32 | 33 | ### Node ### 34 | # Logs 35 | logs 36 | *.log 37 | npm-debug.log* 38 | 39 | # Runtime data 40 | pids 41 | *.pid 42 | *.seed 43 | 44 | # Directory for instrumented libs generated by jscoverage/JSCover 45 | lib-cov 46 | 47 | # Coverage directory used by tools like istanbul 48 | coverage 49 | 50 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 51 | .grunt 52 | 53 | # node-waf configuration 54 | .lock-wscript 55 | 56 | # Compiled binary addons (http://nodejs.org/api/addons.html) 57 | build/Release 58 | 59 | # Dependency directories 60 | node_modules 61 | jspm_packages 62 | 63 | # Optional npm cache directory 64 | .npm 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function( grunt ) { 2 | 3 | require('load-grunt-tasks')(grunt); 4 | 5 | var pkg = grunt.file.readJSON( 'package.json' ); 6 | 7 | // Project configuration 8 | grunt.initConfig( { 9 | 10 | pkg: pkg, 11 | 12 | copy: { 13 | magic: { 14 | src: ['loader.php'], 15 | dest: 'examples/wp-magic.php' 16 | }, 17 | magicversion: { 18 | src: ['examples/wp-magic.php'], 19 | dest: 'examples/wp-magic-version-011.php' 20 | } 21 | }, 22 | 23 | replace: { 24 | makemagic: { 25 | src: ['examples/wp-magic.php'], 26 | overwrite: true, 27 | replacements: [ 28 | { 29 | from: 'LIBCLASSNAME', 30 | to: 'WP_Magic' 31 | }, 32 | { 33 | from: 'LIBNAMEUPPER', 34 | to: 'WP_MAGIC' 35 | }, 36 | { 37 | from: 'LIBNAMELOWER', 38 | to: 'wp_magic' 39 | }, 40 | { 41 | from: 'LIBURL', 42 | to: 'https://wp-magic.io' 43 | }, 44 | { 45 | from: 'AUTHORNAME', 46 | to: 'John Doe' 47 | }, 48 | { 49 | from: 'AUTHOREMAIL', 50 | to: 'john@johndoe.com' 51 | } 52 | ] 53 | }, 54 | makemagicversion: { 55 | src: ['examples/wp-magic-version-011.php'], 56 | overwrite: true, 57 | replacements: [ 58 | { 59 | from: '@version 0.1.0', 60 | to: '@version 0.1.1' 61 | }, 62 | { 63 | from: '@version 0.1.0', 64 | to: '@version 0.1.1' 65 | }, 66 | { 67 | from: 'WP_Magic_010', 68 | to: 'WP_Magic_011' 69 | }, 70 | { 71 | from: "const VERSION = '0.1.0';", 72 | to: "const VERSION = '0.1.1';" 73 | }, 74 | { 75 | from: 'const PRIORITY = 9999;', 76 | to: 'const PRIORITY = 9998;' 77 | } 78 | ] 79 | }, 80 | }, 81 | 82 | githooks: { 83 | all: { 84 | // create zip and deploy changes to ftp 85 | 'pre-push': 'allwpmagic' 86 | } 87 | } 88 | } ); 89 | 90 | grunt.registerTask( 'wpmagic', [ 'copy:magic', 'replace:makemagic' ] ); 91 | grunt.registerTask( 'wpmagicversion', [ 'copy:magicversion', 'replace:makemagicversion' ] ); 92 | grunt.registerTask( 'allwpmagic', [ 'wpmagic', 'wpmagicversion' ] ); 93 | grunt.registerTask( 'default', [ 'allwpmagic' ] ); 94 | 95 | grunt.util.linefeed = '\n'; 96 | }; 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WP Lib Loader 2 | ### [jtsternberg.github.io/wp-lib-loader](http://jtsternberg.github.io/wp-lib-loader) 3 | > Utility template class for smartly loading WordPress code libraries. 4 | 5 | This is a handy template class for generating smart loaders for your WordPress code libraries and eliminates conflicts and the need for `function_exists` or `class_exists` checks in your plugins/themes when including the bundled library. This loader was inspired by [CMB2](https://github.com/WebDevStudios/CMB2/blob/v2.2.1/init.php#L51-L184), and is a proven system. It allows any plugin or theme to bundle your library, and only one instance (the most up-to-date version) will be loaded by the system. 6 | 7 | It accomplishes this through a semi-magical use of the WordPress hooks system combined with unique loader class-names for each version of the library. 8 | 9 | See the background post: [Don’t Repeat Yourself. Use WP Lib Loader instead!](http://dsgnwrks.pro/plugins-and-scripts/dont-repeat-yourself-use-wp-lib-loader-instead/) 10 | 11 | #### [Use the generator tool](http://jtsternberg.github.io/wp-lib-loader/) to quickly create a loader for your library! 12 | 13 | - [Getting Started](#getting-started) 14 | - [Example File](#the-resulting-file) 15 | - [How to version your library](#how-to-version-your-library) 16 | - [Example File (version 0.1.1)](#the-resulting-file-after-bumping-the-version) 17 | - [Additional details](#additional-details) 18 | - [Super important caveats](#super-important-caveats) 19 | - [Examples in the wild](#examples-in-the-wild) 20 | 21 | ## Getting Started 22 | To use this template, you will need to copy the contents of the [`loader.php`](https://github.com/jtsternberg/wp-lib-loader/blob/master/loader.php) file to your library and then complete a five-step find and replace on the file [head over to the generator tool](http://jtsternberg.github.io/wp-lib-loader/)!. As an example, if your library is named **_WP_Magic_**: 23 | 24 | 1. Search for `LIBCLASSNAME` and replace with your library's class-name: `WP_Magic`. 25 | 1. Search for `LIBNAMEUPPER` and replace with your library's class-name, upppercased (for constants): `WP_MAGIC`. 26 | 1. Search for `LIBNAMELOWER` and replace with your library's class-name, lowercased (for hook names): `wp_magic`. 27 | 1. Search for `LIBURL` and replace with your library's URL: `https://wp_magic.io`. 28 | 1. Search for `AUTHORNAME` and replace with your name: `John Doe`. 29 | 1. Search for `AUTHOREMAIL` and replace with your email: `john@johndoe.com`. 30 | 31 | #### The resulting file: 32 | 33 | [examples/wp-magic.php](https://github.com/jtsternberg/wp-lib-loader/blob/master/examples/wp-magic.php) 34 | 35 | ## How to version your library 36 | 37 | Since the real magic of this loader is when you release a new version, let's walk through that process. Let's say you're looking to release a bug-fix for **_WP_Magic_**. 38 | 39 | 1. Increase your version numbers to `0.1.1`. 40 | * Replace the `0.1.0` for the `@version` docblocks. 41 | 42 | ```php 43 | * @version 0.1.1 44 | ``` 45 | * Replace the `0.1.0` for the `VERSION` constant. 46 | 47 | ```php 48 | const VERSION = '0.1.1'; 49 | ``` 50 | 1. Update the loader class-name from `WP_Magic_010` to `WP_Magic_011`. 51 | 1. **MOST IMPORTANT:** Decrement the `PRIORITY` constant. 52 | 53 | ```php 54 | const PRIORITY = 9998; 55 | ``` 56 | 57 | **That's it!** That's all there is to releasing a new version! You can now be sure that this will be the version used if multiple copies of the library exist. 58 | 59 | #### The resulting file after bumping the version: 60 | 61 | [examples/wp-magic-version-011.php](https://github.com/jtsternberg/wp-lib-loader/blob/master/examples/wp-magic-version-011.php) 62 | 63 | ## Additional details 64 | 65 | * The loader assumes your main library class (e.g. `WP_Magic`) exists in the `lib/init.php` file. Obviously, if that is not the case with your library, you will need to update the `require_once` line. 66 | 67 | * Along those lines, this class can be modified/added to if desired, but the core functionality needs to remain. 68 | 69 | * The loader provides 3 useful constants: 70 | * `LIBNAMEUPPER_LOADED` (e.g. `WP_MAGIC_LOADED`) - A constant set right away which dependent plugins/themes can use to determine if your library is loaded (vs `function_exists` or `class_exists` checks). This constant can also be used to determine the priority of the hook in use for the currently loaded version. 71 | 72 | * `LIBNAMEUPPER_VERSION` (e.g. `WP_MAGIC_VERSION`) - Defines the loaded version of your library so dependent plugins/themes have a way to conditionally load features, if needed. 73 | 74 | * `LIBNAMEUPPER_DIR` (e.g. `WP_MAGIC_DIR`) - Defines the directory of the loaded version of your library. Can be useful for determining the location of the library when debugging and multiple copies of the library exist in the system. 75 | 76 | * The loader provides a useful hook, `'LIBNAMELOWER_load'` (e.g. `'wp_magic_load'`), which can be used for hooking in your dependent functionality. To ensure your hook loads _after_ the library is loaded, you will need to use the `LIBNAMEUPPER_LOADED` (e.g. `WP_MAGIC_LOADED`) constant when hooking in: 77 | ```php 78 | if ( defined( 'WP_MAGIC_LOADED' ) ) { 79 | 80 | // Need to hook AFTER the lib. 81 | $priority = ( WP_MAGIC_LOADED + 1 ); 82 | 83 | // And add the functionality. 84 | add_action( 'wp_magic_load', 'my_magic', $priority ); 85 | } 86 | ``` 87 | * _Keep in mind_, the loader includes the library on the first WordPress hook available to it, `'muplugins_loaded'`, `'plugins_loaded'`, or `'after_setup_theme'`. 88 | 89 | 90 | ## Super important caveats 91 | 92 | * This loader only works if you are **100% committed to backwards-compatibility** (like WordPress). This is mandatory because only _one_ instance of the library will be loaded, and will be whichever instance is the most recent (version). If you change a function, hook name, or properties passed into your library hooks, there is a very real chance you will break the plugins/themes which depend on your library. For this reason, you need to take great consideration when developing your public API, i.e. the parts of the library to be exposed for public use. 93 | 94 | * When releasing a new version, it's extremely important to follow [the versioning instructions](#how-to-version-your-library) or this loader will be useless. 95 | 96 | ## Examples in the wild 97 | 98 | * [WordPress Shortcode Button](https://github.com/jtsternberg/Shortcode_Button/blob/master/shortcode-button.php) 99 | * [CMB2-User-Select](https://github.com/WebDevStudios/CMB2-User-Select/blob/master/cmb2-user-select.php) 100 | * [CMB2 Post Search field](https://github.com/WebDevStudios/CMB2-Post-Search-field/blob/master/cmb2_post_search_field.php) 101 | * [CMB2 Attached Posts Field](https://github.com/WebDevStudios/cmb2-attached-posts/blob/master/cmb2-attached-posts-field.php) 102 | * [CMB2 Related Links](https://github.com/jtsternberg/CMB2-Related-Links/blob/master/cmb2-related-links.php) 103 | * [… Add yours!](https://twitter.com/intent/tweet?text=%40jtsternberg%20I%27m%20using%20WP%20Lib%20Loader!&source=webclient) 104 | -------------------------------------------------------------------------------- /examples/wp-magic-version-011.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2016 John Doe 11 | * @license GPL-2.0+ 12 | * @version 0.1.1 13 | * @link https://wp-magic.io 14 | * @since 0.1.0 15 | */ 16 | 17 | /** 18 | * Copyright (c) 2016 John Doe (email : john@johndoe.com) 19 | * 20 | * This program is free software; you can redistribute it and/or modify 21 | * it under the terms of the GNU General Public License, version 2 or, at 22 | * your discretion, any later version, as published by the Free 23 | * Software Foundation. 24 | * 25 | * This program is distributed in the hope that it will be useful, 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 | * GNU General Public License for more details. 29 | * 30 | * You should have received a copy of the GNU General Public License 31 | * along with this program; if not, write to the Free Software 32 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 33 | */ 34 | 35 | /** 36 | * Loader versioning: http://jtsternberg.github.io/wp-lib-loader/ 37 | */ 38 | 39 | if ( ! class_exists( 'WP_Magic_011', false ) ) { 40 | 41 | /** 42 | * Versioned loader class-name 43 | * 44 | * This ensures each version is loaded/checked. 45 | * 46 | * @category WordPressLibrary 47 | * @package WP_Magic 48 | * @author John Doe 49 | * @license GPL-2.0+ 50 | * @version 0.1.1 51 | * @link https://wp-magic.io 52 | * @since 0.1.0 53 | */ 54 | class WP_Magic_011 { 55 | 56 | /** 57 | * WP_Magic version number 58 | * @var string 59 | * @since 0.1.0 60 | */ 61 | const VERSION = '0.1.1'; 62 | 63 | /** 64 | * Current version hook priority. 65 | * Will decrement with each release 66 | * 67 | * @var int 68 | * @since 0.1.0 69 | */ 70 | const PRIORITY = 9998; 71 | 72 | /** 73 | * Starts the version checking process. 74 | * Creates WP_MAGIC_LOADED definition for early detection by 75 | * other scripts. 76 | * 77 | * Hooks WP_Magic inclusion to the wp_magic_load hook 78 | * on a high priority which decrements (increasing the priority) with 79 | * each version release. 80 | * 81 | * @since 0.1.0 82 | */ 83 | public function __construct() { 84 | if ( ! defined( 'WP_MAGIC_LOADED' ) ) { 85 | /** 86 | * A constant you can use to check if WP_Magic is loaded 87 | * for your plugins/themes with WP_Magic dependency. 88 | * 89 | * Can also be used to determine the priority of the hook 90 | * in use for the currently loaded version. 91 | */ 92 | define( 'WP_MAGIC_LOADED', self::PRIORITY ); 93 | } 94 | 95 | // Use the hook system to ensure only the newest version is loaded. 96 | add_action( 'wp_magic_load', array( $this, 'include_lib' ), self::PRIORITY ); 97 | 98 | /* 99 | * Hook in to the first hook we have available and 100 | * fire our `wp_magic_load' hook. 101 | */ 102 | add_action( 'muplugins_loaded', array( __CLASS__, 'fire_hook' ), 9 ); 103 | add_action( 'plugins_loaded', array( __CLASS__, 'fire_hook' ), 9 ); 104 | add_action( 'after_setup_theme', array( __CLASS__, 'fire_hook' ), 9 ); 105 | } 106 | 107 | /** 108 | * Fires the wp_magic_load action hook. 109 | * 110 | * @since 0.1.0 111 | */ 112 | public static function fire_hook() { 113 | if ( ! did_action( 'wp_magic_load' ) ) { 114 | // Then fire our hook. 115 | do_action( 'wp_magic_load' ); 116 | } 117 | } 118 | 119 | /** 120 | * A final check if WP_Magic exists before kicking off 121 | * our WP_Magic loading. 122 | * 123 | * WP_MAGIC_VERSION and WP_MAGIC_DIR constants are 124 | * set at this point. 125 | * 126 | * @since 0.1.0 127 | */ 128 | public function include_lib() { 129 | if ( class_exists( 'WP_Magic', false ) ) { 130 | return; 131 | } 132 | 133 | if ( ! defined( 'WP_MAGIC_VERSION' ) ) { 134 | /** 135 | * Defines the currently loaded version of WP_Magic. 136 | */ 137 | define( 'WP_MAGIC_VERSION', self::VERSION ); 138 | } 139 | 140 | if ( ! defined( 'WP_MAGIC_DIR' ) ) { 141 | /** 142 | * Defines the directory of the currently loaded version of WP_Magic. 143 | */ 144 | define( 'WP_MAGIC_DIR', dirname( __FILE__ ) . '/' ); 145 | } 146 | 147 | // Include and initiate WP_Magic. 148 | require_once WP_MAGIC_DIR . 'lib/init.php'; 149 | } 150 | 151 | } 152 | 153 | // Kick it off. 154 | new WP_Magic_011; 155 | } 156 | -------------------------------------------------------------------------------- /examples/wp-magic.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2016 John Doe 11 | * @license GPL-2.0+ 12 | * @version 0.1.0 13 | * @link https://wp-magic.io 14 | * @since 0.1.0 15 | */ 16 | 17 | /** 18 | * Copyright (c) 2016 John Doe (email : john@johndoe.com) 19 | * 20 | * This program is free software; you can redistribute it and/or modify 21 | * it under the terms of the GNU General Public License, version 2 or, at 22 | * your discretion, any later version, as published by the Free 23 | * Software Foundation. 24 | * 25 | * This program is distributed in the hope that it will be useful, 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 | * GNU General Public License for more details. 29 | * 30 | * You should have received a copy of the GNU General Public License 31 | * along with this program; if not, write to the Free Software 32 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 33 | */ 34 | 35 | /** 36 | * Loader versioning: http://jtsternberg.github.io/wp-lib-loader/ 37 | */ 38 | 39 | if ( ! class_exists( 'WP_Magic_010', false ) ) { 40 | 41 | /** 42 | * Versioned loader class-name 43 | * 44 | * This ensures each version is loaded/checked. 45 | * 46 | * @category WordPressLibrary 47 | * @package WP_Magic 48 | * @author John Doe 49 | * @license GPL-2.0+ 50 | * @version 0.1.0 51 | * @link https://wp-magic.io 52 | * @since 0.1.0 53 | */ 54 | class WP_Magic_010 { 55 | 56 | /** 57 | * WP_Magic version number 58 | * @var string 59 | * @since 0.1.0 60 | */ 61 | const VERSION = '0.1.0'; 62 | 63 | /** 64 | * Current version hook priority. 65 | * Will decrement with each release 66 | * 67 | * @var int 68 | * @since 0.1.0 69 | */ 70 | const PRIORITY = 9999; 71 | 72 | /** 73 | * Starts the version checking process. 74 | * Creates WP_MAGIC_LOADED definition for early detection by 75 | * other scripts. 76 | * 77 | * Hooks WP_Magic inclusion to the wp_magic_load hook 78 | * on a high priority which decrements (increasing the priority) with 79 | * each version release. 80 | * 81 | * @since 0.1.0 82 | */ 83 | public function __construct() { 84 | if ( ! defined( 'WP_MAGIC_LOADED' ) ) { 85 | /** 86 | * A constant you can use to check if WP_Magic is loaded 87 | * for your plugins/themes with WP_Magic dependency. 88 | * 89 | * Can also be used to determine the priority of the hook 90 | * in use for the currently loaded version. 91 | */ 92 | define( 'WP_MAGIC_LOADED', self::PRIORITY ); 93 | } 94 | 95 | // Use the hook system to ensure only the newest version is loaded. 96 | add_action( 'wp_magic_load', array( $this, 'include_lib' ), self::PRIORITY ); 97 | 98 | /* 99 | * Hook in to the first hook we have available and 100 | * fire our `wp_magic_load' hook. 101 | */ 102 | add_action( 'muplugins_loaded', array( __CLASS__, 'fire_hook' ), 9 ); 103 | add_action( 'plugins_loaded', array( __CLASS__, 'fire_hook' ), 9 ); 104 | add_action( 'after_setup_theme', array( __CLASS__, 'fire_hook' ), 9 ); 105 | } 106 | 107 | /** 108 | * Fires the wp_magic_load action hook. 109 | * 110 | * @since 0.1.0 111 | */ 112 | public static function fire_hook() { 113 | if ( ! did_action( 'wp_magic_load' ) ) { 114 | // Then fire our hook. 115 | do_action( 'wp_magic_load' ); 116 | } 117 | } 118 | 119 | /** 120 | * A final check if WP_Magic exists before kicking off 121 | * our WP_Magic loading. 122 | * 123 | * WP_MAGIC_VERSION and WP_MAGIC_DIR constants are 124 | * set at this point. 125 | * 126 | * @since 0.1.0 127 | */ 128 | public function include_lib() { 129 | if ( class_exists( 'WP_Magic', false ) ) { 130 | return; 131 | } 132 | 133 | if ( ! defined( 'WP_MAGIC_VERSION' ) ) { 134 | /** 135 | * Defines the currently loaded version of WP_Magic. 136 | */ 137 | define( 'WP_MAGIC_VERSION', self::VERSION ); 138 | } 139 | 140 | if ( ! defined( 'WP_MAGIC_DIR' ) ) { 141 | /** 142 | * Defines the directory of the currently loaded version of WP_Magic. 143 | */ 144 | define( 'WP_MAGIC_DIR', dirname( __FILE__ ) . '/' ); 145 | } 146 | 147 | // Include and initiate WP_Magic. 148 | require_once WP_MAGIC_DIR . 'lib/init.php'; 149 | } 150 | 151 | } 152 | 153 | // Kick it off. 154 | new WP_Magic_010; 155 | } 156 | -------------------------------------------------------------------------------- /loader.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2016 AUTHORNAME 11 | * @license GPL-2.0+ 12 | * @version 0.1.0 13 | * @link LIBURL 14 | * @since 0.1.0 15 | */ 16 | 17 | /** 18 | * Copyright (c) 2016 AUTHORNAME (email : AUTHOREMAIL) 19 | * 20 | * This program is free software; you can redistribute it and/or modify 21 | * it under the terms of the GNU General Public License, version 2 or, at 22 | * your discretion, any later version, as published by the Free 23 | * Software Foundation. 24 | * 25 | * This program is distributed in the hope that it will be useful, 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 | * GNU General Public License for more details. 29 | * 30 | * You should have received a copy of the GNU General Public License 31 | * along with this program; if not, write to the Free Software 32 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 33 | */ 34 | 35 | /** 36 | * Loader versioning: http://jtsternberg.github.io/wp-lib-loader/ 37 | */ 38 | 39 | if ( ! class_exists( 'LIBCLASSNAME_010', false ) ) { 40 | 41 | /** 42 | * Versioned loader class-name 43 | * 44 | * This ensures each version is loaded/checked. 45 | * 46 | * @category WordPressLibrary 47 | * @package LIBCLASSNAME 48 | * @author AUTHORNAME 49 | * @license GPL-2.0+ 50 | * @version 0.1.0 51 | * @link LIBURL 52 | * @since 0.1.0 53 | */ 54 | class LIBCLASSNAME_010 { 55 | 56 | /** 57 | * LIBCLASSNAME version number 58 | * @var string 59 | * @since 0.1.0 60 | */ 61 | const VERSION = '0.1.0'; 62 | 63 | /** 64 | * Current version hook priority. 65 | * Will decrement with each release 66 | * 67 | * @var int 68 | * @since 0.1.0 69 | */ 70 | const PRIORITY = 9999; 71 | 72 | /** 73 | * Starts the version checking process. 74 | * Creates LIBNAMEUPPER_LOADED definition for early detection by 75 | * other scripts. 76 | * 77 | * Hooks LIBCLASSNAME inclusion to the LIBNAMELOWER_load hook 78 | * on a high priority which decrements (increasing the priority) with 79 | * each version release. 80 | * 81 | * @since 0.1.0 82 | */ 83 | public function __construct() { 84 | if ( ! defined( 'LIBNAMEUPPER_LOADED' ) ) { 85 | /** 86 | * A constant you can use to check if LIBCLASSNAME is loaded 87 | * for your plugins/themes with LIBCLASSNAME dependency. 88 | * 89 | * Can also be used to determine the priority of the hook 90 | * in use for the currently loaded version. 91 | */ 92 | define( 'LIBNAMEUPPER_LOADED', self::PRIORITY ); 93 | } 94 | 95 | // Use the hook system to ensure only the newest version is loaded. 96 | add_action( 'LIBNAMELOWER_load', array( $this, 'include_lib' ), self::PRIORITY ); 97 | 98 | /* 99 | * Hook in to the first hook we have available and 100 | * fire our `LIBNAMELOWER_load' hook. 101 | */ 102 | add_action( 'muplugins_loaded', array( __CLASS__, 'fire_hook' ), 9 ); 103 | add_action( 'plugins_loaded', array( __CLASS__, 'fire_hook' ), 9 ); 104 | add_action( 'after_setup_theme', array( __CLASS__, 'fire_hook' ), 9 ); 105 | } 106 | 107 | /** 108 | * Fires the LIBNAMELOWER_load action hook. 109 | * 110 | * @since 0.1.0 111 | */ 112 | public static function fire_hook() { 113 | if ( ! did_action( 'LIBNAMELOWER_load' ) ) { 114 | // Then fire our hook. 115 | do_action( 'LIBNAMELOWER_load' ); 116 | } 117 | } 118 | 119 | /** 120 | * A final check if LIBCLASSNAME exists before kicking off 121 | * our LIBCLASSNAME loading. 122 | * 123 | * LIBNAMEUPPER_VERSION and LIBNAMEUPPER_DIR constants are 124 | * set at this point. 125 | * 126 | * @since 0.1.0 127 | */ 128 | public function include_lib() { 129 | if ( class_exists( 'LIBCLASSNAME', false ) ) { 130 | return; 131 | } 132 | 133 | if ( ! defined( 'LIBNAMEUPPER_VERSION' ) ) { 134 | /** 135 | * Defines the currently loaded version of LIBCLASSNAME. 136 | */ 137 | define( 'LIBNAMEUPPER_VERSION', self::VERSION ); 138 | } 139 | 140 | if ( ! defined( 'LIBNAMEUPPER_DIR' ) ) { 141 | /** 142 | * Defines the directory of the currently loaded version of LIBCLASSNAME. 143 | */ 144 | define( 'LIBNAMEUPPER_DIR', dirname( __FILE__ ) . '/' ); 145 | } 146 | 147 | // Include and initiate LIBCLASSNAME. 148 | require_once LIBNAMEUPPER_DIR . 'lib/init.php'; 149 | } 150 | 151 | } 152 | 153 | // Kick it off. 154 | new LIBCLASSNAME_010; 155 | } 156 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wp-lib-loader", 3 | "title": "WP Lib Loader", 4 | "version": "0.1.0", 5 | "description": "Utility template class for smartly loading WordPress code libraries.", 6 | "main": "Gruntfile.js", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/jtsternberg/wp-lib-loader" 10 | }, 11 | "author": { 12 | "name": "Justin Sternberg", 13 | "url": "http://dsgnwrks.pro" 14 | }, 15 | "homepage": "https://github.com/jtsternberg/wp-lib-loader", 16 | "license": "GPLv2", 17 | "devDependencies": { 18 | "grunt": "latest", 19 | "grunt-contrib-copy": "latest", 20 | "grunt-text-replace": "latest", 21 | "grunt-githooks": "~0.3.1", 22 | "load-grunt-tasks": "latest" 23 | } 24 | } 25 | --------------------------------------------------------------------------------