classes
1 month ago
fields
1 month ago
static
1 month ago
templates
1 month ago
autoload.php
1 month ago
autoload.php
50 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpai\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | require WP_ALL_IMPORT_ROOT_DIR . '/addon-api/classes/helpers.php'; |
| 8 | |
| 9 | class PMXI_Addon_Autoloader { |
| 10 | use Singleton; |
| 11 | |
| 12 | public function __construct() { |
| 13 | spl_autoload_register([$this, 'autoload']); |
| 14 | |
| 15 | foreach ($this->modules() as $module) { |
| 16 | $module::getInstance(); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | public function modules() { |
| 21 | return [PMXI_Addon_Admin::class, PMXI_Addon_Rest::class]; |
| 22 | } |
| 23 | |
| 24 | public function loadIfFound(string $path) { |
| 25 | $path = WP_ALL_IMPORT_ROOT_DIR . '/addon-api/' . $path . '.php'; |
| 26 | |
| 27 | if (file_exists($path)) { |
| 28 | require_once $path; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public function autoload($class) { |
| 33 | if ( strpos( $class, 'PMXI_Addon_' ) === false ) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | $parts = explode('\\', $class); |
| 38 | $className = end($parts); |
| 39 | $className = str_replace('PMXI_Addon_', '', $className); |
| 40 | $className = str_replace('_', '-', $className); |
| 41 | $className = strtolower($className); |
| 42 | $className = str_replace('-field', '', $className); // E.g. Rename "text-field" to "text" |
| 43 | |
| 44 | $this->loadIfFound('classes/' . $className); |
| 45 | $this->loadIfFound('fields/' . $className); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | PMXI_Addon_Autoloader::getInstance(); |
| 50 |