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
|
|
View Code Duplication |
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 Mailchimp block attributes. |
44
|
|
|
* @param string $content - Mailchimp block content. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
function load_assets( $attr, $content ) { |
49
|
|
|
|
50
|
|
|
if ( ! verify_connection() ) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$values = get_attributes_with_defaults( $attr ); |
55
|
|
|
$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) |
56
|
|
|
? get_current_blog_id() |
57
|
|
|
: Jetpack_Options::get_option( 'id' ); |
58
|
|
|
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
59
|
|
|
$classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attr ); |
60
|
|
|
$amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id ); |
61
|
|
|
$is_amp_request = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request(); |
62
|
|
|
|
63
|
|
|
ob_start(); |
64
|
|
|
?> |
65
|
|
|
|
66
|
|
|
<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>"> |
67
|
|
|
<div class="components-placeholder"> |
68
|
|
|
<form |
69
|
|
|
aria-describedby="wp-block-jetpack-mailchimp_consent-text" |
70
|
|
|
<?php if ( $is_amp_request ) : ?> |
71
|
|
|
action-xhr="<?php echo esc_url( $amp_form_action ); ?>" |
72
|
|
|
method="post" |
73
|
|
|
id="mailchimp_form" |
74
|
|
|
target="_top" |
75
|
|
|
on="submit-success:AMP.setState( { mailing_list_status: 'subscribed', mailing_list_email: event.response.email } )" |
76
|
|
|
<?php endif; ?> |
77
|
|
|
> |
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
|
|
|
name="email" |
86
|
|
|
/> |
87
|
|
|
</p> |
88
|
|
|
<?php foreach ( is_array( $values['interests'] ) ? $values['interests'] : array() as $interest ) : ?> |
89
|
|
|
<input |
90
|
|
|
name="interests[<?php echo esc_attr( $interest ); ?>]" |
91
|
|
|
type="hidden" |
92
|
|
|
class="mc-submit-param" |
93
|
|
|
value="1" |
94
|
|
|
/> |
95
|
|
|
<?php endforeach; ?> |
96
|
|
|
<?php |
97
|
|
|
if ( |
98
|
|
|
! empty( $values['signupFieldTag'] ) |
99
|
|
|
&& ! empty( $values['signupFieldValue'] ) |
100
|
|
|
) : |
101
|
|
|
?> |
102
|
|
|
<input |
103
|
|
|
name="merge_fields[<?php echo esc_attr( $values['signupFieldTag'] ); ?>]" |
104
|
|
|
type="hidden" |
105
|
|
|
class="mc-submit-param" |
106
|
|
|
value="<?php echo esc_attr( $values['signupFieldValue'] ); ?>" |
107
|
|
|
/> |
108
|
|
|
<?php endif; ?> |
109
|
|
|
<?php echo render_button( $attr, $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
110
|
|
|
<p id="wp-block-jetpack-mailchimp_consent-text"> |
111
|
|
|
<?php echo wp_kses_post( $values['consentText'] ); ?> |
112
|
|
|
</p> |
113
|
|
|
|
114
|
|
View Code Duplication |
<?php if ( $is_amp_request ) : ?> |
115
|
|
|
|
116
|
|
|
<div submit-success> |
117
|
|
|
<template type="amp-mustache"> |
118
|
|
|
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp"> |
119
|
|
|
<?php echo esc_html( $values['successLabel'] ); ?> |
120
|
|
|
</div> |
121
|
|
|
</template> |
122
|
|
|
</div> |
123
|
|
|
<div submit-error> |
124
|
|
|
<template type="amp-mustache"> |
125
|
|
|
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp"> |
126
|
|
|
<?php echo esc_html( $values['errorLabel'] ); ?> |
127
|
|
|
</div> |
128
|
|
|
</template> |
129
|
|
|
</div> |
130
|
|
|
<div submitting> |
131
|
|
|
<template type="amp-mustache"> |
132
|
|
|
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status"> |
133
|
|
|
<?php echo esc_html( $values['processingLabel'] ); ?> |
134
|
|
|
</div> |
135
|
|
|
</template> |
136
|
|
|
</div> |
137
|
|
|
|
138
|
|
|
<?php endif; ?> |
139
|
|
|
|
140
|
|
|
</form> |
141
|
|
View Code Duplication |
<?php if ( ! $is_amp_request ) : ?> |
142
|
|
|
|
143
|
|
|
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status"> |
144
|
|
|
<?php echo esc_html( $values['processingLabel'] ); ?> |
145
|
|
|
</div> |
146
|
|
|
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status"> |
147
|
|
|
<?php echo esc_html( $values['successLabel'] ); ?> |
148
|
|
|
</div> |
149
|
|
|
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert"> |
150
|
|
|
<?php echo esc_html( $values['errorLabel'] ); ?> |
151
|
|
|
</div> |
152
|
|
|
|
153
|
|
|
<?php endif; ?> |
154
|
|
|
</div> |
155
|
|
|
</div> |
156
|
|
|
<?php |
157
|
|
|
$html = ob_get_clean(); |
158
|
|
|
return $html; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Mailchimp connection/list selection verification. |
163
|
|
|
* |
164
|
|
|
* @return boolean |
165
|
|
|
*/ |
166
|
|
|
function verify_connection() { |
167
|
|
|
$option = get_option( 'jetpack_mailchimp' ); |
168
|
|
|
if ( ! $option ) { |
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
$data = json_decode( $option, true ); |
172
|
|
|
if ( ! $data ) { |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
return isset( $data['follower_list_id'], $data['keyring_id'] ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Builds complete set of attributes using default values where needed. |
180
|
|
|
* |
181
|
|
|
* @param array $attr Saved set of attributes for the Mailchimp block. |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
function get_attributes_with_defaults( $attr ) { |
185
|
|
|
$values = array(); |
186
|
|
|
$defaults = array( |
187
|
|
|
'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ), |
188
|
|
|
'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' ), |
189
|
|
|
'processingLabel' => esc_html__( 'Processing…', 'jetpack' ), |
190
|
|
|
'successLabel' => esc_html__( 'Success! You\'re on the list.', 'jetpack' ), |
191
|
|
|
'errorLabel' => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ), |
192
|
|
|
'interests' => array(), |
193
|
|
|
'signupFieldTag' => '', |
194
|
|
|
'signupFieldValue' => '', |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
foreach ( $defaults as $id => $default ) { |
198
|
|
|
$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $values; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Renders the Mailchimp block button using inner block content if available |
206
|
|
|
* otherwise generating the HTML button from deprecated attributes. |
207
|
|
|
* |
208
|
|
|
* @param array $attr Attributes for the Mailchimp block. |
209
|
|
|
* @param string $content Mailchimp block content. |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
function render_button( $attr, $content ) { |
214
|
|
|
if ( ! empty( $content ) ) { |
215
|
|
|
$block_id = wp_unique_id( 'mailchimp-button-block-' ); |
216
|
|
|
return str_replace( 'mailchimp-widget-id', $block_id, $content ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return render_deprecated_button( $attr ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Renders HTML button from deprecated Mailchimp block attributes. |
224
|
|
|
* |
225
|
|
|
* @param array $attr Mailchimp block attributes. |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
function render_deprecated_button( $attr ) { |
229
|
|
|
$default = esc_html__( 'Join my email list', 'jetpack' ); |
230
|
|
|
$text = empty( $attr['submitButtonText'] ) ? $default : $attr['submitButtonText']; |
231
|
|
|
$button_styles = array(); |
232
|
|
|
|
233
|
|
|
if ( ! empty( $attr['customBackgroundButtonColor'] ) ) { |
234
|
|
|
array_push( |
235
|
|
|
$button_styles, |
236
|
|
|
sprintf( |
237
|
|
|
'background-color: %s', |
238
|
|
|
sanitize_hex_color( $attr['customBackgroundButtonColor'] ) |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
View Code Duplication |
if ( ! empty( $attr['customTextButtonColor'] ) ) { |
244
|
|
|
array_push( |
245
|
|
|
$button_styles, |
246
|
|
|
sprintf( |
247
|
|
|
'color: %s', |
248
|
|
|
sanitize_hex_color( $attr['customTextButtonColor'] ) |
249
|
|
|
) |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$button_styles = implode( ';', $button_styles ); |
254
|
|
|
$button_classes = 'components-button is-button is-primary '; |
255
|
|
|
|
256
|
|
|
if ( ! empty( $attr['submitButtonClasses'] ) ) { |
257
|
|
|
$button_classes .= $attr['submitButtonClasses']; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return sprintf( |
261
|
|
|
'<p><button type="submit" class="%s" style="%s">%s</button></p>', |
262
|
|
|
esc_attr( $button_classes ), |
263
|
|
|
esc_attr( $button_styles ), |
264
|
|
|
wp_kses_post( $text ) |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
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.