Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Autoloader.php
298 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'Tribe__Autoloader' ) ) { |
| 4 | /** |
| 5 | * Class Tribe__Autoloader |
| 6 | * |
| 7 | * Allows for autoloading of Tribe plugins classes. |
| 8 | * |
| 9 | * Example usage: |
| 10 | * |
| 11 | * // will be `/var/www/site/wp-content/plugins/the-events-calendar' |
| 12 | * $this_dir = dirname(__FILE__); |
| 13 | * |
| 14 | * // gets hold of the singleton instance of the class |
| 15 | * $autoloader = Tribe__Autoloader::instance(); |
| 16 | * |
| 17 | * // register one by one or use `register_prefixes` method |
| 18 | * $autoloader->register_prefix( 'Tribe__Admin__', $this_dir . '/src/Tribe/admin' ); |
| 19 | * $autoloader->register_prefix( 'Tribe__Admin__', $this_dir . '/src/Tribe/another-dir' ); |
| 20 | * $autoloader->register_prefix( 'Tribe__Utils__', $this_dir . '/src/Tribe/another-dir' ); |
| 21 | * |
| 22 | * // register a direct class to path |
| 23 | * $autoloader->register_class( 'Tribe__Some_Class', $this_dir . '/some/path/to/Some_Class.php' ); |
| 24 | * |
| 25 | * // register a fallback dir to be searched for the class before giving up |
| 26 | * $autoloader->add_fallback_dir( $this_dir . '/all-the-classes' ); |
| 27 | * |
| 28 | * // calls `spl_autoload_register` |
| 29 | * $autoloader->register_autoloader(); |
| 30 | * |
| 31 | * // class will be searched in the path |
| 32 | * // `/var/www/site/wp-content/plugins/the-events-calendar/src/Tribe/admin/Some_Class.php' |
| 33 | * // and |
| 34 | * // `/var/www/site/wp-content/plugins/the-events-calendar/src/Tribe/another-dir/Some_Class.php' |
| 35 | * $i = new Tribe__Admin__Some_Class(); |
| 36 | * |
| 37 | * // class will be searched in the path |
| 38 | * // `/var/www/site/wp-content/plugins/the-events-calendar/utils/some-dir/Some_Util.php' |
| 39 | * $i = new Tribe__Utils__Some_Util(); |
| 40 | * |
| 41 | * // class will be searched in the path |
| 42 | * // `/var/www/site/wp-content/plugins/the-events-calendar/deprecated/Tribe_DeprecatedClass.php' |
| 43 | * $i = new Tribe_DeprecatedClass(); |
| 44 | */ |
| 45 | class Tribe__Autoloader { |
| 46 | |
| 47 | /** |
| 48 | * @var Tribe__Autoloader |
| 49 | */ |
| 50 | protected static $instance; |
| 51 | |
| 52 | /** |
| 53 | * An arrays of arrays each containing absolute paths. |
| 54 | * |
| 55 | * Paths are stored trimming any trailing `/`. |
| 56 | * E.g. `/var/www/tribe-pro/wp-content/plugins/the-events-calendar/src/Tribe` |
| 57 | * |
| 58 | * @var string[][] |
| 59 | */ |
| 60 | protected $prefixes; |
| 61 | |
| 62 | /** |
| 63 | * An array of registered prefixes with unique slugs. |
| 64 | * |
| 65 | * @var string[] |
| 66 | */ |
| 67 | protected $prefix_slugs; |
| 68 | |
| 69 | /** |
| 70 | * The string acting as a directory separator in a class name. |
| 71 | * |
| 72 | * E.g.: given `__` as `$dir_separator` then `Admin__Metabox__Some_Metabox` |
| 73 | * will map to `/Admin/Metabox/SomeMetabox.php`. |
| 74 | * |
| 75 | * @var string |
| 76 | */ |
| 77 | protected $dir_separator = '__'; |
| 78 | |
| 79 | /** @var string[] */ |
| 80 | protected $fallback_dirs = []; |
| 81 | |
| 82 | /** |
| 83 | * @var array |
| 84 | */ |
| 85 | protected $class_paths = []; |
| 86 | |
| 87 | /** |
| 88 | * Returns the singleton instance of the class. |
| 89 | * |
| 90 | * @return Tribe__Autoloader |
| 91 | */ |
| 92 | public static function instance() { |
| 93 | if ( ! self::$instance instanceof Tribe__Autoloader ) { |
| 94 | self::$instance = new self(); |
| 95 | } |
| 96 | |
| 97 | return self::$instance; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Registers prefixes and root dirs using an array. |
| 102 | * |
| 103 | * Same as calling `register_prefix` on each one. |
| 104 | * |
| 105 | * @param array $prefixes_to_root_dirs |
| 106 | */ |
| 107 | public function register_prefixes( array $prefixes_to_root_dirs ) { |
| 108 | foreach ( $prefixes_to_root_dirs as $prefix => $root_dir ) { |
| 109 | $this->register_prefix( $prefix, $root_dir ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Associates a class prefix to an absolute path. |
| 115 | * |
| 116 | * @param string $prefix A class prefix, e.g. `Tribe__Admin__` |
| 117 | * @param string $root_dir The absolute path to the dir containing |
| 118 | * the prefixed classes. |
| 119 | * @param string $slug An optional unique slug to associate to the prefix. |
| 120 | */ |
| 121 | public function register_prefix( $prefix, $root_dir, $slug = '' ) { |
| 122 | $root_dir = $this->normalize_root_dir( $root_dir ); |
| 123 | |
| 124 | // Determine if we need to normalize the $prefix. |
| 125 | $is_namespaced = false !== strpos( $prefix, '\\' ); |
| 126 | |
| 127 | if ( $is_namespaced ) { |
| 128 | // If the prefix is a namespace, then normalize it. |
| 129 | $prefix = trim( $prefix, '\\' ) . '\\'; |
| 130 | } |
| 131 | |
| 132 | if ( ! isset( $this->prefixes[ $prefix ] ) ) { |
| 133 | $this->prefixes[ $prefix ] = []; |
| 134 | } |
| 135 | |
| 136 | $this->prefixes[ $prefix ][] = $root_dir; |
| 137 | |
| 138 | // Let's make sure we're not adding duplicates. |
| 139 | $this->prefixes[ $prefix ] = array_unique( $this->prefixes[ $prefix ] ); |
| 140 | |
| 141 | if ( $slug ) { |
| 142 | $this->prefix_slugs[ $slug ] = $prefix; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Triggers the registration of the autoload method in the SPL |
| 148 | * autoload register. |
| 149 | */ |
| 150 | public function register_autoloader() { |
| 151 | spl_autoload_register( [ $this, 'autoload' ] ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Includes the file defining a class. |
| 156 | * |
| 157 | * This is the function that's registered as an autoloader. |
| 158 | * |
| 159 | * @param string $class |
| 160 | */ |
| 161 | public function autoload( $class ) { |
| 162 | $include_path = $this->get_class_path( $class ); |
| 163 | if ( ! empty( $include_path ) ) { |
| 164 | include_once( $include_path ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private function normalize_root_dir( $root_dir ) { |
| 169 | return rtrim( $root_dir, '/' ); |
| 170 | } |
| 171 | |
| 172 | protected function get_prefixed_path( $class ) { |
| 173 | foreach ( $this->prefixes as $prefix => $dirs ) { |
| 174 | $is_namespaced = false !== strpos( $prefix, '\\' ); |
| 175 | |
| 176 | if ( strpos( $class, $prefix ) !== 0 ) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | $class_name = str_replace( $prefix, '', $class ); |
| 181 | |
| 182 | if ( ! $is_namespaced ) { |
| 183 | $class_path_frag = implode( '/', explode( $this->dir_separator, $class_name ) ) . '.php'; |
| 184 | } else { |
| 185 | $class_path_frag = implode( '/', explode( '\\', $class_name ) ) . '.php'; |
| 186 | } |
| 187 | |
| 188 | foreach ( $dirs as $dir ) { |
| 189 | $path = $dir . '/' . $class_path_frag; |
| 190 | if ( ! file_exists( $path ) ) { |
| 191 | // check if the file exists in lowercase |
| 192 | $class_path_frag = strtolower( $class_path_frag ); |
| 193 | $path = $dir . '/' . $class_path_frag; |
| 194 | } |
| 195 | if ( ! file_exists( $path ) ) { |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | return $path; |
| 200 | } |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | protected function get_fallback_path( $class ) { |
| 206 | foreach ( $this->fallback_dirs as $fallback_dir ) { |
| 207 | $include_path = $fallback_dir . '/' . $class . '.php'; |
| 208 | if ( ! file_exists( $include_path ) ) { |
| 209 | // check if the file exists in lowercase |
| 210 | $class = strtolower( $class ); |
| 211 | $include_path = $fallback_dir . '/' . $class . '.php'; |
| 212 | } |
| 213 | if ( ! file_exists( $include_path ) ) { |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | return $include_path; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Gets the absolute path to a class file. |
| 223 | * |
| 224 | * @param string $class The class name |
| 225 | * |
| 226 | * @return string Either the absolute path to the class file or an |
| 227 | * empty string if the file was not found. |
| 228 | */ |
| 229 | public function get_class_path( $class ) { |
| 230 | $prefixed_path = $this->get_prefixed_path( $class ); |
| 231 | if ( $prefixed_path ) { |
| 232 | return $prefixed_path; |
| 233 | } |
| 234 | |
| 235 | $class_path = ! empty( $this->class_paths[ $class ] ) ? $this->class_paths[ $class ] :false; |
| 236 | if ( $class_path ) { |
| 237 | return $class_path; |
| 238 | } |
| 239 | |
| 240 | $fallback_path = $this->get_fallback_path( $class ); |
| 241 | |
| 242 | return $fallback_path ? $fallback_path : ''; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get the registered prefix by slug |
| 247 | * |
| 248 | * @param string $slug Unique slug for registered prefix. |
| 249 | * |
| 250 | * @return false|string Either the prefix registered to the |
| 251 | * unique slug or false if not found. |
| 252 | */ |
| 253 | public function get_prefix_by_slug( $slug ) { |
| 254 | $prefix = false; |
| 255 | |
| 256 | if ( isset( $this->prefix_slugs[ $slug ] ) ) { |
| 257 | $prefix = $this->prefix_slugs[ $slug ]; |
| 258 | } |
| 259 | |
| 260 | return $prefix; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Adds a folder to search for classes that were not found among |
| 265 | * the prefixed ones. |
| 266 | * |
| 267 | * This is the method to use to register a directory of deprecated |
| 268 | * classes. |
| 269 | * |
| 270 | * @param string $dir An absolute path dto a dir. |
| 271 | */ |
| 272 | public function add_fallback_dir( $dir ) { |
| 273 | if ( in_array( $dir, $this->fallback_dirs ) ) { |
| 274 | return; |
| 275 | } |
| 276 | $this->fallback_dirs[] = $this->normalize_root_dir( $dir ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @return string |
| 281 | */ |
| 282 | public function get_dir_separator() { |
| 283 | return $this->dir_separator; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @param string $dir_separator |
| 288 | */ |
| 289 | public function set_dir_separator( $dir_separator ) { |
| 290 | $this->dir_separator = $dir_separator; |
| 291 | } |
| 292 | |
| 293 | public function register_class( $class, $path ) { |
| 294 | $this->class_paths[ $class ] = $path; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 |