Completed
Push — upgrade/eslint-5 ( 071910...9f2520 )
by
unknown
07:52
created

mailchimp.php ➔ jetpack_mailchimp_block_load_assets()   F

Complexity

Conditions 11
Paths 385

Size

Total Lines 94

Duplication

Lines 18
Ratio 19.15 %

Importance

Changes 0
Metric Value
cc 11
nc 385
nop 1
dl 18
loc 94
rs 3.4675
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
	$values  = array();
32
	$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
33
		get_current_blog_id() : Jetpack_Options::get_option( 'id' );
34
	Jetpack_Gutenberg::load_assets_as_required( 'mailchimp', array( 'wp-polyfill' ) );
35
	$defaults = array(
36
		'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
37
		'submitButtonText' => esc_html__( 'Join my email list', 'jetpack' ),
38
		'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' ),
39
		'processingLabel'  => esc_html__( 'Processing…', 'jetpack' ),
40
		'successLabel'     => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
41
		'errorLabel'       => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
42
	);
43
	foreach ( $defaults as $id => $default ) {
44
		$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
45
	}
46
47
	$values['submitButtonText'] = empty( $values['submitButtonText'] ) ? $defaults['submitButtonText'] : $values['submitButtonText'];
48
49
	/* TODO: replace with centralized block_class function */
50
	$align   = isset( $attr['align'] ) ? $attr['align'] : 'center';
51
	$type    = 'mailchimp';
52
	$classes = array(
53
		'wp-block-jetpack-' . $type,
54
		'align' . $align,
55
	);
56
	if ( isset( $attr['className'] ) ) {
57
		array_push( $classes, $attr['className'] );
58
	}
59
	$classes = implode( $classes, ' ' );
60
61
	$button_styles = array();
62 View Code Duplication
	if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
63
		array_push(
64
			$button_styles,
65
			sprintf(
66
				'background-color: %s',
67
				sanitize_hex_color( $attr['customBackgroundButtonColor'] )
68
			)
69
		);
70
	}
71 View Code Duplication
	if ( ! empty( $attr['customTextButtonColor'] ) ) {
72
		array_push(
73
			$button_styles,
74
			sprintf(
75
				'color: %s',
76
				sanitize_hex_color( $attr['customTextButtonColor'] )
77
			)
78
		);
79
	}
80
	$button_styles = implode( $button_styles, ';' );
81
82
	ob_start();
83
	?>
84
	<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
85
		<div class="components-placeholder">
86
			<form aria-describedby="wp-block-jetpack-mailchimp_consent-text">
87
				<p>
88
					<input
89
						aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
90
						placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
91
						required
92
						title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
93
						type="email"
94
					/>
95
				</p>
96
				<p>
97
					<button type="submit" class="components-button is-button is-primary" style="<?php echo esc_attr( $button_styles ); ?>">
98
						<?php echo wp_kses_post( $values['submitButtonText'] ); ?>
99
					</button>
100
				</p>
101
				<p id="wp-block-jetpack-mailchimp_consent-text" name="wp-block-jetpack-mailchimp_consent-text">
102
					<?php echo wp_kses_post( $values['consentText'] ); ?>
103
				</p>
104
			</form>
105
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
106
				<?php echo esc_html( $values['processingLabel'] ); ?>
107
			</div>
108
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
109
				<?php echo esc_html( $values['successLabel'] ); ?>
110
			</div>
111
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
112
				<?php echo esc_html( $values['errorLabel'] ); ?>
113
			</div>
114
		</div>
115
	</div>
116
	<?php
117
	$html = ob_get_clean();
118
	return $html;
119
}
120
121
/**
122
 * Mailchimp connection/list selection verification.
123
 *
124
 * @return boolean
125
 */
126 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...
127
	$option = get_option( 'jetpack_mailchimp' );
128
	if ( ! $option ) {
129
		return false;
130
	}
131
	$data = json_decode( $option, true );
132
	if ( ! $data ) {
133
		return false;
134
	}
135
	return isset( $data['follower_list_id'], $data['keyring_id'] );
136
}
137