Completed
Push — renovate/dealerdirect-phpcodes... ( 14797c...4cde9a )
by
unknown
27:42 queued 19:25
created

eventbrite.php ➔ jetpack_render_eventbrite_block()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

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