Passed
Push — master ( b01dba...2c96ef )
by Jip
03:00
created

Stencil_File_System::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
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
		if ( empty( $directory ) ) {
28
			return false;
29
		}
30
31
		$paths = self::get_potential_directories( $directory );
32
		foreach ( $paths as $path ) {
33
			if ( is_file( $path . $file ) ) {
34
				include $path . $file;
35
36
				return true;
37
			}
38
		}
39
40
		return false;
41
	}
42
43
	/**
44
	 * Get the paths available that might contain a subdirectory
45
	 *
46
	 * Depending on the theme being a child theme or not
47
	 * The child theme is being prepended to the paths list
48
	 *
49
	 * @param string $sub_directory Name of directory to test.
50
	 *
51
	 * @return array Root and optionally child path
52
	 */
53
	public static function get_potential_directories( $sub_directory ) {
54
		static $theme_root;
55
		static $child_root;
56
57
		if ( ! isset( $theme_root ) ) {
58
			$theme_root = get_template_directory();
59
		}
60
		if ( ! isset( $child_root ) ) {
61
			$child_root = get_stylesheet_directory();
62
		}
63
64
		$paths = array(
65
			implode( DIRECTORY_SEPARATOR, array( $theme_root, $sub_directory, '' ) ),
66
		);
67
68
		// First check child theme.
69
		if ( $theme_root !== $child_root ) {
70
			array_unshift( $paths, implode( DIRECTORY_SEPARATOR, array( $child_root, $sub_directory, '' ) ) );
71
		}
72
73
		return $paths;
74
	}
75
}
76