Completed
Push — fix/package-release-script ( 8004a7...315d04 )
by
unknown
06:57
created

eventbrite.php ➔ jetpack_render_eventbrite_block()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 2
dl 0
loc 77
rs 8.1907
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Eventbrite Block.
4
 *
5
 * @since 8.2.0
6
 *
7
 * @package Jetpack
8
 */
9
10
jetpack_register_block(
11
	'jetpack/eventbrite',
12
	array(
13
		'render_callback' => 'jetpack_render_eventbrite_block',
14
	)
15
);
16
17
/**
18
 * Eventbrite block registration/dependency delclaration.
19
 *
20
 * @param array  $attr    Eventbrite block attributes.
21
 * @param string $content Rendered embed element (without scripts) from the block editor.
22
 *
23
 * @return string
24
 */
25
function jetpack_render_eventbrite_block( $attr, $content ) {
26
	if ( is_admin() || empty( $attr['eventId'] ) || empty( $attr['url'] ) ) {
27
		return '';
28
	}
29
30
	$widget_id = wp_unique_id( 'eventbrite-widget-' );
31
32
	wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true );
33
34
	// Add CSS to hide direct link.
35
	Jetpack_Gutenberg::load_assets_as_required( 'eventbrite' );
36
37
	// Show the embedded version.
38
	if ( empty( $attr['useModal'] ) ) {
39
		wp_add_inline_script(
40
			'eventbrite-widget',
41
			"window.EBWidgets.createWidget( {
42
				widgetType: 'checkout',
43
				eventId: " . absint( $attr['eventId'] ) . ",
44
				iframeContainerId: '" . esc_js( $widget_id ) . "',
45
			} );"
46
		);
47
48
		// $content contains a fallback link to the event that's saved in the post_content.
49
		// Append a div that will hold the iframe embed created by the Eventbrite widget.js.
50
		$content .= sprintf(
51
			'<div id="%s" class="eventbrite__in-page-checkout"></div>',
52
			esc_attr( $widget_id )
53
		);
54
55
		return sprintf(
56
			'%s<noscript><a href="%s" rel="noopener noreferrer" target="_blank">%s</a></noscript>',
57
			$content,
58
			esc_url( $attr['url'] ),
59
			esc_html__( 'Register on Eventbrite', 'jetpack' )
60
		);
61
	}
62
63
	// Show the modal version.
64
	wp_add_inline_script(
65
		'eventbrite-widget',
66
		"window.EBWidgets.createWidget( {
67
			widgetType: 'checkout',
68
			eventId: " . absint( $attr['eventId'] ) . ",
69
			modal: true,
70
			modalTriggerElementId: '" . esc_js( $widget_id ) . "',
71
		} );"
72
	);
73
74
	// Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed
75
	// by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior.
76
	// @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html.
77
	wp_add_inline_script(
78
		'eventbrite-widget',
79
		"( function() {
80
			var widget = document.getElementById( '" . esc_js( $widget_id ) . "' );
81
			if ( widget ) {
82
				widget.addEventListener( 'click', function( event ) {
83
					event.preventDefault();
84
				} );
85
86
				widget.addEventListener( 'keydown', function( event ) {
87
					// Enter and space keys.
88
					if ( event.keyCode === 13 || event.keyCode === 32 ) {
89
						event.preventDefault();
90
						event.target && event.target.click();
91
					}
92
				} );
93
			}
94
		} )();"
95
	);
96
97
	// Replace the placeholder id saved in the post_content with a unique id used by widget.js.
98
	$content = preg_replace( '/eventbrite-widget-\d+/', $widget_id, $content );
99
100
	return $content;
101
}
102