Changeset 61556 for branches/6.9/src/wp-includes/script-loader.php
- Timestamp:
- 01/29/2026 09:25:28 PM (7 months ago)
- Location:
- branches/6.9
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
src/wp-includes/script-loader.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/6.9
-
branches/6.9/src/wp-includes/script-loader.php
r61174 r61556 3644 3644 } 3645 3645 3646 // Capture the styles enqueued at the enqueue_block_assets action, so that non-core block styles and global styles can be inserted afterwards during hoisting. 3647 $style_handles_at_enqueue_block_assets = array(); 3648 add_action( 3649 'enqueue_block_assets', 3650 static function () use ( &$style_handles_at_enqueue_block_assets ) { 3651 $style_handles_at_enqueue_block_assets = wp_styles()->queue; 3652 }, 3653 PHP_INT_MIN 3654 ); 3655 add_action( 3656 'enqueue_block_assets', 3657 static function () use ( &$style_handles_at_enqueue_block_assets ) { 3658 $style_handles_at_enqueue_block_assets = array_values( array_diff( wp_styles()->queue, $style_handles_at_enqueue_block_assets ) ); 3659 }, 3660 PHP_INT_MAX 3661 ); 3662 3646 3663 /* 3647 * Add a placeholder comment into the inline styles for wp-block-library, after which wherethe late block styles3664 * Add a placeholder comment into the inline styles for wp-block-library, after which the late block styles 3648 3665 * can be hoisted from the footer to be printed in the header by means of a filter below on the template enhancement 3649 3666 * output buffer. The `wp_print_styles` action is used to ensure that if the inline style gets replaced at … … 3664 3681 * before `print_footer_scripts()` is called. 3665 3682 */ 3666 $printed_block_styles = ''; 3667 $printed_late_styles = ''; 3668 $capture_late_styles = static function () use ( &$printed_block_styles, &$printed_late_styles ) { 3683 $printed_core_block_styles = ''; 3684 $printed_other_block_styles = ''; 3685 $printed_global_styles = ''; 3686 $printed_late_styles = ''; 3687 3688 $capture_late_styles = static function () use ( &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) { 3669 3689 // Gather the styles related to on-demand block enqueues. 3670 $all_block_style_handles = array(); 3690 $all_core_block_style_handles = array(); 3691 $all_other_block_style_handles = array(); 3671 3692 foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block_type ) { 3672 foreach ( $block_type->style_handles as $style_handle ) { 3673 $all_block_style_handles[] = $style_handle; 3693 if ( str_starts_with( $block_type->name, 'core/' ) ) { 3694 foreach ( $block_type->style_handles as $style_handle ) { 3695 $all_core_block_style_handles[] = $style_handle; 3696 } 3697 } else { 3698 foreach ( $block_type->style_handles as $style_handle ) { 3699 $all_other_block_style_handles[] = $style_handle; 3700 } 3674 3701 } 3675 3702 } 3676 $all_block_style_handles = array_merge(3677 $all_block_style_handles,3678 array(3679 'global-styles',3680 'block-style-variation-styles',3681 'core-block-supports',3682 'core-block-supports-duotone',3683 )3684 );3685 3703 3686 3704 /* 3687 * First print all styles related to blocks which should inserted right after the wp-block-library stylesheet3705 * First print all styles related to blocks which should be inserted right after the wp-block-library stylesheet 3688 3706 * to preserve the CSS cascade. The logic in this `if` statement is derived from `wp_print_styles()`. 3689 3707 */ 3690 $enqueued_ block_styles = array_values( array_intersect( $all_block_style_handles, wp_styles()->queue ) );3691 if ( count( $enqueued_ block_styles ) > 0 ) {3708 $enqueued_core_block_styles = array_values( array_intersect( $all_core_block_style_handles, wp_styles()->queue ) ); 3709 if ( count( $enqueued_core_block_styles ) > 0 ) { 3692 3710 ob_start(); 3693 wp_styles()->do_items( $enqueued_block_styles ); 3694 $printed_block_styles = ob_get_clean(); 3711 wp_styles()->do_items( $enqueued_core_block_styles ); 3712 $printed_core_block_styles = ob_get_clean(); 3713 } 3714 3715 // Non-core block styles get printed after the classic-theme-styles stylesheet. 3716 $enqueued_other_block_styles = array_values( array_intersect( $all_other_block_style_handles, wp_styles()->queue ) ); 3717 if ( count( $enqueued_other_block_styles ) > 0 ) { 3718 ob_start(); 3719 wp_styles()->do_items( $enqueued_other_block_styles ); 3720 $printed_other_block_styles = ob_get_clean(); 3721 } 3722 3723 // Capture the global-styles so that it can be printed separately after classic-theme-styles and other styles enqueued at enqueue_block_assets. 3724 if ( wp_style_is( 'global-styles' ) ) { 3725 ob_start(); 3726 wp_styles()->do_items( array( 'global-styles' ) ); 3727 $printed_global_styles = ob_get_clean(); 3695 3728 } 3696 3729 … … 3733 3766 add_filter( 3734 3767 'wp_template_enhancement_output_buffer', 3735 static function ( $buffer ) use ( $placeholder, &$ printed_block_styles, &$printed_late_styles ) {3768 static function ( $buffer ) use ( $placeholder, &$style_handles_at_enqueue_block_assets, &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) { 3736 3769 3737 3770 // Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans. … … 3778 3811 }; 3779 3812 3780 /* 3781 * Insert block styles right after wp-block-library (if it is present), and then insert any remaining styles 3782 * at </head> (or else print everything there). The placeholder CSS comment will always be added to the 3783 * wp-block-library inline style since it gets printed at `wp_head` before the blocks are rendered. 3784 * This means that there may not actually be any block styles to hoist from the footer to insert after this 3785 * inline style. The placeholder CSS comment needs to be added so that the inline style gets printed, but 3786 * if the resulting inline style is empty after the placeholder is removed, then the inline style is 3787 * removed. 3788 */ 3813 // Locate the insertion points in the HEAD. 3789 3814 while ( $processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) { 3790 3815 if ( … … 3792 3817 'wp-block-library-inline-css' === $processor->get_attribute( 'id' ) 3793 3818 ) { 3794 $ css_text = $processor->get_modifiable_text();3795 3796 /*3797 * A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to3798 * be printed. Now that we've located the inline style, the placeholder comment can be removed. If3799 * there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL3800 * comment, then remove the STYLE entirely.)3801 */3802 $css_text = str_replace( $placeholder, '', $css_text );3803 if ( preg_match( ':^/\*# sourceURL=\S+? \*/$:', trim( $css_text ) ) ) {3804 $processor->remove();3805 } else {3806 $ processor->set_modifiable_text( $css_text );3819 $processor->set_bookmark( 'wp_block_library' ); 3820 } elseif ( 'HEAD' === $processor->get_tag() && $processor->is_tag_closer() ) { 3821 $processor->set_bookmark( 'head_end' ); 3822 break; 3823 } elseif ( ( 'STYLE' === $processor->get_tag() || 'LINK' === $processor->get_tag() ) && $processor->get_attribute( 'id' ) ) { 3824 $id = $processor->get_attribute( 'id' ); 3825 $handle = null; 3826 if ( 'STYLE' === $processor->get_tag() ) { 3827 if ( preg_match( '/^(.+)-inline-css$/', $id, $matches ) ) { 3828 $handle = $matches[1]; 3829 } 3830 } elseif ( preg_match( '/^(.+)-css$/', $id, $matches ) ) { 3831 $handle = $matches[1]; 3807 3832 } 3808 3833 3809 // Insert the $printed_late_styles immediately after the closing inline STYLE tag. This preserves the CSS cascade. 3810 if ( '' !== $printed_block_styles ) { 3811 $processor->insert_after( $printed_block_styles ); 3812 3813 // Prevent printing them again at </head>. 3814 $printed_block_styles = ''; 3834 if ( 'classic-theme-styles' === $handle ) { 3835 $processor->set_bookmark( 'classic_theme_styles' ); 3815 3836 } 3816 3837 3817 // If there aren't any late styles, there's no need to continue to finding </head>. 3818 if ( '' === $printed_late_styles ) { 3819 break; 3838 if ( $handle && in_array( $handle, $style_handles_at_enqueue_block_assets, true ) ) { 3839 if ( ! $processor->has_bookmark( 'first_style_at_enqueue_block_assets' ) ) { 3840 $processor->set_bookmark( 'first_style_at_enqueue_block_assets' ); 3841 } 3842 $processor->set_bookmark( 'last_style_at_enqueue_block_assets' ); 3820 3843 } 3821 } elseif ( 'HEAD' === $processor->get_tag() && $processor->is_tag_closer() ) {3822 $processor->insert_before( $printed_block_styles . $printed_late_styles );3823 break;3824 3844 } 3825 3845 } 3826 3846 3847 /* 3848 * Insert block styles right after wp-block-library (if it is present). The placeholder CSS comment will 3849 * always be added to the wp-block-library inline style since it gets printed at `wp_head` before the blocks 3850 * are rendered. This means that there may not actually be any block styles to hoist from the footer to 3851 * insert after this inline style. The placeholder CSS comment needs to be added so that the inline style 3852 * gets printed, but if the resulting inline style is empty after the placeholder is removed, then the 3853 * inline style is removed. 3854 */ 3855 if ( $processor->has_bookmark( 'wp_block_library' ) ) { 3856 $processor->seek( 'wp_block_library' ); 3857 3858 $css_text = $processor->get_modifiable_text(); 3859 3860 /* 3861 * A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to 3862 * be printed. Now that we've located the inline style, the placeholder comment can be removed. If 3863 * there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL 3864 * comment), then remove the STYLE entirely. 3865 */ 3866 $css_text = str_replace( $placeholder, '', $css_text ); 3867 if ( preg_match( ':^/\*# sourceURL=\S+? \*/$:', trim( $css_text ) ) ) { 3868 $processor->remove(); 3869 } else { 3870 $processor->set_modifiable_text( $css_text ); 3871 } 3872 3873 $inserted_after = $printed_core_block_styles; 3874 $printed_core_block_styles = ''; 3875 3876 // If the classic-theme-styles is absent, then the third-party block styles cannot be inserted after it, so they get inserted here. 3877 if ( ! $processor->has_bookmark( 'classic_theme_styles' ) ) { 3878 if ( '' !== $printed_other_block_styles ) { 3879 $inserted_after .= $printed_other_block_styles; 3880 } 3881 $printed_other_block_styles = ''; 3882 3883 // If there aren't any other styles printed at enqueue_block_assets either, then the global styles need to also be printed here. 3884 if ( ! $processor->has_bookmark( 'last_style_at_enqueue_block_assets' ) ) { 3885 if ( '' !== $printed_global_styles ) { 3886 $inserted_after .= $printed_global_styles; 3887 } 3888 $printed_global_styles = ''; 3889 } 3890 } 3891 3892 if ( '' !== $inserted_after ) { 3893 $processor->insert_after( "\n" . $inserted_after ); 3894 } 3895 } 3896 3897 // Insert global-styles after the styles enqueued at enqueue_block_assets. 3898 if ( '' !== $printed_global_styles && $processor->has_bookmark( 'last_style_at_enqueue_block_assets' ) ) { 3899 $processor->seek( 'last_style_at_enqueue_block_assets' ); 3900 3901 $processor->insert_after( "\n" . $printed_global_styles ); 3902 $printed_global_styles = ''; 3903 3904 if ( ! $processor->has_bookmark( 'classic_theme_styles' ) && '' !== $printed_other_block_styles ) { 3905 $processor->insert_after( "\n" . $printed_other_block_styles ); 3906 $printed_other_block_styles = ''; 3907 } 3908 } 3909 3910 // Insert third-party block styles right after the classic-theme-styles. 3911 if ( '' !== $printed_other_block_styles && $processor->has_bookmark( 'classic_theme_styles' ) ) { 3912 $processor->seek( 'classic_theme_styles' ); 3913 $processor->insert_after( "\n" . $printed_other_block_styles ); 3914 $printed_other_block_styles = ''; 3915 } 3916 3917 // Print all remaining styles. 3918 $remaining_styles = $printed_core_block_styles . $printed_other_block_styles . $printed_global_styles . $printed_late_styles; 3919 if ( $remaining_styles && $processor->has_bookmark( 'head_end' ) ) { 3920 $processor->seek( 'head_end' ); 3921 $processor->insert_before( $remaining_styles . "\n" ); 3922 } 3827 3923 return $processor->get_updated_html(); 3828 3924 }
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)