Changeset 64120
- Timestamp:
- 10/06/2026 07:26:22 AM (5 weeks ago)
- Location:
- trunk
- Files:
-
- 7 added
- 15 edited
-
src/wp-admin/admin-header.php (modified) (1 diff)
-
src/wp-admin/admin.php (modified) (1 diff)
-
src/wp-admin/edit-form-blocks.php (modified) (1 diff)
-
src/wp-admin/includes/admin-filters.php (modified) (1 diff)
-
src/wp-includes/class-wp-admin-bar.php (modified) (1 diff)
-
src/wp-includes/class-wp-dependencies.php (modified) (1 diff)
-
src/wp-includes/class-wp-editor.php (modified) (1 diff)
-
src/wp-includes/class-wp-scripts.php (modified) (2 diffs)
-
src/wp-includes/class-wp-styles.php (modified) (4 diffs)
-
src/wp-includes/default-filters.php (modified) (1 diff)
-
src/wp-includes/functions.php (modified) (1 diff)
-
src/wp-includes/media.php (modified) (2 diffs)
-
src/wp-includes/script-loader.php (modified) (5 diffs)
-
tests/phpunit/tests/dependencies/scripts.php (modified) (3 diffs)
-
tests/phpunit/tests/dependencies/styles.php (modified) (1 diff)
-
tests/phpunit/tests/dependencies/wpPrefetchAdminAssets.php (added)
-
tests/phpunit/tests/dependencies/wpScripts (added)
-
tests/phpunit/tests/dependencies/wpScripts/getSrc.php (added)
-
tests/phpunit/tests/dependencies/wpShouldConcatenateAdminScripts.php (added)
-
tests/phpunit/tests/dependencies/wpStyles (added)
-
tests/phpunit/tests/dependencies/wpStyles/getRtlSrc.php (added)
-
tests/phpunit/tests/dependencies/wpStyles/getSrc.php (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/admin-header.php
r63626 r64120 96 96 <?php 97 97 98 /* 99 * The dependencies of `colors` and the `utils` script are prefetched from the login screen by 100 * wp_prefetch_admin_assets(), which needs updating if these change. 101 */ 98 102 wp_enqueue_style( 'colors' ); 99 103 wp_enqueue_script( 'utils' ); -
trunk/src/wp-admin/admin.php
r61994 r64120 119 119 $time_format = __( 'g:i a' ); 120 120 121 /* 122 * The `jquery` dependency of this script is prefetched from the login screen by 123 * wp_prefetch_admin_assets(), which needs updating if this changes. 124 */ 121 125 wp_enqueue_script( 'common' ); 122 126 -
trunk/src/wp-admin/edit-form-blocks.php
r62846 r64120 343 343 * Styles 344 344 */ 345 // Prefetched for the block editor by wp_prefetch_admin_assets(), which needs updating if this changes. 345 346 wp_enqueue_style( 'wp-edit-post' ); 346 347 -
trunk/src/wp-admin/includes/admin-filters.php
r61866 r64120 49 49 add_action( 'admin_head', 'wp_site_icon' ); 50 50 add_action( 'admin_head', 'wp_admin_viewport_meta' ); 51 add_action( 'admin_head', 'wp_prefetch_admin_assets' ); 51 52 add_action( 'customize_controls_head', 'wp_admin_viewport_meta' ); 52 53 add_filter( 'nav_menu_meta_box_object', '_wp_nav_menu_meta_box_object' ); -
trunk/src/wp-includes/class-wp-admin-bar.php
r63492 r64120 71 71 72 72 wp_enqueue_script( 'admin-bar' ); 73 // Prefetched from the login screen by wp_prefetch_admin_assets(), which needs updating if this changes. 73 74 wp_enqueue_style( 'admin-bar' ); 74 75 -
trunk/src/wp-includes/class-wp-dependencies.php
r63941 r64120 317 317 if ( 'conditional' === $key && '_required-conditional-dependency_' !== $value ) { 318 318 _deprecated_argument( 319 'WP_Dependencies ->add_data()',319 'WP_Dependencies::add_data()', 320 320 '6.9.0', 321 321 __( 'IE conditional comments are ignored by all supported browsers.' ) -
trunk/src/wp-includes/class-wp-editor.php
r62696 r64120 914 914 self::enqueue_scripts( true ); 915 915 916 // Also add wp-includes/css/editor.css. 916 /* 917 * Also add wp-includes/css/editor.css. It is prefetched for the block editor by 918 * wp_prefetch_admin_assets(), which needs updating if this changes. 919 */ 917 920 wp_enqueue_style( 'editor-buttons' ); 918 921 -
trunk/src/wp-includes/class-wp-scripts.php
r63579 r64120 416 416 } 417 417 418 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) { 419 $src = $this->base_url . $src; 420 } 421 422 $ver_to_add = ''; 423 if ( empty( $obj->ver ) && null !== $obj->ver && is_string( $this->default_version ) ) { 424 $ver_to_add = $this->default_version; 425 } elseif ( is_scalar( $obj->ver ) ) { 426 $ver_to_add = (string) $obj->ver; 427 } 428 429 $added_args = (string) ( $this->args[ $handle ] ?? '' ); 430 431 if ( '' !== $ver_to_add || '' !== $added_args ) { 432 $fragment = strstr( $src, '#' ); 433 if ( false !== $fragment ) { 434 $src = substr( $src, 0, -strlen( $fragment ) ); 435 } 436 437 if ( '' !== $ver_to_add ) { 438 $src .= ( str_contains( $src, '?' ) ? '&' : '?' ) . 'ver=' . rawurlencode( $ver_to_add ); 439 } 440 if ( '' !== $added_args ) { 441 $src .= ( str_contains( $src, '?' ) ? '&' : '?' ) . $added_args; 442 } 443 444 if ( false !== $fragment ) { 445 $src .= $fragment; 446 } 447 } 448 449 /** This filter is documented in wp-includes/class-wp-scripts.php */ 450 $src = esc_url_raw( apply_filters( 'script_loader_src', $src, $handle ) ); 418 $src = esc_url_raw( $this->get_src( $handle ) ); 451 419 452 420 if ( ! $src ) { … … 505 473 506 474 return true; 475 } 476 477 /** 478 * Gets the URL a registered script is loaded from. 479 * 480 * This is the URL printed in the script's `src` attribute, including the version query 481 * argument and any arguments added to the handle, after the {@see 'script_loader_src'} filter. 482 * Like {@see WP_Script_Modules::get_src()}, it is neither sanitized nor escaped, so a caller 483 * can pass it through esc_url_raw(), as WP_Scripts::do_item() does, or esc_url(), once. 484 * 485 * @since 7.2.0 486 * 487 * @param string $handle Script handle. 488 * @return string Script URL, or an empty string when the script is not registered, has no 489 * source of its own because it only aliases other scripts, or was filtered away. 490 */ 491 public function get_src( string $handle ): string { 492 if ( ! isset( $this->registered[ $handle ] ) ) { 493 return ''; 494 } 495 496 $obj = $this->registered[ $handle ]; 497 $src = $obj->src; 498 499 if ( ! $src || ! is_string( $src ) ) { 500 return ''; 501 } 502 503 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) { 504 $src = $this->base_url . $src; 505 } 506 507 $ver_to_add = ''; 508 if ( empty( $obj->ver ) && null !== $obj->ver && is_string( $this->default_version ) ) { 509 $ver_to_add = $this->default_version; 510 } elseif ( is_scalar( $obj->ver ) ) { 511 $ver_to_add = (string) $obj->ver; 512 } 513 514 $added_args = (string) ( $this->args[ $handle ] ?? '' ); 515 516 if ( '' !== $ver_to_add || '' !== $added_args ) { 517 $fragment = strstr( $src, '#' ); 518 if ( false !== $fragment ) { 519 $src = substr( $src, 0, -strlen( $fragment ) ); 520 } 521 522 if ( '' !== $ver_to_add ) { 523 $src .= ( str_contains( $src, '?' ) ? '&' : '?' ) . 'ver=' . rawurlencode( $ver_to_add ); 524 } 525 if ( '' !== $added_args ) { 526 $src .= ( str_contains( $src, '?' ) ? '&' : '?' ) . $added_args; 527 } 528 529 if ( false !== $fragment ) { 530 $src .= $fragment; 531 } 532 } 533 534 /** This filter is documented in wp-includes/class-wp-scripts.php */ 535 $src = apply_filters( 'script_loader_src', $src, $handle ); 536 537 return is_string( $src ) ? $src : ''; 507 538 } 508 539 -
trunk/src/wp-includes/class-wp-styles.php
r63440 r64120 225 225 $tag = apply_filters( 'style_loader_tag', $tag, $handle, $href, $media ); 226 226 227 if ( 'rtl' === $this->text_direction && isset( $obj->extra['rtl'] ) && $obj->extra['rtl'] ) { 228 if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) { 229 $suffix = $obj->extra['suffix'] ?? ''; 230 $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $src, $ver, "$handle-rtl" ) ); 231 } else { 232 $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" ); 233 } 234 235 $rtl_tag = sprintf( 227 $rtl_src = $this->get_rtl_src( $handle ); 228 229 if ( null !== $rtl_src ) { 230 $rtl_href = esc_url( $rtl_src ); 231 $rtl_tag = sprintf( 236 232 "<link rel='%s' id='%s-rtl-css'%s href='%s' media='%s' />\n", 237 233 $rel, … … 263 259 264 260 return true; 261 } 262 263 /** 264 * Gets the URL of the right-to-left stylesheet for a registered style. 265 * 266 * On a right-to-left locale, a style registered with `rtl` data is served by a separate 267 * stylesheet, which either replaces its left-to-right one (when the data is `'replace'`) 268 * or loads alongside it. 269 * 270 * Like {@see WP_Styles::get_src()}, the URL is neither sanitized nor escaped, so a caller can 271 * pass it through esc_url() to print it, or esc_url_raw() otherwise, once. 272 * 273 * @since 7.2.0 274 * 275 * @param string $handle The style's registered handle. 276 * @return string|null URL of the right-to-left stylesheet, after the {@see 'style_loader_src'} 277 * filter. Null when the text direction is not right-to-left, or the style is 278 * not registered, has no source of its own, or has no right-to-left variant. 279 */ 280 public function get_rtl_src( string $handle ): ?string { 281 if ( 'rtl' !== $this->text_direction || ! isset( $this->registered[ $handle ] ) ) { 282 return null; 283 } 284 285 $obj = $this->registered[ $handle ]; 286 287 if ( ! $obj->src || ! isset( $obj->extra['rtl'] ) || ! $obj->extra['rtl'] ) { 288 return null; 289 } 290 291 /* 292 * The version and the handle's added arguments are those of the left-to-right stylesheet, 293 * appended the same way, while the filter is passed the handle with `-rtl` appended, as it 294 * always has been. 295 */ 296 if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) { 297 $suffix = isset( $obj->extra['suffix'] ) && is_string( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : ''; 298 299 return str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->build_src( $obj->src, $obj->ver, $handle, "$handle-rtl" ) ); 300 } 301 302 // Any other value is the URL of the right-to-left stylesheet itself. 303 if ( ! is_string( $obj->extra['rtl'] ) ) { 304 return null; 305 } 306 307 return $this->build_src( $obj->extra['rtl'], $obj->ver, $handle, "$handle-rtl" ); 265 308 } 266 309 … … 402 445 * @param string|false|null $ver The version of the enqueued style. 403 446 * @param string $handle The style's registered handle. 404 * @return string Style's fully-qualified URL .447 * @return string Style's fully-qualified URL, escaped for use in an HTML attribute. 405 448 */ 406 449 public function _css_href( $src, $ver, $handle ) { 450 return esc_url( $this->build_src( $src, $ver, (string) $handle ) ); 451 } 452 453 /** 454 * Gets the URL a registered style is loaded from. 455 * 456 * This is the URL printed in the stylesheet's `href` attribute, including the version query 457 * argument and any arguments added to the handle, after the {@see 'style_loader_src'} filter. 458 * Unlike {@see WP_Styles::_css_href()}, it is neither sanitized nor escaped, so a caller can 459 * pass it through esc_url() to print it, or esc_url_raw() otherwise, once. This is the same as 460 * {@see WP_Script_Modules::get_src()}. 461 * 462 * @since 7.2.0 463 * 464 * @param string $handle The style's registered handle. 465 * @return string Style URL, or an empty string when the style is not registered, has no 466 * source of its own because it only aliases other styles, or was filtered away. 467 */ 468 public function get_src( string $handle ): string { 469 if ( ! isset( $this->registered[ $handle ] ) ) { 470 return ''; 471 } 472 473 $obj = $this->registered[ $handle ]; 474 475 /* 476 * An alias, with no source of its own, has no URL. A source of `true`, like that of `colors`, 477 * gets past this, since its URL comes from the 'style_loader_src' filter in build_src(), as 478 * it does when WP_Styles::do_item() prints the stylesheet. 479 */ 480 if ( ! $obj->src ) { 481 return ''; 482 } 483 484 return $this->build_src( $obj->src, $obj->ver, $handle ); 485 } 486 487 /** 488 * Builds a style's fully-qualified URL and passes it through the 'style_loader_src' filter. 489 * 490 * The result is not escaped, so callers can escape it for where it is used. 491 * 492 * @since 7.2.0 493 * 494 * @param string|true $src The source of the style, or true for one whose URL comes 495 * from the {@see 'style_loader_src'} filter. 496 * @param string|false|null $ver The version of the style. 497 * @param string $handle The style's registered handle, whose added arguments 498 * are appended. 499 * @param string|null $filter_handle Optional. The handle passed to the 500 * {@see 'style_loader_src'} filter, such as 501 * `{$handle}-rtl` for a right-to-left stylesheet. 502 * Default `$handle`. 503 * @return string The filtered URL, or an empty string when the filter returns anything other 504 * than a string. 505 */ 506 private function build_src( $src, $ver, string $handle, ?string $filter_handle = null ): string { 407 507 if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) { 408 508 $src = $this->base_url . $src; … … 444 544 * @param string $handle The style's registered handle. 445 545 */ 446 $src = apply_filters( 'style_loader_src', $src, $handle ); 447 return esc_url( $src ); 546 $src = apply_filters( 'style_loader_src', $src, $filter_handle ?? $handle ); 547 548 return is_string( $src ) ? $src : ''; 448 549 } 449 550 -
trunk/src/wp-includes/default-filters.php
r64118 r64120 399 399 add_action( 'login_head', 'wp_site_icon', 99 ); 400 400 add_action( 'login_footer', 'wp_print_footer_scripts', 20 ); 401 add_action( 'login_footer', 'wp_prefetch_admin_assets', 21 ); // By the footer, the login screen has enqueued everything it loads, so those are skipped. 401 402 add_action( 'login_init', 'send_frame_options_header', 10, 0 ); 402 403 add_action( 'login_init', 'wp_admin_headers' ); -
trunk/src/wp-includes/functions.php
r63941 r64120 7665 7665 */ 7666 7666 if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) { 7667 // Prefetched from the login screen by wp_prefetch_admin_assets(), which needs updating if this changes. 7667 7668 wp_enqueue_style( 'wp-auth-check' ); 7668 7669 wp_enqueue_script( 'wp-auth-check' ); -
trunk/src/wp-includes/media.php
r63596 r64120 5306 5306 5307 5307 wp_enqueue_script( 'media-audiovideo' ); 5308 // Prefetched for the block editor by wp_prefetch_admin_assets(), which needs updating if this changes. 5308 5309 wp_enqueue_style( 'media-views' ); 5309 5310 if ( is_admin() ) { … … 5311 5312 wp_enqueue_script( 'image-edit' ); 5312 5313 } 5314 // Prefetched for the block editor by wp_prefetch_admin_assets(), which needs updating if this changes. 5313 5315 wp_enqueue_style( 'imgareaselect' ); 5314 5316 wp_plupload_default_settings(); -
trunk/src/wp-includes/script-loader.php
r63983 r64120 2478 2478 2479 2479 if ( ! isset( $concatenate_scripts ) ) { 2480 $concatenate_scripts = defined( 'CONCATENATE_SCRIPTS' ) ? CONCATENATE_SCRIPTS : true; 2481 if ( ( ! is_admin() && ! did_action( 'login_init' ) ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) { 2482 $concatenate_scripts = false; 2483 } 2480 $concatenate_scripts = ( is_admin() || did_action( 'login_init' ) ) && wp_should_concatenate_admin_scripts(); 2484 2481 } 2485 2482 … … 2497 2494 } 2498 2495 } 2496 } 2497 2498 /** 2499 * Determines whether scripts and styles are concatenated on admin screens and the login screen. 2500 * 2501 * Concatenation is on unless the `CONCATENATE_SCRIPTS` constant turns it off, and `SCRIPT_DEBUG` 2502 * turns it off regardless. Scripts and styles are never concatenated elsewhere. 2503 * 2504 * This is the default that script_concat_settings() gives the `$concatenate_scripts` global when 2505 * the global has not already been set. It is also how wp_prefetch_admin_assets() predicts, from the 2506 * login screen, what the admin screen the login leads to will do. 2507 * 2508 * This function and its filter are intended to be removed before 7.2-beta1. They exist only while 2509 * concatenation is still an option, and once concatenation is retired, there is nothing left for 2510 * them to decide. Do not rely on them; setting the `$concatenate_scripts` global remains the way 2511 * to override concatenation on a request. 2512 * 2513 * @since 7.2.0 2514 * 2515 * @return bool Whether scripts and styles are concatenated on admin screens and the login screen. 2516 */ 2517 function wp_should_concatenate_admin_scripts(): bool { 2518 $concatenate = ( defined( 'CONCATENATE_SCRIPTS' ) ? (bool) CONCATENATE_SCRIPTS : true ) 2519 && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ); 2520 2521 /** 2522 * Filters whether scripts and styles are concatenated on admin screens and the login screen. 2523 * 2524 * Setting the `$concatenate_scripts` global directly still takes precedence over this filter on 2525 * the request where it is set. 2526 * 2527 * An admin screen settles the global the first time scripts are registered, when the 2528 * {@see 'wp_default_scripts'} action registers TinyMCE. That can be as early as while plugins 2529 * load, before the theme's functions.php, whereas the login screen reads this filter only when 2530 * it prints, to predict what the admin will do. So add a callback when a plugin loads, rather 2531 * than from a theme or on a later hook, or the admin may not see it while the login screen does. 2532 * 2533 * @since 7.2.0 2534 * 2535 * @param bool $concatenate Whether scripts and styles are concatenated. Default true, unless the 2536 * `CONCATENATE_SCRIPTS` constant is false or `SCRIPT_DEBUG` is true. 2537 */ 2538 return (bool) apply_filters( 'wp_should_concatenate_admin_scripts', $concatenate ); 2539 } 2540 2541 /** 2542 * Prints prefetch links for the assets of the screen the user is most likely to open next. 2543 * 2544 * Runs wherever the next screen can be predicted with confidence, and prefetches only what that 2545 * screen is certain to need. Two cases qualify today: 2546 * 2547 * - The login screen, which is followed by an admin screen. With concatenation disabled that 2548 * screen downloads each core script and stylesheet separately, which is what makes an uncached 2549 * admin load slower than a concatenated one. Requesting the ones that block its first paint 2550 * while the login form is on screen puts them in the HTTP cache during the time the user spends 2551 * typing credentials, so the redirect that follows finds them already there. 2552 * - The Dashboard and the post list tables, from which the editor is the usual next stop. Only the 2553 * editor's stylesheets are prefetched, not its scripts: the scripts run to well over a megabyte 2554 * compressed, which is far too much to spend on a screen the user may never open, whereas the 2555 * stylesheets are render-blocking and in the same size class as the login screen's own prefetch. 2556 * 2557 * Handles the current screen has printed or queued are skipped, along with their dependencies, so 2558 * each context only fetches what it is actually adding. On the login screen this runs in the 2559 * footer, since some of what the login form loads, such as `user-profile` and the `jquery` it 2560 * depends on, is only enqueued after its header has printed. On admin screens it runs in the head, 2561 * where the screen's assets have been enqueued already. 2562 * 2563 * These are resources for the *next* navigation rather than for the screen printing them, which is 2564 * what `rel="prefetch"` describes. `rel="preload"` would fetch them at the current document's 2565 * priority, and browsers warn about preloaded resources the document never uses. A prefetch is 2566 * already dispatched at the browser's lowest priority, so it stays out of the way of that screen's 2567 * own render-blocking assets without needing `fetchpriority`. 2568 * 2569 * A prefetched response is reused only for as long as the HTTP cache considers it fresh, the same as 2570 * any other cached response. Core does not send caching headers for its static files, so how long 2571 * that is depends on the server: an explicit `max-age` or `Expires`, or else a heuristic lifetime 2572 * derived from `Last-Modified`. Once the response is stale the next screen still revalidates it, 2573 * which saves the download but not the round trip. 2574 * 2575 * The `as` attribute is still worth setting: it gives the request the same destination the admin 2576 * screen will later ask for, which is what lets the prefetched response be reused. 2577 * 2578 * The admin-wide handles cover every admin screen rather than only the Dashboard, so that part of 2579 * the list does not vary with where the login lands. They are printed on every screen of the login 2580 * page, such as the password reset form, since those mostly lead to the admin as well. Nothing is 2581 * printed at all when `redirect_to` points outside this site's admin. 2582 * 2583 * Nothing is printed when the next screen will concatenate its assets, since `load-scripts.php` 2584 * and `load-styles.php` already collapse these handles into a handful of requests. 2585 * 2586 * @since 7.2.0 2587 * 2588 * @see wp_preload_resources() 2589 * @see wp_should_concatenate_admin_scripts() 2590 * 2591 * @global bool $concatenate_scripts Whether scripts and styles are concatenated. 2592 * @global WP_Screen|null $current_screen The current admin screen, if any. 2593 */ 2594 function wp_prefetch_admin_assets(): void { 2595 global $concatenate_scripts, $current_screen; 2596 2597 /* 2598 * The context is told apart by the request rather than by the hook this runs on, so it works 2599 * from whichever hook it is added to. The 'login_init' action is fired by wp-login.php however 2600 * it is reached, which is also how script_concat_settings() recognizes it. The is_login() 2601 * function would not do: it compares the login URL with the script handling the request, and a 2602 * plugin serving the login screen at a URL of its own runs wp-login.php from another script, 2603 * such as index.php. 2604 */ 2605 $on_login = (bool) did_action( 'login_init' ); 2606 2607 if ( $on_login ) { 2608 /* 2609 * Deliberately not the $concatenate_scripts global: script_concat_settings() often runs on a 2610 * login request before 'login_init' fires — anything registering a script on 'init' is enough 2611 * to trigger it — and at that point it evaluates is_admin() as false and settles the global on 2612 * false whatever the constant says. What matters here is what the admin screen this login 2613 * leads to will do, which is what wp_should_concatenate_admin_scripts() predicts. 2614 */ 2615 if ( wp_should_concatenate_admin_scripts() ) { 2616 return; 2617 } 2618 } else { 2619 /* 2620 * From the Dashboard and the post list tables, the editor is the usual next stop. Anywhere 2621 * else in the admin there is no destination worth guessing at. 2622 * 2623 * The global is read rather than calling get_current_screen(), which only exists once the 2624 * admin includes are loaded. On a request with no screen, such as one for the front end, 2625 * this prints nothing. 2626 */ 2627 if ( ! $current_screen instanceof WP_Screen || ! in_array( $current_screen->base, array( 'dashboard', 'edit' ), true ) ) { 2628 return; 2629 } 2630 2631 /* 2632 * On an admin screen the global has settled by the time its head is printed, and the editor 2633 * will be served the same way. 2634 */ 2635 script_concat_settings(); 2636 2637 if ( $concatenate_scripts ) { 2638 return; 2639 } 2640 } 2641 2642 $script_roots = array(); 2643 $style_roots = array(); 2644 2645 if ( $on_login ) { 2646 /* 2647 * Resolve where the login is going to land, the same way wp-login.php will: `redirect_to` 2648 * when one was given, and the admin otherwise. wp_safe_redirect() sends the browser to what 2649 * wp_validate_redirect() returns, so that is used here as it is: it sanitizes the value, 2650 * resolves a relative path against the current request, as the browser would, and falls back 2651 * to the admin for a value pointing off-host. Passing the value through esc_url_raw() first 2652 * would not match it, since that takes a relative path such as `wp-admin/post.php` for a host. 2653 * 2654 * This runs on every screen wp-login.php prints, not only the login form, since the others 2655 * mostly lead to the admin as well, and each one gives the prefetching another chance to 2656 * finish before the user gets there. The password reset and registration flows end at the 2657 * login form, and the admin email confirmation follows a login that has already succeeded. 2658 * On the lost password and registration screens, `redirect_to` is where submitting the 2659 * form goes rather than where the login lands, but it is normally absent, and when it is 2660 * not, it typically points back into wp-login.php, so nothing is prefetched. Nor does an 2661 * interim login need to be excluded: it shows inside a modal on an admin screen that has 2662 * already loaded these assets, so they are served from the HTTP cache. 2663 * 2664 * An empty `redirect_to` counts as none, since wp-login.php falls back to the admin for it 2665 * too. The lost password form submits one, for instance, so it is present when the form is 2666 * shown again with an error. 2667 */ 2668 $admin_url = admin_url(); 2669 $next_screen = $admin_url; 2670 2671 if ( ! empty( $_REQUEST['redirect_to'] ) && is_string( $_REQUEST['redirect_to'] ) ) { 2672 $next_screen = wp_validate_redirect( wp_unslash( $_REQUEST['redirect_to'] ), $admin_url ); 2673 } 2674 2675 /* 2676 * When the login lands somewhere other than the admin, such as the front end or a plugin's 2677 * own screen, none of these assets are wanted. The admin's path ends in a slash, which a 2678 * `redirect_to` of the admin itself may leave off, as in `/wp-admin`. 2679 */ 2680 $admin_path = (string) wp_parse_url( $admin_url, PHP_URL_PATH ); 2681 2682 if ( '' === $admin_path || ! str_starts_with( trailingslashit( (string) wp_parse_url( $next_screen, PHP_URL_PATH ) ), $admin_path ) ) { 2683 return; 2684 } 2685 2686 /* 2687 * Nor are they when it lands in the admin of another host. wp_validate_redirect() accepts any 2688 * host in 'allowed_redirect_hosts', such as another site on a multisite network, and that 2689 * admin would request its assets from its own host rather than from this one. The same goes 2690 * for this host's admin under another scheme, such as an `https` destination from an `http` 2691 * login: the URLs prefetched here take the scheme of the current request, so that admin 2692 * would request different ones. A relative `redirect_to` stays on this host and scheme, while 2693 * wp_validate_redirect() gives a protocol-relative one the `http` scheme, which is where 2694 * wp_safe_redirect() then sends it. A port is compared with the scheme's default filled in 2695 * when none is given, since `https://example.com:443/` is the same admin as 2696 * `https://example.com/`. 2697 */ 2698 $next_screen_host = wp_parse_url( $next_screen, PHP_URL_HOST ); 2699 2700 if ( is_string( $next_screen_host ) ) { 2701 $default_ports = array( 2702 'http' => 80, 2703 'https' => 443, 2704 ); 2705 2706 $admin_scheme = strtolower( (string) wp_parse_url( $admin_url, PHP_URL_SCHEME ) ); 2707 $next_screen_scheme = strtolower( (string) wp_parse_url( $next_screen, PHP_URL_SCHEME ) ); 2708 $admin_port = wp_parse_url( $admin_url, PHP_URL_PORT ) ?? $default_ports[ $admin_scheme ] ?? null; 2709 $next_screen_port = wp_parse_url( $next_screen, PHP_URL_PORT ) ?? $default_ports[ $next_screen_scheme ] ?? null; 2710 2711 if ( 2712 strtolower( $next_screen_host ) !== strtolower( (string) wp_parse_url( $admin_url, PHP_URL_HOST ) ) || 2713 $next_screen_scheme !== $admin_scheme || 2714 $next_screen_port !== $admin_port 2715 ) { 2716 return; 2717 } 2718 } 2719 } else { 2720 $post_type = ( 'edit' === $current_screen->base && $current_screen->post_type ) ? $current_screen->post_type : 'post'; 2721 $post_type_object = get_post_type_object( $post_type ); 2722 2723 if ( ! $post_type_object instanceof WP_Post_Type ) { 2724 return; 2725 } 2726 2727 /* 2728 * A user who cannot edit posts of this type will never reach the editor from here. This is 2729 * `edit_posts` rather than `create_posts`, since the editor is reached by opening an existing 2730 * post as well as by adding a new one, and a user may be able to do the first but not the 2731 * second. Adding a new one requires `edit_posts` too. 2732 */ 2733 if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) { 2734 return; 2735 } 2736 2737 $next_screen = add_query_arg( 'post_type', $post_type, admin_url( 'post-new.php' ) ); 2738 } 2739 2740 if ( $on_login ) { 2741 /* 2742 * The handles that block rendering on every admin screen: the stylesheets, and the scripts 2743 * printed in the head. These are what stand between the redirect and the first paint, so 2744 * they are what is worth having in the cache already. Every handle these expand to loads on 2745 * all admin screens, not just the one the login happens to land on, so the list does not depend 2746 * on the destination. Screen-specific handles are deliberately left out: `site-health` 2747 * blocks rendering on the Dashboard but loads nowhere else. 2748 * 2749 * Scripts printed in the footer are left out even though they block DOMContentLoaded, since 2750 * they do not hold back the first paint. They are also where the bulk of the admin's bytes 2751 * are, largely the command palette's dependencies, and a prefetch of them still in flight 2752 * when the login form is submitted competes with the admin screen's own render-blocking 2753 * stylesheets and delays its first paint on a slow connection. 2754 * 2755 * These are roots rather than the full set: everything they depend on is pulled in with them 2756 * below, so the set follows the dependencies declared in wp_default_scripts() and 2757 * wp_default_styles(). `jquery` stands for `jquery-core` and `jquery-migrate`, and `wp-admin` 2758 * for the admin's own stylesheets, which is what the `colors` handle enqueued on every admin 2759 * screen depends on. Aliases like these have no source of their own, so only what they expand 2760 * to is prefetched. `colors` itself is left out, since the color scheme is a per-user setting 2761 * and the user is not known yet. 2762 * 2763 * Each root mirrors an enqueue elsewhere, which carries a note pointing back here: `common` 2764 * (for `jquery`) in wp-admin/admin.php, `colors` (for `wp-admin` and `buttons`) and `utils` in 2765 * wp-admin/admin-header.php, `admin-bar` in WP_Admin_Bar::initialize(), `wp-auth-check` in 2766 * wp_auth_check_load(), and `wp-commands` in wp_enqueue_command_palette_assets(). 2767 */ 2768 $script_roots = array( 2769 'jquery', 2770 'utils', 2771 ); 2772 2773 $style_roots = array( 2774 'wp-admin', 2775 'buttons', 2776 'admin-bar', 2777 'wp-auth-check', 2778 'wp-commands', 2779 ); 2780 } 2781 2782 /* 2783 * post-new.php always opens the editor, while post.php also handles trashing, restoring and 2784 * bulk edits, so it counts only when it is editing. 2785 */ 2786 $next_screen_file = basename( (string) wp_parse_url( $next_screen, PHP_URL_PATH ) ); 2787 $next_screen_query = array(); 2788 wp_parse_str( (string) wp_parse_url( $next_screen, PHP_URL_QUERY ), $next_screen_query ); 2789 2790 $next_screen_is_block_editor = 'post-new.php' === $next_screen_file 2791 || ( 'post.php' === $next_screen_file && 'edit' === ( $next_screen_query['action'] ?? '' ) ); 2792 2793 if ( $next_screen_is_block_editor ) { 2794 /* 2795 * The editor only loads these stylesheets when the post type uses the block editor, which 2796 * the classic editor, for instance, can turn off. The post type is the one in the query of 2797 * the next screen, as for post-new.php. An edit link to post.php from the login screen does 2798 * not carry one, so it is taken to be a post: looking the post up instead would let anyone 2799 * tell from the login screen whether a post with a given ID exists, drafts and private posts 2800 * included. 2801 */ 2802 $next_screen_post_type = $next_screen_query['post_type'] ?? 'post'; 2803 $next_screen_is_block_editor = is_string( $next_screen_post_type ) && use_block_editor_for_post_type( $next_screen_post_type ); 2804 } 2805 2806 if ( $next_screen_is_block_editor ) { 2807 /* 2808 * Roots as well, expanded along with any from the login screen. `wp-edit-post` alone accounts 2809 * for most of the editor chrome, including the block editor's content and reset styles by way 2810 * of `wp-edit-blocks`; the rest cover the block directory, the format library, the classic 2811 * editor's buttons and the media modal. 2812 * 2813 * Each root mirrors an enqueue elsewhere, which carries a note pointing back here: 2814 * `wp-edit-post` in wp-admin/edit-form-blocks.php, `wp-block-directory` in 2815 * wp_enqueue_editor_block_directory_assets(), `wp-format-library` in 2816 * wp_enqueue_editor_format_library_assets(), `editor-buttons` in 2817 * _WP_Editors::enqueue_default_editor(), and `media-views` and `imgareaselect` in 2818 * wp_enqueue_media(). 2819 */ 2820 $style_roots = array_merge( 2821 $style_roots, 2822 array( 2823 'wp-edit-post', 2824 'wp-block-directory', 2825 'wp-format-library', 2826 'editor-buttons', 2827 'media-views', 2828 'imgareaselect', 2829 ) 2830 ); 2831 } 2832 2833 // From an admin screen, nothing is left to prefetch when the editor turns out not to be the block editor. 2834 if ( ! $script_roots && ! $style_roots ) { 2835 return; 2836 } 2837 2838 $resources = array(); 2839 2840 foreach ( 2841 array( 2842 'script' => array( wp_scripts(), $script_roots ), 2843 'style' => array( wp_styles(), $style_roots ), 2844 ) 2845 as $as => list( $dependencies, $roots ) 2846 ) { 2847 // Skip a type with nothing to prefetch, such as scripts for the editor, rather than expanding the current screen's queue of it. 2848 if ( ! $roots ) { 2849 continue; 2850 } 2851 2852 /* 2853 * Expand the roots to include everything they depend on, roots first, and likewise what the 2854 * current screen has queued. A handle that is not registered is dropped along with its 2855 * dependencies. Unlike WP_Dependencies::all_deps(), this leaves the dependencies' state 2856 * untouched. 2857 */ 2858 $expanded = array( 2859 'roots' => array(), 2860 'queue' => array(), 2861 ); 2862 2863 foreach ( 2864 array( 2865 'roots' => $roots, 2866 'queue' => $dependencies->queue, 2867 ) 2868 as $list => $handles 2869 ) { 2870 while ( $handles ) { 2871 $handle = array_shift( $handles ); 2872 2873 if ( isset( $expanded[ $list ][ $handle ] ) || ! isset( $dependencies->registered[ $handle ] ) ) { 2874 continue; 2875 } 2876 2877 $expanded[ $list ][ $handle ] = true; 2878 2879 foreach ( $dependencies->registered[ $handle ]->deps as $dependency ) { 2880 if ( is_string( $dependency ) && '' !== $dependency && ! isset( $expanded[ $list ][ $dependency ] ) ) { 2881 $handles[] = $dependency; 2882 } 2883 } 2884 } 2885 } 2886 2887 /* 2888 * Whichever screen this is running on shares some of these handles, and the browser fetches 2889 * those for it anyway, so prefetching them as well would only compete with its own requests. 2890 * These are the ones it has printed, and the ones it has queued along with their 2891 * dependencies, which it has yet to print if this runs before its footer scripts. On the 2892 * login screen, for instance, `user-profile` brings in `jquery`. 2893 */ 2894 $on_current_screen = $expanded['queue'] + array_fill_keys( $dependencies->done, true ); 2895 2896 foreach ( array_keys( $expanded['roots'] ) as $handle ) { 2897 if ( isset( $on_current_screen[ $handle ] ) ) { 2898 continue; 2899 } 2900 2901 // The next screen prints nothing for a handle with conditional data, as do_item() returns early for it. 2902 if ( $dependencies->registered[ $handle ]->extra['conditional'] ?? false ) { 2903 continue; 2904 } 2905 2906 /* 2907 * The URLs are built the same way WP_Scripts::do_item() and WP_Styles::do_item() build 2908 * those in the tags they print, so they match what the next screen will request. They are 2909 * not escaped yet, so the filter sees plain URLs; they are escaped when printed. 2910 * 2911 * A handle with no URL of its own, such as an alias or one whose URL was filtered away, 2912 * prints nothing either, and for a style that includes its right-to-left stylesheet, 2913 * since WP_Styles::do_item() returns before getting to it. 2914 */ 2915 $src = $dependencies->get_src( $handle ); 2916 2917 if ( '' === $src ) { 2918 continue; 2919 } 2920 2921 $urls = array( $src ); 2922 2923 if ( $dependencies instanceof WP_Styles ) { 2924 $rtl_src = $dependencies->get_rtl_src( $handle ); 2925 2926 if ( null !== $rtl_src ) { 2927 if ( 'replace' === $dependencies->get_data( $handle, 'rtl' ) ) { 2928 $urls = array( $rtl_src ); 2929 } else { 2930 $urls[] = $rtl_src; 2931 } 2932 } 2933 } 2934 2935 foreach ( array_filter( $urls ) as $url ) { 2936 $resources[] = array( 2937 'href' => $url, 2938 'as' => $as, 2939 ); 2940 } 2941 } 2942 } 2943 2944 /** 2945 * Filters the assets prefetched for the screen the user is expected to open next. 2946 * 2947 * Fires on any screen from which the next one can be predicted, so `$next_screen` is what 2948 * distinguishes the contexts: the login screen passes the URL it is about to redirect to, and 2949 * the Dashboard and post list tables pass the editor they expect the user to open. 2950 * 2951 * Only the `href` and `as` attributes below are printed; any other key is ignored. Resources 2952 * sharing an `href` are collapsed to the first of them, so a callback may append without 2953 * checking what is already there. Returning an empty array turns the prefetching off. 2954 * 2955 * On the login screen the URLs are built before the user is authenticated and outside the 2956 * admin, so there is no current user and `is_admin()` is false. A {@see 'script_loader_src'} 2957 * or {@see 'style_loader_src'} callback that depends on either can produce a URL the admin 2958 * screen will not request, which wastes the prefetch; a callback on this filter can correct it. 2959 * 2960 * @since 7.2.0 2961 * 2962 * @param array $resources { 2963 * Array of resources and their attributes to prefetch. 2964 * 2965 * @type array ...$0 { 2966 * Array of resource attributes. 2967 * 2968 * @type string $href URL to prefetch. Required. 2969 * @type string $as How the browser should treat the resource 2970 * (`script`, `style`, `image`, `document`, etc). Required. 2971 * } 2972 * } 2973 * @param string $next_screen URL of the screen the assets are being prefetched for. Always 2974 * points into the admin, since nothing is prefetched otherwise. 2975 * From the login screen this is the redirect target, already run 2976 * through wp_validate_redirect() with the admin as the fallback, 2977 * and it may be relative: it is the value as wp_safe_redirect() 2978 * will receive it, so a request-supplied path is passed through 2979 * unchanged and only the fallback is a full URL. 2980 */ 2981 $resources = apply_filters( 'wp_prefetch_admin_assets', $resources, $next_screen ); 2982 2983 if ( ! is_array( $resources ) ) { 2984 return; 2985 } 2986 2987 $unique_resources = array(); 2988 2989 // Parse the complete resource list and extract unique resources. 2990 foreach ( $resources as $resource ) { 2991 if ( ! is_array( $resource ) ) { 2992 continue; 2993 } 2994 2995 $href = $resource['href'] ?? ''; 2996 $as = $resource['as'] ?? ''; 2997 2998 if ( ! is_string( $href ) || '' === $href || ! is_string( $as ) || '' === $as ) { 2999 continue; 3000 } 3001 3002 /* 3003 * Check again once escaped, since esc_url() returns an empty string for a URL it rejects. 3004 * An empty `href` would resolve to the current page, which the script below would then fetch. 3005 * Only `http` and `https` URLs are allowed, as wp_preload_resources() does, since nothing else 3006 * can be prefetched, and the script would pass anything else to fetch(). 3007 */ 3008 $href = esc_url( $href, array( 'http', 'https' ) ); 3009 3010 if ( '' === $href || isset( $unique_resources[ $href ] ) ) { 3011 continue; 3012 } 3013 3014 $unique_resources[ $href ] = $as; 3015 } 3016 3017 if ( ! $unique_resources ) { 3018 return; 3019 } 3020 3021 // Build and output the HTML for each unique resource. Each URL has already been escaped. 3022 foreach ( $unique_resources as $escaped_href => $as ) { 3023 printf( 3024 "<link rel='prefetch' href='%s' as='%s' />\n", 3025 $escaped_href, 3026 esc_attr( $as ) 3027 ); 3028 } 3029 3030 /* 3031 * Safari does not support `rel="prefetch"`, so in browsers that lack it, fetch the URLs of the 3032 * page's prefetch links with fetch() instead, to put the responses in the HTTP cache all the 3033 * same. Reading the links from the page, rather than passing their URLs to the script, keeps a 3034 * single list of them, and also covers any prefetch links added by plugins. The fetches wait 3035 * until the current screen has loaded, and ask for low priority, so they stay out of the way 3036 * of its own assets. Unlike a prefetch link, a fetch still in flight is canceled when the user 3037 * navigates away, so it cannot compete with the next screen's render-blocking assets. 3038 * 3039 * The requests use `no-cors` mode and include credentials, as the stylesheet and script 3040 * requests of the next screen do, so the cached responses match what that screen will ask for. 3041 * The body is read to completion so that the full response is cached. 3042 */ 3043 $js_function = <<<'JS' 3044 /** 3045 * Prefetches the page's rel=prefetch links with fetch() in browsers that do not support rel=prefetch. 3046 * 3047 * @see https://caniuse.com/link-rel-prefetch 3048 */ 3049 () => { 3050 if ( document.createElement( 'link' ).relList?.supports?.( 'prefetch' ) ) { 3051 return; 3052 } 3053 3054 const prefetch = () => { 3055 for ( const link of /** @type {NodeListOf<HTMLLinkElement>} */ ( document.querySelectorAll( 'link[rel~="prefetch"][href]' ) ) ) { 3056 fetch( link.href, { mode: 'no-cors', credentials: 'include', priority: 'low' } ) 3057 .then( ( response ) => response.blob() ) 3058 .catch( () => {} ); 3059 } 3060 }; 3061 3062 const schedule = () => { 3063 if ( 'requestIdleCallback' in window ) { 3064 window.requestIdleCallback( prefetch ); 3065 } else { 3066 setTimeout( prefetch, 0 ); 3067 } 3068 }; 3069 3070 if ( 'complete' === document.readyState ) { 3071 schedule(); 3072 } else { 3073 window.addEventListener( 'load', schedule, { once: true } ); 3074 } 3075 } 3076 JS; 3077 3078 wp_print_inline_script_tag( "( $js_function )();\n//# sourceURL=" . rawurlencode( __FUNCTION__ ) ); 2499 3079 } 2500 3080 … … 2917 3497 function wp_enqueue_editor_block_directory_assets() { 2918 3498 wp_enqueue_script( 'wp-block-directory' ); 3499 // Prefetched for the block editor by wp_prefetch_admin_assets(), which needs updating if this changes. 2919 3500 wp_enqueue_style( 'wp-block-directory' ); 2920 3501 } … … 2927 3508 function wp_enqueue_editor_format_library_assets() { 2928 3509 wp_enqueue_script( 'wp-format-library' ); 3510 // Prefetched for the block editor by wp_prefetch_admin_assets(), which needs updating if this changes. 2929 3511 wp_enqueue_style( 'wp-format-library' ); 2930 3512 } … … 3608 4190 3609 4191 wp_enqueue_script( 'wp-commands' ); 4192 // Prefetched from the login screen by wp_prefetch_admin_assets(), which needs updating if this changes. 3610 4193 wp_enqueue_style( 'wp-commands' ); 3611 4194 wp_enqueue_script( 'wp-core-commands' ); -
trunk/tests/phpunit/tests/dependencies/scripts.php
r63983 r64120 2351 2351 * Testing `wp_script_add_data` with the conditional key. 2352 2352 * 2353 * @expectedDeprecated WP_Dependencies ->add_data()2353 * @expectedDeprecated WP_Dependencies::add_data() 2354 2354 * 2355 2355 * @since 6.9.0 Conditional comments should now return an empty string. … … 2771 2771 2772 2772 /** 2773 * @expectedDeprecated WP_Dependencies ->add_data()2773 * @expectedDeprecated WP_Dependencies::add_data() 2774 2774 * 2775 2775 * @ticket 14853 … … 2822 2822 2823 2823 /** 2824 * @expectedDeprecated WP_Dependencies ->add_data()2824 * @expectedDeprecated WP_Dependencies::add_data() 2825 2825 * 2826 2826 * @ticket 36392 -
trunk/tests/phpunit/tests/dependencies/styles.php
r63628 r64120 365 365 * stylesheets are also conditional. 366 366 * 367 * @expectedDeprecated WP_Dependencies ->add_data()367 * @expectedDeprecated WP_Dependencies::add_data() 368 368 */ 369 369 public function test_conditional_inline_styles_are_also_conditional() {
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)