-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Use output buffer and HTML tag processor to inject directives on BODY tag for full-page client-side navigation #61212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bc71592
8bd0a3c
29eeed4
36fa0d2
300d9e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… buffering
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,16 +42,57 @@ function _gutenberg_add_enhanced_pagination_to_query_block( $parsed_block ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return array The same block content with the directives needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function _gutenberg_add_client_side_navigation_directives( $content ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $p = new WP_HTML_Tag_Processor( $content ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Hack to add the necessary directives to the body tag. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Find a proper way to add directives to the body tag. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static $body_interactive_added; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! $body_interactive_added ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $body_interactive_added = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (string) $p . '<body data-wp-interactive="core/experimental" data-wp-context="{}">'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'gutenberg_template_output_buffer', static function ( $html ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $p = new WP_HTML_Tag_Processor( $html ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( $p->next_tag( array( 'tag_name' => 'BODY' ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $p->set_attribute( 'data-wp-interactive', 'core/experimental' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $p->set_attribute( 'data-wp-context', '{}' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $html = $p->get_updated_html(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $html; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (string) $p; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $content; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Explore moving this to the server directive processing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'render_block', '_gutenberg_add_client_side_navigation_directives' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Starts output buffering at the end of the 'template_include' filter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * This is to implement #43258 in core. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * This is a hack which would eventually be replaced with something like this in wp-includes/template-loader.php: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * $template = apply_filters( 'template_include', $template ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * + ob_start( 'wp_template_output_buffer_callback' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * if ( $template ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * include $template; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * } elseif ( current_user_can( 'switch_themes' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 0.1.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @link https://core.trac.wordpress.org/ticket/43258 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $passthrough Value for the template_include filter which is passed through. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return string Unmodified value of $passthrough. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function _gutenberg_buffer_template_output( string $passthrough ): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ob_start( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static function ( string $output ): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Filters the template output buffer prior to sending to the client. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $output Output buffer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return string Filtered output buffer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (string) apply_filters( 'gutenberg_template_output_buffer', $output ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+88
to
+98
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on WordPress/performance#1317, I believe this should now rather be improved as follows:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $passthrough; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'template_include', '_gutenberg_buffer_template_output', PHP_INT_MAX ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't an action just a filter whose return value is ignored? we could eliminate the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, function add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
return add_filter( $hook_name, $callback, $priority, $accepted_args );
}So if the callback doesn't return a value, then the filter is borked. I just tested: diff --git a/lib/experimental/full-page-client-side-navigation.php b/lib/experimental/full-page-client-side-navigation.php
index 259517cfc84..452abee6c63 100644
--- a/lib/experimental/full-page-client-side-navigation.php
+++ b/lib/experimental/full-page-client-side-navigation.php
@@ -68,12 +68,8 @@ add_filter( 'gutenberg_template_output_buffer', '_gutenberg_add_client_side_navi
* } elseif ( current_user_can( 'switch_themes' ) ) {
*
* @link https://core.trac.wordpress.org/ticket/43258
- *
- * @param string $passthrough Value for the template_include filter which is passed through.
- *
- * @return string Unmodified value of $passthrough.
*/
-function _gutenberg_buffer_template_output( string $passthrough ): string {
+function _gutenberg_buffer_template_output() {
ob_start(
static function ( string $output ): string {
/**
@@ -85,6 +81,5 @@ function _gutenberg_buffer_template_output( string $passthrough ): string {
return (string) apply_filters( 'gutenberg_template_output_buffer', $output );
}
);
- return $passthrough;
}
-add_filter( 'template_include', '_gutenberg_buffer_template_output', PHP_INT_MAX );
+add_action( 'template_include', '_gutenberg_buffer_template_output', PHP_INT_MAX );The result is an empty page because the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. darn. you've shattered my understanding of this. I guess |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.