Completed
Push — instant-search-master ( a786a9...893b7c )
by
unknown
85:32 queued 79:17
created

calendly.php ➔ load_assets()   C

Complexity

Conditions 9
Paths 5

Size

Total Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 5
nop 2
dl 0
loc 94
rs 6.5753
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 Jetpack\Calendly_Block;
11
12
const FEATURE_NAME = 'calendly';
13
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
14
15
/**
16
 * Check if the block should be available on the site.
17
 *
18
 * @return bool
19
 */
20 View Code Duplication
function is_available() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
	if (
22
		defined( 'IS_WPCOM' )
23
		&& IS_WPCOM
24
		&& function_exists( 'has_any_blog_stickers' )
25
	) {
26
		if ( has_any_blog_stickers(
27
			array( 'premium-plan', 'business-plan', 'ecommerce-plan' ),
28
			get_current_blog_id()
29
		) ) {
30
			return true;
31
		}
32
		return false;
33
	}
34
35
	return true;
36
}
37
38
/**
39
 * Registers the block for use in Gutenberg
40
 * This is done via an action so that we can disable
41
 * registration if we need to.
42
 */
43
function register_block() {
44
	if ( is_available() ) {
45
		jetpack_register_block(
46
			BLOCK_NAME,
47
			array( 'render_callback' => 'Jetpack\Calendly_Block\load_assets' )
48
		);
49
	}
50
}
51
add_action( 'init', 'Jetpack\Calendly_Block\register_block' );
52
53
/**
54
 * Set the availability of the block as the editor
55
 * is loaded
56
 */
57 View Code Duplication
function set_availability() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
	if ( is_available() ) {
59
		\Jetpack_Gutenberg::set_extension_available( BLOCK_NAME );
60
	} else {
61
		\Jetpack_Gutenberg::set_extension_unavailable(
62
			BLOCK_NAME,
63
			'missing_plan',
64
			array(
65
				'required_feature' => 'calendly',
66
				'required_plan'    => 'value_bundle',
67
			)
68
		);
69
	}
70
}
71
add_action( 'init', 'Jetpack\Calendly_Block\set_availability' );
72
73
/**
74
 * Calendly block registration/dependency declaration.
75
 *
76
 * @param array  $attr    Array containing the Calendly block attributes.
77
 * @param string $content String containing the Calendly block content.
78
 *
79
 * @return string
80
 */
81
function load_assets( $attr, $content ) {
82
	if ( is_admin() ) {
83
		return;
84
	}
85
	$url = \Jetpack_Gutenberg::validate_block_embed_url(
86
		get_attribute( $attr, 'url' ),
87
		array( 'calendly.com' )
88
	);
89
	if ( empty( $url ) ) {
90
		return;
91
	}
92
93
	/*
94
	 * Enqueue necessary scripts and styles.
95
	 */
96
	\Jetpack_Gutenberg::load_assets_as_required( 'calendly' );
97
	wp_enqueue_script(
98
		'jetpack-calendly-external-js',
99
		'https://assets.calendly.com/assets/external/widget.js',
100
		null,
101
		JETPACK__VERSION,
102
		true
103
	);
104
105
	$style                          = get_attribute( $attr, 'style' );
106
	$hide_event_type_details        = get_attribute( $attr, 'hideEventTypeDetails' );
107
	$background_color               = get_attribute( $attr, 'backgroundColor' );
108
	$text_color                     = get_attribute( $attr, 'textColor' );
109
	$primary_color                  = get_attribute( $attr, 'primaryColor' );
110
	$submit_button_text             = get_attribute( $attr, 'submitButtonText' );
111
	$submit_button_classes          = get_attribute( $attr, 'submitButtonClasses' );
112
	$submit_button_text_color       = get_attribute( $attr, 'customTextButtonColor' );
113
	$submit_button_background_color = get_attribute( $attr, 'customBackgroundButtonColor' );
114
	$classes                        = \Jetpack_Gutenberg::block_classes( 'calendly', $attr, array( 'calendly-style-' . $style ) );
115
	$block_id                       = wp_unique_id( 'calendly-block-' );
116
117
	$url = add_query_arg(
118
		array(
119
			'hide_event_type_details' => (int) $hide_event_type_details,
120
			'background_color'        => sanitize_hex_color_no_hash( $background_color ),
121
			'text_color'              => sanitize_hex_color_no_hash( $text_color ),
122
			'primary_color'           => sanitize_hex_color_no_hash( $primary_color ),
123
		),
124
		$url
125
	);
126
127
	if ( 'link' === $style ) {
128
		wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION );
129
130
		/*
131
		 * If we have some additional styles from the editor
132
		 * (a custom text color, custom bg color, or both )
133
		 * Let's add that CSS inline.
134
		 */
135
		if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) {
136
			$inline_styles = sprintf(
137
				'#%1$s .wp-block-button__link{%2$s%3$s}',
138
				esc_attr( $block_id ),
139
				! empty( $submit_button_text_color )
140
					? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';'
141
					: '',
142
				! empty( $submit_button_background_color )
143
					? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';'
144
					: ''
145
			);
146
			wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles );
147
		}
148
149
		$content = sprintf(
150
			'<div class="wp-block-button %1$s" id="%2$s"><a class="%3$s" role="button" onclick="Calendly.initPopupWidget({url:\'%4$s\'});return false;">%5$s</a></div>',
151
			esc_attr( $classes ),
152
			esc_attr( $block_id ),
153
			! empty( $submit_button_classes ) ? esc_attr( $submit_button_classes ) : 'wp-block-button__link',
154
			esc_js( $url ),
155
			wp_kses_post( $submit_button_text )
156
		);
157
	} else { // Inline style.
158
		$content = sprintf(
159
			'<div class="%1$s" id="%2$s"></div>',
160
			esc_attr( $classes ),
161
			esc_attr( $block_id )
162
		);
163
		$script  = <<<JS_END
164
Calendly.initInlineWidget({
165
	url: '%s',
166
	parentElement: document.getElementById('%s'),
167
	inlineStyles: false,
168
});
169
JS_END;
170
		wp_add_inline_script( 'jetpack-calendly-external-js', sprintf( $script, esc_url( $url ), esc_js( $block_id ) ) );
171
	}
172
173
	return $content;
174
}
175
176
/**
177
 * Get filtered attributes.
178
 *
179
 * @param array  $attributes     Array containing the Calendly block attributes.
180
 * @param string $attribute_name String containing the attribute name to get.
181
 *
182
 * @return string
183
 */
184
function get_attribute( $attributes, $attribute_name ) {
185
	if ( isset( $attributes[ $attribute_name ] ) ) {
186
		return $attributes[ $attribute_name ];
187
	}
188
189
	$default_attributes = array(
190
		'style'                => 'inline',
191
		'submitButtonText'     => esc_html__( 'Schedule time with me', 'jetpack' ),
192
		'backgroundColor'      => 'ffffff',
193
		'textColor'            => '4D5055',
194
		'primaryColor'         => '00A2FF',
195
		'hideEventTypeDetails' => false,
196
	);
197
198
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
199
		return $default_attributes[ $attribute_name ];
200
	}
201
}
202