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