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() { |
|
|
|
|
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() { |
|
|
|
|
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 = \Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes ); |
95
|
|
|
$content = '<div class="' . esc_attr( $classes ) . '">'; |
96
|
|
|
// The OpenTable script uses multiple `rid` paramters, |
97
|
|
|
// so we can't use WordPress to output it, as WordPress attempts to validate it and removes them. |
98
|
|
|
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript |
99
|
|
|
$content .= '<script type="text/javascript" src="' . esc_url( build_embed_url( $attributes ) ) . '"></script>'; |
100
|
|
|
$content .= '</div>'; |
101
|
|
|
return $content; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the a block attribute |
106
|
|
|
* |
107
|
|
|
* @param array $attributes Array of block attributes. |
108
|
|
|
* @param string $attribute_name The attribute to get. |
109
|
|
|
* |
110
|
|
|
* @return string The filtered attribute |
111
|
|
|
*/ |
112
|
|
|
function get_attribute( $attributes, $attribute_name ) { |
113
|
|
|
if ( isset( $attributes[ $attribute_name ] ) ) { |
114
|
|
|
if ( in_array( $attribute_name, array( 'iframe', 'newtab' ), true ) ) { |
115
|
|
|
return $attributes[ $attribute_name ] ? 'true' : 'false'; |
116
|
|
|
} |
117
|
|
|
return $attributes[ $attribute_name ]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$default_attributes = array( |
121
|
|
|
'style' => 'standard', |
122
|
|
|
'iframe' => 'true', |
123
|
|
|
'domain' => 'com', |
124
|
|
|
'lang' => 'en-US', |
125
|
|
|
'newtab' => 'false', |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
return isset( $default_attributes[ $attribute_name ] ) ? $default_attributes[ $attribute_name ] : null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the block type attribute |
133
|
|
|
* |
134
|
|
|
* @param array $attributes Array of block attributes. |
135
|
|
|
* |
136
|
|
|
* @return string The filtered attribute |
137
|
|
|
*/ |
138
|
|
|
function get_type_attribute( $attributes ) { |
139
|
|
|
if ( ! empty( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) { |
140
|
|
|
return 'multi'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ( empty( $attributes['style'] ) || 'button' !== $attributes['style'] ) { |
144
|
|
|
return 'standard'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return 'button'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the block theme attribute |
152
|
|
|
* |
153
|
|
|
* OpenTable has a confusing mix of themes and types for the widget. A type |
154
|
|
|
* can have a theme, but the button style can not have a theme. The other two |
155
|
|
|
* types (multi and standard) can have one of the three themes. |
156
|
|
|
* |
157
|
|
|
* We have combined these into a `style` attribute as really there are 4 styles |
158
|
|
|
* standard, wide, tall, and button. Multi can be determined by the number of |
159
|
|
|
* restaurant IDs we have. |
160
|
|
|
* |
161
|
|
|
* This function along with `jetpack_opentable_block_get_type_attribute`, translates |
162
|
|
|
* the style attribute to a type and theme. |
163
|
|
|
* |
164
|
|
|
* Type Theme Style |
165
|
|
|
* ==========|==========|========== |
166
|
|
|
* Multi | | |
167
|
|
|
* Standard | Standard | Standard |
168
|
|
|
* | Wide | Wide |
169
|
|
|
* | Tall | Tall |
170
|
|
|
* Button | Standard | Button |
171
|
|
|
* |
172
|
|
|
* @param array $attributes Array of block attributes. |
173
|
|
|
* |
174
|
|
|
* @return string The filtered attribute |
175
|
|
|
*/ |
176
|
|
|
function get_theme_attribute( $attributes ) { |
177
|
|
|
$valid_themes = array( 'standard', 'wide', 'tall' ); |
178
|
|
|
|
179
|
|
|
if ( empty( $attributes['style'] ) |
180
|
|
|
|| ! in_array( $attributes['style'], $valid_themes, true ) |
181
|
|
|
|| 'button' === $attributes['style'] ) { |
182
|
|
|
return 'standard'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $attributes['style']; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Build an embed URL from an array of block attributes. |
190
|
|
|
* |
191
|
|
|
* @param array $attributes Array of block attributess. |
192
|
|
|
* |
193
|
|
|
* @return string Embed URL |
194
|
|
|
*/ |
195
|
|
|
function build_embed_url( $attributes ) { |
196
|
|
|
$url = add_query_arg( |
197
|
|
|
array( |
198
|
|
|
'type' => get_type_attribute( $attributes ), |
199
|
|
|
'theme' => get_theme_attribute( $attributes ), |
200
|
|
|
'iframe' => get_attribute( $attributes, 'iframe' ), |
201
|
|
|
'domain' => get_attribute( $attributes, 'domain' ), |
202
|
|
|
'lang' => get_attribute( $attributes, 'lang' ), |
203
|
|
|
'newtab' => get_attribute( $attributes, 'newtab' ), |
204
|
|
|
), |
205
|
|
|
'//www.opentable.com/widget/reservation/loader' |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
if ( ! empty( $attributes['rid'] ) ) { |
209
|
|
|
foreach ( $attributes['rid'] as $rid ) { |
210
|
|
|
$url .= '&rid=' . $rid; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Filter the OpenTable URL used to embed a widget. |
216
|
|
|
* |
217
|
|
|
* @since 8.2.0 |
218
|
|
|
* |
219
|
|
|
* @param string $url OpenTable embed URL. |
220
|
|
|
* @param array $attributes Array of block attributes. |
221
|
|
|
*/ |
222
|
|
|
return apply_filters( 'jetpack_opentable_block_url', $url, $attributes ); |
223
|
|
|
} |
224
|
|
|
|
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.