[ Index ]

WordPress 5.4.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

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


Generated: Tue May 19 15:51:04 2020 Cross-referenced by PHPXref 0.7.1