Completed
Push — updates/infinity-vanilla-js ( fda899...9466ff )
by
unknown
07:55
created

eventbrite.php ➔ render_block()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 3
nop 2
dl 0
loc 86
rs 7.3721
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
namespace Automattic\Jetpack\Extensions\Eventbrite;
11
12
use Jetpack_Gutenberg;
13
14
const FEATURE_NAME = 'eventbrite';
15
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
16
17
/**
18
 * Registers the block for use in Gutenberg
19
 * This is done via an action so that we can disable
20
 * registration if we need to.
21
 */
22
function register_block() {
23
	jetpack_register_block(
24
		BLOCK_NAME,
25
		array( 'render_callback' => __NAMESPACE__ . '\render_block' )
26
	);
27
}
28
add_action( 'init', __NAMESPACE__ . '\register_block' );
29
30
/**
31
 * Eventbrite block registration/dependency delclaration.
32
 *
33
 * @param array  $attr    Eventbrite block attributes.
34
 * @param string $content Rendered embed element (without scripts) from the block editor.
35
 *
36
 * @return string
37
 */
38
function render_block( $attr, $content ) {
39
	if ( is_admin() || empty( $attr['eventId'] ) || empty( $attr['url'] ) ) {
40
		return '';
41
	}
42
43
	$attr['url'] = Jetpack_Gutenberg::validate_block_embed_url(
44
		$attr['url'],
45
		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+)\/?(?:\?[^\/]*)?$#' ),
46
		true
47
	);
48
49
	$widget_id = wp_unique_id( 'eventbrite-widget-' );
50
51
	wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true );
52
53
	// Add CSS to hide direct link.
54
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
55
56
	// Show the embedded version.
57
	if ( empty( $attr['useModal'] ) && ( empty( $attr['style'] ) || 'modal' !== $attr['style'] ) ) {
58
		wp_add_inline_script(
59
			'eventbrite-widget',
60
			"window.EBWidgets.createWidget( {
61
				widgetType: 'checkout',
62
				eventId: " . absint( $attr['eventId'] ) . ",
63
				iframeContainerId: '" . esc_js( $widget_id ) . "',
64
			} );"
65
		);
66
67
		// $content contains a fallback link to the event that's saved in the post_content.
68
		// Append a div that will hold the iframe embed created by the Eventbrite widget.js.
69
		$classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr );
70
71
		$content .= sprintf(
72
			'<div id="%1$s" class="%2$s"></div>',
73
			esc_attr( $widget_id ),
74
			esc_attr( $classes )
75
		);
76
77
		return sprintf(
78
			'%s<noscript><a href="%s" rel="noopener noreferrer" target="_blank">%s</a></noscript>',
79
			$content,
80
			esc_url( $attr['url'] ),
81
			esc_html__( 'Register on Eventbrite', 'jetpack' )
82
		);
83
	}
84
85
	// Show the modal version.
86
	wp_add_inline_script(
87
		'eventbrite-widget',
88
		"window.EBWidgets.createWidget( {
89
			widgetType: 'checkout',
90
			eventId: " . absint( $attr['eventId'] ) . ",
91
			modal: true,
92
			modalTriggerElementId: '" . esc_js( $widget_id ) . "',
93
		} );"
94
	);
95
96
	// Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed
97
	// by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior.
98
	// @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html.
99
	wp_add_inline_script(
100
		'eventbrite-widget',
101
		"( function() {
102
			var widget = document.getElementById( '" . esc_js( $widget_id ) . "' );
103
			if ( widget ) {
104
				widget.addEventListener( 'click', function( event ) {
105
					event.preventDefault();
106
				} );
107
108
				widget.addEventListener( 'keydown', function( event ) {
109
					// Enter and space keys.
110
					if ( event.keyCode === 13 || event.keyCode === 32 ) {
111
						event.preventDefault();
112
						event.target && event.target.click();
113
					}
114
				} );
115
			}
116
		} )();"
117
	);
118
119
	// Replace the placeholder id saved in the post_content with a unique id used by widget.js.
120
	$content = preg_replace( '/eventbrite-widget-\d+/', $widget_id, $content );
121
122
	return $content;
123
}
124