Completed
Push — renovate/slack-web-api-5.x ( f1014a...4f2b74 )
by
unknown
30:35 queued 23:31
created

calendly.php ➔ jetpack_calendly_block_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
jetpack_register_block(
11
	'jetpack/calendly',
12
	array( 'render_callback' => 'jetpack_calendly_block_load_assets' )
13
);
14
15
/**
16
 * Calendly block registration/dependency declaration.
17
 *
18
 * @param array  $attr    Array containing the Calendly block attributes.
19
 * @param string $content String containing the Calendly block content.
20
 *
21
 * @return string
22
 */
23
function jetpack_calendly_block_load_assets( $attr, $content ) {
24
	$url = jetpack_calendly_block_get_attribute( $attr, 'url' );
25
	if ( empty( $url ) ) {
26
		return;
27
	}
28
29
	/*
30
	 * Enqueue necessary scripts and styles.
31
	 */
32
	Jetpack_Gutenberg::load_assets_as_required( 'calendly' );
33
	wp_enqueue_script(
34
		'jetpack-calendly-external-js',
35
		'https://assets.calendly.com/assets/external/widget.js',
36
		null,
37
		JETPACK__VERSION,
38
		false
39
	);
40
41
	$style                          = jetpack_calendly_block_get_attribute( $attr, 'style' );
42
	$hide_event_type_details        = jetpack_calendly_block_get_attribute( $attr, 'hideEventTypeDetails' );
43
	$background_color               = jetpack_calendly_block_get_attribute( $attr, 'backgroundColor' );
44
	$text_color                     = jetpack_calendly_block_get_attribute( $attr, 'textColor' );
45
	$primary_color                  = jetpack_calendly_block_get_attribute( $attr, 'primaryColor' );
46
	$submit_button_text             = jetpack_calendly_block_get_attribute( $attr, 'submitButtonText' );
47
	$submit_button_text_color       = jetpack_calendly_block_get_attribute( $attr, 'customTextButtonColor' );
48
	$submit_button_background_color = jetpack_calendly_block_get_attribute( $attr, 'customBackgroundButtonColor' );
49
	$classes                        = Jetpack_Gutenberg::block_classes( 'calendly', $attr );
50
51
	$url = add_query_arg(
52
		array(
53
			'hide_event_type_details' => (int) $hide_event_type_details,
54
			'background_color'        => sanitize_hex_color_no_hash( $background_color ),
55
			'text_color'              => sanitize_hex_color_no_hash( $text_color ),
56
			'primary_color'           => sanitize_hex_color_no_hash( $primary_color ),
57
		),
58
		$url
59
	);
60
61
	if ( 'link' === $style ) {
62
		wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION );
63
64
		/*
65
		 * If we have some additional styles from the editor
66
		 * (a custom text color, custom bg color, or both )
67
		 * Let's add that CSS inline.
68
		 */
69
		if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) {
70
			$inline_styles = sprintf(
71
				'.wp-block-jetpack-calendly .button{%1$s%2$s}',
72
				! empty( $submit_button_text_color )
73
					? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';'
74
					: '',
75
				! empty( $submit_button_background_color )
76
					? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';'
77
					: ''
78
			);
79
			wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles );
80
		}
81
82
		$content = sprintf(
83
			'<div class="%1$s"><a class="button" href="" onclick="Calendly.initPopupWidget({url:\'%2$s\'});return false;">%3$s</a></div>',
84
			esc_attr( $classes ),
85
			esc_url( $url ),
86
			wp_kses_post( $submit_button_text )
87
		);
88
	} else { // Button style.
89
		$content = sprintf(
90
			'<div class="calendly-inline-widget %1$s" data-url="%2$s" style="min-width:320px;height:630px;"></div>',
91
			esc_attr( $classes ),
92
			esc_url( $url )
93
		);
94
	}
95
96
	return $content;
97
}
98
99
/**
100
 * Get filtered attributes.
101
 *
102
 * @param array  $attributes     Array containing the Calendly block attributes.
103
 * @param string $attribute_name String containing the attribute name to get.
104
 *
105
 * @return string
106
 */
107
function jetpack_calendly_block_get_attribute( $attributes, $attribute_name ) {
108
	if ( isset( $attributes[ $attribute_name ] ) ) {
109
		return $attributes[ $attribute_name ];
110
	}
111
112
	$default_attributes = array(
113
		'style'                => 'inline',
114
		'submitButtonText'     => esc_html__( 'Schedule time with me', 'jetpack' ),
115
		'backgroundColor'      => 'ffffff',
116
		'textColor'            => '4D5055',
117
		'primaryColor'         => '00A2FF',
118
		'hideEventTypeDetails' => false,
119
	);
120
121
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
122
		return $default_attributes[ $attribute_name ];
123
	}
124
}
125