Completed
Push — fix/package-release-script ( 8004a7...315d04 )
by
unknown
06:57
created

calendly.php ➔ load_assets()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

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