Completed
Push — develop ( cb29d4...dd518f )
by Aristeides
02:38
created

Kirki_Autoload::get_paths()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 20
nop 1
dl 0
loc 49
rs 6.7272
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
	 * The transient name.
23
	 *
24
	 * @access private
25
	 * @since 3.0.10
26
	 * @var string
27
	 */
28
	private $transient_name = 'kirki_autoload_paths';
29
30
	/**
31
	 * Cached paths.
32
	 *
33
	 * @access private
34
	 * @since 3.0.10
35
	 * @var array
36
	 */
37
	private $cached_paths = array();
38
39
	/**
40
	 * Class constructor.
41
	 *
42
	 * @access public
43
	 * @since 3.0.10
44
	 */
45
	public function __construct() {
46
47
		$this->cached_paths = get_transient( $this->transient_name );
48
		spl_autoload_register( array( $this, 'autoload' ) );
49
	}
50
51
	/**
52
	 * The Kirki class autoloader.
53
	 * Finds the path to a class that we're requiring and includes the file.
54
	 *
55
	 * @access protected
56
	 * @since 3.0.10
57
	 * @param string $class_name The name of the class we're trying to load.
58
	 */
59
	protected function autoload( $class_name ) {
60
61
		// Not a Kirki file, early exit.
62
		if ( 0 !== stripos( $class_name, 'Kirki' ) ) {
63
			return;
64
		}
65
66
		// Check if we've got it cached and ready.
67
		if ( isset( $this->cached_paths[ $class_name ] ) && file_exists( $this->cached_paths[ $class_name ] ) ) {
68
			include_once $this->cached_paths[ $class_name ];
69
			return;
70
		}
71
72
		$paths = $this->get_paths( $class_name );
73
74
		foreach ( $paths as $path ) {
75
			$path = wp_normalize_path( $path );
76
			if ( file_exists( $path ) ) {
77
				$this->cached_paths[ $class_name ] = $path;
78
				include_once $path;
79
				return;
80
			}
81
		}
82
		set_transient( $this->transient_name, $this->cached_paths, 5 * MINUTE_IN_SECONDS );
83
	}
84
85
	/**
86
	 * Get an array of possible paths for the file.
87
	 *
88
	 * @access protected
89
	 * @since 3.0.10
90
	 * @param $class_name The name of the class we're looking for.
91
	 * @return array
92
	 */
93
	protected function get_paths( $class_name ) {
94
95
		$paths = array();
96
		// Build the filename.
97
		$filename = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
98
99
		// Break class-name is parts.
100
		$name_parts = explode( '_', str_replace( 'Kirki_', '', $class_name ) );
101
102
		// Handle modules loading.
103
		if ( isset( $name_parts[0] ) && 'Modules' === $name_parts[0] ) {
104
			$path  = dirname( __FILE__ ) . '/modules/';
105
			$path .= strtolower( str_replace( '_', '-', str_replace( 'Kirki_Modules_', '', $class_name ) ) ) . '/';
106
			$paths[] = $path . $filename;
107
		}
108
109
		if ( isset( $name_parts[0] ) ) {
110
111
			// Handle controls loading.
112
			if ( 'Control' === $name_parts[0] ) {
113
				$path  = dirname( __FILE__ ) . '/controls/';
114
				$path .= strtolower( str_replace( '_', '-', str_replace( 'Kirki_Control_', '', $class_name ) ) ) . '/';
115
116
				$paths[] = $path . $filename;
117
			}
118
119
			// Handle settings loading.
120
			if ( 'Settings' === $name_parts[0] ) {
121
				$path  = dirname( __FILE__ ) . '/controls/';
122
				$path .= strtolower( str_replace( '_', '-', str_replace( array( 'Kirki_Settings_', '_Setting' ), '', $class_name ) ) ) . '/';
123
124
				$paths[] = $path . $filename;
125
			}
126
		}
127
128
		$paths[] = dirname( __FILE__ ) . '/core/' . $filename;
129
		$paths[] = dirname( __FILE__ ) . '/lib/' . $filename;
130
131
		$substr   = str_replace( 'Kirki_', '', $class_name );
132
		$exploded = explode( '_', $substr );
133
		$levels   = count( $exploded );
134
135
		$previous_path = '';
136
		for ( $i = 0; $i < $levels; $i++ ) {
137
			$paths[] = dirname( __FILE__ ) . '/' . $previous_path . strtolower( $exploded[ $i ] ) . '/' . $filename;
138
			$previous_path .= strtolower( $exploded[ $i ] ) . '/';
139
		}
140
		return $paths;
141
	}
142
}
143