-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathautoloader.php
82 lines (68 loc) · 2.54 KB
/
autoloader.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* The Kirki autoloader.
* Handles locating and loading other class-files.
*
* @package Kirki
* @category Core
* @author Aristeides Stathopoulos
* @copyright Copyright (c) 2017, Aristeides Stathopoulos
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT
* @since 1.0
*/
if ( ! function_exists( 'kirki_autoload_classes' ) ) {
/**
* The Kirki class autoloader.
* Finds the path to a class that we're requiring and includes the file.
*
* @param string $class_name The name of the class we're trying to load.
*/
function kirki_autoload_classes( $class_name ) {
$paths = array();
if ( 0 === stripos( $class_name, 'Kirki' ) ) {
// Build the filename.
$filename = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
// Break class-name is parts.
$name_parts = explode( '_', str_replace( 'Kirki_', '', $class_name ) );
// Handle modules loading.
if ( isset( $name_parts[0] ) && 'Modules' === $name_parts[0] ) {
$path = dirname( __FILE__ ) . '/modules/';
$path .= strtolower( str_replace( '_', '-', str_replace( 'Kirki_Modules_', '', $class_name ) ) ) . '/';
$paths[] = $path . $filename;
}
if ( isset( $name_parts[0] ) ) {
// Handle controls loading.
if ( 'Control' === $name_parts[0] ) {
$path = dirname( __FILE__ ) . '/controls/';
$path .= strtolower( str_replace( '_', '-', str_replace( 'Kirki_Control_', '', $class_name ) ) ) . '/';
$paths[] = $path . $filename;
}
// Handle settings loading.
if ( 'Settings' === $name_parts[0] ) {
$path = dirname( __FILE__ ) . '/controls/';
$path .= strtolower( str_replace( '_', '-', str_replace( array( 'Kirki_Settings_', '_Setting' ), '', $class_name ) ) ) . '/';
$paths[] = $path . $filename;
}
}
$paths[] = dirname( __FILE__ ) . '/core/' . $filename;
$paths[] = dirname( __FILE__ ) . '/lib/' . $filename;
$substr = str_replace( 'Kirki_', '', $class_name );
$exploded = explode( '_', $substr );
$levels = count( $exploded );
$previous_path = '';
for ( $i = 0; $i < $levels; $i++ ) {
$paths[] = dirname( __FILE__ ) . '/' . $previous_path . strtolower( $exploded[ $i ] ) . '/' . $filename;
$previous_path .= strtolower( $exploded[ $i ] ) . '/';
}
foreach ( $paths as $path ) {
$path = wp_normalize_path( $path );
if ( file_exists( $path ) ) {
include_once $path;
return;
}
}
} // End if().
}
// Run the autoloader.
spl_autoload_register( 'kirki_autoload_classes' );
} // End if().