| 1 |
<?php |
| 2 |
|
| 3 |
namespace Jet_Form_Builder; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Autoloader handler class is responsible for loading the different |
| 11 |
* classes needed to run the plugin. |
| 12 |
*/ |
| 13 |
class Autoloader { |
| 14 |
|
| 15 |
/** |
| 16 |
* Run autoloader. |
| 17 |
* |
| 18 |
* Register a function as `__autoload()` implementation. |
| 19 |
* |
| 20 |
* @since 1.6.0 |
| 21 |
* @access public |
| 22 |
* @static |
| 23 |
*/ |
| 24 |
public static function run() { |
| 25 |
spl_autoload_register( array( __CLASS__, 'autoload' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Load class. |
| 30 |
* |
| 31 |
* For a given class name, require the class file. |
| 32 |
* |
| 33 |
* @param string $relative_class_name Class name. |
| 34 |
* |
| 35 |
* @since 1.6.0 |
| 36 |
* @access private |
| 37 |
* @static |
| 38 |
*/ |
| 39 |
private static function load_class( $class_name ) { |
| 40 |
|
| 41 |
$file = str_replace( '\\', DIRECTORY_SEPARATOR, $class_name ); |
| 42 |
$file = strtolower( str_replace( '_', '-', $file ) ); |
| 43 |
$filepath = JET_FORM_BUILDER_PATH . 'includes/' . $file . '.php'; |
| 44 |
|
| 45 |
if ( is_readable( $filepath ) ) { |
| 46 |
require $filepath; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Autoload. |
| 52 |
* |
| 53 |
* For a given class, check if it exist and load it. |
| 54 |
* |
| 55 |
* @param string $class Class name. |
| 56 |
* |
| 57 |
* @since 1.6.0 |
| 58 |
* @access private |
| 59 |
* @static |
| 60 |
*/ |
| 61 |
private static function autoload( $class ) { |
| 62 |
|
| 63 |
if ( 0 !== strpos( $class, __NAMESPACE__ . '\\' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$relative_class_name = preg_replace( '/^' . __NAMESPACE__ . '\\\/', '', $class ); |
| 68 |
$final_class_name = __NAMESPACE__ . '\\' . $relative_class_name; |
| 69 |
|
| 70 |
if ( ! class_exists( $final_class_name ) ) { |
| 71 |
self::load_class( $relative_class_name ); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
} |
| 76 |
|