| @@ -1,0 +1,215 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * OpenTable block render implementation. | |
| 4 | + * | |
| 5 | + * Loaded lazily from opentable.php only when the block is rendered, to keep the | |
| 6 | + * render body out of the eager front-end PHP/opcache footprint. | |
| 7 | + * | |
| 8 | + * @package automattic/jetpack | |
| 9 | + */ | |
| 10 | + | |
| 11 | +namespace Automattic\Jetpack\Extensions\OpenTable; | |
| 12 | + | |
| 13 | +use Automattic\Jetpack\Blocks; | |
| 14 | +use Jetpack_Gutenberg; | |
| 15 | + | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit( 0 ); | |
| 18 | +} | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * OpenTable block render. | |
| 22 | + * | |
| 23 | + * @param array $attributes Array containing the OpenTable block attributes. | |
| 24 | + * | |
| 25 | + * @return string | |
| 26 | + */ | |
| 27 | +function render( $attributes ) { | |
| 28 | + | |
| 29 | + Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 30 | + | |
| 31 | + $classes = array(); | |
| 32 | + $class_name = get_attribute( $attributes, 'className' ); | |
| 33 | + $style = get_attribute( $attributes, 'style' ); | |
| 34 | + | |
| 35 | + if ( 'wide' === $style && jetpack_is_mobile() ) { | |
| 36 | + $attributes = array_merge( $attributes, array( 'style' => 'standard' ) ); | |
| 37 | + $classes[] = 'is-style-mobile'; | |
| 38 | + } | |
| 39 | + | |
| 40 | + // Handles case of deprecated version using theme instead of block styles. | |
| 41 | + if ( ! $class_name || ! str_contains( $class_name, 'is-style-' ) ) { | |
| 42 | + $classes[] = sprintf( 'is-style-%s', $style ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + if ( array_key_exists( 'rid', $attributes ) && is_array( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) { | |
| 46 | + $classes[] = 'is-multi'; | |
| 47 | + } | |
| 48 | + if ( array_key_exists( 'negativeMargin', $attributes ) && $attributes['negativeMargin'] ) { | |
| 49 | + $classes[] = 'has-no-margin'; | |
| 50 | + } | |
| 51 | + $classes = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attributes, $classes ); | |
| 52 | + $content = '<div class="' . esc_attr( $classes ) . '">'; | |
| 53 | + | |
| 54 | + $script_url = build_embed_url( $attributes ); | |
| 55 | + | |
| 56 | + if ( Blocks::is_amp_request() ) { | |
| 57 | + // Extract params from URL since it had jetpack_opentable_block_url filters applied. | |
| 58 | + $url_query = \wp_parse_url( $script_url, PHP_URL_QUERY ) . '&overlay=false&disablega=false'; | |
| 59 | + | |
| 60 | + $src = "https://www.opentable.com/widget/reservation/canvas?$url_query"; | |
| 61 | + | |
| 62 | + $params = array(); | |
| 63 | + wp_parse_str( $url_query, $params ); | |
| 64 | + | |
| 65 | + // Note an iframe is similarly constructed in the block edit function. | |
| 66 | + $content .= sprintf( | |
| 67 | + '<amp-iframe src="%s" layout="fill" sandbox="allow-scripts allow-forms allow-same-origin allow-popups">%s</amp-iframe>', | |
| 68 | + esc_url( $src ), | |
| 69 | + sprintf( | |
| 70 | + '<a placeholder href="%s">%s</a>', | |
| 71 | + esc_url( | |
| 72 | + add_query_arg( | |
| 73 | + array( | |
| 74 | + 'rid' => $params['rid'], | |
| 75 | + ), | |
| 76 | + 'https://www.opentable.com/restref/client/' | |
| 77 | + ) | |
| 78 | + ), | |
| 79 | + esc_html__( 'Make a reservation', 'jetpack' ) | |
| 80 | + ) | |
| 81 | + ); | |
| 82 | + } else { | |
| 83 | + /* | |
| 84 | + * The OpenTable script uses multiple `rid` paramters, | |
| 85 | + * so we can't use WordPress to output it, as WordPress attempts to validate it and removes them. | |
| 86 | + */ | |
| 87 | + // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript | |
| 88 | + $content .= '<script src="' . esc_url( $script_url ) . '"></script>'; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $content .= '</div>'; | |
| 92 | + | |
| 93 | + return $content; | |
| 94 | +} | |
| 95 | + | |
| 96 | +/** | |
| 97 | + * Get the a block attribute | |
| 98 | + * | |
| 99 | + * @param array $attributes Array of block attributes. | |
| 100 | + * @param string $attribute_name The attribute to get. | |
| 101 | + * | |
| 102 | + * @return string|null The filtered attribute | |
| 103 | + */ | |
| 104 | +function get_attribute( $attributes, $attribute_name ) { | |
| 105 | + if ( isset( $attributes[ $attribute_name ] ) ) { | |
| 106 | + if ( in_array( $attribute_name, array( 'iframe', 'newtab' ), true ) ) { | |
| 107 | + return $attributes[ $attribute_name ] ? 'true' : 'false'; | |
| 108 | + } | |
| 109 | + return $attributes[ $attribute_name ]; | |
| 110 | + } | |
| 111 | + | |
| 112 | + $default_attributes = array( | |
| 113 | + 'style' => 'standard', | |
| 114 | + 'iframe' => 'true', | |
| 115 | + 'domain' => 'com', | |
| 116 | + 'lang' => 'en-US', | |
| 117 | + 'newtab' => 'false', | |
| 118 | + ); | |
| 119 | + | |
| 120 | + return $default_attributes[ $attribute_name ] ?? null; | |
| 121 | +} | |
| 122 | + | |
| 123 | +/** | |
| 124 | + * Get the block type attribute | |
| 125 | + * | |
| 126 | + * @param array $attributes Array of block attributes. | |
| 127 | + * | |
| 128 | + * @return string The filtered attribute | |
| 129 | + */ | |
| 130 | +function get_type_attribute( $attributes ) { | |
| 131 | + if ( ! empty( $attributes['rid'] ) && is_countable( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) { | |
| 132 | + return 'multi'; | |
| 133 | + } | |
| 134 | + | |
| 135 | + if ( empty( $attributes['style'] ) || 'button' !== $attributes['style'] ) { | |
| 136 | + return 'standard'; | |
| 137 | + } | |
| 138 | + | |
| 139 | + return 'button'; | |
| 140 | +} | |
| 141 | + | |
| 142 | +/** | |
| 143 | + * Get the block theme attribute | |
| 144 | + * | |
| 145 | + * OpenTable has a confusing mix of themes and types for the widget. A type | |
| 146 | + * can have a theme, but the button style cannot have a theme. The other two | |
| 147 | + * types (multi and standard) can have one of the three themes. | |
| 148 | + * | |
| 149 | + * We have combined these into a `style` attribute as really there are 4 styles | |
| 150 | + * standard, wide, tall, and button. Multi can be determined by the number of | |
| 151 | + * restaurant IDs we have. | |
| 152 | + * | |
| 153 | + * This function along with `jetpack_opentable_block_get_type_attribute`, translates | |
| 154 | + * the style attribute to a type and theme. | |
| 155 | + * | |
| 156 | + * Type Theme Style | |
| 157 | + * ==========|==========|========== | |
| 158 | + * Multi | | | |
| 159 | + * Standard | Standard | Standard | |
| 160 | + * | Wide | Wide | |
| 161 | + * | Tall | Tall | |
| 162 | + * Button | Standard | Button | |
| 163 | + * | |
| 164 | + * @param array $attributes Array of block attributes. | |
| 165 | + * | |
| 166 | + * @return string The filtered attribute | |
| 167 | + */ | |
| 168 | +function get_theme_attribute( $attributes ) { | |
| 169 | + $valid_themes = array( 'standard', 'wide', 'tall' ); | |
| 170 | + | |
| 171 | + if ( empty( $attributes['style'] ) | |
| 172 | + || ! in_array( $attributes['style'], $valid_themes, true ) | |
| 173 | + || 'button' === $attributes['style'] ) { | |
| 174 | + return 'standard'; | |
| 175 | + } | |
| 176 | + | |
| 177 | + return $attributes['style']; | |
| 178 | +} | |
| 179 | + | |
| 180 | +/** | |
| 181 | + * Build an embed URL from an array of block attributes. | |
| 182 | + * | |
| 183 | + * @param array $attributes Array of block attributess. | |
| 184 | + * | |
| 185 | + * @return string Embed URL | |
| 186 | + */ | |
| 187 | +function build_embed_url( $attributes ) { | |
| 188 | + $url = add_query_arg( | |
| 189 | + array( | |
| 190 | + 'type' => get_type_attribute( $attributes ), | |
| 191 | + 'theme' => get_theme_attribute( $attributes ), | |
| 192 | + 'iframe' => get_attribute( $attributes, 'iframe' ), | |
| 193 | + 'domain' => get_attribute( $attributes, 'domain' ), | |
| 194 | + 'lang' => get_attribute( $attributes, 'lang' ), | |
| 195 | + 'newtab' => get_attribute( $attributes, 'newtab' ), | |
| 196 | + ), | |
| 197 | + '//www.opentable.com/widget/reservation/loader' | |
| 198 | + ); | |
| 199 | + | |
| 200 | + if ( ! empty( $attributes['rid'] ) ) { | |
| 201 | + foreach ( $attributes['rid'] as $rid ) { | |
| 202 | + $url .= '&rid=' . $rid; | |
| 203 | + } | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * Filter the OpenTable URL used to embed a widget. | |
| 208 | + * | |
| 209 | + * @since 8.2.0 | |
| 210 | + * | |
| 211 | + * @param string $url OpenTable embed URL. | |
| 212 | + * @param array $attributes Array of block attributes. | |
| 213 | + */ | |
| 214 | + return apply_filters( 'jetpack_opentable_block_url', $url, $attributes ); | |
| 215 | +} | |