summaryrefslogtreecommitdiff
blob: 1640e136bc3c566a0ca96f1995fc803f636d575a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
 * Mailchimp Block.
 *
 * @since 7.1.0
 *
 * @package automattic/jetpack
 */

namespace Automattic\Jetpack\Extensions\Mailchimp;

use Automattic\Jetpack\Blocks;
use Jetpack;
use Jetpack_Gutenberg;
use Jetpack_Options;

const FEATURE_NAME = 'mailchimp';
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;

/**
 * Registers the block for use in Gutenberg
 * This is done via an action so that we can disable
 * registration if we need to.
 */
function register_block() {
	if (
		( defined( 'IS_WPCOM' ) && IS_WPCOM )
		|| Jetpack::is_connection_ready()
	) {
		Blocks::jetpack_register_block(
			BLOCK_NAME,
			array(
				'render_callback' => __NAMESPACE__ . '\load_assets',
			)
		);
	}
}
add_action( 'init', __NAMESPACE__ . '\register_block' );

/**
 * Mailchimp block registration/dependency declaration.
 *
 * @param array  $attr - Array containing the Mailchimp block attributes.
 * @param string $content - Mailchimp block content.
 *
 * @return string
 */
function load_assets( $attr, $content ) {

	if ( ! verify_connection() ) {
		return null;
	}

	$values  = get_attributes_with_defaults( $attr );
	$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
		? get_current_blog_id()
		: Jetpack_Options::get_option( 'id' );
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
	$classes         = Blocks::classes( FEATURE_NAME, $attr );
	$amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id );
	$is_amp_request  = Blocks::is_amp_request();

	ob_start();
	?>

	<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
		<form
			aria-describedby="wp-block-jetpack-mailchimp_consent-text"
			<?php if ( $is_amp_request ) : ?>
				action-xhr="<?php echo esc_url( $amp_form_action ); ?>"
				method="post"
				id="mailchimp_form"
				target="_top"
				on="submit-success:AMP.setState( { mailing_list_status: 'subscribed', mailing_list_email: event.response.email } )"
			<?php endif; ?>
		>
			<p>
				<input
					aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
					placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
					required
					title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
					type="email"
					name="email"
				/>
			</p>
			<?php foreach ( is_array( $values['interests'] ) ? $values['interests'] : array() as $interest ) : ?>
				<input
					name="interests[<?php echo esc_attr( $interest ); ?>]"
					type="hidden"
					class="mc-submit-param"
					value="1"
				/>
			<?php endforeach; ?>
			<?php
			if (
				! empty( $values['signupFieldTag'] )
				&& ! empty( $values['signupFieldValue'] )
				) :
				?>
				<input
					name="merge_fields[<?php echo esc_attr( $values['signupFieldTag'] ); ?>]"
					type="hidden"
					class="mc-submit-param"
					value="<?php echo esc_attr( $values['signupFieldValue'] ); ?>"
				/>
			<?php endif; ?>
			<?php echo render_button( $attr, $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			<p id="wp-block-jetpack-mailchimp_consent-text">
				<?php echo wp_kses_post( $values['consentText'] ); ?>
			</p>

			<?php if ( $is_amp_request ) : ?>

				<div submit-success>
					<template type="amp-mustache">
						<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp">
							<?php echo esc_html( $values['successLabel'] ); ?>
						</div>
					</template>
				</div>
				<div submit-error>
					<template type="amp-mustache">
						<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp">
							<?php echo esc_html( $values['errorLabel'] ); ?>
						</div>
					</template>
				</div>
				<div submitting>
					<template type="amp-mustache">
						<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status">
							<?php echo esc_html( $values['processingLabel'] ); ?>
						</div>
					</template>
				</div>

			<?php endif; ?>

		</form>
		<?php if ( ! $is_amp_request ) : ?>

			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
				<?php echo esc_html( $values['processingLabel'] ); ?>
			</div>
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
				<?php echo esc_html( $values['successLabel'] ); ?>
			</div>
			<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
				<?php echo esc_html( $values['errorLabel'] ); ?>
			</div>

		<?php endif; ?>
	</div>
	<?php
	$html = ob_get_clean();
	return $html;
}

/**
 * Mailchimp connection/list selection verification.
 *
 * @return boolean
 */
function verify_connection() {
	$option = get_option( 'jetpack_mailchimp' );
	if ( ! $option ) {
		return false;
	}
	$data = json_decode( $option, true );
	if ( ! $data ) {
		return false;
	}
	return isset( $data['follower_list_id'], $data['keyring_id'] );
}

/**
 * Builds complete set of attributes using default values where needed.
 *
 * @param array $attr Saved set of attributes for the Mailchimp block.
 * @return array
 */
function get_attributes_with_defaults( $attr ) {
	$values   = array();
	$defaults = array(
		'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
		'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' ),
		'processingLabel'  => esc_html__( 'Processing…', 'jetpack' ),
		'successLabel'     => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
		'errorLabel'       => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
		'interests'        => array(),
		'signupFieldTag'   => '',
		'signupFieldValue' => '',
	);

	foreach ( $defaults as $id => $default ) {
		$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
	}

	return $values;
}

/**
 * Renders the Mailchimp block button using inner block content if available
 * otherwise generating the HTML button from deprecated attributes.
 *
 * @param array  $attr Attributes for the Mailchimp block.
 * @param string $content Mailchimp block content.
 *
 * @return string
 */
function render_button( $attr, $content ) {
	if ( ! empty( $content ) ) {
		$block_id = wp_unique_id( 'mailchimp-button-block-' );
		return str_replace( 'mailchimp-widget-id', $block_id, $content );
	}

	return render_deprecated_button( $attr );
}

/**
 * Renders HTML button from deprecated Mailchimp block attributes.
 *
 * @param array $attr Mailchimp block attributes.
 * @return string
 */
function render_deprecated_button( $attr ) {
	$default       = esc_html__( 'Join my email list', 'jetpack' );
	$text          = empty( $attr['submitButtonText'] ) ? $default : $attr['submitButtonText'];
	$button_styles = array();

	if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
		array_push(
			$button_styles,
			sprintf(
				'background-color: %s',
				sanitize_hex_color( $attr['customBackgroundButtonColor'] )
			)
		);
	}

	if ( ! empty( $attr['customTextButtonColor'] ) ) {
		array_push(
			$button_styles,
			sprintf(
				'color: %s',
				sanitize_hex_color( $attr['customTextButtonColor'] )
			)
		);
	}

	$button_styles  = implode( ';', $button_styles );
	$button_classes = 'components-button is-button is-primary ';

	if ( ! empty( $attr['submitButtonClasses'] ) ) {
		$button_classes .= $attr['submitButtonClasses'];
	}

	return sprintf(
		'<p><button type="submit" class="%s" style="%s">%s</button></p>',
		esc_attr( $button_classes ),
		esc_attr( $button_styles ),
		wp_kses_post( $text )
	);
}