Completed
Push — update/composer-lock ( 4bc3dc...ab53f1 )
by Jeremy
08:14
created

mailchimp.php ➔ jetpack_mailchimp_block_load_assets()   F

Complexity

Conditions 13
Paths 1537

Size

Total Lines 129

Duplication

Lines 47
Ratio 36.43 %

Importance

Changes 0
Metric Value
cc 13
nc 1537
nop 1
dl 47
loc 129
rs 1.96
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
 * 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
28
	if ( ! jetpack_mailchimp_verify_connection() ) {
29
		return null;
30
	}
31
32
	$values  = array();
33
	$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
34
		? get_current_blog_id()
35
		: Jetpack_Options::get_option( 'id' );
36
	Jetpack_Gutenberg::load_assets_as_required( 'mailchimp' );
37
	$defaults = array(
38
		'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
39
		'submitButtonText' => esc_html__( 'Join my email list', 'jetpack' ),
40
		'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' ),
41
		'processingLabel'  => esc_html__( 'Processing…', 'jetpack' ),
42
		'successLabel'     => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
43
		'errorLabel'       => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
44
	);
45
	foreach ( $defaults as $id => $default ) {
46
		$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
47
	}
48
49
	$values['submitButtonText'] = empty( $values['submitButtonText'] ) ? $defaults['submitButtonText'] : $values['submitButtonText'];
50
51
	$classes = Jetpack_Gutenberg::block_classes( 'mailchimp', $attr );
52
53
	$button_styles = array();
54
	if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
55
		array_push(
56
			$button_styles,
57
			sprintf(
58
				'background-color: %s',
59
				sanitize_hex_color( $attr['customBackgroundButtonColor'] )
60
			)
61
		);
62
	}
63 View Code Duplication
	if ( ! empty( $attr['customTextButtonColor'] ) ) {
64
		array_push(
65
			$button_styles,
66
			sprintf(
67
				'color: %s',
68
				sanitize_hex_color( $attr['customTextButtonColor'] )
69
			)
70
		);
71
	}
72
	$button_styles   = implode( ';', $button_styles );
73
	$amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id );
74
	$is_amp_request  = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
75
76
	ob_start();
77
	?>
78
79
	<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
80
		<div class="components-placeholder">
81
			<form
82
				aria-describedby="wp-block-jetpack-mailchimp_consent-text"
83
				<?php if ( $is_amp_request ) : ?>
84
					action-xhr="<?php echo esc_url( $amp_form_action ); ?>"
85
					method="post"
86
					id="mailchimp_form"
87
					target="_top"
88
				<?php endif; ?>
89
			>
90
				<p>
91
					<input
92
						aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
93
						placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
94
						required
95
						title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
96
						type="email"
97
						name="email"
98
					/>
99
				</p>
100
				<p>
101
					<button type="submit" class="components-button is-button is-primary" style="<?php echo esc_attr( $button_styles ); ?>">
102
						<?php echo wp_kses_post( $values['submitButtonText'] ); ?>
103
					</button>
104
				</p>
105
				<p id="wp-block-jetpack-mailchimp_consent-text">
106
					<?php echo wp_kses_post( $values['consentText'] ); ?>
107
				</p>
108
109 View Code Duplication
				<?php if ( $is_amp_request ) : ?>
110
111
					<div submit-success>
112
						<template type="amp-mustache">
113
							<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp">
114
								<?php echo esc_html( $values['successLabel'] ); ?>
115
							</div>
116
						</template>
117
					</div>
118
					<div submit-error>
119
						<template type="amp-mustache">
120
							<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp">
121
								<?php echo esc_html( $values['errorLabel'] ); ?>
122
							</div>
123
						</template>
124
					</div>
125
					<div submitting>
126
						<template type="amp-mustache">
127
							<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status">
128
								<?php echo esc_html( $values['processingLabel'] ); ?>
129
							</div>
130
						</template>
131
					</div>
132
133
				<?php endif; ?>
134
135
			</form>
136 View Code Duplication
			<?php if ( ! $is_amp_request ) : ?>
137
138
				<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
139
					<?php echo esc_html( $values['processingLabel'] ); ?>
140
				</div>
141
				<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
142
					<?php echo esc_html( $values['successLabel'] ); ?>
143
				</div>
144
				<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
145
					<?php echo esc_html( $values['errorLabel'] ); ?>
146
				</div>
147
148
			<?php endif; ?>
149
		</div>
150
	</div>
151
	<?php
152
	$html = ob_get_clean();
153
	return $html;
154
}
155
156
/**
157
 * Mailchimp connection/list selection verification.
158
 *
159
 * @return boolean
160
 */
161 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...
162
	$option = get_option( 'jetpack_mailchimp' );
163
	if ( ! $option ) {
164
		return false;
165
	}
166
	$data = json_decode( $option, true );
167
	if ( ! $data ) {
168
		return false;
169
	}
170
	return isset( $data['follower_list_id'], $data['keyring_id'] );
171
}
172