Completed
Push — add/changelog-71 ( fdf304...3e56fc )
by Jeremy
07:00
created

extensions/blocks/mailchimp/mailchimp.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 );
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
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