Completed
Push — instant-search-master ( 95535d...2c1eb1 )
by
unknown
22:37 queued 16:04
created

calendly.php ➔ load_assets()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

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