Completed
Push — update/plugins-loaded-priority... ( b525f3...841a40 )
by
unknown
13:54 queued 06:36
created

calendly.php ➔ load_assets()   C

Complexity

Conditions 9
Paths 5

Size

Total Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 5
nop 2
dl 0
loc 91
rs 6.6408
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 = get_attribute( $attr, 'url' );
86
	if ( empty( $url ) ) {
87
		return;
88
	}
89
90
	/*
91
	 * Enqueue necessary scripts and styles.
92
	 */
93
	\Jetpack_Gutenberg::load_assets_as_required( 'calendly' );
94
	wp_enqueue_script(
95
		'jetpack-calendly-external-js',
96
		'https://assets.calendly.com/assets/external/widget.js',
97
		null,
98
		JETPACK__VERSION,
99
		true
100
	);
101
102
	$style                          = get_attribute( $attr, 'style' );
103
	$hide_event_type_details        = get_attribute( $attr, 'hideEventTypeDetails' );
104
	$background_color               = get_attribute( $attr, 'backgroundColor' );
105
	$text_color                     = get_attribute( $attr, 'textColor' );
106
	$primary_color                  = get_attribute( $attr, 'primaryColor' );
107
	$submit_button_text             = get_attribute( $attr, 'submitButtonText' );
108
	$submit_button_classes          = get_attribute( $attr, 'submitButtonClasses' );
109
	$submit_button_text_color       = get_attribute( $attr, 'customTextButtonColor' );
110
	$submit_button_background_color = get_attribute( $attr, 'customBackgroundButtonColor' );
111
	$classes                        = \Jetpack_Gutenberg::block_classes( 'calendly', $attr, array( 'calendly-style-' . $style ) );
112
	$block_id                       = wp_unique_id( 'calendly-block-' );
113
114
	$url = add_query_arg(
115
		array(
116
			'hide_event_type_details' => (int) $hide_event_type_details,
117
			'background_color'        => sanitize_hex_color_no_hash( $background_color ),
118
			'text_color'              => sanitize_hex_color_no_hash( $text_color ),
119
			'primary_color'           => sanitize_hex_color_no_hash( $primary_color ),
120
		),
121
		$url
122
	);
123
124
	if ( 'link' === $style ) {
125
		wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION );
126
127
		/*
128
		 * If we have some additional styles from the editor
129
		 * (a custom text color, custom bg color, or both )
130
		 * Let's add that CSS inline.
131
		 */
132
		if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) {
133
			$inline_styles = sprintf(
134
				'#%1$s .wp-block-button__link{%2$s%3$s}',
135
				esc_attr( $block_id ),
136
				! empty( $submit_button_text_color )
137
					? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';'
138
					: '',
139
				! empty( $submit_button_background_color )
140
					? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';'
141
					: ''
142
			);
143
			wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles );
144
		}
145
146
		$content = sprintf(
147
			'<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>',
148
			esc_attr( $classes ),
149
			esc_attr( $block_id ),
150
			! empty( $submit_button_classes ) ? esc_attr( $submit_button_classes ) : 'wp-block-button__link',
151
			esc_js( $url ),
152
			wp_kses_post( $submit_button_text )
153
		);
154
	} else { // Inline style.
155
		$content = sprintf(
156
			'<div class="%1$s" id="%2$s"></div>',
157
			esc_attr( $classes ),
158
			esc_attr( $block_id )
159
		);
160
		$script  = <<<JS_END
161
Calendly.initInlineWidget({
162
	url: '%s',
163
	parentElement: document.getElementById('%s'),
164
	inlineStyles: false,
165
});
166
JS_END;
167
		wp_add_inline_script( 'jetpack-calendly-external-js', sprintf( $script, esc_url( $url ), esc_js( $block_id ) ) );
168
	}
169
170
	return $content;
171
}
172
173
/**
174
 * Get filtered attributes.
175
 *
176
 * @param array  $attributes     Array containing the Calendly block attributes.
177
 * @param string $attribute_name String containing the attribute name to get.
178
 *
179
 * @return string
180
 */
181
function get_attribute( $attributes, $attribute_name ) {
182
	if ( isset( $attributes[ $attribute_name ] ) ) {
183
		return $attributes[ $attribute_name ];
184
	}
185
186
	$default_attributes = array(
187
		'style'                => 'inline',
188
		'submitButtonText'     => esc_html__( 'Schedule time with me', 'jetpack' ),
189
		'backgroundColor'      => 'ffffff',
190
		'textColor'            => '4D5055',
191
		'primaryColor'         => '00A2FF',
192
		'hideEventTypeDetails' => false,
193
	);
194
195
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
196
		return $default_attributes[ $attribute_name ];
197
	}
198
}
199