Completed
Pull Request — master (#1454)
by Aristeides
04:46 queued 01:57
created

Kirki_Autoload::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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 string $class_name The name of the class we're trying to load.
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
		$paths[] = dirname( __FILE__ ) . '/core/' . $filename;
110
		$paths[] = dirname( __FILE__ ) . '/lib/' . $filename;
111
112
		$substr   = str_replace( 'Kirki_', '', $class_name );
113
		$exploded = explode( '_', $substr );
114
		$levels   = count( $exploded );
115
116
		$previous_path = '';
117
		for ( $i = 0; $i < $levels; $i++ ) {
118
			$paths[] = dirname( __FILE__ ) . '/' . $previous_path . strtolower( $exploded[ $i ] ) . '/' . $filename;
119
			$previous_path .= strtolower( $exploded[ $i ] ) . '/';
120
		}
121
		return $paths;
122
	}
123
}
124