diff options
Diffstat (limited to 'plugins/jetpack/extensions/blocks/opentable/opentable.php')
-rw-r--r-- | plugins/jetpack/extensions/blocks/opentable/opentable.php | 137 |
1 files changed, 72 insertions, 65 deletions
diff --git a/plugins/jetpack/extensions/blocks/opentable/opentable.php b/plugins/jetpack/extensions/blocks/opentable/opentable.php index 1ea9cef1..e92f9308 100644 --- a/plugins/jetpack/extensions/blocks/opentable/opentable.php +++ b/plugins/jetpack/extensions/blocks/opentable/opentable.php @@ -4,71 +4,32 @@ * * @since 8.2 * - * @package Jetpack + * @package automattic/jetpack */ -namespace Jetpack\OpenTable_Block; +namespace Automattic\Jetpack\Extensions\OpenTable; + +use Automattic\Jetpack\Blocks; +use Jetpack_Gutenberg; const FEATURE_NAME = 'opentable'; const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; /** - * Check if the block should be available on the site. - * - * @return bool - */ -function is_available() { - if ( - defined( 'IS_WPCOM' ) - && IS_WPCOM - && function_exists( 'has_any_blog_stickers' ) - ) { - if ( has_any_blog_stickers( - array( 'premium-plan', 'business-plan', 'ecommerce-plan' ), - get_current_blog_id() - ) ) { - return true; - } - return false; - } - - return true; -} - -/** * Registers the block for use in Gutenberg * This is done via an action so that we can disable * registration if we need to. */ function register_block() { - if ( is_available() ) { - jetpack_register_block( - BLOCK_NAME, - array( 'render_callback' => 'Jetpack\OpenTable_Block\load_assets' ) - ); - } -} -add_action( 'init', 'Jetpack\OpenTable_Block\register_block' ); - -/** - * Set the availability of the block as the editor - * is loaded. - */ -function set_availability() { - if ( is_available() ) { - \Jetpack_Gutenberg::set_extension_available( BLOCK_NAME ); - } else { - \Jetpack_Gutenberg::set_extension_unavailable( - BLOCK_NAME, - 'missing_plan', - array( - 'required_feature' => 'opentable', - 'required_plan' => 'premium-plan', - ) - ); - } + Blocks::jetpack_register_block( + BLOCK_NAME, + array( + 'render_callback' => __NAMESPACE__ . '\load_assets', + 'plan_check' => true, + ) + ); } -add_action( 'jetpack_register_gutenberg_extensions', 'Jetpack\OpenTable_Block\set_availability' ); +add_action( 'init', __NAMESPACE__ . '\register_block' ); /** * Adds an inline script which updates the block editor settings to @@ -79,7 +40,7 @@ add_action( 'jetpack_register_gutenberg_extensions', 'Jetpack\OpenTable_Block\se function add_language_setting() { wp_add_inline_script( 'jetpack-blocks-editor', sprintf( "wp.data.dispatch( 'core/block-editor' ).updateSettings( { siteLocale: '%s' } )", str_replace( '_', '-', get_locale() ) ), 'before' ); } -add_action( 'enqueue_block_assets', 'Jetpack\OpenTable_Block\add_language_setting' ); +add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_language_setting' ); /** * OpenTable block registration/dependency declaration. @@ -89,23 +50,69 @@ add_action( 'enqueue_block_assets', 'Jetpack\OpenTable_Block\add_language_settin * @return string */ function load_assets( $attributes ) { - \Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); - $classes = array( sprintf( 'wp-block-jetpack-%s-theme-%s', FEATURE_NAME, get_attribute( $attributes, 'style' ) ) ); - if ( count( $attributes['rid'] ) > 1 ) { + Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); + + $classes = array(); + $class_name = get_attribute( $attributes, 'className' ); + $style = get_attribute( $attributes, 'style' ); + + if ( 'wide' === $style && jetpack_is_mobile() ) { + $attributes = array_merge( $attributes, array( 'style' => 'standard' ) ); + $classes[] = 'is-style-mobile'; + } + + // Handles case of deprecated version using theme instead of block styles. + if ( ! $class_name || strpos( $class_name, 'is-style-' ) === false ) { + $classes[] = sprintf( 'is-style-%s', $style ); + } + + if ( array_key_exists( 'rid', $attributes ) && is_array( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) { $classes[] = 'is-multi'; } - $classes = \Jetpack_Gutenberg::block_classes( - FEATURE_NAME, - $attributes, - $classes - ); + if ( array_key_exists( 'negativeMargin', $attributes ) && $attributes['negativeMargin'] ) { + $classes[] = 'has-no-margin'; + } + $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; } |