Completed
Push — update/avail-check-logic ( d23ca0 )
by
unknown
11:05 queued 03:46
created

calendly.php ➔ is_available()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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