Completed
Push — update/mailchimp-ui-changes ( f4a7d6...e71283 )
by
unknown
11:27
created

mailchimp.php ➔ jetpack_mailchimp_block_load_assets()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 48
nop 1
dl 0
loc 63
rs 7.8739
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
 * Mailchimp Block.
4
 *
5
 * @since 7.1.0
6
 *
7
 * @package Jetpack
8
 */
9
10
if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) {
11
	register_block_type(
12
		'jetpack/mailchimp',
13
		array(
14
			'render_callback' => 'jetpack_mailchimp_block_load_assets',
15
		)
16
	);
17
}
18
19
/**
20
 * Mailchimp block registration/dependency declaration.
21
 *
22
 * @param array $attr - Array containing the map block attributes.
23
 *
24
 * @return string
25
 */
26
function jetpack_mailchimp_block_load_assets( $attr ) {
27
	$values  = array();
28
	$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
29
		get_current_blog_id() : Jetpack_Options::get_option( 'id' );
30
	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...
31
	$defaults = array(
32
		'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
33
		'submitLabel'      => esc_html__( 'Join my email list', 'jetpack' ),
34
		'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' ),
35
		'processingLabel'  => esc_html__( 'Processing…', 'jetpack' ),
36
		'successLabel'     => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
37
		'errorLabel'       => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
38
	);
39
	foreach ( $defaults as $id => $default ) {
40
		$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
41
	}
42
43
	/* TODO: replace with centralized block_class function */
44
	$align   = isset( $attr['align'] ) ? $attr['align'] : 'center';
45
	$type    = 'mailchimp';
46
	$classes = array(
47
		'wp-block-jetpack-' . $type,
48
		'align' . $align,
49
	);
50
	if ( isset( $attr['className'] ) ) {
51
		array_push( $classes, $attr['className'] );
52
	}
53
	$classes = implode( $classes, ' ' );
54
55
	ob_start();
56
	?>
57
	<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
58
		<div class="components-placeholder">
59
			<form>
60
				<input
61
					type="email"
62
					required
63
					placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
64
				/>
65
				<button type="submit" class="components-button is-button is-primary">
66
					<?php echo esc_html( $values['submitLabel'] ); ?>
67
				</button>
68
				<p class="wp-block-jetpack-mailchimp_consent-text">
69
					<small>
70
						<?php echo esc_html( $values['consentText'] ); ?>
71
					</small>
72
				</p>
73
			</form>
74
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing">
75
				<?php echo esc_html( $values['processingLabel'] ); ?>
76
			</div>
77
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success">
78
				<?php echo esc_html( $values['successLabel'] ); ?>
79
			</div>
80
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error">
81
				<?php echo esc_html( $values['errorLabel'] ); ?>
82
			</div>
83
		</div>
84
	</div>
85
	<?php
86
	$html = ob_get_clean();
87
	return $html;
88
}
89