Completed
Push — fix/youtube-shortcode-amp-comp... ( 0aea78...87f47e )
by
unknown
08:56
created

opentable.php ➔ load_assets()   C

Complexity

Conditions 11
Paths 32

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 32
nop 1
dl 0
loc 66
rs 6.5951
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Automattic\Jetpack\Blocks;
13
use Jetpack_Gutenberg;
14
15
const FEATURE_NAME = 'opentable';
16
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
17
18
/**
19
 * Registers the block for use in Gutenberg
20
 * This is done via an action so that we can disable
21
 * registration if we need to.
22
 */
23
function register_block() {
24
	Blocks::jetpack_register_block(
25
		BLOCK_NAME,
26
		array(
27
			'render_callback' => __NAMESPACE__ . '\load_assets',
28
			'plan_check'      => true,
29
		)
30
	);
31
}
32
add_action( 'init', __NAMESPACE__ . '\register_block' );
33
34
/**
35
 * Adds an inline script which updates the block editor settings to
36
 * add the site locale. This feels sligktly better than calling back
37
 * to the API before registering the block. It also seemed better than
38
 * creating a global
39
 */
40
function add_language_setting() {
41
	wp_add_inline_script( 'jetpack-blocks-editor', sprintf( "wp.data.dispatch( 'core/block-editor' ).updateSettings( { siteLocale: '%s' } )", str_replace( '_', '-', get_locale() ) ), 'before' );
42
}
43
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\add_language_setting' );
44
45
/**
46
 * OpenTable block registration/dependency declaration.
47
 *
48
 * @param array $attributes    Array containing the OpenTable block attributes.
49
 *
50
 * @return string
51
 */
