Completed
Push — update/wpcom-premium-block-reg... ( ceb48a )
by Jeremy
07:15
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 Method

Rating   Name   Duplication   Size   Complexity  
A calendly.php ➔ register_block() 0 9 1
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
/**
17
 * Registers the block for use in Gutenberg
18
 * This is done via an action so that we can disable
19
 * registration if we need to.
20
 */
21
function register_block() {
22
	jetpack_register_block(
23
		BLOCK_NAME,
24
		array( 'render_callback' => 'Jetpack\Calendly_Block\load_assets' ),
25
		array(
26
			'wpcom' => 'premium-plan',
27
		)
28
	);
29
}
30
31
add_action( 'init', 'Jetpack\Calendly_Block\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
	$url = get_attribute( $attr, 'url' );
43
	if ( empty( $url ) ) {
44
		return;
45
	}
46
47
	/*
48
	 * Enqueue necessary scripts and styles.
49
	 */
50
	\Jetpack_Gutenberg::load_assets_as_required( 'calendly' );
51
	wp_enqueue_script(
52
		'jetpack-calendly-external-js',
53
		'https://assets.calendly.com/assets/external/widget.js',
54
		null,
55
		JETPACK__VERSION,
56
		false
57
	);
58
59
	$style                          = get_attribute( $attr, 'style' );
60
	$hide_event_type_details        = get_attribute( $attr, 'hideEventTypeDetails' );
61
	$background_color               = get_attribute( $attr, 'backgroundColor' );
62
	$text_color                     = get_attribute( $attr, 'textColor' );
63
	$primary_color                  = get_attribute( $attr, 'primaryColor' );
64
	$submit_button_text             = get_attribute( $attr, 'submitButtonText' );
65
	$submit_button_text_color       = get_attribute( $attr, 'customTextButtonColor' );
66
	$submit_button_background_color = get_attribute( $attr, 'customBackgroundButtonColor' );
67
	$classes                        = \Jetpack_Gutenberg::block_classes( 'calendly', $attr );
68
69
	$url = add_query_arg(
70
		array(
71
			'hide_event_type_details' => (int) $hide_event_type_details,
72
			'background_color'        => sanitize_hex_color_no_hash( $background_color ),
73
			'text_color'              => sanitize_hex_color_no_hash( $text_color ),
74
			'primary_color'           => sanitize_hex_color_no_hash( $primary_color ),
75
		),
76
		$url
77
	);
78
79
	if ( 'link' === $style ) {
80
		wp_enqueue_style( 'jetpack-calendly-external-css', 'https://assets.calendly.com/assets/external/widget.css', null, JETPACK__VERSION );
81
82
		/*
83
		 * If we have some additional styles from the editor
84
		 * (a custom text color, custom bg color, or both )
85
		 * Let's add that CSS inline.
86
		 */
87
		if ( ! empty( $submit_button_text_color ) || ! empty( $submit_button_background_color ) ) {
88
			$inline_styles = sprintf(
89
				'.wp-block-jetpack-calendly .button{%1$s%2$s}',
90
				! empty( $submit_button_text_color )
91
					? 'color:#' . sanitize_hex_color_no_hash( $submit_button_text_color ) . ';'
92
					: '',
93
				! empty( $submit_button_background_color )
94
					? 'background-color:#' . sanitize_hex_color_no_hash( $submit_button_background_color ) . ';'
95
					: ''
96
			);
97
			wp_add_inline_style( 'jetpack-calendly-external-css', $inline_styles );
98
		}
99
100
		$content = sprintf(
101
			'<div class="%1$s"><a class="wp-block-button__link" role="button" onclick="Calendly.initPopupWidget({url:\'%2$s\'});return false;">%3$s</a></div>',
102
			esc_attr( $classes ),
103
			esc_js( $url ),
104
			wp_kses_post( $submit_button_text )
105
		);
106
	} else { // Button style.
107
		$content = sprintf(
108
			'<div class="calendly-inline-widget %1$s" data-url="%2$s" style="min-width:320px;height:630px;"></div>',
109
			esc_attr( $classes ),
110
			esc_url( $url )
111
		);
112
	}
113
114
	return $content;
115
}
116
117
/**
118
 * Get filtered attributes.
119
 *
120
 * @param array  $attributes     Array containing the Calendly block attributes.
121
 * @param string $attribute_name String containing the attribute name to get.
122
 *
123
 * @return string
124
 */
125
function get_attribute( $attributes, $attribute_name ) {
126
	if ( isset( $attributes[ $attribute_name ] ) ) {
127
		return $attributes[ $attribute_name ];
128
	}
129
130
	$default_attributes = array(
131
		'style'                => 'inline',
132
		'submitButtonText'     => esc_html__( 'Schedule time with me', 'jetpack' ),
133
		'backgroundColor'      => 'ffffff',
134
		'textColor'            => '4D5055',
135
		'primaryColor'         => '00A2FF',
136
		'hideEventTypeDetails' => false,
137
	);
138
139
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
140
		return $default_attributes[ $attribute_name ];
141
	}
142
}
143