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