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


Generated: Thu Sep 23 01:00:04 2021 Cross-referenced by PHPXref 0.7.1