Extension.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /*************************************************************/
  3. // This class is not intended to be extended directly,
  4. // but rather should be used as a boilerplate for an
  5. // extension plugin base file.
  6. // This file, if included or required, will immediately exit.
  7. // TODO: Remove this header before use.
  8. exit;
  9. /*************************************************************/
  10. /**
  11. * Class NF_Abstracts_Extension
  12. */
  13. final class NF_Abstracts_Extension
  14. {
  15. /**
  16. * @since 3.0
  17. */
  18. const VERSION = '';
  19. /**
  20. * @var NF_Abstracts_Extension
  21. * @since 3.0
  22. */
  23. private static $instance;
  24. /**
  25. * Plugin Directory
  26. *
  27. * @since 3.0
  28. * @var string $dir
  29. */
  30. public static $dir = '';
  31. /**
  32. * Plugin URL
  33. *
  34. * @since 3.0
  35. * @var string $url
  36. */
  37. public static $url = '';
  38. /**
  39. * Form Fields
  40. *
  41. * @since 3.0
  42. * @var array
  43. */
  44. public $fields = array();
  45. /**
  46. * Form Actions
  47. *
  48. * @since 3.0
  49. * @var array
  50. */
  51. public $actions = array();
  52. protected $autoloader_prefix = '';
  53. /**
  54. * Main Plugin Instance
  55. *
  56. * Insures that only one instance of a plugin class exists in memory at any one
  57. * time. Also prevents needing to define globals all over the place.
  58. *
  59. * @since 3.0
  60. * @static
  61. * @staticvar array $instance
  62. * @return Plugin Highlander Instance
  63. */
  64. public static function instance()
  65. {
  66. if (!isset(self::$instance) && !(self::$instance instanceof NF_Abstracts_Extension)) {
  67. self::$instance = new NF_Abstracts_Extension();
  68. self::$dir = plugin_dir_path(__FILE__);
  69. self::$url = plugin_dir_url(__FILE__);
  70. /*
  71. * Register our autoloader
  72. */
  73. spl_autoload_register(array(self::$instance, 'autoloader'));
  74. }
  75. }
  76. public function autoloader( $class_name )
  77. {
  78. if( class_exists( $class_name ) ) return;
  79. if( ! $this->autoloader_prefix ) {
  80. $class = explode( '_', __CLASS__ );
  81. $this->autoloader_prefix = $class[ 0 ];
  82. }
  83. if ( false !== strpos( $class_name, $this->autoloader_prefix ) ) {
  84. $class_name = str_replace($this->autoloader_prefix, '', $class_name);
  85. $classes_dir = realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR;
  86. $class_file = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
  87. if (file_exists($classes_dir . $class_file)) {
  88. require_once $classes_dir . $class_file;
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * The main function responsible for returning The Highlander Plugin
  95. * Instance to functions everywhere.
  96. *
  97. * Use this function like you would a global variable, except without needing
  98. * to declare the global.
  99. *
  100. * Example: <?php $nf = NF_Abstracts_Extension(); ?>
  101. *
  102. * @since 3.0
  103. * @return Plugin Highlander Instance
  104. */
  105. function NF_Abstracts_Extension()
  106. {
  107. return NF_Abstracts_Extension::instance();
  108. }
  109. NF_Abstracts_Extension();