Completed
Push — add/rest-api-media-videopress ( fb887c...363acf )
by
unknown
11:15
created

blocks.php ➔ jetpack_mailchimp_block_load_assets()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 48
nop 1
dl 0
loc 64
rs 7.8521
c 0
b 0
f 0

How to fix   Long Method   

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
 * Load code specific to Gutenberg blocks which are not tied to a module.
4
 * This file is unusual, and is not an actual `module` as such.
5
 * It is included in ./module-extras.php
6
 */
7
8
/**
9
 * Map block.
10
 *
11
 * @since 6.8.0
12
 */
13
jetpack_register_block(
14
	'map',
15
	array(
16
		'render_callback' => 'jetpack_map_block_load_assets',
17
	)
18
);
19
20
/**
21
 * Map block registration/dependency declaration.
22
 *
23
 * @param array $attr - Array containing the map block attributes.
24
 * @param string $content - String containing the map block content.
25
 *
26
 * @return string
27
 */
28
function jetpack_map_block_load_assets( $attr, $content ) {
29
	$dependencies = array(
30
		'lodash',
31
		'wp-element',
32
		'wp-i18n',
33
	);
34
35
	$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
36
37
	Jetpack_Gutenberg::load_assets_as_required( 'map', $dependencies );
38
39
	return preg_replace( '/<div /', '<div data-api-key="' . esc_attr( $api_key ) . '" ', $content, 1 );
40
}
41
42
43
/**
44
 * Tiled Gallery block. Depends on the Photon module.
45
 *
46
 * @since 6.9.0
47
 */
