暂无描述

opentable.php 6.8KB

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