52
function load_assets( $attributes ) {
53
54
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
55
56
	$classes    = array();
57
	$class_name = get_attribute( $attributes, 'className' );
58
	$style      = get_attribute( $attributes, 'style' );
59
60
	if ( 'wide' === $style && jetpack_is_mobile() ) {
61
		$attributes = array_merge( $attributes, array( 'style' => 'standard' ) );
62
		$classes[]  = 'is-style-mobile';
63
	}
64
65
	// Handles case of deprecated version using theme instead of block styles.
66
	if ( ! $class_name || strpos( $class_name, 'is-style-' ) === false ) {
67
		$classes[] = sprintf( 'is-style-%s', $style );
68
	}
69
70
	if ( array_key_exists( 'rid', $attributes ) && is_array( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) {
71
		$classes[] = 'is-multi';
72
	}
73
	if ( array_key_exists( 'negativeMargin', $attributes ) && $attributes['negativeMargin'] ) {
74
		$classes[] = 'has-no-margin';
75
	}
76
	$classes = Blocks::classes( FEATURE_NAME, $attributes, $classes );
77
	$content = '<div class="' . esc_attr( $classes ) . '">';
78
79
	$script_url = build_embed_url( $attributes );
80
81
	if ( Blocks::is_amp_request() ) {
82
		// Extract params from URL since it had jetpack_opentable_block_url filters applied.
83
		$url_query = \wp_parse_url( $script_url, PHP_URL_QUERY ) . '&overlay=false&disablega=false';
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_QUERY.

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...
84
85
		$src = "https://www.opentable.com/widget/reservation/canvas?$url_query";
86
87
		$params = array();
88
		wp_parse_str( $url_query, $params );
89
90
		// Note an iframe is similarly constructed in the block edit function.
91
		$content .= sprintf(
92
			'<amp-iframe src="%s" layout="fill" sandbox="allow-scripts allow-forms allow-same-origin allow-popups">%s</amp-iframe>',
93
			esc_url( $src ),
94
			sprintf(
95
				'<a placeholder href="%s">%s</a>',
96
				esc_url(
97
					add_query_arg(
98
						array(
99
							'rid' => $params['rid'],
100
						),
101
						'https://www.opentable.com/restref/client/'
102
					)
103
				),
104
				esc_html__( 'Make a reservation', 'jetpack' )
105
			)
106
		);
107
	} else {
108
		// The OpenTable script uses multiple `rid` paramters,
109
		// so we can't use WordPress to output it, as WordPress attempts to validate it and removes them.
110
		// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
111
		$content .= '<script src="' . esc_url( $script_url ) . '"></script>';
112
	}
113
114
	$content .= '</div>';
115
116
	return $content;
117
}
118
119
/**
120
 * Get the a block attribute
121
 *
122
 * @param array  $attributes Array of block attributes.
123
 * @param string $attribute_name The attribute to get.
124
 *
125
 * @return string The filtered attribute
126
 */
127
function get_attribute( $attributes, $attribute_name ) {
128
	if ( isset( $attributes[ $attribute_name ] ) ) {
129
		if ( in_array( $attribute_name, array( 'iframe', 'newtab' ), true ) ) {
130
			return $attributes[ $attribute_name ] ? 'true' : 'false';
131
		}
132
		return $attributes[ $attribute_name ];
133
	}
134
135
	$default_attributes = array(
136
		'style'  => 'standard',
137
		'iframe' => 'true',
138
		'domain' => 'com',
139
		'lang'   => 'en-US',
140
		'newtab' => 'false',
141
	);
142
143
	return isset( $default_attributes[ $attribute_name ] ) ? $default_attributes[ $attribute_name ] : null;
144
}
145
146
/**
147
 * Get the block type attribute
148
 *
149
 * @param array $attributes Array of block attributes.
150
 *
151
 * @return string The filtered attribute
152
 */
153
function get_type_attribute( $attributes ) {
154
	if ( ! empty( $attributes['rid'] ) && count( $attributes['rid'] ) > 1 ) {
155
		return 'multi';
156
	}
157
158
	if ( empty( $attributes['style'] ) || 'button' !== $attributes['style'] ) {
159
		return 'standard';
160
	}
161
162
	return 'button';
163
}
164
165
/**
166
 * Get the block theme attribute
167
 *
168
 * OpenTable has a confusing mix of themes and types for the widget. A type
169
 * can have a theme, but the button style can not have a theme. The other two
170
 * types (multi and standard) can have one of the three themes.
171
 *
172
 * We have combined these into a `style` attribute as really there are 4 styles
173
 * standard, wide, tall, and button. Multi can be determined by the number of
174
 * restaurant IDs we have.
175
 *
176
 * This function along with `jetpack_opentable_block_get_type_attribute`, translates
177
 * the style attribute to a type and theme.
178
 *
179
 * Type        Theme      Style
180
 * ==========|==========|==========
181
 * Multi     |          |
182
 * Standard  | Standard | Standard
183
 *           | Wide     | Wide
184
 *           | Tall     | Tall
185
 * Button    | Standard | Button
186
 *
187
 * @param array $attributes Array of block attributes.
188
 *
189
 * @return string The filtered attribute
190
 */
191
function get_theme_attribute( $attributes ) {
192
	$valid_themes = array( 'standard', 'wide', 'tall' );
193
194
	if ( empty( $attributes['style'] )
195
		|| ! in_array( $attributes['style'], $valid_themes, true )
196
		|| 'button' === $attributes['style'] ) {
197
		return 'standard';
198
	}
199
200
	return $attributes['style'];
201
}
202
203
/**
204
 * Build an embed URL from an array of block attributes.
205
 *
206
 * @param array $attributes Array of block attributess.
207
 *
208
 * @return string Embed URL
209
 */
210
function build_embed_url( $attributes ) {
211
	$url = add_query_arg(
212
		array(
213
			'type'   => get_type_attribute( $attributes ),
214
			'theme'  => get_theme_attribute( $attributes ),
215
			'iframe' => get_attribute( $attributes, 'iframe' ),
216
			'domain' => get_attribute( $attributes, 'domain' ),
217
			'lang'   => get_attribute( $attributes, 'lang' ),
218
			'newtab' => get_attribute( $attributes, 'newtab' ),
219
		),
220
		'//www.opentable.com/widget/reservation/loader'
221
	);
222
223
	if ( ! empty( $attributes['rid'] ) ) {
224
		foreach ( $attributes['rid'] as $rid ) {
225
			$url .= '&rid=' . $rid;
226
		}
227
	}
228
229
	/**
230
	 * Filter the OpenTable URL used to embed a widget.
231
	 *
232
	 * @since 8.2.0
233
	 *
234
	 * @param string $url        OpenTable embed URL.
235
	 * @param array  $attributes Array of block attributes.
236
	 */
237
	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...
238
}
239