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