__NAMESPACE__ . '\render_block' ) ); } add_action( 'init', __NAMESPACE__ . '\register_block' ); /** * Get current URL. * * @return string Current URL. */ function get_current_url() { if ( isset( $_SERVER['HTTP_HOST'] ) ) { $host = wp_unslash( $_SERVER['HTTP_HOST'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } else { $host = wp_parse_url( home_url(), PHP_URL_HOST ); } if ( isset( $_SERVER['REQUEST_URI'] ) ) { $path = wp_unslash( $_SERVER['REQUEST_URI'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } else { $path = '/'; } return esc_url_raw( ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $path ); } /** * Eventbrite block registration/dependency delclaration. * * @param array $attr Eventbrite block attributes. * @param string $content Rendered embed element (without scripts) from the block editor. * * @return string Rendered block. */ function render_block( $attr, $content ) { if ( is_admin() || empty( $attr['eventId'] ) || empty( $attr['url'] ) ) { return ''; } $attr['url'] = Jetpack_Gutenberg::validate_block_embed_url( $attr['url'], 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+)\/?(?:\?[^\/]*)?$#' ), true ); $widget_id = wp_unique_id( 'eventbrite-widget-' ); // Show the embedded version. if ( empty( $attr['useModal'] ) && ( empty( $attr['style'] ) || 'modal' !== $attr['style'] ) ) { return render_embed_block( $widget_id, Blocks::is_amp_request(), $attr ); } else { return render_modal_block( $widget_id, Blocks::is_amp_request(), $attr, $content ); } } /** * Render block with embed style. * * @param string $widget_id Widget ID to use. * @param bool $is_amp Whether AMP page. * @param array $attr Eventbrite block attributes. * @return string Rendered block. */ function render_embed_block( $widget_id, $is_amp, $attr ) { // $content contains a fallback link to the event that's saved in the post_content. // Append a div that will hold the iframe embed created by the Eventbrite widget.js. $classes = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ); $classes .= ' wp-block-jetpack-eventbrite--embed'; $direct_link = sprintf( '%s', esc_url( $attr['url'] ), $is_amp ? 'placeholder fallback' : '', esc_html__( 'Register on Eventbrite', 'jetpack' ) ); if ( $is_amp ) { $embed = sprintf( '%s', esc_url( add_query_arg( array( 'eid' => $attr['eventId'], 'parent' => rawurlencode( get_current_url() ), ), 'https://www.eventbrite.com/checkout-external' ) ), esc_html__( 'Expand', 'jetpack' ), $direct_link ); } else { $embed = $direct_link; wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true ); // Add CSS to hide direct link. Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); wp_add_inline_script( 'eventbrite-widget', "window.EBWidgets.createWidget( { widgetType: 'checkout', eventId: " . absint( $attr['eventId'] ) . ", iframeContainerId: '" . esc_js( $widget_id ) . "', } );" ); } return sprintf( '
%3$s
', esc_attr( $widget_id ), esc_attr( $classes ), $embed ); } /** * Render block with modal style. * * @param string $widget_id Widget ID to use. * @param bool $is_amp Whether AMP page. * @param array $attr Eventbrite block attributes. * @param string $content Rendered embed element (without scripts) from the block editor. * @return string Rendered block. */ function render_modal_block( $widget_id, $is_amp, $attr, $content ) { if ( $is_amp ) { $lightbox_id = "{$widget_id}-lightbox"; // Add CSS to for lightbox. Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); $content = preg_replace( '/\shref="#" target="_blank/', sprintf( ' on="%s" ', esc_attr( "tap:{$lightbox_id}.open" ) ), $content ); $iframe_src = add_query_arg( array( // Note that modal=1 is intentionally omitted here since we need to put the close button inside the amp-lightbox. 'eid' => $attr['eventId'], 'parent' => rawurlencode( get_current_url() ), ), 'https://www.eventbrite.com/checkout-external' ); $lightbox = sprintf( '%3$s', esc_attr( $lightbox_id ), esc_attr( "tap:{$lightbox_id}.close" ), sprintf( '
', esc_url( $iframe_src ), esc_attr( "tap:{$lightbox_id}.close" ), esc_attr__( 'Close', 'jetpack' ) ) ); $content = preg_replace( ':(?=\s*$):', $lightbox, $content ); return $content; } wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true ); // Show the modal version. wp_add_inline_script( 'eventbrite-widget', "window.EBWidgets.createWidget( { widgetType: 'checkout', eventId: " . absint( $attr['eventId'] ) . ", modal: true, modalTriggerElementId: '" . esc_js( $widget_id ) . "', } );" ); // Modal button is saved as an `` element with `role="button"` because `