tribe-common-libraries.class.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Class for managing overlapping helper plugins. This ensures that we use the latest versions of common code.
  4. *
  5. * Usage: include this file on any plugin that may have shared code BEFORE the 'plugins_loaded' action is completed.
  6. * After including this file, register the helper files using the TribeCommonLibraries::register() instead of including the files directly.
  7. *
  8. * @version 1.1
  9. */
  10. // Don't load directly
  11. if ( ! defined('ABSPATH') ) { die('-1'); }
  12. if ( ! class_exists( 'TribeCommonLibraries' ) ) {
  13. class TribeCommonLibraries {
  14. private static $tribe_registered_plugins;
  15. /**
  16. * Enforce Singleton Pattern
  17. */
  18. private static $instance;
  19. public static function getInstance() {
  20. if ( null == self::$instance ) {
  21. $className = __CLASS__;
  22. self::$instance = new $className;
  23. }
  24. return self::$instance;
  25. }
  26. private function __construct() {
  27. self::$tribe_registered_plugins = array();
  28. add_action( 'plugins_loaded', array( $this, 'activate_plugins' ), 999 );
  29. }
  30. /**
  31. * Register a plugin / helper class
  32. *
  33. * @param string $slug - slug specific to the helper class / plugin
  34. * @param string $version - version of the helper class / plugin
  35. * @param string $path - absolute path of the helper class / plugin file
  36. */
  37. public static function register( $slug, $version, $path ) {
  38. if ( ! isset( self::$tribe_registered_plugins[$slug] ) || version_compare( self::$tribe_registered_plugins[$slug]['version'], $version, '<' ) ) {
  39. self::$tribe_registered_plugins[$slug] = array(
  40. 'version' => $version,
  41. 'path' => $path,
  42. );
  43. }
  44. }
  45. /**
  46. * Activate all plugins.
  47. */
  48. public function activate_plugins() {
  49. foreach ( self::$tribe_registered_plugins as $k => $v ) {
  50. require_once( $v['path'] );
  51. do_action( 'tribe_helper_activate_' . $k );
  52. }
  53. do_action( 'tribe_helper_activation_complete' );
  54. }
  55. }
  56. }
  57. TribeCommonLibraries::getInstance();