Changeset 61088
- Timestamp:
- 10/30/2025 12:46:29 AM (3 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
-
src/wp-includes/class-wp.php (modified) (1 diff)
-
src/wp-includes/default-filters.php (modified) (1 diff)
-
src/wp-includes/template.php (modified) (4 diffs)
-
tests/phpunit/tests/template.php (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp.php
r59728 r61088 589 589 * Fires once the requested HTTP headers for caching, content type, etc. have been sent. 590 590 * 591 * The {@see 'wp_send_late_headers'} action may be used to send headers after rendering the template into an 592 * output buffer. 593 * 591 594 * @since 2.1.0 592 595 * -
trunk/src/wp-includes/default-filters.php
r61078 r61088 423 423 add_action( 'do_robots', 'do_robots' ); 424 424 add_action( 'do_favicon', 'do_favicon' ); 425 add_action( 'wp_before_include_template', 'wp_start_template_enhancement_output_buffer' );425 add_action( 'wp_before_include_template', 'wp_start_template_enhancement_output_buffer', 1000 ); // Late priority to let `wp_template_enhancement_output_buffer` filters and `wp_send_late_headers` actions be registered. 426 426 add_action( 'set_comment_cookies', 'wp_set_comment_cookies', 10, 3 ); 427 427 add_action( 'sanitize_comment_cookies', 'sanitize_comment_cookies' ); -
trunk/src/wp-includes/template.php
r60994 r61088 845 845 * 846 846 * By default, an output buffer is only started if a {@see 'wp_template_enhancement_output_buffer'} filter has been 847 * added. For this default to apply, a filter must be added by the time the template is included at the 848 * {@see 'wp_before_include_template'} action. This allows template responses to be streamed as much as possible 849 * when no template enhancements are registered to apply. This filter allows a site to opt in to adding such 850 * template enhancement filters during the rendering of the template. 847 * added or if a plugin has added a {@see 'wp_send_late_headers'} action. For this default to apply, either of the 848 * hooks must be added by the time the template is included at the {@see 'wp_before_include_template'} action. This 849 * allows template responses to be streamed unless the there is code which depends on an output buffer being opened. 850 * This filter allows a site to opt in to adding such template enhancement filters later during the rendering of the 851 * template. 851 852 * 852 853 * @since 6.9.0 … … 854 855 * @param bool $use_output_buffer Whether an output buffer is started. 855 856 */ 856 return (bool) apply_filters( 'wp_should_output_buffer_template_for_enhancement', has_filter( 'wp_template_enhancement_output_buffer' ) );857 return (bool) apply_filters( 'wp_should_output_buffer_template_for_enhancement', has_filter( 'wp_template_enhancement_output_buffer' ) || has_action( 'wp_send_late_headers' ) ); 857 858 } 858 859 … … 958 959 // If the content type is not HTML, short-circuit since it is not relevant for enhancement. 959 960 if ( ! $is_html_content_type ) { 961 /** This action is documented in wp-includes/template.php */ 962 do_action( 'wp_send_late_headers', $output ); 960 963 return $output; 961 964 } … … 978 981 * @param string $output Original HTML template output buffer. 979 982 */ 980 return (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output ); 981 } 983 $filtered_output = (string) apply_filters( 'wp_template_enhancement_output_buffer', $filtered_output, $output ); 984 985 /** 986 * Fires at the last moment HTTP headers may be sent. 987 * 988 * This happens immediately before the template enhancement output buffer is flushed. This is in contrast with 989 * the {@see 'send_headers'} action which fires after the initial headers have been sent before the template 990 * has begun rendering, and thus does not depend on output buffering. This action does not fire if the "template 991 * enhancement output buffer" was not started. This output buffer is automatically started if this action is added 992 * before {@see wp_start_template_enhancement_output_buffer()} runs at the {@see 'wp_before_include_template'} 993 * action with priority 1000. Before this point, the output buffer will also be started automatically if there was a 994 * {@see 'wp_template_enhancement_output_buffer'} filter added, or if the 995 * {@see 'wp_should_output_buffer_template_for_enhancement'} filter is made to return `true`. 996 * 997 * @since 6.9.0 998 * 999 * @param string $output Output buffer. 1000 */ 1001 do_action( 'wp_send_late_headers', $filtered_output ); 1002 1003 return $filtered_output; 1004 } -
trunk/tests/phpunit/tests/template.php
r61076 r61088 604 604 * 605 605 * @ticket 43258 606 * 606 607 * @covers ::wp_should_output_buffer_template_for_enhancement 607 608 * @covers ::wp_start_template_enhancement_output_buffer … … 627 628 * 628 629 * @ticket 43258 630 * @ticket 64126 631 * 629 632 * @covers ::wp_start_template_enhancement_output_buffer 630 633 * @covers ::wp_finalize_template_enhancement_output_buffer … … 634 637 ob_start(); 635 638 636 $ filter_args = null;639 $mock_filter_callback = new MockAction(); 637 640 add_filter( 638 641 'wp_template_enhancement_output_buffer', 639 static function ( string $buffer ) use ( &$filter_args ): string { 640 $filter_args = func_get_args(); 641 642 array( $mock_filter_callback, 'filter' ), 643 10, 644 PHP_INT_MAX 645 ); 646 647 $mock_action_callback = new MockAction(); 648 add_filter( 649 'wp_send_late_headers', 650 array( $mock_action_callback, 'action' ), 651 10, 652 PHP_INT_MAX 653 ); 654 655 add_filter( 656 'wp_template_enhancement_output_buffer', 657 static function ( string $buffer ): string { 642 658 $p = WP_HTML_Processor::create_full_parser( $buffer ); 643 659 while ( $p->next_tag() ) { … … 657 673 } 658 674 return $p->get_updated_html(); 659 }, 660 10, 661 PHP_INT_MAX 675 } 662 676 ); 663 677 … … 696 710 $this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' ); 697 711 712 $this->assertSame( 1, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to have applied.' ); 713 $filter_args = $mock_filter_callback->get_args()[0]; 698 714 $this->assertIsArray( $filter_args, 'Expected the wp_template_enhancement_output_buffer filter to have applied.' ); 699 715 $this->assertCount( 2, $filter_args, 'Expected two args to be supplied to the wp_template_enhancement_output_buffer filter.' ); … … 717 733 $this->assertStringContainsString( '<h1>¡Hola, mundo!</h1>', $processed_output, 'Expected processed output to contain string.' ); 718 734 $this->assertStringContainsString( '</html>', $processed_output, 'Expected processed output to contain string.' ); 735 736 $this->assertSame( 1, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to have fired.' ); 737 $this->assertSame( 1, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' ); 738 $action_args = $mock_action_callback->get_args()[0]; 739 $this->assertCount( 1, $action_args, 'Expected the wp_send_late_headers action to have been passed only one argument.' ); 740 $this->assertSame( $processed_output, $action_args[0], 'Expected the arg passed to wp_send_late_headers to be the same as the processed output buffer.' ); 719 741 } 720 742 … … 723 745 * 724 746 * @ticket 43258 747 * @ticket 64126 748 * 725 749 * @covers ::wp_start_template_enhancement_output_buffer 726 750 * @covers ::wp_finalize_template_enhancement_output_buffer … … 730 754 ob_start(); 731 755 732 $ applied_filter = false;756 $mock_filter_callback = new MockAction(); 733 757 add_filter( 734 758 'wp_template_enhancement_output_buffer', 735 static function ( string $buffer ) use ( &$applied_filter ): string { 736 $applied_filter = true; 737 759 array( $mock_filter_callback, 'filter' ) 760 ); 761 762 add_filter( 763 'wp_template_enhancement_output_buffer', 764 static function ( string $buffer ): string { 738 765 $p = WP_HTML_Processor::create_full_parser( $buffer ); 739 766 if ( $p->next_tag( array( 'tag_name' => 'TITLE' ) ) ) { … … 742 769 return $p->get_updated_html(); 743 770 } 771 ); 772 773 $mock_action_callback = new MockAction(); 774 add_filter( 775 'wp_send_late_headers', 776 array( $mock_action_callback, 'action' ), 777 10, 778 PHP_INT_MAX 744 779 ); 745 780 … … 775 810 $this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' ); 776 811 777 $this->assertFalse( $applied_filter, 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' ); 778 $this->assertSame( 0, did_action( 'wp_final_template_output_buffer' ), 'Expected the wp_final_template_output_buffer action to not have fired.' ); 812 $this->assertSame( 0, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' ); 779 813 780 814 // Obtain the output via the wrapper output buffer. … … 784 818 $this->assertStringNotContainsString( '<title>Processed</title>', $output, 'Expected output buffer to not have string since the filter did not apply.' ); 785 819 $this->assertStringContainsString( '<title>Output Buffer Not Processed</title>', $output, 'Expected output buffer to have string since the output buffer was ended with cleaning.' ); 820 821 $this->assertSame( 0, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to not have fired.' ); 822 $this->assertSame( 0, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' ); 786 823 } 787 824 … … 790 827 * 791 828 * @ticket 43258 829 * @ticket 64126 830 * 792 831 * @covers ::wp_start_template_enhancement_output_buffer 793 832 * @covers ::wp_finalize_template_enhancement_output_buffer … … 797 836 ob_start(); 798 837 799 $ called_filter = false;838 $mock_filter_callback = new MockAction(); 800 839 add_filter( 801 840 'wp_template_enhancement_output_buffer', 802 static function ( string $buffer ) use ( &$called_filter ): string { 803 $called_filter = true; 804 841 array( $mock_filter_callback, 'filter' ) 842 ); 843 844 add_filter( 845 'wp_template_enhancement_output_buffer', 846 static function ( string $buffer ): string { 805 847 $p = WP_HTML_Processor::create_full_parser( $buffer ); 806 848 if ( $p->next_tag( array( 'tag_name' => 'TITLE' ) ) ) { … … 809 851 return $p->get_updated_html(); 810 852 } 853 ); 854 855 $mock_action_callback = new MockAction(); 856 add_filter( 857 'wp_send_late_headers', 858 array( $mock_action_callback, 'action' ), 859 10, 860 PHP_INT_MAX 811 861 ); 812 862 … … 847 897 $this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' ); 848 898 849 $this->assert True( $called_filter, 'Expected the wp_template_enhancement_output_buffer filter to have applied.' );899 $this->assertSame( 1, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to have applied.' ); 850 900 851 901 // Obtain the output via the wrapper output buffer. … … 855 905 $this->assertStringContainsString( '<title>Processed</title>', $output, 'Expected output buffer to have string due to filtering.' ); 856 906 $this->assertStringContainsString( '<h1>Template Replaced</h1>', $output, 'Expected output buffer to have string due to replaced template.' ); 907 908 $this->assertSame( 1, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to have fired.' ); 909 $this->assertSame( 1, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' ); 910 $action_args = $mock_action_callback->get_args()[0]; 911 $this->assertCount( 1, $action_args, 'Expected the wp_send_late_headers action to have been passed only one argument.' ); 912 $this->assertSame( $output, $action_args[0], 'Expected the arg passed to wp_send_late_headers to be the same as the processed output buffer.' ); 857 913 } 858 914 … … 861 917 * 862 918 * @ticket 43258 919 * @ticket 64126 920 * 863 921 * @covers ::wp_start_template_enhancement_output_buffer 864 922 * @covers ::wp_finalize_template_enhancement_output_buffer … … 870 928 $mock_filter_callback = new MockAction(); 871 929 add_filter( 'wp_template_enhancement_output_buffer', array( $mock_filter_callback, 'filter' ) ); 930 931 $mock_action_callback = new MockAction(); 932 add_filter( 933 'wp_send_late_headers', 934 array( $mock_action_callback, 'action' ), 935 10, 936 PHP_INT_MAX 937 ); 872 938 873 939 $initial_ob_level = ob_get_level(); … … 904 970 $this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' ); 905 971 $this->assertSame( $json, $output, 'Expected output to not be processed.' ); 972 973 $this->assertSame( 1, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to have fired even though the wp_template_enhancement_output_buffer filter did not apply.' ); 974 $this->assertSame( 1, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' ); 975 $action_args = $mock_action_callback->get_args()[0]; 976 $this->assertCount( 1, $action_args, 'Expected the wp_send_late_headers action to have been passed only one argument.' ); 977 $this->assertSame( $output, $action_args[0], 'Expected the arg passed to wp_send_late_headers to be the same as the processed output buffer.' ); 906 978 } 907 979
Note: See TracChangeset
for help on using the changeset viewer.