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