Completed
Push — try/allow-embeds-in-amp ( 2ccd08...2692ec )
by
unknown
113:58 queued 106:11
created

calendly.php ➔ load_assets()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 12
nop 2
dl 0
loc 76
rs 7.5903
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
 * Calendly Block.
4
 *
5
 * @since 8.2.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Calendly;
11
12
use Jetpack_Gutenberg;
13
14
const FEATURE_NAME = 'calendly';
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(
26
			'render_callback' => __NAMESPACE__ . '\load_assets',
27
			'plan_check'      => true,
28
		)
29
	);
30
}
31
add_action( 'init', __NAMESPACE__ . '\register_block' );
32
33
/**
34
 * Calendly block registration/dependency declaration.
35
 *
36
 * @param array  $attr    Array containing the Calendly block attributes.
37
 * @param string $content String containing the Calendly block content.
38
 *
39
 * @return string
40
 */
41
function load_assets( $attr, $content ) {
42
43
	if ( is_admin() ) {
44
		return;
45
	}
46
	$url = Jetpack_Gutenberg::validate_block_embed_url(
47
		get_attribute( $attr, 'url' ),
48
		array( 'calendly.com' )
49
	);
50
	if ( empty( $url ) ) {
51
		return;
52
	}
53
54
	/*
55
	 * Enqueue necessary scripts and styles.
56
	 */
57
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
58
59
	$style                   = get_attribute( $attr, 'style' );
60
	$hide_event_type_details = get_attribute( $attr, 'hideEventTypeDetails' );
61
	$background_color        = get_attribute( $attr, 'backgroundColor' );
62
	$text_color              = get_attribute( $attr, 'textColor' );
63
	$primary_color           = get_attribute( $attr, 'primaryColor' );
64
	$classes                 = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr, array( 'calendly-style-' . $style ) );
65
	$block_id                = wp_unique_id( 'calendly-block-' );
66
67
	if ( ! wp_script_is( 'jetpack-calendly-external-js' ) ) {
68
		enqueue_calendly_js();
69
	}
70
71
	$base_url = $url;
72
	$url      = add_query_arg(
73
		array(
74
			'hide_event_type_details' => (int) $hide_event_type_details,
75
			'background_color'        => sanitize_hex_color_no_hash( $background_color ),
76
			'text_color'              => sanitize_hex_color_no_hash( $text_color ),
77
			'primary_color'           => sanitize_hex_color_no_hash( $primary_color ),
78
		),
79
		$url
80
	);
81
82
	if ( 'link' === $style ) {
83
		if ( ! wp_style_is( 'jetpack-calendly-external-css' ) ) {
84
			wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION );
85
		}
86
87
		// Render deprecated version of Calendly block if needed. New markup block button class before rendering here.
88
		if ( false === strpos( $content, 'wp-block-jetpack-button' ) ) {
89
			$content = deprecated_render_button_v1( $attr, $block_id, $classes, $url );
90
		} else {
91
			$content = str_replace( 'calendly-widget-id', esc_attr( $block_id ), $content );
92
			$content = str_replace( $base_url, $url, $content );
93
		}
94
95
		wp_add_inline_script(
96
			'jetpack-calendly-external-js',
97
			sprintf( "calendly_attach_link_events( '%s' )", esc_js( $block_id ) )
98
		);
99
	} else { // Inline style.
100
		$content = sprintf(
101
			'<div class="%1$s" id="%2$s"></div>',
102
			esc_attr( $classes ),
103
			esc_attr( $block_id )
104
		);
105
		$script  = <<<JS_END
106
Calendly.initInlineWidget({
107
url: '%s',
108
parentElement: document.getElementById('%s'),
109
inlineStyles: false,
110
});
111
JS_END;
112
		wp_add_inline_script( 'jetpack-calendly-external-js', sprintf( $script, esc_url( $url ), esc_js( $block_id ) ) );
113
	}
114
115
	return $content;
