author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:30:03 +0200 | |
changeset 10 | 372f2766ea20 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WP_Theme Class |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Theme |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* @since 3.4.0 |
0 | 8 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
final class WP_Theme implements ArrayAccess { |
0 | 10 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
* Whether the theme has been marked as updateable. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* @var bool |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* @see WP_MS_Themes_List_Table |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
public $update = false; |
0 | 20 |
|
21 |
/** |
|
22 |
* Headers for style.css files. |
|
23 |
* |
|
24 |
* @var array |
|
25 |
*/ |
|
26 |
private static $file_headers = array( |
|
27 |
'Name' => 'Theme Name', |
|
28 |
'ThemeURI' => 'Theme URI', |
|
29 |
'Description' => 'Description', |
|
30 |
'Author' => 'Author', |
|
31 |
'AuthorURI' => 'Author URI', |
|
32 |
'Version' => 'Version', |
|
33 |
'Template' => 'Template', |
|
34 |
'Status' => 'Status', |
|
35 |
'Tags' => 'Tags', |
|
36 |
'TextDomain' => 'Text Domain', |
|
37 |
'DomainPath' => 'Domain Path', |
|
38 |
); |
|
39 |
||
40 |
/** |
|
41 |
* Default themes. |
|
42 |
* |
|
43 |
* @var array |
|
44 |
*/ |
|
45 |
private static $default_themes = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
'classic' => 'WordPress Classic', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
'default' => 'WordPress Default', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
'twentyten' => 'Twenty Ten', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
'twentyeleven' => 'Twenty Eleven', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
'twentytwelve' => 'Twenty Twelve', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
'twentythirteen' => 'Twenty Thirteen', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
'twentyfourteen' => 'Twenty Fourteen', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
'twentyfifteen' => 'Twenty Fifteen', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
'twentysixteen' => 'Twenty Sixteen', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
'twentyseventeen' => 'Twenty Seventeen', |
9 | 56 |
'twentynineteen' => 'Twenty Nineteen', |
5 | 57 |
); |
58 |
||
59 |
/** |
|
60 |
* Renamed theme tags. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
* @var array |
5 | 63 |
*/ |
64 |
private static $tag_map = array( |
|
65 |
'fixed-width' => 'fixed-layout', |
|
66 |
'flexible-width' => 'fluid-layout', |
|
0 | 67 |
); |
68 |
||
69 |
/** |
|
70 |
* Absolute path to the theme root, usually wp-content/themes |
|
71 |
* |
|
72 |
* @var string |
|
73 |
*/ |
|
74 |
private $theme_root; |
|
75 |
||
76 |
/** |
|
77 |
* Header data from the theme's style.css file. |
|
78 |
* |
|
79 |
* @var array |
|
80 |
*/ |
|
81 |
private $headers = array(); |
|
82 |
||
83 |
/** |
|
84 |
* Header data from the theme's style.css file after being sanitized. |
|
85 |
* |
|
86 |
* @var array |
|
87 |
*/ |
|
88 |
private $headers_sanitized; |
|
89 |
||
90 |
/** |
|
91 |
* Header name from the theme's style.css after being translated. |
|
92 |
* |
|
93 |
* Cached due to sorting functions running over the translated name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
* @var string |
0 | 96 |
*/ |
97 |
private $name_translated; |
|
98 |
||
99 |
/** |
|
100 |
* Errors encountered when initializing the theme. |
|
101 |
* |
|
102 |
* @var WP_Error |
|
103 |
*/ |
|
104 |
private $errors; |
|
105 |
||
106 |
/** |
|
107 |
* The directory name of the theme's files, inside the theme root. |
|
108 |
* |
|
109 |
* In the case of a child theme, this is directory name of the child theme. |
|
110 |
* Otherwise, 'stylesheet' is the same as 'template'. |
|
111 |
* |
|
112 |
* @var string |
|
113 |
*/ |
|
114 |
private $stylesheet; |
|
115 |
||
116 |
/** |
|
117 |
* The directory name of the theme's files, inside the theme root. |
|
118 |
* |
|
119 |
* In the case of a child theme, this is the directory name of the parent theme. |
|
120 |
* Otherwise, 'template' is the same as 'stylesheet'. |
|
121 |
* |
|
122 |
* @var string |
|
123 |
*/ |
|
124 |
private $template; |
|
125 |
||
126 |
/** |
|
127 |
* A reference to the parent theme, in the case of a child theme. |
|
128 |
* |
|
129 |
* @var WP_Theme |
|
130 |
*/ |
|
131 |
private $parent; |
|
132 |
||
133 |
/** |
|
134 |
* URL to the theme root, usually an absolute URL to wp-content/themes |
|
135 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* @var string |
0 | 137 |
*/ |
138 |
private $theme_root_uri; |
|
139 |
||
140 |
/** |
|
141 |
* Flag for whether the theme's textdomain is loaded. |
|
142 |
* |
|
143 |
* @var bool |
|
144 |
*/ |
|
145 |
private $textdomain_loaded; |
|
146 |
||
147 |
/** |
|
148 |
* Stores an md5 hash of the theme root, to function as the cache key. |
|
149 |
* |
|
150 |
* @var string |
|
151 |
*/ |
|
152 |
private $cache_hash; |
|
153 |
||
154 |
/** |
|
155 |
* Flag for whether the themes cache bucket should be persistently cached. |
|
156 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
* Default is false. Can be set with the {@see 'wp_cache_themes_persistently'} filter. |
0 | 158 |
* |
159 |
* @var bool |
|
160 |
*/ |
|
161 |
private static $persistently_cache; |
|
162 |
||
163 |
/** |
|
164 |
* Expiration time for the themes cache bucket. |
|
165 |
* |
|
166 |
* By default the bucket is not cached, so this value is useless. |
|
167 |
* |
|
168 |
* @var bool |
|
169 |
*/ |
|
170 |
private static $cache_expiration = 1800; |
|
171 |
||
172 |
/** |
|
173 |
* Constructor for WP_Theme. |
|
174 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
* @global array $wp_theme_directories |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* |
0 | 179 |
* @param string $theme_dir Directory of the theme within the theme_root. |
180 |
* @param string $theme_root Theme root. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
* @param WP_Error|void $_child If this theme is a parent theme, the child may be passed for validation purposes. |
0 | 182 |
*/ |
183 |
public function __construct( $theme_dir, $theme_root, $_child = null ) { |
|
184 |
global $wp_theme_directories; |
|
185 |
||
186 |
// Initialize caching on first run. |
|
187 |
if ( ! isset( self::$persistently_cache ) ) { |
|
5 | 188 |
/** This action is documented in wp-includes/theme.php */ |
0 | 189 |
self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' ); |
190 |
if ( self::$persistently_cache ) { |
|
191 |
wp_cache_add_global_groups( 'themes' ); |
|
9 | 192 |
if ( is_int( self::$persistently_cache ) ) { |
0 | 193 |
self::$cache_expiration = self::$persistently_cache; |
9 | 194 |
} |
0 | 195 |
} else { |
196 |
wp_cache_add_non_persistent_groups( 'themes' ); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
$this->theme_root = $theme_root; |
|
201 |
$this->stylesheet = $theme_dir; |
|
202 |
||
203 |
// Correct a situation where the theme is 'some-directory/some-theme' but 'some-directory' was passed in as part of the theme root instead. |
|
204 |
if ( ! in_array( $theme_root, (array) $wp_theme_directories ) && in_array( dirname( $theme_root ), (array) $wp_theme_directories ) ) { |
|
205 |
$this->stylesheet = basename( $this->theme_root ) . '/' . $this->stylesheet; |
|
206 |
$this->theme_root = dirname( $theme_root ); |
|
207 |
} |
|
208 |
||
209 |
$this->cache_hash = md5( $this->theme_root . '/' . $this->stylesheet ); |
|
9 | 210 |
$theme_file = $this->stylesheet . '/style.css'; |
0 | 211 |
|
212 |
$cache = $this->cache_get( 'theme' ); |
|
213 |
||
214 |
if ( is_array( $cache ) ) { |
|
215 |
foreach ( array( 'errors', 'headers', 'template' ) as $key ) { |
|
9 | 216 |
if ( isset( $cache[ $key ] ) ) { |
0 | 217 |
$this->$key = $cache[ $key ]; |
9 | 218 |
} |
0 | 219 |
} |
9 | 220 |
if ( $this->errors ) { |
0 | 221 |
return; |
9 | 222 |
} |
223 |
if ( isset( $cache['theme_root_template'] ) ) { |
|
0 | 224 |
$theme_root_template = $cache['theme_root_template']; |
9 | 225 |
} |
0 | 226 |
} elseif ( ! file_exists( $this->theme_root . '/' . $theme_file ) ) { |
227 |
$this->headers['Name'] = $this->stylesheet; |
|
9 | 228 |
if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
$this->errors = new WP_Error( 'theme_not_found', sprintf( __( 'The theme directory "%s" does not exist.' ), esc_html( $this->stylesheet ) ) ); |
9 | 230 |
} else { |
0 | 231 |
$this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) ); |
9 | 232 |
} |
0 | 233 |
$this->template = $this->stylesheet; |
9 | 234 |
$this->cache_add( |
235 |
'theme', |
|
236 |
array( |
|
237 |
'headers' => $this->headers, |
|
238 |
'errors' => $this->errors, |
|
239 |
'stylesheet' => $this->stylesheet, |
|
240 |
'template' => $this->template, |
|
241 |
) |
|
242 |
); |
|
243 |
if ( ! file_exists( $this->theme_root ) ) { // Don't cache this one. |
|
0 | 244 |
$this->errors->add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn’t exist. Please check your installation.' ) ); |
9 | 245 |
} |
0 | 246 |
return; |
247 |
} elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) { |
|
248 |
$this->headers['Name'] = $this->stylesheet; |
|
9 | 249 |
$this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); |
250 |
$this->template = $this->stylesheet; |
|
251 |
$this->cache_add( |
|
252 |
'theme', |
|
253 |
array( |
|
254 |
'headers' => $this->headers, |
|
255 |
'errors' => $this->errors, |
|
256 |
'stylesheet' => $this->stylesheet, |
|
257 |
'template' => $this->template, |
|
258 |
) |
|
259 |
); |
|
0 | 260 |
return; |
261 |
} else { |
|
262 |
$this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' ); |
|
263 |
// Default themes always trump their pretenders. |
|
264 |
// Properly identify default themes that are inside a directory within wp-content/themes. |
|
265 |
if ( $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes ) ) { |
|
9 | 266 |
if ( basename( $this->stylesheet ) != $default_theme_slug ) { |
0 | 267 |
$this->headers['Name'] .= '/' . $this->stylesheet; |
9 | 268 |
} |
0 | 269 |
} |
270 |
} |
|
271 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
if ( ! $this->template && $this->stylesheet === $this->headers['Template'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
273 |
/* translators: %s: Template */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
$this->errors = new WP_Error( 'theme_child_invalid', sprintf( __( 'The theme defines itself as its parent theme. Please check the %s header.' ), '<code>Template</code>' ) ); |
9 | 275 |
$this->cache_add( |
276 |
'theme', |
|
277 |
array( |
|
278 |
'headers' => $this->headers, |
|
279 |
'errors' => $this->errors, |
|
280 |
'stylesheet' => $this->stylesheet, |
|
281 |
) |
|
282 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
|
0 | 287 |
// (If template is set from cache [and there are no errors], we know it's good.) |
288 |
if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) { |
|
289 |
$this->template = $this->stylesheet; |
|
290 |
if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
$error_message = sprintf( |
9 | 292 |
/* translators: 1: index.php, 2: link to documentation, 3: style.css */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
__( 'Template is missing. Standalone themes need to have a %1$s template file. <a href="%2$s">Child themes</a> need to have a Template header in the %3$s stylesheet.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
'<code>index.php</code>', |
9 | 295 |
__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
'<code>style.css</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
$this->errors = new WP_Error( 'theme_no_index', $error_message ); |
9 | 299 |
$this->cache_add( |
300 |
'theme', |
|
301 |
array( |
|
302 |
'headers' => $this->headers, |
|
303 |
'errors' => $this->errors, |
|
304 |
'stylesheet' => $this->stylesheet, |
|
305 |
'template' => $this->template, |
|
306 |
) |
|
307 |
); |
|
0 | 308 |
return; |
309 |
} |
|
310 |
} |
|
311 |
||
312 |
// If we got our data from cache, we can assume that 'template' is pointing to the right place. |
|
313 |
if ( ! is_array( $cache ) && $this->template != $this->stylesheet && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) ) { |
|
314 |
// If we're in a directory of themes inside /themes, look for the parent nearby. |
|
315 |
// wp-content/themes/directory-of-themes/* |
|
316 |
$parent_dir = dirname( $this->stylesheet ); |
|
317 |
if ( '.' != $parent_dir && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) ) { |
|
318 |
$this->template = $parent_dir . '/' . $this->template; |
|
319 |
} elseif ( ( $directories = search_theme_directories() ) && isset( $directories[ $this->template ] ) ) { |
|
320 |
// Look for the template in the search_theme_directories() results, in case it is in another theme root. |
|
321 |
// We don't look into directories of themes, just the theme root. |
|
322 |
$theme_root_template = $directories[ $this->template ]['theme_root']; |
|
323 |
} else { |
|
324 |
// Parent theme is missing. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
$this->errors = new WP_Error( 'theme_no_parent', sprintf( __( 'The parent theme is missing. Please install the "%s" parent theme.' ), esc_html( $this->template ) ) ); |
9 | 326 |
$this->cache_add( |
327 |
'theme', |
|
328 |
array( |
|
329 |
'headers' => $this->headers, |
|
330 |
'errors' => $this->errors, |
|
331 |
'stylesheet' => $this->stylesheet, |
|
332 |
'template' => $this->template, |
|
333 |
) |
|
334 |
); |
|
0 | 335 |
$this->parent = new WP_Theme( $this->template, $this->theme_root, $this ); |
336 |
return; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
// Set the parent, if we're a child theme. |
|
341 |
if ( $this->template != $this->stylesheet ) { |
|
342 |
// If we are a parent, then there is a problem. Only two generations allowed! Cancel things out. |
|
5 | 343 |
if ( $_child instanceof WP_Theme && $_child->template == $this->stylesheet ) { |
0 | 344 |
$_child->parent = null; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
$_child->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), esc_html( $_child->template ) ) ); |
9 | 346 |
$_child->cache_add( |
347 |
'theme', |
|
348 |
array( |
|
349 |
'headers' => $_child->headers, |
|
350 |
'errors' => $_child->errors, |
|
351 |
'stylesheet' => $_child->stylesheet, |
|
352 |
'template' => $_child->template, |
|
353 |
) |
|
354 |
); |
|
0 | 355 |
// The two themes actually reference each other with the Template header. |
356 |
if ( $_child->stylesheet == $this->template ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
$this->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), esc_html( $this->template ) ) ); |
9 | 358 |
$this->cache_add( |
359 |
'theme', |
|
360 |
array( |
|
361 |
'headers' => $this->headers, |
|
362 |
'errors' => $this->errors, |
|
363 |
'stylesheet' => $this->stylesheet, |
|
364 |
'template' => $this->template, |
|
365 |
) |
|
366 |
); |
|
0 | 367 |
} |
368 |
return; |
|
369 |
} |
|
370 |
// Set the parent. Pass the current instance so we can do the crazy checks above and assess errors. |
|
371 |
$this->parent = new WP_Theme( $this->template, isset( $theme_root_template ) ? $theme_root_template : $this->theme_root, $this ); |
|
372 |
} |
|
373 |
||
9 | 374 |
if ( wp_paused_themes()->get( $this->stylesheet ) && ( ! is_wp_error( $this->errors ) || ! isset( $this->errors->errors['theme_paused'] ) ) ) { |
375 |
$this->errors = new WP_Error( 'theme_paused', __( 'This theme failed to load properly and was paused within the admin backend.' ) ); |
|
376 |
} |
|
377 |
||
0 | 378 |
// We're good. If we didn't retrieve from cache, set it. |
379 |
if ( ! is_array( $cache ) ) { |
|
9 | 380 |
$cache = array( |
381 |
'headers' => $this->headers, |
|
382 |
'errors' => $this->errors, |
|
383 |
'stylesheet' => $this->stylesheet, |
|
384 |
'template' => $this->template, |
|
385 |
); |
|
0 | 386 |
// If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above. |
9 | 387 |
if ( isset( $theme_root_template ) ) { |
0 | 388 |
$cache['theme_root_template'] = $theme_root_template; |
9 | 389 |
} |
0 | 390 |
$this->cache_add( 'theme', $cache ); |
391 |
} |
|
392 |
} |
|
393 |
||
394 |
/** |
|
395 |
* When converting the object to a string, the theme name is returned. |
|
396 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* |
0 | 399 |
* @return string Theme name, ready for display (translated) |
400 |
*/ |
|
401 |
public function __toString() { |
|
9 | 402 |
return (string) $this->display( 'Name' ); |
0 | 403 |
} |
404 |
||
405 |
/** |
|
406 |
* __isset() magic method for properties formerly returned by current_theme_info() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
* @staticvar array $properties |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
* @param string $offset Property to check if set. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
* @return bool Whether the given property is set. |
0 | 414 |
*/ |
415 |
public function __isset( $offset ) { |
|
416 |
static $properties = array( |
|
9 | 417 |
'name', |
418 |
'title', |
|
419 |
'version', |
|
420 |
'parent_theme', |
|
421 |
'template_dir', |
|
422 |
'stylesheet_dir', |
|
423 |
'template', |
|
424 |
'stylesheet', |
|
425 |
'screenshot', |
|
426 |
'description', |
|
427 |
'author', |
|
428 |
'tags', |
|
429 |
'theme_root', |
|
430 |
'theme_root_uri', |
|
0 | 431 |
); |
432 |
||
433 |
return in_array( $offset, $properties ); |
|
434 |
} |
|
435 |
||
436 |
/** |
|
437 |
* __get() magic method for properties formerly returned by current_theme_info() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
* @param string $offset Property to get. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
* @return mixed Property value. |
0 | 443 |
*/ |
444 |
public function __get( $offset ) { |
|
445 |
switch ( $offset ) { |
|
9 | 446 |
case 'name': |
447 |
case 'title': |
|
448 |
return $this->get( 'Name' ); |
|
449 |
case 'version': |
|
450 |
return $this->get( 'Version' ); |
|
451 |
case 'parent_theme': |
|
452 |
return $this->parent() ? $this->parent()->get( 'Name' ) : ''; |
|
453 |
case 'template_dir': |
|
0 | 454 |
return $this->get_template_directory(); |
9 | 455 |
case 'stylesheet_dir': |
0 | 456 |
return $this->get_stylesheet_directory(); |
9 | 457 |
case 'template': |
0 | 458 |
return $this->get_template(); |
9 | 459 |
case 'stylesheet': |
0 | 460 |
return $this->get_stylesheet(); |
9 | 461 |
case 'screenshot': |
0 | 462 |
return $this->get_screenshot( 'relative' ); |
463 |
// 'author' and 'description' did not previously return translated data. |
|
9 | 464 |
case 'description': |
465 |
return $this->display( 'Description' ); |
|
466 |
case 'author': |
|
467 |
return $this->display( 'Author' ); |
|
468 |
case 'tags': |
|
0 | 469 |
return $this->get( 'Tags' ); |
9 | 470 |
case 'theme_root': |
0 | 471 |
return $this->get_theme_root(); |
9 | 472 |
case 'theme_root_uri': |
0 | 473 |
return $this->get_theme_root_uri(); |
474 |
// For cases where the array was converted to an object. |
|
9 | 475 |
default: |
0 | 476 |
return $this->offsetGet( $offset ); |
477 |
} |
|
478 |
} |
|
479 |
||
480 |
/** |
|
481 |
* Method to implement ArrayAccess for keys formerly returned by get_themes() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* @param mixed $offset |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
* @param mixed $value |
0 | 487 |
*/ |
488 |
public function offsetSet( $offset, $value ) {} |
|
489 |
||
490 |
/** |
|
491 |
* Method to implement ArrayAccess for keys formerly returned by get_themes() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
* @param mixed $offset |
0 | 496 |
*/ |
497 |
public function offsetUnset( $offset ) {} |
|
498 |
||
499 |
/** |
|
500 |
* Method to implement ArrayAccess for keys formerly returned by get_themes() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
* @staticvar array $keys |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
* @param mixed $offset |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
* @return bool |
0 | 508 |
*/ |
509 |
public function offsetExists( $offset ) { |
|
510 |
static $keys = array( |
|
9 | 511 |
'Name', |
512 |
'Version', |
|
513 |
'Status', |
|
514 |
'Title', |
|
515 |
'Author', |
|
516 |
'Author Name', |
|
517 |
'Author URI', |
|
518 |
'Description', |
|
519 |
'Template', |
|
520 |
'Stylesheet', |
|
521 |
'Template Files', |
|
522 |
'Stylesheet Files', |
|
523 |
'Template Dir', |
|
524 |
'Stylesheet Dir', |
|
525 |
'Screenshot', |
|
526 |
'Tags', |
|
527 |
'Theme Root', |
|
528 |
'Theme Root URI', |
|
529 |
'Parent Theme', |
|
0 | 530 |
); |
531 |
||
532 |
return in_array( $offset, $keys ); |
|
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* Method to implement ArrayAccess for keys formerly returned by get_themes(). |
|
537 |
* |
|
538 |
* Author, Author Name, Author URI, and Description did not previously return |
|
539 |
* translated data. We are doing so now as it is safe to do. However, as |
|
540 |
* Name and Title could have been used as the key for get_themes(), both remain |
|
541 |
* untranslated for back compatibility. This means that ['Name'] is not ideal, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
* and care should be taken to use `$theme::display( 'Name' )` to get a properly |
0 | 543 |
* translated header. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
544 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
* @param mixed $offset |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
548 |
* @return mixed |
0 | 549 |
*/ |
550 |
public function offsetGet( $offset ) { |
|
551 |
switch ( $offset ) { |
|
9 | 552 |
case 'Name': |
553 |
case 'Title': |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
* See note above about using translated data. get() is not ideal. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
* It is only for backward compatibility. Use display(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
*/ |
9 | 558 |
return $this->get( 'Name' ); |
559 |
case 'Author': |
|
560 |
return $this->display( 'Author' ); |
|
561 |
case 'Author Name': |
|
562 |
return $this->display( 'Author', false ); |
|
563 |
case 'Author URI': |
|
564 |
return $this->display( 'AuthorURI' ); |
|
565 |
case 'Description': |
|
566 |
return $this->display( 'Description' ); |
|
567 |
case 'Version': |
|
568 |
case 'Status': |
|
0 | 569 |
return $this->get( $offset ); |
9 | 570 |
case 'Template': |
0 | 571 |
return $this->get_template(); |
9 | 572 |
case 'Stylesheet': |
0 | 573 |
return $this->get_stylesheet(); |
9 | 574 |
case 'Template Files': |
0 | 575 |
return $this->get_files( 'php', 1, true ); |
9 | 576 |
case 'Stylesheet Files': |
0 | 577 |
return $this->get_files( 'css', 0, false ); |
9 | 578 |
case 'Template Dir': |
0 | 579 |
return $this->get_template_directory(); |
9 | 580 |
case 'Stylesheet Dir': |
0 | 581 |
return $this->get_stylesheet_directory(); |
9 | 582 |
case 'Screenshot': |
0 | 583 |
return $this->get_screenshot( 'relative' ); |
9 | 584 |
case 'Tags': |
585 |
return $this->get( 'Tags' ); |
|
586 |
case 'Theme Root': |
|
0 | 587 |
return $this->get_theme_root(); |
9 | 588 |
case 'Theme Root URI': |
0 | 589 |
return $this->get_theme_root_uri(); |
9 | 590 |
case 'Parent Theme': |
591 |
return $this->parent() ? $this->parent()->get( 'Name' ) : ''; |
|
592 |
default: |
|
0 | 593 |
return null; |
594 |
} |
|
595 |
} |
|
596 |
||
597 |
/** |
|
598 |
* Returns errors property. |
|
599 |
* |
|
600 |
* @since 3.4.0 |
|
601 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
* @return WP_Error|false WP_Error if there are errors, or false. |
0 | 603 |
*/ |
604 |
public function errors() { |
|
605 |
return is_wp_error( $this->errors ) ? $this->errors : false; |
|
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* Whether the theme exists. |
|
610 |
* |
|
611 |
* A theme with errors exists. A theme with the error of 'theme_not_found', |
|
612 |
* meaning that the theme's directory was not found, does not exist. |
|
613 |
* |
|
614 |
* @since 3.4.0 |
|
615 |
* |
|
616 |
* @return bool Whether the theme exists. |
|
617 |
*/ |
|
618 |
public function exists() { |
|
619 |
return ! ( $this->errors() && in_array( 'theme_not_found', $this->errors()->get_error_codes() ) ); |
|
620 |
} |
|
621 |
||
622 |
/** |
|
623 |
* Returns reference to the parent theme. |
|
624 |
* |
|
625 |
* @since 3.4.0 |
|
626 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
* @return WP_Theme|false Parent theme, or false if the current theme is not a child theme. |
0 | 628 |
*/ |
629 |
public function parent() { |
|
630 |
return isset( $this->parent ) ? $this->parent : false; |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Adds theme data to cache. |
|
635 |
* |
|
636 |
* Cache entries keyed by the theme and the type of data. |
|
637 |
* |
|
638 |
* @since 3.4.0 |
|
639 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
* @param string $key Type of data to store (theme, screenshot, headers, post_templates) |
0 | 641 |
* @param string $data Data to store |
642 |
* @return bool Return value from wp_cache_add() |
|
643 |
*/ |
|
644 |
private function cache_add( $key, $data ) { |
|
645 |
return wp_cache_add( $key . '-' . $this->cache_hash, $data, 'themes', self::$cache_expiration ); |
|
646 |
} |
|
647 |
||
648 |
/** |
|
649 |
* Gets theme data from cache. |
|
650 |
* |
|
651 |
* Cache entries are keyed by the theme and the type of data. |
|
652 |
* |
|
653 |
* @since 3.4.0 |
|
654 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* @param string $key Type of data to retrieve (theme, screenshot, headers, post_templates) |
0 | 656 |
* @return mixed Retrieved data |
657 |
*/ |
|
658 |
private function cache_get( $key ) { |
|
659 |
return wp_cache_get( $key . '-' . $this->cache_hash, 'themes' ); |
|
660 |
} |
|
661 |
||
662 |
/** |
|
663 |
* Clears the cache for the theme. |
|
664 |
* |
|
665 |
* @since 3.4.0 |
|
666 |
*/ |
|
667 |
public function cache_delete() { |
|
9 | 668 |
foreach ( array( 'theme', 'screenshot', 'headers', 'post_templates' ) as $key ) { |
0 | 669 |
wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' ); |
9 | 670 |
} |
0 | 671 |
$this->template = $this->textdomain_loaded = $this->theme_root_uri = $this->parent = $this->errors = $this->headers_sanitized = $this->name_translated = null; |
9 | 672 |
$this->headers = array(); |
0 | 673 |
$this->__construct( $this->stylesheet, $this->theme_root ); |
674 |
} |
|
675 |
||
676 |
/** |
|
677 |
* Get a raw, unformatted theme header. |
|
678 |
* |
|
679 |
* The header is sanitized, but is not translated, and is not marked up for display. |
|
680 |
* To get a theme header for display, use the display() method. |
|
681 |
* |
|
682 |
* Use the get_template() method, not the 'Template' header, for finding the template. |
|
683 |
* The 'Template' header is only good for what was written in the style.css, while |
|
684 |
* get_template() takes into account where WordPress actually located the theme and |
|
685 |
* whether it is actually valid. |
|
686 |
* |
|
687 |
* @since 3.4.0 |
|
688 |
* |
|
689 |
* @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
* @return string|false String on success, false on failure. |
0 | 691 |
*/ |
692 |
public function get( $header ) { |
|
9 | 693 |
if ( ! isset( $this->headers[ $header ] ) ) { |
0 | 694 |
return false; |
9 | 695 |
} |
0 | 696 |
|
697 |
if ( ! isset( $this->headers_sanitized ) ) { |
|
698 |
$this->headers_sanitized = $this->cache_get( 'headers' ); |
|
9 | 699 |
if ( ! is_array( $this->headers_sanitized ) ) { |
0 | 700 |
$this->headers_sanitized = array(); |
9 | 701 |
} |
0 | 702 |
} |
703 |
||
9 | 704 |
if ( isset( $this->headers_sanitized[ $header ] ) ) { |
0 | 705 |
return $this->headers_sanitized[ $header ]; |
9 | 706 |
} |
0 | 707 |
|
708 |
// If themes are a persistent group, sanitize everything and cache it. One cache add is better than many cache sets. |
|
709 |
if ( self::$persistently_cache ) { |
|
9 | 710 |
foreach ( array_keys( $this->headers ) as $_header ) { |
0 | 711 |
$this->headers_sanitized[ $_header ] = $this->sanitize_header( $_header, $this->headers[ $_header ] ); |
9 | 712 |
} |
0 | 713 |
$this->cache_add( 'headers', $this->headers_sanitized ); |
714 |
} else { |
|
715 |
$this->headers_sanitized[ $header ] = $this->sanitize_header( $header, $this->headers[ $header ] ); |
|
716 |
} |
|
717 |
||
718 |
return $this->headers_sanitized[ $header ]; |
|
719 |
} |
|
720 |
||
721 |
/** |
|
722 |
* Gets a theme header, formatted and translated for display. |
|
723 |
* |
|
724 |
* @since 3.4.0 |
|
725 |
* |
|
726 |
* @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
|
727 |
* @param bool $markup Optional. Whether to mark up the header. Defaults to true. |
|
728 |
* @param bool $translate Optional. Whether to translate the header. Defaults to true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
* @return string|false Processed header, false on failure. |
0 | 730 |
*/ |
731 |
public function display( $header, $markup = true, $translate = true ) { |
|
732 |
$value = $this->get( $header ); |
|
5 | 733 |
if ( false === $value ) { |
734 |
return false; |
|
735 |
} |
|
0 | 736 |
|
9 | 737 |
if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) ) { |
0 | 738 |
$translate = false; |
9 | 739 |
} |
0 | 740 |
|
9 | 741 |
if ( $translate ) { |
0 | 742 |
$value = $this->translate_header( $header, $value ); |
9 | 743 |
} |
0 | 744 |
|
9 | 745 |
if ( $markup ) { |
0 | 746 |
$value = $this->markup_header( $header, $value, $translate ); |
9 | 747 |
} |
0 | 748 |
|
749 |
return $value; |
|
750 |
} |
|
751 |
||
752 |
/** |
|
753 |
* Sanitize a theme header. |
|
754 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
756 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
* @staticvar array $header_tags |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
* @staticvar array $header_tags_with_a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
759 |
* |
0 | 760 |
* @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
761 |
* @param string $value Value to sanitize. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
762 |
* @return mixed |
0 | 763 |
*/ |
764 |
private function sanitize_header( $header, $value ) { |
|
765 |
switch ( $header ) { |
|
9 | 766 |
case 'Status': |
0 | 767 |
if ( ! $value ) { |
768 |
$value = 'publish'; |
|
769 |
break; |
|
770 |
} |
|
771 |
// Fall through otherwise. |
|
9 | 772 |
case 'Name': |
0 | 773 |
static $header_tags = array( |
774 |
'abbr' => array( 'title' => true ), |
|
775 |
'acronym' => array( 'title' => true ), |
|
776 |
'code' => true, |
|
777 |
'em' => true, |
|
778 |
'strong' => true, |
|
779 |
); |
|
9 | 780 |
$value = wp_kses( $value, $header_tags ); |
0 | 781 |
break; |
9 | 782 |
case 'Author': |
0 | 783 |
// There shouldn't be anchor tags in Author, but some themes like to be challenging. |
9 | 784 |
case 'Description': |
0 | 785 |
static $header_tags_with_a = array( |
9 | 786 |
'a' => array( |
787 |
'href' => true, |
|
788 |
'title' => true, |
|
789 |
), |
|
0 | 790 |
'abbr' => array( 'title' => true ), |
791 |
'acronym' => array( 'title' => true ), |
|
792 |
'code' => true, |
|
793 |
'em' => true, |
|
794 |
'strong' => true, |
|
795 |
); |
|
9 | 796 |
$value = wp_kses( $value, $header_tags_with_a ); |
0 | 797 |
break; |
9 | 798 |
case 'ThemeURI': |
799 |
case 'AuthorURI': |
|
0 | 800 |
$value = esc_url_raw( $value ); |
801 |
break; |
|
9 | 802 |
case 'Tags': |
0 | 803 |
$value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) ); |
804 |
break; |
|
9 | 805 |
case 'Version': |
5 | 806 |
$value = strip_tags( $value ); |
807 |
break; |
|
0 | 808 |
} |
809 |
||
810 |
return $value; |
|
811 |
} |
|
812 |
||
813 |
/** |
|
814 |
* Mark up a theme header. |
|
815 |
* |
|
9 | 816 |
* @since 3.4.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
* @staticvar string $comma |
0 | 819 |
* |
820 |
* @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
|
821 |
* @param string $value Value to mark up. |
|
822 |
* @param string $translate Whether the header has been translated. |
|
823 |
* @return string Value, marked up. |
|
824 |
*/ |
|
825 |
private function markup_header( $header, $value, $translate ) { |
|
826 |
switch ( $header ) { |
|
9 | 827 |
case 'Name': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
if ( empty( $value ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
$value = esc_html( $this->get_stylesheet() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
} |
0 | 831 |
break; |
9 | 832 |
case 'Description': |
0 | 833 |
$value = wptexturize( $value ); |
834 |
break; |
|
9 | 835 |
case 'Author': |
836 |
if ( $this->get( 'AuthorURI' ) ) { |
|
5 | 837 |
$value = sprintf( '<a href="%1$s">%2$s</a>', $this->display( 'AuthorURI', true, $translate ), $value ); |
0 | 838 |
} elseif ( ! $value ) { |
839 |
$value = __( 'Anonymous' ); |
|
840 |
} |
|
841 |
break; |
|
9 | 842 |
case 'Tags': |
0 | 843 |
static $comma = null; |
844 |
if ( ! isset( $comma ) ) { |
|
845 |
/* translators: used between list items, there is a space after the comma */ |
|
846 |
$comma = __( ', ' ); |
|
847 |
} |
|
848 |
$value = implode( $comma, $value ); |
|
849 |
break; |
|
9 | 850 |
case 'ThemeURI': |
851 |
case 'AuthorURI': |
|
0 | 852 |
$value = esc_url( $value ); |
853 |
break; |
|
854 |
} |
|
855 |
||
856 |
return $value; |
|
857 |
} |
|
858 |
||
859 |
/** |
|
860 |
* Translate a theme header. |
|
861 |
* |
|
862 |
* @since 3.4.0 |
|
863 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
* @staticvar array $tags_list |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
865 |
* |
0 | 866 |
* @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags. |
867 |
* @param string $value Value to translate. |
|
868 |
* @return string Translated value. |
|
869 |
*/ |
|
870 |
private function translate_header( $header, $value ) { |
|
871 |
switch ( $header ) { |
|
9 | 872 |
case 'Name': |
0 | 873 |
// Cached for sorting reasons. |
9 | 874 |
if ( isset( $this->name_translated ) ) { |
0 | 875 |
return $this->name_translated; |
9 | 876 |
} |
877 |
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain |
|
878 |
$this->name_translated = translate( $value, $this->get( 'TextDomain' ) ); |
|
0 | 879 |
return $this->name_translated; |
9 | 880 |
case 'Tags': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
if ( empty( $value ) || ! function_exists( 'get_theme_feature_list' ) ) { |
0 | 882 |
return $value; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
883 |
} |
0 | 884 |
|
885 |
static $tags_list; |
|
886 |
if ( ! isset( $tags_list ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
887 |
$tags_list = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
888 |
// As of 4.6, deprecated tags which are only used to provide translation for older themes. |
9 | 889 |
'black' => __( 'Black' ), |
890 |
'blue' => __( 'Blue' ), |
|
891 |
'brown' => __( 'Brown' ), |
|
892 |
'gray' => __( 'Gray' ), |
|
893 |
'green' => __( 'Green' ), |
|
894 |
'orange' => __( 'Orange' ), |
|
895 |
'pink' => __( 'Pink' ), |
|
896 |
'purple' => __( 'Purple' ), |
|
897 |
'red' => __( 'Red' ), |
|
898 |
'silver' => __( 'Silver' ), |
|
899 |
'tan' => __( 'Tan' ), |
|
900 |
'white' => __( 'White' ), |
|
901 |
'yellow' => __( 'Yellow' ), |
|
902 |
'dark' => __( 'Dark' ), |
|
903 |
'light' => __( 'Light' ), |
|
904 |
'fixed-layout' => __( 'Fixed Layout' ), |
|
905 |
'fluid-layout' => __( 'Fluid Layout' ), |
|
906 |
'responsive-layout' => __( 'Responsive Layout' ), |
|
907 |
'blavatar' => __( 'Blavatar' ), |
|
908 |
'photoblogging' => __( 'Photoblogging' ), |
|
909 |
'seasonal' => __( 'Seasonal' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
|
0 | 912 |
$feature_list = get_theme_feature_list( false ); // No API |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
913 |
foreach ( $feature_list as $tags ) { |
0 | 914 |
$tags_list += $tags; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
915 |
} |
0 | 916 |
} |
917 |
||
918 |
foreach ( $value as &$tag ) { |
|
5 | 919 |
if ( isset( $tags_list[ $tag ] ) ) { |
0 | 920 |
$tag = $tags_list[ $tag ]; |
5 | 921 |
} elseif ( isset( self::$tag_map[ $tag ] ) ) { |
922 |
$tag = $tags_list[ self::$tag_map[ $tag ] ]; |
|
923 |
} |
|
0 | 924 |
} |
925 |
||
926 |
return $value; |
|
5 | 927 |
|
9 | 928 |
default: |
929 |
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain |
|
930 |
$value = translate( $value, $this->get( 'TextDomain' ) ); |
|
0 | 931 |
} |
932 |
return $value; |
|
933 |
} |
|
934 |
||
935 |
/** |
|
936 |
* The directory name of the theme's "stylesheet" files, inside the theme root. |
|
937 |
* |
|
938 |
* In the case of a child theme, this is directory name of the child theme. |
|
939 |
* Otherwise, get_stylesheet() is the same as get_template(). |
|
940 |
* |
|
941 |
* @since 3.4.0 |
|
942 |
* |
|
943 |
* @return string Stylesheet |
|
944 |
*/ |
|
945 |
public function get_stylesheet() { |
|
946 |
return $this->stylesheet; |
|
947 |
} |
|
948 |
||
949 |
/** |
|
950 |
* The directory name of the theme's "template" files, inside the theme root. |
|
951 |
* |
|
952 |
* In the case of a child theme, this is the directory name of the parent theme. |
|
953 |
* Otherwise, the get_template() is the same as get_stylesheet(). |
|
954 |
* |
|
955 |
* @since 3.4.0 |
|
956 |
* |
|
957 |
* @return string Template |
|
958 |
*/ |
|
959 |
public function get_template() { |
|
960 |
return $this->template; |
|
961 |
} |
|
962 |
||
963 |
/** |
|
964 |
* Returns the absolute path to the directory of a theme's "stylesheet" files. |
|
965 |
* |
|
966 |
* In the case of a child theme, this is the absolute path to the directory |
|
967 |
* of the child theme's files. |
|
968 |
* |
|
969 |
* @since 3.4.0 |
|
970 |
* |
|
971 |
* @return string Absolute path of the stylesheet directory. |
|
972 |
*/ |
|
973 |
public function get_stylesheet_directory() { |
|
9 | 974 |
if ( $this->errors() && in_array( 'theme_root_missing', $this->errors()->get_error_codes() ) ) { |
0 | 975 |
return ''; |
9 | 976 |
} |
0 | 977 |
|
978 |
return $this->theme_root . '/' . $this->stylesheet; |
|
979 |
} |
|
980 |
||
981 |
/** |
|
982 |
* Returns the absolute path to the directory of a theme's "template" files. |
|
983 |
* |
|
984 |
* In the case of a child theme, this is the absolute path to the directory |
|
985 |
* of the parent theme's files. |
|
986 |
* |
|
987 |
* @since 3.4.0 |
|
988 |
* |
|
989 |
* @return string Absolute path of the template directory. |
|
990 |
*/ |
|
991 |
public function get_template_directory() { |
|
9 | 992 |
if ( $this->parent() ) { |
0 | 993 |
$theme_root = $this->parent()->theme_root; |
9 | 994 |
} else { |
0 | 995 |
$theme_root = $this->theme_root; |
9 | 996 |
} |
0 | 997 |
|
998 |
return $theme_root . '/' . $this->template; |
|
999 |
} |
|
1000 |
||
1001 |
/** |
|
1002 |
* Returns the URL to the directory of a theme's "stylesheet" files. |
|
1003 |
* |
|
1004 |
* In the case of a child theme, this is the URL to the directory of the |
|
1005 |
* child theme's files. |
|
1006 |
* |
|
1007 |
* @since 3.4.0 |
|
1008 |
* |
|
1009 |
* @return string URL to the stylesheet directory. |
|
1010 |
*/ |
|
1011 |
public function get_stylesheet_directory_uri() { |
|
1012 |
return $this->get_theme_root_uri() . '/' . str_replace( '%2F', '/', rawurlencode( $this->stylesheet ) ); |
|
1013 |
} |
|
1014 |
||
1015 |
/** |
|
1016 |
* Returns the URL to the directory of a theme's "template" files. |
|
1017 |
* |
|
1018 |
* In the case of a child theme, this is the URL to the directory of the |
|
1019 |
* parent theme's files. |
|
1020 |
* |
|
1021 |
* @since 3.4.0 |
|
1022 |
* |
|
1023 |
* @return string URL to the template directory. |
|
1024 |
*/ |
|
1025 |
public function get_template_directory_uri() { |
|
9 | 1026 |
if ( $this->parent() ) { |
0 | 1027 |
$theme_root_uri = $this->parent()->get_theme_root_uri(); |
9 | 1028 |
} else { |
0 | 1029 |
$theme_root_uri = $this->get_theme_root_uri(); |
9 | 1030 |
} |
0 | 1031 |
|
1032 |
return $theme_root_uri . '/' . str_replace( '%2F', '/', rawurlencode( $this->template ) ); |
|
1033 |
} |
|
1034 |
||
1035 |
/** |
|
1036 |
* The absolute path to the directory of the theme root. |
|
1037 |
* |
|
1038 |
* This is typically the absolute path to wp-content/themes. |
|
1039 |
* |
|
1040 |
* @since 3.4.0 |
|
1041 |
* |
|
1042 |
* @return string Theme root. |
|
1043 |
*/ |
|
1044 |
public function get_theme_root() { |
|
1045 |
return $this->theme_root; |
|
1046 |
} |
|
1047 |
||
1048 |
/** |
|
1049 |
* Returns the URL to the directory of the theme root. |
|
1050 |
* |
|
1051 |
* This is typically the absolute URL to wp-content/themes. This forms the basis |
|
1052 |
* for all other URLs returned by WP_Theme, so we pass it to the public function |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
* get_theme_root_uri() and allow it to run the {@see 'theme_root_uri'} filter. |
0 | 1054 |
* |
1055 |
* @since 3.4.0 |
|
1056 |
* |
|
1057 |
* @return string Theme root URI. |
|
1058 |
*/ |
|
1059 |
public function get_theme_root_uri() { |
|
9 | 1060 |
if ( ! isset( $this->theme_root_uri ) ) { |
0 | 1061 |
$this->theme_root_uri = get_theme_root_uri( $this->stylesheet, $this->theme_root ); |
9 | 1062 |
} |
0 | 1063 |
return $this->theme_root_uri; |
1064 |
} |
|
1065 |
||
1066 |
/** |
|
1067 |
* Returns the main screenshot file for the theme. |
|
1068 |
* |
|
1069 |
* The main screenshot is called screenshot.png. gif and jpg extensions are also allowed. |
|
1070 |
* |
|
1071 |
* Screenshots for a theme must be in the stylesheet directory. (In the case of child |
|
1072 |
* themes, parent theme screenshots are not inherited.) |
|
1073 |
* |
|
1074 |
* @since 3.4.0 |
|
1075 |
* |
|
1076 |
* @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
* @return string|false Screenshot file. False if the theme does not have a screenshot. |
0 | 1078 |
*/ |
1079 |
public function get_screenshot( $uri = 'uri' ) { |
|
1080 |
$screenshot = $this->cache_get( 'screenshot' ); |
|
1081 |
if ( $screenshot ) { |
|
9 | 1082 |
if ( 'relative' == $uri ) { |
0 | 1083 |
return $screenshot; |
9 | 1084 |
} |
0 | 1085 |
return $this->get_stylesheet_directory_uri() . '/' . $screenshot; |
1086 |
} elseif ( 0 === $screenshot ) { |
|
1087 |
return false; |
|
1088 |
} |
|
1089 |
||
1090 |
foreach ( array( 'png', 'gif', 'jpg', 'jpeg' ) as $ext ) { |
|
1091 |
if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) { |
|
1092 |
$this->cache_add( 'screenshot', 'screenshot.' . $ext ); |
|
9 | 1093 |
if ( 'relative' == $uri ) { |
0 | 1094 |
return 'screenshot.' . $ext; |
9 | 1095 |
} |
0 | 1096 |
return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext; |
1097 |
} |
|
1098 |
} |
|
1099 |
||
1100 |
$this->cache_add( 'screenshot', 0 ); |
|
1101 |
return false; |
|
1102 |
} |
|
1103 |
||
1104 |
/** |
|
1105 |
* Return files in the theme's directory. |
|
1106 |
* |
|
1107 |
* @since 3.4.0 |
|
1108 |
* |
|
1109 |
* @param mixed $type Optional. Array of extensions to return. Defaults to all files (null). |
|
1110 |
* @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite. |
|
1111 |
* @param bool $search_parent Optional. Whether to return parent files. Defaults to false. |
|
1112 |
* @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1113 |
* being absolute paths. |
0 | 1114 |
*/ |
1115 |
public function get_files( $type = null, $depth = 0, $search_parent = false ) { |
|
1116 |
$files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth ); |
|
1117 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
if ( $search_parent && $this->parent() ) { |
0 | 1119 |
$files += (array) self::scandir( $this->get_template_directory(), $type, $depth ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
} |
0 | 1121 |
|
1122 |
return $files; |
|
1123 |
} |
|
1124 |
||
1125 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
* Returns the theme's post templates. |
0 | 1127 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
* @since 4.7.0 |
0 | 1129 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1130 |
* @return array Array of page templates, keyed by filename and post type, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
* with the value of the translated header name. |
0 | 1132 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
public function get_post_templates() { |
0 | 1134 |
// If you screw up your current theme and we invalidate your parent, most things still work. Let it slide. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) { |
0 | 1136 |
return array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
} |
0 | 1138 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
$post_templates = $this->cache_get( 'post_templates' ); |
0 | 1140 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1141 |
if ( ! is_array( $post_templates ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
$post_templates = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
|
9 | 1144 |
$files = (array) $this->get_files( 'php', 1, true ); |
0 | 1145 |
|
1146 |
foreach ( $files as $file => $full_path ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) { |
0 | 1148 |
continue; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1149 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1151 |
$types = array( 'page' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1152 |
if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1153 |
$types = explode( ',', _cleanup_header_comment( $type[1] ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
foreach ( $types as $type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
$type = sanitize_key( $type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
if ( ! isset( $post_templates[ $type ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
$post_templates[ $type ] = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1160 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1161 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1162 |
$post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1163 |
} |
0 | 1164 |
} |
1165 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
$this->cache_add( 'post_templates', $post_templates ); |
0 | 1167 |
} |
1168 |
||
1169 |
if ( $this->load_textdomain() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1170 |
foreach ( $post_templates as &$post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1171 |
foreach ( $post_type as &$post_template ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1172 |
$post_template = $this->translate_header( 'Template Name', $post_template ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
} |
0 | 1174 |
} |
1175 |
} |
|
1176 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
return $post_templates; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
* Returns the theme's post templates for a given post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
* @since 4.7.0 Added the `$post_type` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1186 |
* @param WP_Post|null $post Optional. The post being edited, provided for context. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1187 |
* @param string $post_type Optional. Post type to get the templates for. Default 'page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
* If a post is provided, its post type is used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
* @return array Array of page templates, keyed by filename, with the value of the translated header name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1191 |
public function get_page_templates( $post = null, $post_type = 'page' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
if ( $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1193 |
$post_type = get_post_type( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1195 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
$post_templates = $this->get_post_templates(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
$post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array(); |
0 | 1198 |
|
5 | 1199 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
* Filters list of page templates for a theme. |
5 | 1201 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
* @since 4.9.6 |
5 | 1203 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
* @param string[] $post_templates Array of page templates. Keys are filenames, |
5 | 1205 |
* values are translated names. |
1206 |
* @param WP_Theme $this The theme object. |
|
1207 |
* @param WP_Post|null $post The post being edited, provided for context, or null. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
* @param string $post_type Post type to get the templates for. |
5 | 1209 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
$post_templates = (array) apply_filters( 'theme_templates', $post_templates, $this, $post, $post_type ); |
5 | 1211 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1213 |
* Filters list of page templates for a theme. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
* The dynamic portion of the hook name, `$post_type`, refers to the post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
* @since 4.4.0 Converted to allow complete control over the `$page_templates` array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
* @since 4.7.0 Added the `$post_type` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
* |
9 | 1221 |
* @param string[] $post_templates Array of page templates. Keys are filenames, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
* values are translated names. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
* @param WP_Theme $this The theme object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1224 |
* @param WP_Post|null $post The post being edited, provided for context, or null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
* @param string $post_type Post type to get the templates for. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
$post_templates = (array) apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
return $post_templates; |
0 | 1230 |
} |
1231 |
||
1232 |
/** |
|
1233 |
* Scans a directory for files of a certain extension. |
|
1234 |
* |
|
1235 |
* @since 3.4.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1236 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
* @param string $path Absolute path to search. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
* @param array|string|null $extensions Optional. Array of extensions to find, string of a single extension, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
* or null for all extensions. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
* @param int $depth Optional. How many levels deep to search for files. Accepts 0, 1+, or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1241 |
* -1 (infinite depth). Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1242 |
* @param string $relative_path Optional. The basename of the absolute path. Used to control the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
* returned path for the found files, particularly when this function |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1244 |
* recurses to lower depths. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
* @return array|false Array of files, keyed by the path to the file relative to the `$path` directory prepended |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
* with `$relative_path`, with the values being absolute paths. False otherwise. |
0 | 1247 |
*/ |
1248 |
private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1249 |
if ( ! is_dir( $path ) ) { |
0 | 1250 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
} |
0 | 1252 |
|
1253 |
if ( $extensions ) { |
|
9 | 1254 |
$extensions = (array) $extensions; |
0 | 1255 |
$_extensions = implode( '|', $extensions ); |
1256 |
} |
|
1257 |
||
1258 |
$relative_path = trailingslashit( $relative_path ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1259 |
if ( '/' == $relative_path ) { |
0 | 1260 |
$relative_path = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1261 |
} |
0 | 1262 |
|
1263 |
$results = scandir( $path ); |
|
9 | 1264 |
$files = array(); |
0 | 1265 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1266 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
* Filters the array of excluded directories and files while scanning theme folder. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1269 |
* @since 4.7.4 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1270 |
* |
9 | 1271 |
* @param string[] $exclusions Array of excluded directories and files. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
$exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
|
0 | 1275 |
foreach ( $results as $result ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
if ( '.' == $result[0] || in_array( $result, $exclusions, true ) ) { |
0 | 1277 |
continue; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
} |
0 | 1279 |
if ( is_dir( $path . '/' . $result ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
if ( ! $depth ) { |
0 | 1281 |
continue; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1282 |
} |
9 | 1283 |
$found = self::scandir( $path . '/' . $result, $extensions, $depth - 1, $relative_path . $result ); |
0 | 1284 |
$files = array_merge_recursive( $files, $found ); |
1285 |
} elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) { |
|
1286 |
$files[ $relative_path . $result ] = $path . '/' . $result; |
|
1287 |
} |
|
1288 |
} |
|
1289 |
||
1290 |
return $files; |
|
1291 |
} |
|
1292 |
||
1293 |
/** |
|
1294 |
* Loads the theme's textdomain. |
|
1295 |
* |
|
1296 |
* Translation files are not inherited from the parent theme. Todo: if this fails for the |
|
1297 |
* child theme, it should probably try to load the parent theme's translations. |
|
1298 |
* |
|
1299 |
* @since 3.4.0 |
|
1300 |
* |
|
5 | 1301 |
* @return bool True if the textdomain was successfully loaded or has already been loaded. |
9 | 1302 |
* False if no textdomain was specified in the file headers, or if the domain could not be loaded. |
0 | 1303 |
*/ |
1304 |
public function load_textdomain() { |
|
9 | 1305 |
if ( isset( $this->textdomain_loaded ) ) { |
0 | 1306 |
return $this->textdomain_loaded; |
9 | 1307 |
} |
0 | 1308 |
|
9 | 1309 |
$textdomain = $this->get( 'TextDomain' ); |
0 | 1310 |
if ( ! $textdomain ) { |
1311 |
$this->textdomain_loaded = false; |
|
1312 |
return false; |
|
1313 |
} |
|
1314 |
||
1315 |
if ( is_textdomain_loaded( $textdomain ) ) { |
|
1316 |
$this->textdomain_loaded = true; |
|
1317 |
return true; |
|
1318 |
} |
|
1319 |
||
1320 |
$path = $this->get_stylesheet_directory(); |
|
9 | 1321 |
if ( $domainpath = $this->get( 'DomainPath' ) ) { |
0 | 1322 |
$path .= $domainpath; |
9 | 1323 |
} else { |
0 | 1324 |
$path .= '/languages'; |
9 | 1325 |
} |
0 | 1326 |
|
1327 |
$this->textdomain_loaded = load_theme_textdomain( $textdomain, $path ); |
|
1328 |
return $this->textdomain_loaded; |
|
1329 |
} |
|
1330 |
||
1331 |
/** |
|
1332 |
* Whether the theme is allowed (multisite only). |
|
1333 |
* |
|
1334 |
* @since 3.4.0 |
|
1335 |
* |
|
1336 |
* @param string $check Optional. Whether to check only the 'network'-wide settings, the 'site' |
|
9 | 1337 |
* settings, or 'both'. Defaults to 'both'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
* @param int $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current site. |
0 | 1339 |
* @return bool Whether the theme is allowed for the network. Returns true in single-site. |
1340 |
*/ |
|
1341 |
public function is_allowed( $check = 'both', $blog_id = null ) { |
|
9 | 1342 |
if ( ! is_multisite() ) { |
0 | 1343 |
return true; |
9 | 1344 |
} |
0 | 1345 |
|
1346 |
if ( 'both' == $check || 'network' == $check ) { |
|
1347 |
$allowed = self::get_allowed_on_network(); |
|
9 | 1348 |
if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) { |
0 | 1349 |
return true; |
9 | 1350 |
} |
0 | 1351 |
} |
1352 |
||
1353 |
if ( 'both' == $check || 'site' == $check ) { |
|
1354 |
$allowed = self::get_allowed_on_site( $blog_id ); |
|
9 | 1355 |
if ( ! empty( $allowed[ $this->get_stylesheet() ] ) ) { |
0 | 1356 |
return true; |
9 | 1357 |
} |
0 | 1358 |
} |
1359 |
||
1360 |
return false; |
|
1361 |
} |
|
1362 |
||
1363 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
* Determines the latest WordPress default theme that is installed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
* This hits the filesystem. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
* @return WP_Theme|false Object, or false if no theme is installed, which would be bad. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1372 |
public static function get_core_default_theme() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1373 |
foreach ( array_reverse( self::$default_themes ) as $slug => $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1374 |
$theme = wp_get_theme( $slug ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
if ( $theme->exists() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1376 |
return $theme; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
/** |
0 | 1383 |
* Returns array of stylesheet names of themes allowed on the site or network. |
1384 |
* |
|
1385 |
* @since 3.4.0 |
|
1386 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
* @param int $blog_id Optional. ID of the site. Defaults to the current site. |
9 | 1388 |
* @return string[] Array of stylesheet names. |
0 | 1389 |
*/ |
1390 |
public static function get_allowed( $blog_id = null ) { |
|
5 | 1391 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
* Filters the array of themes allowed on the network. |
5 | 1393 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
* Site is provided as context so that a list of network allowed themes can |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
* be filtered further. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
* @since 4.5.0 |
5 | 1398 |
* |
9 | 1399 |
* @param string[] $allowed_themes An array of theme stylesheet names. |
1400 |
* @param int $blog_id ID of the site. |
|
5 | 1401 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
$network = (array) apply_filters( 'network_allowed_themes', self::get_allowed_on_network(), $blog_id ); |
0 | 1403 |
return $network + self::get_allowed_on_site( $blog_id ); |
1404 |
} |
|
1405 |
||
1406 |
/** |
|
1407 |
* Returns array of stylesheet names of themes allowed on the network. |
|
1408 |
* |
|
1409 |
* @since 3.4.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* @staticvar array $allowed_themes |
0 | 1412 |
* |
9 | 1413 |
* @return string[] Array of stylesheet names. |
0 | 1414 |
*/ |
1415 |
public static function get_allowed_on_network() { |
|
1416 |
static $allowed_themes; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
if ( ! isset( $allowed_themes ) ) { |
0 | 1418 |
$allowed_themes = (array) get_site_option( 'allowedthemes' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
* Filters the array of themes allowed on the network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
* @since MU (3.0.0) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
* |
9 | 1426 |
* @param string[] $allowed_themes An array of theme stylesheet names. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
$allowed_themes = apply_filters( 'allowed_themes', $allowed_themes ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
|
0 | 1430 |
return $allowed_themes; |
1431 |
} |
|
1432 |
||
1433 |
/** |
|
1434 |
* Returns array of stylesheet names of themes allowed on the site. |
|
1435 |
* |
|
1436 |
* @since 3.4.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
* @staticvar array $allowed_themes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1439 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1440 |
* @param int $blog_id Optional. ID of the site. Defaults to the current site. |
9 | 1441 |
* @return string[] Array of stylesheet names. |
0 | 1442 |
*/ |
1443 |
public static function get_allowed_on_site( $blog_id = null ) { |
|
1444 |
static $allowed_themes = array(); |
|
1445 |
||
9 | 1446 |
if ( ! $blog_id || ! is_multisite() ) { |
0 | 1447 |
$blog_id = get_current_blog_id(); |
9 | 1448 |
} |
0 | 1449 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1450 |
if ( isset( $allowed_themes[ $blog_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1451 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
* Filters the array of themes allowed on the site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1455 |
* |
9 | 1456 |
* @param string[] $allowed_themes An array of theme stylesheet names. |
1457 |
* @param int $blog_id ID of the site. Defaults to current site. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1460 |
} |
0 | 1461 |
|
1462 |
$current = $blog_id == get_current_blog_id(); |
|
1463 |
||
1464 |
if ( $current ) { |
|
1465 |
$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' ); |
|
1466 |
} else { |
|
1467 |
switch_to_blog( $blog_id ); |
|
1468 |
$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' ); |
|
1469 |
restore_current_blog(); |
|
1470 |
} |
|
1471 |
||
1472 |
// This is all super old MU back compat joy. |
|
1473 |
// 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name. |
|
1474 |
if ( false === $allowed_themes[ $blog_id ] ) { |
|
1475 |
if ( $current ) { |
|
1476 |
$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' ); |
|
1477 |
} else { |
|
1478 |
switch_to_blog( $blog_id ); |
|
1479 |
$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' ); |
|
1480 |
restore_current_blog(); |
|
1481 |
} |
|
1482 |
||
1483 |
if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) { |
|
1484 |
$allowed_themes[ $blog_id ] = array(); |
|
1485 |
} else { |
|
1486 |
$converted = array(); |
|
9 | 1487 |
$themes = wp_get_themes(); |
0 | 1488 |
foreach ( $themes as $stylesheet => $theme_data ) { |
9 | 1489 |
if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get( 'Name' ) ] ) ) { |
0 | 1490 |
$converted[ $stylesheet ] = true; |
9 | 1491 |
} |
0 | 1492 |
} |
1493 |
$allowed_themes[ $blog_id ] = $converted; |
|
1494 |
} |
|
1495 |
// Set the option so we never have to go through this pain again. |
|
1496 |
if ( is_admin() && $allowed_themes[ $blog_id ] ) { |
|
1497 |
if ( $current ) { |
|
1498 |
update_option( 'allowedthemes', $allowed_themes[ $blog_id ] ); |
|
1499 |
delete_option( 'allowed_themes' ); |
|
1500 |
} else { |
|
1501 |
switch_to_blog( $blog_id ); |
|
1502 |
update_option( 'allowedthemes', $allowed_themes[ $blog_id ] ); |
|
1503 |
delete_option( 'allowed_themes' ); |
|
1504 |
restore_current_blog(); |
|
1505 |
} |
|
1506 |
} |
|
1507 |
} |
|
1508 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1509 |
/** This filter is documented in wp-includes/class-wp-theme.php */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1510 |
return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1511 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1512 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1513 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1514 |
* Enables a theme for all sites on the current network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1515 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1516 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1517 |
* |
9 | 1518 |
* @param string|string[] $stylesheets Stylesheet name or array of stylesheet names. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1519 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1520 |
public static function network_enable_theme( $stylesheets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1521 |
if ( ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1522 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1523 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1524 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1525 |
if ( ! is_array( $stylesheets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1526 |
$stylesheets = array( $stylesheets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1527 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1528 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1529 |
$allowed_themes = get_site_option( 'allowedthemes' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1530 |
foreach ( $stylesheets as $stylesheet ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1531 |
$allowed_themes[ $stylesheet ] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1532 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1533 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1534 |
update_site_option( 'allowedthemes', $allowed_themes ); |
0 | 1535 |
} |
1536 |
||
1537 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
* Disables a theme for all sites on the current network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1539 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1541 |
* |
9 | 1542 |
* @param string|string[] $stylesheets Stylesheet name or array of stylesheet names. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1543 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1544 |
public static function network_disable_theme( $stylesheets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1545 |
if ( ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1546 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1547 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1548 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1549 |
if ( ! is_array( $stylesheets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1550 |
$stylesheets = array( $stylesheets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1551 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1552 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1553 |
$allowed_themes = get_site_option( 'allowedthemes' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1554 |
foreach ( $stylesheets as $stylesheet ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1555 |
if ( isset( $allowed_themes[ $stylesheet ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1556 |
unset( $allowed_themes[ $stylesheet ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1557 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1558 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1559 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1560 |
update_site_option( 'allowedthemes', $allowed_themes ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1562 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1563 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1564 |
* Sorts themes by name. |
0 | 1565 |
* |
1566 |
* @since 3.4.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1567 |
* |
9 | 1568 |
* @param WP_Theme[] $themes Array of theme objects to sort (passed by reference). |
0 | 1569 |
*/ |
1570 |
public static function sort_by_name( &$themes ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1571 |
if ( 0 === strpos( get_user_locale(), 'en_' ) ) { |
0 | 1572 |
uasort( $themes, array( 'WP_Theme', '_name_sort' ) ); |
1573 |
} else { |
|
9 | 1574 |
foreach ( $themes as $key => $theme ) { |
1575 |
$theme->translate_header( 'Name', $theme->headers['Name'] ); |
|
1576 |
} |
|
0 | 1577 |
uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) ); |
1578 |
} |
|
1579 |
} |
|
1580 |
||
1581 |
/** |
|
1582 |
* Callback function for usort() to naturally sort themes by name. |
|
1583 |
* |
|
1584 |
* Accesses the Name header directly from the class for maximum speed. |
|
1585 |
* Would choke on HTML but we don't care enough to slow it down with strip_tags(). |
|
1586 |
* |
|
1587 |
* @since 3.4.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1588 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1589 |
* @param string $a First name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1590 |
* @param string $b Second name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1591 |
* @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1592 |
* Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort(). |
0 | 1593 |
*/ |
1594 |
private static function _name_sort( $a, $b ) { |
|
1595 |
return strnatcasecmp( $a->headers['Name'], $b->headers['Name'] ); |
|
1596 |
} |
|
1597 |
||
1598 |
/** |
|
9 | 1599 |
* Callback function for usort() to naturally sort themes by translated name. |
0 | 1600 |
* |
1601 |
* @since 3.4.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1602 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1603 |
* @param string $a First name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1604 |
* @param string $b Second name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1605 |
* @return int Negative if `$a` falls lower in the natural order than `$b`. Zero if they fall equally. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1606 |
* Greater than 0 if `$a` falls higher in the natural order than `$b`. Used with usort(). |
0 | 1607 |
*/ |
1608 |
private static function _name_sort_i18n( $a, $b ) { |
|
9 | 1609 |
return strnatcasecmp( $a->name_translated, $b->name_translated ); |
0 | 1610 |
} |
1611 |
} |