Completed
Push — fix/package-release-script ( 8004a7...315d04 )
by
unknown
06:57
created

extensions/blocks/opentable/opentable.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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