Completed
Push — fix/button-padding-in-opentabl... ( 9af520...6c5a80 )
by
unknown
06:46
created

opentable.php ➔ load_assets()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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