1 | <?php |
||
2 | /** |
||
3 | * The Kirki autoloader. |
||
4 | * Handles locating and loading other class-files. |
||
5 | * |
||
6 | * @package Kirki |
||
7 | * @category Core |
||
8 | * @author Aristeides Stathopoulos |
||
9 | * @copyright Copyright (c) 2017, Aristeides Stathopoulos |
||
10 | * @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
||
11 | * @since 1.0 |
||
12 | */ |
||
13 | |||
14 | /** |
||
15 | * Autoloader class. |
||
16 | * |
||
17 | * @since 3.0.10 |
||
18 | */ |
||
19 | class Kirki_Autoload { |
||
20 | |||
21 | /** |
||
22 | * Cached paths. |
||
23 | * |
||
24 | * @access private |
||
25 | * @since 3.0.10 |
||
26 | * @var array |
||
27 | */ |
||
28 | private $cached_paths = array(); |
||
29 | |||
30 | /** |
||
31 | * Class constructor. |
||
32 | * |
||
33 | * @access public |
||
34 | * @since 3.0.10 |
||
35 | */ |
||
36 | public function __construct() { |
||
37 | |||
38 | spl_autoload_register( array( $this, 'autoload' ) ); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * The Kirki class autoloader. |
||
43 | * Finds the path to a class that we're requiring and includes the file. |
||
44 | * |
||
45 | * @access protected |
||
46 | * @since 3.0.10 |
||
47 | * @param string $class_name The name of the class we're trying to load. |
||
48 | */ |
||
49 | protected function autoload( $class_name ) { |
||
50 | |||
51 | // Not a Kirki file, early exit. |
||
52 | if ( 0 !== stripos( $class_name, 'Kirki' ) ) { |
||
53 | return; |
||
54 | } |
||
55 | |||
56 | // Check if we've got it cached and ready. |
||
57 | if ( isset( $this->cached_paths[ $class_name ] ) && file_exists( $this->cached_paths[ $class_name ] ) ) { |
||
58 | include_once $this->cached_paths[ $class_name ]; |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | $paths = $this->get_paths( $class_name ); |
||
63 | |||
64 | foreach ( $paths as $path ) { |
||
65 | $path = wp_normalize_path( $path ); |
||
1 ignored issue
–
show
Bug
introduced
by
![]() |
|||
66 | if ( file_exists( $path ) ) { |
||
67 | $this->cached_paths[ $class_name ] = $path; |
||
68 | include_once $path; |
||
69 | return; |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get an array of possible paths for the file. |
||
76 | * |
||
77 | * @access protected |
||
78 | * @since 3.0.10 |
||
79 | * @param string $class_name The name of the class we're trying to load. |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function get_paths( $class_name ) { |
||
83 | |||
84 | $paths = array(); |
||
85 | // Build the filename. |
||
86 | $filename = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php'; |
||
87 | |||
88 | // Break class-name is parts. |
||
89 | $name_parts = explode( '_', str_replace( 'Kirki_', '', $class_name ) ); |
||
90 | |||
91 | // Handle modules loading. |
||
92 | if ( isset( $name_parts[0] ) && 'Modules' === $name_parts[0] ) { |
||
93 | $path = dirname( __FILE__ ) . '/modules/'; |
||
94 | $path .= strtolower( str_replace( '_', '-', str_replace( 'Kirki_Modules_', '', $class_name ) ) ) . '/'; |
||
95 | $paths[] = $path . $filename; |
||
96 | } |
||
97 | |||
98 | if ( isset( $name_parts[0] ) ) { |
||
99 | |||
100 | // Handle controls loading. |
||
101 | if ( 'Control' === $name_parts[0] || 'Settings' === $name_parts[0] ) { |
||
102 | $path = dirname( __FILE__ ) . '/controls/php/'; |
||
103 | $paths[] = $path . $filename; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $paths[] = dirname( __FILE__ ) . '/core/' . $filename; |
||
108 | $paths[] = dirname( __FILE__ ) . '/lib/' . $filename; |
||
109 | |||
110 | $substr = str_replace( 'Kirki_', '', $class_name ); |
||
111 | $exploded = explode( '_', $substr ); |
||
112 | $levels = count( $exploded ); |
||
113 | |||
114 | $previous_path = ''; |
||
115 | for ( $i = 0; $i < $levels; $i++ ) { |
||
116 | $paths[] = dirname( __FILE__ ) . '/' . $previous_path . strtolower( $exploded[ $i ] ) . '/' . $filename; |
||
117 | $previous_path .= strtolower( $exploded[ $i ] ) . '/'; |
||
118 | } |
||
119 | return $paths; |
||
120 | } |
||
121 | } |
||
122 |