[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

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


Generated: Sat Apr 27 01:00:02 2024 Cross-referenced by PHPXref 0.7.1