116
}
117
118
/**
119
 * Get filtered attributes.
120
 *
121
 * @param array  $attributes     Array containing the Calendly block attributes.
122
 * @param string $attribute_name String containing the attribute name to get.
123
 *
124
 * @return string
125
 */
126
function get_attribute( $attributes, $attribute_name ) {
127
	if ( isset( $attributes[ $attribute_name ] ) ) {
128
		return $attributes[ $attribute_name ];
129
	}
130
131
	$default_attributes = array(
132
		'style'                => 'inline',
133
		'submitButtonText'     => esc_html__( 'Schedule time with me', 'jetpack' ),
134
		'backgroundColor'      => 'ffffff',
135
		'textColor'            => '4D5055',
136
		'primaryColor'         => '00A2FF',
137
		'hideEventTypeDetails' => false,
138
	);
139
140
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
141
		return $default_attributes[ $attribute_name ];
142
	}
143
}
144
145
/**
146
 * Enqueues the Calendly JS library and inline function to attach event
147
 * handlers to the button.
148
 *
149
 * @return void
150
 */
151
function enqueue_calendly_js() {
152
	wp_enqueue_script(
153
		'jetpack-calendly-external-js',
154
		'https://assets.calendly.com/assets/external/widget.js',
155
		null,
156
		JETPACK__VERSION,
157
		false
158
	);
159
160
	wp_add_inline_script(
161
		'jetpack-calendly-external-js',
162
		"function calendly_attach_link_events( elementId ) {
163
			var widget = document.getElementById( elementId );
164
			if ( widget ) {
165
				widget.addEventListener( 'click', function( event ) {
166
					event.preventDefault();
167
					Calendly.initPopupWidget( { url: event.target.href } );
168
				} );
169
				widget.addEventListener( 'keydown', function( event ) {
170
					// Enter and space keys.
171
					if ( event.keyCode === 13 || event.keyCode === 32 ) {
172
						event.preventDefault();
173
						event.target && event.target.click();
174
					}
175
				} );
176
			}
177
		}"
178
	);
179
}
180
181
/**
182
 * Renders a deprecated legacy version of the button HTML.
183
 *
184
 * @param array  $attributes Array containing the Calendly block attributes.
185
 * @param string $block_id  The value for the ID attribute of the link.
186
 * @param string $classes   The CSS classes for the wrapper div.
187
 * @param string $url       Calendly URL for the link HREF.
188
 *
189
 * @return string
190
 */
191
function deprecated_render_button_v1( $attributes, $block_id, $classes, $url ) {
192
	// This is the legacy version, so create the full link content.
193
	$submit_button_text             = get_attribute( $attributes, 'submitButtonText' );
194
	$submit_button_classes          = get_attribute( $attributes, 'submitButtonClasses' );
195
	$submit_button_text_color       = get_attribute( $attributes, 'customTextButtonColor' );
196
	$submit_button_background_color = get_attribute( $attributes, 'customBackgroundButtonColor' );
197
198
	/*
199
	 * If we have some additional styles from the editor
200
	 * (a custom text color, custom bg color, or both )
201
	 * Let's add that CSS inline.
202
	 */
203
	if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) {
204
		$inline_styles = sprintf(
205
			'#%1$s{%2$s%3$s}',
206
			esc_attr( $block_id ),
207
			! empty( $submit_button_text_color )
208
				? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';'
209
				: '',
210
			! empty( $submit_button_background_color )
211
				? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';'
212
				: ''
213
		);
214
		wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles );
215
	}
216
217
	return sprintf(
218
		'<div class="wp-block-button %1$s"><a id="%2$s" class="%3$s" href="%4$s" role="button">%5$s</a></div>',
219
		esc_attr( $classes ),
220
		esc_attr( $block_id ),
221
		! empty( $submit_button_classes ) ? esc_attr( $submit_button_classes ) : 'wp-block-button__link',
222
		esc_js( $url ),
223
		wp_kses_post( $submit_button_text )
224
	);
225
}
226