48
if (
49
	( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
50
	class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' )
51
) {
52
	jetpack_register_block(
53
		'tiled-gallery',
54
		array(
55
			'render_callback' => 'jetpack_tiled_gallery_load_block_assets',
56
		)
57
	);
58
59
	/**
60
	 * Tiled gallery block registration/dependency declaration.
61
	 *
62
	 * @param array $attr - Array containing the block attributes.
63
	 * @param string $content - String containing the block content.
64
	 *
65
	 * @return string
66
	 */
67
	function jetpack_tiled_gallery_load_block_assets( $attr, $content ) {
68
		$dependencies = array(
69
			'lodash',
70
			'wp-i18n',
71
			'wp-token-list',
72
		);
73
		Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery', $dependencies );
74
75
		/**
76
		 * Filter the output of the Tiled Galleries content.
77
		 *
78
		 * @module tiled-gallery
79
		 *
80
		 * @since 6.9.0
81
		 *
82
		 * @param string $content Tiled Gallery block content.
83
		 */
84
		return apply_filters( 'jetpack_tiled_galleries_block_content', $content );
85
	}
86
}
87
88
/**
89
 * GIF Block.
90
 *
91
 * @since 7.0.0
92
 */
93
jetpack_register_block(
94
	'gif',
95
	array(
96
		'render_callback' => 'jetpack_gif_block_render',
97
	)
98
);
99
100
/**
101
 * Gif block registration/dependency declaration.
102
 *
103
 * @param array $attr - Array containing the gif block attributes.
104
 *
105
 * @return string
106
 */
107
function jetpack_gif_block_render( $attr ) {
108
	$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
109
	$style       = 'padding-top:' . $padding_top;
110
	$giphy_url   = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : null;
111
	$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
112
	$caption     = isset( $attr['caption'] ) ? $attr['caption'] : null;
113
114
	if ( ! $giphy_url ) {
115
		return null;
116
	}
117
118
	/* TODO: replace with centralized block_class function */
119
	$align   = isset( $attr['align'] ) ? $attr['align'] : 'center';
120
	$type    = 'gif';
121
	$classes = array(
122
		'wp-block-jetpack-' . $type,
123
		'align' . $align,
124
	);
125
	if ( isset( $attr['className'] ) ) {
126
		array_push( $classes, $attr['className'] );
127
	}
128
	$classes = implode( $classes, ' ' );
129
130
	ob_start();
131
	?>
132
	<div class="<?php echo esc_attr( $classes ); ?>">
133
		<figure>
134
			<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
135
				<iframe src="<?php echo esc_url( $giphy_url ); ?>"
136
						title="<?php echo esc_attr( $search_text ); ?>"></iframe>
137
			</div>
138
			<?php if ( $caption ) : ?>
139
				<figcaption
140
						class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
141
			<?php endif; ?>
142
		</figure>
143
	</div>
144
	<?php
145
	$html = ob_get_clean();
146
147
	Jetpack_Gutenberg::load_assets_as_required( 'gif' );
148
149
	return $html;
150
}
151
152
/**
153
 * Contact Info block and its child blocks.
154
 */
155
jetpack_register_block(
156
	'contact-info',
157
	array(
158
		'render_callback' => 'jetpack_contact_info_block_load_assets',
159
	)
160
);
161
jetpack_register_block(
162
	'email',
163
	array( 'parent' => array( 'jetpack/contact-info' ) )
164
);
165
jetpack_register_block(
166
	'address',
167
	array( 'parent' => array( 'jetpack/contact-info' ) )
168
);
169
jetpack_register_block(
170
	'phone',
171
	array( 'parent' => array( 'jetpack/contact-info' ) )
172
);
173
174
/**
175
 * Contact info block registration/dependency declaration.
176
 *
177
 * @param array  $attr - Array containing the contact info block attributes.
178
 * @param string $content - String containing the contact info block content.
179
 *
180
 * @return string
181
 */
182
function jetpack_contact_info_block_load_assets( $attr, $content ) {
183
	Jetpack_Gutenberg::load_assets_as_required( 'contact-info' );
184
	return $content;
185
}
186
187
/**
188
 * VR Block.
189
 */
190
jetpack_register_block( 'vr' );
191
192
/**
193
 * Slideshow Block.
194
 */
195
jetpack_register_block(
196
	'slideshow',
197
	array(
198
		'render_callback' => 'jetpack_slideshow_block_load_assets',
199
	)
200
);
201
202
/**
203
 * Slideshow block registration/dependency declaration.
204
 *
205
 * @param array  $attr - Array containing the slideshow block attributes.
206
 * @param string $content - String containing the slideshow block content.
207
 *
208
 * @return string
209
 */
210
function jetpack_slideshow_block_load_assets( $attr, $content ) {
211
	$dependencies = array(
212
		'lodash',
213
		'wp-element',
214
		'wp-i18n',
215
	);
216
	Jetpack_Gutenberg::load_assets_as_required( 'slideshow', $dependencies );
217
218
	return $content;
219
}
220
221
/**
222
 * Business Hours Block.
223
 */
224
jetpack_register_block(
225
	'business-hours',
226
	array( 'render_callback' => 'jetpack_business_hours_render' )
227
);
228
229
/**
230
 * Business Hours Block dynamic rending of the glock.
231
 *
232
 * @param array  $attributes Array containing the business hours block attributes.
233
 * @param string $content    String containing the business hours block content.
234
 *
235
 * @return string
236
 */
237
function jetpack_business_hours_render( $attributes, $content ) {
238
	global $wp_locale;
239
240
	if ( empty( $attributes['hours'] ) || ! is_array( $attributes['hours'] ) ) {
241
		return $content;
242
	}
243
244
	$start_of_week     = (int) get_option( 'start_of_week', 0 );
245
	$time_format       = get_option( 'time_format' );
246
	$today             = current_time( 'D' );
247
	$custom_class_name = isset( $attributes['className'] ) ? $attributes['className'] : '';
0 ignored issues
show
Unused Code introduced by
$custom_class_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
248
	$content           = sprintf(
249
		'<dl class="jetpack-business-hours %s">',
250
		! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
251
	);
252
253
	$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
254
255
	if ( $start_of_week ) {
256
		$chunk1              = array_slice( $attributes['hours'], 0, $start_of_week );
257
		$chunk2              = array_slice( $attributes['hours'], $start_of_week );
258
		$attributes['hours'] = array_merge( $chunk2, $chunk1 );
259
	}
260
261
	foreach ( $attributes['hours'] as $day => $hours ) {
262
		$opening = strtotime( $hours['opening'] );
263
		$closing = strtotime( $hours['closing'] );
264
265
		$content .= '<dt class="' . esc_attr( $day ) . '">' .
266
			ucfirst( $wp_locale->get_weekday( array_search( $day, $days ) ) ) .
267
			'</dt>';
268
		$content .= '<dd class="' . esc_attr( $day ) . '">';
269
		if ( $hours['opening'] && $hours['closing'] ) {
270
			$content .= sprintf(
271
				/* Translators: Business opening hours info. */
272
				_x( 'From %1$s to %2$s', 'from business opening hour to closing hour', 'jetpack' ),
273
				date( $time_format, $opening ),
274
				date( $time_format, $closing )
275
			);
276
277
			if ( $today === $day ) {
278
				$now = strtotime( current_time( 'H:i' ) );
279
				if ( $now < $opening ) {
280
					$content .= '<br />';
281
					$content .= esc_html( sprintf(
282
						/* Translators: Amount of time until business opens. */
283
						_x( 'Opening in %s', 'Amount of time until business opens', 'jetpack' ),
284
						human_time_diff( $now, $opening )
285
					) );
286
				} elseif ( $now >= $opening && $now < $closing ) {
287
					$content .= '<br />';
288
					$content .= esc_html( sprintf(
289
						/* Translators: Amount of time until business closes. */
290
						_x( 'Closing in %s', 'Amount of time until business closes', 'jetpack' ),
291
						human_time_diff( $now, $closing )
292
					) );
293
				}
294
			}
295
		} else {
296
			$content .= esc_html__( 'CLOSED', 'jetpack' );
297
		}
298
		$content .= '</dd>';
299
	}
300
301
	$content .= '</dl>';
302
303
	return $content;
304
}
305
306
/**
307
 * Mailchimp Block.
308
 */
309
if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) {
310
	jetpack_register_block(
311
		'mailchimp',
312
		array(
313
			'render_callback' => 'jetpack_mailchimp_block_load_assets',
314
		)
315
	);
316
}
317
318
/**
319
 * Mailchimp block registration/dependency declaration.
320
 *
321
 * @param array $attr - Array containing the map block attributes.
322
 *
323
 * @return string
324
 */
