Completed
Push — fix/15025-map-block-infinite-s... ( 9a8b21...52d6c3 )
by
unknown
63:16 queued 56:01
created

opentable.php ➔ load_assets()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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