Changeset 61556
- Timestamp:
- 01/29/2026 09:25:28 PM (5 weeks ago)
- Location:
- branches/6.9
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
src/wp-includes/script-loader.php (modified) (5 diffs)
-
tests/phpunit/tests/template.php (modified) (9 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 } -
branches/6.9/tests/phpunit/tests/template.php
r61174 r61556 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 … … 1694 1793 static function () { 1695 1794 return '/* CUSTOM CSS from Customizer */'; 1795 } 1796 ); 1797 1798 wp_register_style( 'third-party-test-block', 'https://example.com/third-party-test-block.css', array(), null ); 1799 register_block_type( 1800 'third-party/test', 1801 array( 1802 'style_handles' => array( 'third-party-test-block' ), 1803 ) 1804 ); 1805 1806 /* 1807 * This is very old guidance about how to enqueue styles for blocks. Certain themes still enqueue block 1808 * styles using this action. 1809 */ 1810 add_action( 1811 'enqueue_block_assets', 1812 static function () { 1813 wp_enqueue_style( 'custom-block-styles', 'https://example.com/custom-block-styles.css', array(), null ); 1696 1814 } 1697 1815 ); … … 1747 1865 $content = apply_filters( 1748 1866 'the_content', 1749 '<!-- wp:separator --><hr class="wp-block-separator has-alpha-channel-opacity"/><!-- /wp:separator -->' 1867 '<!-- wp:separator --><hr class="wp-block-separator has-alpha-channel-opacity"/><!-- /wp:separator -->' . 1868 '<!-- wp:third-party/test --><div>This is only a test!</div><!-- /wp:third-party/test -->' 1750 1869 ); 1751 1870
Note: See TracChangeset
for help on using the changeset viewer.