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