Completed
Pull Request — master (#1592)
by Aristeides
04:14 queued 02:08
created

Kirki_Autoload::autoload()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 5
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
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 );
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] ) {
102
				$path  = dirname( __FILE__ ) . '/controls/';
103
				$path .= strtolower( str_replace( '_', '-', str_replace( 'Kirki_Control_', '', $class_name ) ) ) . '/';
104
105
				$paths[] = $path . $filename;
106
			}
107
108
			// Handle settings loading.
109
			if ( 'Settings' === $name_parts[0] ) {
110
				$path  = dirname( __FILE__ ) . '/controls/';
111
				$path .= strtolower( str_replace( '_', '-', str_replace( array( 'Kirki_Settings_', '_Setting' ), '', $class_name ) ) ) . '/';
112
113
				$paths[] = $path . $filename;
114
			}
115
		}
116
117
		$paths[] = dirname( __FILE__ ) . '/core/' . $filename;
118
		$paths[] = dirname( __FILE__ ) . '/lib/' . $filename;
119
120
		$substr   = str_replace( 'Kirki_', '', $class_name );
121
		$exploded = explode( '_', $substr );
122
		$levels   = count( $exploded );
123
124
		$previous_path = '';
125
		for ( $i = 0; $i < $levels; $i++ ) {
126
			$paths[] = dirname( __FILE__ ) . '/' . $previous_path . strtolower( $exploded[ $i ] ) . '/' . $filename;
127
			$previous_path .= strtolower( $exploded[ $i ] ) . '/';
128
		}
129
		return $paths;
130
	}
131
}
132