Completed
Push — fix/jp_api_constants_in_connec... ( de839c...a94bf0 )
by
unknown
07:20
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 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
 * Check if the block should be available on the site.
19
 *
20
 * @return bool
21
 */
22 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...
23
	if (
24
		defined( 'IS_WPCOM' )
25
		&& IS_WPCOM
26
		&& function_exists( 'has_any_blog_stickers' )
27
	) {
28
		if ( has_any_blog_stickers(
29
			array( 'premium-plan', 'business-plan', 'ecommerce-plan' ),
30
			get_current_blog_id()
31
		) ) {
32
			return true;
33
		}
34
		return false;
35
	}
36
37
	return true;
38
}
39
40
/**
41
 * Registers the block for use in Gutenberg
42
 * This is done via an action so that we can disable
43
 * registration if we need to.
44
 */
45
function register_block() {
46
	if ( is_available() ) {
47
		jetpack_register_block(
48
			BLOCK_NAME,
49
			array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
50
		);
51
	}
52
}
53
add_action( 'init', __NAMESPACE__ . '\register_block' );
54
55
/**
56
 * Set the availability of the block as the editor
57
 * is loaded
58
 */
59 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...
60
	if ( is_available() ) {
61
		Jetpack_Gutenberg::set_extension_available( BLOCK_NAME );
62
	} else {
63
		Jetpack_Gutenberg::set_extension_unavailable(
64
			BLOCK_NAME,
65
			'missing_plan',
66
			array(
67
				'required_feature' => 'calendly',
68
				'required_plan'    => 'value_bundle',
69
			)
70
		);
71
	}
72
}
73
add_action( 'init', __NAMESPACE__ . '\set_availability' );
74
75
/**
76
 * Calendly block registration/dependency declaration.
77
 *
78
 * @param array  $attr    Array containing the Calendly block attributes.
79
 * @param string $content String containing the Calendly block content.
80
 *
81
 * @return string
82
 */
83
function load_assets( $attr, $content ) {
84
	if ( is_admin() ) {
85
		return;
86
	}
87
	$url = Jetpack_Gutenberg::validate_block_embed_url(
88
		get_attribute( $attr, 'url' ),
89
		array( 'calendly.com' )
90
	);
91
	if ( empty( $url ) ) {
92
		return;
93
	}
94
95
	/*
96
	 * Enqueue necessary scripts and styles.
97
	 */
98
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
99
	wp_enqueue_script(
100
		'jetpack-calendly-external-js',
101
		'https://assets.calendly.com/assets/external/widget.js',
102
		null,
103
		JETPACK__VERSION,
104
		true
105
	);
106
107
	$style                          = get_attribute( $attr, 'style' );
108
	$hide_event_type_details        = get_attribute( $attr, 'hideEventTypeDetails' );
109
	$background_color               = get_attribute( $attr, 'backgroundColor' );
110
	$text_color                     = get_attribute( $attr, 'textColor' );
111
	$primary_color                  = get_attribute( $attr, 'primaryColor' );
112
	$submit_button_text             = get_attribute( $attr, 'submitButtonText' );
113
	$submit_button_classes          = get_attribute( $attr, 'submitButtonClasses' );
114
	$submit_button_text_color       = get_attribute( $attr, 'customTextButtonColor' );
115
	$submit_button_background_color = get_attribute( $attr, 'customBackgroundButtonColor' );
116
	$classes                        = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr, array( 'calendly-style-' . $style ) );
117
	$block_id                       = wp_unique_id( 'calendly-block-' );
118
119
	$url = add_query_arg(
120
		array(
121
			'hide_event_type_details' => (int) $hide_event_type_details,
122
			'background_color'        => sanitize_hex_color_no_hash( $background_color ),
123
			'text_color'              => sanitize_hex_color_no_hash( $text_color ),
124
			'primary_color'           => sanitize_hex_color_no_hash( $primary_color ),
125
		),
126
		$url
127
	);
128
129
	if ( 'link' === $style ) {
130
		wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION );
131
132
		/*
133
		 * If we have some additional styles from the editor
134
		 * (a custom text color, custom bg color, or both )
135
		 * Let's add that CSS inline.
136
		 */
137
		if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) {
138
			$inline_styles = sprintf(
139
				'#%1$s .wp-block-button__link{%2$s%3$s}',
140
				esc_attr( $block_id ),
141
				! empty( $submit_button_text_color )
142
					? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';'
143
					: '',
144
				! empty( $submit_button_background_color )
145
					? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';'
146
					: ''
147
			);
148
			wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles );
149
		}
150
151
		$content = sprintf(
152
			'<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>',
153
			esc_attr( $classes ),
154
			esc_attr( $block_id ),
155
			! empty( $submit_button_classes ) ? esc_attr( $submit_button_classes ) : 'wp-block-button__link',
156
			esc_js( $url ),
157
			wp_kses_post( $submit_button_text )
158
		);
159
	} else { // Inline style.
160
		$content = sprintf(
161
			'<div class="%1$s" id="%2$s"></div>',
162
			esc_attr( $classes ),
163
			esc_attr( $block_id )
164
		);
165
		$script  = <<<JS_END
166
Calendly.initInlineWidget({
167
	url: '%s',
168
	parentElement: document.getElementById('%s'),
169
	inlineStyles: false,
170
});
171
JS_END;
172
		wp_add_inline_script( 'jetpack-calendly-external-js', sprintf( $script, esc_url( $url ), esc_js( $block_id ) ) );
173
	}
174
175
	return $content;
176
}
177
178
/**
179
 * Get filtered attributes.
180
 *
181
 * @param array  $attributes     Array containing the Calendly block attributes.
182
 * @param string $attribute_name String containing the attribute name to get.
183
 *
184
 * @return string
185
 */
186
function get_attribute( $attributes, $attribute_name ) {
187
	if ( isset( $attributes[ $attribute_name ] ) ) {
188
		return $attributes[ $attribute_name ];
189
	}
190
191
	$default_attributes = array(
192
		'style'                => 'inline',
193
		'submitButtonText'     => esc_html__( 'Schedule time with me', 'jetpack' ),
194
		'backgroundColor'      => 'ffffff',
195
		'textColor'            => '4D5055',
196
		'primaryColor'         => '00A2FF',
197
		'hideEventTypeDetails' => false,
198
	);
199
200
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
201
		return $default_attributes[ $attribute_name ];
202
	}
203
}
204