/
LocationManager.php
164 lines (145 loc) · 3.84 KB
/
LocationManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Timber;
class LocationManager {
/**
* @param bool|string $caller the calling directory (or false)
* @return array
*/
public static function get_locations( $caller = false ) {
//prioirty: user locations, caller (but not theme), child theme, parent theme, caller
$locs = array();
$locs = array_merge($locs, self::get_locations_user());
$locs = array_merge($locs, self::get_locations_caller($caller));
//remove themes from caller
$locs = array_diff($locs, self::get_locations_theme());
$locs = array_merge($locs, self::get_locations_theme());
$locs = array_merge($locs, self::get_locations_caller($caller));
$locs = array_unique($locs);
//now make sure theres a trailing slash on everything
$locs = array_map('trailingslashit', $locs);
/**
* Filters …
*
* @todo Add summary, description, example, parameter description
*
* @since 0.20.10
*
* @param array $locs
*/
$locs = apply_filters('timber/locations', $locs);
/**
* Filters …
*
* @todo Add summary
*
* @deprecated 2.0.0, use `timber/locations`
*/
$locs = apply_filters_deprecated( 'timber_locations', array( $locs ), '2.0.0', 'timber/locations' );
return $locs;
}
/**
* @return array
*/
protected static function get_locations_theme() {
$theme_locs = array();
$theme_dirs = LocationManager::get_locations_theme_dir();
$roots = array(get_stylesheet_directory(), get_template_directory());
$roots = array_map('realpath', $roots);
$roots = array_unique($roots);
foreach ( $roots as $root ) {
if ( !is_dir($root) ) {
continue;
}
$theme_locs[] = $root;
$root = trailingslashit($root);
foreach ( $theme_dirs as $dirname ) {
$tloc = realpath($root.$dirname);
if ( is_dir($tloc) ) {
$theme_locs[] = $tloc;
}
}
}
return $theme_locs;
}
/**
* Get calling script file.
* @api
* @param int $offset
* @return string|null
*/
public static function get_calling_script_file( $offset = 0 ) {
$callers = array();
$backtrace = debug_backtrace();
foreach ( $backtrace as $trace ) {
if ( array_key_exists('file', $trace) && $trace['file'] != __FILE__ ) {
$callers[] = $trace['file'];
}
}
$callers = array_unique($callers);
$callers = array_values($callers);
return $callers[$offset];
}
/**
* Get calling script dir.
* @api
* @return string
*/
public static function get_calling_script_dir( $offset = 0 ) {
$caller = self::get_calling_script_file($offset);
if ( !is_null($caller) ) {
$pathinfo = pathinfo($caller);
$dir = $pathinfo['dirname'];
return $dir;
}
}
/**
* returns an array of the directory inside themes that holds twig files
* @return string[] the names of directores, ie: array('templats', 'views');
*/
public static function get_locations_theme_dir() {
if ( is_string(Timber::$dirname) ) {
return array(Timber::$dirname);
}
return Timber::$dirname;
}
/**
*
* @return array
*/
protected static function get_locations_user() {
$locs = array();
if ( isset(Timber::$locations) ) {
if ( is_string(Timber::$locations) ) {
Timber::$locations = array(Timber::$locations);
}
foreach ( Timber::$locations as $tloc ) {
$tloc = realpath($tloc);
if ( is_dir($tloc) ) {
$locs[] = $tloc;
}
}
}
return $locs;
}
/**
* @param bool|string $caller the calling directory
* @return array
*/
protected static function get_locations_caller( $caller = false ) {
$locs = array();
if ( $caller && is_string($caller) ) {
$caller = realpath($caller);
if ( is_dir($caller) ) {
$locs[] = $caller;
}
$caller = trailingslashit($caller);
foreach ( LocationManager::get_locations_theme_dir() as $dirname ) {
$caller_sub = realpath($caller.$dirname);
if ( is_dir($caller_sub) ) {
$locs[] = $caller_sub;
}
}
}
return $locs;
}
}