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