[ Index ]

PHP Cross Reference of WordPress

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


Generated: Wed Jan 26 01:00:03 2022 Cross-referenced by PHPXref 0.7.1