Completed
Push — add/new-disconnect-dialog ( b4649f...72298c )
by
unknown
23:43 queued 16:44
created

mailchimp.php ➔ jetpack_mailchimp_block_load_assets()   C

Complexity

Conditions 9
Paths 97

Size

Total Lines 85

Duplication

Lines 9
Ratio 10.59 %

Importance

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