[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> theme.php (source)

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


Generated : Wed Feb 11 08:20:09 2026 Cross-referenced by PHPXref