Completed
Push — updates/infinity-vanilla-js ( fda899...9466ff )
by
unknown
07:55
created

mailchimp.php ➔ load_assets()   F

Complexity

Conditions 19
Paths 18433

Size

Total Lines 161

Duplication

Lines 47
Ratio 29.19 %

Importance

Changes 0
Metric Value
cc 19
nc 18433
nop 1
dl 47
loc 161
rs 0.2799
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
namespace Automattic\Jetpack\Extensions\Mailchimp;
11
12
use Jetpack;
13
use Jetpack_AMP_Support;
14
use Jetpack_Gutenberg;
15
use Jetpack_Options;
16
17
const FEATURE_NAME = 'mailchimp';
18
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
19
20
/**
21
 * Registers the block for use in Gutenberg
22
 * This is done via an action so that we can disable
23
 * registration if we need to.
24
 */
25
function register_block() {
26
	if (
27
		( defined( 'IS_WPCOM' ) && IS_WPCOM )
28
		|| Jetpack::is_active()
29
	) {
30
		jetpack_register_block(
31
			BLOCK_NAME,
32
			array(
33
				'render_callback' => __NAMESPACE__ . '\load_assets',
34
			)
35
		);
36
	}
37
}
38
add_action( 'init', __NAMESPACE__ . '\register_block' );
39
40
/**
41
 * Mailchimp block registration/dependency declaration.
42
 *
43
 * @param array $attr - Array containing the map block attributes.
44
 *
45
 * @return string
46
 */
47
function load_assets( $attr ) {
48
49
	if ( ! verify_connection() ) {
50
		return null;
51
	}
52
53
	$values  = array();
54
	$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
55
		? get_current_blog_id()
56
		: Jetpack_Options::get_option( 'id' );
57
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
58
	$defaults = array(
59
		'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
60
		'submitButtonText' => esc_html__( 'Join my email list', 'jetpack' ),
61
		'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' ),
62
		'processingLabel'  => esc_html__( 'Processing…', 'jetpack' ),
63
		'successLabel'     => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
64
		'errorLabel'       => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
65
		'interests'        => array(),
66
		'signupFieldTag'   => '',
67
		'signupFieldValue' => '',
68
	);
69
	foreach ( $defaults as $id => $default ) {
70
		$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
71
	}
72
73
	$values['submitButtonText'] = empty( $values['submitButtonText'] ) ? $defaults['submitButtonText'] : $values['submitButtonText'];
74
75
	$classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr );
76
77
	$button_styles = array();
78
	if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
79
		array_push(
80
			$button_styles,
81
			sprintf(
82
				'background-color: %s',
83
				sanitize_hex_color( $attr['customBackgroundButtonColor'] )
84
			)
85
		);
86
	}
87 View Code Duplication
	if ( ! empty( $attr['customTextButtonColor'] ) ) {
88
		array_push(
89
			$button_styles,
90
			sprintf(
91
				'color: %s',
92
				sanitize_hex_color( $attr['customTextButtonColor'] )
93
			)
94
		);
95
	}
96
	$button_styles   = implode( ';', $button_styles );
97
	$amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id );
98
	$is_amp_request  = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
99
100
	$button_classes = 'components-button is-button is-primary ';
101
	if ( ! empty( $attr['submitButtonClasses'] ) ) {
102
		$button_classes .= $attr['submitButtonClasses'];
103
	}
104
105
	ob_start();
106
	?>
107
108
	<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
109
		<div class="components-placeholder">
110
			<form
111
				aria-describedby="wp-block-jetpack-mailchimp_consent-text"
112
				<?php if ( $is_amp_request ) : ?>
113
					action-xhr="<?php echo esc_url( $amp_form_action ); ?>"
114
					method="post"
115
					id="mailchimp_form"
116
					target="_top"
117
					<?php if ( $is_amp_request ) : ?>
118
					on="submit-success:AMP.setState( { mailing_list_status: 'subscribed', mailing_list_email: event.response.email } )"
119
					<?php endif; ?>
120
				<?php endif; ?>
121
			>
122
				<p>
123
					<input
124
						aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
125
						placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
126
						required
127
						title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
128
						type="email"
129
						name="email"
130
					/>
131
				</p>
132
				<?php foreach ( is_array( $values['interests'] ) ? $values['interests'] : array() as $interest ) : ?>
133
					<input
134
						name="interests[<?php echo esc_attr( $interest ); ?>]"
135
						type="hidden"
136
						class="mc-submit-param"
137
						value="1"
138
					/>
139
				<?php endforeach; ?>
140
				<?php
141
				if (
142
					! empty( $values['signupFieldTag'] )
143
					&& ! empty( $values['signupFieldValue'] )
144
					) :
145
					?>
146
					<input
147
						name="merge_fields[<?php echo esc_attr( $values['signupFieldTag'] ); ?>]"
148
						type="hidden"
149
						class="mc-submit-param"
150
						value="<?php echo esc_attr( $values['signupFieldValue'] ); ?>"
151
					/>
152
				<?php endif; ?>
153
				<p>
154
					<button type="submit" class="<?php echo esc_attr( $button_classes ); ?>" style="<?php echo esc_attr( $button_styles ); ?>">
155
						<?php echo wp_kses_post( $values['submitButtonText'] ); ?>
156
					</button>
157
				</p>
158
				<p id="wp-block-jetpack-mailchimp_consent-text">
159
					<?php echo wp_kses_post( $values['consentText'] ); ?>
160
				</p>
161
162 View Code Duplication
				<?php if ( $is_amp_request ) : ?>
163
164
					<div submit-success>
165
						<template type="amp-mustache">
166
							<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp">
167
								<?php echo esc_html( $values['successLabel'] ); ?>
168
							</div>
169
						</template>
170
					</div>
171
					<div submit-error>
172
						<template type="amp-mustache">
173
							<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp">
174
								<?php echo esc_html( $values['errorLabel'] ); ?>
175
							</div>
176
						</template>
177
					</div>
178
					<div submitting>
179
						<template type="amp-mustache">
180
							<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status">
181
								<?php echo esc_html( $values['processingLabel'] ); ?>
182
							</div>
183
						</template>
184
					</div>
185
186
				<?php endif; ?>
187
188
			</form>
189 View Code Duplication
			<?php if ( ! $is_amp_request ) : ?>
190
191
				<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
192
					<?php echo esc_html( $values['processingLabel'] ); ?>
193
				</div>
194
				<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
195
					<?php echo esc_html( $values['successLabel'] ); ?>
196
				</div>
197
				<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
198
					<?php echo esc_html( $values['errorLabel'] ); ?>
199
				</div>
200
201
			<?php endif; ?>
202
		</div>
203
	</div>
204
	<?php
205
	$html = ob_get_clean();
206
	return $html;
207
}
208
209
/**
210
 * Mailchimp connection/list selection verification.
211
 *
212
 * @return boolean
213
 */
214
function verify_connection() {
215
	$option = get_option( 'jetpack_mailchimp' );
216
	if ( ! $option ) {
217
		return false;
218
	}
219
	$data = json_decode( $option, true );
220
	if ( ! $data ) {
221
		return false;
222
	}
223
	return isset( $data['follower_list_id'], $data['keyring_id'] );
224
}
225