Completed
Push — fix/14506-eventbrite-button-co... ( 91a07d...4320ba )
by
unknown
11:49 queued 05:10
created

eventbrite.php ➔ jetpack_render_eventbrite_block()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 68
rs 8.6981
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
const JETPACK_EVENTBRITE_WIDGET_SLUG = 'eventbrite-widget';
18
19
/**
20
 * Eventbrite block registration/dependency delclaration.
21
 *
22
 * @param array  $attr    Eventbrite block attributes.
23
 * @param string $content Rendered embed element (without scripts) from the block editor.
24
 *
25
 * @return string
26
 */
27
function jetpack_render_eventbrite_block( $attr, $content ) {
28
	if ( empty( $attr['eventId'] ) || empty( $attr['url'] ) ) {
29
		return '';
30
	}
31
32
	$widget_id = JETPACK_EVENTBRITE_WIDGET_SLUG . '-' . $attr['eventId'];
33
34
	wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true );
35
36
	// Show the embedded version.
37
	if ( empty( $attr['useModal'] ) ) {
38
		wp_add_inline_script(
39
			'eventbrite-widget',
40
			"window.EBWidgets.createWidget({
41
				widgetType: 'checkout',
42
				eventId: " . absint( $attr['eventId'] ) . ",
43
				iframeContainerId: '" . esc_js( $widget_id ) . "',
44
			});"
45
		);
46
47
		return sprintf(
48
			'%s<noscript><a href="%s" rel="noopener noreferrer" target="_blank">%s</a></noscript>',
49
			$content,
50
			esc_url( $attr['url'] ),
51
			esc_html__( 'Register on Eventbrite', 'jetpack' )
52
		);
53
	}
54
55
	// Show the modal version.
56
	wp_add_inline_script(
57
		'eventbrite-widget',
58
		"window.EBWidgets.createWidget({
59
			widgetType: 'checkout',
60
			eventId: " . absint( $attr['eventId'] ) . ",
61
			modal: true,
62
			modalTriggerElementId: '" . esc_js( $widget_id ) . "',
63
		});"
64
	);
65
66
	// Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed
67
	// by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior.
68
	// @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html.
69
	wp_add_inline_script(
70
		'eventbrite-widget',
71
		"(function() {
72
			var widget = document.getElementById('" . esc_js( $widget_id ) . "');
73
			if ( widget ) {
74
				widget.addEventListener( 'click',
75
					function( event ) {
76
						event.preventDefault();
77
					}
78
				);
79
80
				widget.addEventListener( 'keydown',
81
					function( event ) {
82
						// Enter and space keys.
83
						if ( event.keyCode === 13 || event.keyCode === 32 ) {
84
							event.preventDefault();
85
							event.target.click();
86
						}
87
					}
88
				);
89
			}
90
		})();"
91
	);
92
93
	return $content;
94
}
95
96
/**
97
 * Share PHP block settings with js block code.
98
 *
99
 * @return void
100
 */
101
function jetpack_eventbrite_block_editor_assets() {
102
	wp_localize_script(
103
		'jetpack-blocks-editor',
104
		'Jetpack_Block_Eventbrite_Settings',
105
		array(
106
			'widget_slug' => JETPACK_EVENTBRITE_WIDGET_SLUG,
107
		)
108
	);
109
}
110
111
add_action( 'enqueue_block_editor_assets', 'jetpack_eventbrite_block_editor_assets' );
112