Completed
Push — update/opentable-block-registr... ( 19b3e8...6c214a )
by Jeremy
28:48 queued 18:20
created

opentable.php ➔ set_availability()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * OpenTable Block.
4
 *
5
 * @since 8.2
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Jetpack\OpenTable_Block;
11
12
const FEATURE_NAME = 'opentable';
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
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 down via an action so that we can disable
41
 * registration if we need to
42
 */
43
function register_block() {
44
	if ( is_available() ) {
45
		jetpack_register_block(
46
			BLOCK_NAME,
47
			array( 'render_callback' => 'Jetpack\OpenTable_Block\load_assets' )
48
		);
49
	}
50
}
51
add_action( 'init', 'Jetpack\OpenTable_Block\register_block' );
52
53
/**
54
 * Set block's availability.
55
 */
56
function set_availability() {
57
	if ( is_available() ) {
58
		\Jetpack_Gutenberg::set_extension_available( BLOCK_NAME );
59
	} else {
60
		\Jetpack_Gutenberg::set_extension_unavailable(
61
			BLOCK_NAME,
62
			'missing_plan',
63
			array(
64
				'required_feature' => 'opentable',
65
				'required_plan'    => 'premium-plan',
66
			)
67
		);
68
	}
69
}
70
add_action( 'jetpack_register_gutenberg_extensions', 'Jetpack\OpenTable_Block\set_availability' );
71
72
/**
73
 * Adds an inline script which updates the block editor settings to
74
 * add the site locale. This feels sligktly better than calling back
75
 * to the API before registering the block. It also seemed better than
76
 * creating a global
77
 */
78
function add_language_setting() {
79
	wp_add_inline_script( 'jetpack-blocks-editor', sprintf( "wp.data.dispatch( 'core/block-editor' ).updateSettings( { siteLocale: '%s' } )", str_replace( '_', '-', get_locale() ) ), 'before' );
80
}
81
add_action( 'enqueue_block_assets', 'Jetpack\OpenTable_Block\add_language_setting' );
82
83
/**
84
 * OpenTable block registration/dependency declaration.
85
 *
86
 * @param array $attributes    Array containing the OpenTable block attributes.
87
 *
88
 * @return string
89
 */
90
function load_assets( $attributes ) {
91
	\Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
92
93
	$classes = \Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes );
94
	$content = '<div class="' . esc_attr( $classes ) . '">';
95
	// The OpenTable script uses multiple `rid` paramters,
96
	// so we can't use WordPress to output it, as WordPress attempts to validate it and removes them.
97
	// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
98
	$content .= '<script type="text/javascript" src="' . esc_url( build_embed_url( $attributes ) ) . '"></script>';
99
	$content .= '</div>';
100
	return $content;
101
}
102
103
/**
104
 * Get the a block attribute
105
 *
106
 * @param array  $attributes Array of block attributes.
107
 * @param string $attribute_name The attribute to get.
108
 *
109
 * @return string The filtered attribute
110
 */
111
function get_attribute( $attributes, $attribute_name ) {
112
	if ( isset( $attributes[ $attribute_name ] ) ) {
113
		if ( in_array( $attribute_name, array( 'iframe', 'newtab' ), true ) ) {
114
			return $attributes[ $attribute_name ] ? 'true' : 'false';
115
		}
116
		return $attributes[ $attribute_name ];
117
	}
118
119
	$default_attributes = array(
120
		'style'  => 'standard',
121
		'iframe' => 'true',
122
		'domain' => 'com',
123
		'lang'   => 'en-US',
124
		'newtab' => 'false',
125
	);
126
127
	return isset( $default_attributes[ $attribute_name ] ) ? $default_attributes[ $attribute_name ] : null;
128
}
129
130
/**
131
 * Get the block type attribute
132
 *
133
 * @param array $attributes Array of block attributes.
134
 *
135
 * @return string The filtered attribute
136
 */
137
function get_type_attribute( $attributes ) {
138
	if ( ! empty( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) {
139
		return 'multi';
140
	}
141
142
	if ( empty( $attributes['style'] ) || 'button' !== $attributes['style'] ) {
143
		return 'standard';
144
	}
145
146
	return 'button';
147
}
148
149
/**
150
 * Get the block theme attribute
151
 *
152
 * OpenTable has a confusing mix of themes and types for the widget. A type
153
 * can have a theme, but the button style can not have a theme. The other two
154
 * types (multi and standard) can have one of the three themes.
155
 *
156
 * We have combined these into a `style` attribute as really there are 4 styles
157
 * standard, wide, tall, and button. Multi can be determined by the number of
158
 * restaurant IDs we have.
159
 *
160
 * This function along with `jetpack_opentable_block_get_type_attribute`, translates
161
 * the style attribute to a type and theme.
162
 *
163
 * Type        Theme      Style
164
 * ==========|==========|==========
165
 * Multi     |          |
166
 * Standard  | Standard | Standard
167
 *           | Wide     | Wide
168
 *           | Tall     | Tall
169
 * Button    | Standard | Button
170
 *
171
 * @param array $attributes Array of block attributes.
172
 *
173
 * @return string The filtered attribute
174
 */
175
function get_theme_attribute( $attributes ) {
176
	$valid_themes = array( 'standard', 'wide', 'tall' );
177
178
	if ( empty( $attributes['style'] )
179
		|| ! in_array( $attributes['style'], $valid_themes, true )
180
		|| 'button' === $attributes['style'] ) {
181
		return 'standard';
182
	}
183
184
	return $attributes['style'];
185
}
186
187
/**
188
 * Build an embed URL from an array of block attributes.
189
 *
190
 * @param array $attributes Array of block attributess.
191
 *
192
 * @return string Embed URL
193
 */
194
function build_embed_url( $attributes ) {
195
	$url = add_query_arg(
196
		array(
197
			'type'   => get_type_attribute( $attributes ),
198
			'theme'  => get_theme_attribute( $attributes ),
199
			'iframe' => get_attribute( $attributes, 'iframe' ),
200
			'domain' => get_attribute( $attributes, 'domain' ),
201
			'lang'   => get_attribute( $attributes, 'lang' ),
202
			'newtab' => get_attribute( $attributes, 'newtab' ),
203
		),
204
		'//www.opentable.com/widget/reservation/loader'
205
	);
206
207
	if ( ! empty( $attributes['rid'] ) ) {
208
		foreach ( $attributes['rid'] as $rid ) {
209
			$url .= '&rid=' . $rid;
210
		}
211
	}
212
213
	/**
214
	 * Filter the OpenTable URL used to embed a widget.
215
	 *
216
	 * @since 8.2.0
217
	 *
218
	 * @param string $url        OpenTable embed URL.
219
	 * @param array  $attributes Array of block attributes.
220
	 */
221
	return apply_filters( 'jetpack_opentable_block_url', $url, $attributes );
222
}
223