Нет описания

eventbrite.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Eventbrite Block.
  4. *
  5. * @since 8.2.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\Eventbrite;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack_Gutenberg;
  12. const FEATURE_NAME = 'eventbrite';
  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( 'render_callback' => __NAMESPACE__ . '\render_block' )
  23. );
  24. }
  25. add_action( 'init', __NAMESPACE__ . '\register_block' );
  26. /**
  27. * Get current URL.
  28. *
  29. * @return string Current URL.
  30. */
  31. function get_current_url() {
  32. if ( isset( $_SERVER['HTTP_HOST'] ) ) {
  33. $host = wp_unslash( $_SERVER['HTTP_HOST'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  34. } else {
  35. $host = wp_parse_url( home_url(), PHP_URL_HOST );
  36. }
  37. if ( isset( $_SERVER['REQUEST_URI'] ) ) {
  38. $path = wp_unslash( $_SERVER['REQUEST_URI'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  39. } else {
  40. $path = '/';
  41. }
  42. return esc_url_raw( ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $path );
  43. }
  44. /**
  45. * Eventbrite block registration/dependency delclaration.
  46. *
  47. * @param array $attr Eventbrite block attributes.
  48. * @param string $content Rendered embed element (without scripts) from the block editor.
  49. *
  50. * @return string Rendered block.
  51. */
  52. function render_block( $attr, $content ) {
  53. if ( is_admin() || empty( $attr['eventId'] ) || empty( $attr['url'] ) ) {
  54. return '';
  55. }
  56. $attr['url'] = Jetpack_Gutenberg::validate_block_embed_url(
  57. $attr['url'],
  58. array( '#^https?:\/\/(?:[0-9a-z]+\.)?eventbrite\.(?:com|co\.uk|com\.ar|com\.au|be|com\.br|ca|cl|co|dk|de|es|fi|fr|hk|ie|it|com\.mx|nl|co\.nz|at|com\.pe|pt|ch|sg|se)\/e\/[^\/]*?(?:\d+)\/?(?:\?[^\/]*)?$#' ),
  59. true
  60. );
  61. $widget_id = wp_unique_id( 'eventbrite-widget-' );
  62. // Show the embedded version.
  63. if ( empty( $attr['useModal'] ) && ( empty( $attr['style'] ) || 'modal' !== $attr['style'] ) ) {
  64. return render_embed_block( $widget_id, Blocks::is_amp_request(), $attr );
  65. } else {
  66. return render_modal_block( $widget_id, Blocks::is_amp_request(), $attr, $content );
  67. }
  68. }
  69. /**
  70. * Render block with embed style.
  71. *
  72. * @param string $widget_id Widget ID to use.
  73. * @param bool $is_amp Whether AMP page.
  74. * @param array $attr Eventbrite block attributes.
  75. * @return string Rendered block.
  76. */
  77. function render_embed_block( $widget_id, $is_amp, $attr ) {
  78. // $content contains a fallback link to the event that's saved in the post_content.
  79. // Append a div that will hold the iframe embed created by the Eventbrite widget.js.
  80. $classes = Blocks::classes( FEATURE_NAME, $attr );
  81. $classes .= ' wp-block-jetpack-eventbrite--embed';
  82. $direct_link = sprintf(
  83. '<a href="%s" rel="noopener noreferrer" target="_blank" class="eventbrite__direct-link" %s>%s</a>',
  84. esc_url( $attr['url'] ),
  85. $is_amp ? 'placeholder fallback' : '',
  86. esc_html__( 'Register on Eventbrite', 'jetpack' )
  87. );
  88. if ( $is_amp ) {
  89. $embed = sprintf(
  90. '<amp-iframe src="%s" layout="responsive" resizable width="1" height="1" sandbox="allow-scripts allow-same-origin allow-forms"><button overflow>%s</button>%s</amp-iframe>',
  91. esc_url(
  92. add_query_arg(
  93. array(
  94. 'eid' => $attr['eventId'],
  95. 'parent' => rawurlencode( get_current_url() ),
  96. ),
  97. 'https://www.eventbrite.com/checkout-external'
  98. )
  99. ),
  100. esc_html__( 'Expand', 'jetpack' ),
  101. $direct_link
  102. );
  103. } else {
  104. $embed = $direct_link;
  105. wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true );
  106. // Add CSS to hide direct link.
  107. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  108. wp_add_inline_script(
  109. 'eventbrite-widget',
  110. "window.EBWidgets.createWidget( {
  111. widgetType: 'checkout',
  112. eventId: " . absint( $attr['eventId'] ) . ",
  113. iframeContainerId: '" . esc_js( $widget_id ) . "',
  114. } );"
  115. );
  116. }
  117. return sprintf(
  118. '<div id="%1$s" class="%2$s">%3$s</div>',
  119. esc_attr( $widget_id ),
  120. esc_attr( $classes ),
  121. $embed
  122. );
  123. }
  124. /**
  125. * Render block with modal style.
  126. *
  127. * @param string $widget_id Widget ID to use.
  128. * @param bool $is_amp Whether AMP page.
  129. * @param array $attr Eventbrite block attributes.
  130. * @param string $content Rendered embed element (without scripts) from the block editor.
  131. * @return string Rendered block.
  132. */
  133. function render_modal_block( $widget_id, $is_amp, $attr, $content ) {
  134. if ( $is_amp ) {
  135. $lightbox_id = "{$widget_id}-lightbox";
  136. // Add CSS to for lightbox.
  137. Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
  138. $content = preg_replace(
  139. '/\shref="#" target="_blank/',
  140. sprintf( ' on="%s" ', esc_attr( "tap:{$lightbox_id}.open" ) ),
  141. $content
  142. );
  143. $iframe_src = add_query_arg(
  144. array(
  145. // Note that modal=1 is intentionally omitted here since we need to put the close button inside the amp-lightbox.
  146. 'eid' => $attr['eventId'],
  147. 'parent' => rawurlencode( get_current_url() ),
  148. ),
  149. 'https://www.eventbrite.com/checkout-external'
  150. );
  151. $lightbox = sprintf(
  152. '<amp-lightbox id="%1$s" on="%2$s" class="eventbrite__lightbox" layout="nodisplay">%3$s</amp-lightbox>',
  153. esc_attr( $lightbox_id ),
  154. esc_attr( "tap:{$lightbox_id}.close" ),
  155. sprintf(
  156. '
  157. <div class="eventbrite__lighbox-inside">
  158. <div class="eventbrite__lighbox-iframe-wrapper">
  159. <amp-iframe class="eventbrite__lighbox-iframe" src="%s" layout="fill" sandbox="allow-scripts allow-same-origin allow-forms">
  160. <span placeholder=""></span>
  161. </amp-iframe>
  162. <span class="eventbrite__lighbox-close" on="%s" role="button" tabindex="0" aria-label="%s">
  163. <svg viewBox="0 0 24 24">
  164. <path d="M13.4 12l3.5-3.5-1.4-1.4-3.5 3.5-3.5-3.5-1.4 1.4 3.5 3.5-3.5 3.5 1.4 1.4 3.5-3.5 3.5 3.5 1.4-1.4z"></path>
  165. </svg>
  166. </span>
  167. </div>
  168. </div>
  169. ',
  170. esc_url( $iframe_src ),
  171. esc_attr( "tap:{$lightbox_id}.close" ),
  172. esc_attr__( 'Close', 'jetpack' )
  173. )
  174. );
  175. $content = preg_replace(
  176. ':(?=</div>\s*$):',
  177. $lightbox,
  178. $content
  179. );
  180. return $content;
  181. }
  182. wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true );
  183. // Show the modal version.
  184. wp_add_inline_script(
  185. 'eventbrite-widget',
  186. "window.EBWidgets.createWidget( {
  187. widgetType: 'checkout',
  188. eventId: " . absint( $attr['eventId'] ) . ",
  189. modal: true,
  190. modalTriggerElementId: '" . esc_js( $widget_id ) . "',
  191. } );"
  192. );
  193. // Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed
  194. // by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior.
  195. // @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html.
  196. wp_add_inline_script(
  197. 'eventbrite-widget',
  198. "( function() {
  199. var widget = document.getElementById( '" . esc_js( $widget_id ) . "' );
  200. if ( widget ) {
  201. widget.addEventListener( 'click', function( event ) {
  202. event.preventDefault();
  203. } );
  204. widget.addEventListener( 'keydown', function( event ) {
  205. // Enter and space keys.
  206. if ( event.keyCode === 13 || event.keyCode === 32 ) {
  207. event.preventDefault();
  208. event.target && event.target.click();
  209. }
  210. } );
  211. }
  212. } )();"
  213. );
  214. // Replace the placeholder id saved in the post_content with a unique id used by widget.js.
  215. $content = str_replace( 'eventbrite-widget-id', esc_attr( $widget_id ), $content );
  216. // Fallback for block version deprecated/v2.
  217. $content = preg_replace( '/eventbrite-widget-\d+/', esc_attr( $widget_id ), $content );
  218. // Inject URL to event in case the JS for the lightbox fails to load.
  219. $content = preg_replace(
  220. '/\shref="#"/',
  221. sprintf(
  222. ' href="%s" rel="noopener noreferrer" target="_blank"',
  223. esc_url( $attr['url'] )
  224. ),
  225. $content
  226. );
  227. return $content;
  228. }