-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoload.php
45 lines (37 loc) · 1.49 KB
/
autoload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Dynamically loads the class attempting to be instantiated elsewhere in the
* plugin. Based on the tutorial by Tom McFarlin - https://code.tutsplus.com/tutorials/using-namespaces-and-autoloading-in-wordpress-plugins-4--cms-27342
*
* @package Alchemy_Options\Loader
*/
spl_autoload_register( 'alchemy_options_autoload' );
function alchemy_options_autoload( $class_name ) {
if ( false === strpos( $class_name, 'Alchemy' ) ) {
return;
}
$file_parts = explode( '\\', $class_name );
$namespace = '';
$file_name = '';
for ( $i = count( $file_parts ) - 1; $i > 0; $i-- ) {
$current = str_ireplace( '_', '-', strtolower( $file_parts[ $i ] ) );
if ( count( $file_parts ) - 1 === $i ) {
if ( strpos( strtolower( $file_parts[ count( $file_parts ) - 1 ] ), 'interface' ) ) {
$interface_name = strtolower( explode( '_', $file_parts[ count( $file_parts ) - 1 ] )[0] );
$file_name = "interface-$interface_name.php";
} else {
$file_name = "class-$current.php";
}
} else {
$namespace = '/' . $current . $namespace;
}
}
$filepath = trailingslashit( untrailingslashit( AlCHEMY_DIR_PATH ) . $namespace ) . $file_name;
if ( file_exists( $filepath ) ) {
include_once( $filepath );
} else {
wp_die(
esc_html__( sprintf( "The file attempting to be loaded at %s does not exist.", $filepath ), 'alchemy' )
);
}
}