Completed
Push — add/eventbrite-block ( 292953...ec3a59 )
by
unknown
06:08
created

eventbrite.php ➔ jetpack_eventbrite_block_load_assets()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 13
nop 1
dl 0
loc 51
rs 8.4468
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.0.0
6
 *
7
 * @package Jetpack
8
 */
9
10
jetpack_register_block(
11
	'jetpack/eventbrite',
12
	array(
13
		'attributes'      => array(
14
			'url'      => array(
15
				'type' => 'string',
16
			),
17
			'useModal' => array(
18
				'type' => 'boolean',
19
			),
20
		),
21
		'render_callback' => 'jetpack_eventbrite_block_load_assets',
22
	)
23
);
24
25
/**
26
 * Eventbrite block registration/dependency delclaration.
27
 *
28
 * @param array $attr    Eventbrite block attributes.
29
 *
30
 * @return string
31
 */
32
function jetpack_eventbrite_block_load_assets( $attr ) {
33
	if ( empty( $attr['url'] ) ) {
34
		return '';
35
	}
36
37
	$matches = array();
38
	preg_match( '/(\d+)$/', $attr['url'], $matches );
39
	$event_id = isset( $matches[1] ) && $matches[1] ? $matches[1] : null;
40
41
	if ( ! $event_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $event_id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
42
		return '';
43
	}
44
45
	wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true );
46
47
	// Show the embedded version.
48
	if ( empty( $attr['useModal'] ) ) {
49
		wp_add_inline_script(
50
			'eventbrite-widget',
51
			"window.EBWidgets.createWidget({
52
				widgetType: 'checkout',
53
				event_id: ${event_id},
54
				iframeContainerId: 'eventbrite-widget-container-${event_id}',
55
			});"
56
		);
57
58
		return <<<EOT
59
<div id="eventbrite-widget-container-${event_id}"></div>
60
<noscript>
61
	<a href="https://www.eventbrite.com/e/${event_id}" rel="noopener noreferrer" target="_blank">Buy Tickets on Eventbrite</a>
62
</noscript>
63
EOT;
64
	}
65
66
	// Show the modal version.
67
	wp_add_inline_script(
68
		'eventbrite-widget',
69
		"window.EBWidgets.createWidget({
70
			widgetType: 'checkout',
71
			event_id: ${event_id},
72
			modal: true,
73
			modalTriggerElementId: 'eventbrite-widget-modal-trigger-${event_id}',
74
		});"
75
	);
76
77
	return <<<EOT
78
	<noscript><a href="https://www.eventbrite.com.au/e/${event_id}" rel="noopener noreferrer" target="_blank"></noscript>
79
	<button id="eventbrite-widget-modal-trigger-${event_id}" type="button">Buy Tickets</button>
80
	<noscript></a>Buy Tickets on Eventbrite</noscript>
81
EOT;
82
}
83