325
function jetpack_mailchimp_block_load_assets( $attr ) {
326
	$values  = array();
327
	$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
328
		get_current_blog_id() : Jetpack_Options::get_option( 'id' );
329
	Jetpack_Gutenberg::load_assets_as_required( 'mailchimp', null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
330
	$defaults = array(
331
		'title'            => esc_html__( 'Join my email list', 'jetpack' ),
332
		'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
333
		'submitLabel'      => esc_html__( 'Join My Email List', 'jetpack' ),
334
		'consentText'      => esc_html__( 'By clicking submit, you agree to share your email address with the site owner and MailChimp to receive marketing, updates, and other emails from the site owner. Use the unsubscribe link in those emails to opt out at any time.', 'jetpack' ),
335
		'processingLabel'  => esc_html__( 'Processing…', 'jetpack' ),
336
		'successLabel'     => esc_html__( 'Success! You\'ve been added to the list.', 'jetpack' ),
337
		'errorLabel'       => esc_html__( 'Oh no! Unfortunately there was an error. Please try reloading this page and adding your email once more.', 'jetpack' ),
338
	);
339
	foreach ( $defaults as $id => $default ) {
340
		$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
341
	}
342
343
	/* TODO: replace with centralized block_class function */
344
	$align   = isset( $attr['align'] ) ? $attr['align'] : 'center';
345
	$type    = 'mailchimp';
346
	$classes = array(
347
		'wp-block-jetpack-' . $type,
348
		'align' . $align,
349
	);
350
	if ( isset( $attr['className'] ) ) {
351
		array_push( $classes, $attr['className'] );
352
	}
353
	$classes = implode( $classes, ' ' );
354
355
	ob_start();
356
	?>
357
	<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
358
		<div class="components-placeholder">
359
			<h3><?php echo esc_html( $values['title'] ); ?></h3>
360
			<form>
361
				<input
362
					type="text"
363
					class="components-text-control__input wp-block-jetpack-mailchimp-email"
364
					required
365
					placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
366
				/>
367
				<button type="submit" class="components-button is-button is-primary">
368
					<?php echo esc_html( $values['submitLabel'] ); ?>
369
				</button>
370
				<figcaption>
371
					<?php echo esc_html( $values['consentText'] ); ?>
372
				</figcaption>
373
			</form>
374
			<div class="wp-block-jetpack-mailchimp-notification wp-block-jetpack-mailchimp-processing">
375
				<?php echo esc_html( $values['processingLabel'] ); ?>
376
			</div>
377
			<div class="wp-block-jetpack-mailchimp-notification wp-block-jetpack-mailchimp-success">
378
				<?php echo esc_html( $values['successLabel'] ); ?>
379
			</div>
380
			<div class="wp-block-jetpack-mailchimp-notification wp-block-jetpack-mailchimp-error">
381
				<?php echo esc_html( $values['errorLabel'] ); ?>
382
			</div>
383
		</div>
384
	</div>
385
	<?php
386
	$html = ob_get_clean();
387
	return $html;
388
}
389