[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> script-loader.php (source)

   1  <?php
   2  /**
   3   * WordPress scripts and styles default loader.
   4   *
   5   * This constant is used to manage the loading of scripts and CSS:
   6   * define('SCRIPT_DEBUG', true); loads the development (non-minified) versions of all scripts and CSS.
   7   *
   8   * @package WordPress
   9   */
  10  
  11  /** WordPress Dependency Class */
  12  require  ABSPATH . WPINC . '/class-wp-dependency.php';
  13  
  14  /** WordPress Dependencies Class */
  15  require  ABSPATH . WPINC . '/class-wp-dependencies.php';
  16  
  17  /** WordPress Scripts Class */
  18  require  ABSPATH . WPINC . '/class-wp-scripts.php';
  19  
  20  /** WordPress Scripts Functions */
  21  require  ABSPATH . WPINC . '/functions.wp-scripts.php';
  22  
  23  /** WordPress Styles Class */
  24  require  ABSPATH . WPINC . '/class-wp-styles.php';
  25  
  26  /** WordPress Styles Functions */
  27  require  ABSPATH . WPINC . '/functions.wp-styles.php';
  28  
  29  /**
  30   * Registers TinyMCE scripts.
  31   *
  32   * TinyMCE core and the compat3x plugin are registered as separate files. TinyMCE loads its theme
  33   * and any other plugins itself when an editor is initialized. The `wp-tinymce.js` bundle of all of
  34   * them is no longer registered, but it can still be registered in their place under the
  35   * `wp-tinymce` handle.
  36   *
  37   * @since 5.0.0
  38   * @since 7.2.0 The `wp-tinymce.js` bundle is no longer registered, regardless of whether scripts
  39   *              are concatenated or compressed.
  40   * @since 7.2.0 The `$force_uncompressed` parameter was deprecated and renamed to `$deprecated`.
  41   *
  42   * @global string $tinymce_version
  43   *
  44   * @param WP_Scripts $scripts    WP_Scripts object.
  45   * @param bool       $deprecated Not used.
  46   *
  47   * @phpstan-param false $deprecated
  48   */
  49  function wp_register_tinymce_scripts( $scripts, $deprecated = false ): void {
  50      global $tinymce_version;
  51  
  52      if ( false !== $deprecated ) {
  53          _deprecated_argument( __FUNCTION__, '7.2.0' );
  54      }
  55  
  56      $suffix     = wp_scripts_get_suffix();
  57      $dev_suffix = wp_scripts_get_suffix( 'dev' );
  58  
  59      // Load tinymce.js when running from /src, otherwise tinymce.min.js.
  60      $scripts->add( 'wp-tinymce-root', includes_url( 'js/tinymce/' ) . "tinymce$dev_suffix.js", array(), $tinymce_version );
  61      $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . "plugins/compat3x/plugin$dev_suffix.js", array( 'wp-tinymce-root' ), $tinymce_version );
  62  
  63      $scripts->add( 'wp-tinymce-lists', includes_url( "js/tinymce/plugins/lists/plugin$suffix.js" ), array( 'wp-tinymce' ), $tinymce_version );
  64  }
  65  
  66  /**
  67   * Registers all the WordPress vendor scripts that are in the standardized
  68   * `js/dist/vendor/` location.
  69   *
  70   * For the order of `$scripts->add` see `wp_default_scripts`.
  71   *
  72   * @since 5.0.0
  73   *
  74   * @global WP_Locale $wp_locale WordPress date and time locale object.
  75   *
  76   * @param WP_Scripts $scripts WP_Scripts object.
  77   */
  78  function wp_default_packages_vendor( $scripts ) {
  79      global $wp_locale;
  80  
  81      $suffix = wp_scripts_get_suffix();
  82  
  83      $vendor_scripts = array(
  84          'react'                       => array(),
  85          'react-dom'                   => array( 'react' ),
  86          'react-jsx-runtime'           => array( 'react' ),
  87          'regenerator-runtime'         => array(),
  88          'moment'                      => array(),
  89          'lodash'                      => array(),
  90          'wp-polyfill-fetch'           => array(),
  91          'wp-polyfill-formdata'        => array(),
  92          'wp-polyfill-node-contains'   => array(),
  93          'wp-polyfill-url'             => array(),
  94          'wp-polyfill-dom-rect'        => array(),
  95          'wp-polyfill-element-closest' => array(),
  96          'wp-polyfill-object-fit'      => array(),
  97          'wp-polyfill-inert'           => array(),
  98          'wp-polyfill'                 => array(),
  99      );
 100  
 101      $vendor_scripts_versions = array(
 102          'react'                       => '18.3.1.1', // Final .1 due to switch to UMD build, can be removed in the next update.
 103          'react-dom'                   => '18.3.1.1', // Final .1 due to switch to UMD build, can be removed in the next update.
 104          'react-jsx-runtime'           => '18.3.1',
 105          'regenerator-runtime'         => '0.14.1',
 106          'moment'                      => '2.31.0',
 107          'lodash'                      => '4.18.1',
 108          'wp-polyfill-fetch'           => '3.6.20',
 109          'wp-polyfill-formdata'        => '4.0.10',
 110          'wp-polyfill-node-contains'   => '4.8.0',
 111          'wp-polyfill-url'             => '3.6.4',
 112          'wp-polyfill-dom-rect'        => '4.8.0',
 113          'wp-polyfill-element-closest' => '3.0.2',
 114          'wp-polyfill-object-fit'      => '2.3.5',
 115          'wp-polyfill-inert'           => '3.1.3',
 116          'wp-polyfill'                 => '3.15.0',
 117      );
 118  
 119      foreach ( $vendor_scripts as $handle => $dependencies ) {
 120          $scripts->add(
 121              $handle,
 122              "/wp-includes/js/dist/vendor/$handle$suffix.js",
 123              $dependencies,
 124              $vendor_scripts_versions[ $handle ],
 125              1
 126          );
 127      }
 128  
 129      did_action( 'init' ) && $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );
 130  
 131      did_action( 'init' ) && $scripts->add_inline_script(
 132          'moment',
 133          sprintf(
 134              "moment.updateLocale( '%s', %s );",
 135              esc_js( get_user_locale() ),
 136              wp_json_encode(
 137                  array(
 138                      'months'         => array_values( $wp_locale->month ),
 139                      'monthsShort'    => array_values( $wp_locale->month_abbrev ),
 140                      'weekdays'       => array_values( $wp_locale->weekday ),
 141                      'weekdaysShort'  => array_values( $wp_locale->weekday_abbrev ),
 142                      'week'           => array(
 143                          'dow' => (int) get_option( 'start_of_week', 0 ),
 144                      ),
 145                      'longDateFormat' => array(
 146                          'LT'   => get_option( 'time_format', __( 'g:i a' ) ),
 147                          'LTS'  => null,
 148                          'L'    => null,
 149                          'LL'   => get_option( 'date_format', __( 'F j, Y' ) ),
 150                          'LLL'  => __( 'F j, Y g:i a' ),
 151                          'LLLL' => null,
 152                      ),
 153                  ),
 154                  JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
 155              )
 156          ),
 157          'after'
 158      );
 159  }
 160  
 161  /**
 162   * Registers development scripts that integrate with `@wordpress/scripts`.
 163   *
 164   * These scripts enable hot module replacement (HMR) for block development
 165   * when using `wp-scripts start --hot`.
 166   *
 167   * @link https://github.com/WordPress/gutenberg/tree/trunk/packages/scripts#start
 168   *
 169   * @since 6.0.0
 170   *
 171   * @param WP_Scripts $scripts WP_Scripts object.
 172   */
 173  function wp_register_development_scripts( $scripts ) {
 174      if (
 175          ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG
 176          || empty( $scripts->registered['react'] )
 177          || defined( 'WP_RUN_CORE_TESTS' )
 178      ) {
 179          return;
 180      }
 181  
 182      // React Refresh runtime - exposes ReactRefreshRuntime global.
 183      // No dependencies.
 184      $scripts->add(
 185          'wp-react-refresh-runtime',
 186          '/wp-includes/js/dist/development/react-refresh-runtime.js',
 187          array(),
 188          '0.14.0'
 189      );
 190  
 191      // React Refresh entry - injects runtime into global hook.
 192      // Must load before React to set up hooks.
 193      $scripts->add(
 194          'wp-react-refresh-entry',
 195          '/wp-includes/js/dist/development/react-refresh-entry.js',
 196          array( 'wp-react-refresh-runtime' ),
 197          '0.14.0'
 198      );
 199  
 200      // Add entry as a dependency of React so it loads first.
 201      // See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
 202      $scripts->registered['react']->deps[] = 'wp-react-refresh-entry';
 203  }
 204  
 205  /**
 206   * Returns contents of an inline script used in appending polyfill scripts for
 207   * browsers which fail the provided tests. The provided array is a mapping from
 208   * a condition to verify feature support to its polyfill script handle.
 209   *
 210   * @since 5.0.0
 211   *
 212   * @param WP_Scripts $scripts WP_Scripts object.
 213   * @param string[]   $tests   Features to detect.
 214   * @return string Conditional polyfill inline script.
 215   */
 216  function wp_get_script_polyfill( $scripts, $tests ) {
 217      $polyfill = '';
 218      foreach ( $tests as $test => $handle ) {
 219          if ( ! array_key_exists( $handle, $scripts->registered ) ) {
 220              continue;
 221          }
 222  
 223          $src = $scripts->registered[ $handle ]->src;
 224          $ver = $scripts->registered[ $handle ]->ver;
 225  
 226          if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && str_starts_with( $src, $scripts->content_url ) ) ) {
 227              $src = $scripts->base_url . $src;
 228          }
 229  
 230          if ( ! empty( $ver ) ) {
 231              $src = add_query_arg( 'ver', $ver, $src );
 232          }
 233  
 234          /** This filter is documented in wp-includes/class-wp-scripts.php */
 235          $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
 236  
 237          if ( ! $src ) {
 238              continue;
 239          }
 240  
 241          $polyfill .= (
 242              // Test presence of feature...
 243              '( ' . $test . ' ) || ' .
 244              /*
 245               * ...appending polyfill on any failures. Cautious viewers may balk
 246               * at the `document.write`. Its caveat of synchronous mid-stream
 247               * blocking write is exactly the behavior we need though.
 248               */
 249              'document.write( \'<script src="' .
 250              $src .
 251              '"></scr\' + \'ipt>\' );'
 252          );
 253      }
 254  
 255      return $polyfill;
 256  }
 257  
 258  /**
 259   * Registers all the WordPress packages scripts that are in the standardized
 260   * `js/dist/` location.
 261   *
 262   * For the order of `$scripts->add` see `wp_default_scripts`.
 263   *
 264   * @since 5.0.0
 265   *
 266   * @param WP_Scripts $scripts WP_Scripts object.
 267   */
 268  function wp_default_packages_scripts( $scripts ) {
 269      $suffix = defined( 'WP_RUN_CORE_TESTS' ) ? '.min' : wp_scripts_get_suffix();
 270      /*
 271       * Expects multidimensional array like:
 272       *
 273       *     'a11y.js' => array('dependencies' => array(...), 'version' => '...'),
 274       *     'annotations.js' => array('dependencies' => array(...), 'version' => '...'),
 275       *     'api-fetch.js' => array(...
 276       */
 277      $assets_file = ABSPATH . WPINC . '/assets/script-loader-packages.php';
 278      $assets      = file_exists( $assets_file ) ? include $assets_file : array();
 279  
 280      foreach ( $assets as $file_name => $package_data ) {
 281          $basename = str_replace( '.js', '', basename( $file_name ) );
 282          $handle   = 'wp-' . $basename;
 283          $path     = "/wp-includes/js/dist/{$basename}{$suffix}.js";
 284  
 285          if ( ! empty( $package_data['dependencies'] ) ) {
 286              $dependencies = $package_data['dependencies'];
 287          } else {
 288              $dependencies = array();
 289          }
 290  
 291          // Add dependencies that cannot be detected and generated by build tools.
 292          switch ( $handle ) {
 293              case 'wp-block-library':
 294                  array_push( $dependencies, 'editor' );
 295                  break;
 296              case 'wp-edit-post':
 297                  array_push( $dependencies, 'media-models', 'media-views', 'postbox', 'wp-dom-ready' );
 298                  break;
 299              case 'wp-preferences':
 300                  array_push( $dependencies, 'wp-preferences-persistence' );
 301                  break;
 302          }
 303  
 304          $scripts->add( $handle, $path, $dependencies, $package_data['version'], 1 );
 305  
 306          if ( ! empty( $package_data['module_dependencies'] ) ) {
 307              $scripts->add_data( $handle, 'module_dependencies', $package_data['module_dependencies'] );
 308          }
 309  
 310          if ( in_array( 'wp-i18n', $dependencies, true ) ) {
 311              $scripts->set_translations( $handle );
 312          }
 313  
 314          /*
 315           * Manually set the text direction localization after wp-i18n is printed.
 316           * This ensures that wp.i18n.isRTL() returns true in RTL languages.
 317           * We cannot use $scripts->set_translations( 'wp-i18n' ) to do this
 318           * because WordPress prints a script's translations *before* the script,
 319           * which means, in the case of wp-i18n, that wp.i18n.setLocaleData()
 320           * is called before wp.i18n is defined.
 321           */
 322          if ( 'wp-i18n' === $handle ) {
 323              $ltr    = _x( 'ltr', 'text direction' );
 324              $script = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] } );", $ltr );
 325              $scripts->add_inline_script( $handle, $script, 'after' );
 326          }
 327      }
 328  }
 329  
 330  /**
 331   * Adds inline scripts required for the WordPress JavaScript packages.
 332   *
 333   * @since 5.0.0
 334   * @since 6.4.0 Added relative time strings for the `wp-date` inline script output.
 335   *
 336   * @global WP_Locale $wp_locale WordPress date and time locale object.
 337   * @global wpdb      $wpdb      WordPress database abstraction object.
 338   *
 339   * @param WP_Scripts $scripts WP_Scripts object.
 340   */
 341  function wp_default_packages_inline_scripts( $scripts ) {
 342      global $wp_locale, $wpdb;
 343  
 344      if ( isset( $scripts->registered['wp-api-fetch'] ) ) {
 345          $scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
 346      }
 347      $scripts->add_inline_script(
 348          'wp-api-fetch',
 349          sprintf(
 350              'wp.apiFetch.use( wp.apiFetch.createRootURLMiddleware( "%s" ) );',
 351              sanitize_url( get_rest_url() )
 352          ),
 353          'after'
 354      );
 355      $scripts->add_inline_script(
 356          'wp-api-fetch',
 357          implode(
 358              "\n",
 359              array(
 360                  sprintf(
 361                      'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
 362                      wp_installing() ? '' : wp_create_nonce( 'wp_rest' )
 363                  ),
 364                  'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',
 365                  'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
 366                  sprintf(
 367                      'wp.apiFetch.nonceEndpoint = "%s";',
 368                      admin_url( 'admin-ajax.php?action=rest-nonce' )
 369                  ),
 370              )
 371          ),
 372          'after'
 373      );
 374  
 375      $meta_key     = $wpdb->get_blog_prefix() . 'persisted_preferences';
 376      $user_id      = get_current_user_id();
 377      $preload_data = get_user_meta( $user_id, $meta_key, true );
 378      $scripts->add_inline_script(
 379          'wp-preferences',
 380          sprintf(
 381              '( function() {
 382                  var serverData = %s;
 383                  var userId = "%d";
 384                  var persistenceLayer = wp.preferencesPersistence.__unstableCreatePersistenceLayer( serverData, userId );
 385                  var preferencesStore = wp.preferences.store;
 386                  wp.data.dispatch( preferencesStore ).setPersistenceLayer( persistenceLayer );
 387              } ) ();',
 388              wp_json_encode( $preload_data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
 389              $user_id
 390          )
 391      );
 392  
 393      // Backwards compatibility - configure the old wp-data persistence system.
 394      $scripts->add_inline_script(
 395          'wp-data',
 396          implode(
 397              "\n",
 398              array(
 399                  '( function() {',
 400                  '    var userId = ' . get_current_user_id() . ';',
 401                  '    var storageKey = "WP_DATA_USER_" + userId;',
 402                  '    wp.data',
 403                  '        .use( wp.data.plugins.persistence, { storageKey: storageKey } );',
 404                  '} )();',
 405              )
 406          )
 407      );
 408  
 409      // Calculate the timezone abbr (EDT, PST) if possible.
 410      $timezone_string = get_option( 'timezone_string', 'UTC' );
 411      $timezone_abbr   = '';
 412  
 413      if ( ! empty( $timezone_string ) ) {
 414          $timezone_date = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
 415          $timezone_abbr = $timezone_date->format( 'T' );
 416      }
 417  
 418      $gmt_offset = get_option( 'gmt_offset', 0 );
 419  
 420      $scripts->add_inline_script(
 421          'wp-date',
 422          sprintf(
 423              'wp.date.setSettings( %s );',
 424              wp_json_encode(
 425                  array(
 426                      'l10n'     => array(
 427                          'locale'        => get_user_locale(),
 428                          'months'        => array_values( $wp_locale->month ),
 429                          'monthsShort'   => array_values( $wp_locale->month_abbrev ),
 430                          'weekdays'      => array_values( $wp_locale->weekday ),
 431                          'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
 432                          'meridiem'      => (object) $wp_locale->meridiem,
 433                          'relative'      => array(
 434                              /* translators: %s: Duration. */
 435                              'future' => __( '%s from now' ),
 436                              /* translators: %s: Duration. */
 437                              'past'   => __( '%s ago' ),
 438                              /* translators: One second from or to a particular datetime, e.g., "a second ago" or "a second from now". */
 439                              's'      => __( 'a second' ),
 440                              /* translators: %d: Duration in seconds from or to a particular datetime, e.g., "4 seconds ago" or "4 seconds from now". */
 441                              'ss'     => __( '%d seconds' ),
 442                              /* translators: One minute from or to a particular datetime, e.g., "a minute ago" or "a minute from now". */
 443                              'm'      => __( 'a minute' ),
 444                              /* translators: %d: Duration in minutes from or to a particular datetime, e.g., "4 minutes ago" or "4 minutes from now". */
 445                              'mm'     => __( '%d minutes' ),
 446                              /* translators: One hour from or to a particular datetime, e.g., "an hour ago" or "an hour from now". */
 447                              'h'      => __( 'an hour' ),
 448                              /* translators: %d: Duration in hours from or to a particular datetime, e.g., "4 hours ago" or "4 hours from now". */
 449                              'hh'     => __( '%d hours' ),
 450                              /* translators: One day from or to a particular datetime, e.g., "a day ago" or "a day from now". */
 451                              'd'      => __( 'a day' ),
 452                              /* translators: %d: Duration in days from or to a particular datetime, e.g., "4 days ago" or "4 days from now". */
 453                              'dd'     => __( '%d days' ),
 454                              /* translators: One month from or to a particular datetime, e.g., "a month ago" or "a month from now". */
 455                              'M'      => __( 'a month' ),
 456                              /* translators: %d: Duration in months from or to a particular datetime, e.g., "4 months ago" or "4 months from now". */
 457                              'MM'     => __( '%d months' ),
 458                              /* translators: One year from or to a particular datetime, e.g., "a year ago" or "a year from now". */
 459                              'y'      => __( 'a year' ),
 460                              /* translators: %d: Duration in years from or to a particular datetime, e.g., "4 years ago" or "4 years from now". */
 461                              'yy'     => __( '%d years' ),
 462                          ),
 463                          'startOfWeek'   => (int) get_option( 'start_of_week', 0 ),
 464                      ),
 465                      'formats'  => array(
 466                          /* translators: Time format, see https://www.php.net/manual/datetime.format.php */
 467                          'time'                => get_option( 'time_format', __( 'g:i a' ) ),
 468                          /* translators: Date format, see https://www.php.net/manual/datetime.format.php */
 469                          'date'                => get_option( 'date_format', __( 'F j, Y' ) ),
 470                          /* translators: Date/Time format, see https://www.php.net/manual/datetime.format.php */
 471                          'datetime'            => __( 'F j, Y g:i a' ),
 472                          /* translators: Abbreviated date/time format, see https://www.php.net/manual/datetime.format.php */
 473                          'datetimeAbbreviated' => __( 'M j, Y g:i a' ),
 474                      ),
 475                      'timezone' => array(
 476                          'offset'          => (float) $gmt_offset,
 477                          'offsetFormatted' => str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), (string) $gmt_offset ),
 478                          'string'          => $timezone_string,
 479                          'abbr'            => $timezone_abbr,
 480                      ),
 481                  ),
 482                  JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
 483              )
 484          ),
 485          'after'
 486      );
 487  
 488      // Loading the old editor and its config to ensure the classic block works as expected.
 489      $scripts->add_inline_script(
 490          'editor',
 491          'window.wp.oldEditor = window.wp.editor;',
 492          'after'
 493      );
 494  
 495      /*
 496       * wp-editor module is exposed as window.wp.editor.
 497       * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor.
 498       * Solution: fuse the two objects together to maintain backward compatibility.
 499       * For more context, see https://github.com/WordPress/gutenberg/issues/33203.
 500       */
 501      $scripts->add_inline_script(
 502          'wp-editor',
 503          'Object.assign( window.wp.editor, window.wp.oldEditor );',
 504          'after'
 505      );
 506  }
 507  
 508  /**
 509   * Adds inline scripts required for the TinyMCE in the block editor.
 510   *
 511   * These TinyMCE init settings are used to extend and override the default settings
 512   * from `_WP_Editors::default_settings()` for the Classic block.
 513   *
 514   * @since 5.0.0
 515   *
 516   * @global WP_Scripts $wp_scripts
 517   */
 518  function wp_tinymce_inline_scripts() {
 519      global $wp_scripts;
 520  
 521      /** This filter is documented in wp-includes/class-wp-editor.php */
 522      $editor_settings = apply_filters( 'wp_editor_settings', array( 'tinymce' => true ), 'classic-block' );
 523  
 524      $tinymce_plugins = array(
 525          'charmap',
 526          'colorpicker',
 527          'hr',
 528          'lists',
 529          'media',
 530          'paste',
 531          'tabfocus',
 532          'textcolor',
 533          'fullscreen',
 534          'wordpress',
 535          'wpautoresize',
 536          'wpeditimage',
 537          'wpemoji',
 538          'wpgallery',
 539          'wplink',
 540          'wpdialogs',
 541          'wptextpattern',
 542          'wpview',
 543      );
 544  
 545      /** This filter is documented in wp-includes/class-wp-editor.php */
 546      $tinymce_plugins = apply_filters( 'tiny_mce_plugins', $tinymce_plugins, 'classic-block' );
 547      $tinymce_plugins = array_unique( $tinymce_plugins );
 548  
 549      $disable_captions = false;
 550      // Runs after `tiny_mce_plugins` but before `mce_buttons`.
 551      /** This filter is documented in wp-admin/includes/media.php */
 552      if ( apply_filters( 'disable_captions', '' ) ) {
 553          $disable_captions = true;
 554      }
 555  
 556      $toolbar1 = array(
 557          'formatselect',
 558          'bold',
 559          'italic',
 560          'bullist',
 561          'numlist',
 562          'blockquote',
 563          'alignleft',
 564          'aligncenter',
 565          'alignright',
 566          'link',
 567          'unlink',
 568          'wp_more',
 569          'spellchecker',
 570          'wp_add_media',
 571          'wp_adv',
 572      );
 573  
 574      /** This filter is documented in wp-includes/class-wp-editor.php */
 575      $toolbar1 = apply_filters( 'mce_buttons', $toolbar1, 'classic-block' );
 576  
 577      $toolbar2 = array(
 578          'strikethrough',
 579          'hr',
 580          'forecolor',
 581          'pastetext',
 582          'removeformat',
 583          'charmap',
 584          'outdent',
 585          'indent',
 586          'undo',
 587          'redo',
 588          'wp_help',
 589      );
 590  
 591      /** This filter is documented in wp-includes/class-wp-editor.php */
 592      $toolbar2 = apply_filters( 'mce_buttons_2', $toolbar2, 'classic-block' );
 593      /** This filter is documented in wp-includes/class-wp-editor.php */
 594      $toolbar3 = apply_filters( 'mce_buttons_3', array(), 'classic-block' );
 595      /** This filter is documented in wp-includes/class-wp-editor.php */
 596      $toolbar4 = apply_filters( 'mce_buttons_4', array(), 'classic-block' );
 597      /** This filter is documented in wp-includes/class-wp-editor.php */
 598      $external_plugins = apply_filters( 'mce_external_plugins', array(), 'classic-block' );
 599  
 600      $tinymce_settings = array(
 601          'plugins'              => implode( ',', $tinymce_plugins ),
 602          'toolbar1'             => implode( ',', $toolbar1 ),
 603          'toolbar2'             => implode( ',', $toolbar2 ),
 604          'toolbar3'             => implode( ',', $toolbar3 ),
 605          'toolbar4'             => implode( ',', $toolbar4 ),
 606          'external_plugins'     => wp_json_encode( $external_plugins ),
 607          'classic_block_editor' => true,
 608      );
 609  
 610      if ( $disable_captions ) {
 611          $tinymce_settings['wpeditimage_disable_captions'] = true;
 612      }
 613  
 614      if ( ! empty( $editor_settings['tinymce'] ) && is_array( $editor_settings['tinymce'] ) ) {
 615          $tinymce_settings = array_merge( $tinymce_settings, $editor_settings['tinymce'] );
 616      }
 617  
 618      /** This filter is documented in wp-includes/class-wp-editor.php */
 619      $tinymce_settings = apply_filters( 'tiny_mce_before_init', $tinymce_settings, 'classic-block' );
 620  
 621      /*
 622       * Do "by hand" translation from PHP array to js object.
 623       * Prevents breakage in some custom settings.
 624       */
 625      $init_obj = '';
 626      foreach ( $tinymce_settings as $key => $value ) {
 627          if ( is_bool( $value ) ) {
 628              $val       = $value ? 'true' : 'false';
 629              $init_obj .= $key . ':' . $val . ',';
 630              continue;
 631          } elseif ( ! empty( $value ) && is_string( $value ) && (
 632              ( '{' === $value[0] && '}' === $value[ strlen( $value ) - 1 ] ) ||
 633              ( '[' === $value[0] && ']' === $value[ strlen( $value ) - 1 ] ) ||
 634              preg_match( '/^\(?function ?\(/', $value ) ) ) {
 635              $init_obj .= $key . ':' . $value . ',';
 636              continue;
 637          }
 638          $init_obj .= $key . ':"' . $value . '",';
 639      }
 640  
 641      $init_obj = '{' . trim( $init_obj, ' ,' ) . '}';
 642  
 643      $script = 'window.wpEditorL10n = {
 644          tinymce: {
 645              baseURL: ' . wp_json_encode( includes_url( 'js/tinymce' ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ',
 646              suffix: ' . ( SCRIPT_DEBUG ? '""' : '".min"' ) . ',
 647              settings: ' . $init_obj . ',
 648          }
 649      }';
 650  
 651      $wp_scripts->add_inline_script( 'wp-block-library', $script, 'before' );
 652  }
 653  
 654  /**
 655   * Registers all the WordPress packages scripts.
 656   *
 657   * @since 5.0.0
 658   *
 659   * @param WP_Scripts $scripts WP_Scripts object.
 660   */
 661  function wp_default_packages( $scripts ) {
 662      wp_default_packages_vendor( $scripts );
 663      wp_register_development_scripts( $scripts );
 664      wp_register_tinymce_scripts( $scripts );
 665      wp_default_packages_scripts( $scripts );
 666  
 667      if ( did_action( 'init' ) ) {
 668          wp_default_packages_inline_scripts( $scripts );
 669      }
 670  }
 671  
 672  /**
 673   * Returns the suffix that can be used for the scripts.
 674   *
 675   * There are two suffix types, the normal one and the dev suffix.
 676   *
 677   * @since 5.0.0
 678   *
 679   * @param string $type The type of suffix to retrieve.
 680   * @return string The script suffix.
 681   *
 682   * @phpstan-return ''|'.min'
 683   */
 684  function wp_scripts_get_suffix( $type = '' ) {
 685      /** @var array{ suffix: ''|'.min', dev_suffix: ''|'.min' }|null $suffixes */
 686      static $suffixes;
 687  
 688      if ( null === $suffixes ) {
 689          $develop_src = str_contains( wp_get_wp_version(), '-src' );
 690  
 691          if ( ! defined( 'SCRIPT_DEBUG' ) ) {
 692              define( 'SCRIPT_DEBUG', $develop_src );
 693          }
 694          $suffix     = SCRIPT_DEBUG ? '' : '.min';
 695          $dev_suffix = $develop_src ? '' : '.min';
 696  
 697          $suffixes = array(
 698              'suffix'     => $suffix,
 699              'dev_suffix' => $dev_suffix,
 700          );
 701      }
 702  
 703      if ( 'dev' === $type ) {
 704          return $suffixes['dev_suffix'];
 705      }
 706  
 707      return $suffixes['suffix'];
 708  }
 709  
 710  /**
 711   * Registers all WordPress scripts.
 712   *
 713   * Localizes some of them.
 714   * args order: `$scripts->add( 'handle', 'url', 'dependencies', 'query-string', 1 );`
 715   * when last arg === 1 queues the script for the footer
 716   *
 717   * @since 2.6.0
 718   *
 719   * @param WP_Scripts $scripts WP_Scripts object.
 720   */
 721  function wp_default_scripts( $scripts ) {
 722      $suffix     = wp_scripts_get_suffix();
 723      $dev_suffix = wp_scripts_get_suffix( 'dev' );
 724      $guessurl   = site_url();
 725  
 726      if ( ! $guessurl ) {
 727          $guessed_url = true;
 728          $guessurl    = wp_guess_url();
 729      }
 730  
 731      $scripts->base_url        = $guessurl;
 732      $scripts->content_url     = defined( 'WP_CONTENT_URL' ) ? WP_CONTENT_URL : '';
 733      $scripts->default_version = get_bloginfo( 'version' );
 734      $scripts->default_dirs    = array( '/wp-admin/js/', '/wp-includes/js/' ); // Deprecated, but still set for plugins that read it.
 735  
 736      $scripts->add( 'utils', "/wp-includes/js/utils$suffix.js" );
 737      did_action( 'init' ) && $scripts->localize(
 738          'utils',
 739          'userSettings',
 740          array(
 741              'url'    => (string) SITECOOKIEPATH,
 742              'uid'    => (string) get_current_user_id(),
 743              'time'   => (string) time(),
 744              'secure' => (string) ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) ),
 745          )
 746      );
 747  
 748      $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array( 'jquery', 'hoverIntent', 'utils', 'wp-a11y' ), false, 1 );
 749      $scripts->set_translations( 'common' );
 750  
 751      $bulk_action_observer_ids = array(
 752          'bulk_action' => 'action',
 753          'changeit'    => 'new_role',
 754      );
 755      did_action( 'init' ) && $scripts->localize(
 756          'common',
 757          'bulkActionObserverIds',
 758          /**
 759           * Filters the array of field name attributes for bulk actions.
 760           *
 761           * @since 6.8.1
 762           *
 763           * @param array $bulk_action_observer_ids {
 764           *      An array of field name attributes for bulk actions.
 765           *
 766           *      @type string $bulk_action The bulk action field name. Default 'action'.
 767           *      @type string $changeit    The new role field name. Default 'new_role'.
 768           * }
 769           */
 770          apply_filters( 'bulk_action_observer_ids', $bulk_action_observer_ids )
 771      );
 772  
 773      $scripts->add( 'wp-sanitize', "/wp-includes/js/wp-sanitize$suffix.js", array(), false, 1 );
 774  
 775      $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", array(), '1.6.1', 1 );
 776  
 777      $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", array(), false, 1 );
 778      did_action( 'init' ) && $scripts->localize(
 779          'quicktags',
 780          'quicktagsL10n',
 781          array(
 782              'closeAllOpenTags'      => __( 'Close all open tags' ),
 783              'closeTags'             => __( 'close tags' ),
 784              'enterURL'              => __( 'Enter the URL' ),
 785              'enterImageURL'         => __( 'Enter the URL of the image' ),
 786              'enterImageDescription' => __( 'Enter a description of the image' ),
 787              'textdirection'         => __( 'text direction' ),
 788              'toggleTextdirection'   => __( 'Switch Editor Text Direction' ),
 789              'dfw'                   => __( 'Distraction-free writing mode' ),
 790              'strong'                => __( 'Bold' ),
 791              'strongClose'           => __( 'Close bold tag' ),
 792              'em'                    => __( 'Italic' ),
 793              'emClose'               => __( 'Close italic tag' ),
 794              'link'                  => __( 'Insert link' ),
 795              'blockquote'            => __( 'Blockquote' ),
 796              'blockquoteClose'       => __( 'Close blockquote tag' ),
 797              'del'                   => __( 'Deleted text (strikethrough)' ),
 798              'delClose'              => __( 'Close deleted text tag' ),
 799              'ins'                   => __( 'Inserted text' ),
 800              'insClose'              => __( 'Close inserted text tag' ),
 801              'image'                 => __( 'Insert image' ),
 802              'ul'                    => __( 'Bulleted list' ),
 803              'ulClose'               => __( 'Close bulleted list tag' ),
 804              'ol'                    => __( 'Numbered list' ),
 805              'olClose'               => __( 'Close numbered list tag' ),
 806              'li'                    => __( 'List item' ),
 807              'liClose'               => __( 'Close list item tag' ),
 808              'code'                  => __( 'Code' ),
 809              'codeClose'             => __( 'Close code tag' ),
 810              'more'                  => __( 'Insert Read More tag' ),
 811          )
 812      );
 813  
 814      $scripts->add( 'colorpicker', "/wp-includes/js/colorpicker$suffix.js", array( 'prototype' ), '3517m' );
 815  
 816      $scripts->add( 'editor', "/wp-admin/js/editor$suffix.js", array( 'utils', 'jquery' ), false, 1 );
 817  
 818      $scripts->add( 'clipboard', "/wp-includes/js/clipboard$suffix.js", array(), '2.0.11', 1 );
 819  
 820      $scripts->add( 'wp-ajax-response', "/wp-includes/js/wp-ajax-response$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
 821      did_action( 'init' ) && $scripts->localize(
 822          'wp-ajax-response',
 823          'wpAjax',
 824          array(
 825              'noPerm' => __( 'Sorry, you are not allowed to do that.' ),
 826              'broken' => __( 'An error occurred while processing your request. Please try again later.' ),
 827          )
 828      );
 829  
 830      $scripts->add( 'wp-api-request', "/wp-includes/js/api-request$suffix.js", array( 'jquery' ), false, 1 );
 831      // `wpApiSettings` is also used by `wp-api`, which depends on this script.
 832      did_action( 'init' ) && $scripts->localize(
 833          'wp-api-request',
 834          'wpApiSettings',
 835          array(
 836              'root'          => sanitize_url( get_rest_url() ),
 837              'nonce'         => wp_installing() ? '' : wp_create_nonce( 'wp_rest' ),
 838              'versionString' => 'wp/v2/',
 839          )
 840      );
 841  
 842      $scripts->add( 'wp-pointer', "/wp-includes/js/wp-pointer$suffix.js", array( 'jquery-ui-core' ), false, 1 );
 843      $scripts->set_translations( 'wp-pointer' );
 844  
 845      $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array( 'heartbeat' ), false, 1 );
 846  
 847      $scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array( 'jquery', 'wp-hooks' ), false, 1 );
 848      did_action( 'init' ) && $scripts->localize(
 849          'heartbeat',
 850          'heartbeatSettings',
 851          /**
 852           * Filters the Heartbeat settings.
 853           *
 854           * @since 3.6.0
 855           *
 856           * @param array $settings Heartbeat settings array.
 857           */
 858          apply_filters( 'heartbeat_settings', array() )
 859      );
 860  
 861      $scripts->add( 'wp-auth-check', "/wp-includes/js/wp-auth-check$suffix.js", array( 'heartbeat' ), false, 1 );
 862      $scripts->set_translations( 'wp-auth-check' );
 863  
 864      $scripts->add( 'wp-tooltip', '/wp-includes/js/wp-tooltip.js', array(), false, 1 );
 865  
 866      $scripts->add( 'wp-lists', "/wp-includes/js/wp-lists$suffix.js", array( 'wp-ajax-response', 'jquery-color' ), false, 1 );
 867  
 868      $scripts->add( 'site-icon', '/wp-admin/js/site-icon.js', array( 'jquery' ), false, 1 );
 869      $scripts->set_translations( 'site-icon' );
 870  
 871      // WordPress no longer uses or bundles Prototype or script.aculo.us. These are now pulled from an external source.
 872      $scripts->add( 'prototype', 'https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js', array(), '1.7.1' );
 873      $scripts->add( 'scriptaculous-root', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/scriptaculous.js', array( 'prototype' ), '1.9.0' );
 874      $scripts->add( 'scriptaculous-builder', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/builder.js', array( 'scriptaculous-root' ), '1.9.0' );
 875      $scripts->add( 'scriptaculous-dragdrop', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/dragdrop.js', array( 'scriptaculous-builder', 'scriptaculous-effects' ), '1.9.0' );
 876      $scripts->add( 'scriptaculous-effects', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/effects.js', array( 'scriptaculous-root' ), '1.9.0' );
 877      $scripts->add( 'scriptaculous-slider', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/slider.js', array( 'scriptaculous-effects' ), '1.9.0' );
 878      $scripts->add( 'scriptaculous-sound', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/sound.js', array( 'scriptaculous-root' ), '1.9.0' );
 879      $scripts->add( 'scriptaculous-controls', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/controls.js', array( 'scriptaculous-root' ), '1.9.0' );
 880      $scripts->add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );
 881  
 882      // Not used in core, replaced by Jcrop.js.
 883      $scripts->add( 'cropper', '/wp-includes/js/crop/cropper.js', array( 'scriptaculous-dragdrop' ) );
 884  
 885      /*
 886       * jQuery.
 887       * The unminified jquery.js and jquery-migrate.js are included to facilitate debugging.
 888       */
 889      $scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '3.7.1' );
 890      $scripts->add( 'jquery-core', "/wp-includes/js/jquery/jquery$suffix.js", array(), '3.7.1' );
 891      $scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '3.6.0' );
 892  
 893      /*
 894       * Full jQuery UI.
 895       * The build process in 1.12.1 has changed significantly.
 896       * In order to keep backwards compatibility, and to keep the optimized loading,
 897       * the source files were flattened and included with some modifications for AMD loading.
 898       * A notable change is that 'jquery-ui-core' now contains 'jquery-ui-position' and 'jquery-ui-widget'.
 899       */
 900      $scripts->add( 'jquery-ui-core', "/wp-includes/js/jquery/ui/core$suffix.js", array( 'jquery' ), '1.14.2', 1 );
 901      $scripts->add_inline_script( 'jquery-ui-core', 'jQuery.uiBackCompat = true;', 'before' );
 902      $scripts->add( 'jquery-effects-core', "/wp-includes/js/jquery/ui/effect$suffix.js", array( 'jquery' ), '1.14.2', 1 );
 903  
 904      $scripts->add( 'jquery-effects-blind', "/wp-includes/js/jquery/ui/effect-blind$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 905      $scripts->add( 'jquery-effects-bounce', "/wp-includes/js/jquery/ui/effect-bounce$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 906      $scripts->add( 'jquery-effects-clip', "/wp-includes/js/jquery/ui/effect-clip$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 907      $scripts->add( 'jquery-effects-drop', "/wp-includes/js/jquery/ui/effect-drop$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 908      $scripts->add( 'jquery-effects-explode', "/wp-includes/js/jquery/ui/effect-explode$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 909      $scripts->add( 'jquery-effects-fade', "/wp-includes/js/jquery/ui/effect-fade$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 910      $scripts->add( 'jquery-effects-fold', "/wp-includes/js/jquery/ui/effect-fold$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 911      $scripts->add( 'jquery-effects-highlight', "/wp-includes/js/jquery/ui/effect-highlight$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 912      $scripts->add( 'jquery-effects-puff', "/wp-includes/js/jquery/ui/effect-puff$suffix.js", array( 'jquery-effects-core', 'jquery-effects-scale' ), '1.14.2', 1 );
 913      $scripts->add( 'jquery-effects-pulsate', "/wp-includes/js/jquery/ui/effect-pulsate$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 914      $scripts->add( 'jquery-effects-scale', "/wp-includes/js/jquery/ui/effect-scale$suffix.js", array( 'jquery-effects-core', 'jquery-effects-size' ), '1.14.2', 1 );
 915      $scripts->add( 'jquery-effects-shake', "/wp-includes/js/jquery/ui/effect-shake$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 916      $scripts->add( 'jquery-effects-size', "/wp-includes/js/jquery/ui/effect-size$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 917      $scripts->add( 'jquery-effects-slide', "/wp-includes/js/jquery/ui/effect-slide$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 918      $scripts->add( 'jquery-effects-transfer', "/wp-includes/js/jquery/ui/effect-transfer$suffix.js", array( 'jquery-effects-core' ), '1.14.2', 1 );
 919  
 920      // Widgets
 921      $scripts->add( 'jquery-ui-accordion', "/wp-includes/js/jquery/ui/accordion$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 922      $scripts->add( 'jquery-ui-autocomplete', "/wp-includes/js/jquery/ui/autocomplete$suffix.js", array( 'jquery-ui-menu', 'wp-a11y' ), '1.14.2', 1 );
 923      $scripts->add( 'jquery-ui-button', "/wp-includes/js/jquery/ui/button$suffix.js", array( 'jquery-ui-core', 'jquery-ui-controlgroup', 'jquery-ui-checkboxradio' ), '1.14.2', 1 );
 924      $scripts->add( 'jquery-ui-datepicker', "/wp-includes/js/jquery/ui/datepicker$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 925      $scripts->add( 'jquery-ui-dialog', "/wp-includes/js/jquery/ui/dialog$suffix.js", array( 'jquery-ui-resizable', 'jquery-ui-draggable', 'jquery-ui-button' ), '1.14.2', 1 );
 926      $scripts->add( 'jquery-ui-menu', "/wp-includes/js/jquery/ui/menu$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 927      $scripts->add( 'jquery-ui-mouse', "/wp-includes/js/jquery/ui/mouse$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 928      $scripts->add( 'jquery-ui-progressbar', "/wp-includes/js/jquery/ui/progressbar$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 929      $scripts->add( 'jquery-ui-selectmenu', "/wp-includes/js/jquery/ui/selectmenu$suffix.js", array( 'jquery-ui-menu' ), '1.14.2', 1 );
 930      $scripts->add( 'jquery-ui-slider', "/wp-includes/js/jquery/ui/slider$suffix.js", array( 'jquery-ui-mouse' ), '1.14.2', 1 );
 931      $scripts->add( 'jquery-ui-spinner', "/wp-includes/js/jquery/ui/spinner$suffix.js", array( 'jquery-ui-button' ), '1.14.2', 1 );
 932      $scripts->add( 'jquery-ui-tabs', "/wp-includes/js/jquery/ui/tabs$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 933      $scripts->add( 'jquery-ui-tooltip', "/wp-includes/js/jquery/ui/tooltip$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 934  
 935      // Added in jQuery UI 1.12.1.
 936      $scripts->add( 'jquery-ui-checkboxradio', "/wp-includes/js/jquery/ui/checkboxradio$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 937      $scripts->add( 'jquery-ui-controlgroup', "/wp-includes/js/jquery/ui/controlgroup$suffix.js", array( 'jquery-ui-core' ), '1.14.2', 1 );
 938  
 939      // Interactions
 940      $scripts->add( 'jquery-ui-draggable', "/wp-includes/js/jquery/ui/draggable$suffix.js", array( 'jquery-ui-mouse' ), '1.14.2', 1 );
 941      $scripts->add( 'jquery-ui-droppable', "/wp-includes/js/jquery/ui/droppable$suffix.js", array( 'jquery-ui-draggable' ), '1.14.2', 1 );
 942      $scripts->add( 'jquery-ui-resizable', "/wp-includes/js/jquery/ui/resizable$suffix.js", array( 'jquery-ui-mouse' ), '1.14.2', 1 );
 943      $scripts->add( 'jquery-ui-selectable', "/wp-includes/js/jquery/ui/selectable$suffix.js", array( 'jquery-ui-mouse' ), '1.14.2', 1 );
 944      $scripts->add( 'jquery-ui-sortable', "/wp-includes/js/jquery/ui/sortable$suffix.js", array( 'jquery-ui-mouse' ), '1.14.2', 1 );
 945  
 946      /*
 947       * As of 1.12.1 `jquery-ui-position` and `jquery-ui-widget` are part of `jquery-ui-core`.
 948       * Listed here for back-compat.
 949       */
 950      $scripts->add( 'jquery-ui-position', false, array( 'jquery-ui-core' ), '1.14.2', 1 );
 951      $scripts->add( 'jquery-ui-widget', false, array( 'jquery-ui-core' ), '1.14.2', 1 );
 952  
 953      // Deprecated, not used in core, most functionality is included in jQuery 1.3.
 954      $scripts->add( 'jquery-form', "/wp-includes/js/jquery/jquery.form$suffix.js", array( 'jquery' ), '4.4.0', 1 );
 955  
 956      // jQuery plugins.
 957      $scripts->add( 'jquery-color', '/wp-includes/js/jquery/jquery.color.min.js', array( 'jquery' ), '3.0.0', 1 );
 958      $scripts->add( 'schedule', '/wp-includes/js/jquery/jquery.schedule.js', array( 'jquery' ), '20m', 1 );
 959      $scripts->add( 'jquery-query', '/wp-includes/js/jquery/jquery.query.js', array( 'jquery' ), '2.2.3', 1 );
 960      $scripts->add( 'jquery-serialize-object', '/wp-includes/js/jquery/jquery.serialize-object.js', array( 'jquery' ), '0.2-wp', 1 );
 961      $scripts->add( 'jquery-hotkeys', "/wp-includes/js/jquery/jquery.hotkeys$suffix.js", array( 'jquery' ), '0.0.2m', 1 );
 962      $scripts->add( 'jquery-table-hotkeys', "/wp-includes/js/jquery/jquery.table-hotkeys$suffix.js", array( 'jquery', 'jquery-hotkeys' ), false, 1 );
 963      $scripts->add( 'jquery-touch-punch', '/wp-includes/js/jquery/jquery.ui.touch-punch.js', array( 'jquery-ui-core', 'jquery-ui-mouse' ), '0.2.2', 1 );
 964  
 965      // Not used any more, registered for backward compatibility.
 966      $scripts->add( 'suggest', "/wp-includes/js/jquery/suggest$suffix.js", array( 'jquery' ), '1.1-20110113', 1 );
 967  
 968      /*
 969       * Masonry v2 depended on jQuery. v3 does not. The older jquery-masonry handle is a shiv.
 970       * It sets jQuery as a dependency, as the theme may have been implicitly loading it this way.
 971       */
 972      $scripts->add( 'imagesloaded', '/wp-includes/js/imagesloaded.min.js', array(), '5.0.0', 1 );
 973      $scripts->add( 'masonry', '/wp-includes/js/masonry.min.js', array( 'imagesloaded' ), '4.2.2', 1 );
 974      $scripts->add( 'jquery-masonry', '/wp-includes/js/jquery/jquery.masonry.min.js', array( 'jquery', 'masonry' ), '3.1.2b', 1 );
 975  
 976      $scripts->add( 'thickbox', '/wp-includes/js/thickbox/thickbox.js', array( 'jquery' ), '3.1-20121105', 1 );
 977      did_action( 'init' ) && $scripts->localize(
 978          'thickbox',
 979          'thickboxL10n',
 980          array(
 981              'next'             => __( 'Next &gt;' ),
 982              'prev'             => __( '&lt; Prev' ),
 983              'image'            => __( 'Image' ),
 984              'of'               => __( 'of' ),
 985              'close'            => __( 'Close' ),
 986              'noiframes'        => __( 'This feature requires inline frames. You have iframes disabled or your browser does not support them.' ),
 987              'loadingAnimation' => includes_url( 'js/thickbox/loadingAnimation.gif' ),
 988          )
 989      );
 990  
 991      // Not used in core, replaced by imgAreaSelect.
 992      $scripts->add( 'jcrop', '/wp-includes/js/jcrop/jquery.Jcrop.min.js', array( 'jquery' ), '0.9.15' );
 993  
 994      // Error messages for Plupload.
 995      $uploader_l10n = array(
 996          'queue_limit_exceeded'      => __( 'You have attempted to queue too many files.' ),
 997          /* translators: %s: File name. */
 998          'file_exceeds_size_limit'   => __( '%s exceeds the maximum upload size for this site.' ),
 999          'zero_byte_file'            => __( 'This file is empty. Please try another.' ),
1000          'invalid_filetype'          => __( 'This file cannot be processed by the web server.' ),
1001          'not_an_image'              => __( 'This file is not an image. Please try another.' ),
1002          'image_memory_exceeded'     => __( 'Memory exceeded. Please try another smaller file.' ),
1003          'image_dimensions_exceeded' => __( 'This is larger than the maximum size. Please try another.' ),
1004          'default_error'             => __( 'An error occurred in the upload. Please try again later.' ),
1005          'missing_upload_url'        => __( 'There was a configuration error. Please contact the server administrator.' ),
1006          'upload_limit_exceeded'     => __( 'You may only upload 1 file.' ),
1007          'http_error'                => __( 'Unexpected response from the server. The file may have been uploaded successfully. Check in the Media Library or reload the page.' ),
1008          'http_error_image'          => __( 'The server cannot process the image. This can happen if the server is busy or does not have enough resources to complete the task. Uploading a smaller image may help. Suggested maximum size is 2560 pixels.' ),
1009          'upload_failed'             => __( 'Upload failed.' ),
1010          /* translators: 1: Opening link tag, 2: Closing link tag. */
1011          'big_upload_failed'         => __( 'Please try uploading this file with the %1$sbrowser uploader%2$s.' ),
1012          /* translators: %s: File name. */
1013          'big_upload_queued'         => __( '%s exceeds the maximum upload size for the multi-file uploader when used in your browser.' ),
1014          'io_error'                  => __( 'IO error.' ),
1015          'security_error'            => __( 'Security error.' ),
1016          'file_cancelled'            => __( 'File canceled.' ),
1017          'upload_stopped'            => __( 'Upload stopped.' ),
1018          'dismiss'                   => __( 'Dismiss' ),
1019          'crunching'                 => __( 'Crunching&hellip;' ),
1020          'deleted'                   => __( 'moved to the Trash.' ),
1021          /* translators: %s: File name. */
1022          'error_uploading'           => __( '&#8220;%s&#8221; has failed to upload.' ),
1023          'unsupported_image'         => __( 'The server cannot process HEIC images. Convert it to JPEG before uploading.' ),
1024          'noneditable_image'         => __( 'The web server cannot generate responsive image sizes for this image. Convert it to JPEG or PNG before uploading.' ),
1025          'file_url_copied'           => __( 'The file URL has been copied to your clipboard' ),
1026      );
1027  
1028      $scripts->add( 'moxiejs', "/wp-includes/js/plupload/moxie$suffix.js", array(), '1.3.5.1' );
1029      $scripts->add( 'plupload', "/wp-includes/js/plupload/plupload$suffix.js", array( 'moxiejs' ), '2.1.9' );
1030      // Back compat handles:
1031      foreach ( array( 'all', 'html5', 'flash', 'silverlight', 'html4' ) as $handle ) {
1032          $scripts->add( "plupload-$handle", false, array( 'plupload' ), '2.1.1' );
1033      }
1034  
1035      $scripts->add( 'plupload-handlers', "/wp-includes/js/plupload/handlers$suffix.js", array( 'clipboard', 'jquery', 'plupload', 'underscore', 'wp-a11y', 'wp-i18n' ) );
1036      did_action( 'init' ) && $scripts->localize( 'plupload-handlers', 'pluploadL10n', $uploader_l10n );
1037  
1038      $scripts->add( 'wp-plupload', "/wp-includes/js/plupload/wp-plupload$suffix.js", array( 'plupload', 'jquery', 'media-models' ), false, 1 );
1039      did_action( 'init' ) && $scripts->localize( 'wp-plupload', 'pluploadL10n', $uploader_l10n );
1040  
1041      $scripts->add( 'comment-reply', "/wp-includes/js/comment-reply$suffix.js", array(), false, 1 );
1042      if ( did_action( 'init' ) ) {
1043          $scripts->add_data( 'comment-reply', 'strategy', 'async' );
1044          $scripts->add_data( 'comment-reply', 'fetchpriority', 'low' ); // In Chrome this is automatically low due to the async strategy, but in Firefox and Safari the priority is normal/medium.
1045      }
1046  
1047      // Not used in core, obsolete. Registered for backward compatibility.
1048      $scripts->add( 'json2', "/wp-includes/js/json2$suffix.js", array(), '2015-05-03' );
1049      did_action( 'init' ) && $scripts->add_data( 'json2', 'conditional', '_required-conditional-dependency_' );
1050  
1051      $scripts->add( 'underscore', "/wp-includes/js/underscore$dev_suffix.js", array(), '1.13.8', 1 );
1052      $scripts->add( 'backbone', "/wp-includes/js/backbone$dev_suffix.js", array( 'underscore', 'jquery' ), '1.6.1', 1 );
1053  
1054      $scripts->add( 'wp-util', "/wp-includes/js/wp-util$suffix.js", array( 'underscore', 'jquery' ), false, 1 );
1055      did_action( 'init' ) && $scripts->localize(
1056          'wp-util',
1057          '_wpUtilSettings',
1058          array(
1059              'ajax' => array(
1060                  'url' => admin_url( 'admin-ajax.php', 'relative' ),
1061              ),
1062          )
1063      );
1064  
1065      $scripts->add( 'wp-backbone', "/wp-includes/js/wp-backbone$suffix.js", array( 'backbone', 'wp-util' ), false, 1 );
1066  
1067      $scripts->add( 'revisions', "/wp-admin/js/revisions$suffix.js", array( 'wp-backbone', 'jquery-ui-slider', 'hoverIntent' ), false, 1 );
1068  
1069      $scripts->add( 'imgareaselect', "/wp-includes/js/imgareaselect/jquery.imgareaselect$suffix.js", array( 'jquery' ), false, 1 );
1070  
1071      $scripts->add( 'mediaelement', false, array( 'jquery', 'mediaelement-core', 'mediaelement-migrate' ), '4.2.17', 1 );
1072      $scripts->add( 'mediaelement-core', "/wp-includes/js/mediaelement/mediaelement-and-player$suffix.js", array(), '4.2.17', 1 );
1073      $scripts->add( 'mediaelement-migrate', "/wp-includes/js/mediaelement/mediaelement-migrate$suffix.js", array(), false, 1 );
1074  
1075      did_action( 'init' ) && $scripts->add_inline_script(
1076          'mediaelement-core',
1077          sprintf(
1078              'var mejsL10n = %s;',
1079              wp_json_encode(
1080                  array(
1081                      'language' => strtolower( strtok( determine_locale(), '_-' ) ),
1082                      'strings'  => array(
1083                          'mejs.download-file'       => __( 'Download File' ),
1084                          'mejs.install-flash'       => __( 'You are using a browser that does not have Flash player enabled or installed. Please turn on your Flash player plugin or download the latest version from https://get.adobe.com/flashplayer/' ),
1085                          'mejs.fullscreen'          => __( 'Fullscreen' ),
1086                          'mejs.play'                => __( 'Play' ),
1087                          'mejs.pause'               => __( 'Pause' ),
1088                          'mejs.time-slider'         => __( 'Time Slider' ),
1089                          'mejs.time-help-text'      => __( 'Use Left/Right Arrow keys to advance one second, Up/Down arrows to advance ten seconds.' ),
1090                          'mejs.live-broadcast'      => __( 'Live Broadcast' ),
1091                          'mejs.volume-help-text'    => __( 'Use Up/Down Arrow keys to increase or decrease volume.' ),
1092                          'mejs.unmute'              => __( 'Unmute' ),
1093                          'mejs.mute'                => __( 'Mute' ),
1094                          'mejs.volume-slider'       => __( 'Volume Slider' ),
1095                          'mejs.video-player'        => __( 'Video Player' ),
1096                          'mejs.audio-player'        => __( 'Audio Player' ),
1097                          'mejs.captions-subtitles'  => __( 'Captions/Subtitles' ),
1098                          'mejs.captions-chapters'   => __( 'Chapters' ),
1099                          'mejs.none'                => __( 'None' ),
1100                          'mejs.afrikaans'           => __( 'Afrikaans' ),
1101                          'mejs.albanian'            => __( 'Albanian' ),
1102                          'mejs.arabic'              => __( 'Arabic' ),
1103                          'mejs.belarusian'          => __( 'Belarusian' ),
1104                          'mejs.bulgarian'           => __( 'Bulgarian' ),
1105                          'mejs.catalan'             => __( 'Catalan' ),
1106                          'mejs.chinese'             => __( 'Chinese' ),
1107                          'mejs.chinese-simplified'  => __( 'Chinese (Simplified)' ),
1108                          'mejs.chinese-traditional' => __( 'Chinese (Traditional)' ),
1109                          'mejs.croatian'            => __( 'Croatian' ),
1110                          'mejs.czech'               => __( 'Czech' ),
1111                          'mejs.danish'              => __( 'Danish' ),
1112                          'mejs.dutch'               => __( 'Dutch' ),
1113                          'mejs.english'             => __( 'English' ),
1114                          'mejs.estonian'            => __( 'Estonian' ),
1115                          'mejs.filipino'            => __( 'Filipino' ),
1116                          'mejs.finnish'             => __( 'Finnish' ),
1117                          'mejs.french'              => __( 'French' ),
1118                          'mejs.galician'            => __( 'Galician' ),
1119                          'mejs.german'              => __( 'German' ),
1120                          'mejs.greek'               => __( 'Greek' ),
1121                          'mejs.haitian-creole'      => __( 'Haitian Creole' ),
1122                          'mejs.hebrew'              => __( 'Hebrew' ),
1123                          'mejs.hindi'               => __( 'Hindi' ),
1124                          'mejs.hungarian'           => __( 'Hungarian' ),
1125                          'mejs.icelandic'           => __( 'Icelandic' ),
1126                          'mejs.indonesian'          => __( 'Indonesian' ),
1127                          'mejs.irish'               => __( 'Irish' ),
1128                          'mejs.italian'             => __( 'Italian' ),
1129                          'mejs.japanese'            => __( 'Japanese' ),
1130                          'mejs.korean'              => __( 'Korean' ),
1131                          'mejs.latvian'             => __( 'Latvian' ),
1132                          'mejs.lithuanian'          => __( 'Lithuanian' ),
1133                          'mejs.macedonian'          => __( 'Macedonian' ),
1134                          'mejs.malay'               => __( 'Malay' ),
1135                          'mejs.maltese'             => __( 'Maltese' ),
1136                          'mejs.norwegian'           => __( 'Norwegian' ),
1137                          'mejs.persian'             => __( 'Persian' ),
1138                          'mejs.polish'              => __( 'Polish' ),
1139                          'mejs.portuguese'          => __( 'Portuguese' ),
1140                          'mejs.romanian'            => __( 'Romanian' ),
1141                          'mejs.russian'             => __( 'Russian' ),
1142                          'mejs.serbian'             => __( 'Serbian' ),
1143                          'mejs.slovak'              => __( 'Slovak' ),
1144                          'mejs.slovenian'           => __( 'Slovenian' ),
1145                          'mejs.spanish'             => __( 'Spanish' ),
1146                          'mejs.swahili'             => __( 'Swahili' ),
1147                          'mejs.swedish'             => __( 'Swedish' ),
1148                          'mejs.tagalog'             => __( 'Tagalog' ),
1149                          'mejs.thai'                => __( 'Thai' ),
1150                          'mejs.turkish'             => __( 'Turkish' ),
1151                          'mejs.ukrainian'           => __( 'Ukrainian' ),
1152                          'mejs.vietnamese'          => __( 'Vietnamese' ),
1153                          'mejs.welsh'               => __( 'Welsh' ),
1154                          'mejs.yiddish'             => __( 'Yiddish' ),
1155                      ),
1156                  ),
1157                  JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
1158              )
1159          ),
1160          'before'
1161      );
1162  
1163      $scripts->add( 'mediaelement-vimeo', '/wp-includes/js/mediaelement/renderers/vimeo.min.js', array( 'mediaelement' ), '4.2.17', 1 );
1164      $scripts->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.js", array( 'mediaelement' ), false, 1 );
1165      $mejs_settings = array(
1166          'pluginPath'            => includes_url( 'js/mediaelement/', 'relative' ),
1167          'classPrefix'           => 'mejs-',
1168          'stretching'            => 'responsive',
1169          /** This filter is documented in wp-includes/media.php */
1170          'audioShortcodeLibrary' => apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ),
1171          /** This filter is documented in wp-includes/media.php */
1172          'videoShortcodeLibrary' => apply_filters( 'wp_video_shortcode_library', 'mediaelement' ),
1173      );
1174      did_action( 'init' ) && $scripts->localize(
1175          'mediaelement',
1176          '_wpmejsSettings',
1177          /**
1178           * Filters the MediaElement configuration settings.
1179           *
1180           * @since 4.4.0
1181           *
1182           * @param array $mejs_settings MediaElement settings array.
1183           */
1184          apply_filters( 'mejs_settings', $mejs_settings )
1185      );
1186  
1187      $scripts->add( 'wp-codemirror', '/wp-includes/js/codemirror/codemirror.min.js', array(), '5.65.20' );
1188      $scripts->add( 'csslint', '/wp-includes/js/codemirror/csslint.js', array(), '1.0.5' );
1189      $scripts->add( 'esprima', '/wp-includes/js/codemirror/esprima.js', array(), '4.0.1' ); // Deprecated.
1190      $scripts->add( 'jshint', '/wp-includes/js/codemirror/fakejshint.js', array( 'esprima' ), '2.9.5' ); // Deprecated.
1191      $scripts->add( 'jsonlint', '/wp-includes/js/codemirror/jsonlint.js', array(), '1.6.3' );
1192      $scripts->add( 'htmlhint', '/wp-includes/js/codemirror/htmlhint.js', array(), '1.9.2' );
1193      $scripts->add( 'htmlhint-kses', '/wp-includes/js/codemirror/htmlhint-kses.js', array( 'htmlhint' ) );
1194      $scripts->add( 'code-editor', "/wp-admin/js/code-editor$suffix.js", array( 'jquery', 'wp-codemirror', 'underscore' ) );
1195      $scripts->add( 'wp-theme-plugin-editor', "/wp-admin/js/theme-plugin-editor$suffix.js", array( 'common', 'wp-util', 'wp-sanitize', 'jquery', 'jquery-ui-core', 'wp-a11y', 'underscore' ), false, 1 );
1196      $scripts->set_translations( 'wp-theme-plugin-editor' );
1197  
1198      $scripts->add( 'wp-playlist', "/wp-includes/js/mediaelement/wp-playlist$suffix.js", array( 'wp-util', 'backbone', 'mediaelement' ), false, 1 );
1199  
1200      $scripts->add( 'zxcvbn-async', "/wp-includes/js/zxcvbn-async$suffix.js" );
1201      did_action( 'init' ) && $scripts->localize(
1202          'zxcvbn-async',
1203          '_zxcvbnSettings',
1204          array(
1205              'src' => empty( $guessed_url ) ? includes_url( '/js/zxcvbn.min.js' ) : $scripts->base_url . '/wp-includes/js/zxcvbn.min.js',
1206          )
1207      );
1208  
1209      $scripts->add( 'password-strength-meter', "/wp-admin/js/password-strength-meter$suffix.js", array( 'jquery', 'zxcvbn-async' ), false, 1 );
1210      did_action( 'init' ) && $scripts->localize(
1211          'password-strength-meter',
1212          'pwsL10n',
1213          array(
1214              'unknown'  => _x( 'Password strength unknown', 'password strength' ),
1215              'short'    => _x( 'Very weak', 'password strength' ),
1216              'bad'      => _x( 'Weak', 'password strength' ),
1217              'good'     => _x( 'Medium', 'password strength' ),
1218              'strong'   => _x( 'Strong', 'password strength' ),
1219              'mismatch' => _x( 'Mismatch', 'password mismatch' ),
1220          )
1221      );
1222      $scripts->set_translations( 'password-strength-meter' );
1223  
1224      $scripts->add( 'password-toggle', "/wp-admin/js/password-toggle$suffix.js", array(), false, 1 );
1225      $scripts->set_translations( 'password-toggle' );
1226  
1227      $scripts->add( 'application-passwords', "/wp-admin/js/application-passwords$suffix.js", array( 'jquery', 'wp-util', 'wp-api-request', 'wp-date', 'wp-i18n', 'wp-hooks' ), false, 1 );
1228      $scripts->set_translations( 'application-passwords' );
1229  
1230      $scripts->add( 'auth-app', "/wp-admin/js/auth-app$suffix.js", array( 'jquery', 'wp-api-request', 'wp-i18n', 'wp-hooks' ), false, 1 );
1231      $scripts->set_translations( 'auth-app' );
1232  
1233      $scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'clipboard', 'jquery', 'password-strength-meter', 'wp-util', 'wp-a11y' ), false, 1 );
1234      $scripts->set_translations( 'user-profile' );
1235      $user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : 0;
1236      did_action( 'init' ) && $scripts->localize(
1237          'user-profile',
1238          'userProfileL10n',
1239          array(
1240              'user_id' => $user_id,
1241              'nonce'   => wp_installing() ? '' : wp_create_nonce( 'reset-password-for-' . $user_id ),
1242          )
1243      );
1244  
1245      $scripts->add( 'language-chooser', "/wp-admin/js/language-chooser$suffix.js", array( 'jquery' ), false, 1 );
1246  
1247      $scripts->add( 'user-suggest', "/wp-admin/js/user-suggest$suffix.js", array( 'jquery-ui-autocomplete' ), false, 1 );
1248  
1249      $scripts->add( 'admin-bar', "/wp-includes/js/admin-bar$suffix.js", array( 'hoverintent-js' ), false, 1 );
1250  
1251      $scripts->add( 'wplink', "/wp-includes/js/wplink$suffix.js", array( 'common', 'jquery', 'wp-a11y', 'wp-i18n' ), false, 1 );
1252      $scripts->set_translations( 'wplink' );
1253      did_action( 'init' ) && $scripts->localize(
1254          'wplink',
1255          'wpLinkL10n',
1256          array(
1257              'title'          => __( 'Insert/edit link' ),
1258              'update'         => __( 'Update' ),
1259              'save'           => __( 'Add Link' ),
1260              'noTitle'        => __( '(no title)' ),
1261              'noMatchesFound' => __( 'No results found.' ),
1262              'linkSelected'   => __( 'Link selected.' ),
1263              'linkInserted'   => __( 'Link inserted.' ),
1264              /* translators: Minimum input length in characters to start searching posts in the "Insert/edit link" modal. */
1265              'minInputLength' => (int) _x( '3', 'minimum input length for searching post links' ),
1266          )
1267      );
1268  
1269      $scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 );
1270  
1271      $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 );
1272  
1273      $scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 );
1274  
1275      $scripts->add( 'hoverIntent', "/wp-includes/js/hoverIntent$suffix.js", array( 'jquery' ), '1.10.2', 1 );
1276  
1277      // JS-only version of hoverintent (no dependencies).
1278      $scripts->add( 'hoverintent-js', '/wp-includes/js/hoverintent-js.min.js', array(), '2.2.1', 1 );
1279  
1280      $scripts->add( 'customize-base', "/wp-includes/js/customize-base$suffix.js", array( 'jquery', 'underscore' ), false, 1 );
1281      $scripts->add( 'customize-loader', "/wp-includes/js/customize-loader$suffix.js", array( 'customize-base' ), false, 1 );
1282      $scripts->add( 'customize-preview', "/wp-includes/js/customize-preview$suffix.js", array( 'wp-a11y', 'customize-base' ), false, 1 );
1283      $scripts->add( 'customize-models', '/wp-includes/js/customize-models.js', array( 'underscore', 'backbone' ), false, 1 );
1284      $scripts->add( 'customize-views', '/wp-includes/js/customize-views.js', array( 'jquery', 'underscore', 'imgareaselect', 'customize-models', 'media-editor', 'media-views' ), false, 1 );
1285      $scripts->add( 'customize-controls', "/wp-admin/js/customize-controls$suffix.js", array( 'customize-base', 'wp-a11y', 'wp-util', 'jquery-ui-core' ), false, 1 );
1286      did_action( 'init' ) && $scripts->localize(
1287          'customize-controls',
1288          '_wpCustomizeControlsL10n',
1289          array(
1290              'activate'                => __( 'Activate &amp; Publish' ),
1291              'save'                    => __( 'Save &amp; Publish' ), // @todo Remove as not required.
1292              'publish'                 => __( 'Publish' ),
1293              'published'               => __( 'Published' ),
1294              'saveDraft'               => __( 'Save Draft' ),
1295              'draftSaved'              => __( 'Draft Saved' ),
1296              'updating'                => __( 'Updating' ),
1297              'schedule'                => _x( 'Schedule', 'customizer changeset action/button label' ),
1298              'scheduled'               => _x( 'Scheduled', 'customizer changeset status' ),
1299              'invalid'                 => __( 'Invalid' ),
1300              'saveBeforeShare'         => __( 'Please save your changes in order to share the preview.' ),
1301              'futureDateError'         => __( 'You must supply a future date to schedule.' ),
1302              'saveAlert'               => __( 'The changes you made will be lost if you navigate away from this page.' ),
1303              'saved'                   => __( 'Saved' ),
1304              'cancel'                  => __( 'Cancel' ),
1305              'close'                   => __( 'Close' ),
1306              'action'                  => __( 'Action' ),
1307              'discardChanges'          => __( 'Discard changes' ),
1308              'cheatin'                 => __( 'An error occurred. Please try again later.' ),
1309              'notAllowedHeading'       => __( 'You need a higher level of permission.' ),
1310              'notAllowed'              => __( 'Sorry, you are not allowed to customize this site.' ),
1311              'previewIframeTitle'      => __( 'Site Preview' ),
1312              'loginIframeTitle'        => __( 'Session expired' ),
1313              'collapseSidebar'         => _x( 'Hide Controls', 'label for hide controls button without length constraints' ),
1314              'expandSidebar'           => _x( 'Show Controls', 'label for hide controls button without length constraints' ),
1315              'untitledBlogName'        => __( '(Untitled)' ),
1316              'unknownRequestFail'      => __( 'Looks like something&#8217;s gone wrong. Wait a couple seconds, and then try again.' ),
1317              'themeDownloading'        => __( 'Downloading your new theme&hellip;' ),
1318              'themePreviewWait'        => __( 'Setting up your live preview. This may take a bit.' ),
1319              'revertingChanges'        => __( 'Reverting unpublished changes&hellip;' ),
1320              'trashConfirm'            => __( 'Are you sure you want to discard your unpublished changes?' ),
1321              /* translators: %s: Display name of the user who has taken over the changeset in customizer. */
1322              'takenOverMessage'        => __( '%s has taken over and is currently customizing.' ),
1323              /* translators: %s: URL to the Customizer to load the autosaved version. */
1324              'autosaveNotice'          => __( 'There is a more recent autosave of your changes than the one you are previewing. <a href="%s">Restore the autosave</a>' ),
1325              'videoHeaderNotice'       => __( 'This theme does not support video headers on this page. Navigate to the front page or another page that supports video headers.' ),
1326              // Used for overriding the file types allowed in Plupload.
1327              'allowedFiles'            => __( 'Allowed Files' ),
1328              'customCssError'          => array(
1329                  /* translators: %d: Error count. */
1330                  'singular' => _n( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.', 1 ),
1331                  /* translators: %d: Error count. */
1332                  'plural'   => _n( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.', 2 ),
1333                  // @todo This is lacking, as some languages have a dedicated dual form. For proper handling of plurals in JS, see #20491.
1334              ),
1335              'pageOnFrontError'        => __( 'Homepage and posts page must be different.' ),
1336              'saveBlockedError'        => array(
1337                  /* translators: %s: Number of invalid settings. */
1338                  'singular' => _n( 'Unable to save due to %s invalid setting.', 'Unable to save due to %s invalid settings.', 1 ),
1339                  /* translators: %s: Number of invalid settings. */
1340                  'plural'   => _n( 'Unable to save due to %s invalid setting.', 'Unable to save due to %s invalid settings.', 2 ),
1341                  // @todo This is lacking, as some languages have a dedicated dual form. For proper handling of plurals in JS, see #20491.
1342              ),
1343              'scheduleDescription'     => __( 'Schedule your customization changes to publish ("go live") at a future date.' ),
1344              'themePreviewUnavailable' => __( 'Sorry, you cannot preview new themes when you have changes scheduled or saved as a draft. Please publish your changes, or wait until they publish to preview new themes.' ),
1345              'themeInstallUnavailable' => sprintf(
1346                  /* translators: %s: URL to Add Themes admin screen. */
1347                  __( 'You will not be able to install new themes from here yet since your install requires SFTP credentials. For now, please <a href="%s">add themes in the admin</a>.' ),
1348                  esc_url( admin_url( 'theme-install.php' ) )
1349              ),
1350              'publishSettings'         => __( 'Publish Settings' ),
1351              'invalidDate'             => __( 'Invalid date.' ),
1352              'invalidValue'            => __( 'Invalid value.' ),
1353              'blockThemeNotification'  => sprintf(
1354                  /* translators: 1: Link to Site Editor documentation on HelpHub, 2: HTML button. */
1355                  __( 'Hurray! Your theme supports site editing with blocks. <a href="%1$s">Tell me more</a>. %2$s' ),
1356                  __( 'https://wordpress.org/documentation/article/site-editor/' ),
1357                  sprintf(
1358                      '<button type="button" data-action="%1$s" class="button switch-to-editor">%2$s</button>',
1359                      esc_url( admin_url( 'site-editor.php' ) ),
1360                      __( 'Use Site Editor' )
1361                  )
1362              ),
1363          )
1364      );
1365      $scripts->add( 'customize-selective-refresh', "/wp-includes/js/customize-selective-refresh$suffix.js", array( 'jquery', 'wp-util', 'customize-preview' ), false, 1 );
1366  
1367      $scripts->add( 'customize-widgets', "/wp-admin/js/customize-widgets$suffix.js", array( 'jquery', 'jquery-ui-sortable', 'jquery-ui-droppable', 'wp-backbone', 'customize-controls' ), false, 1 );
1368      $scripts->add( 'customize-preview-widgets', "/wp-includes/js/customize-preview-widgets$suffix.js", array( 'jquery', 'wp-util', 'customize-preview', 'customize-selective-refresh' ), false, 1 );
1369  
1370      $scripts->add( 'customize-nav-menus', "/wp-admin/js/customize-nav-menus$suffix.js", array( 'jquery', 'wp-backbone', 'customize-controls', 'accordion', 'nav-menu', 'wp-sanitize' ), false, 1 );
1371      $scripts->add( 'customize-preview-nav-menus', "/wp-includes/js/customize-preview-nav-menus$suffix.js", array( 'jquery', 'wp-util', 'customize-preview', 'customize-selective-refresh' ), false, 1 );
1372  
1373      $scripts->add( 'wp-custom-header', "/wp-includes/js/wp-custom-header$suffix.js", array( 'wp-a11y' ), false, 1 );
1374  
1375      $scripts->add( 'accordion', "/wp-admin/js/accordion$suffix.js", array( 'jquery' ), false, 1 );
1376  
1377      $scripts->add( 'shortcode', "/wp-includes/js/shortcode$suffix.js", array( 'underscore' ), false, 1 );
1378      $scripts->add( 'media-models', "/wp-includes/js/media-models$suffix.js", array( 'wp-backbone' ), false, 1 );
1379      did_action( 'init' ) && $scripts->localize(
1380          'media-models',
1381          '_wpMediaModelsL10n',
1382          array(
1383              'settings' => array(
1384                  'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
1385                  'post'    => array( 'id' => 0 ),
1386              ),
1387          )
1388      );
1389  
1390      $scripts->add( 'wp-embed', "/wp-includes/js/wp-embed$suffix.js" );
1391      did_action( 'init' ) && $scripts->add_data( 'wp-embed', 'strategy', 'defer' );
1392  
1393      /*
1394       * To enqueue media-views or media-editor, call wp_enqueue_media().
1395       * Both rely on numerous settings, styles, and templates to operate correctly.
1396       */
1397      $scripts->add( 'media-views', "/wp-includes/js/media-views$suffix.js", array( 'utils', 'media-models', 'wp-plupload', 'jquery-ui-sortable', 'wp-mediaelement', 'wp-api-request', 'wp-a11y', 'clipboard' ), false, 1 );
1398      $scripts->set_translations( 'media-views' );
1399  
1400      $scripts->add( 'media-editor', "/wp-includes/js/media-editor$suffix.js", array( 'shortcode', 'media-views' ), false, 1 );
1401      $scripts->set_translations( 'media-editor' );
1402      $scripts->add( 'media-audiovideo', "/wp-includes/js/media-audiovideo$suffix.js", array( 'media-editor' ), false, 1 );
1403      $scripts->add( 'mce-view', "/wp-includes/js/mce-view$suffix.js", array( 'shortcode', 'jquery', 'media-views', 'media-audiovideo' ), false, 1 );
1404  
1405      $scripts->add( 'wp-api', "/wp-includes/js/wp-api$suffix.js", array( 'jquery', 'backbone', 'underscore', 'wp-api-request' ), false, 1 );
1406  
1407      if ( is_admin() ) {
1408          $scripts->add( 'admin-tags', "/wp-admin/js/tags$suffix.js", array( 'jquery', 'wp-ajax-response' ), false, 1 );
1409          $scripts->set_translations( 'admin-tags' );
1410  
1411          $scripts->add( 'admin-comments', "/wp-admin/js/edit-comments$suffix.js", array( 'wp-lists', 'quicktags', 'jquery-query', 'wp-a11y' ), false, 1 );
1412          $scripts->set_translations( 'admin-comments' );
1413          did_action( 'init' ) && $scripts->localize(
1414              'admin-comments',
1415              'adminCommentsSettings',
1416              array(
1417                  'hotkeys_highlight_first' => isset( $_GET['hotkeys_highlight_first'] ),
1418                  'hotkeys_highlight_last'  => isset( $_GET['hotkeys_highlight_last'] ),
1419              )
1420          );
1421  
1422          $scripts->add( 'xfn', "/wp-admin/js/xfn$suffix.js", array( 'jquery' ), false, 1 );
1423  
1424          $scripts->add( 'postbox', "/wp-admin/js/postbox$suffix.js", array( 'jquery-ui-sortable', 'wp-a11y', 'wp-tooltip' ), false, 1 );
1425          $scripts->set_translations( 'postbox' );
1426  
1427          $scripts->add( 'tags-box', "/wp-admin/js/tags-box$suffix.js", array( 'jquery', 'tags-suggest' ), false, 1 );
1428          $scripts->set_translations( 'tags-box' );
1429  
1430          $scripts->add( 'tags-suggest', "/wp-admin/js/tags-suggest$suffix.js", array( 'common', 'jquery-ui-autocomplete', 'wp-a11y', 'wp-i18n' ), false, 1 );
1431          $scripts->set_translations( 'tags-suggest' );
1432  
1433          $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count', 'wp-a11y', 'wp-sanitize', 'clipboard' ), false, 1 );
1434          $scripts->set_translations( 'post' );
1435  
1436          $scripts->add( 'editor-expand', "/wp-admin/js/editor-expand$suffix.js", array( 'jquery', 'underscore' ), false, 1 );
1437  
1438          $scripts->add( 'link', "/wp-admin/js/link$suffix.js", array( 'wp-lists', 'postbox' ), false, 1 );
1439  
1440          $scripts->add( 'comment', "/wp-admin/js/comment$suffix.js", array( 'jquery', 'postbox' ), false, 1 );
1441          $scripts->set_translations( 'comment' );
1442  
1443          $scripts->add( 'admin-gallery', "/wp-admin/js/gallery$suffix.js", array( 'jquery-ui-sortable' ) );
1444  
1445          $scripts->add( 'admin-widgets', "/wp-admin/js/widgets$suffix.js", array( 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'wp-a11y' ), false, 1 );
1446          $scripts->set_translations( 'admin-widgets' );
1447  
1448          $scripts->add( 'media-widgets', "/wp-admin/js/widgets/media-widgets$suffix.js", array( 'jquery', 'media-models', 'media-views', 'wp-api-request' ) );
1449          $scripts->add_inline_script( 'media-widgets', 'wp.mediaWidgets.init();', 'after' );
1450  
1451          $scripts->add( 'media-audio-widget', "/wp-admin/js/widgets/media-audio-widget$suffix.js", array( 'media-widgets', 'media-audiovideo' ) );
1452          $scripts->add( 'media-image-widget', "/wp-admin/js/widgets/media-image-widget$suffix.js", array( 'media-widgets' ) );
1453          $scripts->add( 'media-gallery-widget', "/wp-admin/js/widgets/media-gallery-widget$suffix.js", array( 'media-widgets' ) );
1454          $scripts->add( 'media-video-widget', "/wp-admin/js/widgets/media-video-widget$suffix.js", array( 'media-widgets', 'media-audiovideo', 'wp-api-request' ) );
1455          $scripts->add( 'text-widgets', "/wp-admin/js/widgets/text-widgets$suffix.js", array( 'jquery', 'backbone', 'editor', 'wp-util', 'wp-a11y' ) );
1456          $scripts->add( 'custom-html-widgets', "/wp-admin/js/widgets/custom-html-widgets$suffix.js", array( 'jquery', 'backbone', 'wp-util', 'jquery-ui-core', 'wp-a11y' ) );
1457  
1458          $scripts->add( 'theme', "/wp-admin/js/theme$suffix.js", array( 'wp-backbone', 'wp-a11y', 'customize-base' ), false, 1 );
1459          $scripts->set_translations( 'theme' );
1460  
1461          $scripts->add( 'inline-edit-post', "/wp-admin/js/inline-edit-post$suffix.js", array( 'jquery', 'tags-suggest', 'wp-a11y' ), false, 1 );
1462          $scripts->set_translations( 'inline-edit-post' );
1463  
1464          $scripts->add( 'inline-edit-tax', "/wp-admin/js/inline-edit-tax$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
1465          $scripts->set_translations( 'inline-edit-tax' );
1466  
1467          $scripts->add( 'plugin-install', "/wp-admin/js/plugin-install$suffix.js", array( 'jquery', 'jquery-ui-core', 'thickbox' ), false, 1 );
1468          $scripts->set_translations( 'plugin-install' );
1469  
1470          $scripts->add( 'site-health', "/wp-admin/js/site-health$suffix.js", array( 'clipboard', 'jquery', 'wp-util', 'wp-a11y', 'wp-api-request', 'wp-url', 'wp-i18n', 'wp-hooks' ), false, 1 );
1471          $scripts->set_translations( 'site-health' );
1472  
1473          $scripts->add( 'privacy-tools', "/wp-admin/js/privacy-tools$suffix.js", array( 'jquery', 'wp-a11y' ), false, 1 );
1474          $scripts->set_translations( 'privacy-tools' );
1475  
1476          $scripts->add( 'updates', "/wp-admin/js/updates$suffix.js", array( 'common', 'jquery', 'wp-util', 'wp-a11y', 'wp-sanitize', 'wp-i18n' ), false, 1 );
1477          $scripts->set_translations( 'updates' );
1478          did_action( 'init' ) && $scripts->localize(
1479              'updates',
1480              '_wpUpdatesSettings',
1481              array(
1482                  'ajax_nonce' => wp_installing() ? '' : wp_create_nonce( 'updates' ),
1483              )
1484          );
1485  
1486          $scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array( 'jquery' ), '1.2' );
1487  
1488          $scripts->add( 'iris', '/wp-admin/js/iris.min.js', array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), '1.1.1', 1 );
1489          $scripts->add( 'wp-color-picker', "/wp-admin/js/color-picker$suffix.js", array( 'iris' ), false, 1 );
1490          $scripts->set_translations( 'wp-color-picker' );
1491  
1492          $scripts->add( 'dashboard', "/wp-admin/js/dashboard$suffix.js", array( 'common', 'jquery', 'admin-comments', 'postbox', 'wp-util', 'wp-a11y', 'wp-date' ), false, 1 );
1493          $scripts->set_translations( 'dashboard' );
1494  
1495          // Deprecated, no longer used in core, registered for backward compatibility.
1496          $scripts->add( 'list-revisions', "/wp-includes/js/wp-list-revisions$suffix.js" );
1497  
1498          $scripts->add( 'media-grid', "/wp-includes/js/media-grid$suffix.js", array( 'media-editor' ), false, 1 );
1499          $scripts->add( 'media', "/wp-admin/js/media$suffix.js", array( 'jquery', 'clipboard', 'wp-i18n', 'wp-a11y' ), false, 1 );
1500          $scripts->set_translations( 'media' );
1501  
1502          $scripts->add( 'media-upload-pipeline', "/wp-admin/js/media-upload-pipeline$suffix.js", array( 'plupload', 'wp-upload-media', 'wp-media-utils', 'wp-api-fetch', 'wp-data', 'wp-element', 'wp-i18n' ), false, 1 );
1503          $scripts->set_translations( 'media-upload-pipeline' );
1504  
1505          $scripts->add( 'media-frame-upload', "/wp-admin/js/media-frame-upload$suffix.js", array( 'media-views', 'media-upload-pipeline' ), false, 1 );
1506  
1507          $scripts->add( 'media-new-upload', "/wp-admin/js/media-new-upload$suffix.js", array( 'plupload-handlers', 'media-upload-pipeline', 'wp-a11y', 'wp-i18n' ), false, 1 );
1508          $scripts->set_translations( 'media-new-upload' );
1509  
1510          $scripts->add( 'image-edit', "/wp-admin/js/image-edit$suffix.js", array( 'jquery', 'jquery-ui-core', 'imgareaselect', 'wp-a11y' ), false, 1 );
1511          $scripts->set_translations( 'image-edit' );
1512  
1513          $scripts->add( 'set-post-thumbnail', "/wp-admin/js/set-post-thumbnail$suffix.js", array( 'jquery' ), false, 1 );
1514          $scripts->set_translations( 'set-post-thumbnail' );
1515  
1516          /*
1517           * Navigation Menus: Adding underscore as a dependency to utilize _.debounce
1518           * see https://core.trac.wordpress.org/ticket/42321
1519           */
1520          $scripts->add( 'nav-menu', "/wp-admin/js/nav-menu$suffix.js", array( 'jquery-ui-sortable', 'jquery-ui-draggable', 'jquery-ui-droppable', 'wp-lists', 'postbox', 'underscore' ) );
1521          $scripts->set_translations( 'nav-menu' );
1522  
1523          $scripts->add( 'custom-header', '/wp-admin/js/custom-header.js', array( 'jquery-masonry' ), false, 1 );
1524          $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array( 'wp-color-picker', 'media-views' ), false, 1 );
1525          $scripts->add( 'media-gallery', "/wp-admin/js/media-gallery$suffix.js", array( 'jquery' ), false, 1 );
1526  
1527          $scripts->add( 'svg-painter', '/wp-admin/js/svg-painter.js', array( 'jquery' ), false, 1 );
1528      }
1529  }
1530  
1531  /**
1532   * Assigns default styles to $styles object.
1533   *
1534   * Nothing is returned, because the $styles parameter is passed by reference.
1535   * Meaning that whatever object is passed will be updated without having to
1536   * reassign the variable that was passed back to the same value. This saves
1537   * memory.
1538   *
1539   * Adding default styles is not the only task, it also assigns the base_url
1540   * property, the default version, and text direction for the object.
1541   *
1542   * @since 2.6.0
1543   *
1544   * @global array $editor_styles
1545   *
1546   * @param WP_Styles $styles
1547   */
1548  function wp_default_styles( $styles ) {
1549      global $editor_styles;
1550  
1551      $guessurl = site_url();
1552  
1553      if ( ! $guessurl ) {
1554          $guessurl = wp_guess_url();
1555      }
1556  
1557      $styles->base_url        = $guessurl;
1558      $styles->content_url     = defined( 'WP_CONTENT_URL' ) ? WP_CONTENT_URL : '';
1559      $styles->default_version = get_bloginfo( 'version' );
1560      $styles->text_direction  = is_rtl() ? 'rtl' : 'ltr';
1561      $styles->default_dirs    = array( '/wp-admin/', '/wp-includes/css/' ); // Deprecated, but still set for plugins that read it.
1562  
1563      // Open Sans is no longer used by core, but may be relied upon by themes and plugins.
1564      $open_sans_font_url = '';
1565  
1566      /*
1567       * translators: If there are characters in your language that are not supported
1568       * by Open Sans, translate this to 'off'. Do not translate into your own language.
1569       */
1570      if ( 'off' !== _x( 'on', 'Open Sans font: on or off' ) ) {
1571          $subsets = 'latin,latin-ext';
1572  
1573          /*
1574           * translators: To add an additional Open Sans character subset specific to your language,
1575           * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
1576           */
1577          $subset = _x( 'no-subset', 'Open Sans font: add new subset (greek, cyrillic, vietnamese)' );
1578  
1579          if ( 'cyrillic' === $subset ) {
1580              $subsets .= ',cyrillic,cyrillic-ext';
1581          } elseif ( 'greek' === $subset ) {
1582              $subsets .= ',greek,greek-ext';
1583          } elseif ( 'vietnamese' === $subset ) {
1584              $subsets .= ',vietnamese';
1585          }
1586  
1587          // Hotlink Open Sans, for now.
1588          $open_sans_font_url = "https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,300,400,600&subset=$subsets&display=fallback";
1589      }
1590  
1591      // Register a stylesheet for the selected admin color scheme.
1592      $styles->add( 'colors', true, array( 'wp-admin', 'buttons' ) );
1593  
1594      $suffix = wp_scripts_get_suffix();
1595  
1596      // Admin CSS.
1597      $styles->add( 'wp-tooltip', "/wp-admin/css/wp-tooltip$suffix.css", array( 'dashicons' ) );
1598      $styles->add( 'common', "/wp-admin/css/common$suffix.css" );
1599      $styles->add( 'forms', "/wp-admin/css/forms$suffix.css" );
1600      $styles->add( 'admin-menu', "/wp-admin/css/admin-menu$suffix.css" );
1601      $styles->add( 'dashboard', "/wp-admin/css/dashboard$suffix.css" );
1602      $styles->add( 'list-tables', "/wp-admin/css/list-tables$suffix.css" );
1603      $styles->add( 'edit', "/wp-admin/css/edit$suffix.css" );
1604      $styles->add( 'revisions', "/wp-admin/css/revisions$suffix.css" );
1605      $styles->add( 'media', "/wp-admin/css/media$suffix.css" );
1606      $styles->add( 'themes', "/wp-admin/css/themes$suffix.css" );
1607      $styles->add( 'about', "/wp-admin/css/about$suffix.css" );
1608      $styles->add( 'nav-menus', "/wp-admin/css/nav-menus$suffix.css" );
1609      $styles->add( 'widgets', "/wp-admin/css/widgets$suffix.css", array( 'wp-pointer' ) );
1610      $styles->add( 'site-icon', "/wp-admin/css/site-icon$suffix.css" );
1611      $styles->add( 'l10n', "/wp-admin/css/l10n$suffix.css" );
1612      $styles->add( 'code-editor', "/wp-admin/css/code-editor$suffix.css", array( 'wp-codemirror' ) );
1613      $styles->add( 'site-health', "/wp-admin/css/site-health$suffix.css" );
1614  
1615      $styles->add( 'wp-admin', false, array( 'dashicons', 'common', 'forms', 'admin-menu', 'dashboard', 'list-tables', 'edit', 'revisions', 'media', 'themes', 'about', 'nav-menus', 'widgets', 'site-icon', 'l10n', 'wp-base-styles', 'wp-tooltip' ) );
1616  
1617      $styles->add( 'login', "/wp-admin/css/login$suffix.css", array( 'dashicons', 'buttons', 'forms', 'l10n', 'wp-base-styles', 'wp-tooltip' ) );
1618      $styles->add( 'install', "/wp-admin/css/install$suffix.css", array( 'dashicons', 'buttons', 'forms', 'l10n', 'wp-base-styles' ) );
1619      $styles->add( 'wp-color-picker', "/wp-admin/css/color-picker$suffix.css" );
1620      $styles->add( 'customize-controls', "/wp-admin/css/customize-controls$suffix.css", array( 'wp-admin', 'colors', 'imgareaselect' ) );
1621      $styles->add( 'customize-widgets', "/wp-admin/css/customize-widgets$suffix.css", array( 'wp-admin', 'colors' ) );
1622      $styles->add( 'customize-nav-menus', "/wp-admin/css/customize-nav-menus$suffix.css", array( 'wp-admin', 'colors' ) );
1623  
1624      // Common dependencies.
1625      $styles->add( 'buttons', "/wp-includes/css/buttons$suffix.css" );
1626      $styles->add( 'dashicons', "/wp-includes/css/dashicons$suffix.css" );
1627  
1628      // Includes CSS.
1629      $styles->add( 'admin-bar', "/wp-includes/css/admin-bar$suffix.css", array( 'dashicons' ) );
1630      $styles->add( 'wp-auth-check', "/wp-includes/css/wp-auth-check$suffix.css", array( 'dashicons' ) );
1631      $styles->add( 'editor-buttons', "/wp-includes/css/editor$suffix.css", array( 'dashicons' ) );
1632      $styles->add( 'media-views', "/wp-includes/css/media-views$suffix.css", array( 'buttons', 'dashicons', 'wp-mediaelement' ) );
1633      $styles->add( 'wp-pointer', "/wp-includes/css/wp-pointer$suffix.css", array( 'dashicons' ) );
1634      $styles->add( 'customize-preview', "/wp-includes/css/customize-preview$suffix.css", array( 'dashicons' ) );
1635      $styles->add( 'wp-empty-template-alert', "/wp-includes/css/wp-empty-template-alert$suffix.css" );
1636      $skip_link_style_path = WPINC . "/css/wp-block-template-skip-link$suffix.css";
1637      $styles->add( 'wp-block-template-skip-link', "/$skip_link_style_path" );
1638      $styles->add_data( 'wp-block-template-skip-link', 'path', ABSPATH . $skip_link_style_path );
1639  
1640      // External libraries and friends.
1641      $styles->add( 'imgareaselect', '/wp-includes/js/imgareaselect/imgareaselect.css', array(), '0.9.8' );
1642      $styles->add( 'wp-jquery-ui-dialog', "/wp-includes/css/jquery-ui-dialog$suffix.css", array( 'dashicons' ) );
1643      $styles->add( 'mediaelement', '/wp-includes/js/mediaelement/mediaelementplayer-legacy.min.css', array(), '4.2.17' );
1644      $styles->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.css", array( 'mediaelement' ) );
1645      $styles->add( 'thickbox', '/wp-includes/js/thickbox/thickbox.css', array( 'dashicons' ) );
1646      $styles->add( 'wp-codemirror', '/wp-includes/js/codemirror/codemirror.min.css', array(), '5.65.20' );
1647  
1648      // Deprecated CSS.
1649      $styles->add( 'deprecated-media', "/wp-admin/css/deprecated-media$suffix.css" );
1650      $styles->add( 'farbtastic', "/wp-admin/css/farbtastic$suffix.css", array(), '1.3u1' );
1651      $styles->add( 'jcrop', '/wp-includes/js/jcrop/jquery.Jcrop.min.css', array(), '0.9.15' );
1652      $styles->add( 'colors-fresh', false, array( 'wp-admin', 'buttons' ) ); // Old handle.
1653      $styles->add( 'open-sans', $open_sans_font_url ); // No longer used in core as of 4.6.
1654      $styles->add( 'wp-embed-template-ie', false );
1655      $styles->add_data( 'wp-embed-template-ie', 'conditional', '_required-conditional-dependency_' );
1656  
1657      // Noto Serif is no longer used by core, but may be relied upon by themes and plugins.
1658      $fonts_url = '';
1659  
1660      /*
1661       * translators: Use this to specify the proper Google Font name and variants
1662       * to load that is supported by your language. Do not translate.
1663       * Set to 'off' to disable loading.
1664       */
1665      $font_family = _x( 'Noto Serif:400,400i,700,700i', 'Google Font Name and Variants' );
1666      if ( 'off' !== $font_family ) {
1667          $fonts_url = 'https://fonts.googleapis.com/css?family=' . urlencode( $font_family );
1668      }
1669      $styles->add( 'wp-editor-font', $fonts_url ); // No longer used in core as of 5.7.
1670      $block_library_theme_path = WPINC . "/css/dist/block-library/theme$suffix.css";
1671      $styles->add( 'wp-block-library-theme', "/$block_library_theme_path" );
1672      $styles->add_data( 'wp-block-library-theme', 'path', ABSPATH . $block_library_theme_path );
1673  
1674      $classic_theme_styles_path = WPINC . "/css/classic-themes$suffix.css";
1675      $styles->add( 'classic-theme-styles', "/$classic_theme_styles_path" );
1676      $styles->add_data( 'classic-theme-styles', 'path', ABSPATH . $classic_theme_styles_path );
1677  
1678      $styles->add(
1679          'wp-reset-editor-styles',
1680          "/wp-includes/css/dist/block-library/reset$suffix.css",
1681          array( 'common', 'forms' ) // Make sure the reset is loaded after the default WP Admin styles.
1682      );
1683  
1684      $styles->add(
1685          'wp-editor-classic-layout-styles',
1686          "/wp-includes/css/dist/edit-post/classic$suffix.css",
1687          array()
1688      );
1689  
1690      $styles->add(
1691          'wp-block-editor-content',
1692          "/wp-includes/css/dist/block-editor/content$suffix.css",
1693          array( 'wp-components' )
1694      );
1695  
1696      // Only add CONTENT styles here that should be enqueued in the iframe!
1697      $wp_edit_blocks_dependencies = array(
1698          'wp-theme',
1699          'wp-base-styles',
1700          'wp-components',
1701          /*
1702           * This needs to be added before the block library styles,
1703           * The block library styles override the "reset" styles.
1704           */
1705          'wp-reset-editor-styles',
1706          'wp-block-library',
1707          'wp-block-editor-content',
1708      );
1709  
1710      // Only load the default layout and margin styles for themes without theme.json file.
1711      if ( ! wp_theme_has_theme_json() ) {
1712          $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
1713      }
1714  
1715      if (
1716          current_theme_supports( 'wp-block-styles' ) &&
1717          ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 )
1718      ) {
1719          /*
1720           * Include opinionated block styles if the theme supports block styles and
1721           * no $editor_styles are declared, so the editor never appears broken.
1722           */
1723          $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
1724      }
1725  
1726      $styles->add(
1727          'wp-edit-blocks',
1728          "/wp-includes/css/dist/block-library/editor$suffix.css",
1729          $wp_edit_blocks_dependencies
1730      );
1731  
1732      $styles->add( 'wp-view-transitions-admin', false );
1733      did_action( 'init' ) && $styles->add_inline_style( 'wp-view-transitions-admin', wp_get_view_transitions_admin_css() );
1734  
1735      $package_styles = array(
1736          'block-editor'         => array( 'wp-components', 'wp-preferences' ),
1737          'block-library'        => array(),
1738          'block-directory'      => array(),
1739          'theme'                => array(),
1740          'base-styles'          => array(),
1741          'components'           => array( 'wp-theme' ),
1742          'commands'             => array( 'wp-components' ),
1743          'edit-post'            => array(
1744              'wp-components',
1745              'wp-block-editor',
1746              'wp-editor',
1747              'wp-edit-blocks',
1748              'wp-block-library',
1749              'wp-commands',
1750              'wp-preferences',
1751          ),
1752          'editor'               => array(
1753              'wp-components',
1754              'wp-block-editor',
1755              'wp-reusable-blocks',
1756              'wp-patterns',
1757              'wp-preferences',
1758              'wp-media-utils',
1759          ),
1760          'format-library'       => array(),
1761          'list-reusable-blocks' => array( 'wp-components' ),
1762          'media-utils'          => array( 'wp-components' ),
1763          'reusable-blocks'      => array( 'wp-components' ),
1764          'patterns'             => array( 'wp-components' ),
1765          'preferences'          => array( 'wp-components' ),
1766          'nux'                  => array( 'wp-components' ),
1767          'widgets'              => array(
1768              'wp-components',
1769          ),
1770          'edit-widgets'         => array(
1771              'wp-widgets',
1772              'wp-block-editor',
1773              'wp-editor',
1774              'wp-edit-blocks',
1775              'wp-block-library',
1776              'wp-patterns',
1777              'wp-preferences',
1778          ),
1779          'customize-widgets'    => array(
1780              'wp-widgets',
1781              'wp-block-editor',
1782              'wp-editor',
1783              'wp-edit-blocks',
1784              'wp-block-library',
1785              'wp-patterns',
1786              'wp-preferences',
1787          ),
1788          'edit-site'            => array(
1789              'wp-components',
1790              'wp-block-editor',
1791              'wp-editor',
1792              'wp-edit-blocks',
1793              'wp-commands',
1794              'wp-preferences',
1795          ),
1796      );
1797  
1798      foreach ( $package_styles as $package => $dependencies ) {
1799          $handle = 'wp-' . $package;
1800          $path   = "/wp-includes/css/dist/$package/style$suffix.css";
1801  
1802          if ( 'block-library' === $package && wp_should_load_separate_core_block_assets() ) {
1803              $path = "/wp-includes/css/dist/$package/common$suffix.css";
1804          }
1805  
1806          if ( 'base-styles' === $package ) {
1807              $path = "/wp-includes/css/dist/base-styles/admin-schemes$suffix.css";
1808          }
1809  
1810          if ( 'theme' === $package ) {
1811              $path = "/wp-includes/css/dist/theme/design-tokens$suffix.css";
1812          }
1813  
1814          $styles->add( $handle, $path, $dependencies );
1815          $styles->add_data( $handle, 'path', ABSPATH . $path );
1816      }
1817  
1818      // RTL CSS.
1819      $rtl_styles = array(
1820          // Admin CSS.
1821          'common',
1822          'forms',
1823          'admin-menu',
1824          'dashboard',
1825          'list-tables',
1826          'edit',
1827          'revisions',
1828          'media',
1829          'themes',
1830          'about',
1831          'nav-menus',
1832          'widgets',
1833          'site-icon',
1834          'l10n',
1835          'install',
1836          'wp-color-picker',
1837          'customize-controls',
1838          'customize-widgets',
1839          'customize-nav-menus',
1840          'customize-preview',
1841          'login',
1842          'site-health',
1843          'wp-empty-template-alert',
1844          // Includes CSS.
1845          'buttons',
1846          'admin-bar',
1847          'wp-auth-check',
1848          'editor-buttons',
1849          'media-views',
1850          'wp-pointer',
1851          'wp-jquery-ui-dialog',
1852          'wp-block-template-skip-link',
1853          // Package styles.
1854          'wp-reset-editor-styles',
1855          'wp-editor-classic-layout-styles',
1856          'wp-block-library-theme',
1857          'wp-theme',
1858          'wp-edit-blocks',
1859          'wp-block-editor',
1860          'wp-block-library',
1861          'wp-block-directory',
1862          'wp-commands',
1863          'wp-components',
1864          'wp-customize-widgets',
1865          'wp-edit-post',
1866          'wp-edit-site',
1867          'wp-edit-widgets',
1868          'wp-editor',
1869          'wp-format-library',
1870          'wp-list-reusable-blocks',
1871          'wp-media-utils',
1872          'wp-reusable-blocks',
1873          'wp-patterns',
1874          'wp-nux',
1875          'wp-widgets',
1876          // Deprecated CSS.
1877          'deprecated-media',
1878          'farbtastic',
1879      );
1880  
1881      foreach ( $rtl_styles as $rtl_style ) {
1882          $styles->add_data( $rtl_style, 'rtl', 'replace' );
1883          if ( $suffix ) {
1884              $styles->add_data( $rtl_style, 'suffix', $suffix );
1885          }
1886      }
1887  }
1888  
1889  /**
1890   * Reorders JavaScript scripts array to place prototype before jQuery.
1891   *
1892   * @since 2.3.1
1893   *
1894   * @param string[] $js_array JavaScript scripts array
1895   * @return string[] Reordered array, if needed.
1896   */
1897  function wp_prototype_before_jquery( $js_array ) {
1898      $prototype = array_search( 'prototype', $js_array, true );
1899  
1900      if ( false === $prototype ) {
1901          return $js_array;
1902      }
1903  
1904      $jquery = array_search( 'jquery', $js_array, true );
1905  
1906      if ( false === $jquery ) {
1907          return $js_array;
1908      }
1909  
1910      if ( $prototype < $jquery ) {
1911          return $js_array;
1912      }
1913  
1914      unset( $js_array[ $prototype ] );
1915  
1916      array_splice( $js_array, $jquery, 0, 'prototype' );
1917  
1918      return $js_array;
1919  }
1920  
1921  /**
1922   * Loads localized data on print rather than initialization.
1923   *
1924   * These localizations require information that may not be loaded even by init.
1925   *
1926   * @since 2.5.0
1927   *
1928   * @global array $shortcode_tags
1929   */
1930  function wp_just_in_time_script_localization() {
1931  
1932      wp_localize_script(
1933          'autosave',
1934          'autosaveL10n',
1935          array(
1936              'autosaveInterval' => AUTOSAVE_INTERVAL,
1937              'blog_id'          => get_current_blog_id(),
1938          )
1939      );
1940  
1941      wp_localize_script(
1942          'mce-view',
1943          'mceViewL10n',
1944          array(
1945              'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
1946          )
1947      );
1948  
1949      wp_localize_script(
1950          'word-count',
1951          'wordCountL10n',
1952          array(
1953              'type'       => wp_get_word_count_type(),
1954              'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array(),
1955          )
1956      );
1957  }
1958  
1959  /**
1960   * Localizes the jQuery UI datepicker.
1961   *
1962   * @since 4.6.0
1963   *
1964   * @link https://api.jqueryui.com/datepicker/#options
1965   *
1966   * @global WP_Locale $wp_locale WordPress date and time locale object.
1967   */
1968  function wp_localize_jquery_ui_datepicker() {
1969      global $wp_locale;
1970  
1971      if ( ! wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) ) {
1972          return;
1973      }
1974  
1975      // Convert the PHP date format into jQuery UI's format.
1976      $datepicker_date_format = str_replace(
1977          array(
1978              'd',
1979              'j',
1980              'l',
1981              'z', // Day.
1982              'F',
1983              'M',
1984              'n',
1985              'm', // Month.
1986              'Y',
1987              'y', // Year.
1988          ),
1989          array(
1990              'dd',
1991              'd',
1992              'DD',
1993              'o',
1994              'MM',
1995              'M',
1996              'm',
1997              'mm',
1998              'yy',
1999              'y',
2000          ),
2001          get_option( 'date_format' )
2002      );
2003  
2004      $datepicker_defaults = wp_json_encode(
2005          array(
2006              'closeText'       => __( 'Close' ),
2007              'currentText'     => __( 'Today' ),
2008              'monthNames'      => array_values( $wp_locale->month ),
2009              'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
2010              'nextText'        => _x( 'Next', 'datepicker: navigate to next month' ),
2011              'prevText'        => _x( 'Previous', 'datepicker: navigate to previous month' ),
2012              'dayNames'        => array_values( $wp_locale->weekday ),
2013              'dayNamesShort'   => array_values( $wp_locale->weekday_abbrev ),
2014              'dayNamesMin'     => array_values( $wp_locale->weekday_initial ),
2015              'dateFormat'      => $datepicker_date_format,
2016              'firstDay'        => absint( get_option( 'start_of_week' ) ),
2017              'isRTL'           => $wp_locale->is_rtl(),
2018          ),
2019          JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
2020      );
2021  
2022      wp_add_inline_script( 'jquery-ui-datepicker', "jQuery(function(jQuery){jQuery.datepicker.setDefaults({$datepicker_defaults});});" );
2023  }
2024  
2025  /**
2026   * Localizes community events data that needs to be passed to dashboard.js.
2027   *
2028   * @since 4.8.0
2029   */
2030  function wp_localize_community_events() {
2031      if ( ! wp_script_is( 'dashboard' ) ) {
2032          return;
2033      }
2034  
2035      require_once  ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
2036  
2037      $user_id            = get_current_user_id();
2038      $saved_location     = get_user_option( 'community-events-location', $user_id );
2039      $saved_ip_address   = $saved_location['ip'] ?? false;
2040      $current_ip_address = WP_Community_Events::get_unsafe_client_ip();
2041  
2042      /*
2043       * If the user's location is based on their IP address, then update their
2044       * location when their IP address changes. This allows them to see events
2045       * in their current city when travelling. Otherwise, they would always be
2046       * shown events in the city where they were when they first loaded the
2047       * Dashboard, which could have been months or years ago.
2048       */
2049      if ( $saved_ip_address && $current_ip_address && $current_ip_address !== $saved_ip_address ) {
2050          $saved_location['ip'] = $current_ip_address;
2051          update_user_meta( $user_id, 'community-events-location', $saved_location );
2052      }
2053  
2054      $events_client = new WP_Community_Events( $user_id, $saved_location );
2055  
2056      wp_localize_script(
2057          'dashboard',
2058          'communityEventsData',
2059          array(
2060              'nonce'       => wp_create_nonce( 'community_events' ),
2061              'cache'       => $events_client->get_cached_events(),
2062              'time_format' => get_option( 'time_format' ),
2063          )
2064      );
2065  }
2066  
2067  /**
2068   * Administration Screen CSS for changing the styles.
2069   *
2070   * If installing the 'wp-admin/' directory will be replaced with './'.
2071   *
2072   * The $_wp_admin_css_colors global manages the Administration Screens CSS
2073   * stylesheet that is loaded. The option that is set is 'admin_color' and is the
2074   * color and key for the array. The value for the color key is an object with
2075   * a 'url' parameter that has the URL path to the CSS file.
2076   *
2077   * The query from $src parameter will be appended to the URL that is given from
2078   * the $_wp_admin_css_colors array value URL.
2079   *
2080   * @since 2.6.0
2081   *
2082   * @global array $_wp_admin_css_colors
2083   *
2084   * @param string $src    Source URL.
2085   * @param string $handle Either 'colors' or 'colors-rtl'.
2086   * @return string|false URL path to CSS stylesheet for Administration Screens.
2087   */
2088  function wp_style_loader_src( $src, $handle ) {
2089      global $_wp_admin_css_colors;
2090  
2091      if ( wp_installing() ) {
2092          return preg_replace( '#^wp-admin/#', './', $src );
2093      }
2094  
2095      if ( 'colors' === $handle ) {
2096          $color = get_user_option( 'admin_color' );
2097  
2098          if ( empty( $color ) || ! isset( $_wp_admin_css_colors[ $color ] ) ) {
2099              $color = 'modern';
2100          }
2101  
2102          $color = $_wp_admin_css_colors[ $color ] ?? null;
2103          $url   = $color->url ?? '';
2104  
2105          if ( ! $url ) {
2106              return false;
2107          }
2108  
2109          $parsed = parse_url( $src );
2110          if ( isset( $parsed['query'] ) && $parsed['query'] ) {
2111              wp_parse_str( $parsed['query'], $qv );
2112              $url = add_query_arg( $qv, $url );
2113          }
2114  
2115          return $url;
2116      }
2117  
2118      return $src;
2119  }
2120  
2121  /**
2122   * Prints the script queue in the HTML head on admin pages.
2123   *
2124   * Postpones the scripts that were queued for the footer.
2125   * print_footer_scripts() is called in the footer to print these scripts.
2126   *
2127   * @since 2.8.0
2128   *
2129   * @see wp_print_scripts()
2130   *
2131   * @return string[] Handles of the scripts that were printed.
2132   */
2133  function print_head_scripts() {
2134      if ( ! did_action( 'wp_print_scripts' ) ) {
2135          /** This action is documented in wp-includes/functions.wp-scripts.php */
2136          do_action( 'wp_print_scripts' );
2137      }
2138  
2139      $wp_scripts = wp_scripts();
2140      $wp_scripts->do_head_items();
2141  
2142      /**
2143       * Filters whether to print the head scripts.
2144       *
2145       * @since 2.8.0
2146       * @deprecated 7.2.0 Scripts are no longer concatenated, so there is nothing for this filter to control.
2147       *
2148       * @param bool $print Whether to print the head scripts. Default true.
2149       */
2150      apply_filters_deprecated( 'print_head_scripts', array( true ), '7.2.0' );
2151  
2152      $wp_scripts->reset();
2153      return $wp_scripts->done;
2154  }
2155  
2156  /**
2157   * Prints the scripts that were queued for the footer or too late for the HTML head.
2158   *
2159   * @since 2.8.0
2160   *
2161   * @global WP_Scripts $wp_scripts
2162   *
2163   * @return string[] Handles of the scripts that were printed.
2164   */
2165  function print_footer_scripts() {
2166      global $wp_scripts;
2167  
2168      if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
2169          return array(); // No need to run if not instantiated.
2170      }
2171      $wp_scripts->do_footer_items();
2172  
2173      /**
2174       * Filters whether to print the footer scripts.
2175       *
2176       * @since 2.8.0
2177       * @deprecated 7.2.0 Scripts are no longer concatenated, so there is nothing for this filter to control.
2178       *
2179       * @param bool $print Whether to print the footer scripts. Default true.
2180       */
2181      apply_filters_deprecated( 'print_footer_scripts', array( true ), '7.2.0' );
2182  
2183      $wp_scripts->reset();
2184      return $wp_scripts->done;
2185  }
2186  
2187  /**
2188   * Prints the script queue in the HTML head on the front end.
2189   *
2190   * Postpones the scripts that were queued for the footer.
2191   * wp_print_footer_scripts() is called in the footer to print these scripts.
2192   *
2193   * @since 2.8.0
2194   *
2195   * @global WP_Scripts $wp_scripts
2196   *
2197   * @return string[] Handles of the scripts that were printed.
2198   */
2199  function wp_print_head_scripts() {
2200      global $wp_scripts;
2201  
2202      if ( ! did_action( 'wp_print_scripts' ) ) {
2203          /** This action is documented in wp-includes/functions.wp-scripts.php */
2204          do_action( 'wp_print_scripts' );
2205      }
2206  
2207      if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
2208          return array(); // No need to run if nothing is queued.
2209      }
2210  
2211      return print_head_scripts();
2212  }
2213  
2214  /**
2215   * Private, for use in *_footer_scripts hooks
2216   *
2217   * In classic themes, when block styles are loaded on demand via wp_load_classic_theme_block_styles_on_demand(),
2218   * this function is replaced by a closure in wp_hoist_late_printed_styles() which will capture the printing of
2219   * two sets of "late" styles to be hoisted to the HEAD by means of the template enhancement output buffer:
2220   *
2221   * 1. Styles related to blocks are inserted right after the wp-block-library stylesheet.
2222   * 2. All other styles are appended to the end of the HEAD.
2223   *
2224   * The closure calls print_footer_scripts() to print scripts in the footer as usual.
2225   *
2226   * @since 3.3.0
2227   */
2228  function _wp_footer_scripts() {
2229      print_late_styles();
2230      print_footer_scripts();
2231  }
2232  
2233  /**
2234   * Hooks to print the scripts and styles in the footer.
2235   *
2236   * @since 2.8.0
2237   */
2238  function wp_print_footer_scripts() {
2239      /**
2240       * Fires when footer scripts are printed.
2241       *
2242       * @since 2.8.0
2243       */
2244      do_action( 'wp_print_footer_scripts' );
2245  }
2246  
2247  /**
2248   * Wrapper for do_action( 'wp_enqueue_scripts' ).
2249   *
2250   * Allows plugins to queue scripts for the front end using wp_enqueue_script().
2251   * Runs first in wp_head() where all is_home(), is_page(), etc. functions are available.
2252   *
2253   * @since 2.8.0
2254   */
2255  function wp_enqueue_scripts() {
2256      /**
2257       * Fires when scripts and styles are enqueued.
2258       *
2259       * @since 2.8.0
2260       */
2261      do_action( 'wp_enqueue_scripts' );
2262  }
2263  
2264  /**
2265   * Prints the styles queue in the HTML head on admin pages.
2266   *
2267   * @since 2.8.0
2268   *
2269   * @return string[] Handles of the styles that were printed.
2270   */
2271  function print_admin_styles() {
2272      $wp_styles = wp_styles();
2273      $wp_styles->do_items( false );
2274  
2275      /**
2276       * Filters whether to print the admin styles.
2277       *
2278       * @since 2.8.0
2279       * @deprecated 7.2.0 Styles are no longer concatenated, so there is nothing for this filter to control.
2280       *
2281       * @param bool $print Whether to print the admin styles. Default true.
2282       */
2283      apply_filters_deprecated( 'print_admin_styles', array( true ), '7.2.0' );
2284  
2285      $wp_styles->reset();
2286      return $wp_styles->done;
2287  }
2288  
2289  /**
2290   * Prints the styles that were queued too late for the HTML head.
2291   *
2292   * @since 3.3.0
2293   *
2294   * @global WP_Styles $wp_styles
2295   *
2296   * @return string[]|null
2297   */
2298  function print_late_styles() {
2299      global $wp_styles;
2300  
2301      if ( ! ( $wp_styles instanceof WP_Styles ) ) {
2302          return null;
2303      }
2304  
2305      $wp_styles->do_footer_items();
2306  
2307      /**
2308       * Filters whether to print the styles queued too late for the HTML head.
2309       *
2310       * @since 3.3.0
2311       * @deprecated 7.2.0 Styles are no longer concatenated, so there is nothing for this filter to control.
2312       *
2313       * @param bool $print Whether to print the 'late' styles. Default true.
2314       */
2315      apply_filters_deprecated( 'print_late_styles', array( true ), '7.2.0' );
2316  
2317      $wp_styles->reset();
2318      return $wp_styles->done;
2319  }
2320  
2321  /**
2322   * Prints prefetch links for the assets of the screen the user is most likely to open next.
2323   *
2324   * Runs wherever the next screen can be predicted with confidence, and prefetches only what that
2325   * screen is certain to need. Two cases qualify today:
2326   *
2327   * - The login screen, which is followed by an admin screen. That screen downloads each core script
2328   *   and stylesheet separately, which is what makes an uncached admin load slow. Requesting the
2329   *   ones that block its first paint while the login form is on screen puts them in the HTTP cache
2330   *   during the time the user spends typing credentials, so the redirect that follows finds them
2331   *   already there.
2332   * - The Dashboard and the post list tables, from which the editor is the usual next stop. Only the
2333   *   editor's stylesheets are prefetched, not its scripts: the scripts run to well over a megabyte
2334   *   compressed, which is far too much to spend on a screen the user may never open, whereas the
2335   *   stylesheets are render-blocking and in the same size class as the login screen's own prefetch.
2336   *
2337   * Handles the current screen has printed or queued are skipped, along with their dependencies, so
2338   * each context only fetches what it is actually adding. On the login screen this runs in the
2339   * footer, since some of what the login form loads, such as `user-profile` and the `jquery` it
2340   * depends on, is only enqueued after its header has printed. On admin screens it runs in the head,
2341   * where the screen's assets have been enqueued already.
2342   *
2343   * These are resources for the *next* navigation rather than for the screen printing them, which is
2344   * what `rel="prefetch"` describes. `rel="preload"` would fetch them at the current document's
2345   * priority, and browsers warn about preloaded resources the document never uses. A prefetch is
2346   * already dispatched at the browser's lowest priority, so it stays out of the way of that screen's
2347   * own render-blocking assets without needing `fetchpriority`.
2348   *
2349   * A prefetched response is reused only for as long as the HTTP cache considers it fresh, the same as
2350   * any other cached response. Core does not send caching headers for its static files, so how long
2351   * that is depends on the server: an explicit `max-age` or `Expires`, or else a heuristic lifetime
2352   * derived from `Last-Modified`. Once the response is stale the next screen still revalidates it,
2353   * which saves the download but not the round trip.
2354   *
2355   * The `as` attribute is still worth setting: it gives the request the same destination the admin
2356   * screen will later ask for, which is what lets the prefetched response be reused.
2357   *
2358   * The admin-wide handles cover every admin screen rather than only the Dashboard, so that part of
2359   * the list does not vary with where the login lands. They are printed on every screen of the login
2360   * page, such as the password reset form, since those mostly lead to the admin as well. Nothing is
2361   * printed at all when `redirect_to` points outside this site's admin.
2362   *
2363   * @since 7.2.0
2364   *
2365   * @see wp_preload_resources()
2366   *
2367   * @global WP_Screen|null $current_screen The current admin screen, if any.
2368   */
2369  function wp_prefetch_admin_assets(): void {
2370      global $current_screen;
2371  
2372      /*
2373       * The context is told apart by the request rather than by the hook this runs on, so it works
2374       * from whichever hook it is added to. The 'login_init' action is fired by wp-login.php however
2375       * it is reached. The is_login() function would not do: it compares the login URL with the
2376       * script handling the request, and a plugin serving the login screen at a URL of its own runs
2377       * wp-login.php from another script, such as index.php.
2378       */
2379      $on_login = (bool) did_action( 'login_init' );
2380  
2381      if ( ! $on_login ) {
2382          /*
2383           * From the Dashboard and the post list tables, the editor is the usual next stop. Anywhere
2384           * else in the admin there is no destination worth guessing at.
2385           *
2386           * The global is read rather than calling get_current_screen(), which only exists once the
2387           * admin includes are loaded. On a request with no screen, such as one for the front end,
2388           * this prints nothing.
2389           */
2390          if ( ! $current_screen instanceof WP_Screen || ! in_array( $current_screen->base, array( 'dashboard', 'edit' ), true ) ) {
2391              return;
2392          }
2393      }
2394  
2395      $script_roots = array();
2396      $style_roots  = array();
2397  
2398      if ( $on_login ) {
2399          /*
2400           * Resolve where the login is going to land, the same way wp-login.php will: `redirect_to`
2401           * when one was given, and the admin otherwise. wp_safe_redirect() sends the browser to what
2402           * wp_validate_redirect() returns, so that is used here as it is: it sanitizes the value,
2403           * resolves a relative path against the current request, as the browser would, and falls back
2404           * to the admin for a value pointing off-host. Passing the value through esc_url_raw() first
2405           * would not match it, since that takes a relative path such as `wp-admin/post.php` for a host.
2406           *
2407           * This runs on every screen wp-login.php prints, not only the login form, since the others
2408           * mostly lead to the admin as well, and each one gives the prefetching another chance to
2409           * finish before the user gets there. The password reset and registration flows end at the
2410           * login form, and the admin email confirmation follows a login that has already succeeded.
2411           * On the lost password and registration screens, `redirect_to` is where submitting the
2412           * form goes rather than where the login lands, but it is normally absent, and when it is
2413           * not, it typically points back into wp-login.php, so nothing is prefetched. Nor does an
2414           * interim login need to be excluded: it shows inside a modal on an admin screen that has
2415           * already loaded these assets, so they are served from the HTTP cache.
2416           *
2417           * An empty `redirect_to` counts as none, since wp-login.php falls back to the admin for it
2418           * too. The lost password form submits one, for instance, so it is present when the form is
2419           * shown again with an error.
2420           */
2421          $admin_url   = admin_url();
2422          $next_screen = $admin_url;
2423  
2424          if ( ! empty( $_REQUEST['redirect_to'] ) && is_string( $_REQUEST['redirect_to'] ) ) {
2425              $next_screen = wp_validate_redirect( wp_unslash( $_REQUEST['redirect_to'] ), $admin_url );
2426          }
2427  
2428          /*
2429           * When the login lands somewhere other than the admin, such as the front end or a plugin's
2430           * own screen, none of these assets are wanted. The admin's path ends in a slash, which a
2431           * `redirect_to` of the admin itself may leave off, as in `/wp-admin`.
2432           */
2433          $admin_path = (string) wp_parse_url( $admin_url, PHP_URL_PATH );
2434  
2435          if ( '' === $admin_path || ! str_starts_with( trailingslashit( (string) wp_parse_url( $next_screen, PHP_URL_PATH ) ), $admin_path ) ) {
2436              return;
2437          }
2438  
2439          /*
2440           * Nor are they when it lands in the admin of another host. wp_validate_redirect() accepts any
2441           * host in 'allowed_redirect_hosts', such as another site on a multisite network, and that
2442           * admin would request its assets from its own host rather than from this one. The same goes
2443           * for this host's admin under another scheme, such as an `https` destination from an `http`
2444           * login: the URLs prefetched here take the scheme of the current request, so that admin
2445           * would request different ones. A relative `redirect_to` stays on this host and scheme, while
2446           * wp_validate_redirect() gives a protocol-relative one the `http` scheme, which is where
2447           * wp_safe_redirect() then sends it. A port is compared with the scheme's default filled in
2448           * when none is given, since `https://example.com:443/` is the same admin as
2449           * `https://example.com/`.
2450           */
2451          $next_screen_host = wp_parse_url( $next_screen, PHP_URL_HOST );
2452  
2453          if ( is_string( $next_screen_host ) ) {
2454              $default_ports = array(
2455                  'http'  => 80,
2456                  'https' => 443,
2457              );
2458  
2459              $admin_scheme       = strtolower( (string) wp_parse_url( $admin_url, PHP_URL_SCHEME ) );
2460              $next_screen_scheme = strtolower( (string) wp_parse_url( $next_screen, PHP_URL_SCHEME ) );
2461              $admin_port         = wp_parse_url( $admin_url, PHP_URL_PORT ) ?? $default_ports[ $admin_scheme ] ?? null;
2462              $next_screen_port   = wp_parse_url( $next_screen, PHP_URL_PORT ) ?? $default_ports[ $next_screen_scheme ] ?? null;
2463  
2464              if (
2465                  strtolower( $next_screen_host ) !== strtolower( (string) wp_parse_url( $admin_url, PHP_URL_HOST ) ) ||
2466                  $next_screen_scheme !== $admin_scheme ||
2467                  $next_screen_port !== $admin_port
2468              ) {
2469                  return;
2470              }
2471          }
2472      } else {
2473          $post_type        = ( 'edit' === $current_screen->base && $current_screen->post_type ) ? $current_screen->post_type : 'post';
2474          $post_type_object = get_post_type_object( $post_type );
2475  
2476          if ( ! $post_type_object instanceof WP_Post_Type ) {
2477              return;
2478          }
2479  
2480          /*
2481           * A user who cannot edit posts of this type will never reach the editor from here. This is
2482           * `edit_posts` rather than `create_posts`, since the editor is reached by opening an existing
2483           * post as well as by adding a new one, and a user may be able to do the first but not the
2484           * second. Adding a new one requires `edit_posts` too.
2485           */
2486          if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
2487              return;
2488          }
2489  
2490          $next_screen = add_query_arg( 'post_type', $post_type, admin_url( 'post-new.php' ) );
2491      }
2492  
2493      if ( $on_login ) {
2494          /*
2495           * The handles that block rendering on every admin screen: the stylesheets, and the scripts
2496           * printed in the head. These are what stand between the redirect and the first paint, so
2497           * they are what is worth having in the cache already. Every handle these expand to loads on
2498           * all admin screens, not just the one the login happens to land on, so the list does not depend
2499           * on the destination. Screen-specific handles are deliberately left out: `site-health`
2500           * blocks rendering on the Dashboard but loads nowhere else.
2501           *
2502           * Scripts printed in the footer are left out even though they block DOMContentLoaded, since
2503           * they do not hold back the first paint. They are also where the bulk of the admin's bytes
2504           * are, largely the command palette's dependencies, and a prefetch of them still in flight
2505           * when the login form is submitted competes with the admin screen's own render-blocking
2506           * stylesheets and delays its first paint on a slow connection.
2507           *
2508           * These are roots rather than the full set: everything they depend on is pulled in with them
2509           * below, so the set follows the dependencies declared in wp_default_scripts() and
2510           * wp_default_styles(). `jquery` stands for `jquery-core` and `jquery-migrate`, and `wp-admin`
2511           * for the admin's own stylesheets, which is what the `colors` handle enqueued on every admin
2512           * screen depends on. Aliases like these have no source of their own, so only what they expand
2513           * to is prefetched. `colors` itself is left out, since the color scheme is a per-user setting
2514           * and the user is not known yet.
2515           *
2516           * Each root mirrors an enqueue elsewhere, which carries a note pointing back here: `common`
2517           * (for `jquery`) in wp-admin/admin.php, `colors` (for `wp-admin` and `buttons`) and `utils` in
2518           * wp-admin/admin-header.php, `admin-bar` in WP_Admin_Bar::initialize(), `wp-auth-check` in
2519           * wp_auth_check_load(), and `wp-commands` in wp_enqueue_command_palette_assets().
2520           */
2521          $script_roots = array(
2522              'jquery',
2523              'utils',
2524          );
2525  
2526          $style_roots = array(
2527              'wp-admin',
2528              'buttons',
2529              'admin-bar',
2530              'wp-auth-check',
2531              'wp-commands',
2532          );
2533      }
2534  
2535      /*
2536       * post-new.php always opens the editor, while post.php also handles trashing, restoring and
2537       * bulk edits, so it counts only when it is editing.
2538       */
2539      $next_screen_file  = basename( (string) wp_parse_url( $next_screen, PHP_URL_PATH ) );
2540      $next_screen_query = array();
2541      wp_parse_str( (string) wp_parse_url( $next_screen, PHP_URL_QUERY ), $next_screen_query );
2542  
2543      $next_screen_is_block_editor = 'post-new.php' === $next_screen_file
2544          || ( 'post.php' === $next_screen_file && 'edit' === ( $next_screen_query['action'] ?? '' ) );
2545  
2546      if ( $next_screen_is_block_editor ) {
2547          /*
2548           * The editor only loads these stylesheets when the post type uses the block editor, which
2549           * the classic editor, for instance, can turn off. The post type is the one in the query of
2550           * the next screen, as for post-new.php. An edit link to post.php from the login screen does
2551           * not carry one, so it is taken to be a post: looking the post up instead would let anyone
2552           * tell from the login screen whether a post with a given ID exists, drafts and private posts
2553           * included.
2554           */
2555          $next_screen_post_type       = $next_screen_query['post_type'] ?? 'post';
2556          $next_screen_is_block_editor = is_string( $next_screen_post_type ) && use_block_editor_for_post_type( $next_screen_post_type );
2557      }
2558  
2559      if ( $next_screen_is_block_editor ) {
2560          /*
2561           * Roots as well, expanded along with any from the login screen. `wp-edit-post` alone accounts
2562           * for most of the editor chrome, including the block editor's content and reset styles by way
2563           * of `wp-edit-blocks`; the rest cover the block directory, the format library, the classic
2564           * editor's buttons and the media modal.
2565           *
2566           * Each root mirrors an enqueue elsewhere, which carries a note pointing back here:
2567           * `wp-edit-post` in wp-admin/edit-form-blocks.php, `wp-block-directory` in
2568           * wp_enqueue_editor_block_directory_assets(), `wp-format-library` in
2569           * wp_enqueue_editor_format_library_assets(), `editor-buttons` in
2570           * _WP_Editors::enqueue_default_editor(), and `media-views` and `imgareaselect` in
2571           * wp_enqueue_media().
2572           */
2573          $style_roots = array_merge(
2574              $style_roots,
2575              array(
2576                  'wp-edit-post',
2577                  'wp-block-directory',
2578                  'wp-format-library',
2579                  'editor-buttons',
2580                  'media-views',
2581                  'imgareaselect',
2582              )
2583          );
2584      }
2585  
2586      // From an admin screen, nothing is left to prefetch when the editor turns out not to be the block editor.
2587      if ( ! $script_roots && ! $style_roots ) {
2588          return;
2589      }
2590  
2591      $resources = array();
2592  
2593      foreach (
2594          array(
2595              'script' => array( wp_scripts(), $script_roots ),
2596              'style'  => array( wp_styles(), $style_roots ),
2597          )
2598          as $as => list( $dependencies, $roots )
2599      ) {
2600          // Skip a type with nothing to prefetch, such as scripts for the editor, rather than expanding the current screen's queue of it.
2601          if ( ! $roots ) {
2602              continue;
2603          }
2604  
2605          /*
2606           * Expand the roots to include everything they depend on, roots first, and likewise what the
2607           * current screen has queued. A handle that is not registered is dropped along with its
2608           * dependencies. Unlike WP_Dependencies::all_deps(), this leaves the dependencies' state
2609           * untouched.
2610           */
2611          $expanded = array(
2612              'roots' => array(),
2613              'queue' => array(),
2614          );
2615  
2616          foreach (
2617              array(
2618                  'roots' => $roots,
2619                  'queue' => $dependencies->queue,
2620              )
2621              as $list => $handles
2622          ) {
2623              while ( $handles ) {
2624                  $handle = array_shift( $handles );
2625  
2626                  if ( isset( $expanded[ $list ][ $handle ] ) || ! isset( $dependencies->registered[ $handle ] ) ) {
2627                      continue;
2628                  }
2629  
2630                  $expanded[ $list ][ $handle ] = true;
2631  
2632                  foreach ( $dependencies->registered[ $handle ]->deps as $dependency ) {
2633                      if ( is_string( $dependency ) && '' !== $dependency && ! isset( $expanded[ $list ][ $dependency ] ) ) {
2634                          $handles[] = $dependency;
2635                      }
2636                  }
2637              }
2638          }
2639  
2640          /*
2641           * Whichever screen this is running on shares some of these handles, and the browser fetches
2642           * those for it anyway, so prefetching them as well would only compete with its own requests.
2643           * These are the ones it has printed, and the ones it has queued along with their
2644           * dependencies, which it has yet to print if this runs before its footer scripts. On the
2645           * login screen, for instance, `user-profile` brings in `jquery`.
2646           */
2647          $on_current_screen = $expanded['queue'] + array_fill_keys( $dependencies->done, true );
2648  
2649          foreach ( array_keys( $expanded['roots'] ) as $handle ) {
2650              if ( isset( $on_current_screen[ $handle ] ) ) {
2651                  continue;
2652              }
2653  
2654              // The next screen prints nothing for a handle with conditional data, as do_item() returns early for it.
2655              if ( $dependencies->registered[ $handle ]->extra['conditional'] ?? false ) {
2656                  continue;
2657              }
2658  
2659              /*
2660               * The URLs are built the same way WP_Scripts::do_item() and WP_Styles::do_item() build
2661               * those in the tags they print, so they match what the next screen will request. They are
2662               * not escaped yet, so the filter sees plain URLs; they are escaped when printed.
2663               *
2664               * A handle with no URL of its own, such as an alias or one whose URL was filtered away,
2665               * prints nothing either, and for a style that includes its right-to-left stylesheet,
2666               * since WP_Styles::do_item() returns before getting to it.
2667               */
2668              $src = $dependencies->get_src( $handle );
2669  
2670              if ( '' === $src ) {
2671                  continue;
2672              }
2673  
2674              $urls = array( $src );
2675  
2676              if ( $dependencies instanceof WP_Styles ) {
2677                  $rtl_src = $dependencies->get_rtl_src( $handle );
2678  
2679                  if ( null !== $rtl_src ) {
2680                      if ( 'replace' === $dependencies->get_data( $handle, 'rtl' ) ) {
2681                          $urls = array( $rtl_src );
2682                      } else {
2683                          $urls[] = $rtl_src;
2684                      }
2685                  }
2686              }
2687  
2688              foreach ( array_filter( $urls ) as $url ) {
2689                  $resources[] = array(
2690                      'href' => $url,
2691                      'as'   => $as,
2692                  );
2693              }
2694          }
2695      }
2696  
2697      /**
2698       * Filters the assets prefetched for the screen the user is expected to open next.
2699       *
2700       * Fires on any screen from which the next one can be predicted, so `$next_screen` is what
2701       * distinguishes the contexts: the login screen passes the URL it is about to redirect to, and
2702       * the Dashboard and post list tables pass the editor they expect the user to open.
2703       *
2704       * Only the `href` and `as` attributes below are printed; any other key is ignored. Resources
2705       * sharing an `href` are collapsed to the first of them, so a callback may append without
2706       * checking what is already there. Returning an empty array turns the prefetching off.
2707       *
2708       * On the login screen the URLs are built before the user is authenticated and outside the
2709       * admin, so there is no current user and `is_admin()` is false. A {@see 'script_loader_src'}
2710       * or {@see 'style_loader_src'} callback that depends on either can produce a URL the admin
2711       * screen will not request, which wastes the prefetch; a callback on this filter can correct it.
2712       *
2713       * @since 7.2.0
2714       *
2715       * @param array  $resources {
2716       *     Array of resources and their attributes to prefetch.
2717       *
2718       *     @type array ...$0 {
2719       *         Array of resource attributes.
2720       *
2721       *         @type string $href URL to prefetch. Required.
2722       *         @type string $as   How the browser should treat the resource
2723       *                            (`script`, `style`, `image`, `document`, etc). Required.
2724       *     }
2725       * }
2726       * @param string $next_screen URL of the screen the assets are being prefetched for. Always
2727       *                            points into the admin, since nothing is prefetched otherwise.
2728       *                            From the login screen this is the redirect target, already run
2729       *                            through wp_validate_redirect() with the admin as the fallback,
2730       *                            and it may be relative: it is the value as wp_safe_redirect()
2731       *                            will receive it, so a request-supplied path is passed through
2732       *                            unchanged and only the fallback is a full URL.
2733       */
2734      $resources = apply_filters( 'wp_prefetch_admin_assets', $resources, $next_screen );
2735  
2736      if ( ! is_array( $resources ) ) {
2737          return;
2738      }
2739  
2740      $unique_resources = array();
2741  
2742      // Parse the complete resource list and extract unique resources.
2743      foreach ( $resources as $resource ) {
2744          if ( ! is_array( $resource ) ) {
2745              continue;
2746          }
2747  
2748          $href = $resource['href'] ?? '';
2749          $as   = $resource['as'] ?? '';
2750  
2751          if ( ! is_string( $href ) || '' === $href || ! is_string( $as ) || '' === $as ) {
2752              continue;
2753          }
2754  
2755          /*
2756           * Check again once escaped, since esc_url() returns an empty string for a URL it rejects.
2757           * An empty `href` would resolve to the current page, which the script below would then fetch.
2758           * Only `http` and `https` URLs are allowed, as wp_preload_resources() does, since nothing else
2759           * can be prefetched, and the script would pass anything else to fetch().
2760           */
2761          $href = esc_url( $href, array( 'http', 'https' ) );
2762  
2763          if ( '' === $href || isset( $unique_resources[ $href ] ) ) {
2764              continue;
2765          }
2766  
2767          $unique_resources[ $href ] = $as;
2768      }
2769  
2770      if ( ! $unique_resources ) {
2771          return;
2772      }
2773  
2774      // Build and output the HTML for each unique resource. Each URL has already been escaped.
2775      foreach ( $unique_resources as $escaped_href => $as ) {
2776          printf(
2777              "<link rel='prefetch' href='%s' as='%s' />\n",
2778              $escaped_href,
2779              esc_attr( $as )
2780          );
2781      }
2782  
2783      /*
2784       * Safari does not support `rel="prefetch"`, so in browsers that lack it, fetch the URLs of the
2785       * page's prefetch links with fetch() instead, to put the responses in the HTTP cache all the
2786       * same. Reading the links from the page, rather than passing their URLs to the script, keeps a
2787       * single list of them, and also covers any prefetch links added by plugins. The fetches wait
2788       * until the current screen has loaded, and ask for low priority, so they stay out of the way
2789       * of its own assets. Unlike a prefetch link, a fetch still in flight is canceled when the user
2790       * navigates away, so it cannot compete with the next screen's render-blocking assets.
2791       *
2792       * The requests use `no-cors` mode and include credentials, as the stylesheet and script
2793       * requests of the next screen do, so the cached responses match what that screen will ask for.
2794       * The body is read to completion so that the full response is cached.
2795       */
2796      $js_function = <<<'JS'
2797          /**
2798           * Prefetches the page's rel=prefetch links with fetch() in browsers that do not support rel=prefetch.
2799           *
2800           * @link https://caniuse.com/link-rel-prefetch
2801           */
2802          () => {
2803              if ( document.createElement( 'link' ).relList?.supports?.( 'prefetch' ) ) {
2804                  return;
2805              }
2806  
2807              const prefetch = () => {
2808                  for ( const link of /** @type {NodeListOf<HTMLLinkElement>} */ ( document.querySelectorAll( 'link[rel~="prefetch"][href]' ) ) ) {
2809                      fetch( link.href, { mode: 'no-cors', credentials: 'include', priority: 'low' } )
2810                          .then( ( response ) => response.blob() )
2811                          .catch( () => {} );
2812                  }
2813              };
2814  
2815              const schedule = () => {
2816                  if ( 'requestIdleCallback' in window ) {
2817                      window.requestIdleCallback( prefetch );
2818                  } else {
2819                      setTimeout( prefetch, 0 );
2820                  }
2821              };
2822  
2823              if ( 'complete' === document.readyState ) {
2824                  schedule();
2825              } else {
2826                  window.addEventListener( 'load', schedule, { once: true } );
2827              }
2828          }
2829          JS;
2830  
2831      wp_print_inline_script_tag( "( $js_function )();\n//# sourceURL=" . rawurlencode( __FUNCTION__ ) );
2832  }
2833  
2834  /**
2835   * Handles the enqueueing of block scripts and styles that are common to both
2836   * the editor and the front-end.
2837   *
2838   * @since 5.0.0
2839   */
2840  function wp_common_block_scripts_and_styles() {
2841      if ( is_admin() && ! wp_should_load_block_editor_scripts_and_styles() ) {
2842          return;
2843      }
2844  
2845      wp_enqueue_style( 'wp-block-library' );
2846  
2847      if ( current_theme_supports( 'wp-block-styles' ) && ! wp_should_load_separate_core_block_assets() ) {
2848          wp_enqueue_style( 'wp-block-library-theme' );
2849      }
2850  
2851      /**
2852       * Fires after enqueuing block assets for both editor and front-end.
2853       *
2854       * Call `add_action` on any hook before 'wp_enqueue_scripts'.
2855       *
2856       * In the function call you supply, simply use `wp_enqueue_script` and
2857       * `wp_enqueue_style` to add your functionality to the Gutenberg editor.
2858       *
2859       * @since 5.0.0
2860       */
2861      do_action( 'enqueue_block_assets' );
2862  }
2863  
2864  /**
2865   * Applies a filter to the list of style nodes that comes from WP_Theme_JSON::get_style_nodes().
2866   *
2867   * This particular filter removes all of the blocks from the array.
2868   *
2869   * We want WP_Theme_JSON to be ignorant of the implementation details of how the CSS is being used.
2870   * This filter allows us to modify the output of WP_Theme_JSON depending on whether or not we are
2871   * loading separate assets, without making the class aware of that detail.
2872   *
2873   * @since 6.1.0
2874   *
2875   * @param array<array<string, mixed>> $nodes The nodes to filter.
2876   * @return array<array<string, mixed>> A filtered array of style nodes.
2877   */
2878  function wp_filter_out_block_nodes( $nodes ) {
2879      return array_filter(
2880          $nodes,
2881          static function ( $node ) {
2882              return ! in_array( 'blocks', $node['path'], true );
2883          },
2884          ARRAY_FILTER_USE_BOTH
2885      );
2886  }
2887  
2888  /**
2889   * Enqueues the global styles defined via theme.json.
2890   *
2891   * @since 5.8.0
2892   */
2893  function wp_enqueue_global_styles() {
2894      $assets_on_demand = wp_should_load_block_assets_on_demand();
2895      $is_block_theme   = wp_is_block_theme();
2896      $is_classic_theme = ! $is_block_theme;
2897  
2898      /**
2899       * Global styles should be printed in the HEAD for block themes, or for classic themes when loading assets on
2900       * demand is disabled (which is no longer the default since WordPress 6.9).
2901       *
2902       * @link https://core.trac.wordpress.org/ticket/53494
2903       * @link https://core.trac.wordpress.org/ticket/61965
2904       */
2905      if (
2906          doing_action( 'wp_footer' ) &&
2907          (
2908              $is_block_theme ||
2909              ( $is_classic_theme && ! $assets_on_demand )
2910          )
2911      ) {
2912          return;
2913      }
2914  
2915      /**
2916       * The footer should only be used for classic themes when loading assets on demand is enabled. In WP 6.9 this is the
2917       * default with the introduction of hoisting late-printed styles (via {@see wp_load_classic_theme_block_styles_on_demand()}).
2918       * So even though the main global styles are not printed here in the HEAD for classic themes with on-demand asset
2919       * loading, a placeholder for the global styles is still enqueued. Then when {@see wp_hoist_late_printed_styles()}
2920       * processes the output buffer, it can locate the placeholder and inject the global styles from the footer into the
2921       * HEAD, replacing the placeholder.
2922       *
2923       * @link https://core.trac.wordpress.org/ticket/64099
2924       */
2925      if ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand ) {
2926          if ( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) {
2927              wp_register_style( 'wp-global-styles-placeholder', false );
2928              wp_add_inline_style( 'wp-global-styles-placeholder', ':root { --wp-internal-comment: "Placeholder for wp_hoist_late_printed_styles() to replace with the global-styles printed at wp_footer." }' );
2929              wp_enqueue_style( 'wp-global-styles-placeholder' );
2930          }
2931          return;
2932      }
2933  
2934      /*
2935       * If loading the CSS for each block separately, then load the theme.json CSS conditionally.
2936       * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
2937       * This filter must be registered before calling wp_get_global_stylesheet();
2938       */
2939      add_filter( 'wp_theme_json_get_style_nodes', 'wp_filter_out_block_nodes' );
2940  
2941      $stylesheet = wp_get_global_stylesheet();
2942  
2943      /*
2944       * For block themes, merge Customizer's custom CSS into the global styles stylesheet
2945       * before the global styles custom CSS, ensuring proper cascade order.
2946       * For classic themes, let the Customizer CSS print separately via wp_custom_css_cb()
2947       * at priority 101 in wp_head, preserving its position at the end of the <head>.
2948       */
2949      if ( $is_block_theme ) {
2950          /*
2951           * Dequeue the Customizer's custom CSS
2952           * and add it before the global styles custom CSS.
2953           */
2954          remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
2955  
2956          /*
2957           * Get the custom CSS from the Customizer and add it to the global stylesheet.
2958           * Always do this in Customizer preview for the sake of live preview since it be empty.
2959           */
2960          $custom_css = trim( wp_get_custom_css() );
2961          if ( $custom_css || is_customize_preview() ) {
2962              if ( is_customize_preview() ) {
2963                  /*
2964                   * When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2965                   * to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2966                   * the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2967                   * would break live previewing.
2968                   */
2969                  $before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2970                  $after_milestone  = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2971                  $custom_css       = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2972                  $custom_css       = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
2973              }
2974              $custom_css = "\n" . $custom_css;
2975          }
2976          $stylesheet .= $custom_css;
2977  
2978          // Add the global styles custom CSS at the end.
2979          $stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) );
2980      }
2981  
2982      if ( empty( $stylesheet ) ) {
2983          return;
2984      }
2985  
2986      wp_register_style( 'global-styles', false );
2987      wp_add_inline_style( 'global-styles', $stylesheet );
2988      wp_enqueue_style( 'global-styles' );
2989  
2990      // Add each block as an inline css.
2991      wp_add_global_styles_for_blocks();
2992  }
2993  
2994  /**
2995   * Checks if the editor scripts and styles for all registered block types
2996   * should be enqueued on the current screen.
2997   *
2998   * @since 5.6.0
2999   *
3000   * @global WP_Screen $current_screen WordPress current screen object.
3001   *
3002   * @return bool Whether scripts and styles should be enqueued.
3003   */
3004  function wp_should_load_block_editor_scripts_and_styles() {
3005      global $current_screen;
3006  
3007      $is_block_editor_screen = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor();
3008  
3009      /**
3010       * Filters the flag that decides whether or not block editor scripts and styles
3011       * are going to be enqueued on the current screen.
3012       *
3013       * @since 5.6.0
3014       *
3015       * @param bool $is_block_editor_screen Current value of the flag.
3016       */
3017      return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
3018  }
3019  
3020  /**
3021   * Checks whether separate styles should be loaded for core blocks.
3022   *
3023   * When this function returns true, other functions ensure that core blocks use their own separate stylesheets.
3024   * When this function returns false, all core blocks will use the single combined 'wp-block-library' stylesheet.
3025   *
3026   * As a side effect, the return value will by default result in block assets to be loaded on demand, via the
3027   * {@see wp_should_load_block_assets_on_demand()} function. This behavior can be separately altered via that function.
3028   *
3029   * This only affects front end and not the block editor screens.
3030   *
3031   * @since 5.8.0
3032   * @see wp_should_load_block_assets_on_demand()
3033   * @see wp_enqueue_registered_block_scripts_and_styles()
3034   * @see register_block_style_handle()
3035   *
3036   * @return bool Whether separate core block assets will be loaded.
3037   */
3038  function wp_should_load_separate_core_block_assets() {
3039      if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
3040          return false;
3041      }
3042  
3043      /**
3044       * Filters whether block styles should be loaded separately.
3045       *
3046       * Returning false loads all core block assets, regardless of whether they are rendered
3047       * in a page or not. Returning true loads core block assets only when they are rendered.
3048       *
3049       * @since 5.8.0
3050       *
3051       * @param bool $load_separate_assets Whether separate assets will be loaded.
3052       *                                   Default false (all block assets are loaded, even when not used).
3053       */
3054      return apply_filters( 'should_load_separate_core_block_assets', false );
3055  }
3056  
3057  /**
3058   * Checks whether block styles should be loaded only on-render.
3059   *
3060   * When this function returns true, other functions ensure that blocks only load their assets on-render.
3061   * When this function returns false, all block assets are loaded regardless of whether they are rendered in a page.
3062   *
3063   * The default return value depends on the result of {@see wp_should_load_separate_core_block_assets()}, which controls
3064   * whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library' stylesheet.
3065   *
3066   * This only affects front end and not the block editor screens.
3067   *
3068   * @since 6.8.0
3069   * @see wp_should_load_separate_core_block_assets()
3070   *
3071   * @return bool Whether to load block assets only when they are rendered.
3072   */
3073  function wp_should_load_block_assets_on_demand() {
3074      if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
3075          return false;
3076      }
3077  
3078      /*
3079       * For backward compatibility, the default return value for this function is based on the return value of
3080       * `wp_should_load_separate_core_block_assets()`. Initially, this function used to control both of these concerns.
3081       */
3082      $load_assets_on_demand = wp_should_load_separate_core_block_assets();
3083  
3084      /**
3085       * Filters whether block styles should be loaded on demand.
3086       *
3087       * Returning false loads all block assets, regardless of whether they are rendered in a page or not.
3088       * Returning true loads block assets only when they are rendered.
3089       *
3090       * The default value of the filter depends on the result of {@see wp_should_load_separate_core_block_assets()},
3091       * which controls whether Core block stylesheets should be loaded separately or via a combined 'wp-block-library'
3092       * stylesheet.
3093       *
3094       * @since 6.8.0
3095       *
3096       * @param bool $load_assets_on_demand Whether to load block assets only when they are rendered.
3097       */
3098      return apply_filters( 'should_load_block_assets_on_demand', $load_assets_on_demand );
3099  }
3100  
3101  /**
3102   * Enqueues registered block scripts and styles, depending on current rendered
3103   * context (only enqueuing editor scripts while in context of the editor).
3104   *
3105   * @since 5.0.0
3106   */
3107  function wp_enqueue_registered_block_scripts_and_styles() {
3108      if ( wp_should_load_block_assets_on_demand() ) {
3109          /**
3110           * Add placeholder for where block styles would historically get enqueued in a classic theme when block assets
3111           * are not loaded on demand. This happens right after {@see wp_common_block_scripts_and_styles()} is called
3112           * at which time wp-block-library is enqueued.
3113           */
3114          if ( ! wp_is_block_theme() && has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) {
3115              wp_register_style( 'wp-block-styles-placeholder', false );
3116              wp_add_inline_style( 'wp-block-styles-placeholder', ':root { --wp-internal-comment: "Placeholder for wp_hoist_late_printed_styles() to replace with the block styles printed at wp_footer." }' );
3117              wp_enqueue_style( 'wp-block-styles-placeholder' );
3118          }
3119          return;
3120      }
3121  
3122      $load_editor_scripts_and_styles = is_admin() && wp_should_load_block_editor_scripts_and_styles();
3123  
3124      $block_registry = WP_Block_Type_Registry::get_instance();
3125  
3126      /*
3127       * Block styles are only enqueued if they're registered. For core blocks, this is only the case if
3128       * `wp_should_load_separate_core_block_assets()` returns true. Otherwise they use the single combined
3129       * 'wp-block-library` stylesheet. See also `register_core_block_style_handles()`.
3130       * Since `wp_enqueue_style()` does not trigger warnings if the style is not registered, it is okay to not cater for
3131       * this behavior here and simply call `wp_enqueue_style()` unconditionally.
3132       */
3133      foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
3134          // Front-end and editor styles.
3135          foreach ( $block_type->style_handles as $style_handle ) {
3136              wp_enqueue_style( $style_handle );
3137          }
3138  
3139          // Front-end and editor scripts.
3140          foreach ( $block_type->script_handles as $script_handle ) {
3141              wp_enqueue_script( $script_handle );
3142          }
3143  
3144          if ( $load_editor_scripts_and_styles ) {
3145              // Editor styles.
3146              foreach ( $block_type->editor_style_handles as $editor_style_handle ) {
3147                  wp_enqueue_style( $editor_style_handle );
3148              }
3149  
3150              // Editor scripts.
3151              foreach ( $block_type->editor_script_handles as $editor_script_handle ) {
3152                  wp_enqueue_script( $editor_script_handle );
3153              }
3154          }
3155      }
3156  }
3157  
3158  /**
3159   * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
3160   *
3161   * @since 5.3.0
3162   *
3163   * @global WP_Styles $wp_styles
3164   */
3165  function enqueue_block_styles_assets() {
3166      global $wp_styles;
3167  
3168      $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
3169  
3170      foreach ( $block_styles as $block_name => $styles ) {
3171          foreach ( $styles as $style_properties ) {
3172              if ( isset( $style_properties['style_handle'] ) ) {
3173  
3174                  // If the site loads block styles on demand, enqueue the stylesheet on render.
3175                  if ( wp_should_load_block_assets_on_demand() ) {
3176                      add_filter(
3177                          'render_block',
3178                          static function ( $html, $block ) use ( $block_name, $style_properties ) {
3179                              if ( $block['blockName'] === $block_name ) {
3180                                  wp_enqueue_style( $style_properties['style_handle'] );
3181                              }
3182                              return $html;
3183                          },
3184                          10,
3185                          2
3186                      );
3187                  } else {
3188                      wp_enqueue_style( $style_properties['style_handle'] );
3189                  }
3190              }
3191              if ( isset( $style_properties['inline_style'] ) ) {
3192  
3193                  // Default to "wp-block-library".
3194                  $handle = 'wp-block-library';
3195  
3196                  // If the site loads block styles on demand, check if the block has a stylesheet registered.
3197                  if ( wp_should_load_block_assets_on_demand() ) {
3198                      $block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' );
3199  
3200                      if ( isset( $wp_styles->registered[ $block_stylesheet_handle ] ) ) {
3201                          $handle = $block_stylesheet_handle;
3202                      }
3203                  }
3204  
3205                  // Add inline styles to the calculated handle.
3206                  wp_add_inline_style( $handle, $style_properties['inline_style'] );
3207              }
3208          }
3209      }
3210  }
3211  
3212  /**
3213   * Function responsible for enqueuing the assets required for block styles functionality on the editor.
3214   *
3215   * @since 5.3.0
3216   */
3217  function enqueue_editor_block_styles_assets() {
3218      $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
3219  
3220      $register_script_lines = array( '( function() {' );
3221      foreach ( $block_styles as $block_name => $styles ) {
3222          foreach ( $styles as $style_properties ) {
3223              $block_style = array(
3224                  'name'  => $style_properties['name'],
3225                  'label' => $style_properties['label'],
3226              );
3227              if ( isset( $style_properties['is_default'] ) ) {
3228                  $block_style['isDefault'] = $style_properties['is_default'];
3229              }
3230              $register_script_lines[] = sprintf(
3231                  '    wp.blocks.registerBlockStyle( \'%s\', %s );',
3232                  $block_name,
3233                  wp_json_encode( $block_style, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
3234              );
3235          }
3236      }
3237      $register_script_lines[] = '} )();';
3238      $inline_script           = implode( "\n", $register_script_lines );
3239  
3240      wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, array( 'in_footer' => true ) );
3241      wp_add_inline_script( 'wp-block-styles', $inline_script );
3242      wp_enqueue_script( 'wp-block-styles' );
3243  }
3244  
3245  /**
3246   * Enqueues the assets required for the block directory within the block editor.
3247   *
3248   * @since 5.5.0
3249   */
3250  function wp_enqueue_editor_block_directory_assets() {
3251      wp_enqueue_script( 'wp-block-directory' );
3252      // Prefetched for the block editor by wp_prefetch_admin_assets(), which needs updating if this changes.
3253      wp_enqueue_style( 'wp-block-directory' );
3254  }
3255  
3256  /**
3257   * Enqueues the assets required for the format library within the block editor.
3258   *
3259   * @since 5.8.0
3260   */
3261  function wp_enqueue_editor_format_library_assets() {
3262      wp_enqueue_script( 'wp-format-library' );
3263      // Prefetched for the block editor by wp_prefetch_admin_assets(), which needs updating if this changes.
3264      wp_enqueue_style( 'wp-format-library' );
3265  }
3266  
3267  /**
3268   * Formats `<script>` loader tags.
3269   *
3270   * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
3271   * Automatically injects type attribute if needed.
3272   *
3273   * @since 5.7.0
3274   *
3275   * @param array<string, string|bool> $attributes Key-value pairs representing `<script>` tag attributes.
3276   * @return string String containing `<script>` opening and closing tags.
3277   *
3278   * @phpstan-return non-falsy-string
3279   */
3280  function wp_get_script_tag( $attributes ) {
3281      /**
3282       * Filters attributes to be added to a script tag.
3283       *
3284       * @since 5.7.0
3285       *
3286       * @param array $attributes Key-value pairs representing `<script>` tag attributes.
3287       *                          Only the attribute name is added to the `<script>` tag for
3288       *                          entries with a boolean value, and that are true.
3289       */
3290      $attributes = apply_filters( 'wp_script_attributes', $attributes );
3291  
3292      $processor = new WP_HTML_Tag_Processor( '<script></script>' );
3293      $processor->next_tag();
3294      foreach ( $attributes as $name => $value ) {
3295          /*
3296           * Lexical variations of an attribute name may represent the
3297           * same attribute in HTML, therefore it’s possible that the
3298           * input array might contain duplicate attributes even though
3299           * it’s keyed on their name. Calling code should rewrite an
3300           * attribute’s value rather than sending a duplicate attribute.
3301           *
3302           * Example:
3303           *
3304           *     array( 'id' => 'main', 'ID' => 'nav' )
3305           *
3306           * In this example, there are two keys both describing the `id`
3307           * attribute. PHP array iteration is in key-insertion order so
3308           * the 'id' value will be set in the SCRIPT tag.
3309           */
3310          if ( null !== $processor->get_attribute( $name ) ) {
3311              continue;
3312          }
3313  
3314          $processor->set_attribute( $name, $value ?? true );
3315      }
3316      return "{$processor->get_updated_html()}\n";
3317  }
3318  
3319  /**
3320   * Prints formatted `<script>` loader tag.
3321   *
3322   * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_script_attributes'} filter.
3323   * Automatically injects type attribute if needed.
3324   *
3325   * @since 5.7.0
3326   *
3327   * @param array<string, string|bool> $attributes Key-value pairs representing `<script>` tag attributes.
3328   */
3329  function wp_print_script_tag( $attributes ) {
3330      echo wp_get_script_tag( $attributes );
3331  }
3332  
3333  /**
3334   * Constructs an inline script tag.
3335   *
3336   * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_inline_script_attributes'} filter.
3337   *
3338   * If the `$data` is unsafe to embed in a `<script>` tag, an empty script tag with the provided
3339   * attributes will be returned. JavaScript and JSON contents can be escaped, so this is only likely
3340   * to be a problem with unusual content types.
3341   *
3342   * Example:
3343   *
3344   *     // The dangerous JavaScript in this example will be safely escaped.
3345   *     // A string with the script tag and the desired contents will be returned.
3346   *     wp_get_inline_script_tag( 'console.log( "</script>" );' );
3347   *
3348   *     // This data is unsafe and `text/plain` cannot be escaped.
3349   *     // The following will return `""` to indicate failure:
3350   *     wp_get_inline_script_tag( '</script>', array( 'type' => 'text/plain' ) );
3351   *
3352   * @since 5.7.0
3353   * @since 7.0.0 Returns an empty string if the data cannot be safely embedded in a script tag.
3354   *
3355   * @param string                     $data       Data for script tag: JavaScript, importmap, speculationrules, etc.
3356   * @param array<string, string|bool> $attributes Optional. Key-value pairs representing `<script>` tag attributes.
3357   * @return string HTML script tag containing the provided $data or the empty string `""` if the data cannot be safely embedded in a script tag.
3358   */
3359  function wp_get_inline_script_tag( $data, $attributes = array() ) {
3360      $data = "\n" . trim( $data, "\n\r " ) . "\n";
3361  
3362      /**
3363       * Filters attributes to be added to a script tag.
3364       *
3365       * Escaping and normalization can make the text a browser parses from the printed tag differ
3366       * from `$data`. A callback that needs that text, such as one computing a CSP hash, can set
3367       * `$data` as the modifiable text of a SCRIPT tag in a `WP_HTML_Tag_Processor` with the
3368       * filtered attributes applied and then read {@see WP_HTML_Tag_Processor::get_modifiable_text()},
3369       * at a late priority so that the attributes are final.
3370       *
3371       * @since 5.7.0
3372       *
3373       * @param array<string, string|bool> $attributes Key-value pairs representing `<script>` tag attributes.
3374       *                                               Only the attribute name is added to the `<script>` tag for
3375       *                                               entries with a boolean value, and that are true.
3376       * @param string                     $data       Inline data.
3377       */
3378      $attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $data );
3379  
3380      $processor = new WP_HTML_Tag_Processor( '<script></script>' );
3381      $processor->next_tag();
3382      foreach ( $attributes as $name => $value ) {
3383          /*
3384           * Lexical variations of an attribute name may represent the
3385           * same attribute in HTML, therefore it’s possible that the
3386           * input array might contain duplicate attributes even though
3387           * it’s keyed on their name. Calling code should rewrite an
3388           * attribute’s value rather than sending a duplicate attribute.
3389           *
3390           * Example:
3391           *
3392           *     array( 'id' => 'main', 'ID' => 'nav' )
3393           *
3394           * In this example, there are two keys both describing the `id`
3395           * attribute. PHP array iteration is in key-insertion order so
3396           * the 'id' value will be set in the SCRIPT tag.
3397           */
3398          if ( null !== $processor->get_attribute( $name ) ) {
3399              continue;
3400          }
3401  
3402          $processor->set_attribute( $name, $value ?? true );
3403      }
3404  
3405      if ( ! $processor->set_modifiable_text( $data ) ) {
3406          return '';
3407      }
3408  
3409      return "{$processor->get_updated_html()}\n";
3410  }
3411  
3412  /**
3413   * Prints an inline script tag.
3414   *
3415   * It is possible to inject attributes in the `<script>` tag via the {@see 'wp_inline_script_attributes'} filter.
3416   * Automatically injects type attribute if needed.
3417   *
3418   * @since 5.7.0
3419   *
3420   * @param string                     $data       Data for script tag: JavaScript, importmap, speculationrules, etc.
3421   * @param array<string, string|bool> $attributes Optional. Key-value pairs representing `<script>` tag attributes.
3422   */
3423  function wp_print_inline_script_tag( $data, $attributes = array() ) {
3424      echo wp_get_inline_script_tag( $data, $attributes );
3425  }
3426  
3427  /**
3428   * Allows small styles to be inlined.
3429   *
3430   * This improves performance and sustainability, and is opt-in. Stylesheets can opt in
3431   * by adding `path` data using `wp_style_add_data`, and defining the file's absolute path:
3432   *
3433   *     wp_style_add_data( $style_handle, 'path', $file_path );
3434   *
3435   * @since 5.8.0
3436   *
3437   * @global WP_Styles $wp_styles
3438   */
3439  function wp_maybe_inline_styles() {
3440      global $wp_styles;
3441  
3442      $total_inline_limit = 40000;
3443      /**
3444       * The maximum size of inlined styles in bytes.
3445       *
3446       * @since 5.8.0
3447       * @since 6.9.0 The default limit increased from 20K to 40K.
3448       *
3449       * @param int $total_inline_limit The file-size threshold, in bytes. Default 40000.
3450       */
3451      $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit );
3452  
3453      $styles = array();
3454  
3455      // Build an array of styles that have a path defined.
3456      foreach ( $wp_styles->queue as $handle ) {
3457          if ( ! isset( $wp_styles->registered[ $handle ] ) ) {
3458              continue;
3459          }
3460          $src  = $wp_styles->registered[ $handle ]->src;
3461          $path = $wp_styles->get_data( $handle, 'path' );
3462          if ( $path && $src ) {
3463              $size = wp_filesize( $path );
3464              if ( 0 === $size && ! file_exists( $path ) ) {
3465                  _doing_it_wrong(
3466                      __FUNCTION__,
3467                      sprintf(
3468                          /* translators: 1: 'path', 2: filesystem path, 3: style handle */
3469                          __( 'Unable to read the "%1$s" key with value "%2$s" for stylesheet "%3$s".' ),
3470                          'path',
3471                          esc_html( $path ),
3472                          esc_html( $handle )
3473                      ),
3474                      '7.0.0'
3475                  );
3476                  continue;
3477              }
3478              $styles[] = array(
3479                  'handle' => $handle,
3480                  'src'    => $src,
3481                  'path'   => $path,
3482                  'size'   => $size,
3483              );
3484          }
3485      }
3486  
3487      if ( ! empty( $styles ) ) {
3488          // Reorder styles array based on size.
3489          usort(
3490              $styles,
3491              static function ( $a, $b ) {
3492                  return $a['size'] <=> $b['size'];
3493              }
3494          );
3495  
3496          /*
3497           * The total inlined size.
3498           *
3499           * On each iteration of the loop, if a style gets added inline the value of this var increases
3500           * to reflect the total size of inlined styles.
3501           */
3502          $total_inline_size = 0;
3503  
3504          // Loop styles.
3505          foreach ( $styles as $style ) {
3506  
3507              // Size check. Since styles are ordered by size, we can break the loop.
3508              if ( $total_inline_size + $style['size'] > $total_inline_limit ) {
3509                  break;
3510              }
3511  
3512              // Get the styles if we don't already have them.
3513              if ( ! is_readable( $style['path'] ) ) {
3514                  _doing_it_wrong(
3515                      __FUNCTION__,
3516                      sprintf(
3517                          /* translators: 1: 'path', 2: filesystem path, 3: style handle */
3518                          __( 'Unable to read the "%1$s" key with value "%2$s" for stylesheet "%3$s".' ),
3519                          'path',
3520                          esc_html( $style['path'] ),
3521                          esc_html( $style['handle'] )
3522                      ),
3523                      '7.0.0'
3524                  );
3525                  continue;
3526              }
3527              $style['css'] = file_get_contents( $style['path'] );
3528  
3529              /*
3530               * Check if the style contains relative URLs that need to be modified.
3531               * URLs relative to the stylesheet's path should be converted to relative to the site's root.
3532               */
3533              $style['css'] = _wp_normalize_relative_css_links( $style['css'], $style['src'] );
3534  
3535              // Keep track of the original `src` for the style that was inlined so that the `sourceURL` comment can be added.
3536              $wp_styles->add_data( $style['handle'], 'inlined_src', $style['src'] );
3537  
3538              // Set `src` to `false` and add styles inline.
3539              $wp_styles->registered[ $style['handle'] ]->src = false;
3540              if ( empty( $wp_styles->registered[ $style['handle'] ]->extra['after'] ) ) {
3541                  $wp_styles->registered[ $style['handle'] ]->extra['after'] = array();
3542              }
3543              array_unshift( $wp_styles->registered[ $style['handle'] ]->extra['after'], $style['css'] );
3544  
3545              // Add the styles size to the $total_inline_size var.
3546              $total_inline_size += (int) $style['size'];
3547          }
3548      }
3549  }
3550  
3551  /**
3552   * Makes URLs relative to the WordPress installation.
3553   *
3554   * @since 5.9.0
3555   * @access private
3556   *
3557   * @param string $css            The CSS to make URLs relative to the WordPress installation.
3558   * @param string $stylesheet_url The URL to the stylesheet.
3559   * @return string The CSS with URLs made relative to the WordPress installation.
3560   */
3561  function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
3562      return preg_replace_callback(
3563          '#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#',
3564          static function ( $matches ) use ( $stylesheet_url ) {
3565              list( , $prefix, $url ) = $matches;
3566  
3567              // Short-circuit if the URL does not require normalization.
3568              if (
3569                  str_starts_with( $url, 'http:' ) ||
3570                  str_starts_with( $url, 'https:' ) ||
3571                  str_starts_with( $url, '/' ) ||
3572                  str_starts_with( $url, '#' ) ||
3573                  str_starts_with( $url, 'data:' )
3574              ) {
3575                  return $matches[0];
3576              }
3577  
3578              // Build the absolute URL.
3579              $absolute_url = dirname( $stylesheet_url ) . '/' . $url;
3580              $absolute_url = str_replace( '/./', '/', $absolute_url );
3581  
3582              // Convert to URL related to the site root.
3583              $url = wp_make_link_relative( $absolute_url );
3584  
3585              return $prefix . $url;
3586          },
3587          $css
3588      );
3589  }
3590  
3591  /**
3592   * Function that enqueues the CSS Custom Properties coming from theme.json.
3593   *
3594   * @since 5.9.0
3595   */
3596  function wp_enqueue_global_styles_css_custom_properties() {
3597      wp_register_style( 'global-styles-css-custom-properties', false );
3598      wp_add_inline_style( 'global-styles-css-custom-properties', wp_get_global_stylesheet( array( 'variables' ) ) );
3599      wp_enqueue_style( 'global-styles-css-custom-properties' );
3600  }
3601  
3602  /**
3603   * Hooks inline styles in the proper place, depending on the active theme.
3604   *
3605   * @since 5.9.1
3606   * @since 6.1.0 Added the `$priority` parameter.
3607   *
3608   * For block themes, styles are loaded in the head.
3609   * For classic ones, styles are loaded in the body because the wp_head action happens before render_block.
3610   *
3611   * @link https://core.trac.wordpress.org/ticket/53494.
3612   *
3613   * @param string $style    String containing the CSS styles to be added.
3614   * @param int    $priority To set the priority for the add_action.
3615   */
3616  function wp_enqueue_block_support_styles( $style, $priority = 10 ) {
3617      $action_hook_name = 'wp_footer';
3618      if ( wp_is_block_theme() ) {
3619          $action_hook_name = 'wp_head';
3620      }
3621      add_action(
3622          $action_hook_name,
3623          static function () use ( $style ) {
3624              $processor = new WP_HTML_Tag_Processor( '<style></style>' );
3625              $processor->next_tag();
3626              $processor->set_modifiable_text( $style );
3627              echo "{$processor->get_updated_html()}\n";
3628          },
3629          $priority
3630      );
3631  }
3632  
3633  /**
3634   * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
3635   * Styles are stored via the style engine API.
3636   *
3637   * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
3638   *
3639   * @since 6.1.0
3640   *
3641   * @param array<string, bool> $options {
3642   *     Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context().
3643   *     Default empty array.
3644   *
3645   *     @type bool $optimize Whether to optimize the CSS output, e.g., combine rules.
3646   *                          Default false.
3647   *     @type bool $prettify Whether to add new lines and indents to output.
3648   *                          Default to whether the `SCRIPT_DEBUG` constant is defined.
3649   * }
3650   */
3651  function wp_enqueue_stored_styles( $options = array() ) {
3652      // Note: Styles printed at wp_footer for classic themes may still end up in the head due to wp_load_classic_theme_block_styles_on_demand().
3653      $is_block_theme   = wp_is_block_theme();
3654      $is_classic_theme = ! $is_block_theme;
3655  
3656      /*
3657       * For block themes, this function prints stored styles in the header.
3658       * For classic themes, in the footer.
3659       */
3660      if (
3661          ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
3662          ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) )
3663      ) {
3664          return;
3665      }
3666  
3667      $core_styles_keys         = array( 'block-supports' );
3668      $compiled_core_stylesheet = '';
3669      $style_tag_id             = 'core';
3670      // Adds comment if code is prettified to identify core styles sections in debugging.
3671      $should_prettify = isset( $options['prettify'] ) ? true === $options['prettify'] : defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
3672      foreach ( $core_styles_keys as $style_key ) {
3673          if ( $should_prettify ) {
3674              $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n";
3675          }
3676          // Chains core store ids to signify what the styles contain.
3677          $style_tag_id             .= '-' . $style_key;
3678          $compiled_core_stylesheet .= wp_style_engine_get_stylesheet_from_context( $style_key, $options );
3679      }
3680  
3681      // Combines Core styles.
3682      if ( ! empty( $compiled_core_stylesheet ) ) {
3683          wp_register_style( $style_tag_id, false );
3684          wp_add_inline_style( $style_tag_id, $compiled_core_stylesheet );
3685          wp_enqueue_style( $style_tag_id );
3686      }
3687  
3688      // Prints out any other stores registered by themes or otherwise.
3689      $additional_stores = WP_Style_Engine_CSS_Rules_Store::get_stores();
3690      foreach ( array_keys( $additional_stores ) as $store_name ) {
3691          if ( in_array( $store_name, $core_styles_keys, true ) ) {
3692              continue;
3693          }
3694          $styles = wp_style_engine_get_stylesheet_from_context( $store_name, $options );
3695          if ( ! empty( $styles ) ) {
3696              $key = "wp-style-engine-$store_name";
3697              wp_register_style( $key, false );
3698              wp_add_inline_style( $key, $styles );
3699              wp_enqueue_style( $key );
3700          }
3701      }
3702  }
3703  
3704  /**
3705   * Enqueues a stylesheet for a specific block.
3706   *
3707   * If the theme has opted-in to load block styles on demand,
3708   * then the stylesheet will be enqueued on-render,
3709   * otherwise when the block inits.
3710   *
3711   * @since 5.9.0
3712   *
3713   * @param string                                   $block_name The block-name, including namespace.
3714   * @param array<string, string|string[]|bool|null> $args       {
3715   *     An array of arguments. See wp_register_style() for full information about each argument.
3716   *
3717   *     @type string           $handle The handle for the stylesheet.
3718   *     @type string|false     $src    The source URL of the stylesheet.
3719   *     @type string[]         $deps   Array of registered stylesheet handles this stylesheet depends on.
3720   *     @type string|bool|null $ver    Stylesheet version number.
3721   *     @type string           $media  The media for which this stylesheet has been defined.
3722   *     @type string|null      $path   Absolute path to the stylesheet, so that it can potentially be inlined.
3723   * }
3724   */
3725  function wp_enqueue_block_style( $block_name, $args ) {
3726      $args = wp_parse_args(
3727          $args,
3728          array(
3729              'handle' => '',
3730              'src'    => '',
3731              'deps'   => array(),
3732              'ver'    => false,
3733              'media'  => 'all',
3734          )
3735      );
3736  
3737      /**
3738       * Callback function to register and enqueue styles.
3739       *
3740       * @param string $content When the callback is used for the render_block filter,
3741       *                        the content needs to be returned so the function parameter
3742       *                        is to ensure the content exists.
3743       * @return string Block content.
3744       */
3745      $callback = static function ( $content ) use ( $args ) {
3746          // Register the stylesheet.
3747          if ( ! empty( $args['src'] ) ) {
3748              wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );
3749          }
3750  
3751          // Add `path` data if provided.
3752          if ( isset( $args['path'] ) ) {
3753              wp_style_add_data( $args['handle'], 'path', $args['path'] );
3754  
3755              // Get the RTL file path.
3756              $rtl_file_path = str_replace( '.css', '-rtl.css', $args['path'] );
3757  
3758              // Add RTL stylesheet.
3759              if ( file_exists( $rtl_file_path ) ) {
3760                  wp_style_add_data( $args['handle'], 'rtl', 'replace' );
3761  
3762                  if ( is_rtl() ) {
3763                      wp_style_add_data( $args['handle'], 'path', $rtl_file_path );
3764                  }
3765              }
3766          }
3767  
3768          // Enqueue the stylesheet.
3769          wp_enqueue_style( $args['handle'] );
3770  
3771          return $content;
3772      };
3773  
3774      $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
3775      if ( wp_should_load_block_assets_on_demand() ) {
3776          /**
3777           * Callback function to register and enqueue styles.
3778           *
3779           * @param string $content The block content.
3780           * @param array  $block   The full block, including name and attributes.
3781           * @return string Block content.
3782           */
3783          $callback_separate = static function ( $content, $block ) use ( $block_name, $callback ) {
3784              if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {
3785                  return $callback( $content );
3786              }
3787              return $content;
3788          };
3789  
3790          /*
3791           * The filter's callback here is an anonymous function because
3792           * using a named function in this case is not possible.
3793           *
3794           * The function cannot be unhooked, however, users are still able
3795           * to dequeue the stylesheets registered/enqueued by the callback
3796           * which is why in this case, using an anonymous function
3797           * was deemed acceptable.
3798           */
3799          add_filter( 'render_block', $callback_separate, 10, 2 );
3800          return;
3801      }
3802  
3803      /*
3804       * The filter's callback here is an anonymous function because
3805       * using a named function in this case is not possible.
3806       *
3807       * The function cannot be unhooked, however, users are still able
3808       * to dequeue the stylesheets registered/enqueued by the callback
3809       * which is why in this case, using an anonymous function
3810       * was deemed acceptable.
3811       */
3812      add_filter( $hook, $callback );
3813  
3814      // Enqueue assets in the editor.
3815      add_action( 'enqueue_block_assets', $callback );
3816  }
3817  
3818  /**
3819   * Loads classic theme styles when the current theme lacks a theme.json file.
3820   *
3821   * This is used for backwards compatibility for Button and File blocks specifically.
3822   *
3823   * @since 6.1.0
3824   * @since 6.2.0 Added File block styles.
3825   * @since 6.8.0 Moved stylesheet registration outside of this function.
3826   */
3827  function wp_enqueue_classic_theme_styles() {
3828      if ( ! wp_theme_has_theme_json() ) {
3829          wp_enqueue_style( 'classic-theme-styles' );
3830      }
3831  }
3832  
3833  /**
3834   * Enqueues the assets required for the Command Palette.
3835   *
3836   * @since 6.9.0
3837   *
3838   * @global array  $menu
3839   * @global array  $submenu
3840   */
3841  function wp_enqueue_command_palette_assets() {
3842      global $menu, $submenu;
3843  
3844      $command_palette_settings = array(
3845          'is_network_admin' => is_network_admin(),
3846      );
3847  
3848      /**
3849       * Extracts root-level text nodes from HTML string.
3850       *
3851       * @ignore
3852       * @param string $label HTML string to extract text from.
3853       * @return string Extracted text content, trimmed.
3854       */
3855      $extract_root_text = static function ( string $label ): string {
3856          if ( '' === $label ) {
3857              return '';
3858          }
3859  
3860          $processor  = new WP_HTML_Tag_Processor( $label );
3861          $text_parts = array();
3862          $depth      = 0;
3863  
3864          while ( $processor->next_token() ) {
3865              $token_type = $processor->get_token_type();
3866  
3867              if ( '#text' === $token_type ) {
3868                  if ( 0 === $depth ) {
3869                      $text_parts[] = $processor->get_modifiable_text();
3870                  }
3871                  continue;
3872              }
3873  
3874              if ( '#tag' !== $token_type ) {
3875                  continue;
3876              }
3877  
3878              if ( $processor->is_tag_closer() ) {
3879                  if ( $depth > 0 ) {
3880                      --$depth;
3881                  }
3882                  continue;
3883              }
3884  
3885              $token_name = $processor->get_tag();
3886              if ( $token_name && ! WP_HTML_Processor::is_void( $token_name ) ) {
3887                  ++$depth;
3888              }
3889          }
3890  
3891          return trim( implode( '', $text_parts ) );
3892      };
3893  
3894      if ( $menu ) {
3895          $menu_commands = array();
3896          foreach ( $menu as $menu_item ) {
3897              if ( empty( $menu_item[0] ) || ! is_string( $menu_item[0] ) || ! empty( $menu_item[1] ) && ! current_user_can( $menu_item[1] ) ) {
3898                  continue;
3899              }
3900  
3901              $menu_label = $extract_root_text( $menu_item[0] );
3902              $menu_url   = '';
3903              $menu_slug  = $menu_item[2];
3904  
3905              if ( preg_match( '/\.php($|\?)/', $menu_slug ) || wp_http_validate_url( $menu_slug ) ) {
3906                  $menu_url = $menu_slug;
3907              } elseif ( ! empty( menu_page_url( $menu_slug, false ) ) ) {
3908                  $menu_url = WP_HTML_Decoder::decode_attribute( menu_page_url( $menu_slug, false ) );
3909              }
3910  
3911              if ( $menu_url ) {
3912                  $menu_commands[] = array(
3913                      'label' => $menu_label,
3914                      'url'   => $menu_url,
3915                      'name'  => $menu_slug,
3916                  );
3917              }
3918  
3919              if ( array_key_exists( $menu_slug, $submenu ) ) {
3920                  foreach ( $submenu[ $menu_slug ] as $submenu_item ) {
3921                      if ( empty( $submenu_item[0] ) || ! empty( $submenu_item[1] ) && ! current_user_can( $submenu_item[1] ) ) {
3922                          continue;
3923                      }
3924  
3925                      $submenu_label = $extract_root_text( $submenu_item[0] );
3926                      $submenu_url   = '';
3927                      $submenu_slug  = $submenu_item[2];
3928  
3929                      if ( preg_match( '/\.php($|\?)/', $submenu_slug ) || wp_http_validate_url( $submenu_slug ) ) {
3930                          $submenu_url = $submenu_slug;
3931                      } elseif ( ! empty( menu_page_url( $submenu_slug, false ) ) ) {
3932                          $submenu_url = WP_HTML_Decoder::decode_attribute( menu_page_url( $submenu_slug, false ) );
3933                      }
3934                      if ( $submenu_url ) {
3935                          $menu_commands[] = array(
3936                              'label' => sprintf(
3937                                  /* translators: 1: Menu label, 2: Submenu label. */
3938                                  __( '%1$s > %2$s' ),
3939                                  $menu_label,
3940                                  $submenu_label
3941                              ),
3942                              'url'   => $submenu_url,
3943                              'name'  => $menu_slug . '-' . $submenu_item[2],
3944                          );
3945                      }
3946                  }
3947              }
3948          }
3949          $command_palette_settings['menu_commands'] = $menu_commands;
3950      }
3951  
3952      wp_enqueue_script( 'wp-commands' );
3953      // Prefetched from the login screen by wp_prefetch_admin_assets(), which needs updating if this changes.
3954      wp_enqueue_style( 'wp-commands' );
3955      wp_enqueue_script( 'wp-core-commands' );
3956  
3957      wp_add_inline_script(
3958          'wp-core-commands',
3959          sprintf(
3960              'wp.coreCommands.initializeCommandPalette( %s );',
3961              wp_json_encode( $command_palette_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
3962          )
3963      );
3964  }
3965  
3966  /**
3967   * Removes leading and trailing _empty_ script tags.
3968   *
3969   * This is a helper meant to be used for literal script tag construction
3970   * within `wp_get_inline_script_tag()` or `wp_print_inline_script_tag()`.
3971   * It removes the literal values of "<script>" and "</script>" from
3972   * around an inline script after trimming whitespace. Typically this
3973   * is used in conjunction with output buffering, where `ob_get_clean()`
3974   * is passed as the `$contents` argument.
3975   *
3976   * Example:
3977   *
3978   *     // Strips exact literal empty SCRIPT tags.
3979   *     $js = '<script>sayHello();</script>;
3980   *     'sayHello();' === wp_remove_surrounding_empty_script_tags( $js );
3981   *
3982   *     // Otherwise if anything is different it warns in the JS console.
3983   *     $js = '<script type="module">console.log( "hi" );</script>';
3984   *     'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js );
3985   *
3986   * @since 6.4.0
3987   * @access private
3988   *
3989   * @see wp_print_inline_script_tag()
3990   * @see wp_get_inline_script_tag()
3991   *
3992   * @param string $contents Script body with manually created SCRIPT tag literals.
3993   * @return string Script body without surrounding script tag literals, or
3994   *                original contents if both exact literals aren't present.
3995   */
3996  function wp_remove_surrounding_empty_script_tags( $contents ) {
3997      $contents = trim( $contents );
3998      $opener   = '<SCRIPT>';
3999      $closer   = '</SCRIPT>';
4000  
4001      if (
4002          strlen( $contents ) > strlen( $opener ) + strlen( $closer ) &&
4003          strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener &&
4004          strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer
4005      ) {
4006          return substr( $contents, strlen( $opener ), -strlen( $closer ) );
4007      } else {
4008          $error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' );
4009          _doing_it_wrong( __FUNCTION__, $error_message, '6.4' );
4010          return sprintf(
4011              'console.error(%s)',
4012              wp_json_encode(
4013                  sprintf(
4014                      /* translators: %s: wp_remove_surrounding_empty_script_tags() */
4015                      __( 'Function %s used incorrectly in PHP.' ),
4016                      'wp_remove_surrounding_empty_script_tags()'
4017                  ) . ' ' . $error_message
4018              )
4019          );
4020      }
4021  }
4022  
4023  /**
4024   * Adds hooks to load block styles on demand in classic themes.
4025   *
4026   * This function must be called before {@see wp_default_styles()} and {@see register_core_block_style_handles()} so that
4027   * the filters are added to cause {@see wp_should_load_separate_core_block_assets()} to return true.
4028   *
4029   * @since 6.9.0
4030   * @since 7.0.0 This is now invoked at the `wp_default_styles` action with priority 0 instead of at `init` with priority 8.
4031   *
4032   * @see _add_default_theme_supports()
4033   */
4034  function wp_load_classic_theme_block_styles_on_demand(): void {
4035      // This is not relevant to block themes, as they are opted in to loading separate styles on demand via _add_default_theme_supports().
4036      if ( wp_is_block_theme() ) {
4037          return;
4038      }
4039  
4040      /*
4041       * Make sure that wp_should_output_buffer_template_for_enhancement() returns true even if there aren't any
4042       * `wp_template_enhancement_output_buffer` filters added, but do so at priority zero so that applications which
4043       * wish to stream responses can more easily turn this off.
4044       */
4045      add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 );
4046  
4047      // If a site has opted out of the template enhancement output buffer, then bail.
4048      if ( ! wp_should_output_buffer_template_for_enhancement() ) {
4049          return;
4050      }
4051  
4052      // The following two filters are added by default for block themes in _add_default_theme_supports().
4053  
4054      /*
4055       * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally, and so
4056       * that block-specific styles will only be enqueued when they are used on the page. A priority of zero allows for
4057       * this to be easily overridden by themes which wish to opt out. If a site has explicitly opted out of loading
4058       * separate block styles, then abort.
4059       */
4060      add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
4061      if ( ! wp_should_load_separate_core_block_assets() ) {
4062          return;
4063      }
4064  
4065      /*
4066       * Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
4067       * As above, a priority of zero allows for this to be easily overridden by themes which wish to opt out. If a site
4068       * has explicitly opted out of loading block styles on demand, then abort.
4069       */
4070      add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
4071      if ( ! wp_should_load_block_assets_on_demand() ) {
4072          return;
4073      }
4074  
4075      // Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early.
4076      add_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' );
4077  }
4078  
4079  /**
4080   * Adds the hooks needed for CSS output to be delayed until after the content of the page has been established.
4081   *
4082   * @since 6.9.0
4083   *
4084   * @see wp_load_classic_theme_block_styles_on_demand()
4085   * @see _wp_footer_scripts()
4086   */
4087  function wp_hoist_late_printed_styles(): void {
4088      // Skip the embed template on-demand styles aren't relevant, and there is no wp_head action.
4089      if ( is_embed() ) {
4090          return;
4091      }
4092  
4093      /*
4094       * Add a placeholder comment into the inline styles for wp-block-library, after which the late block styles
4095       * can be hoisted from the footer to be printed in the header by means of a filter below on the template enhancement
4096       * output buffer.
4097       *
4098       * Note that wp_maybe_inline_styles() prepends the inlined style to the extra 'after' array, which happens after
4099       * this code runs. This ensures that the placeholder appears right after any inlined wp-block-library styles,
4100       * which would be common.css.
4101       */
4102      $placeholder = sprintf( '/*%s*/', uniqid( 'wp_block_styles_on_demand_placeholder:' ) );
4103      $dependency  = wp_styles()->query( 'wp-block-library', 'registered' );
4104      if ( $dependency ) {
4105          if ( ! isset( $dependency->extra['after'] ) ) {
4106              wp_add_inline_style( 'wp-block-library', $placeholder );
4107          } else {
4108              array_unshift( $dependency->extra['after'], $placeholder );
4109          }
4110      }
4111  
4112      /*
4113       * Create a substitute for `print_late_styles()` which is aware of block styles. This substitute does not print
4114       * the styles, but it captures what would be printed for block styles and non-block styles so that they can be
4115       * later hoisted to the HEAD in the template enhancement output buffer. This will run at `wp_print_footer_scripts`
4116       * before `print_footer_scripts()` is called.
4117       */
4118      $printed_core_block_styles  = '';
4119      $printed_other_block_styles = '';
4120      $printed_global_styles      = '';
4121      $printed_late_styles        = '';
4122  
4123      $capture_late_styles = static function () use ( &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {
4124          // Gather the styles related to on-demand block enqueues.
4125          $all_core_block_style_handles  = array();
4126          $all_other_block_style_handles = array();
4127          foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block_type ) {
4128              if ( str_starts_with( $block_type->name, 'core/' ) ) {
4129                  foreach ( $block_type->style_handles as $style_handle ) {
4130                      $all_core_block_style_handles[] = $style_handle;
4131                  }
4132              } else {
4133                  foreach ( $block_type->style_handles as $style_handle ) {
4134                      $all_other_block_style_handles[] = $style_handle;
4135                  }
4136              }
4137          }
4138  
4139          /*
4140           * First print all styles related to core blocks which should be inserted right after the wp-block-library stylesheet
4141           * to preserve the CSS cascade. The logic in this `if` statement is derived from `wp_print_styles()`.
4142           */
4143          $enqueued_core_block_styles = array_values( array_intersect( $all_core_block_style_handles, wp_styles()->queue ) );
4144          if ( count( $enqueued_core_block_styles ) > 0 ) {
4145              ob_start();
4146              wp_styles()->do_items( $enqueued_core_block_styles );
4147              $printed_core_block_styles = (string) ob_get_clean();
4148          }
4149  
4150          // Capture non-core block styles so they can get printed at the point where wp_enqueue_registered_block_scripts_and_styles() runs.
4151          $enqueued_other_block_styles = array_values( array_intersect( $all_other_block_style_handles, wp_styles()->queue ) );
4152          if ( count( $enqueued_other_block_styles ) > 0 ) {
4153              ob_start();
4154              wp_styles()->do_items( $enqueued_other_block_styles );
4155              $printed_other_block_styles = (string) ob_get_clean();
4156          }
4157  
4158          // Capture the global-styles so that it can be printed at the point where wp_enqueue_global_styles() runs.
4159          if ( wp_style_is( 'global-styles' ) ) {
4160              ob_start();
4161              wp_styles()->do_items( array( 'global-styles' ) );
4162              $printed_global_styles = (string) ob_get_clean();
4163          }
4164  
4165          /*
4166           * Print all remaining styles not related to blocks. This contains a subset of the logic from
4167           * `print_late_styles()`, without admin-specific logic and the `print_late_styles` filter to control whether
4168           * late styles are printed (since they are being hoisted anyway).
4169           */
4170          ob_start();
4171          wp_styles()->do_footer_items();
4172          $printed_late_styles = (string) ob_get_clean();
4173      };
4174  
4175      /*
4176       * If `_wp_footer_scripts()` was unhooked from the `wp_print_footer_scripts` action, or if `wp_print_footer_scripts()`
4177       * was unhooked from running at the `wp_footer` action, then only add a callback to `wp_footer` which will capture the
4178       * late-printed styles.
4179       *
4180       * Otherwise, in the normal case where `_wp_footer_scripts()` will run at the `wp_print_footer_scripts` action, then
4181       * swap out `_wp_footer_scripts()` with an alternative which captures the printed styles (for hoisting to HEAD) before
4182       * proceeding with printing the footer scripts.
4183       */
4184      $wp_print_footer_scripts_priority = has_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
4185      if ( false === $wp_print_footer_scripts_priority || false === has_action( 'wp_footer', 'wp_print_footer_scripts' ) ) {
4186          // The normal priority for wp_print_footer_scripts() is to run at 20.
4187          add_action( 'wp_footer', $capture_late_styles, 20 );
4188      } else {
4189          remove_action( 'wp_print_footer_scripts', '_wp_footer_scripts', $wp_print_footer_scripts_priority );
4190          add_action(
4191              'wp_print_footer_scripts',
4192              static function () use ( $capture_late_styles ) {
4193                  $capture_late_styles();
4194                  print_footer_scripts();
4195              },
4196              $wp_print_footer_scripts_priority
4197          );
4198      }
4199  
4200      // Replace placeholder with the captured late styles.
4201      add_filter(
4202          'wp_template_enhancement_output_buffer',
4203          static function ( $buffer ) use ( $placeholder, &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) {
4204  
4205              // Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans.
4206              $processor = new class( $buffer ) extends WP_HTML_Tag_Processor {
4207                  /**
4208                   * Gets the span for the current token.
4209                   *
4210                   * @return WP_HTML_Span Current token span.
4211                   */
4212  				private function get_span(): WP_HTML_Span {
4213                      // Note: This call will never fail according to the usage of this class, given it is always called after ::next_tag() is true.
4214                      $this->set_bookmark( 'here' );
4215                      return $this->bookmarks['here'];
4216                  }
4217  
4218                  /**
4219                   * Inserts text before the current token.
4220                   *
4221                   * @param string $text Text to insert.
4222                   */
4223  				public function insert_before( string $text ): void {
4224                      $this->lexical_updates[] = new WP_HTML_Text_Replacement( $this->get_span()->start, 0, $text );
4225                  }
4226  
4227                  /**
4228                   * Inserts text after the current token.
4229                   *
4230                   * @param string $text Text to insert.
4231                   */
4232  				public function insert_after( string $text ): void {
4233                      $span = $this->get_span();
4234  
4235                      $this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start + $span->length, 0, $text );
4236                  }
4237  
4238                  /**
4239                   * Removes the current token.
4240                   */
4241  				public function remove(): void {
4242                      $span = $this->get_span();
4243  
4244                      $this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start, $span->length, '' );
4245                  }
4246  
4247                  /**
4248                   * Replaces the current token.
4249                   *
4250                   * @param string $text Text to replace with.
4251                   */
4252  				public function replace( string $text ): void {
4253                      $span = $this->get_span();
4254  
4255                      $this->lexical_updates[] = new WP_HTML_Text_Replacement( $span->start, $span->length, $text );
4256                  }
4257              };
4258  
4259              // Locate the insertion points in the HEAD.
4260              while ( $processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
4261                  if (
4262                      'STYLE' === $processor->get_tag() &&
4263                      'wp-global-styles-placeholder-inline-css' === $processor->get_attribute( 'id' )
4264                  ) {
4265                      /** This is added in {@see wp_enqueue_global_styles()} */
4266                      $processor->set_bookmark( 'wp_global_styles_placeholder' );
4267                  } elseif (
4268                      'STYLE' === $processor->get_tag() &&
4269                      'wp-block-styles-placeholder-inline-css' === $processor->get_attribute( 'id' )
4270                  ) {
4271                      /** This is added in {@see wp_enqueue_registered_block_scripts_and_styles()} */
4272                      $processor->set_bookmark( 'wp_block_styles_placeholder' );
4273                  } elseif (
4274                      'STYLE' === $processor->get_tag() &&
4275                      'wp-block-library-inline-css' === $processor->get_attribute( 'id' )
4276                  ) {
4277                      /** This is added here in {@see wp_hoist_late_printed_styles()} */
4278                      $processor->set_bookmark( 'wp_block_library' );
4279                  } elseif ( 'HEAD' === $processor->get_tag() && $processor->is_tag_closer() ) {
4280                      $processor->set_bookmark( 'head_end' );
4281                      break;
4282                  }
4283              }
4284  
4285              /**
4286               * Replace the placeholder for global styles enqueued during {@see wp_enqueue_global_styles()}. This is done
4287               * even if $printed_global_styles is empty.
4288               */
4289              if ( $processor->has_bookmark( 'wp_global_styles_placeholder' ) ) {
4290                  $processor->seek( 'wp_global_styles_placeholder' );
4291                  $processor->replace( $printed_global_styles );
4292                  $printed_global_styles = '';
4293              }
4294  
4295              /*
4296               * Insert block styles right after wp-block-library (if it is present). The placeholder CSS comment will
4297               * always be added to the wp-block-library inline style since it gets printed at `wp_head` before the blocks
4298               * are rendered. This means that there may not actually be any block styles to hoist from the footer to
4299               * insert after this inline style. The placeholder CSS comment needs to be added so that the inline style
4300               * gets printed, but if the resulting inline style is empty after the placeholder is removed, then the
4301               * inline style is removed.
4302               */
4303              if ( $processor->has_bookmark( 'wp_block_library' ) ) {
4304                  $processor->seek( 'wp_block_library' );
4305  
4306                  $css_text = $processor->get_modifiable_text();
4307  
4308                  /*
4309                   * Split the block library inline style by the placeholder to identify the original inlined CSS, which
4310                   * likely would be common.css, followed by any inline styles which had been added by the theme or
4311                   * plugins via `wp_add_inline_style( 'wp-block-library', '...' )`. The separate block styles loaded on
4312                   * demand will get inserted after the inlined common.css and before the extra inline styles added by the
4313                   * user.
4314                   */
4315                  $css_text_around_placeholder = explode( $placeholder, $css_text, 2 );
4316                  $extra_inline_styles         = '';
4317                  if ( count( $css_text_around_placeholder ) === 2 ) {
4318                      $css_text = $css_text_around_placeholder[0];
4319                      if ( '' !== trim( $css_text ) ) {
4320                          $inlined_src = wp_styles()->get_data( 'wp-block-library', 'inlined_src' );
4321                          if ( $inlined_src ) {
4322                              $css_text .= sprintf(
4323                                  "\n/*# sourceURL=%s */\n",
4324                                  esc_url_raw( $inlined_src )
4325                              );
4326                          }
4327                      }
4328                      $extra_inline_styles = $css_text_around_placeholder[1];
4329                  }
4330  
4331                  /*
4332                   * The placeholder CSS comment was added to the inline style in order to force an inline STYLE tag to
4333                   * be printed. Now that the inline style has been located and the placeholder comment has been removed, if
4334                   * there is no CSS left in the STYLE tag after removal, then remove the STYLE tag entirely.
4335                   */
4336                  if ( '' === trim( $css_text ) ) {
4337                      $processor->remove();
4338                  } else {
4339                      $processor->set_modifiable_text( $css_text );
4340                  }
4341  
4342                  $inserted_after            = $printed_core_block_styles;
4343                  $printed_core_block_styles = '';
4344  
4345                  /*
4346                   * Add a new inline style for any user styles added via wp_add_inline_style( 'wp-block-library', '...' ).
4347                   * This must be added here after $printed_core_block_styles to preserve the original CSS cascade when
4348                   * the combined block library stylesheet was used. The pattern here is checking to see if it is not just
4349                   * a sourceURL comment after the placeholder above is removed.
4350                   */
4351                  if ( ! preg_match( ':^\s*(/\*# sourceURL=\S+? \*/\s*)?$:s', $extra_inline_styles ) ) {
4352                      $style_processor = new WP_HTML_Tag_Processor( '<style></style>' );
4353                      $style_processor->next_tag();
4354                      $style_processor->set_attribute( 'id', 'wp-block-library-inline-css-extra' );
4355                      $style_processor->set_modifiable_text( $extra_inline_styles );
4356                      $inserted_after .= "{$style_processor->get_updated_html()}\n";
4357                  }
4358  
4359                  if ( '' !== $inserted_after ) {
4360                      $processor->insert_after( "\n" . $inserted_after );
4361                  }
4362              }
4363  
4364              // Insert block styles at the point where wp_enqueue_registered_block_scripts_and_styles() normally enqueues styles.
4365              if ( $processor->has_bookmark( 'wp_block_styles_placeholder' ) ) {
4366                  $processor->seek( 'wp_block_styles_placeholder' );
4367                  if ( '' !== $printed_other_block_styles ) {
4368                      $processor->replace( "\n" . $printed_other_block_styles );
4369                  } else {
4370                      $processor->remove();
4371                  }
4372                  $printed_other_block_styles = '';
4373              }
4374  
4375              // Print all remaining styles.
4376              $remaining_styles = $printed_core_block_styles . $printed_other_block_styles . $printed_global_styles . $printed_late_styles;
4377              if ( $remaining_styles && $processor->has_bookmark( 'head_end' ) ) {
4378                  $processor->seek( 'head_end' );
4379                  $processor->insert_before( $remaining_styles . "\n" );
4380              }
4381              return $processor->get_updated_html();
4382          }
4383      );
4384  }
4385  
4386  /**
4387   * Return the corresponding JavaScript `dataset` name for an attribute
4388   * if it represents a custom data attribute, or `null` if not.
4389   *
4390   * Custom data attributes appear in an element's `dataset` property in a
4391   * browser, but there's a specific way the names are translated from HTML
4392   * into JavaScript. This function indicates how the name would appear in
4393   * JavaScript if a browser would recognize it as a custom data attribute.
4394   *
4395   * Example:
4396   *
4397   *     // Dash-letter pairs turn into capital letters.
4398   *     'postId'       === wp_js_dataset_name( 'data-post-id' );
4399   *     'Before'       === wp_js_dataset_name( 'data--before' );
4400   *     '-One--Two---' === wp_js_dataset_name( 'data---one---two---' );
4401   *
4402   *     // Not every attribute name will be interpreted as a custom data attribute.
4403   *     null === wp_js_dataset_name( 'post-id' );
4404   *     null === wp_js_dataset_name( 'data' );
4405   *
4406   *     // Some very surprising names will; for example, a property whose name is the empty string.
4407   *     '' === wp_js_dataset_name( 'data-' );
4408   *     0  === strlen( wp_js_dataset_name( 'data-' ) );
4409   *
4410   * @since 6.9.0
4411   *
4412   * @see \wp_html_custom_data_attribute_name()
4413   * @link https://html.spec.whatwg.org/#concept-domstringmap-pairs
4414   *
4415   * @param string $html_attribute_name Raw attribute name as found in the source HTML.
4416   * @return string|null Transformed `dataset` name, if interpretable as a custom data attribute, else `null`.
4417   */
4418  function wp_js_dataset_name( string $html_attribute_name ): ?string {
4419      if ( 0 !== substr_compare( $html_attribute_name, 'data-', 0, 5, true ) ) {
4420          return null;
4421      }
4422  
4423      $end = strlen( $html_attribute_name );
4424  
4425      /*
4426       * If it contains characters which would end the attribute name parsing then
4427       * something else is wrong and this contains more than just an attribute name.
4428       */
4429      if ( ( $end - 5 ) !== strcspn( $html_attribute_name, "=/> \t\f\r\n", 5 ) ) {
4430          return null;
4431      }
4432  
4433      /**
4434       * > For each name in list, for each U+002D HYPHEN-MINUS character (-)
4435       * > in the name that is followed by an ASCII lower alpha, remove the
4436       * > U+002D HYPHEN-MINUS character (-) and replace the character that
4437       * > followed it by the same character converted to ASCII uppercase.
4438       *
4439       * @link https://html.spec.whatwg.org/#concept-domstringmap-pairs
4440       */
4441      $custom_name = '';
4442      $at          = 5;
4443      $was_at      = $at;
4444  
4445      while ( $at < $end ) {
4446          $next_dash_at = strpos( $html_attribute_name, '-', $at );
4447          if ( false === $next_dash_at || $next_dash_at === $end - 1 ) {
4448              break;
4449          }
4450  
4451          // Transform `-a` to `A`, for example.
4452          $c = $html_attribute_name[ $next_dash_at + 1 ];
4453          if ( ( $c >= 'A' && $c <= 'Z' ) || ( $c >= 'a' && $c <= 'z' ) ) {
4454              $prefix       = substr( $html_attribute_name, $was_at, $next_dash_at - $was_at );
4455              $custom_name .= strtolower( $prefix );
4456              $custom_name .= strtoupper( $c );
4457              $at           = $next_dash_at + 2;
4458              $was_at       = $at;
4459              continue;
4460          }
4461  
4462          $at = $next_dash_at + 1;
4463      }
4464  
4465      // If nothing has been added it means there are no dash-letter pairs; return the name as-is.
4466      return '' === $custom_name
4467          ? strtolower( substr( $html_attribute_name, 5 ) )
4468          : ( $custom_name . strtolower( substr( $html_attribute_name, $was_at ) ) );
4469  }
4470  
4471  /**
4472   * Returns a corresponding HTML attribute name for the given name,
4473   * if that name were found in a JS element’s `dataset` property.
4474   *
4475   * Example:
4476   *
4477   *     'data-post-id'        === wp_html_custom_data_attribute_name( 'postId' );
4478   *     'data--before'        === wp_html_custom_data_attribute_name( 'Before' );
4479   *     'data---one---two---' === wp_html_custom_data_attribute_name( '-One--Two---' );
4480   *
4481   *     // Not every attribute name will be interpreted as a custom data attribute.
4482   *     null === wp_html_custom_data_attribute_name( '/not-an-attribute/' );
4483   *     null === wp_html_custom_data_attribute_name( 'no spaces' );
4484   *
4485   *     // Some very surprising names will; for example, a property whose name is the empty string.
4486   *     'data-' === wp_html_custom_data_attribute_name( '' );
4487   *
4488   * @since 6.9.0
4489   *
4490   * @see \wp_js_dataset_name()
4491   * @link https://html.spec.whatwg.org/#concept-domstringmap-pairs
4492   *
4493   * @param string $js_dataset_name Name of JS `dataset` property to transform.
4494   * @return string|null Corresponding name of an HTML custom data attribute for the given dataset name,
4495   *                     if possible to represent in HTML, otherwise `null`.
4496   */
4497  function wp_html_custom_data_attribute_name( string $js_dataset_name ): ?string {
4498      $end = strlen( $js_dataset_name );
4499      if ( 0 === $end ) {
4500          return 'data-';
4501      }
4502  
4503      /*
4504       * If it contains characters which would end the attribute name parsing then
4505       * something it’s not possible to represent this in HTML.
4506       */
4507      if ( strcspn( $js_dataset_name, "=/> \t\f\r\n" ) !== $end ) {
4508          return null;
4509      }
4510  
4511      $html_name = 'data-';
4512      $at        = 0;
4513      $was_at    = $at;
4514  
4515      while ( $at < $end ) {
4516          $next_upper_after = strcspn( $js_dataset_name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', $at );
4517          $next_upper_at    = $at + $next_upper_after;
4518          if ( $next_upper_at >= $end ) {
4519              break;
4520          }
4521  
4522          $prefix     = substr( $js_dataset_name, $was_at, $next_upper_at - $was_at );
4523          $html_name .= strtolower( $prefix );
4524          $html_name .= '-' . strtolower( $js_dataset_name[ $next_upper_at ] );
4525          $at         = $next_upper_at + 1;
4526          $was_at     = $at;
4527      }
4528  
4529      if ( $was_at < $end ) {
4530          $html_name .= strtolower( substr( $js_dataset_name, $was_at ) );
4531      }
4532  
4533      return $html_name;
4534  }


Generated : Tue Dec 1 08:20:01 2026 Cross-referenced by PHPXref