[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Theme, template, and stylesheet functions. 4 * 5 * @package WordPress 6 * @subpackage Theme 7 */ 8 9 /** 10 * Returns an array of WP_Theme objects based on the arguments. 11 * 12 * Despite advances over get_themes(), this function is quite expensive, and grows 13 * linearly with additional themes. Stick to wp_get_theme() if possible. 14 * 15 * @since 3.4.0 16 * 17 * @global string[] $wp_theme_directories 18 * 19 * @param array $args { 20 * Optional. The search arguments. 21 * 22 * @type mixed $errors True to return themes with errors, false to return 23 * themes without errors, null to return all themes. 24 * Default false. 25 * @type mixed $allowed (Multisite) True to return only allowed themes for a site. 26 * False to return only disallowed themes for a site. 27 * 'site' to return only site-allowed themes. 28 * 'network' to return only network-allowed themes. 29 * Null to return all themes. Default null. 30 * @type int $blog_id (Multisite) The blog ID used to calculate which themes 31 * are allowed. Default 0, synonymous for the current blog. 32 * } 33 * @return WP_Theme[] Array of WP_Theme objects. 34 */ 35 function wp_get_themes( $args = array() ) { 36 global $wp_theme_directories; 37 38 $defaults = array( 39 'errors' => false, 40 'allowed' => null, 41 'blog_id' => 0, 42 ); 43 $args = wp_parse_args( $args, $defaults ); 44 45 $theme_directories = search_theme_directories(); 46 47 if ( is_array( $wp_theme_directories ) && count( $wp_theme_directories ) > 1 ) { 48 /* 49 * Make sure the active theme wins out, in case search_theme_directories() picks the wrong 50 * one in the case of a conflict. (Normally, last registered theme root wins.) 51 */ 52 $current_theme = get_stylesheet(); 53 if ( isset( $theme_directories[ $current_theme ] ) ) { 54 $root_of_current_theme = get_raw_theme_root( $current_theme ); 55 if ( ! in_array( $root_of_current_theme, $wp_theme_directories, true ) ) { 56 $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme; 57 } 58 $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme; 59 } 60 } 61 62 if ( empty( $theme_directories ) ) { 63 return array(); 64 } 65 66 if ( is_multisite() && null !== $args['allowed'] ) { 67 $allowed = $args['allowed']; 68 if ( 'network' === $allowed ) { 69 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() ); 70 } elseif ( 'site' === $allowed ) { 71 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) ); 72 } elseif ( $allowed ) { 73 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); 74 } else { 75 $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) ); 76 } 77 } 78 79 $themes = array(); 80 static $_themes = array(); 81 82 foreach ( $theme_directories as $theme => $theme_root ) { 83 if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) ) { 84 $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ]; 85 } else { 86 $themes[ $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] ); 87 88 $_themes[ $theme_root['theme_root'] . '/' . $theme ] = $themes[ $theme ]; 89 } 90 } 91 92 if ( null !== $args['errors'] ) { 93 foreach ( $themes as $theme => $wp_theme ) { 94 if ( (bool) $wp_theme->errors() !== $args['errors'] ) { 95 unset( $themes[ $theme ] ); 96 } 97 } 98 } 99 100 return $themes; 101 } 102 103 /** 104 * Gets a WP_Theme object for a theme. 105 * 106 * @since 3.4.0 107 * 108 * @global string[] $wp_theme_directories 109 * 110 * @param string $stylesheet Optional. Directory name for the theme. Defaults to active theme. 111 * @param string $theme_root Optional. Absolute path of the theme root to look in. 112 * If not specified, get_raw_theme_root() is used to calculate 113 * the theme root for the $stylesheet provided (or active theme). 114 * @return WP_Theme Theme object. Be sure to check the object's exists() method 115 * if you need to confirm the theme's existence. 116 */ 117 function wp_get_theme( $stylesheet = '', $theme_root = '' ) { 118 global $wp_theme_directories; 119 120 if ( empty( $stylesheet ) ) { 121 $stylesheet = get_stylesheet(); 122 } 123 124 if ( empty( $theme_root ) ) { 125 $theme_root = get_raw_theme_root( $stylesheet ); 126 if ( false === $theme_root ) { 127 $theme_root = WP_CONTENT_DIR . '/themes'; 128 } elseif ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) { 129 $theme_root = WP_CONTENT_DIR . $theme_root; 130 } 131 } 132 133 return new WP_Theme( $stylesheet, $theme_root ); 134 } 135 136 /** 137 * Clears the cache held by get_theme_roots() and WP_Theme. 138 * 139 * @since 3.5.0 140 * @param bool $clear_update_cache Whether to clear the theme updates cache. 141 */ 142 function wp_clean_themes_cache( $clear_update_cache = true ) { 143 if ( $clear_update_cache ) { 144 delete_site_transient( 'update_themes' ); 145 } 146 search_theme_directories( true ); 147 foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme ) { 148 $theme->cache_delete(); 149 } 150 } 151 152 /** 153 * Whether a child theme is in use. 154 * 155 * @since 3.0.0 156 * @since 6.5.0 Makes use of global template variables. 157 * 158 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory. 159 * @global string $wp_template_path Path to current theme's template directory. 160 * 161 * @return bool True if a child theme is in use, false otherwise. 162 */ 163 function is_child_theme() { 164 global $wp_stylesheet_path, $wp_template_path; 165 166 return $wp_stylesheet_path !== $wp_template_path; 167 } 168 169 /** 170 * Retrieves name of the current stylesheet. 171 * 172 * The theme name that is currently set as the front end theme. 173 * 174 * For all intents and purposes, the template name and the stylesheet name 175 * are going to be the same for most cases. 176 * 177 * @since 1.5.0 178 * 179 * @return string Stylesheet name. 180 */ 181 function get_stylesheet() { 182 /** 183 * Filters the name of current stylesheet. 184 * 185 * @since 1.5.0 186 * 187 * @param string $stylesheet Name of the current stylesheet. 188 */ 189 return apply_filters( 'stylesheet', get_option( 'stylesheet' ) ); 190 } 191 192 /** 193 * Retrieves stylesheet directory path for the active theme. 194 * 195 * @since 1.5.0 196 * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme. 197 * @since 6.4.2 Memoization removed. 198 * 199 * @return string Path to active theme's stylesheet directory. 200 */ 201 function get_stylesheet_directory() { 202 $stylesheet = get_stylesheet(); 203 $theme_root = get_theme_root( $stylesheet ); 204 $stylesheet_dir = "$theme_root/$stylesheet"; 205 206 /** 207 * Filters the stylesheet directory path for the active theme. 208 * 209 * @since 1.5.0 210 * 211 * @param string $stylesheet_dir Absolute path to the active theme. 212 * @param string $stylesheet Directory name of the active theme. 213 * @param string $theme_root Absolute path to themes directory. 214 */ 215 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root ); 216 } 217 218 /** 219 * Retrieves stylesheet directory URI for the active theme. 220 * 221 * @since 1.5.0 222 * 223 * @return string URI to active theme's stylesheet directory. 224 */ 225 function get_stylesheet_directory_uri() { 226 $stylesheet = str_replace( '%2F', '/', rawurlencode( get_stylesheet() ) ); 227 $theme_root_uri = get_theme_root_uri( $stylesheet ); 228 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet"; 229 230 /** 231 * Filters the stylesheet directory URI. 232 * 233 * @since 1.5.0 234 * 235 * @param string $stylesheet_dir_uri Stylesheet directory URI. 236 * @param string $stylesheet Name of the activated theme's directory. 237 * @param string $theme_root_uri Themes root URI. 238 */ 239 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri ); 240 } 241 242 /** 243 * Retrieves stylesheet URI for the active theme. 244 * 245 * The stylesheet file name is 'style.css' which is appended to the stylesheet directory URI path. 246 * See get_stylesheet_directory_uri(). 247 * 248 * @since 1.5.0 249 * 250 * @return string URI to active theme's stylesheet. 251 */ 252 function get_stylesheet_uri() { 253 $stylesheet_dir_uri = get_stylesheet_directory_uri(); 254 $stylesheet_uri = $stylesheet_dir_uri . '/style.css'; 255 /** 256 * Filters the URI of the active theme stylesheet. 257 * 258 * @since 1.5.0 259 * 260 * @param string $stylesheet_uri Stylesheet URI for the active theme/child theme. 261 * @param string $stylesheet_dir_uri Stylesheet directory URI for the active theme/child theme. 262 */ 263 return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri ); 264 } 265 266 /** 267 * Retrieves the localized stylesheet URI. 268 * 269 * The stylesheet directory for the localized stylesheet files are located, by 270 * default, in the base theme directory. The name of the locale file will be the 271 * locale followed by '.css'. If that does not exist, then the text direction 272 * stylesheet will be checked for existence, for example 'ltr.css'. 273 * 274 * The theme may change the location of the stylesheet directory by either using 275 * the {@see 'stylesheet_directory_uri'} or {@see 'locale_stylesheet_uri'} filters. 276 * 277 * If you want to change the location of the stylesheet files for the entire 278 * WordPress workflow, then change the former. If you just have the locale in a 279 * separate folder, then change the latter. 280 * 281 * @since 2.1.0 282 * 283 * @global WP_Locale $wp_locale WordPress date and time locale object. 284 * 285 * @return string URI to active theme's localized stylesheet. 286 */ 287 function get_locale_stylesheet_uri() { 288 global $wp_locale; 289 $stylesheet_dir_uri = get_stylesheet_directory_uri(); 290 $dir = get_stylesheet_directory(); 291 $locale = get_locale(); 292 if ( file_exists( "$dir/$locale.css" ) ) { 293 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css"; 294 } elseif ( ! empty( $wp_locale->text_direction ) && file_exists( "$dir/{$wp_locale->text_direction}.css" ) ) { 295 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css"; 296 } else { 297 $stylesheet_uri = ''; 298 } 299 /** 300 * Filters the localized stylesheet URI. 301 * 302 * @since 2.1.0 303 * 304 * @param string $stylesheet_uri Localized stylesheet URI. 305 * @param string $stylesheet_dir_uri Stylesheet directory URI. 306 */ 307 return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri ); 308 } 309 310 /** 311 * Retrieves name of the active theme. 312 * 313 * @since 1.5.0 314 * 315 * @return string Template name. 316 */ 317 function get_template() { 318 /** 319 * Filters the name of the active theme. 320 * 321 * @since 1.5.0 322 * 323 * @param string $template active theme's directory name. 324 */ 325 return apply_filters( 'template', get_option( 'template' ) ); 326 } 327 328 /** 329 * Retrieves template directory path for the active theme. 330 * 331 * @since 1.5.0 332 * @since 6.4.0 Memoizes filter execution so that it only runs once for the current theme. 333 * @since 6.4.1 Memoization removed. 334 * 335 * @return string Path to active theme's template directory. 336 */ 337 function get_template_directory() { 338 $template = get_template(); 339 $theme_root = get_theme_root( $template ); 340 $template_dir = "$theme_root/$template"; 341 342 /** 343 * Filters the active theme directory path. 344 * 345 * @since 1.5.0 346 * 347 * @param string $template_dir The path of the active theme directory. 348 * @param string $template Directory name of the active theme. 349 * @param string $theme_root Absolute path to the themes directory. 350 */ 351 return apply_filters( 'template_directory', $template_dir, $template, $theme_root ); 352 } 353 354 /** 355 * Retrieves template directory URI for the active theme. 356 * 357 * @since 1.5.0 358 * 359 * @return string URI to active theme's template directory. 360 */ 361 function get_template_directory_uri() { 362 $template = str_replace( '%2F', '/', rawurlencode( get_template() ) ); 363 $theme_root_uri = get_theme_root_uri( $template ); 364 $template_dir_uri = "$theme_root_uri/$template"; 365 366 /** 367 * Filters the active theme directory URI. 368 * 369 * @since 1.5.0 370 * 371 * @param string $template_dir_uri The URI of the active theme directory. 372 * @param string $template Directory name of the active theme. 373 * @param string $theme_root_uri The themes root URI. 374 */ 375 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri ); 376 } 377 378 /** 379 * Retrieves theme roots. 380 * 381 * @since 2.9.0 382 * 383 * @global string[] $wp_theme_directories 384 * 385 * @return array|string An array of theme roots keyed by template/stylesheet 386 * or a single theme root if all themes have the same root. 387 */ 388 function get_theme_roots() { 389 global $wp_theme_directories; 390 391 if ( ! is_array( $wp_theme_directories ) || count( $wp_theme_directories ) <= 1 ) { 392 return '/themes'; 393 } 394 395 $theme_roots = get_site_transient( 'theme_roots' ); 396 if ( false === $theme_roots ) { 397 search_theme_directories( true ); // Regenerate the transient. 398 $theme_roots = get_site_transient( 'theme_roots' ); 399 } 400 return $theme_roots; 401 } 402 403 /** 404 * Registers a directory that contains themes. 405 * 406 * @since 2.9.0 407 * 408 * @global string[] $wp_theme_directories 409 * 410 * @param string $directory Either the full filesystem path to a theme folder 411 * or a folder within WP_CONTENT_DIR. 412 * @return bool True if successfully registered a directory that contains themes, 413 * false if the directory does not exist. 414 */ 415 function register_theme_directory( $directory ) { 416 global $wp_theme_directories; 417 418 if ( ! file_exists( $directory ) ) { 419 // Try prepending as the theme directory could be relative to the content directory. 420 $directory = WP_CONTENT_DIR . '/' . $directory; 421 // If this directory does not exist, return and do not register. 422 if ( ! file_exists( $directory ) ) { 423 return false; 424 } 425 } 426 427 if ( ! is_array( $wp_theme_directories ) ) { 428 $wp_theme_directories = array(); 429 } 430 431 $untrailed = untrailingslashit( $directory ); 432 if ( ! empty( $untrailed ) && ! in_array( $untrailed, $wp_theme_directories, true ) ) { 433 $wp_theme_directories[] = $untrailed; 434 } 435 436 return true; 437 } 438 439 /** 440 * Searches all registered theme directories for complete and valid themes. 441 * 442 * @since 2.9.0 443 * 444 * @global string[] $wp_theme_directories 445 * 446 * @param bool $force Optional. Whether to force a new directory scan. Default false. 447 * @return array|false Valid themes found on success, false on failure. 448 */ 449 function search_theme_directories( $force = false ) { 450 global $wp_theme_directories; 451 static $found_themes = null; 452 453 if ( empty( $wp_theme_directories ) ) { 454 return false; 455 } 456 457 if ( ! $force && isset( $found_themes ) ) { 458 return $found_themes; 459 } 460 461 $found_themes = array(); 462 463 $wp_theme_directories = (array) $wp_theme_directories; 464 $relative_theme_roots = array(); 465 466 /* 467 * Set up maybe-relative, maybe-absolute array of theme directories. 468 * We always want to return absolute, but we need to cache relative 469 * to use in get_theme_root(). 470 */ 471 foreach ( $wp_theme_directories as $theme_root ) { 472 if ( str_starts_with( $theme_root, WP_CONTENT_DIR ) ) { 473 $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root; 474 } else { 475 $relative_theme_roots[ $theme_root ] = $theme_root; 476 } 477 } 478 479 /** 480 * Filters whether to get the cache of the registered theme directories. 481 * 482 * @since 3.4.0 483 * 484 * @param bool $cache_expiration Whether to get the cache of the theme directories. Default false. 485 * @param string $context The class or function name calling the filter. 486 */ 487 $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ); 488 489 if ( $cache_expiration ) { 490 $cached_roots = get_site_transient( 'theme_roots' ); 491 if ( is_array( $cached_roots ) ) { 492 foreach ( $cached_roots as $theme_dir => $theme_root ) { 493 // A cached theme root is no longer around, so skip it. 494 if ( ! isset( $relative_theme_roots[ $theme_root ] ) ) { 495 continue; 496 } 497 $found_themes[ $theme_dir ] = array( 498 'theme_file' => $theme_dir . '/style.css', 499 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute. 500 ); 501 } 502 return $found_themes; 503 } 504 if ( ! is_int( $cache_expiration ) ) { 505 $cache_expiration = 30 * MINUTE_IN_SECONDS; 506 } 507 } else { 508 $cache_expiration = 30 * MINUTE_IN_SECONDS; 509 } 510 511 /* Loop the registered theme directories and extract all themes */ 512 foreach ( $wp_theme_directories as $theme_root ) { 513 514 // Start with directories in the root of the active theme directory. 515 $dirs = @ scandir( $theme_root ); 516 if ( ! $dirs ) { 517 wp_trigger_error( __FUNCTION__, "$theme_root is not readable" ); 518 continue; 519 } 520 foreach ( $dirs as $dir ) { 521 if ( ! is_dir( $theme_root . '/' . $dir ) || '.' === $dir[0] || 'CVS' === $dir ) { 522 continue; 523 } 524 if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) { 525 /* 526 * wp-content/themes/a-single-theme 527 * wp-content/themes is $theme_root, a-single-theme is $dir. 528 */ 529 $found_themes[ $dir ] = array( 530 'theme_file' => $dir . '/style.css', 531 'theme_root' => $theme_root, 532 ); 533 } else { 534 $found_theme = false; 535 /* 536 * wp-content/themes/a-folder-of-themes/* 537 * wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs. 538 */ 539 $sub_dirs = @ scandir( $theme_root . '/' . $dir ); 540 if ( ! $sub_dirs ) { 541 wp_trigger_error( __FUNCTION__, "$theme_root/$dir is not readable" ); 542 continue; 543 } 544 foreach ( $sub_dirs as $sub_dir ) { 545 if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || '.' === $dir[0] || 'CVS' === $dir ) { 546 continue; 547 } 548 if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) ) { 549 continue; 550 } 551 $found_themes[ $dir . '/' . $sub_dir ] = array( 552 'theme_file' => $dir . '/' . $sub_dir . '/style.css', 553 'theme_root' => $theme_root, 554 ); 555 $found_theme = true; 556 } 557 /* 558 * Never mind the above, it's just a theme missing a style.css. 559 * Return it; WP_Theme will catch the error. 560 */ 561 if ( ! $found_theme ) { 562 $found_themes[ $dir ] = array( 563 'theme_file' => $dir . '/style.css', 564 'theme_root' => $theme_root, 565 ); 566 } 567 } 568 } 569 } 570 571 asort( $found_themes ); 572 573 $theme_roots = array(); 574 $relative_theme_roots = array_flip( $relative_theme_roots ); 575 576 foreach ( $found_themes as $theme_dir => $theme_data ) { 577 $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative. 578 } 579 580 if ( get_site_transient( 'theme_roots' ) !== $theme_roots ) { 581 set_site_transient( 'theme_roots', $theme_roots, $cache_expiration ); 582 } 583 584 return $found_themes; 585 } 586 587 /** 588 * Retrieves path to themes directory. 589 * 590 * Does not have trailing slash. 591 * 592 * @since 1.5.0 593 * 594 * @global string[] $wp_theme_directories 595 * 596 * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme. 597 * Default is to leverage the main theme root. 598 * @return string Themes directory path. 599 */ 600 function get_theme_root( $stylesheet_or_template = '' ) { 601 global $wp_theme_directories; 602 603 $theme_root = ''; 604 605 if ( $stylesheet_or_template ) { 606 $theme_root = get_raw_theme_root( $stylesheet_or_template ); 607 if ( $theme_root ) { 608 /* 609 * Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory. 610 * This gives relative theme roots the benefit of the doubt when things go haywire. 611 */ 612 if ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) { 613 $theme_root = WP_CONTENT_DIR . $theme_root; 614 } 615 } 616 } 617 618 if ( ! $theme_root ) { 619 $theme_root = WP_CONTENT_DIR . '/themes'; 620 } 621 622 /** 623 * Filters the absolute path to the themes directory. 624 * 625 * @since 1.5.0 626 * 627 * @param string $theme_root Absolute path to themes directory. 628 */ 629 return apply_filters( 'theme_root', $theme_root ); 630 } 631 632 /** 633 * Retrieves URI for themes directory. 634 * 635 * Does not have trailing slash. 636 * 637 * @since 1.5.0 638 * 639 * @global string[] $wp_theme_directories 640 * 641 * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme. 642 * Default is to leverage the main theme root. 643 * @param string $theme_root Optional. The theme root for which calculations will be based, 644 * preventing the need for a get_raw_theme_root() call. Default empty. 645 * @return string Themes directory URI. 646 */ 647 function get_theme_root_uri( $stylesheet_or_template = '', $theme_root = '' ) { 648 global $wp_theme_directories; 649 650 if ( $stylesheet_or_template && ! $theme_root ) { 651 $theme_root = get_raw_theme_root( $stylesheet_or_template ); 652 } 653 654 if ( $stylesheet_or_template && $theme_root ) { 655 if ( in_array( $theme_root, (array) $wp_theme_directories, true ) ) { 656 // Absolute path. Make an educated guess. YMMV -- but note the filter below. 657 if ( str_starts_with( $theme_root, WP_CONTENT_DIR ) ) { 658 $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) ); 659 } elseif ( str_starts_with( $theme_root, ABSPATH ) ) { 660 $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) ); 661 } elseif ( str_starts_with( $theme_root, WP_PLUGIN_DIR ) || str_starts_with( $theme_root, WPMU_PLUGIN_DIR ) ) { 662 $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root ); 663 } else { 664 $theme_root_uri = $theme_root; 665 } 666 } else { 667 $theme_root_uri = content_url( $theme_root ); 668 } 669 } else { 670 $theme_root_uri = content_url( 'themes' ); 671 } 672 673 /** 674 * Filters the URI for themes directory. 675 * 676 * @since 1.5.0 677 * 678 * @param string $theme_root_uri The URI for themes directory. 679 * @param string $siteurl WordPress web address which is set in General Options. 680 * @param string $stylesheet_or_template The stylesheet or template name of the theme. 681 */ 682 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template ); 683 } 684 685 /** 686 * Gets the raw theme root relative to the content directory with no filters applied. 687 * 688 * @since 3.1.0 689 * 690 * @global string[] $wp_theme_directories 691 * 692 * @param string $stylesheet_or_template The stylesheet or template name of the theme. 693 * @param bool $skip_cache Optional. Whether to skip the cache. 694 * Defaults to false, meaning the cache is used. 695 * @return string Theme root. 696 */ 697 function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) { 698 global $wp_theme_directories; 699 700 if ( ! is_array( $wp_theme_directories ) || count( $wp_theme_directories ) <= 1 ) { 701 return '/themes'; 702 } 703 704 $theme_root = false; 705 706 // If requesting the root for the active theme, consult options to avoid calling get_theme_roots(). 707 if ( ! $skip_cache ) { 708 if ( get_option( 'stylesheet' ) === $stylesheet_or_template ) { 709 $theme_root = get_option( 'stylesheet_root' ); 710 } elseif ( get_option( 'template' ) === $stylesheet_or_template ) { 711 $theme_root = get_option( 'template_root' ); 712 } 713 } 714 715 if ( empty( $theme_root ) ) { 716 $theme_roots = get_theme_roots(); 717 if ( ! empty( $theme_roots[ $stylesheet_or_template ] ) ) { 718 $theme_root = $theme_roots[ $stylesheet_or_template ]; 719 } 720 } 721 722 return $theme_root; 723 } 724 725 /** 726 * Displays localized stylesheet link element. 727 * 728 * @since 2.1.0 729 */ 730 function locale_stylesheet() { 731 $stylesheet = get_locale_stylesheet_uri(); 732 if ( empty( $stylesheet ) ) { 733 return; 734 } 735 736 $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; 737 738 printf( 739 '<link rel="stylesheet" href="%s"%s media="screen" />', 740 $stylesheet, 741 $type_attr 742 ); 743 } 744 745 /** 746 * Switches the theme. 747 * 748 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature 749 * of two arguments: $template then $stylesheet. This is for backward compatibility. 750 * 751 * @since 2.5.0 752 * 753 * @global string[] $wp_theme_directories 754 * @global WP_Customize_Manager $wp_customize 755 * @global array $sidebars_widgets 756 * @global array $wp_registered_sidebars 757 * 758 * @param string $stylesheet Stylesheet name. 759 */ 760 function switch_theme( $stylesheet ) { 761 global $wp_theme_directories, $wp_customize, $sidebars_widgets, $wp_registered_sidebars; 762 763 $requirements = validate_theme_requirements( $stylesheet ); 764 if ( is_wp_error( $requirements ) ) { 765 wp_die( $requirements ); 766 } 767 768 $_sidebars_widgets = null; 769 if ( 'wp_ajax_customize_save' === current_action() ) { 770 $old_sidebars_widgets_data_setting = $wp_customize->get_setting( 'old_sidebars_widgets_data' ); 771 if ( $old_sidebars_widgets_data_setting ) { 772 $_sidebars_widgets = $wp_customize->post_value( $old_sidebars_widgets_data_setting ); 773 } 774 } elseif ( is_array( $sidebars_widgets ) ) { 775 $_sidebars_widgets = $sidebars_widgets; 776 } 777 778 if ( is_array( $_sidebars_widgets ) ) { 779 set_theme_mod( 780 'sidebars_widgets', 781 array( 782 'time' => time(), 783 'data' => $_sidebars_widgets, 784 ) 785 ); 786 } 787 788 $nav_menu_locations = get_theme_mod( 'nav_menu_locations' ); 789 update_option( 'theme_switch_menu_locations', $nav_menu_locations, true ); 790 791 if ( func_num_args() > 1 ) { 792 $stylesheet = func_get_arg( 1 ); 793 } 794 795 $old_theme = wp_get_theme(); 796 $new_theme = wp_get_theme( $stylesheet ); 797 $template = $new_theme->get_template(); 798 799 if ( wp_is_recovery_mode() ) { 800 $paused_themes = wp_paused_themes(); 801 $paused_themes->delete( $old_theme->get_stylesheet() ); 802 $paused_themes->delete( $old_theme->get_template() ); 803 } 804 805 update_option( 'template', $template ); 806 update_option( 'stylesheet', $stylesheet ); 807 808 if ( count( $wp_theme_directories ) > 1 ) { 809 update_option( 'template_root', get_raw_theme_root( $template, true ) ); 810 update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) ); 811 } else { 812 delete_option( 'template_root' ); 813 delete_option( 'stylesheet_root' ); 814 } 815 816 $new_name = $new_theme->get( 'Name' ); 817 818 update_option( 'current_theme', $new_name ); 819 820 // Migrate from the old mods_{name} option to theme_mods_{slug}. 821 if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) { 822 $default_theme_mods = (array) get_option( 'mods_' . $new_name ); 823 if ( ! empty( $nav_menu_locations ) && empty( $default_theme_mods['nav_menu_locations'] ) ) { 824 $default_theme_mods['nav_menu_locations'] = $nav_menu_locations; 825 } 826 add_option( "theme_mods_$stylesheet", $default_theme_mods ); 827 } else { 828 /* 829 * Since retrieve_widgets() is called when initializing a theme in the Customizer, 830 * we need to remove the theme mods to avoid overwriting changes made via 831 * the Customizer when accessing wp-admin/widgets.php. 832 */ 833 if ( 'wp_ajax_customize_save' === current_action() ) { 834 remove_theme_mod( 'sidebars_widgets' ); 835 } 836 } 837 838 // Stores classic sidebars for later use by block themes. 839 if ( $new_theme->is_block_theme() ) { 840 set_theme_mod( 'wp_classic_sidebars', $wp_registered_sidebars ); 841 } 842 843 update_option( 'theme_switched', $old_theme->get_stylesheet() ); 844 845 /* 846 * Reset template globals when switching themes outside of a switched blog 847 * context to ensure templates will be loaded from the new theme. 848 */ 849 if ( ! is_multisite() || ! ms_is_switched() ) { 850 wp_set_template_globals(); 851 } 852 853 // Clear pattern caches. 854 if ( ! is_multisite() ) { 855 $new_theme->delete_pattern_cache(); 856 $old_theme->delete_pattern_cache(); 857 } 858 859 // Set autoload=no for the old theme, autoload=yes for the switched theme. 860 $theme_mods_options = array( 861 'theme_mods_' . $stylesheet => 'yes', 862 'theme_mods_' . $old_theme->get_stylesheet() => 'no', 863 ); 864 wp_set_option_autoload_values( $theme_mods_options ); 865 866 /** 867 * Fires after the theme is switched. 868 * 869 * See {@see 'after_switch_theme'}. 870 * 871 * @since 1.5.0 872 * @since 4.5.0 Introduced the `$old_theme` parameter. 873 * 874 * @param string $new_name Name of the new theme. 875 * @param WP_Theme $new_theme WP_Theme instance of the new theme. 876 * @param WP_Theme $old_theme WP_Theme instance of the old theme. 877 */ 878 do_action( 'switch_theme', $new_name, $new_theme, $old_theme ); 879 } 880 881 /** 882 * Checks that the active theme has the required files. 883 * 884 * Standalone themes need to have a `templates/index.html` or `index.php` template file. 885 * Child themes need to have a `Template` header in the `style.css` stylesheet. 886 * 887 * Does not initially check the default theme, which is the fallback and should always exist. 888 * But if it doesn't exist, it'll fall back to the latest core default theme that does exist. 889 * Will switch theme to the fallback theme if active theme does not validate. 890 * 891 * You can use the {@see 'validate_current_theme'} filter to return false to disable 892 * this functionality. 893 * 894 * @since 1.5.0 895 * @since 6.0.0 Removed the requirement for block themes to have an `index.php` template. 896 * 897 * @see WP_DEFAULT_THEME 898 * 899 * @return bool 900 */ 901 function validate_current_theme() { 902 /** 903 * Filters whether to validate the active theme. 904 * 905 * @since 2.7.0 906 * 907 * @param bool $validate Whether to validate the active theme. Default true. 908 */ 909 if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) ) { 910 return true; 911 } 912 913 if ( 914 ! file_exists( get_template_directory() . '/templates/index.html' ) 915 && ! file_exists( get_template_directory() . '/block-templates/index.html' ) // Deprecated path support since 5.9.0. 916 && ! file_exists( get_template_directory() . '/index.php' ) 917 ) { 918 // Invalid. 919 } elseif ( ! file_exists( get_template_directory() . '/style.css' ) ) { 920 // Invalid. 921 } elseif ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) { 922 // Invalid. 923 } else { 924 // Valid. 925 return true; 926 } 927 928 $default = wp_get_theme( WP_DEFAULT_THEME ); 929 if ( $default->exists() ) { 930 switch_theme( WP_DEFAULT_THEME ); 931 return false; 932 } 933 934 /** 935 * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist, 936 * switch to the latest core default theme that's installed. 937 * 938 * If it turns out that this latest core default theme is our current 939 * theme, then there's nothing we can do about that, so we have to bail, 940 * rather than going into an infinite loop. (This is why there are 941 * checks against WP_DEFAULT_THEME above, also.) We also can't do anything 942 * if it turns out there is no default theme installed. (That's `false`.) 943 */ 944 $default = WP_Theme::get_core_default_theme(); 945 if ( false === $default || get_stylesheet() === $default->get_stylesheet() ) { 946 return true; 947 } 948 949 switch_theme( $default->get_stylesheet() ); 950 return false; 951 } 952 953 /** 954 * Validates the theme requirements for WordPress version and PHP version. 955 * 956 * Uses the information from `Requires at least` and `Requires PHP` headers 957 * defined in the theme's `style.css` file. 958 * 959 * @since 5.5.0 960 * @since 5.8.0 Removed support for using `readme.txt` as a fallback. 961 * 962 * @param string $stylesheet Directory name for the theme. 963 * @return true|WP_Error True if requirements are met, WP_Error on failure. 964 */ 965 function validate_theme_requirements( $stylesheet ) { 966 $theme = wp_get_theme( $stylesheet ); 967 968 $requirements = array( 969 'requires' => ! empty( $theme->get( 'RequiresWP' ) ) ? $theme->get( 'RequiresWP' ) : '', 970 'requires_php' => ! empty( $theme->get( 'RequiresPHP' ) ) ? $theme->get( 'RequiresPHP' ) : '', 971 ); 972 973 $compatible_wp = is_wp_version_compatible( $requirements['requires'] ); 974 $compatible_php = is_php_version_compatible( $requirements['requires_php'] ); 975 976 if ( ! $compatible_wp && ! $compatible_php ) { 977 return new WP_Error( 978 'theme_wp_php_incompatible', 979 sprintf( 980 /* translators: %s: Theme name. */ 981 _x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'theme' ), 982 $theme->display( 'Name' ) 983 ) 984 ); 985 } elseif ( ! $compatible_php ) { 986 return new WP_Error( 987 'theme_php_incompatible', 988 sprintf( 989 /* translators: %s: Theme name. */ 990 _x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'theme' ), 991 $theme->display( 'Name' ) 992 ) 993 ); 994 } elseif ( ! $compatible_wp ) { 995 return new WP_Error( 996 'theme_wp_incompatible', 997 sprintf( 998 /* translators: %s: Theme name. */ 999 _x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'theme' ), 1000 $theme->display( 'Name' ) 1001 ) 1002 ); 1003 } 1004 1005 return true; 1006 } 1007 1008 /** 1009 * Retrieves all theme modifications. 1010 * 1011 * @since 3.1.0 1012 * @since 5.9.0 The return value is always an array. 1013 * 1014 * @return array Theme modifications. 1015 */ 1016 function get_theme_mods() { 1017 $theme_slug = get_option( 'stylesheet' ); 1018 $mods = get_option( "theme_mods_$theme_slug" ); 1019 1020 if ( false === $mods ) { 1021 $theme_name = get_option( 'current_theme' ); 1022 if ( false === $theme_name ) { 1023 $theme_name = wp_get_theme()->get( 'Name' ); 1024 } 1025 1026 $mods = get_option( "mods_$theme_name" ); // Deprecated location. 1027 if ( is_admin() && false !== $mods ) { 1028 update_option( "theme_mods_$theme_slug", $mods ); 1029 delete_option( "mods_$theme_name" ); 1030 } 1031 } 1032 1033 if ( ! is_array( $mods ) ) { 1034 $mods = array(); 1035 } 1036 1037 return $mods; 1038 } 1039 1040 /** 1041 * Retrieves theme modification value for the active theme. 1042 * 1043 * If the modification name does not exist and `$default_value` is a string, then the 1044 * default will be passed through the {@link https://www.php.net/sprintf sprintf()} 1045 * PHP function with the template directory URI as the first value and the 1046 * stylesheet directory URI as the second value. 1047 * 1048 * @since 2.1.0 1049 * 1050 * @param string $name Theme modification name. 1051 * @param mixed $default_value Optional. Theme modification default value. Default false. 1052 * @return mixed Theme modification value. 1053 */ 1054 function get_theme_mod( $name, $default_value = false ) { 1055 $mods = get_theme_mods(); 1056 1057 if ( isset( $mods[ $name ] ) ) { 1058 /** 1059 * Filters the theme modification, or 'theme_mod', value. 1060 * 1061 * The dynamic portion of the hook name, `$name`, refers to the key name 1062 * of the modification array. For example, 'header_textcolor', 'header_image', 1063 * and so on depending on the theme options. 1064 * 1065 * @since 2.2.0 1066 * 1067 * @param mixed $current_mod The value of the active theme modification. 1068 */ 1069 return apply_filters( "theme_mod_{$name}", $mods[ $name ] ); 1070 } 1071 1072 if ( is_string( $default_value ) ) { 1073 // Only run the replacement if an sprintf() string format pattern was found. 1074 if ( preg_match( '#(?<!%)%(?:\d+\$?)?s#', $default_value ) ) { 1075 // Remove a single trailing percent sign. 1076 $default_value = preg_replace( '#(?<!%)%$#', '', $default_value ); 1077 $default_value = sprintf( $default_value, get_template_directory_uri(), get_stylesheet_directory_uri() ); 1078 } 1079 } 1080 1081 /** This filter is documented in wp-includes/theme.php */ 1082 return apply_filters( "theme_mod_{$name}", $default_value ); 1083 } 1084 1085 /** 1086 * Updates theme modification value for the active theme. 1087 * 1088 * @since 2.1.0 1089 * @since 5.6.0 A return value was added. 1090 * 1091 * @param string $name Theme modification name. 1092 * @param mixed $value Theme modification value. 1093 * @return bool True if the value was updated, false otherwise. 1094 */ 1095 function set_theme_mod( $name, $value ) { 1096 $mods = get_theme_mods(); 1097 $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false; 1098 1099 /** 1100 * Filters the theme modification, or 'theme_mod', value on save. 1101 * 1102 * The dynamic portion of the hook name, `$name`, refers to the key name 1103 * of the modification array. For example, 'header_textcolor', 'header_image', 1104 * and so on depending on the theme options. 1105 * 1106 * @since 3.9.0 1107 * 1108 * @param mixed $value The new value of the theme modification. 1109 * @param mixed $old_value The current value of the theme modification. 1110 */ 1111 $mods[ $name ] = apply_filters( "pre_set_theme_mod_{$name}", $value, $old_value ); 1112 1113 $theme = get_option( 'stylesheet' ); 1114 1115 return update_option( "theme_mods_$theme", $mods ); 1116 } 1117 1118 /** 1119 * Removes theme modification name from active theme list. 1120 * 1121 * If removing the name also removes all elements, then the entire option 1122 * will be removed. 1123 * 1124 * @since 2.1.0 1125 * 1126 * @param string $name Theme modification name. 1127 */ 1128 function remove_theme_mod( $name ) { 1129 $mods = get_theme_mods(); 1130 1131 if ( ! isset( $mods[ $name ] ) ) { 1132 return; 1133 } 1134 1135 unset( $mods[ $name ] ); 1136 1137 if ( empty( $mods ) ) { 1138 remove_theme_mods(); 1139 return; 1140 } 1141 1142 $theme = get_option( 'stylesheet' ); 1143 1144 update_option( "theme_mods_$theme", $mods ); 1145 } 1146 1147 /** 1148 * Removes theme modifications option for the active theme. 1149 * 1150 * @since 2.1.0 1151 */ 1152 function remove_theme_mods() { 1153 delete_option( 'theme_mods_' . get_option( 'stylesheet' ) ); 1154 1155 // Old style. 1156 $theme_name = get_option( 'current_theme' ); 1157 if ( false === $theme_name ) { 1158 $theme_name = wp_get_theme()->get( 'Name' ); 1159 } 1160 1161 delete_option( 'mods_' . $theme_name ); 1162 } 1163 1164 /** 1165 * Retrieves the custom header text color in 3- or 6-digit hexadecimal form. 1166 * 1167 * @since 2.1.0 1168 * 1169 * @return string Header text color in 3- or 6-digit hexadecimal form (minus the hash symbol). 1170 */ 1171 function get_header_textcolor() { 1172 return get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) ); 1173 } 1174 1175 /** 1176 * Displays the custom header text color in 3- or 6-digit hexadecimal form (minus the hash symbol). 1177 * 1178 * @since 2.1.0 1179 */ 1180 function header_textcolor() { 1181 echo get_header_textcolor(); 1182 } 1183 1184 /** 1185 * Whether to display the header text. 1186 * 1187 * @since 3.4.0 1188 * 1189 * @return bool 1190 */ 1191 function display_header_text() { 1192 if ( ! current_theme_supports( 'custom-header', 'header-text' ) ) { 1193 return false; 1194 } 1195 1196 $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) ); 1197 return 'blank' !== $text_color; 1198 } 1199 1200 /** 1201 * Checks whether a header image is set or not. 1202 * 1203 * @since 4.2.0 1204 * 1205 * @see get_header_image() 1206 * 1207 * @return bool Whether a header image is set or not. 1208 */ 1209 function has_header_image() { 1210 return (bool) get_header_image(); 1211 } 1212 1213 /** 1214 * Retrieves header image for custom header. 1215 * 1216 * @since 2.1.0 1217 * 1218 * @return string|false 1219 */ 1220 function get_header_image() { 1221 $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) ); 1222 1223 if ( 'remove-header' === $url ) { 1224 return false; 1225 } 1226 1227 if ( is_random_header_image() ) { 1228 $url = get_random_header_image(); 1229 } 1230 1231 /** 1232 * Filters the header image URL. 1233 * 1234 * @since 6.1.0 1235 * 1236 * @param string $url Header image URL. 1237 */ 1238 $url = apply_filters( 'get_header_image', $url ); 1239 1240 if ( ! is_string( $url ) ) { 1241 return false; 1242 } 1243 1244 $url = trim( $url ); 1245 return sanitize_url( set_url_scheme( $url ) ); 1246 } 1247 1248 /** 1249 * Creates image tag markup for a custom header image. 1250 * 1251 * @since 4.4.0 1252 * 1253 * @param array $attr Optional. Additional attributes for the image tag. Can be used 1254 * to override the default attributes. Default empty. 1255 * @return string HTML image element markup or empty string on failure. 1256 */ 1257 function get_header_image_tag( $attr = array() ) { 1258 $header = get_custom_header(); 1259 $header->url = get_header_image(); 1260 1261 if ( ! $header->url ) { 1262 return ''; 1263 } 1264 1265 $width = absint( $header->width ); 1266 $height = absint( $header->height ); 1267 $alt = ''; 1268 1269 // Use alternative text assigned to the image, if available. Otherwise, leave it empty. 1270 if ( ! empty( $header->attachment_id ) ) { 1271 $image_alt = get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true ); 1272 1273 if ( is_string( $image_alt ) ) { 1274 $alt = $image_alt; 1275 } 1276 } 1277 1278 $attr = wp_parse_args( 1279 $attr, 1280 array( 1281 'src' => $header->url, 1282 'width' => $width, 1283 'height' => $height, 1284 'alt' => $alt, 1285 ) 1286 ); 1287 1288 // Generate 'srcset' and 'sizes' if not already present. 1289 if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) { 1290 $image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true ); 1291 $size_array = array( $width, $height ); 1292 1293 if ( is_array( $image_meta ) ) { 1294 $srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id ); 1295 1296 if ( ! empty( $attr['sizes'] ) ) { 1297 $sizes = $attr['sizes']; 1298 } else { 1299 $sizes = wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id ); 1300 } 1301 1302 if ( $srcset && $sizes ) { 1303 $attr['srcset'] = $srcset; 1304 $attr['sizes'] = $sizes; 1305 } 1306 } 1307 } 1308 1309 $attr = array_merge( 1310 $attr, 1311 wp_get_loading_optimization_attributes( 'img', $attr, 'get_header_image_tag' ) 1312 ); 1313 1314 /* 1315 * If the default value of `lazy` for the `loading` attribute is overridden 1316 * to omit the attribute for this image, ensure it is not included. 1317 */ 1318 if ( isset( $attr['loading'] ) && ! $attr['loading'] ) { 1319 unset( $attr['loading'] ); 1320 } 1321 1322 // If the `fetchpriority` attribute is overridden and set to false or an empty string. 1323 if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) { 1324 unset( $attr['fetchpriority'] ); 1325 } 1326 1327 // If the `decoding` attribute is overridden and set to false or an empty string. 1328 if ( isset( $attr['decoding'] ) && ! $attr['decoding'] ) { 1329 unset( $attr['decoding'] ); 1330 } 1331 1332 /** 1333 * Filters the list of header image attributes. 1334 * 1335 * @since 5.9.0 1336 * 1337 * @param array $attr Array of the attributes for the image tag. 1338 * @param object $header The custom header object returned by 'get_custom_header()'. 1339 */ 1340 $attr = apply_filters( 'get_header_image_tag_attributes', $attr, $header ); 1341 1342 $attr = array_map( 'esc_attr', $attr ); 1343 $html = '<img'; 1344 1345 foreach ( $attr as $name => $value ) { 1346 $html .= ' ' . $name . '="' . $value . '"'; 1347 } 1348 1349 $html .= ' />'; 1350 1351 /** 1352 * Filters the markup of header images. 1353 * 1354 * @since 4.4.0 1355 * 1356 * @param string $html The HTML image tag markup being filtered. 1357 * @param object $header The custom header object returned by 'get_custom_header()'. 1358 * @param array $attr Array of the attributes for the image tag. 1359 */ 1360 return apply_filters( 'get_header_image_tag', $html, $header, $attr ); 1361 } 1362 1363 /** 1364 * Displays the image markup for a custom header image. 1365 * 1366 * @since 4.4.0 1367 * 1368 * @param array $attr Optional. Attributes for the image markup. Default empty. 1369 */ 1370 function the_header_image_tag( $attr = array() ) { 1371 echo get_header_image_tag( $attr ); 1372 } 1373 1374 /** 1375 * Gets random header image data from registered images in theme. 1376 * 1377 * @since 3.4.0 1378 * 1379 * @access private 1380 * 1381 * @global array $_wp_default_headers 1382 * 1383 * @return object 1384 */ 1385 function _get_random_header_data() { 1386 global $_wp_default_headers; 1387 static $_wp_random_header = null; 1388 1389 if ( empty( $_wp_random_header ) ) { 1390 $header_image_mod = get_theme_mod( 'header_image', '' ); 1391 $headers = array(); 1392 1393 if ( 'random-uploaded-image' === $header_image_mod ) { 1394 $headers = get_uploaded_header_images(); 1395 } elseif ( ! empty( $_wp_default_headers ) ) { 1396 if ( 'random-default-image' === $header_image_mod ) { 1397 $headers = $_wp_default_headers; 1398 } else { 1399 if ( current_theme_supports( 'custom-header', 'random-default' ) ) { 1400 $headers = $_wp_default_headers; 1401 } 1402 } 1403 } 1404 1405 if ( empty( $headers ) ) { 1406 return new stdClass(); 1407 } 1408 1409 $_wp_random_header = (object) $headers[ array_rand( $headers ) ]; 1410 1411 $_wp_random_header->url = sprintf( 1412 $_wp_random_header->url, 1413 get_template_directory_uri(), 1414 get_stylesheet_directory_uri() 1415 ); 1416 1417 $_wp_random_header->thumbnail_url = sprintf( 1418 $_wp_random_header->thumbnail_url, 1419 get_template_directory_uri(), 1420 get_stylesheet_directory_uri() 1421 ); 1422 } 1423 1424 return $_wp_random_header; 1425 } 1426 1427 /** 1428 * Gets random header image URL from registered images in theme. 1429 * 1430 * @since 3.2.0 1431 * 1432 * @return string Path to header image. 1433 */ 1434 function get_random_header_image() { 1435 $random_image = _get_random_header_data(); 1436 1437 if ( empty( $random_image->url ) ) { 1438 return ''; 1439 } 1440 1441 return $random_image->url; 1442 } 1443 1444 /** 1445 * Checks if random header image is in use. 1446 * 1447 * Always true if user expressly chooses the option in Appearance > Header. 1448 * Also true if theme has multiple header images registered, no specific header image 1449 * is chosen, and theme turns on random headers with add_theme_support(). 1450 * 1451 * @since 3.2.0 1452 * 1453 * @param string $type The random pool to use. Possible values include 'any', 1454 * 'default', 'uploaded'. Default 'any'. 1455 * @return bool 1456 */ 1457 function is_random_header_image( $type = 'any' ) { 1458 $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) ); 1459 1460 if ( 'any' === $type ) { 1461 if ( 'random-default-image' === $header_image_mod 1462 || 'random-uploaded-image' === $header_image_mod 1463 || ( empty( $header_image_mod ) && '' !== get_random_header_image() ) 1464 ) { 1465 return true; 1466 } 1467 } else { 1468 if ( "random-$type-image" === $header_image_mod ) { 1469 return true; 1470 } elseif ( 'default' === $type 1471 && empty( $header_image_mod ) && '' !== get_random_header_image() 1472 ) { 1473 return true; 1474 } 1475 } 1476 1477 return false; 1478 } 1479 1480 /** 1481 * Displays header image URL. 1482 * 1483 * @since 2.1.0 1484 */ 1485 function header_image() { 1486 $image = get_header_image(); 1487 1488 if ( $image ) { 1489 echo esc_url( $image ); 1490 } 1491 } 1492 1493 /** 1494 * Gets the header images uploaded for the active theme. 1495 * 1496 * @since 3.2.0 1497 * 1498 * @return array 1499 */ 1500 function get_uploaded_header_images() { 1501 $header_images = array(); 1502 1503 $headers = get_posts( 1504 array( 1505 'post_type' => 'attachment', 1506 'meta_key' => '_wp_attachment_is_custom_header', 1507 'meta_value' => get_option( 'stylesheet' ), 1508 'orderby' => 'none', 1509 'nopaging' => true, 1510 ) 1511 ); 1512 1513 if ( empty( $headers ) ) { 1514 return array(); 1515 } 1516 1517 foreach ( (array) $headers as $header ) { 1518 $url = sanitize_url( wp_get_attachment_url( $header->ID ) ); 1519 $header_data = wp_get_attachment_metadata( $header->ID ); 1520 $header_index = $header->ID; 1521 1522 $header_images[ $header_index ] = array(); 1523 $header_images[ $header_index ]['attachment_id'] = $header->ID; 1524 $header_images[ $header_index ]['url'] = $url; 1525 $header_images[ $header_index ]['thumbnail_url'] = $url; 1526 $header_images[ $header_index ]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true ); 1527 1528 if ( isset( $header_data['attachment_parent'] ) ) { 1529 $header_images[ $header_index ]['attachment_parent'] = $header_data['attachment_parent']; 1530 } else { 1531 $header_images[ $header_index ]['attachment_parent'] = ''; 1532 } 1533 1534 if ( isset( $header_data['width'] ) ) { 1535 $header_images[ $header_index ]['width'] = $header_data['width']; 1536 } 1537 if ( isset( $header_data['height'] ) ) { 1538 $header_images[ $header_index ]['height'] = $header_data['height']; 1539 } 1540 } 1541 1542 return $header_images; 1543 } 1544 1545 /** 1546 * Gets the header image data. 1547 * 1548 * @since 3.4.0 1549 * 1550 * @global array $_wp_default_headers 1551 * 1552 * @return object 1553 */ 1554 function get_custom_header() { 1555 global $_wp_default_headers; 1556 1557 if ( is_random_header_image() ) { 1558 $data = _get_random_header_data(); 1559 } else { 1560 $data = get_theme_mod( 'header_image_data' ); 1561 if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) { 1562 $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() ); 1563 $data = array(); 1564 $data['url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args ); 1565 $data['thumbnail_url'] = $data['url']; 1566 if ( ! empty( $_wp_default_headers ) ) { 1567 foreach ( (array) $_wp_default_headers as $default_header ) { 1568 $url = vsprintf( $default_header['url'], $directory_args ); 1569 if ( $data['url'] === $url ) { 1570 $data = $default_header; 1571 $data['url'] = $url; 1572 $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args ); 1573 break; 1574 } 1575 } 1576 } 1577 } 1578 } 1579 1580 $default = array( 1581 'url' => '', 1582 'thumbnail_url' => '', 1583 'width' => get_theme_support( 'custom-header', 'width' ), 1584 'height' => get_theme_support( 'custom-header', 'height' ), 1585 'video' => get_theme_support( 'custom-header', 'video' ), 1586 ); 1587 return (object) wp_parse_args( $data, $default ); 1588 } 1589 1590 /** 1591 * Registers a selection of default headers to be displayed by the custom header admin UI. 1592 * 1593 * @since 3.0.0 1594 * 1595 * @global array $_wp_default_headers 1596 * 1597 * @param array $headers Array of headers keyed by a string ID. The IDs point to arrays 1598 * containing 'url', 'thumbnail_url', and 'description' keys. 1599 */ 1600 function register_default_headers( $headers ) { 1601 global $_wp_default_headers; 1602 1603 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers ); 1604 } 1605 1606 /** 1607 * Unregisters default headers. 1608 * 1609 * This function must be called after register_default_headers() has already added the 1610 * header you want to remove. 1611 * 1612 * @see register_default_headers() 1613 * @since 3.0.0 1614 * 1615 * @global array $_wp_default_headers 1616 * 1617 * @param string|array $header The header string id (key of array) to remove, or an array thereof. 1618 * @return bool|void A single header returns true on success, false on failure. 1619 * There is currently no return value for multiple headers. 1620 */ 1621 function unregister_default_headers( $header ) { 1622 global $_wp_default_headers; 1623 1624 if ( is_array( $header ) ) { 1625 array_map( 'unregister_default_headers', $header ); 1626 } elseif ( isset( $_wp_default_headers[ $header ] ) ) { 1627 unset( $_wp_default_headers[ $header ] ); 1628 return true; 1629 } else { 1630 return false; 1631 } 1632 } 1633 1634 /** 1635 * Checks whether a header video is set or not. 1636 * 1637 * @since 4.7.0 1638 * 1639 * @see get_header_video_url() 1640 * 1641 * @return bool Whether a header video is set or not. 1642 */ 1643 function has_header_video() { 1644 return (bool) get_header_video_url(); 1645 } 1646 1647 /** 1648 * Retrieves header video URL for custom header. 1649 * 1650 * Uses a local video if present, or falls back to an external video. 1651 * 1652 * @since 4.7.0 1653 * 1654 * @return string|false Header video URL or false if there is no video. 1655 */ 1656 function get_header_video_url() { 1657 $id = absint( get_theme_mod( 'header_video' ) ); 1658 1659 if ( $id ) { 1660 // Get the file URL from the attachment ID. 1661 $url = wp_get_attachment_url( $id ); 1662 } else { 1663 $url = get_theme_mod( 'external_header_video' ); 1664 } 1665 1666 /** 1667 * Filters the header video URL. 1668 * 1669 * @since 4.7.3 1670 * 1671 * @param string $url Header video URL, if available. 1672 */ 1673 $url = apply_filters( 'get_header_video_url', $url ); 1674 1675 if ( ! $id && ! $url ) { 1676 return false; 1677 } 1678 1679 return sanitize_url( set_url_scheme( $url ) ); 1680 } 1681 1682 /** 1683 * Displays header video URL. 1684 * 1685 * @since 4.7.0 1686 */ 1687 function the_header_video_url() { 1688 $video = get_header_video_url(); 1689 1690 if ( $video ) { 1691 echo esc_url( $video ); 1692 } 1693 } 1694 1695 /** 1696 * Retrieves header video settings. 1697 * 1698 * @since 4.7.0 1699 * 1700 * @return array 1701 */ 1702 function get_header_video_settings() { 1703 $header = get_custom_header(); 1704 $video_url = get_header_video_url(); 1705 $video_type = wp_check_filetype( $video_url, wp_get_mime_types() ); 1706 1707 $settings = array( 1708 'mimeType' => '', 1709 'posterUrl' => get_header_image(), 1710 'videoUrl' => $video_url, 1711 'width' => absint( $header->width ), 1712 'height' => absint( $header->height ), 1713 'minWidth' => 900, 1714 'minHeight' => 500, 1715 'l10n' => array( 1716 'pause' => __( 'Pause' ), 1717 'play' => __( 'Play' ), 1718 'pauseSpeak' => __( 'Video is paused.' ), 1719 'playSpeak' => __( 'Video is playing.' ), 1720 ), 1721 ); 1722 1723 if ( preg_match( '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#', $video_url ) ) { 1724 $settings['mimeType'] = 'video/x-youtube'; 1725 } elseif ( ! empty( $video_type['type'] ) ) { 1726 $settings['mimeType'] = $video_type['type']; 1727 } 1728 1729 /** 1730 * Filters header video settings. 1731 * 1732 * @since 4.7.0 1733 * 1734 * @param array $settings An array of header video settings. 1735 */ 1736 return apply_filters( 'header_video_settings', $settings ); 1737 } 1738 1739 /** 1740 * Checks whether a custom header is set or not. 1741 * 1742 * @since 4.7.0 1743 * 1744 * @return bool True if a custom header is set. False if not. 1745 */ 1746 function has_custom_header() { 1747 if ( has_header_image() || ( has_header_video() && is_header_video_active() ) ) { 1748 return true; 1749 } 1750 1751 return false; 1752 } 1753 1754 /** 1755 * Checks whether the custom header video is eligible to show on the current page. 1756 * 1757 * @since 4.7.0 1758 * 1759 * @return bool True if the custom header video should be shown. False if not. 1760 */ 1761 function is_header_video_active() { 1762 if ( ! get_theme_support( 'custom-header', 'video' ) ) { 1763 return false; 1764 } 1765 1766 $video_active_cb = get_theme_support( 'custom-header', 'video-active-callback' ); 1767 1768 if ( empty( $video_active_cb ) || ! is_callable( $video_active_cb ) ) { 1769 $show_video = true; 1770 } else { 1771 $show_video = call_user_func( $video_active_cb ); 1772 } 1773 1774 /** 1775 * Filters whether the custom header video is eligible to show on the current page. 1776 * 1777 * @since 4.7.0 1778 * 1779 * @param bool $show_video Whether the custom header video should be shown. Returns the value 1780 * of the theme setting for the `custom-header`'s `video-active-callback`. 1781 * If no callback is set, the default value is that of `is_front_page()`. 1782 */ 1783 return apply_filters( 'is_header_video_active', $show_video ); 1784 } 1785 1786 /** 1787 * Retrieves the markup for a custom header. 1788 * 1789 * The container div will always be returned in the Customizer preview. 1790 * 1791 * @since 4.7.0 1792 * 1793 * @return string The markup for a custom header on success. 1794 */ 1795 function get_custom_header_markup() { 1796 if ( ! has_custom_header() && ! is_customize_preview() ) { 1797 return ''; 1798 } 1799 1800 return sprintf( 1801 '<div id="wp-custom-header" class="wp-custom-header">%s</div>', 1802 get_header_image_tag() 1803 ); 1804 } 1805 1806 /** 1807 * Prints the markup for a custom header. 1808 * 1809 * A container div will always be printed in the Customizer preview. 1810 * 1811 * @since 4.7.0 1812 */ 1813 function the_custom_header_markup() { 1814 $custom_header = get_custom_header_markup(); 1815 if ( empty( $custom_header ) ) { 1816 return; 1817 } 1818 1819 echo $custom_header; 1820 1821 if ( is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) { 1822 wp_enqueue_script( 'wp-custom-header' ); 1823 wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings() ); 1824 } 1825 } 1826 1827 /** 1828 * Retrieves background image for custom background. 1829 * 1830 * @since 3.0.0 1831 * 1832 * @return string 1833 */ 1834 function get_background_image() { 1835 return get_theme_mod( 'background_image', get_theme_support( 'custom-background', 'default-image' ) ); 1836 } 1837 1838 /** 1839 * Displays background image path. 1840 * 1841 * @since 3.0.0 1842 */ 1843 function background_image() { 1844 echo get_background_image(); 1845 } 1846 1847 /** 1848 * Retrieves value for custom background color. 1849 * 1850 * @since 3.0.0 1851 * 1852 * @return string 1853 */ 1854 function get_background_color() { 1855 return get_theme_mod( 'background_color', get_theme_support( 'custom-background', 'default-color' ) ); 1856 } 1857 1858 /** 1859 * Displays background color value. 1860 * 1861 * @since 3.0.0 1862 */ 1863 function background_color() { 1864 echo get_background_color(); 1865 } 1866 1867 /** 1868 * Default custom background callback. 1869 * 1870 * @since 3.0.0 1871 */ 1872 function _custom_background_cb() { 1873 // $background is the saved custom image, or the default image. 1874 $background = set_url_scheme( get_background_image() ); 1875 1876 /* 1877 * $color is the saved custom color. 1878 * A default has to be specified in style.css. It will not be printed here. 1879 */ 1880 $color = get_background_color(); 1881 1882 if ( get_theme_support( 'custom-background', 'default-color' ) === $color ) { 1883 $color = false; 1884 } 1885 1886 $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; 1887 1888 if ( ! $background && ! $color ) { 1889 if ( is_customize_preview() ) { 1890 printf( '<style%s id="custom-background-css"></style>', $type_attr ); 1891 } 1892 return; 1893 } 1894 1895 $style = $color ? 'background-color: ' . maybe_hash_hex_color( $color ) . ';' : ''; 1896 1897 if ( $background ) { 1898 $image = ' background-image: url("' . sanitize_url( $background ) . '");'; 1899 1900 // Background Position. 1901 $position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ); 1902 $position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) ); 1903 1904 if ( ! in_array( $position_x, array( 'left', 'center', 'right' ), true ) ) { 1905 $position_x = 'left'; 1906 } 1907 1908 if ( ! in_array( $position_y, array( 'top', 'center', 'bottom' ), true ) ) { 1909 $position_y = 'top'; 1910 } 1911 1912 $position = " background-position: $position_x $position_y;"; 1913 1914 // Background Size. 1915 $size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ); 1916 1917 if ( ! in_array( $size, array( 'auto', 'contain', 'cover' ), true ) ) { 1918 $size = 'auto'; 1919 } 1920 1921 $size = " background-size: $size;"; 1922 1923 // Background Repeat. 1924 $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) ); 1925 1926 if ( ! in_array( $repeat, array( 'repeat-x', 'repeat-y', 'repeat', 'no-repeat' ), true ) ) { 1927 $repeat = 'repeat'; 1928 } 1929 1930 $repeat = " background-repeat: $repeat;"; 1931 1932 // Background Scroll. 1933 $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) ); 1934 1935 if ( 'fixed' !== $attachment ) { 1936 $attachment = 'scroll'; 1937 } 1938 1939 $attachment = " background-attachment: $attachment;"; 1940 1941 $style .= $image . $position . $size . $repeat . $attachment; 1942 } 1943 ?> 1944 <style<?php echo $type_attr; ?> id="custom-background-css"> 1945 body.custom-background { <?php echo trim( $style ); ?> } 1946 </style> 1947 <?php 1948 } 1949 1950 /** 1951 * Renders the Custom CSS style element. 1952 * 1953 * @since 4.7.0 1954 */ 1955 function wp_custom_css_cb() { 1956 $styles = wp_get_custom_css(); 1957 if ( $styles || is_customize_preview() ) : 1958 $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; 1959 ?> 1960 <style<?php echo $type_attr; ?> id="wp-custom-css"> 1961 <?php 1962 // Note that esc_html() cannot be used because `div > span` is not interpreted properly. 1963 echo strip_tags( $styles ); 1964 ?> 1965 </style> 1966 <?php 1967 endif; 1968 } 1969 1970 /** 1971 * Fetches the `custom_css` post for a given theme. 1972 * 1973 * @since 4.7.0 1974 * 1975 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the active theme. 1976 * @return WP_Post|null The custom_css post or null if none exists. 1977 */ 1978 function wp_get_custom_css_post( $stylesheet = '' ) { 1979 if ( empty( $stylesheet ) ) { 1980 $stylesheet = get_stylesheet(); 1981 } 1982 1983 $custom_css_query_vars = array( 1984 'post_type' => 'custom_css', 1985 'post_status' => get_post_stati(), 1986 'name' => sanitize_title( $stylesheet ), 1987 'posts_per_page' => 1, 1988 'no_found_rows' => true, 1989 'cache_results' => true, 1990 'update_post_meta_cache' => false, 1991 'update_post_term_cache' => false, 1992 'lazy_load_term_meta' => false, 1993 ); 1994 1995 $post = null; 1996 if ( get_stylesheet() === $stylesheet ) { 1997 $post_id = get_theme_mod( 'custom_css_post_id' ); 1998 1999 if ( $post_id > 0 && get_post( $post_id ) ) { 2000 $post = get_post( $post_id ); 2001 } 2002 2003 // `-1` indicates no post exists; no query necessary. 2004 if ( ! $post && -1 !== $post_id ) { 2005 $query = new WP_Query( $custom_css_query_vars ); 2006 $post = $query->post; 2007 /* 2008 * Cache the lookup. See wp_update_custom_css_post(). 2009 * @todo This should get cleared if a custom_css post is added/removed. 2010 */ 2011 set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 ); 2012 } 2013 } else { 2014 $query = new WP_Query( $custom_css_query_vars ); 2015 $post = $query->post; 2016 } 2017 2018 return $post; 2019 } 2020 2021 /** 2022 * Fetches the saved Custom CSS content for rendering. 2023 * 2024 * @since 4.7.0 2025 * 2026 * @param string $stylesheet Optional. A theme object stylesheet name. Defaults to the active theme. 2027 * @return string The Custom CSS Post content. 2028 */ 2029 function wp_get_custom_css( $stylesheet = '' ) { 2030 $css = ''; 2031 2032 if ( empty( $stylesheet ) ) { 2033 $stylesheet = get_stylesheet(); 2034 } 2035 2036 $post = wp_get_custom_css_post( $stylesheet ); 2037 if ( $post ) { 2038 $css = $post->post_content; 2039 } 2040 2041 /** 2042 * Filters the custom CSS output into the head element. 2043 * 2044 * @since 4.7.0 2045 * 2046 * @param string $css CSS pulled in from the Custom CSS post type. 2047 * @param string $stylesheet The theme stylesheet name. 2048 */ 2049 $css = apply_filters( 'wp_get_custom_css', $css, $stylesheet ); 2050 2051 return $css; 2052 } 2053 2054 /** 2055 * Updates the `custom_css` post for a given theme. 2056 * 2057 * Inserts a `custom_css` post when one doesn't yet exist. 2058 * 2059 * @since 4.7.0 2060 * 2061 * @param string $css CSS, stored in `post_content`. 2062 * @param array $args { 2063 * Args. 2064 * 2065 * @type string $preprocessed Optional. Pre-processed CSS, stored in `post_content_filtered`. 2066 * Normally empty string. 2067 * @type string $stylesheet Optional. Stylesheet (child theme) to update. 2068 * Defaults to active theme/stylesheet. 2069 * } 2070 * @return WP_Post|WP_Error Post on success, error on failure. 2071 */ 2072 function wp_update_custom_css_post( $css, $args = array() ) { 2073 $args = wp_parse_args( 2074 $args, 2075 array( 2076 'preprocessed' => '', 2077 'stylesheet' => get_stylesheet(), 2078 ) 2079 ); 2080 2081 $data = array( 2082 'css' => $css, 2083 'preprocessed' => $args['preprocessed'], 2084 ); 2085 2086 /** 2087 * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args 2088 * for a `custom_css` post being updated. 2089 * 2090 * This filter can be used by plugin that offer CSS pre-processors, to store the original 2091 * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`. 2092 * When used in this way, the `post_content_filtered` should be supplied as the setting value 2093 * instead of `post_content` via a the `customize_value_custom_css` filter, for example: 2094 * 2095 * <code> 2096 * add_filter( 'customize_value_custom_css', function( $value, $setting ) { 2097 * $post = wp_get_custom_css_post( $setting->stylesheet ); 2098 * if ( $post && ! empty( $post->post_content_filtered ) ) { 2099 * $css = $post->post_content_filtered; 2100 * } 2101 * return $css; 2102 * }, 10, 2 ); 2103 * </code> 2104 * 2105 * @since 4.7.0 2106 * @param array $data { 2107 * Custom CSS data. 2108 * 2109 * @type string $css CSS stored in `post_content`. 2110 * @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. 2111 * Normally empty string. 2112 * } 2113 * @param array $args { 2114 * The args passed into `wp_update_custom_css_post()` merged with defaults. 2115 * 2116 * @type string $css The original CSS passed in to be updated. 2117 * @type string $preprocessed The original preprocessed CSS passed in to be updated. 2118 * @type string $stylesheet The stylesheet (theme) being updated. 2119 * } 2120 */ 2121 $data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) ); 2122 2123 $post_data = array( 2124 'post_title' => $args['stylesheet'], 2125 'post_name' => sanitize_title( $args['stylesheet'] ), 2126 'post_type' => 'custom_css', 2127 'post_status' => 'publish', 2128 'post_content' => $data['css'], 2129 'post_content_filtered' => $data['preprocessed'], 2130 ); 2131 2132 // Update post if it already exists, otherwise create a new one. 2133 $post = wp_get_custom_css_post( $args['stylesheet'] ); 2134 if ( $post ) { 2135 $post_data['ID'] = $post->ID; 2136 $r = wp_update_post( wp_slash( $post_data ), true ); 2137 } else { 2138 $r = wp_insert_post( wp_slash( $post_data ), true ); 2139 2140 if ( ! is_wp_error( $r ) ) { 2141 if ( get_stylesheet() === $args['stylesheet'] ) { 2142 set_theme_mod( 'custom_css_post_id', $r ); 2143 } 2144 2145 // Trigger creation of a revision. This should be removed once #30854 is resolved. 2146 $revisions = wp_get_latest_revision_id_and_total_count( $r ); 2147 if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) { 2148 wp_save_post_revision( $r ); 2149 } 2150 } 2151 } 2152 2153 if ( is_wp_error( $r ) ) { 2154 return $r; 2155 } 2156 return get_post( $r ); 2157 } 2158 2159 /** 2160 * Adds callback for custom TinyMCE editor stylesheets. 2161 * 2162 * The parameter $stylesheet is the name of the stylesheet, relative to 2163 * the theme root. It also accepts an array of stylesheets. 2164 * It is optional and defaults to 'editor-style.css'. 2165 * 2166 * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css. 2167 * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE. 2168 * If an array of stylesheets is passed to add_editor_style(), 2169 * RTL is only added for the first stylesheet. 2170 * 2171 * Since version 3.4 the TinyMCE body has .rtl CSS class. 2172 * It is a better option to use that class and add any RTL styles to the main stylesheet. 2173 * 2174 * @since 3.0.0 2175 * 2176 * @global array $editor_styles 2177 * 2178 * @param array|string $stylesheet Optional. Stylesheet name or array thereof, relative to theme root. 2179 * Defaults to 'editor-style.css' 2180 */ 2181 function add_editor_style( $stylesheet = 'editor-style.css' ) { 2182 global $editor_styles; 2183 2184 add_theme_support( 'editor-style' ); 2185 2186 $editor_styles = (array) $editor_styles; 2187 $stylesheet = (array) $stylesheet; 2188 2189 if ( is_rtl() ) { 2190 $rtl_stylesheet = str_replace( '.css', '-rtl.css', $stylesheet[0] ); 2191 $stylesheet[] = $rtl_stylesheet; 2192 } 2193 2194 $editor_styles = array_merge( $editor_styles, $stylesheet ); 2195 } 2196 2197 /** 2198 * Removes all visual editor stylesheets. 2199 * 2200 * @since 3.1.0 2201 * 2202 * @global array $editor_styles 2203 * 2204 * @return bool True on success, false if there were no stylesheets to remove. 2205 */ 2206 function remove_editor_styles() { 2207 if ( ! current_theme_supports( 'editor-style' ) ) { 2208 return false; 2209 } 2210 _remove_theme_support( 'editor-style' ); 2211 if ( is_admin() ) { 2212 $GLOBALS['editor_styles'] = array(); 2213 } 2214 return true; 2215 } 2216 2217 /** 2218 * Retrieves any registered editor stylesheet URLs. 2219 * 2220 * @since 4.0.0 2221 * 2222 * @global array $editor_styles Registered editor stylesheets 2223 * 2224 * @return string[] If registered, a list of editor stylesheet URLs. 2225 */ 2226 function get_editor_stylesheets() { 2227 $stylesheets = array(); 2228 // Load editor_style.css if the active theme supports it. 2229 if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) { 2230 $editor_styles = $GLOBALS['editor_styles']; 2231 2232 $editor_styles = array_unique( array_filter( $editor_styles ) ); 2233 $style_uri = get_stylesheet_directory_uri(); 2234 $style_dir = get_stylesheet_directory(); 2235 2236 // Support externally referenced styles (like, say, fonts). 2237 foreach ( $editor_styles as $key => $file ) { 2238 if ( preg_match( '~^(https?:)?//~', $file ) ) { 2239 $stylesheets[] = sanitize_url( $file ); 2240 unset( $editor_styles[ $key ] ); 2241 } 2242 } 2243 2244 // Look in a parent theme first, that way child theme CSS overrides. 2245 if ( is_child_theme() ) { 2246 $template_uri = get_template_directory_uri(); 2247 $template_dir = get_template_directory(); 2248 2249 foreach ( $editor_styles as $key => $file ) { 2250 if ( $file && file_exists( "$template_dir/$file" ) ) { 2251 $stylesheets[] = "$template_uri/$file"; 2252 } 2253 } 2254 } 2255 2256 foreach ( $editor_styles as $file ) { 2257 if ( $file && file_exists( "$style_dir/$file" ) ) { 2258 $stylesheets[] = "$style_uri/$file"; 2259 } 2260 } 2261 } 2262 2263 /** 2264 * Filters the array of URLs of stylesheets applied to the editor. 2265 * 2266 * @since 4.3.0 2267 * 2268 * @param string[] $stylesheets Array of URLs of stylesheets to be applied to the editor. 2269 */ 2270 return apply_filters( 'editor_stylesheets', $stylesheets ); 2271 } 2272 2273 /** 2274 * Expands a theme's starter content configuration using core-provided data. 2275 * 2276 * @since 4.7.0 2277 * 2278 * @return array Array of starter content. 2279 */ 2280 function get_theme_starter_content() { 2281 $theme_support = get_theme_support( 'starter-content' ); 2282 if ( is_array( $theme_support ) && ! empty( $theme_support[0] ) && is_array( $theme_support[0] ) ) { 2283 $config = $theme_support[0]; 2284 } else { 2285 $config = array(); 2286 } 2287 2288 $core_content = array( 2289 'widgets' => array( 2290 'text_business_info' => array( 2291 'text', 2292 array( 2293 'title' => _x( 'Find Us', 'Theme starter content' ), 2294 'text' => implode( 2295 '', 2296 array( 2297 '<strong>' . _x( 'Address', 'Theme starter content' ) . "</strong>\n", 2298 _x( '123 Main Street', 'Theme starter content' ) . "\n", 2299 _x( 'New York, NY 10001', 'Theme starter content' ) . "\n\n", 2300 '<strong>' . _x( 'Hours', 'Theme starter content' ) . "</strong>\n", 2301 _x( 'Monday–Friday: 9:00AM–5:00PM', 'Theme starter content' ) . "\n", 2302 _x( 'Saturday & Sunday: 11:00AM–3:00PM', 'Theme starter content' ), 2303 ) 2304 ), 2305 'filter' => true, 2306 'visual' => true, 2307 ), 2308 ), 2309 'text_about' => array( 2310 'text', 2311 array( 2312 'title' => _x( 'About This Site', 'Theme starter content' ), 2313 'text' => _x( 'This may be a good place to introduce yourself and your site or include some credits.', 'Theme starter content' ), 2314 'filter' => true, 2315 'visual' => true, 2316 ), 2317 ), 2318 'archives' => array( 2319 'archives', 2320 array( 2321 'title' => _x( 'Archives', 'Theme starter content' ), 2322 ), 2323 ), 2324 'calendar' => array( 2325 'calendar', 2326 array( 2327 'title' => _x( 'Calendar', 'Theme starter content' ), 2328 ), 2329 ), 2330 'categories' => array( 2331 'categories', 2332 array( 2333 'title' => _x( 'Categories', 'Theme starter content' ), 2334 ), 2335 ), 2336 'meta' => array( 2337 'meta', 2338 array( 2339 'title' => _x( 'Meta', 'Theme starter content' ), 2340 ), 2341 ), 2342 'recent-comments' => array( 2343 'recent-comments', 2344 array( 2345 'title' => _x( 'Recent Comments', 'Theme starter content' ), 2346 ), 2347 ), 2348 'recent-posts' => array( 2349 'recent-posts', 2350 array( 2351 'title' => _x( 'Recent Posts', 'Theme starter content' ), 2352 ), 2353 ), 2354 'search' => array( 2355 'search', 2356 array( 2357 'title' => _x( 'Search', 'Theme starter content' ), 2358 ), 2359 ), 2360 ), 2361 'nav_menus' => array( 2362 'link_home' => array( 2363 'type' => 'custom', 2364 'title' => _x( 'Home', 'Theme starter content' ), 2365 'url' => home_url( '/' ), 2366 ), 2367 'page_home' => array( // Deprecated in favor of 'link_home'. 2368 'type' => 'post_type', 2369 'object' => 'page', 2370 'object_id' => '{{home}}', 2371 ), 2372 'page_about' => array( 2373 'type' => 'post_type', 2374 'object' => 'page', 2375 'object_id' => '{{about}}', 2376 ), 2377 'page_blog' => array( 2378 'type' => 'post_type', 2379 'object' => 'page', 2380 'object_id' => '{{blog}}', 2381 ), 2382 'page_news' => array( 2383 'type' => 'post_type', 2384 'object' => 'page', 2385 'object_id' => '{{news}}', 2386 ), 2387 'page_contact' => array( 2388 'type' => 'post_type', 2389 'object' => 'page', 2390 'object_id' => '{{contact}}', 2391 ), 2392 2393 'link_email' => array( 2394 'title' => _x( 'Email', 'Theme starter content' ), 2395 'url' => 'mailto:wordpress@example.com', 2396 ), 2397 'link_facebook' => array( 2398 'title' => _x( 'Facebook', 'Theme starter content' ), 2399 'url' => 'https://www.facebook.com/wordpress', 2400 ), 2401 'link_foursquare' => array( 2402 'title' => _x( 'Foursquare', 'Theme starter content' ), 2403 'url' => 'https://foursquare.com/', 2404 ), 2405 'link_github' => array( 2406 'title' => _x( 'GitHub', 'Theme starter content' ), 2407 'url' => 'https://github.com/wordpress/', 2408 ), 2409 'link_instagram' => array( 2410 'title' => _x( 'Instagram', 'Theme starter content' ), 2411 'url' => 'https://www.instagram.com/explore/tags/wordcamp/', 2412 ), 2413 'link_linkedin' => array( 2414 'title' => _x( 'LinkedIn', 'Theme starter content' ), 2415 'url' => 'https://www.linkedin.com/company/1089783', 2416 ), 2417 'link_pinterest' => array( 2418 'title' => _x( 'Pinterest', 'Theme starter content' ), 2419 'url' => 'https://www.pinterest.com/', 2420 ), 2421 'link_twitter' => array( 2422 'title' => _x( 'Twitter', 'Theme starter content' ), 2423 'url' => 'https://twitter.com/wordpress', 2424 ), 2425 'link_yelp' => array( 2426 'title' => _x( 'Yelp', 'Theme starter content' ), 2427 'url' => 'https://www.yelp.com', 2428 ), 2429 'link_youtube' => array( 2430 'title' => _x( 'YouTube', 'Theme starter content' ), 2431 'url' => 'https://www.youtube.com/channel/UCdof4Ju7amm1chz1gi1T2ZA', 2432 ), 2433 ), 2434 'posts' => array( 2435 'home' => array( 2436 'post_type' => 'page', 2437 'post_title' => _x( 'Home', 'Theme starter content' ), 2438 'post_content' => sprintf( 2439 "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->", 2440 _x( 'Welcome to your site! This is your homepage, which is what most visitors will see when they come to your site for the first time.', 'Theme starter content' ) 2441 ), 2442 ), 2443 'about' => array( 2444 'post_type' => 'page', 2445 'post_title' => _x( 'About', 'Theme starter content' ), 2446 'post_content' => sprintf( 2447 "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->", 2448 _x( 'You might be an artist who would like to introduce yourself and your work here or maybe you are a business with a mission to describe.', 'Theme starter content' ) 2449 ), 2450 ), 2451 'contact' => array( 2452 'post_type' => 'page', 2453 'post_title' => _x( 'Contact', 'Theme starter content' ), 2454 'post_content' => sprintf( 2455 "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->", 2456 _x( 'This is a page with some basic contact information, such as an address and phone number. You might also try a plugin to add a contact form.', 'Theme starter content' ) 2457 ), 2458 ), 2459 'blog' => array( 2460 'post_type' => 'page', 2461 'post_title' => _x( 'Blog', 'Theme starter content' ), 2462 ), 2463 'news' => array( 2464 'post_type' => 'page', 2465 'post_title' => _x( 'News', 'Theme starter content' ), 2466 ), 2467 2468 'homepage-section' => array( 2469 'post_type' => 'page', 2470 'post_title' => _x( 'A homepage section', 'Theme starter content' ), 2471 'post_content' => sprintf( 2472 "<!-- wp:paragraph -->\n<p>%s</p>\n<!-- /wp:paragraph -->", 2473 _x( 'This is an example of a homepage section. Homepage sections can be any page other than the homepage itself, including the page that shows your latest blog posts.', 'Theme starter content' ) 2474 ), 2475 ), 2476 ), 2477 ); 2478 2479 $content = array(); 2480 2481 foreach ( $config as $type => $args ) { 2482 switch ( $type ) { 2483 // Use options and theme_mods as-is. 2484 case 'options': 2485 case 'theme_mods': 2486 $content[ $type ] = $config[ $type ]; 2487 break; 2488 2489 // Widgets are grouped into sidebars. 2490 case 'widgets': 2491 foreach ( $config[ $type ] as $sidebar_id => $widgets ) { 2492 foreach ( $widgets as $id => $widget ) { 2493 if ( is_array( $widget ) ) { 2494 2495 // Item extends core content. 2496 if ( ! empty( $core_content[ $type ][ $id ] ) ) { 2497 $widget = array( 2498 $core_content[ $type ][ $id ][0], 2499 array_merge( $core_content[ $type ][ $id ][1], $widget ), 2500 ); 2501 } 2502 2503 $content[ $type ][ $sidebar_id ][] = $widget; 2504 } elseif ( is_string( $widget ) 2505 && ! empty( $core_content[ $type ] ) 2506 && ! empty( $core_content[ $type ][ $widget ] ) 2507 ) { 2508 $content[ $type ][ $sidebar_id ][] = $core_content[ $type ][ $widget ]; 2509 } 2510 } 2511 } 2512 break; 2513 2514 // And nav menu items are grouped into nav menus. 2515 case 'nav_menus': 2516 foreach ( $config[ $type ] as $nav_menu_location => $nav_menu ) { 2517 2518 // Ensure nav menus get a name. 2519 if ( empty( $nav_menu['name'] ) ) { 2520 $nav_menu['name'] = $nav_menu_location; 2521 } 2522 2523 $content[ $type ][ $nav_menu_location ]['name'] = $nav_menu['name']; 2524 2525 foreach ( $nav_menu['items'] as $id => $nav_menu_item ) { 2526 if ( is_array( $nav_menu_item ) ) { 2527 2528 // Item extends core content. 2529 if ( ! empty( $core_content[ $type ][ $id ] ) ) { 2530 $nav_menu_item = array_merge( $core_content[ $type ][ $id ], $nav_menu_item ); 2531 } 2532 2533 $content[ $type ][ $nav_menu_location ]['items'][] = $nav_menu_item; 2534 } elseif ( is_string( $nav_menu_item ) 2535 && ! empty( $core_content[ $type ] ) 2536 && ! empty( $core_content[ $type ][ $nav_menu_item ] ) 2537 ) { 2538 $content[ $type ][ $nav_menu_location ]['items'][] = $core_content[ $type ][ $nav_menu_item ]; 2539 } 2540 } 2541 } 2542 break; 2543 2544 // Attachments are posts but have special treatment. 2545 case 'attachments': 2546 foreach ( $config[ $type ] as $id => $item ) { 2547 if ( ! empty( $item['file'] ) ) { 2548 $content[ $type ][ $id ] = $item; 2549 } 2550 } 2551 break; 2552 2553 /* 2554 * All that's left now are posts (besides attachments). 2555 * Not a default case for the sake of clarity and future work. 2556 */ 2557 case 'posts': 2558 foreach ( $config[ $type ] as $id => $item ) { 2559 if ( is_array( $item ) ) { 2560 2561 // Item extends core content. 2562 if ( ! empty( $core_content[ $type ][ $id ] ) ) { 2563 $item = array_merge( $core_content[ $type ][ $id ], $item ); 2564 } 2565 2566 // Enforce a subset of fields. 2567 $content[ $type ][ $id ] = wp_array_slice_assoc( 2568 $item, 2569 array( 2570 'post_type', 2571 'post_title', 2572 'post_excerpt', 2573 'post_name', 2574 'post_content', 2575 'menu_order', 2576 'comment_status', 2577 'thumbnail', 2578 'template', 2579 ) 2580 ); 2581 } elseif ( is_string( $item ) && ! empty( $core_content[ $type ][ $item ] ) ) { 2582 $content[ $type ][ $item ] = $core_content[ $type ][ $item ]; 2583 } 2584 } 2585 break; 2586 } 2587 } 2588 2589 /** 2590 * Filters the expanded array of starter content. 2591 * 2592 * @since 4.7.0 2593 * 2594 * @param array $content Array of starter content. 2595 * @param array $config Array of theme-specific starter content configuration. 2596 */ 2597 return apply_filters( 'get_theme_starter_content', $content, $config ); 2598 } 2599 2600 /** 2601 * Registers theme support for a given feature. 2602 * 2603 * Must be called in the theme's functions.php file to work. 2604 * If attached to a hook, it must be {@see 'after_setup_theme'}. 2605 * The {@see 'init'} hook may be too late for some features. 2606 * 2607 * Example usage: 2608 * 2609 * add_theme_support( 'title-tag' ); 2610 * add_theme_support( 'custom-logo', array( 2611 * 'height' => 480, 2612 * 'width' => 720, 2613 * ) ); 2614 * 2615 * @since 2.9.0 2616 * @since 3.4.0 The `custom-header-uploads` feature was deprecated. 2617 * @since 3.6.0 The `html5` feature was added. 2618 * @since 3.6.1 The `html5` feature requires an array of types to be passed. Defaults to 2619 * 'comment-list', 'comment-form', 'search-form' for backward compatibility. 2620 * @since 3.9.0 The `html5` feature now also accepts 'gallery' and 'caption'. 2621 * @since 4.1.0 The `title-tag` feature was added. 2622 * @since 4.5.0 The `customize-selective-refresh-widgets` feature was added. 2623 * @since 4.7.0 The `starter-content` feature was added. 2624 * @since 5.0.0 The `responsive-embeds`, `align-wide`, `dark-editor-style`, `disable-custom-colors`, 2625 * `disable-custom-font-sizes`, `editor-color-palette`, `editor-font-sizes`, 2626 * `editor-styles`, and `wp-block-styles` features were added. 2627 * @since 5.3.0 The `html5` feature now also accepts 'script' and 'style'. 2628 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 2629 * by adding it to the function signature. 2630 * @since 5.4.0 The `disable-custom-gradients` feature limits to default gradients or gradients added 2631 * through `editor-gradient-presets` theme support. 2632 * @since 5.5.0 The `core-block-patterns` feature was added and is enabled by default. 2633 * @since 5.5.0 The `custom-logo` feature now also accepts 'unlink-homepage-logo'. 2634 * @since 5.6.0 The `post-formats` feature warns if no array is passed as the second parameter. 2635 * @since 5.8.0 The `widgets-block-editor` feature enables the Widgets block editor. 2636 * @since 5.8.0 The `block-templates` feature indicates whether a theme uses block-based templates. 2637 * @since 6.0.0 The `html5` feature warns if no array is passed as the second parameter. 2638 * @since 6.1.0 The `block-template-parts` feature allows to edit any reusable template part from site editor. 2639 * @since 6.1.0 The `disable-layout-styles` feature disables the default layout styles. 2640 * @since 6.3.0 The `link-color` feature allows to enable the link color setting. 2641 * @since 6.3.0 The `border` feature allows themes without theme.json to add border styles to blocks. 2642 * @since 6.5.0 The `appearance-tools` feature enables a few design tools for blocks, 2643 * see `WP_Theme_JSON::APPEARANCE_TOOLS_OPT_INS` for a complete list. 2644 * @since 6.6.0 The `editor-spacing-sizes` feature was added. 2645 * 2646 * @global array $_wp_theme_features 2647 * 2648 * @param string $feature The feature being added. Likely core values include: 2649 * - 'admin-bar' 2650 * - 'align-wide' 2651 * - 'appearance-tools' 2652 * - 'automatic-feed-links' 2653 * - 'block-templates' 2654 * - 'block-template-parts' 2655 * - 'border' 2656 * - 'core-block-patterns' 2657 * - 'custom-background' 2658 * - 'custom-header' 2659 * - 'custom-line-height' 2660 * - 'custom-logo' 2661 * - 'customize-selective-refresh-widgets' 2662 * - 'custom-spacing' 2663 * - 'custom-units' 2664 * - 'dark-editor-style' 2665 * - 'disable-custom-colors' 2666 * - 'disable-custom-font-sizes' 2667 * - 'disable-custom-gradients' 2668 * - 'disable-layout-styles' 2669 * - 'editor-color-palette' 2670 * - 'editor-gradient-presets' 2671 * - 'editor-font-sizes' 2672 * - 'editor-spacing-sizes' 2673 * - 'editor-styles' 2674 * - 'featured-content' 2675 * - 'html5' 2676 * - 'link-color' 2677 * - 'menus' 2678 * - 'post-formats' 2679 * - 'post-thumbnails' 2680 * - 'responsive-embeds' 2681 * - 'starter-content' 2682 * - 'title-tag' 2683 * - 'widgets' 2684 * - 'widgets-block-editor' 2685 * - 'wp-block-styles' 2686 * @param mixed ...$args Optional extra arguments to pass along with certain features. 2687 * @return void|false Void on success, false on failure. 2688 */ 2689 function add_theme_support( $feature, ...$args ) { 2690 global $_wp_theme_features; 2691 2692 if ( ! $args ) { 2693 $args = true; 2694 } 2695 2696 switch ( $feature ) { 2697 case 'post-thumbnails': 2698 // All post types are already supported. 2699 if ( true === get_theme_support( 'post-thumbnails' ) ) { 2700 return; 2701 } 2702 2703 /* 2704 * Merge post types with any that already declared their support 2705 * for post thumbnails. 2706 */ 2707 if ( isset( $args[0] ) && is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) { 2708 $args[0] = array_unique( array_merge( $_wp_theme_features['post-thumbnails'][0], $args[0] ) ); 2709 } 2710 2711 break; 2712 2713 case 'post-formats': 2714 if ( isset( $args[0] ) && is_array( $args[0] ) ) { 2715 $post_formats = get_post_format_slugs(); 2716 unset( $post_formats['standard'] ); 2717 2718 $args[0] = array_intersect( $args[0], array_keys( $post_formats ) ); 2719 } else { 2720 _doing_it_wrong( 2721 "add_theme_support( 'post-formats' )", 2722 __( 'You need to pass an array of post formats.' ), 2723 '5.6.0' 2724 ); 2725 return false; 2726 } 2727 break; 2728 2729 case 'html5': 2730 // You can't just pass 'html5', you need to pass an array of types. 2731 if ( empty( $args[0] ) || ! is_array( $args[0] ) ) { 2732 _doing_it_wrong( 2733 "add_theme_support( 'html5' )", 2734 __( 'You need to pass an array of types.' ), 2735 '3.6.1' 2736 ); 2737 2738 if ( ! empty( $args[0] ) && ! is_array( $args[0] ) ) { 2739 return false; 2740 } 2741 2742 // Build an array of types for back-compat. 2743 $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) ); 2744 } 2745 2746 // Calling 'html5' again merges, rather than overwrites. 2747 if ( isset( $_wp_theme_features['html5'] ) ) { 2748 $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] ); 2749 } 2750 break; 2751 2752 case 'custom-logo': 2753 if ( true === $args ) { 2754 $args = array( 0 => array() ); 2755 } 2756 $defaults = array( 2757 'width' => null, 2758 'height' => null, 2759 'flex-width' => false, 2760 'flex-height' => false, 2761 'header-text' => '', 2762 'unlink-homepage-logo' => false, 2763 ); 2764 $args[0] = wp_parse_args( array_intersect_key( $args[0], $defaults ), $defaults ); 2765 2766 // Allow full flexibility if no size is specified. 2767 if ( is_null( $args[0]['width'] ) && is_null( $args[0]['height'] ) ) { 2768 $args[0]['flex-width'] = true; 2769 $args[0]['flex-height'] = true; 2770 } 2771 break; 2772 2773 case 'custom-header-uploads': 2774 return add_theme_support( 'custom-header', array( 'uploads' => true ) ); 2775 2776 case 'custom-header': 2777 if ( true === $args ) { 2778 $args = array( 0 => array() ); 2779 } 2780 2781 $defaults = array( 2782 'default-image' => '', 2783 'random-default' => false, 2784 'width' => 0, 2785 'height' => 0, 2786 'flex-height' => false, 2787 'flex-width' => false, 2788 'default-text-color' => '', 2789 'header-text' => true, 2790 'uploads' => true, 2791 'wp-head-callback' => '', 2792 'admin-head-callback' => '', 2793 'admin-preview-callback' => '', 2794 'video' => false, 2795 'video-active-callback' => 'is_front_page', 2796 ); 2797 2798 $jit = isset( $args[0]['__jit'] ); 2799 unset( $args[0]['__jit'] ); 2800 2801 /* 2802 * Merge in data from previous add_theme_support() calls. 2803 * The first value registered wins. (A child theme is set up first.) 2804 */ 2805 if ( isset( $_wp_theme_features['custom-header'] ) ) { 2806 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] ); 2807 } 2808 2809 /* 2810 * Load in the defaults at the end, as we need to insure first one wins. 2811 * This will cause all constants to be defined, as each arg will then be set to the default. 2812 */ 2813 if ( $jit ) { 2814 $args[0] = wp_parse_args( $args[0], $defaults ); 2815 } 2816 2817 /* 2818 * If a constant was defined, use that value. Otherwise, define the constant to ensure 2819 * the constant is always accurate (and is not defined later, overriding our value). 2820 * As stated above, the first value wins. 2821 * Once we get to wp_loaded (just-in-time), define any constants we haven't already. 2822 * Constants should be avoided. Don't reference them. This is just for backward compatibility. 2823 */ 2824 2825 if ( defined( 'NO_HEADER_TEXT' ) ) { 2826 $args[0]['header-text'] = ! NO_HEADER_TEXT; 2827 } elseif ( isset( $args[0]['header-text'] ) ) { 2828 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) ); 2829 } 2830 2831 if ( defined( 'HEADER_IMAGE_WIDTH' ) ) { 2832 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH; 2833 } elseif ( isset( $args[0]['width'] ) ) { 2834 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] ); 2835 } 2836 2837 if ( defined( 'HEADER_IMAGE_HEIGHT' ) ) { 2838 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT; 2839 } elseif ( isset( $args[0]['height'] ) ) { 2840 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] ); 2841 } 2842 2843 if ( defined( 'HEADER_TEXTCOLOR' ) ) { 2844 $args[0]['default-text-color'] = HEADER_TEXTCOLOR; 2845 } elseif ( isset( $args[0]['default-text-color'] ) ) { 2846 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] ); 2847 } 2848 2849 if ( defined( 'HEADER_IMAGE' ) ) { 2850 $args[0]['default-image'] = HEADER_IMAGE; 2851 } elseif ( isset( $args[0]['default-image'] ) ) { 2852 define( 'HEADER_IMAGE', $args[0]['default-image'] ); 2853 } 2854 2855 if ( $jit && ! empty( $args[0]['default-image'] ) ) { 2856 $args[0]['random-default'] = false; 2857 } 2858 2859 /* 2860 * If headers are supported, and we still don't have a defined width or height, 2861 * we have implicit flex sizes. 2862 */ 2863 if ( $jit ) { 2864 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) ) { 2865 $args[0]['flex-width'] = true; 2866 } 2867 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) ) { 2868 $args[0]['flex-height'] = true; 2869 } 2870 } 2871 2872 break; 2873 2874 case 'custom-background': 2875 if ( true === $args ) { 2876 $args = array( 0 => array() ); 2877 } 2878 2879 $defaults = array( 2880 'default-image' => '', 2881 'default-preset' => 'default', 2882 'default-position-x' => 'left', 2883 'default-position-y' => 'top', 2884 'default-size' => 'auto', 2885 'default-repeat' => 'repeat', 2886 'default-attachment' => 'scroll', 2887 'default-color' => '', 2888 'wp-head-callback' => '_custom_background_cb', 2889 'admin-head-callback' => '', 2890 'admin-preview-callback' => '', 2891 ); 2892 2893 $jit = isset( $args[0]['__jit'] ); 2894 unset( $args[0]['__jit'] ); 2895 2896 // Merge in data from previous add_theme_support() calls. The first value registered wins. 2897 if ( isset( $_wp_theme_features['custom-background'] ) ) { 2898 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] ); 2899 } 2900 2901 if ( $jit ) { 2902 $args[0] = wp_parse_args( $args[0], $defaults ); 2903 } 2904 2905 if ( defined( 'BACKGROUND_COLOR' ) ) { 2906 $args[0]['default-color'] = BACKGROUND_COLOR; 2907 } elseif ( isset( $args[0]['default-color'] ) || $jit ) { 2908 define( 'BACKGROUND_COLOR', $args[0]['default-color'] ); 2909 } 2910 2911 if ( defined( 'BACKGROUND_IMAGE' ) ) { 2912 $args[0]['default-image'] = BACKGROUND_IMAGE; 2913 } elseif ( isset( $args[0]['default-image'] ) || $jit ) { 2914 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] ); 2915 } 2916 2917 break; 2918 2919 // Ensure that 'title-tag' is accessible in the admin. 2920 case 'title-tag': 2921 // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php. 2922 if ( did_action( 'wp_loaded' ) ) { 2923 _doing_it_wrong( 2924 "add_theme_support( 'title-tag' )", 2925 sprintf( 2926 /* translators: 1: title-tag, 2: wp_loaded */ 2927 __( 'Theme support for %1$s should be registered before the %2$s hook.' ), 2928 '<code>title-tag</code>', 2929 '<code>wp_loaded</code>' 2930 ), 2931 '4.1.0' 2932 ); 2933 2934 return false; 2935 } 2936 } 2937 2938 $_wp_theme_features[ $feature ] = $args; 2939 } 2940 2941 /** 2942 * Registers the internal custom header and background routines. 2943 * 2944 * @since 3.4.0 2945 * @access private 2946 * 2947 * @global Custom_Image_Header $custom_image_header 2948 * @global Custom_Background $custom_background 2949 */ 2950 function _custom_header_background_just_in_time() { 2951 global $custom_image_header, $custom_background; 2952 2953 if ( current_theme_supports( 'custom-header' ) ) { 2954 // In case any constants were defined after an add_custom_image_header() call, re-run. 2955 add_theme_support( 'custom-header', array( '__jit' => true ) ); 2956 2957 $args = get_theme_support( 'custom-header' ); 2958 if ( $args[0]['wp-head-callback'] ) { 2959 add_action( 'wp_head', $args[0]['wp-head-callback'] ); 2960 } 2961 2962 if ( is_admin() ) { 2963 require_once ABSPATH . 'wp-admin/includes/class-custom-image-header.php'; 2964 $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] ); 2965 } 2966 } 2967 2968 if ( current_theme_supports( 'custom-background' ) ) { 2969 // In case any constants were defined after an add_custom_background() call, re-run. 2970 add_theme_support( 'custom-background', array( '__jit' => true ) ); 2971 2972 $args = get_theme_support( 'custom-background' ); 2973 add_action( 'wp_head', $args[0]['wp-head-callback'] ); 2974 2975 if ( is_admin() ) { 2976 require_once ABSPATH . 'wp-admin/includes/class-custom-background.php'; 2977 $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] ); 2978 } 2979 } 2980 } 2981 2982 /** 2983 * Adds CSS to hide header text for custom logo, based on Customizer setting. 2984 * 2985 * @since 4.5.0 2986 * @access private 2987 */ 2988 function _custom_logo_header_styles() { 2989 if ( ! current_theme_supports( 'custom-header', 'header-text' ) 2990 && get_theme_support( 'custom-logo', 'header-text' ) 2991 && ! get_theme_mod( 'header_text', true ) 2992 ) { 2993 $classes = (array) get_theme_support( 'custom-logo', 'header-text' ); 2994 $classes = array_map( 'sanitize_html_class', $classes ); 2995 $classes = '.' . implode( ', .', $classes ); 2996 2997 $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; 2998 ?> 2999 <!-- Custom Logo: hide header text --> 3000 <style id="custom-logo-css"<?php echo $type_attr; ?>> 3001 <?php echo $classes; ?> { 3002 position: absolute; 3003 clip-path: inset(50%); 3004 } 3005 </style> 3006 <?php 3007 } 3008 } 3009 3010 /** 3011 * Gets the theme support arguments passed when registering that support. 3012 * 3013 * Example usage: 3014 * 3015 * get_theme_support( 'custom-logo' ); 3016 * get_theme_support( 'custom-header', 'width' ); 3017 * 3018 * @since 3.1.0 3019 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 3020 * by adding it to the function signature. 3021 * 3022 * @global array $_wp_theme_features 3023 * 3024 * @param string $feature The feature to check. See add_theme_support() for the list 3025 * of possible values. 3026 * @param mixed ...$args Optional extra arguments to be checked against certain features. 3027 * @return mixed The array of extra arguments or the value for the registered feature. 3028 */ 3029 function get_theme_support( $feature, ...$args ) { 3030 global $_wp_theme_features; 3031 3032 if ( ! isset( $_wp_theme_features[ $feature ] ) ) { 3033 return false; 3034 } 3035 3036 if ( ! $args ) { 3037 return $_wp_theme_features[ $feature ]; 3038 } 3039 3040 switch ( $feature ) { 3041 case 'custom-logo': 3042 case 'custom-header': 3043 case 'custom-background': 3044 if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) ) { 3045 return $_wp_theme_features[ $feature ][0][ $args[0] ]; 3046 } 3047 return false; 3048 3049 default: 3050 return $_wp_theme_features[ $feature ]; 3051 } 3052 } 3053 3054 /** 3055 * Allows a theme to de-register its support of a certain feature 3056 * 3057 * Should be called in the theme's functions.php file. Generally would 3058 * be used for child themes to override support from the parent theme. 3059 * 3060 * @since 3.0.0 3061 * 3062 * @see add_theme_support() 3063 * 3064 * @param string $feature The feature being removed. See add_theme_support() for the list 3065 * of possible values. 3066 * @return bool|void Whether feature was removed. 3067 */ 3068 function remove_theme_support( $feature ) { 3069 // Do not remove internal registrations that are not used directly by themes. 3070 if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ), true ) ) { 3071 return false; 3072 } 3073 3074 return _remove_theme_support( $feature ); 3075 } 3076 3077 /** 3078 * Do not use. Removes theme support internally without knowledge of those not used 3079 * by themes directly. 3080 * 3081 * @access private 3082 * @since 3.1.0 3083 * @global array $_wp_theme_features 3084 * @global Custom_Image_Header $custom_image_header 3085 * @global Custom_Background $custom_background 3086 * 3087 * @param string $feature The feature being removed. See add_theme_support() for the list 3088 * of possible values. 3089 * @return bool True if support was removed, false if the feature was not registered. 3090 */ 3091 function _remove_theme_support( $feature ) { 3092 global $_wp_theme_features; 3093 3094 switch ( $feature ) { 3095 case 'custom-header-uploads': 3096 if ( ! isset( $_wp_theme_features['custom-header'] ) ) { 3097 return false; 3098 } 3099 add_theme_support( 'custom-header', array( 'uploads' => false ) ); 3100 return; // Do not continue - custom-header-uploads no longer exists. 3101 } 3102 3103 if ( ! isset( $_wp_theme_features[ $feature ] ) ) { 3104 return false; 3105 } 3106 3107 switch ( $feature ) { 3108 case 'custom-header': 3109 if ( ! did_action( 'wp_loaded' ) ) { 3110 break; 3111 } 3112 $support = get_theme_support( 'custom-header' ); 3113 if ( isset( $support[0]['wp-head-callback'] ) ) { 3114 remove_action( 'wp_head', $support[0]['wp-head-callback'] ); 3115 } 3116 if ( isset( $GLOBALS['custom_image_header'] ) ) { 3117 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) ); 3118 unset( $GLOBALS['custom_image_header'] ); 3119 } 3120 break; 3121 3122 case 'custom-background': 3123 if ( ! did_action( 'wp_loaded' ) ) { 3124 break; 3125 } 3126 $support = get_theme_support( 'custom-background' ); 3127 if ( isset( $support[0]['wp-head-callback'] ) ) { 3128 remove_action( 'wp_head', $support[0]['wp-head-callback'] ); 3129 } 3130 remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) ); 3131 unset( $GLOBALS['custom_background'] ); 3132 break; 3133 } 3134 3135 unset( $_wp_theme_features[ $feature ] ); 3136 3137 return true; 3138 } 3139 3140 /** 3141 * Checks a theme's support for a given feature. 3142 * 3143 * Example usage: 3144 * 3145 * current_theme_supports( 'custom-logo' ); 3146 * current_theme_supports( 'html5', 'comment-form' ); 3147 * 3148 * @since 2.9.0 3149 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 3150 * by adding it to the function signature. 3151 * 3152 * @global array $_wp_theme_features 3153 * 3154 * @param string $feature The feature being checked. See add_theme_support() for the list 3155 * of possible values. 3156 * @param mixed ...$args Optional extra arguments to be checked against certain features. 3157 * @return bool True if the active theme supports the feature, false otherwise. 3158 */ 3159 function current_theme_supports( $feature, ...$args ) { 3160 global $_wp_theme_features; 3161 3162 if ( 'custom-header-uploads' === $feature ) { 3163 return current_theme_supports( 'custom-header', 'uploads' ); 3164 } 3165 3166 if ( ! isset( $_wp_theme_features[ $feature ] ) ) { 3167 return false; 3168 } 3169 3170 // If no args passed then no extra checks need to be performed. 3171 if ( ! $args ) { 3172 /** This filter is documented in wp-includes/theme.php */ 3173 return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 3174 } 3175 3176 switch ( $feature ) { 3177 case 'post-thumbnails': 3178 /* 3179 * post-thumbnails can be registered for only certain content/post types 3180 * by passing an array of types to add_theme_support(). 3181 * If no array was passed, then any type is accepted. 3182 */ 3183 if ( true === $_wp_theme_features[ $feature ] ) { // Registered for all types. 3184 return true; 3185 } 3186 $content_type = $args[0]; 3187 return in_array( $content_type, $_wp_theme_features[ $feature ][0], true ); 3188 3189 case 'html5': 3190 case 'post-formats': 3191 /* 3192 * Specific post formats can be registered by passing an array of types 3193 * to add_theme_support(). 3194 * 3195 * Specific areas of HTML5 support *must* be passed via an array to add_theme_support(). 3196 */ 3197 $type = $args[0]; 3198 return in_array( $type, $_wp_theme_features[ $feature ][0], true ); 3199 3200 case 'custom-logo': 3201 case 'custom-header': 3202 case 'custom-background': 3203 // Specific capabilities can be registered by passing an array to add_theme_support(). 3204 return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] ); 3205 } 3206 3207 /** 3208 * Filters whether the active theme supports a specific feature. 3209 * 3210 * The dynamic portion of the hook name, `$feature`, refers to the specific 3211 * theme feature. See add_theme_support() for the list of possible values. 3212 * 3213 * @since 3.4.0 3214 * 3215 * @param bool $supports Whether the active theme supports the given feature. Default true. 3216 * @param array $args Array of arguments for the feature. 3217 * @param string $feature The theme feature. 3218 */ 3219 return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 3220 } 3221 3222 /** 3223 * Checks a theme's support for a given feature before loading the functions which implement it. 3224 * 3225 * @since 2.9.0 3226 * 3227 * @param string $feature The feature being checked. See add_theme_support() for the list 3228 * of possible values. 3229 * @param string $file Path to the file. 3230 * @return bool True if the active theme supports the supplied feature, false otherwise. 3231 */ 3232 function require_if_theme_supports( $feature, $file ) { 3233 if ( current_theme_supports( $feature ) ) { 3234 require $file; 3235 return true; 3236 } 3237 return false; 3238 } 3239 3240 /** 3241 * Registers a theme feature for use in add_theme_support(). 3242 * 3243 * This does not indicate that the active theme supports the feature, it only describes 3244 * the feature's supported options. 3245 * 3246 * @since 5.5.0 3247 * 3248 * @see add_theme_support() 3249 * 3250 * @global array $_wp_registered_theme_features 3251 * 3252 * @param string $feature The name uniquely identifying the feature. See add_theme_support() 3253 * for the list of possible values. 3254 * @param array $args { 3255 * Data used to describe the theme. 3256 * 3257 * @type string $type The type of data associated with this feature. 3258 * Valid values are 'string', 'boolean', 'integer', 3259 * 'number', 'array', and 'object'. Defaults to 'boolean'. 3260 * @type bool $variadic Does this feature utilize the variadic support 3261 * of add_theme_support(), or are all arguments specified 3262 * as the second parameter. Must be used with the "array" type. 3263 * @type string $description A short description of the feature. Included in 3264 * the Themes REST API schema. Intended for developers. 3265 * @type bool|array $show_in_rest { 3266 * Whether this feature should be included in the Themes REST API endpoint. 3267 * Defaults to not being included. When registering an 'array' or 'object' type, 3268 * this argument must be an array with the 'schema' key. 3269 * 3270 * @type array $schema Specifies the JSON Schema definition describing 3271 * the feature. If any objects in the schema do not include 3272 * the 'additionalProperties' keyword, it is set to false. 3273 * @type string $name An alternate name to be used as the property name 3274 * in the REST API. 3275 * @type callable $prepare_callback A function used to format the theme support in the REST API. 3276 * Receives the raw theme support value. 3277 * } 3278 * } 3279 * @return true|WP_Error True if the theme feature was successfully registered, a WP_Error object if not. 3280 */ 3281 function register_theme_feature( $feature, $args = array() ) { 3282 global $_wp_registered_theme_features; 3283 3284 if ( ! is_array( $_wp_registered_theme_features ) ) { 3285 $_wp_registered_theme_features = array(); 3286 } 3287 3288 $defaults = array( 3289 'type' => 'boolean', 3290 'variadic' => false, 3291 'description' => '', 3292 'show_in_rest' => false, 3293 ); 3294 3295 $args = wp_parse_args( $args, $defaults ); 3296 3297 if ( true === $args['show_in_rest'] ) { 3298 $args['show_in_rest'] = array(); 3299 } 3300 3301 if ( is_array( $args['show_in_rest'] ) ) { 3302 $args['show_in_rest'] = wp_parse_args( 3303 $args['show_in_rest'], 3304 array( 3305 'schema' => array(), 3306 'name' => $feature, 3307 'prepare_callback' => null, 3308 ) 3309 ); 3310 } 3311 3312 if ( ! in_array( $args['type'], array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ), true ) ) { 3313 return new WP_Error( 3314 'invalid_type', 3315 __( 'The feature "type" is not valid JSON Schema type.' ) 3316 ); 3317 } 3318 3319 if ( true === $args['variadic'] && 'array' !== $args['type'] ) { 3320 return new WP_Error( 3321 'variadic_must_be_array', 3322 __( 'When registering a "variadic" theme feature, the "type" must be an "array".' ) 3323 ); 3324 } 3325 3326 if ( false !== $args['show_in_rest'] && in_array( $args['type'], array( 'array', 'object' ), true ) ) { 3327 if ( ! is_array( $args['show_in_rest'] ) || empty( $args['show_in_rest']['schema'] ) ) { 3328 return new WP_Error( 3329 'missing_schema', 3330 __( 'When registering an "array" or "object" feature to show in the REST API, the feature\'s schema must also be defined.' ) 3331 ); 3332 } 3333 3334 if ( 'array' === $args['type'] && ! isset( $args['show_in_rest']['schema']['items'] ) ) { 3335 return new WP_Error( 3336 'missing_schema_items', 3337 __( 'When registering an "array" feature, the feature\'s schema must include the "items" keyword.' ) 3338 ); 3339 } 3340 3341 if ( 'object' === $args['type'] && ! isset( $args['show_in_rest']['schema']['properties'] ) ) { 3342 return new WP_Error( 3343 'missing_schema_properties', 3344 __( 'When registering an "object" feature, the feature\'s schema must include the "properties" keyword.' ) 3345 ); 3346 } 3347 } 3348 3349 if ( is_array( $args['show_in_rest'] ) ) { 3350 if ( isset( $args['show_in_rest']['prepare_callback'] ) 3351 && ! is_callable( $args['show_in_rest']['prepare_callback'] ) 3352 ) { 3353 return new WP_Error( 3354 'invalid_rest_prepare_callback', 3355 sprintf( 3356 /* translators: %s: prepare_callback */ 3357 __( 'The "%s" must be a callable function.' ), 3358 'prepare_callback' 3359 ) 3360 ); 3361 } 3362 3363 $args['show_in_rest']['schema'] = wp_parse_args( 3364 $args['show_in_rest']['schema'], 3365 array( 3366 'description' => $args['description'], 3367 'type' => $args['type'], 3368 'default' => false, 3369 ) 3370 ); 3371 3372 if ( is_bool( $args['show_in_rest']['schema']['default'] ) 3373 && ! in_array( 'boolean', (array) $args['show_in_rest']['schema']['type'], true ) 3374 ) { 3375 // Automatically include the "boolean" type when the default value is a boolean. 3376 $args['show_in_rest']['schema']['type'] = (array) $args['show_in_rest']['schema']['type']; 3377 array_unshift( $args['show_in_rest']['schema']['type'], 'boolean' ); 3378 } 3379 3380 $args['show_in_rest']['schema'] = rest_default_additional_properties_to_false( $args['show_in_rest']['schema'] ); 3381 } 3382 3383 $_wp_registered_theme_features[ $feature ] = $args; 3384 3385 return true; 3386 } 3387 3388 /** 3389 * Gets the list of registered theme features. 3390 * 3391 * @since 5.5.0 3392 * 3393 * @global array $_wp_registered_theme_features 3394 * 3395 * @return array[] List of theme features, keyed by their name. 3396 */ 3397 function get_registered_theme_features() { 3398 global $_wp_registered_theme_features; 3399 3400 if ( ! is_array( $_wp_registered_theme_features ) ) { 3401 return array(); 3402 } 3403 3404 return $_wp_registered_theme_features; 3405 } 3406 3407 /** 3408 * Gets the registration config for a theme feature. 3409 * 3410 * @since 5.5.0 3411 * 3412 * @global array $_wp_registered_theme_features 3413 * 3414 * @param string $feature The feature name. See add_theme_support() for the list 3415 * of possible values. 3416 * @return array|null The registration args, or null if the feature was not registered. 3417 */ 3418 function get_registered_theme_feature( $feature ) { 3419 global $_wp_registered_theme_features; 3420 3421 if ( ! is_array( $_wp_registered_theme_features ) ) { 3422 return null; 3423 } 3424 3425 return isset( $_wp_registered_theme_features[ $feature ] ) ? $_wp_registered_theme_features[ $feature ] : null; 3426 } 3427 3428 /** 3429 * Checks an attachment being deleted to see if it's a header or background image. 3430 * 3431 * If true it removes the theme modification which would be pointing at the deleted 3432 * attachment. 3433 * 3434 * @access private 3435 * @since 3.0.0 3436 * @since 4.3.0 Also removes `header_image_data`. 3437 * @since 4.5.0 Also removes custom logo theme mods. 3438 * @since 6.6.0 Also removes `site_logo` option set by the site logo block. 3439 * 3440 * @param int $id The attachment ID. 3441 */ 3442 function _delete_attachment_theme_mod( $id ) { 3443 $attachment_image = wp_get_attachment_url( $id ); 3444 $header_image = get_header_image(); 3445 $background_image = get_background_image(); 3446 $custom_logo_id = (int) get_theme_mod( 'custom_logo' ); 3447 $site_logo_id = (int) get_option( 'site_logo' ); 3448 3449 if ( $custom_logo_id && $custom_logo_id === $id ) { 3450 remove_theme_mod( 'custom_logo' ); 3451 remove_theme_mod( 'header_text' ); 3452 } 3453 3454 if ( $site_logo_id && $site_logo_id === $id ) { 3455 delete_option( 'site_logo' ); 3456 } 3457 3458 if ( $header_image && $header_image === $attachment_image ) { 3459 remove_theme_mod( 'header_image' ); 3460 remove_theme_mod( 'header_image_data' ); 3461 } 3462 3463 if ( $background_image && $background_image === $attachment_image ) { 3464 remove_theme_mod( 'background_image' ); 3465 } 3466 } 3467 3468 /** 3469 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load. 3470 * 3471 * See {@see 'after_switch_theme'}. 3472 * 3473 * @since 3.3.0 3474 */ 3475 function check_theme_switched() { 3476 $stylesheet = get_option( 'theme_switched' ); 3477 3478 if ( $stylesheet ) { 3479 $old_theme = wp_get_theme( $stylesheet ); 3480 3481 // Prevent widget & menu mapping from running since Customizer already called it up front. 3482 if ( get_option( 'theme_switched_via_customizer' ) ) { 3483 remove_action( 'after_switch_theme', '_wp_menus_changed' ); 3484 remove_action( 'after_switch_theme', '_wp_sidebars_changed' ); 3485 update_option( 'theme_switched_via_customizer', false ); 3486 } 3487 3488 if ( $old_theme->exists() ) { 3489 /** 3490 * Fires on the next WP load after the theme has been switched. 3491 * 3492 * The parameters differ according to whether the old theme exists or not. 3493 * If the old theme is missing, the old name will instead be the slug 3494 * of the old theme. 3495 * 3496 * See {@see 'switch_theme'}. 3497 * 3498 * @since 3.3.0 3499 * 3500 * @param string $old_name Old theme name. 3501 * @param WP_Theme $old_theme WP_Theme instance of the old theme. 3502 */ 3503 do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme ); 3504 } else { 3505 /** This action is documented in wp-includes/theme.php */ 3506 do_action( 'after_switch_theme', $stylesheet, $old_theme ); 3507 } 3508 3509 flush_rewrite_rules(); 3510 3511 update_option( 'theme_switched', false ); 3512 } 3513 } 3514 3515 /** 3516 * Includes and instantiates the WP_Customize_Manager class. 3517 * 3518 * Loads the Customizer at plugins_loaded when accessing the customize.php admin 3519 * page or when any request includes a wp_customize=on param or a customize_changeset 3520 * param (a UUID). This param is a signal for whether to bootstrap the Customizer when 3521 * WordPress is loading, especially in the Customizer preview 3522 * or when making Customizer Ajax requests for widgets or menus. 3523 * 3524 * @since 3.4.0 3525 * 3526 * @global WP_Customize_Manager $wp_customize 3527 */ 3528 function _wp_customize_include() { 3529 3530 $is_customize_admin_page = ( is_admin() && 'customize.php' === basename( $_SERVER['PHP_SELF'] ) ); 3531 $should_include = ( 3532 $is_customize_admin_page 3533 || 3534 ( isset( $_REQUEST['wp_customize'] ) && 'on' === $_REQUEST['wp_customize'] ) 3535 || 3536 ( ! empty( $_GET['customize_changeset_uuid'] ) || ! empty( $_POST['customize_changeset_uuid'] ) ) 3537 ); 3538 3539 if ( ! $should_include ) { 3540 return; 3541 } 3542 3543 /* 3544 * Note that wp_unslash() is not being used on the input vars because it is 3545 * called before wp_magic_quotes() gets called. Besides this fact, none of 3546 * the values should contain any characters needing slashes anyway. 3547 */ 3548 $keys = array( 3549 'changeset_uuid', 3550 'customize_changeset_uuid', 3551 'customize_theme', 3552 'theme', 3553 'customize_messenger_channel', 3554 'customize_autosaved', 3555 ); 3556 $input_vars = array_merge( 3557 wp_array_slice_assoc( $_GET, $keys ), 3558 wp_array_slice_assoc( $_POST, $keys ) 3559 ); 3560 3561 $theme = null; 3562 $autosaved = null; 3563 $messenger_channel = null; 3564 3565 /* 3566 * Value false indicates UUID should be determined after_setup_theme 3567 * to either re-use existing saved changeset or else generate a new UUID if none exists. 3568 */ 3569 $changeset_uuid = false; 3570 3571 /* 3572 * Set initially to false since defaults to true for back-compat; 3573 * can be overridden via the customize_changeset_branching filter. 3574 */ 3575 $branching = false; 3576 3577 if ( $is_customize_admin_page && isset( $input_vars['changeset_uuid'] ) ) { 3578 $changeset_uuid = sanitize_key( $input_vars['changeset_uuid'] ); 3579 } elseif ( ! empty( $input_vars['customize_changeset_uuid'] ) ) { 3580 $changeset_uuid = sanitize_key( $input_vars['customize_changeset_uuid'] ); 3581 } 3582 3583 // Note that theme will be sanitized via WP_Theme. 3584 if ( $is_customize_admin_page && isset( $input_vars['theme'] ) ) { 3585 $theme = $input_vars['theme']; 3586 } elseif ( isset( $input_vars['customize_theme'] ) ) { 3587 $theme = $input_vars['customize_theme']; 3588 } 3589 3590 if ( ! empty( $input_vars['customize_autosaved'] ) ) { 3591 $autosaved = true; 3592 } 3593 3594 if ( isset( $input_vars['customize_messenger_channel'] ) ) { 3595 $messenger_channel = sanitize_key( $input_vars['customize_messenger_channel'] ); 3596 } 3597 3598 /* 3599 * Note that settings must be previewed even outside the customizer preview 3600 * and also in the customizer pane itself. This is to enable loading an existing 3601 * changeset into the customizer. Previewing the settings only has to be prevented 3602 * here in the case of a customize_save action because this will cause WP to think 3603 * there is nothing changed that needs to be saved. 3604 */ 3605 $is_customize_save_action = ( 3606 wp_doing_ajax() 3607 && 3608 isset( $_REQUEST['action'] ) 3609 && 3610 'customize_save' === wp_unslash( $_REQUEST['action'] ) 3611 ); 3612 $settings_previewed = ! $is_customize_save_action; 3613 3614 require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; 3615 $GLOBALS['wp_customize'] = new WP_Customize_Manager( 3616 compact( 3617 'changeset_uuid', 3618 'theme', 3619 'messenger_channel', 3620 'settings_previewed', 3621 'autosaved', 3622 'branching' 3623 ) 3624 ); 3625 } 3626 3627 /** 3628 * Publishes a snapshot's changes. 3629 * 3630 * @since 4.7.0 3631 * @access private 3632 * 3633 * @global WP_Customize_Manager $wp_customize Customizer instance. 3634 * 3635 * @param string $new_status New post status. 3636 * @param string $old_status Old post status. 3637 * @param WP_Post $changeset_post Changeset post object. 3638 */ 3639 function _wp_customize_publish_changeset( $new_status, $old_status, $changeset_post ) { 3640 global $wp_customize; 3641 3642 $is_publishing_changeset = ( 3643 'customize_changeset' === $changeset_post->post_type 3644 && 3645 'publish' === $new_status 3646 && 3647 'publish' !== $old_status 3648 ); 3649 if ( ! $is_publishing_changeset ) { 3650 return; 3651 } 3652 3653 if ( empty( $wp_customize ) ) { 3654 require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; 3655 $wp_customize = new WP_Customize_Manager( 3656 array( 3657 'changeset_uuid' => $changeset_post->post_name, 3658 'settings_previewed' => false, 3659 ) 3660 ); 3661 } 3662 3663 if ( ! did_action( 'customize_register' ) ) { 3664 /* 3665 * When running from CLI or Cron, the customize_register action will need 3666 * to be triggered in order for core, themes, and plugins to register their 3667 * settings. Normally core will add_action( 'customize_register' ) at 3668 * priority 10 to register the core settings, and if any themes/plugins 3669 * also add_action( 'customize_register' ) at the same priority, they 3670 * will have a $wp_customize with those settings registered since they 3671 * call add_action() afterward, normally. However, when manually doing 3672 * the customize_register action after the setup_theme, then the order 3673 * will be reversed for two actions added at priority 10, resulting in 3674 * the core settings no longer being available as expected to themes/plugins. 3675 * So the following manually calls the method that registers the core 3676 * settings up front before doing the action. 3677 */ 3678 remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) ); 3679 $wp_customize->register_controls(); 3680 3681 /** This filter is documented in wp-includes/class-wp-customize-manager.php */ 3682 do_action( 'customize_register', $wp_customize ); 3683 } 3684 $wp_customize->_publish_changeset_values( $changeset_post->ID ); 3685 3686 /* 3687 * Trash the changeset post if revisions are not enabled. Unpublished 3688 * changesets by default get garbage collected due to the auto-draft status. 3689 * When a changeset post is published, however, it would no longer get cleaned 3690 * out. This is a problem when the changeset posts are never displayed anywhere, 3691 * since they would just be endlessly piling up. So here we use the revisions 3692 * feature to indicate whether or not a published changeset should get trashed 3693 * and thus garbage collected. 3694 */ 3695 if ( ! wp_revisions_enabled( $changeset_post ) ) { 3696 $wp_customize->trash_changeset_post( $changeset_post->ID ); 3697 } 3698 } 3699 3700 /** 3701 * Filters changeset post data upon insert to ensure post_name is intact. 3702 * 3703 * This is needed to prevent the post_name from being dropped when the post is 3704 * transitioned into pending status by a contributor. 3705 * 3706 * @since 4.7.0 3707 * 3708 * @see wp_insert_post() 3709 * 3710 * @param array $post_data An array of slashed post data. 3711 * @param array $supplied_post_data An array of sanitized, but otherwise unmodified post data. 3712 * @return array Filtered data. 3713 */ 3714 function _wp_customize_changeset_filter_insert_post_data( $post_data, $supplied_post_data ) { 3715 if ( isset( $post_data['post_type'] ) && 'customize_changeset' === $post_data['post_type'] ) { 3716 3717 // Prevent post_name from being dropped, such as when contributor saves a changeset post as pending. 3718 if ( empty( $post_data['post_name'] ) && ! empty( $supplied_post_data['post_name'] ) ) { 3719 $post_data['post_name'] = $supplied_post_data['post_name']; 3720 } 3721 } 3722 return $post_data; 3723 } 3724 3725 /** 3726 * Adds settings for the customize-loader script. 3727 * 3728 * @since 3.4.0 3729 */ 3730 function _wp_customize_loader_settings() { 3731 $admin_origin = parse_url( admin_url() ); 3732 $home_origin = parse_url( home_url() ); 3733 $cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) ); 3734 3735 $browser = array( 3736 'mobile' => wp_is_mobile(), 3737 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ), 3738 ); 3739 3740 $settings = array( 3741 'url' => esc_url( admin_url( 'customize.php' ) ), 3742 'isCrossDomain' => $cross_domain, 3743 'browser' => $browser, 3744 'l10n' => array( 3745 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ), 3746 'mainIframeTitle' => __( 'Customizer' ), 3747 ), 3748 ); 3749 3750 $script = 'var _wpCustomizeLoaderSettings = ' . wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ';'; 3751 3752 $wp_scripts = wp_scripts(); 3753 $data = $wp_scripts->get_data( 'customize-loader', 'data' ); 3754 if ( $data ) { 3755 $script = "$data\n$script"; 3756 } 3757 3758 $wp_scripts->add_data( 'customize-loader', 'data', $script ); 3759 } 3760 3761 /** 3762 * Returns a URL to load the Customizer. 3763 * 3764 * @since 3.4.0 3765 * 3766 * @param string $stylesheet Optional. Theme to customize. Defaults to active theme. 3767 * The theme's stylesheet will be urlencoded if necessary. 3768 * @return string 3769 */ 3770 function wp_customize_url( $stylesheet = '' ) { 3771 $url = admin_url( 'customize.php' ); 3772 if ( $stylesheet ) { 3773 $url = add_query_arg( 'theme', urlencode( $stylesheet ), $url ); 3774 } 3775 return esc_url( $url ); 3776 } 3777 3778 /** 3779 * Prints a script to check whether or not the Customizer is supported, 3780 * and apply either the no-customize-support or customize-support class 3781 * to the body. 3782 * 3783 * This function MUST be called inside the body tag. 3784 * 3785 * Ideally, call this function immediately after the body tag is opened. 3786 * This prevents a flash of unstyled content. 3787 * 3788 * It is also recommended that you add the "no-customize-support" class 3789 * to the body tag by default. 3790 * 3791 * @since 3.4.0 3792 * @since 4.7.0 Support for IE8 and below is explicitly removed via conditional comments. 3793 * @since 5.5.0 IE8 and older are no longer supported. 3794 */ 3795 function wp_customize_support_script() { 3796 $admin_origin = parse_url( admin_url() ); 3797 $home_origin = parse_url( home_url() ); 3798 $cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) ); 3799 ob_start(); 3800 ?> 3801 <script> 3802 (function() { 3803 var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)'); 3804 3805 <?php if ( $cross_domain ) : ?> 3806 request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })(); 3807 <?php else : ?> 3808 request = true; 3809 <?php endif; ?> 3810 3811 b[c] = b[c].replace( rcs, ' ' ); 3812 // The customizer requires postMessage and CORS (if the site is cross domain). 3813 b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs; 3814 }()); 3815 </script> 3816 <?php 3817 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) . "\n//# sourceURL=" . rawurlencode( __FUNCTION__ ) ); 3818 } 3819 3820 /** 3821 * Whether the site is being previewed in the Customizer. 3822 * 3823 * @since 4.0.0 3824 * 3825 * @global WP_Customize_Manager $wp_customize Customizer instance. 3826 * 3827 * @return bool True if the site is being previewed in the Customizer, false otherwise. 3828 */ 3829 function is_customize_preview() { 3830 global $wp_customize; 3831 3832 return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview(); 3833 } 3834 3835 /** 3836 * Makes sure that auto-draft posts get their post_date bumped or status changed 3837 * to draft to prevent premature garbage-collection. 3838 * 3839 * When a changeset is updated but remains an auto-draft, ensure the post_date 3840 * for the auto-draft posts remains the same so that it will be 3841 * garbage-collected at the same time by `wp_delete_auto_drafts()`. Otherwise, 3842 * if the changeset is updated to be a draft then update the posts 3843 * to have a far-future post_date so that they will never be garbage collected 3844 * unless the changeset post itself is deleted. 3845 * 3846 * When a changeset is updated to be a persistent draft or to be scheduled for 3847 * publishing, then transition any dependent auto-drafts to a draft status so 3848 * that they likewise will not be garbage-collected but also so that they can 3849 * be edited in the admin before publishing since there is not yet a post/page 3850 * editing flow in the Customizer. See #39752. 3851 * 3852 * @link https://core.trac.wordpress.org/ticket/39752 3853 * 3854 * @since 4.8.0 3855 * @access private 3856 * @see wp_delete_auto_drafts() 3857 * 3858 * @global wpdb $wpdb WordPress database abstraction object. 3859 * 3860 * @param string $new_status Transition to this post status. 3861 * @param string $old_status Previous post status. 3862 * @param \WP_Post $post Post data. 3863 */ 3864 function _wp_keep_alive_customize_changeset_dependent_auto_drafts( $new_status, $old_status, $post ) { 3865 global $wpdb; 3866 unset( $old_status ); 3867 3868 // Short-circuit if not a changeset or if the changeset was published. 3869 if ( 'customize_changeset' !== $post->post_type || 'publish' === $new_status ) { 3870 return; 3871 } 3872 3873 $data = json_decode( $post->post_content, true ); 3874 if ( empty( $data['nav_menus_created_posts']['value'] ) ) { 3875 return; 3876 } 3877 3878 /* 3879 * Actually, in lieu of keeping alive, trash any customization drafts here if the changeset itself is 3880 * getting trashed. This is needed because when a changeset transitions to a draft, then any of the 3881 * dependent auto-draft post/page stubs will also get transitioned to customization drafts which 3882 * are then visible in the WP Admin. We cannot wait for the deletion of the changeset in which 3883 * _wp_delete_customize_changeset_dependent_auto_drafts() will be called, since they need to be 3884 * trashed to remove from visibility immediately. 3885 */ 3886 if ( 'trash' === $new_status ) { 3887 foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) { 3888 if ( ! empty( $post_id ) && 'draft' === get_post_status( $post_id ) ) { 3889 wp_trash_post( $post_id ); 3890 } 3891 } 3892 return; 3893 } 3894 3895 $post_args = array(); 3896 if ( 'auto-draft' === $new_status ) { 3897 /* 3898 * Keep the post date for the post matching the changeset 3899 * so that it will not be garbage-collected before the changeset. 3900 */ 3901 $post_args['post_date'] = $post->post_date; // Note wp_delete_auto_drafts() only looks at this date. 3902 } else { 3903 /* 3904 * Since the changeset no longer has an auto-draft (and it is not published) 3905 * it is now a persistent changeset, a long-lived draft, and so any 3906 * associated auto-draft posts should likewise transition into having a draft 3907 * status. These drafts will be treated differently than regular drafts in 3908 * that they will be tied to the given changeset. The publish meta box is 3909 * replaced with a notice about how the post is part of a set of customized changes 3910 * which will be published when the changeset is published. 3911 */ 3912 $post_args['post_status'] = 'draft'; 3913 } 3914 3915 foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) { 3916 if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) { 3917 continue; 3918 } 3919 $wpdb->update( 3920 $wpdb->posts, 3921 $post_args, 3922 array( 'ID' => $post_id ) 3923 ); 3924 clean_post_cache( $post_id ); 3925 } 3926 } 3927 3928 /** 3929 * Creates the initial theme features when the 'setup_theme' action is fired. 3930 * 3931 * See {@see 'setup_theme'}. 3932 * 3933 * @since 5.5.0 3934 * @since 6.0.1 The `block-templates` feature was added. 3935 */ 3936 function create_initial_theme_features() { 3937 register_theme_feature( 3938 'align-wide', 3939 array( 3940 'description' => __( 'Whether theme opts in to wide alignment CSS class.' ), 3941 'show_in_rest' => true, 3942 ) 3943 ); 3944 register_theme_feature( 3945 'automatic-feed-links', 3946 array( 3947 'description' => __( 'Whether posts and comments RSS feed links are added to head.' ), 3948 'show_in_rest' => true, 3949 ) 3950 ); 3951 register_theme_feature( 3952 'block-templates', 3953 array( 3954 'description' => __( 'Whether a theme uses block-based templates.' ), 3955 'show_in_rest' => true, 3956 ) 3957 ); 3958 register_theme_feature( 3959 'block-template-parts', 3960 array( 3961 'description' => __( 'Whether a theme uses block-based template parts.' ), 3962 'show_in_rest' => true, 3963 ) 3964 ); 3965 register_theme_feature( 3966 'custom-background', 3967 array( 3968 'description' => __( 'Custom background if defined by the theme.' ), 3969 'type' => 'object', 3970 'show_in_rest' => array( 3971 'schema' => array( 3972 'properties' => array( 3973 'default-image' => array( 3974 'type' => 'string', 3975 'format' => 'uri', 3976 ), 3977 'default-preset' => array( 3978 'type' => 'string', 3979 'enum' => array( 3980 'default', 3981 'fill', 3982 'fit', 3983 'repeat', 3984 'custom', 3985 ), 3986 ), 3987 'default-position-x' => array( 3988 'type' => 'string', 3989 'enum' => array( 3990 'left', 3991 'center', 3992 'right', 3993 ), 3994 ), 3995 'default-position-y' => array( 3996 'type' => 'string', 3997 'enum' => array( 3998 'left', 3999 'center', 4000 'right', 4001 ), 4002 ), 4003 'default-size' => array( 4004 'type' => 'string', 4005 'enum' => array( 4006 'auto', 4007 'contain', 4008 'cover', 4009 ), 4010 ), 4011 'default-repeat' => array( 4012 'type' => 'string', 4013 'enum' => array( 4014 'repeat-x', 4015 'repeat-y', 4016 'repeat', 4017 'no-repeat', 4018 ), 4019 ), 4020 'default-attachment' => array( 4021 'type' => 'string', 4022 'enum' => array( 4023 'scroll', 4024 'fixed', 4025 ), 4026 ), 4027 'default-color' => array( 4028 'type' => 'string', 4029 ), 4030 ), 4031 ), 4032 ), 4033 ) 4034 ); 4035 register_theme_feature( 4036 'custom-header', 4037 array( 4038 'description' => __( 'Custom header if defined by the theme.' ), 4039 'type' => 'object', 4040 'show_in_rest' => array( 4041 'schema' => array( 4042 'properties' => array( 4043 'default-image' => array( 4044 'type' => 'string', 4045 'format' => 'uri', 4046 ), 4047 'random-default' => array( 4048 'type' => 'boolean', 4049 ), 4050 'width' => array( 4051 'type' => 'integer', 4052 ), 4053 'height' => array( 4054 'type' => 'integer', 4055 ), 4056 'flex-height' => array( 4057 'type' => 'boolean', 4058 ), 4059 'flex-width' => array( 4060 'type' => 'boolean', 4061 ), 4062 'default-text-color' => array( 4063 'type' => 'string', 4064 ), 4065 'header-text' => array( 4066 'type' => 'boolean', 4067 ), 4068 'uploads' => array( 4069 'type' => 'boolean', 4070 ), 4071 'video' => array( 4072 'type' => 'boolean', 4073 ), 4074 ), 4075 ), 4076 ), 4077 ) 4078 ); 4079 register_theme_feature( 4080 'custom-logo', 4081 array( 4082 'type' => 'object', 4083 'description' => __( 'Custom logo if defined by the theme.' ), 4084 'show_in_rest' => array( 4085 'schema' => array( 4086 'properties' => array( 4087 'width' => array( 4088 'type' => 'integer', 4089 ), 4090 'height' => array( 4091 'type' => 'integer', 4092 ), 4093 'flex-width' => array( 4094 'type' => 'boolean', 4095 ), 4096 'flex-height' => array( 4097 'type' => 'boolean', 4098 ), 4099 'header-text' => array( 4100 'type' => 'array', 4101 'items' => array( 4102 'type' => 'string', 4103 ), 4104 ), 4105 'unlink-homepage-logo' => array( 4106 'type' => 'boolean', 4107 ), 4108 ), 4109 ), 4110 ), 4111 ) 4112 ); 4113 register_theme_feature( 4114 'customize-selective-refresh-widgets', 4115 array( 4116 'description' => __( 'Whether the theme enables Selective Refresh for Widgets being managed with the Customizer.' ), 4117 'show_in_rest' => true, 4118 ) 4119 ); 4120 register_theme_feature( 4121 'dark-editor-style', 4122 array( 4123 'description' => __( 'Whether theme opts in to the dark editor style UI.' ), 4124 'show_in_rest' => true, 4125 ) 4126 ); 4127 register_theme_feature( 4128 'disable-custom-colors', 4129 array( 4130 'description' => __( 'Whether the theme disables custom colors.' ), 4131 'show_in_rest' => true, 4132 ) 4133 ); 4134 register_theme_feature( 4135 'disable-custom-font-sizes', 4136 array( 4137 'description' => __( 'Whether the theme disables custom font sizes.' ), 4138 'show_in_rest' => true, 4139 ) 4140 ); 4141 register_theme_feature( 4142 'disable-custom-gradients', 4143 array( 4144 'description' => __( 'Whether the theme disables custom gradients.' ), 4145 'show_in_rest' => true, 4146 ) 4147 ); 4148 register_theme_feature( 4149 'disable-layout-styles', 4150 array( 4151 'description' => __( 'Whether the theme disables generated layout styles.' ), 4152 'show_in_rest' => true, 4153 ) 4154 ); 4155 register_theme_feature( 4156 'editor-color-palette', 4157 array( 4158 'type' => 'array', 4159 'description' => __( 'Custom color palette if defined by the theme.' ), 4160 'show_in_rest' => array( 4161 'schema' => array( 4162 'items' => array( 4163 'type' => 'object', 4164 'properties' => array( 4165 'name' => array( 4166 'type' => 'string', 4167 ), 4168 'slug' => array( 4169 'type' => 'string', 4170 ), 4171 'color' => array( 4172 'type' => 'string', 4173 ), 4174 ), 4175 ), 4176 ), 4177 ), 4178 ) 4179 ); 4180 register_theme_feature( 4181 'editor-font-sizes', 4182 array( 4183 'type' => 'array', 4184 'description' => __( 'Custom font sizes if defined by the theme.' ), 4185 'show_in_rest' => array( 4186 'schema' => array( 4187 'items' => array( 4188 'type' => 'object', 4189 'properties' => array( 4190 'name' => array( 4191 'type' => 'string', 4192 ), 4193 'size' => array( 4194 'type' => 'number', 4195 ), 4196 'slug' => array( 4197 'type' => 'string', 4198 ), 4199 ), 4200 ), 4201 ), 4202 ), 4203 ) 4204 ); 4205 register_theme_feature( 4206 'editor-gradient-presets', 4207 array( 4208 'type' => 'array', 4209 'description' => __( 'Custom gradient presets if defined by the theme.' ), 4210 'show_in_rest' => array( 4211 'schema' => array( 4212 'items' => array( 4213 'type' => 'object', 4214 'properties' => array( 4215 'name' => array( 4216 'type' => 'string', 4217 ), 4218 'gradient' => array( 4219 'type' => 'string', 4220 ), 4221 'slug' => array( 4222 'type' => 'string', 4223 ), 4224 ), 4225 ), 4226 ), 4227 ), 4228 ) 4229 ); 4230 register_theme_feature( 4231 'editor-spacing-sizes', 4232 array( 4233 'type' => 'array', 4234 'description' => __( 'Custom spacing sizes if defined by the theme.' ), 4235 'show_in_rest' => array( 4236 'schema' => array( 4237 'items' => array( 4238 'type' => 'object', 4239 'properties' => array( 4240 'name' => array( 4241 'type' => 'string', 4242 ), 4243 'size' => array( 4244 'type' => 'string', 4245 ), 4246 'slug' => array( 4247 'type' => 'string', 4248 ), 4249 ), 4250 ), 4251 ), 4252 ), 4253 ) 4254 ); 4255 register_theme_feature( 4256 'editor-styles', 4257 array( 4258 'description' => __( 'Whether theme opts in to the editor styles CSS wrapper.' ), 4259 'show_in_rest' => true, 4260 ) 4261 ); 4262 register_theme_feature( 4263 'html5', 4264 array( 4265 'type' => 'array', 4266 'description' => __( 'Allows use of HTML5 markup for search forms, comment forms, comment lists, gallery, and caption.' ), 4267 'show_in_rest' => array( 4268 'schema' => array( 4269 'items' => array( 4270 'type' => 'string', 4271 'enum' => array( 4272 'search-form', 4273 'comment-form', 4274 'comment-list', 4275 'gallery', 4276 'caption', 4277 'script', 4278 'style', 4279 ), 4280 ), 4281 ), 4282 ), 4283 ) 4284 ); 4285 register_theme_feature( 4286 'post-formats', 4287 array( 4288 'type' => 'array', 4289 'description' => __( 'Post formats supported.' ), 4290 'show_in_rest' => array( 4291 'name' => 'formats', 4292 'schema' => array( 4293 'items' => array( 4294 'type' => 'string', 4295 'enum' => get_post_format_slugs(), 4296 ), 4297 'default' => array( 'standard' ), 4298 ), 4299 'prepare_callback' => static function ( $formats ) { 4300 $formats = is_array( $formats ) ? array_values( $formats[0] ) : array(); 4301 $formats = array_merge( array( 'standard' ), $formats ); 4302 4303 return $formats; 4304 }, 4305 ), 4306 ) 4307 ); 4308 register_theme_feature( 4309 'post-thumbnails', 4310 array( 4311 'type' => 'array', 4312 'description' => __( 'The post types that support thumbnails or true if all post types are supported.' ), 4313 'show_in_rest' => array( 4314 'type' => array( 'boolean', 'array' ), 4315 'schema' => array( 4316 'items' => array( 4317 'type' => 'string', 4318 ), 4319 ), 4320 ), 4321 ) 4322 ); 4323 register_theme_feature( 4324 'responsive-embeds', 4325 array( 4326 'description' => __( 'Whether the theme supports responsive embedded content.' ), 4327 'show_in_rest' => true, 4328 ) 4329 ); 4330 register_theme_feature( 4331 'title-tag', 4332 array( 4333 'description' => __( 'Whether the theme can manage the document title tag.' ), 4334 'show_in_rest' => true, 4335 ) 4336 ); 4337 register_theme_feature( 4338 'wp-block-styles', 4339 array( 4340 'description' => __( 'Whether theme opts in to default WordPress block styles for viewing.' ), 4341 'show_in_rest' => true, 4342 ) 4343 ); 4344 } 4345 4346 /** 4347 * Returns whether the active theme is a block-based theme or not. 4348 * 4349 * @since 5.9.0 4350 * 4351 * @global string[] $wp_theme_directories 4352 * 4353 * @return bool Whether the active theme is a block-based theme or not. 4354 */ 4355 function wp_is_block_theme() { 4356 if ( empty( $GLOBALS['wp_theme_directories'] ) ) { 4357 _doing_it_wrong( __FUNCTION__, __( 'This function should not be called before the theme directory is registered.' ), '6.8.0' ); 4358 return false; 4359 } 4360 4361 return wp_get_theme()->is_block_theme(); 4362 } 4363 4364 /** 4365 * Given an element name, returns a class name. 4366 * 4367 * Alias of WP_Theme_JSON::get_element_class_name. 4368 * 4369 * @since 6.1.0 4370 * 4371 * @param string $element The name of the element. 4372 * 4373 * @return string The name of the class. 4374 */ 4375 function wp_theme_get_element_class_name( $element ) { 4376 return WP_Theme_JSON::get_element_class_name( $element ); 4377 } 4378 4379 /** 4380 * Adds default theme supports for block themes when the 'after_setup_theme' action fires. 4381 * 4382 * See {@see 'after_setup_theme'}. 4383 * 4384 * @since 5.9.0 4385 * @access private 4386 */ 4387 function _add_default_theme_supports() { 4388 if ( ! wp_is_block_theme() ) { 4389 return; 4390 } 4391 4392 add_theme_support( 'post-thumbnails' ); 4393 add_theme_support( 'responsive-embeds' ); 4394 add_theme_support( 'editor-styles' ); 4395 /* 4396 * Makes block themes support HTML5 by default for the comment block and search form 4397 * (which use default template functions) and `[caption]` and `[gallery]` shortcodes. 4398 * Other blocks contain their own HTML5 markup. 4399 */ 4400 add_theme_support( 'html5', array( 'comment-form', 'comment-list', 'search-form', 'gallery', 'caption', 'style', 'script' ) ); 4401 add_theme_support( 'automatic-feed-links' ); 4402 4403 add_filter( 'should_load_separate_core_block_assets', '__return_true' ); 4404 add_filter( 'should_load_block_assets_on_demand', '__return_true' ); 4405 4406 /* 4407 * Remove the Customizer's Menus panel when block theme is active. 4408 */ 4409 add_filter( 4410 'customize_panel_active', 4411 static function ( $active, WP_Customize_Panel $panel ) { 4412 if ( 4413 'nav_menus' === $panel->id && 4414 ! current_theme_supports( 'menus' ) && 4415 ! current_theme_supports( 'widgets' ) 4416 ) { 4417 $active = false; 4418 } 4419 return $active; 4420 }, 4421 10, 4422 2 4423 ); 4424 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Mon Oct 20 08:20:05 2025 | Cross-referenced by PHPXref |