| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* LP_Autoloader |
| 5 |
* |
| 6 |
* @author ThimPress |
| 7 |
* @package LearnPress/Classes |
| 8 |
* @version 1.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class LP_Autoloader { |
| 16 |
|
| 17 |
/** |
| 18 |
* Path to the includes directory |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $include_path = ''; |
| 22 |
|
| 23 |
protected $time = 0; |
| 24 |
|
| 25 |
/** |
| 26 |
* The Constructor |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
if ( function_exists( '__autoload' ) ) { |
| 30 |
spl_autoload_register( '__autoload' ); |
| 31 |
} |
| 32 |
|
| 33 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 34 |
|
| 35 |
$this->include_path = untrailingslashit( LP_PLUGIN_PATH ) . '/inc/'; |
| 36 |
|
| 37 |
add_action( 'shutdown', array( $this, 'output_time' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
public function output_time() { |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Take a class name and turn it into a file name |
| 45 |
* |
| 46 |
* @param string $class |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
private function get_file_name_from_class( $class ) { |
| 51 |
return 'class-' . str_replace( '_', '-', strtolower( $class ) ) . '.php'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Include a class file |
| 56 |
* |
| 57 |
* @param string $path |
| 58 |
* |
| 59 |
* @return bool successful or not |
| 60 |
*/ |
| 61 |
private function load_file( $path ) { |
| 62 |
if ( $path && is_readable( $path ) ) { |
| 63 |
include_once( $path ); |
| 64 |
|
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* |
| 73 |
* @param string $class |
| 74 |
*/ |
| 75 |
public function autoload( $class ) { |
| 76 |
$class = strtolower( $class ); |
| 77 |
$start = microtime( true ); |
| 78 |
$file = $this->get_file_name_from_class( $class ); |
| 79 |
|
| 80 |
if ( preg_match( '~^lp_abstract_shortcode(.*)$~', $class, $m ) ) { |
| 81 |
$file = 'abstract-shortcode' . str_replace( '_', '-', $m[1] ) . '.php'; |
| 82 |
$path = $this->include_path . 'abstracts/'; |
| 83 |
} elseif ( preg_match( '~^lp_shortcode_(.*)$~', $class, $m ) ) { |
| 84 |
$path = $this->include_path . 'Shortcodes/'; |
| 85 |
} else { |
| 86 |
|
| 87 |
// payment gateways |
| 88 |
if ( strpos( $class, 'lp_gateway_' ) === 0 ) { |
| 89 |
$path = $this->include_path . 'gateways/' . substr( str_replace( '_', '-', $class ), 11 ) . '/'; |
| 90 |
} elseif ( preg_match( '!lp_meta_box_|rwmb_!', $class, $matches ) ) { // meta box fields |
| 91 |
|
| 92 |
$file = 'class-' . substr( str_replace( '_', '-', $class ), 5 ) . '.php'; |
| 93 |
$path = $this->include_path . 'admin/meta-boxes/'; |
| 94 |
|
| 95 |
} else { |
| 96 |
$file = 'class-' . str_replace( '_', '-', $class ) . '.php'; |
| 97 |
$path = dirname( __FILE__ ) . '/'; |
| 98 |
if ( strpos( $class, 'lp_user' ) !== false || $class == 'lp_abstract_user' ) { |
| 99 |
$path .= 'user/'; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! file_exists( $path . $file ) ) { |
| 103 |
$segs = explode( '_', $class ); |
| 104 |
if ( ! empty( $segs[1] ) ) { |
| 105 |
$path .= $segs[1] . '/'; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
$this->time += ( microtime( true ) - $start ); |
| 111 |
$this->load_file( $path . $file ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
new LP_Autoloader(); |
| 116 |
|