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