Completed
Push — update/jetpack-search-watch-bu... ( 10cd86...c6b46b )
by Jeremy
06:39
created

opentable.php ➔ load_assets()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 1
dl 0
loc 27
rs 8.5546
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 Automattic\Jetpack\Extensions\OpenTable;
11
12
use Jetpack_Gutenberg;
13
14
const FEATURE_NAME  = 'opentable';
15
const BLOCK_NAME    = 'jetpack/' . FEATURE_NAME;
16
const REQUIRED_PLAN = 'value_bundle';
17
18
/**
19
 * Check if the block should be available on the site.
20
 *
21
 * @return bool
22
 */
23 View Code Duplication
function is_available() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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.

Loading history...
24
	if (
25
		defined( 'IS_WPCOM' )
26
		&& IS_WPCOM
27
		&& function_exists( 'has_any_blog_stickers' )
28
	) {
29
		if ( has_any_blog_stickers(
30
			array( 'premium-plan', 'business-plan', 'ecommerce-plan' ),
31
			get_current_blog_id()
32
		) ) {
33
			return true;
34
		}
35
		return false;
36
	}
37
38
	return true;
39
}
40
41
/**
42
 * Registers the block for use in Gutenberg
43
 * This is done via an action so that we can disable
44
 * registration if we need to.
45
 */
46
function register_block() {
47
	jetpack_register_block(
48
		BLOCK_NAME,
49
		array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
50
	);
51
}
52
add_action( 'init', __NAMESPACE__ . '\register_block' );
53
54
/**
55
 * Set the availability of the block as the editor
56
 * is loaded.
57
 */
58 View Code Duplication
function set_availability() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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.

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