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() { |
|
|
|
|
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
|
|
View Code Duplication |
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
|
|
|
} else { |
50
|
|
|
\Jetpack_Gutenberg::set_extension_unavailable( |
51
|
|
|
BLOCK_NAME, |
52
|
|
|
'missing_plan', |
53
|
|
|
array( |
54
|
|
|
'required_feature' => 'calendly', |
55
|
|
|
'required_plan' => 'premium-plan', |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
add_action( 'init', 'Jetpack\Calendly_Block\register_block' ); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Calendly block registration/dependency declaration. |
65
|
|
|
* |
66
|
|
|
* @param array $attr Array containing the Calendly block attributes. |
67
|
|
|
* @param string $content String containing the Calendly block content. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
function load_assets( $attr, $content ) { |
72
|
|
|
$url = get_attribute( $attr, 'url' ); |
73
|
|
|
if ( empty( $url ) ) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* |
78
|
|
|
* Enqueue necessary scripts and styles. |
79
|
|
|
*/ |
80
|
|
|
\Jetpack_Gutenberg::load_assets_as_required( 'calendly' ); |
81
|
|
|
wp_enqueue_script( |
82
|
|
|
'jetpack-calendly-external-js', |
83
|
|
|
'https://assets.calendly.com/assets/external/widget.js', |
84
|
|
|
null, |
85
|
|
|
JETPACK__VERSION, |
86
|
|
|
false |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$style = get_attribute( $attr, 'style' ); |
90
|
|
|
$hide_event_type_details = get_attribute( $attr, 'hideEventTypeDetails' ); |
91
|
|
|
$background_color = get_attribute( $attr, 'backgroundColor' ); |
92
|
|
|
$text_color = get_attribute( $attr, 'textColor' ); |
93
|
|
|
$primary_color = get_attribute( $attr, 'primaryColor' ); |
94
|
|
|
$submit_button_text = get_attribute( $attr, 'submitButtonText' ); |
95
|
|
|
$submit_button_text_color = get_attribute( $attr, 'customTextButtonColor' ); |
96
|
|
|
$submit_button_background_color = get_attribute( $attr, 'customBackgroundButtonColor' ); |
97
|
|
|
$classes = \Jetpack_Gutenberg::block_classes( 'calendly', $attr ); |
98
|
|
|
|
99
|
|
|
$url = add_query_arg( |
100
|
|
|
array( |
101
|
|
|
'hide_event_type_details' => (int) $hide_event_type_details, |
102
|
|
|
'background_color' => sanitize_hex_color_no_hash( $background_color ), |
103
|
|
|
'text_color' => sanitize_hex_color_no_hash( $text_color ), |
104
|
|
|
'primary_color' => sanitize_hex_color_no_hash( $primary_color ), |
105
|
|
|
), |
106
|
|
|
$url |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
if ( 'link' === $style ) { |
110
|
|
|
wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION ); |
111
|
|
|
|
112
|
|
|
/* |
113
|
|
|
* If we have some additional styles from the editor |
114
|
|
|
* (a custom text color, custom bg color, or both ) |
115
|
|
|
* Let's add that CSS inline. |
116
|
|
|
*/ |
117
|
|
|
if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) { |
118
|
|
|
$inline_styles = sprintf( |
119
|
|
|
'.wp-block-jetpack-calendly .button{%1$s%2$s}', |
120
|
|
|
! empty( $submit_button_text_color ) |
121
|
|
|
? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';' |
122
|
|
|
: '', |
123
|
|
|
! empty( $submit_button_background_color ) |
124
|
|
|
? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';' |
125
|
|
|
: '' |
126
|
|
|
); |
127
|
|
|
wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$content = sprintf( |
131
|
|
|
'<div class="%1$s"><a class="wp-block-button__link" role="button" onclick="Calendly.initPopupWidget({url:\'%2$s\'});return false;">%3$s</a></div>', |
132
|
|
|
esc_attr( $classes ), |
133
|
|
|
esc_js( $url ), |
134
|
|
|
wp_kses_post( $submit_button_text ) |
135
|
|
|
); |
136
|
|
|
} else { // Button style. |
137
|
|
|
$content = sprintf( |
138
|
|
|
'<div class="calendly-inline-widget %1$s" data-url="%2$s" style="min-width:320px;height:630px;"></div>', |
139
|
|
|
esc_attr( $classes ), |
140
|
|
|
esc_url( $url ) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $content; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get filtered attributes. |
149
|
|
|
* |
150
|
|
|
* @param array $attributes Array containing the Calendly block attributes. |
151
|
|
|
* @param string $attribute_name String containing the attribute name to get. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
function get_attribute( $attributes, $attribute_name ) { |
156
|
|
|
if ( isset( $attributes[ $attribute_name ] ) ) { |
157
|
|
|
return $attributes[ $attribute_name ]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$default_attributes = array( |
161
|
|
|
'style' => 'inline', |
162
|
|
|
'submitButtonText' => esc_html__( 'Schedule time with me', 'jetpack' ), |
163
|
|
|
'backgroundColor' => 'ffffff', |
164
|
|
|
'textColor' => '4D5055', |
165
|
|
|
'primaryColor' => '00A2FF', |
166
|
|
|
'hideEventTypeDetails' => false, |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
if ( isset( $default_attributes[ $attribute_name ] ) ) { |
170
|
|
|
return $default_attributes[ $attribute_name ]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
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.