eventbrite.php
7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* Eventbrite Block.
*
* @since 8.2.0
*
* @package automattic/jetpack
*/
namespace Automattic\Jetpack\Extensions\Eventbrite;
use Automattic\Jetpack\Blocks;
use Jetpack_Gutenberg;
const FEATURE_NAME = 'eventbrite';
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
/**
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
* registration if we need to.
*/
function register_block() {
Blocks::jetpack_register_block(
BLOCK_NAME,
array( 'render_callback' => __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( FEATURE_NAME, $attr );
$classes .= ' wp-block-jetpack-eventbrite--embed';
$direct_link = sprintf(
'<a href="%s" rel="noopener noreferrer" target="_blank" class="eventbrite__direct-link" %s>%s</a>',
esc_url( $attr['url'] ),
$is_amp ? 'placeholder fallback' : '',
esc_html__( 'Register on Eventbrite', 'jetpack' )
);
if ( $is_amp ) {
$embed = sprintf(
'<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>',
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( FEATURE_NAME );
wp_add_inline_script(
'eventbrite-widget',
"window.EBWidgets.createWidget( {
widgetType: 'checkout',
eventId: " . absint( $attr['eventId'] ) . ",
iframeContainerId: '" . esc_js( $widget_id ) . "',
} );"
);
}
return sprintf(
'<div id="%1$s" class="%2$s">%3$s</div>',
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( FEATURE_NAME );
$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(
'<amp-lightbox id="%1$s" on="%2$s" class="eventbrite__lightbox" layout="nodisplay">%3$s</amp-lightbox>',
esc_attr( $lightbox_id ),
esc_attr( "tap:{$lightbox_id}.close" ),
sprintf(
'
<div class="eventbrite__lighbox-inside">
<div class="eventbrite__lighbox-iframe-wrapper">
<amp-iframe class="eventbrite__lighbox-iframe" src="%s" layout="fill" sandbox="allow-scripts allow-same-origin allow-forms">
<span placeholder=""></span>
</amp-iframe>
<span class="eventbrite__lighbox-close" on="%s" role="button" tabindex="0" aria-label="%s">
<svg viewBox="0 0 24 24">
<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>
</svg>
</span>
</div>
</div>
',
esc_url( $iframe_src ),
esc_attr( "tap:{$lightbox_id}.close" ),
esc_attr__( 'Close', 'jetpack' )
)
);
$content = preg_replace(
':(?=</div>\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 `<a>` element with `role="button"` because `<button>` is not allowed
// by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior.
// @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html.
wp_add_inline_script(
'eventbrite-widget',
"( function() {
var widget = document.getElementById( '" . esc_js( $widget_id ) . "' );
if ( widget ) {
widget.addEventListener( 'click', function( event ) {
event.preventDefault();
} );
widget.addEventListener( 'keydown', function( event ) {
// Enter and space keys.
if ( event.keyCode === 13 || event.keyCode === 32 ) {
event.preventDefault();
event.target && event.target.click();
}
} );
}
} )();"
);
// Replace the placeholder id saved in the post_content with a unique id used by widget.js.
$content = str_replace( 'eventbrite-widget-id', esc_attr( $widget_id ), $content );
// Fallback for block version deprecated/v2.
$content = preg_replace( '/eventbrite-widget-\d+/', esc_attr( $widget_id ), $content );
// Inject URL to event in case the JS for the lightbox fails to load.
$content = preg_replace(
'/\shref="#"/',
sprintf(
' href="%s" rel="noopener noreferrer" target="_blank"',
esc_url( $attr['url'] )
),
$content
);
return $content;
}