Completed
Push — renovate/gulp-sass-4.x ( 7f0527...3c2ae1 )
by
unknown
113:41 queued 106:54
created

calendly.php ➔ is_available()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 0
dl 17
loc 17
rs 9.3888
c 0
b 0
f 0
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
 * Registers the block for use in Gutenberg
19
 * This is done via an action so that we can disable
20
 * registration if we need to.
21
 */
22
function register_block() {
23
	jetpack_register_block(
24
		BLOCK_NAME,
25
		array(
26
			'render_callback' => __NAMESPACE__ . '\load_assets',
27
			'plan_check'      => true,
28
		)
29
	);
30
}
31
add_action( 'init', __NAMESPACE__ . '\register_block' );
32
33
/**
34
 * Calendly block registration/dependency declaration.
35
 *
36
 * @param array  $attr    Array containing the Calendly block attributes.
37
 * @param string $content String containing the Calendly block content.
38
 *
39
 * @return string
40
 */
41
function load_assets( $attr, $content ) {
42
43
	if ( is_admin() ) {
44
		return;
45
	}
46
	$url = Jetpack_Gutenberg::validate_block_embed_url(
47
		get_attribute( $attr, 'url' ),
48
		array( 'calendly.com' )
49
	);
50
	if ( empty( $url ) ) {
51
		return;
52
	}
53
54
	/*
55
	 * Enqueue necessary scripts and styles.
56
	 */
57
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
58
	wp_enqueue_script(
59
		'jetpack-calendly-external-js',
60
		'https://assets.calendly.com/assets/external/widget.js',
61
		null,
62
		JETPACK__VERSION,
63
		true
64
	);
65
66
	$style                          = get_attribute( $attr, 'style' );
67
	$hide_event_type_details        = get_attribute( $attr, 'hideEventTypeDetails' );
68
	$background_color               = get_attribute( $attr, 'backgroundColor' );
69
	$text_color                     = get_attribute( $attr, 'textColor' );
70
	$primary_color                  = get_attribute( $attr, 'primaryColor' );
71
	$submit_button_text             = get_attribute( $attr, 'submitButtonText' );
72
	$submit_button_classes          = get_attribute( $attr, 'submitButtonClasses' );
73
	$submit_button_text_color       = get_attribute( $attr, 'customTextButtonColor' );
74
	$submit_button_background_color = get_attribute( $attr, 'customBackgroundButtonColor' );
75
	$classes                        = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr, array( 'calendly-style-' . $style ) );
76
	$block_id                       = wp_unique_id( 'calendly-block-' );
77
78
	$url = add_query_arg(
79
		array(
80
			'hide_event_type_details' => (int) $hide_event_type_details,
81
			'background_color'        => sanitize_hex_color_no_hash( $background_color ),
82
			'text_color'              => sanitize_hex_color_no_hash( $text_color ),
83
			'primary_color'           => sanitize_hex_color_no_hash( $primary_color ),
84
		),
85
		$url
86
	);
87
88
	if ( 'link' === $style ) {
89
		wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION );
90
91
		/*
92
		 * If we have some additional styles from the editor
93
		 * (a custom text color, custom bg color, or both )
94
		 * Let's add that CSS inline.
95
		 */
96
		if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) {
97
			$inline_styles = sprintf(
98
				'#%1$s .wp-block-button__link{%2$s%3$s}',
99
				esc_attr( $block_id ),
100
				! empty( $submit_button_text_color )
101
					? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';'
102
					: '',
103
				! empty( $submit_button_background_color )
104
					? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';'
105
					: ''
106
			);
107
			wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles );
108
		}
109
110
		$content = sprintf(
111
			'<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>',
112
			esc_attr( $classes ),
113
			esc_attr( $block_id ),
114
			! empty( $submit_button_classes ) ? esc_attr( $submit_button_classes ) : 'wp-block-button__link',
115
			esc_js( $url ),
116
			wp_kses_post( $submit_button_text )
117
		);
118
	} else { // Inline style.
119
		$content = sprintf(
120
			'<div class="%1$s" id="%2$s"></div>',
121
			esc_attr( $classes ),
122
			esc_attr( $block_id )
123
		);
124
		$script  = <<<JS_END
125
Calendly.initInlineWidget({
126
	url: '%s',
127
	parentElement: document.getElementById('%s'),
128
	inlineStyles: false,
129
});
130
JS_END;
131
		wp_add_inline_script( 'jetpack-calendly-external-js', sprintf( $script, esc_url( $url ), esc_js( $block_id ) ) );
132
	}
133
134
	return $content;
135
}
136
137
/**
138
 * Get filtered attributes.
139
 *
140
 * @param array  $attributes     Array containing the Calendly block attributes.
141
 * @param string $attribute_name String containing the attribute name to get.
142
 *
143
 * @return string
144
 */
145
function get_attribute( $attributes, $attribute_name ) {
146
	if ( isset( $attributes[ $attribute_name ] ) ) {
147
		return $attributes[ $attribute_name ];
148
	}
149
150
	$default_attributes = array(
151
		'style'                => 'inline',
152
		'submitButtonText'     => esc_html__( 'Schedule time with me', 'jetpack' ),
153
		'backgroundColor'      => 'ffffff',
154
		'textColor'            => '4D5055',
155
		'primaryColor'         => '00A2FF',
156
		'hideEventTypeDetails' => false,
157
	);
158
159
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
160
		return $default_attributes[ $attribute_name ];
161
	}
162
}
163