Changeset 61554
- Timestamp:
- 01/29/2026 06:14:05 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
src/wp-includes/script-loader.php (modified) (5 diffs)
-
tests/phpunit/tests/template.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/script-loader.php
r61539 r61554 3702 3702 } 3703 3703 3704 // 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. 3705 $style_handles_at_enqueue_block_assets = array(); 3706 add_action( 3707 'enqueue_block_assets', 3708 static function () use ( &$style_handles_at_enqueue_block_assets ) { 3709 $style_handles_at_enqueue_block_assets = wp_styles()->queue; 3710 }, 3711 PHP_INT_MIN 3712 ); 3713 add_action( 3714 'enqueue_block_assets', 3715 static function () use ( &$style_handles_at_enqueue_block_assets ) { 3716 $style_handles_at_enqueue_block_assets = array_values( array_diff( wp_styles()->queue, $style_handles_at_enqueue_block_assets ) ); 3717 }, 3718 PHP_INT_MAX 3719 ); 3720 3704 3721 /* 3705 * Add a placeholder comment into the inline styles for wp-block-library, after which wherethe late block styles3722 * Add a placeholder comment into the inline styles for wp-block-library, after which the late block styles 3706 3723 * can be hoisted from the footer to be printed in the header by means of a filter below on the template enhancement 3707 3724 * output buffer. The `wp_print_styles` action is used to ensure that if the inline style gets replaced at … … 3722 3739 * before `print_footer_scripts()` is called. 3723 3740 */ 3724 $printed_block_styles = ''; 3725 $printed_late_styles = ''; 3726 $capture_late_styles = static function () use ( &$printed_block_styles, &$printed_late_styles ) { 3741 $printed_core_block_styles = ''; 3742 $printed_other_block_styles = ''; 3743 $printed_global_styles = ''; 3744 $printed_late_styles = ''; 3745 3746 $capture_late_styles = static function () use ( &$printed_core_block_styles, &$printed_other_block_styles, &$printed_global_styles, &$printed_late_styles ) { 3727 3747 // Gather the styles related to on-demand block enqueues. 3728 $all_block_style_handles = array(); 3748 $all_core_block_style_handles = array(); 3749 $all_other_block_style_handles = array(); 3729 3750 foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block_type ) { 3730 foreach ( $block_type->style_handles as $style_handle ) { 3731 $all_block_style_handles[] = $style_handle; 3751 if ( str_starts_with( $block_type->name, 'core/' ) ) { 3752 foreach ( $block_type->style_handles as $style_handle ) { 3753 $all_core_block_style_handles[] = $style_handle; 3754 } 3755 } else { 3756 foreach ( $block_type->style_handles as $style_handle ) { 3757 $all_other_block_style_handles[] = $style_handle; 3758 } 3732 3759 } 3733 3760 } 3734 $all_block_style_handles = array_merge(3735 $all_block_style_handles,3736 array(3737 'global-styles',3738 'block-style-variation-styles',3739 'core-block-supports',3740 'core-block-supports-duotone',3741 )3742 );3743 3761 3744 3762 /* 3745 * First print all styles related to blocks which should inserted right after the wp-block-library stylesheet3763 * First print all styles related to blocks which should be inserted right after the wp-block-library stylesheet 3746 3764 * to preserve the CSS cascade. The logic in this `if` statement is derived from `wp_print_styles()`. 3747 3765 */ 3748 $enqueued_ block_styles = array_values( array_intersect( $all_block_style_handles, wp_styles()->queue ) );3749 if ( count( $enqueued_ block_styles ) > 0 ) {3766 $enqueued_core_block_styles = array_values( array_intersect( $all_core_block_style_handles, wp_styles()->queue ) ); 3767 if ( count( $enqueued_core_block_styles ) > 0 ) { 3750 3768 ob_start(); 3751 wp_styles()->do_items( $enqueued_block_styles ); 3752 $printed_block_styles = ob_get_clean(); 3769 wp_styles()->do_items( $enqueued_core_block_styles ); 3770 $printed_core_block_styles = ob_get_clean(); 3771 } 3772 3773 // Non-core block styles get printed after the classic-theme-styles stylesheet. 3774 $enqueued_other_block_styles = array_values( array_intersect( $all_other_block_style_handles, wp_styles()->queue ) ); 3775 if ( count( $enqueued_other_block_styles ) > 0 ) { 3776 ob_start(); 3777 wp_styles()->do_items( $enqueued_other_block_styles ); 3778 $printed_other_block_styles = ob_get_clean(); 3779 } 3780 3781 // Capture the global-styles so that it can be printed separately after classic-theme-styles and other styles enqueued at enqueue_block_assets. 3782 if ( wp_style_is( 'global-styles' ) ) { 3783 ob_start(); 3784 wp_styles()->do_items( array( 'global-styles' ) ); 3785 $printed_global_styles = ob_get_clean(); 3753 3786 } 3754 3787 … … 3791 3824 add_filter( 3792 3825 'wp_template_enhancement_output_buffer', 3793 static function ( $buffer ) use ( $placeholder, &$ printed_block_styles, &$printed_late_styles ) {3826 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 ) { 3794 3827 3795 3828 // Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans. … … 3836 3869 }; 3837 3870 3838 /* 3839 * Insert block styles right after wp-block-library (if it is present), and then insert any remaining styles 3840 * at </head> (or else print everything there). The placeholder CSS comment will always be added to the 3841 * wp-block-library inline style since it gets printed at `wp_head` before the blocks are rendered. 3842 * This means that there may not actually be any block styles to hoist from the footer to insert after this 3843 * inline style. The placeholder CSS comment needs to be added so that the inline style gets printed, but 3844 * if the resulting inline style is empty after the placeholder is removed, then the inline style is 3845 * removed. 3846 */ 3871 // Locate the insertion points in the HEAD. 3847 3872 while ( $processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) { 3848 3873 if ( … … 3850 3875 'wp-block-library-inline-css' === $processor->get_attribute( 'id' ) 3851 3876 ) { 3852 $ css_text = $processor->get_modifiable_text();3853 3854 /*3855 * A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to3856 * be printed. Now that we've located the inline style, the placeholder comment can be removed. If3857 * there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL3858 * comment, then remove the STYLE entirely.)3859 */3860 $css_text = str_replace( $placeholder, '', $css_text );3861 if ( preg_match( ':^/\*# sourceURL=\S+? \*/$:', trim( $css_text ) ) ) {3862 $processor->remove();3863 } else {3864 $ processor->set_modifiable_text( $css_text );3877 $processor->set_bookmark( 'wp_block_library' ); 3878 } elseif ( 'HEAD' === $processor->get_tag() && $processor->is_tag_closer() ) { 3879 $processor->set_bookmark( 'head_end' ); 3880 break; 3881 } elseif ( ( 'STYLE' === $processor->get_tag() || 'LINK' === $processor->get_tag() ) && $processor->get_attribute( 'id' ) ) { 3882 $id = $processor->get_attribute( 'id' ); 3883 $handle = null; 3884 if ( 'STYLE' === $processor->get_tag() ) { 3885 if ( preg_match( '/^(.+)-inline-css$/', $id, $matches ) ) { 3886 $handle = $matches[1]; 3887 } 3888 } elseif ( preg_match( '/^(.+)-css$/', $id, $matches ) ) { 3889 $handle = $matches[1]; 3865 3890 } 3866 3891 3867 // Insert the $printed_late_styles immediately after the closing inline STYLE tag. This preserves the CSS cascade. 3868 if ( '' !== $printed_block_styles ) { 3869 $processor->insert_after( $printed_block_styles ); 3870 3871 // Prevent printing them again at </head>. 3872 $printed_block_styles = ''; 3892 if ( 'classic-theme-styles' === $handle ) { 3893 $processor->set_bookmark( 'classic_theme_styles' ); 3873 3894 } 3874 3895 3875 // If there aren't any late styles, there's no need to continue to finding </head>. 3876 if ( '' === $printed_late_styles ) { 3877 break; 3896 if ( $handle && in_array( $handle, $style_handles_at_enqueue_block_assets, true ) ) { 3897 if ( ! $processor->has_bookmark( 'first_style_at_enqueue_block_assets' ) ) { 3898 $processor->set_bookmark( 'first_style_at_enqueue_block_assets' ); 3899 } 3900 $processor->set_bookmark( 'last_style_at_enqueue_block_assets' ); 3878 3901 } 3879 } elseif ( 'HEAD' === $processor->get_tag() && $processor->is_tag_closer() ) {3880 $processor->insert_before( $printed_block_styles . $printed_late_styles );3881 break;3882 3902 } 3883 3903 } 3884 3904 3905 /* 3906 * Insert block styles right after wp-block-library (if it is present). The placeholder CSS comment will 3907 * always be added to the wp-block-library inline style since it gets printed at `wp_head` before the blocks 3908 * are rendered. This means that there may not actually be any block styles to hoist from the footer to 3909 * insert after this inline style. The placeholder CSS comment needs to be added so that the inline style 3910 * gets printed, but if the resulting inline style is empty after the placeholder is removed, then the 3911 * inline style is removed. 3912 */ 3913 if ( $processor->has_bookmark( 'wp_block_library' ) ) { 3914 $processor->seek( 'wp_block_library' ); 3915 3916 $css_text = $processor->get_modifiable_text(); 3917 3918 /* 3919 * A placeholder CSS comment is added to the inline style in order to force an inline STYLE tag to 3920 * be printed. Now that we've located the inline style, the placeholder comment can be removed. If 3921 * there is no CSS left in the STYLE tag after removing the placeholder (aside from the sourceURL 3922 * comment), then remove the STYLE entirely. 3923 */ 3924 $css_text = str_replace( $placeholder, '', $css_text ); 3925 if ( preg_match( ':^/\*# sourceURL=\S+? \*/$:', trim( $css_text ) ) ) { 3926 $processor->remove(); 3927 } else { 3928 $processor->set_modifiable_text( $css_text ); 3929 } 3930 3931 $inserted_after = $printed_core_block_styles; 3932 $printed_core_block_styles = ''; 3933 3934 // If the classic-theme-styles is absent, then the third-party block styles cannot be inserted after it, so they get inserted here. 3935 if ( ! $processor->has_bookmark( 'classic_theme_styles' ) ) { 3936 if ( '' !== $printed_other_block_styles ) { 3937 $inserted_after .= $printed_other_block_styles; 3938 } 3939 $printed_other_block_styles = ''; 3940 3941 // If there aren't any other styles printed at enqueue_block_assets either, then the global styles need to also be printed here. 3942 if ( ! $processor->has_bookmark( 'last_style_at_enqueue_block_assets' ) ) { 3943 if ( '' !== $printed_global_styles ) { 3944 $inserted_after .= $printed_global_styles; 3945 } 3946 $printed_global_styles = ''; 3947 } 3948 } 3949 3950 if ( '' !== $inserted_after ) { 3951 $processor->insert_after( "\n" . $inserted_after ); 3952 } 3953 } 3954 3955 // Insert global-styles after the styles enqueued at enqueue_block_assets. 3956 if ( '' !== $printed_global_styles && $processor->has_bookmark( 'last_style_at_enqueue_block_assets' ) ) { 3957 $processor->seek( 'last_style_at_enqueue_block_assets' ); 3958 3959 $processor->insert_after( "\n" . $printed_global_styles ); 3960 $printed_global_styles = ''; 3961 3962 if ( ! $processor->has_bookmark( 'classic_theme_styles' ) && '' !== $printed_other_block_styles ) { 3963 $processor->insert_after( "\n" . $printed_other_block_styles ); 3964 $printed_other_block_styles = ''; 3965 } 3966 } 3967 3968 // Insert third-party block styles right after the classic-theme-styles. 3969 if ( '' !== $printed_other_block_styles && $processor->has_bookmark( 'classic_theme_styles' ) ) { 3970 $processor->seek( 'classic_theme_styles' ); 3971 $processor->insert_after( "\n" . $printed_other_block_styles ); 3972 $printed_other_block_styles = ''; 3973 } 3974 3975 // Print all remaining styles. 3976 $remaining_styles = $printed_core_block_styles . $printed_other_block_styles . $printed_global_styles . $printed_late_styles; 3977 if ( $remaining_styles && $processor->has_bookmark( 'head_end' ) ) { 3978 $processor->seek( 'head_end' ); 3979 $processor->insert_before( $remaining_styles . "\n" ); 3980 } 3885 3981 return $processor->get_updated_html(); 3886 3982 } -
trunk/tests/phpunit/tests/template.php
r61479 r61554 146 146 unregister_taxonomy( 'taxo' ); 147 147 $this->set_permalink_structure( '' ); 148 149 $registry = WP_Block_Type_Registry::get_instance(); 150 if ( $registry->is_registered( 'third-party/test' ) ) { 151 $registry->unregister( 'third-party/test' ); 152 } 148 153 149 154 parent::tear_down(); … … 1476 1481 */ 1477 1482 public function data_wp_hoist_late_printed_styles(): array { 1478 $ common_expected_head_styles = array(1483 $early_common_styles = array( 1479 1484 'wp-img-auto-sizes-contain-inline-css', 1480 1485 'early-css', 1481 1486 'early-inline-css', 1482 1487 'wp-emoji-styles-inline-css', 1483 'wp-block-library-css', 1484 'wp-block-separator-css', 1485 'global-styles-inline-css', 1486 'core-block-supports-inline-css', 1487 'classic-theme-styles-css', 1488 ); 1489 1490 $common_late_in_head = array( 1491 // Styles enqueued at wp_enqueue_scripts (priority 10). 1488 1492 'normal-css', 1489 1493 'normal-inline-css', 1494 1495 // Styles printed at wp_head priority 10. 1490 1496 'wp-custom-css', 1497 ); 1498 1499 $common_late_in_body = array( 1491 1500 'late-css', 1492 1501 'late-inline-css', 1502 'core-block-supports-inline-css', 1503 ); 1504 1505 $common_expected_head_styles = array_merge( 1506 $early_common_styles, 1507 array( 1508 // Core block styles enqueued by wp_common_block_scripts_and_styles(), which runs at wp_enqueue_scripts priority 10, added first. 1509 'wp-block-library-css', // Inline printed. 1510 'wp-block-separator-css', // Hoisted. 1511 1512 // The wp_common_block_scripts_and_styles() function also fires enqueue_block_assets, at which wp_enqueue_classic_theme_styles() runs. 1513 'classic-theme-styles-css', // Printed at enqueue_block_assets. 1514 1515 // Third-party block styles. 1516 'third-party-test-block-css', // Hoisted. 1517 1518 // Other styles enqueued at enqueue_block_assets, which is fired by wp_common_block_scripts_and_styles(). 1519 'custom-block-styles-css', // Printed at enqueue_block_assets. 1520 1521 // Hoisted. Enqueued by wp_enqueue_global_styles() which runs at wp_enqueue_scripts priority 10 and wp_footer priority 1. 1522 'global-styles-inline-css', 1523 ), 1524 $common_late_in_head, 1525 $common_late_in_body 1493 1526 ); 1494 1527 … … 1506 1539 'inline_size_limit' => PHP_INT_MAX, 1507 1540 'expected_styles' => array( 1508 'HEAD' => array( 1509 'wp-img-auto-sizes-contain-inline-css', 1510 'early-css', 1511 'early-inline-css', 1512 'wp-emoji-styles-inline-css', 1513 'wp-block-library-inline-css', 1514 'wp-block-separator-inline-css', 1515 'global-styles-inline-css', 1516 'core-block-supports-inline-css', 1517 'classic-theme-styles-inline-css', 1518 'normal-css', 1519 'normal-inline-css', 1520 'wp-custom-css', 1521 'late-css', 1522 'late-inline-css', 1541 'HEAD' => array_merge( 1542 $early_common_styles, 1543 array( 1544 'wp-block-library-inline-css', 1545 'wp-block-separator-inline-css', 1546 'classic-theme-styles-inline-css', 1547 'third-party-test-block-css', 1548 'custom-block-styles-css', 1549 'global-styles-inline-css', 1550 ), 1551 $common_late_in_head, 1552 $common_late_in_body 1553 ), 1554 'BODY' => array(), 1555 ), 1556 ), 1557 'classic_theme_styles_omitted' => array( 1558 'set_up' => static function () { 1559 // Note that wp_enqueue_scripts is used instead of enqueue_block_assets because it runs again at the former action. 1560 add_action( 1561 'wp_enqueue_scripts', 1562 static function () { 1563 wp_dequeue_style( 'classic-theme-styles' ); 1564 }, 1565 100 1566 ); 1567 }, 1568 'inline_size_limit' => PHP_INT_MAX, 1569 'expected_styles' => array( 1570 'HEAD' => array_merge( 1571 $early_common_styles, 1572 array( 1573 'wp-block-library-inline-css', 1574 'wp-block-separator-inline-css', 1575 'third-party-test-block-css', 1576 'custom-block-styles-css', 1577 'global-styles-inline-css', 1578 ), 1579 $common_late_in_head, 1580 $common_late_in_body 1581 ), 1582 'BODY' => array(), 1583 ), 1584 ), 1585 'no_styles_at_enqueued_block_assets' => array( 1586 'set_up' => static function () { 1587 add_action( 1588 'wp_enqueue_scripts', 1589 static function () { 1590 wp_dequeue_style( 'classic-theme-styles' ); 1591 wp_dequeue_style( 'custom-block-styles' ); 1592 }, 1593 100 1594 ); 1595 }, 1596 'inline_size_limit' => PHP_INT_MAX, 1597 'expected_styles' => array( 1598 'HEAD' => array_merge( 1599 $early_common_styles, 1600 array( 1601 'wp-block-library-inline-css', 1602 'wp-block-separator-inline-css', 1603 'third-party-test-block-css', 1604 'global-styles-inline-css', 1605 ), 1606 $common_late_in_head, 1607 $common_late_in_body 1608 ), 1609 'BODY' => array(), 1610 ), 1611 ), 1612 'no_global_styles' => array( 1613 'set_up' => static function () { 1614 add_filter( 1615 'print_styles_array', 1616 static function ( $handles ) { 1617 return array_values( array_diff( $handles, array( 'global-styles' ) ) ); 1618 } 1619 ); 1620 }, 1621 'inline_size_limit' => PHP_INT_MAX, 1622 'expected_styles' => array( 1623 'HEAD' => array_merge( 1624 $early_common_styles, 1625 array( 1626 'wp-block-library-inline-css', 1627 'wp-block-separator-inline-css', 1628 'classic-theme-styles-inline-css', 1629 'third-party-test-block-css', 1630 'custom-block-styles-css', 1631 ), 1632 $common_late_in_head, 1633 $common_late_in_body 1523 1634 ), 1524 1635 'BODY' => array(), … … 1552 1663 'inline_size_limit' => 0, 1553 1664 'expected_styles' => array( 1554 'HEAD' => array( 1555 'wp-img-auto-sizes-contain-inline-css', 1556 'early-css', 1557 'early-inline-css', 1558 'wp-emoji-styles-inline-css', 1559 'wp-block-library-css', 1560 'classic-theme-styles-css', 1561 'global-styles-inline-css', 1562 'normal-css', 1563 'normal-inline-css', 1564 'wp-custom-css', 1665 'HEAD' => array_merge( 1666 $early_common_styles, 1667 array( 1668 'wp-block-library-css', 1669 'classic-theme-styles-css', 1670 'third-party-test-block-css', 1671 'custom-block-styles-css', 1672 'global-styles-inline-css', 1673 ), 1674 $common_late_in_head 1565 1675 ), 1566 'BODY' => array( 1567 'late-css', 1568 'late-inline-css', 1569 'core-block-supports-inline-css', 1570 ), 1676 'BODY' => $common_late_in_body, 1571 1677 ), 1572 1678 ), … … 1615 1721 'inline_size_limit' => 0, 1616 1722 'expected_styles' => array( 1617 'HEAD' => array( 1618 'wp-img-auto-sizes-contain-inline-css', 1619 'early-css', 1620 'early-inline-css', 1621 'wp-emoji-styles-inline-css', 1622 'classic-theme-styles-css', 1623 'global-styles-inline-css', 1624 'normal-css', 1625 'normal-inline-css', 1626 'wp-custom-css', 1723 'HEAD' => array_merge( 1724 $early_common_styles, 1725 array( 1726 'classic-theme-styles-css', 1727 'third-party-test-block-css', 1728 'custom-block-styles-css', 1729 'global-styles-inline-css', 1730 ), 1731 $common_late_in_head 1627 1732 ), 1628 'BODY' => array( 1629 'late-css', 1630 'late-inline-css', 1631 'core-block-supports-inline-css', 1632 ), 1733 'BODY' => $common_late_in_body, 1633 1734 ), 1634 1735 ), … … 1645 1746 'inline_size_limit' => 0, 1646 1747 'expected_styles' => array( 1647 'HEAD' => array( 1648 'wp-img-auto-sizes-contain-inline-css', 1649 'early-css', 1650 'early-inline-css', 1651 'wp-emoji-styles-inline-css', 1652 'wp-block-library-css', 1653 'wp-block-library-inline-css', // This contains the "OVERRIDDEN" text. 1654 'wp-block-separator-css', 1655 'global-styles-inline-css', 1656 'core-block-supports-inline-css', 1657 'classic-theme-styles-css', 1658 'normal-css', 1659 'normal-inline-css', 1660 'wp-custom-css', 1661 'late-css', 1662 'late-inline-css', 1748 'HEAD' => array_merge( 1749 $early_common_styles, 1750 array( 1751 'wp-block-library-css', 1752 'wp-block-library-inline-css', // This contains the "OVERRIDDEN" text. 1753 'wp-block-separator-css', 1754 'classic-theme-styles-css', 1755 'third-party-test-block-css', 1756 'custom-block-styles-css', 1757 'global-styles-inline-css', 1758 ), 1759 $common_late_in_head, 1760 $common_late_in_body 1663 1761 ), 1664 1762 'BODY' => array(), … … 1672 1770 * 1673 1771 * @ticket 64099 1772 * @ticket 64354 1674 1773 * @covers ::wp_load_classic_theme_block_styles_on_demand 1675 1774 * @covers ::wp_hoist_late_printed_styles … … 1697 1796 static function () { 1698 1797 return '/* CUSTOM CSS from Customizer */'; 1798 } 1799 ); 1800 1801 wp_register_style( 'third-party-test-block', 'https://example.com/third-party-test-block.css', array(), null ); 1802 register_block_type( 1803 'third-party/test', 1804 array( 1805 'style_handles' => array( 'third-party-test-block' ), 1806 ) 1807 ); 1808 1809 /* 1810 * This is very old guidance about how to enqueue styles for blocks. Certain themes still enqueue block 1811 * styles using this action. 1812 */ 1813 add_action( 1814 'enqueue_block_assets', 1815 static function () { 1816 wp_enqueue_style( 'custom-block-styles', 'https://example.com/custom-block-styles.css', array(), null ); 1699 1817 } 1700 1818 ); … … 1752 1870 $content = apply_filters( 1753 1871 'the_content', 1754 '<!-- wp:separator --><hr class="wp-block-separator has-alpha-channel-opacity"/><!-- /wp:separator -->' 1872 '<!-- wp:separator --><hr class="wp-block-separator has-alpha-channel-opacity"/><!-- /wp:separator -->' . 1873 '<!-- wp:third-party/test --><div>This is only a test!</div><!-- /wp:third-party/test -->' 1755 1874 ); 1756 1875
Note: See TracChangeset
for help on using the changeset viewer.