Created
March 20, 2012 11:47
-
-
Save peterchester/2134425 to your computer and use it in GitHub Desktop.
WordPress library management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class for managing overlapping helper plugins. This ensures that we use the latest versions of common code. | |
* | |
* Usage: include this file on any plugin that may have shared code BEFORE the 'plugins_loaded' action is completed. | |
* After including this file, register the helper files using the TribeCommonLibraries::register() instead of including the files directly. | |
* | |
* @author Peter Chester | |
* @version 1.0 | |
*/ | |
// Don't load directly | |
if ( !defined('ABSPATH') ) { die('-1'); } | |
if( !class_exists( 'TribeCommonLibraries' ) ) { | |
class TribeCommonLibraries { | |
private static $tribe_registered_plugins; | |
/** | |
* Enforce Singleton Pattern | |
*/ | |
private static $instance; | |
public function getInstance() { | |
if(null == self::$instance) { | |
$className = __CLASS__; | |
self::$instance = new $className; | |
} | |
return self::$instance; | |
} | |
private function __construct() { | |
self::$tribe_registered_plugins = array(); | |
add_action( 'plugins_loaded', array( __CLASS__, 'activate_plugins' ) ); | |
} | |
/** | |
* Register a plugin / helper class | |
* | |
* @param string $slug - slug specific to the helper class / plugin | |
* @param string $version - version of the helper class / plugin | |
* @param string $path - absolute path of the helper class / plugin file | |
*/ | |
public static function register($slug,$version,$path) { | |
if ( !isset( self::$tribe_registered_plugins[$slug] ) || version_compare( self::$tribe_registered_plugins[$slug]['version'], $version, '>' ) ) { | |
self::$tribe_registered_plugins[$slug] = array( | |
'version'=>$version, | |
'path'=>$path, | |
); | |
} | |
} | |
/** | |
* Activate all plugins. | |
*/ | |
public function activate_plugins() { | |
foreach(self::$tribe_registered_plugins as $k => $v) { | |
require_once($v['path']); | |
do_action('tribe_helper_activate_'.$k); | |
} | |
do_action('tribe_helper_activation_complete'); | |
} | |
} | |
TribeCommonLibraries::getInstance(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment