Skip to content

Commit

Permalink
Ensure client-side directives are only added to HTML response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Apr 29, 2024
1 parent 36fa0d2 commit 04171bf
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/experimental/full-page-client-side-navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,38 @@ function _gutenberg_add_enhanced_pagination_to_query_block( $parsed_block ) {

add_filter( 'render_block_data', '_gutenberg_add_enhanced_pagination_to_query_block' );

add_action( 'send_headers', static function() {

Check failure on line 35 in lib/experimental/full-page-client-side-navigation.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Opening parenthesis of a multi-line function call must be the last content on the line

Check failure on line 35 in lib/experimental/full-page-client-side-navigation.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Only one argument is allowed per line in a multi-line function call

Check failure on line 35 in lib/experimental/full-page-client-side-navigation.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after FUNCTION keyword; 0 found
// header('content-type: text/plain' );

Check failure on line 36 in lib/experimental/full-page-client-side-navigation.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 1 tabs, found 0

Check failure on line 36 in lib/experimental/full-page-client-side-navigation.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Spaces must be used for mid-line alignment; tabs are not allowed

Check failure on line 36 in lib/experimental/full-page-client-side-navigation.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Multi-line function call not indented correctly; expected 4 spaces but found 0
} );

Check failure on line 37 in lib/experimental/full-page-client-side-navigation.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Closing parenthesis of a multi-line function call must be on a line by itself

/**
* Adds client-side navigation directives to BODY tag.
*
* Note: This should probably be done per site, not by default when this option is enabled.
*
* @param string $html The rendered template.
* @param string $response_body The response body.
*
* @return string The rendered template with modified BODY attributes.
*/
function _gutenberg_add_client_side_navigation_directives( $html ) {
$p = new WP_HTML_Tag_Processor( $html );
function _gutenberg_add_client_side_navigation_directives( $response_body ) {
$is_html_content_type = false;
foreach ( headers_list() as $header ) {
$header_parts = preg_split( '/\s*[:;]\s*/', strtolower( $header ) );
if ( count( $header_parts ) >= 2 && 'content-type' === $header_parts[0] ) {
$is_html_content_type = in_array( $header_parts[1], array( 'text/html', 'application/xhtml+xml' ), true );
}
}
if ( ! $is_html_content_type ) {
return $response_body;
}

$p = new WP_HTML_Tag_Processor( $response_body );
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();
$response_body = $p->get_updated_html();
}
return $html;
return $response_body;
}

// TODO: Explore moving this to the server directive processing.
Expand Down

0 comments on commit 04171bf

Please sign in to comment.