Passed
Push — master ( 6c357f...71c354 )
by Jip
02:55
created

Stencil_File_System::get()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.5907
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
/**
3
 * Load files from root or child theme
4
 *
5
 * @package Stencil
6
 */
7
8
/**
9
 * Class Stencil_File_System
10
 */
11
class Stencil_File_System {
12
	/**
13
	 * Include a file from child or root theme
14
	 *
15
	 * @param string $file File to include.
16
	 *
17
	 * @return bool True if a file was found.
18
	 */
19
	public static function load( $file ) {
20
		$file = rtrim( $file, '.php' ) . '.php';
21
22
		/**
23
		 * Filter controllers directory
24
		 */
25
		$directory = Stencil_Environment::filter( 'path-controllers', 'controllers' );
26
27
		$paths = self::get_potential_directories( $directory );
28
		foreach ( $paths as $path ) {
29
			if ( is_file( $path . $file ) ) {
30
				include $path . $file;
31
32
				return true;
33
			}
34
		}
35
36
		return false;
37
	}
38
39
	/**
40
	 * Get the paths available that might contain a subdirectory
41
	 *
42
	 * Depending on the theme being a child theme or not
43
	 * The child theme is being prepended to the paths list
44
	 *
45
	 * @param string $sub_directory Name of directory to test.
46
	 *
47
	 * @return array Root and optionally child path
48
	 */
49
	public static function get_potential_directories( $sub_directory ) {
50
		static $theme_root;
51
		static $child_root;
52
53
		if ( ! isset( $theme_root ) ) {
54
			$theme_root = get_template_directory();
55
		}
56
		if ( ! isset( $child_root ) ) {
57
			$child_root = get_stylesheet_directory();
58
		}
59
60
		$paths = array(
61
			implode( DIRECTORY_SEPARATOR, array( $theme_root, $sub_directory, '' ) )
62
		);
63
64
		// First check child theme.
65
		if ( $theme_root !== $child_root ) {
66
			array_unshift( $paths, implode( DIRECTORY_SEPARATOR, array( $child_root, $sub_directory, '' ) ) );
67
		}
68
69
		return $paths;
70
	}
71
}
72