Completed
Push — add/changelog-71 ( b70f6c...ac535c )
by Jeremy
10:29
created

mailchimp.php ➔ jetpack_mailchimp_block_load_assets()   B

Complexity

Conditions 8
Paths 49

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 49
nop 1
dl 0
loc 66
rs 7.4973
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
	jetpack_register_block(
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
	if ( ! jetpack_mailchimp_verify_connection() ) {
28
		return null;
29
	}
30
	$values  = array();
31
	$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
32
		get_current_blog_id() : Jetpack_Options::get_option( 'id' );
33
	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...
34
	$defaults = array(
35
		'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
36
		'submitLabel'      => esc_html__( 'Join my email list', 'jetpack' ),
37
		'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' ),
38
		'processingLabel'  => esc_html__( 'Processing…', 'jetpack' ),
39
		'successLabel'     => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
40
		'errorLabel'       => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
41
	);
42
	foreach ( $defaults as $id => $default ) {
43
		$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
44
	}
45
46
	/* TODO: replace with centralized block_class function */
47
	$align   = isset( $attr['align'] ) ? $attr['align'] : 'center';
48
	$type    = 'mailchimp';
49
	$classes = array(
50
		'wp-block-jetpack-' . $type,
51
		'align' . $align,
52
	);
53
	if ( isset( $attr['className'] ) ) {
54
		array_push( $classes, $attr['className'] );
55
	}
56
	$classes = implode( $classes, ' ' );
57
58
	ob_start();
59
	?>
60
	<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
61
		<div class="components-placeholder">
62
			<form>
63
				<input
64
					type="email"
65
					required
66
					placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
67
				/>
68
				<button type="submit" class="components-button is-button is-primary">
69
					<?php echo wp_kses_post( $values['submitLabel'] ); ?>
70
				</button>
71
				<p>
72
					<small>
73
						<?php echo wp_kses_post( $values['consentText'] ); ?>
74
					</small>
75
				</p>
76
			</form>
77
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing">
78
				<?php echo esc_html( $values['processingLabel'] ); ?>
79
			</div>
80
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success">
81
				<?php echo esc_html( $values['successLabel'] ); ?>
82
			</div>
83
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error">
84
				<?php echo esc_html( $values['errorLabel'] ); ?>
85
			</div>
86
		</div>
87
	</div>
88
	<?php
89
	$html = ob_get_clean();
90
	return $html;
91
}
92
93
/**
94
 * Mailchimp connection/list selection verification.
95
 *
96
 * @return boolean
97
 */
98 View Code Duplication
function jetpack_mailchimp_verify_connection() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
	$option = get_option( 'jetpack_mailchimp' );
100
	if ( ! $option ) {
101
		return false;
102
	}
103
	$data = json_decode( $option, true );
104
	if ( ! $data ) {
105
		return false;
106
	}
107
	return isset( $data['follower_list_id'], $data['keyring_id'] );
108
}
109