Completed
Push — update/editor-blocks-icon-colo... ( 093ab2...3cfb5e )
by
unknown
08:47
created

opentable.php ➔ add_language_setting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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();
56
	$class_name = get_attribute( $attributes, 'className' );
57
58
	// Handles case of deprecated version using theme instead of block styles.
59
	if ( ! $class_name || strpos( $class_name, 'is-style-' ) === false ) {
60
		$classes[] = sprintf( 'is-style-%s', get_attribute( $attributes, 'style' ) );
61
	}
62
63
	if ( array_key_exists( 'rid', $attributes ) && is_array( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) {
64
		$classes[] = 'is-multi';
65
	}
66
	if ( array_key_exists( 'negativeMargin', $attributes ) && $attributes['negativeMargin'] ) {
67
		$classes[] = 'has-no-margin';
68
	}
69
	$classes = Jetpack_Gutenberg::block_classes(
70
		FEATURE_NAME,
71
		$attributes,
72
		$classes
73
	);
74
	$content = '<div class="' . esc_attr( $classes ) . '">';
75
	// The OpenTable script uses multiple `rid` paramters,
76
	// so we can't use WordPress to output it, as WordPress attempts to validate it and removes them.
77
	// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
78
	$content .= '<script type="text/javascript" src="' . esc_url( build_embed_url( $attributes ) ) . '"></script>';
79
	$content .= '</div>';
80
	return $content;
81
}
82
83
/**
84
 * Get the a block attribute
85
 *
86
 * @param array  $attributes Array of block attributes.
87
 * @param string $attribute_name The attribute to get.
88
 *
89
 * @return string The filtered attribute
90
 */
91
function get_attribute( $attributes, $attribute_name ) {
92
	if ( isset( $attributes[ $attribute_name ] ) ) {
93
		if ( in_array( $attribute_name, array( 'iframe', 'newtab' ), true ) ) {
94
			return $attributes[ $attribute_name ] ? 'true' : 'false';
95
		}
96
		return $attributes[ $attribute_name ];
97
	}
98
99
	$default_attributes = array(
100
		'style'  => 'standard',
101
		'iframe' => 'true',
102
		'domain' => 'com',
103
		'lang'   => 'en-US',
104
		'newtab' => 'false',
105
	);
106
107
	return isset( $default_attributes[ $attribute_name ] ) ? $default_attributes[ $attribute_name ] : null;
108
}
109
110
/**
111
 * Get the block type attribute
112
 *
113
 * @param array $attributes Array of block attributes.
114
 *
115
 * @return string The filtered attribute
116
 */
117
function get_type_attribute( $attributes ) {
118
	if ( ! empty( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) {
119
		return 'multi';
120
	}
121
122
	if ( empty( $attributes['style'] ) || 'button' !== $attributes['style'] ) {
123
		return 'standard';
124
	}
125
126
	return 'button';
127
}
128
129
/**
130
 * Get the block theme attribute
131
 *
132
 * OpenTable has a confusing mix of themes and types for the widget. A type
133
 * can have a theme, but the button style can not have a theme. The other two
134
 * types (multi and standard) can have one of the three themes.
135
 *
136
 * We have combined these into a `style` attribute as really there are 4 styles
137
 * standard, wide, tall, and button. Multi can be determined by the number of
138
 * restaurant IDs we have.
139
 *
140
 * This function along with `jetpack_opentable_block_get_type_attribute`, translates
141
 * the style attribute to a type and theme.
142
 *
143
 * Type        Theme      Style
144
 * ==========|==========|==========
145
 * Multi     |          |
146
 * Standard  | Standard | Standard
147
 *           | Wide     | Wide
148
 *           | Tall     | Tall
149
 * Button    | Standard | Button
150
 *
151
 * @param array $attributes Array of block attributes.
152
 *
153
 * @return string The filtered attribute
154
 */
155
function get_theme_attribute( $attributes ) {
156
	$valid_themes = array( 'standard', 'wide', 'tall' );
157
158
	if ( empty( $attributes['style'] )
159
		|| ! in_array( $attributes['style'], $valid_themes, true )
160
		|| 'button' === $attributes['style'] ) {
161
		return 'standard';
162
	}
163
164
	return $attributes['style'];
165
}
166
167
/**
168
 * Build an embed URL from an array of block attributes.
169
 *
170
 * @param array $attributes Array of block attributess.
171
 *
172
 * @return string Embed URL
173
 */
174
function build_embed_url( $attributes ) {
175
	$url = add_query_arg(
176
		array(
177
			'type'   => get_type_attribute( $attributes ),
178
			'theme'  => get_theme_attribute( $attributes ),
179
			'iframe' => get_attribute( $attributes, 'iframe' ),
180
			'domain' => get_attribute( $attributes, 'domain' ),
181
			'lang'   => get_attribute( $attributes, 'lang' ),
182
			'newtab' => get_attribute( $attributes, 'newtab' ),
183
		),
184
		'//www.opentable.com/widget/reservation/loader'
185
	);
186
187
	if ( ! empty( $attributes['rid'] ) ) {
188
		foreach ( $attributes['rid'] as $rid ) {
189
			$url .= '&rid=' . $rid;
190
		}
191
	}
192
193
	/**
194
	 * Filter the OpenTable URL used to embed a widget.
195
	 *
196
	 * @since 8.2.0
197
	 *
198
	 * @param string $url        OpenTable embed URL.
199
	 * @param array  $attributes Array of block attributes.
200
	 */
201
	return apply_filters( 'jetpack_opentable_block_url', $url, $attributes );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $attributes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
202
}
203