Skip to content
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

Add AMP compatibility for OpenTable block #17085

Merged
merged 7 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions extensions/blocks/opentable/opentable.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,41 @@ function load_assets( $attributes ) {
$classes = Blocks::classes( FEATURE_NAME, $attributes, $classes );
$content = '<div class="' . esc_attr( $classes ) . '">';

// The OpenTable script uses multiple `rid` paramters,
// so we can't use WordPress to output it, as WordPress attempts to validate it and removes them.
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$content .= '<script type="text/javascript" src="' . esc_url( build_embed_url( $attributes ) ) . '"></script>';
$script_url = build_embed_url( $attributes );

if ( Blocks::is_amp_request() ) {
// Extract params from URL since it had jetpack_opentable_block_url filters applied.
$url_query = \wp_parse_url( $script_url, PHP_URL_QUERY ) . '&overlay=false&disablega=false';

$src = "https://www.opentable.com/widget/reservation/canvas?$url_query";

$params = array();
wp_parse_str( $url_query, $params );

// Note an iframe is similarly constructed in the block edit function.
$content .= sprintf(
'<amp-iframe src="%s" layout="fill" sandbox="allow-scripts allow-forms allow-same-origin allow-popups">%s</amp-iframe>',
esc_url( $src ),
sprintf(
'<a placeholder href="%s">%s</a>',
esc_url(
add_query_arg(
array(
'rid' => $params['rid'],
),
'https://www.opentable.com/restref/client/'
)
),
esc_html__( 'Make a reservation', 'jetpack' )
)
);
} else {
// The OpenTable script uses multiple `rid` paramters,
// so we can't use WordPress to output it, as WordPress attempts to validate it and removes them.
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$content .= '<script src="' . esc_url( $script_url ) . '"></script>';
}

$content .= '</div>';

return $content;
Expand Down
6 changes: 6 additions & 0 deletions extensions/blocks/opentable/view.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.wp-block-jetpack-opentable {
position: relative; /* For sake of amp-iframe with layout=fill */
jeherve marked this conversation as resolved.
Show resolved Hide resolved

> iframe {
background: transparent; /* Override AMP Reader mode gray background for iframes in legacy post templates. */
margin: 0; /* Override margins in AMP legacy Reader mode. */
}

&.aligncenter iframe {
margin: 0 auto;
Expand Down