Completed
Push — update/wpcom-premium-block-reg... ( ceb48a )
by Jeremy
07:15
created

opentable.php ➔ register_block()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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