PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
← All changes | extensions/blocks/mailchimp/mailchimp.php +48 -215 12.2.3 → 16.3-beta View file →
@@ -8,15 +8,17 @@
8 8 */
9 9
10 10 namespace Automattic\Jetpack\Extensions\Mailchimp;
11 11
12 +use Automattic\Jetpack\Assets;
12 13 use Automattic\Jetpack\Blocks;
14 +use Automattic\Jetpack\External_Connections;
13 15 use Jetpack;
14 16 use Jetpack_Gutenberg;
15 -use Jetpack_Options;
16 17
17 -const FEATURE_NAME = 'mailchimp';
18 -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
18 +if ( ! defined( 'ABSPATH' ) ) {
19 + exit( 0 );
20 +}
19 21
20 22 /**
21 23 * Registers the block for use in Gutenberg
22 24 * This is done via an action so that we can disable
@@ -27,23 +29,15 @@
27 29 ( defined( 'IS_WPCOM' ) && IS_WPCOM )
28 30 || Jetpack::is_connection_ready()
29 31 ) {
30 32 Blocks::jetpack_register_block(
31 - BLOCK_NAME,
33 + __DIR__,
32 34 array(
33 35 'render_callback' => __NAMESPACE__ . '\load_assets',
34 - 'supports' => array(
35 - 'align' => array( 'wide', 'full' ),
36 - 'color' => array(
37 - 'gradients' => true,
38 - ),
39 - 'spacing' => array(
40 - 'padding' => true,
41 - 'margin' => true,
42 - ),
43 - ),
44 36 )
45 37 );
38 +
39 + register_admin_settings();
46 40 }
47 41 }
48 42 add_action( 'init', __NAMESPACE__ . '\register_block' );
49 43
@@ -49,8 +43,11 @@
49 43
50 44 /**
51 45 * Mailchimp block registration/dependency declaration.
52 46 *
47 + * The render implementation lives in render.php and is only loaded when the
48 + * block is actually rendered, keeping it out of the eager front-end path.
49 + *
53 50 * @param array $attr - Array containing the Mailchimp block attributes.
54 51 * @param string $content - Mailchimp block content.
55 52 *
56 53 * @return string
@@ -55,221 +52,57 @@
55 52 *
56 53 * @return string
57 54 */
58 55 function load_assets( $attr, $content ) {
59 -
60 - if ( ! verify_connection() ) {
61 - return null;
62 - }
63 -
64 - $values = get_attributes_with_defaults( $attr );
65 - $blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
66 - ? get_current_blog_id()
67 - : Jetpack_Options::get_option( 'id' );
68 - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
69 - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
70 - $classes = ! empty( $wrapper_attributes['class'] ) ? $wrapper_attributes['class'] : '';
71 - $amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id );
72 - $is_amp_request = Blocks::is_amp_request();
73 -
74 - ob_start();
75 - ?>
76 -
77 - <div class="<?php echo esc_attr( $classes ); ?>"<?php echo ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : ''; ?> data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
78 - <form
79 - aria-describedby="wp-block-jetpack-mailchimp_consent-text"
80 - <?php if ( $is_amp_request ) : ?>
81 - action-xhr="<?php echo esc_url( $amp_form_action ); ?>"
82 - method="post"
83 - id="mailchimp_form"
84 - target="_top"
85 - on="submit-success:AMP.setState( { mailing_list_status: 'subscribed', mailing_list_email: event.response.email } )"
86 - <?php endif; ?>
87 - >
88 - <p>
89 - <input
90 - aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
91 - placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
92 - required
93 - title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
94 - type="email"
95 - name="email"
96 - />
97 - </p>
98 - <?php foreach ( is_array( $values['interests'] ) ? $values['interests'] : array() as $interest ) : ?>
99 - <input
100 - name="interests[<?php echo esc_attr( $interest ); ?>]"
101 - type="hidden"
102 - class="mc-submit-param"
103 - value="1"
104 - />
105 - <?php endforeach; ?>
106 - <?php
107 - if (
108 - ! empty( $values['signupFieldTag'] )
109 - && ! empty( $values['signupFieldValue'] )
110 - ) :
111 - ?>
112 - <input
113 - name="merge_fields[<?php echo esc_attr( $values['signupFieldTag'] ); ?>]"
114 - type="hidden"
115 - class="mc-submit-param"
116 - value="<?php echo esc_attr( $values['signupFieldValue'] ); ?>"
117 - />
118 - <?php endif; ?>
119 - <?php echo render_button( $attr, $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
120 - <p id="wp-block-jetpack-mailchimp_consent-text">
121 - <?php echo wp_kses_post( $values['consentText'] ); ?>
122 - </p>
123 -
124 - <?php if ( $is_amp_request ) : ?>
125 -
126 - <div submit-success>
127 - <template type="amp-mustache">
128 - <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp">
129 - <?php echo esc_html( $values['successLabel'] ); ?>
130 - </div>
131 - </template>
132 - </div>
133 - <div submit-error>
134 - <template type="amp-mustache">
135 - <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp">
136 - <?php echo esc_html( $values['errorLabel'] ); ?>
137 - </div>
138 - </template>
139 - </div>
140 - <div submitting>
141 - <template type="amp-mustache">
142 - <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status">
143 - <?php echo esc_html( $values['processingLabel'] ); ?>
144 - </div>
145 - </template>
146 - </div>
147 -
148 - <?php endif; ?>
149 -
150 - </form>
151 - <?php if ( ! $is_amp_request ) : ?>
152 -
153 - <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
154 - <?php echo esc_html( $values['processingLabel'] ); ?>
155 - </div>
156 - <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
157 - <?php echo esc_html( $values['successLabel'] ); ?>
158 - </div>
159 - <div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
160 - <?php echo esc_html( $values['errorLabel'] ); ?>
161 - </div>
162 -
163 - <?php endif; ?>
164 - </div>
165 - <?php
166 - $html = ob_get_clean();
167 - return $html;
56 + require_once __DIR__ . '/render.php';
57 + return load_assets_implementation( $attr, $content );
168 58 }
169 59
170 60 /**
171 - * Mailchimp connection/list selection verification.
172 - *
173 - * @return boolean
61 + * Registers the settings to manage the Mailchimp connection.
174 62 */
175 -function verify_connection() {
176 - $option = get_option( 'jetpack_mailchimp' );
177 - if ( ! $option ) {
178 - return false;
179 - }
180 - $data = json_decode( $option, true );
181 - if ( ! $data ) {
182 - return false;
183 - }
184 - return isset( $data['follower_list_id'] ) && isset( $data['keyring_id'] );
185 -}
63 +function register_admin_settings() {
64 + Assets::register_script(
65 + 'jetpack-mailchimp-admin-extra-settings',
66 + Jetpack_Gutenberg::get_blocks_directory() . '/mailchimp/admin.js',
67 + JETPACK__PLUGIN_FILE,
68 + array(
69 + 'textdomain' => 'jetpack',
70 + )
71 + );
186 72
187 -/**
188 - * Builds complete set of attributes using default values where needed.
189 - *
190 - * @param array $attr Saved set of attributes for the Mailchimp block.
191 - * @return array
192 - */
193 -function get_attributes_with_defaults( $attr ) {
194 - $values = array();
195 - $defaults = array(
196 - 'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
197 - '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' ),
198 - 'processingLabel' => esc_html__( 'Processing…', 'jetpack' ),
199 - 'successLabel' => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
200 - 'errorLabel' => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
201 - 'interests' => array(),
202 - 'signupFieldTag' => '',
203 - 'signupFieldValue' => '',
73 + External_Connections::add_settings_for_service(
74 + 'writing',
75 + array(
76 + 'service' => 'mailchimp',
77 + 'title' => __( 'Mailchimp', 'jetpack' ),
78 + 'signup_link' => 'https://public-api.wordpress.com/rest/v1.1/sharing/mailchimp/signup',
79 + 'description' => __( 'Allow users to sign up to your Mailchimp mailing list.', 'jetpack' ),
80 + 'script' => 'jetpack-mailchimp-admin-extra-settings',
81 + 'support_link' => array(
82 + 'wpcom' => 'https://wordpress.com/support/wordpress-editor/blocks/mailchimp-block/',
83 + 'jetpack' => 'mailchimp-block',
84 + ),
85 + )
204 86 );
205 87
206 - foreach ( $defaults as $id => $default ) {
207 - $values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
208 - }
209 -
210 - return $values;
88 + add_action( 'load-options.php', __NAMESPACE__ . '\update_settings' );
211 89 }
212 90
213 91 /**
214 - * Renders the Mailchimp block button using inner block content if available
215 - * otherwise generating the HTML button from deprecated attributes.
216 - *
217 - * @param array $attr Attributes for the Mailchimp block.
218 - * @param string $content Mailchimp block content.
219 - *
220 - * @return string
92 + * Update the site options that are related to Mailchimp.
221 93 */
222 -function render_button( $attr, $content ) {
223 - if ( ! empty( $content ) ) {
224 - $block_id = wp_unique_id( 'mailchimp-button-block-' );
225 - return str_replace( 'mailchimp-widget-id', $block_id, $content );
94 +function update_settings() {
95 + $action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : '';
96 + $option_page = ! empty( $_REQUEST['option_page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['option_page'] ) ) : '';
97 + $audience = ! empty( $_REQUEST['jetpack-mailchimp-audience'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['jetpack-mailchimp-audience'] ) ) : '';
98 + if ( $action !== 'update' || $option_page !== 'writing' || ! current_user_can( 'manage_options' ) || $audience === '' ) {
99 + return;
226 100 }
227 101
228 - return render_deprecated_button( $attr );
229 -}
102 + check_admin_referer( 'writing-options' );
230 103
231 -/**
232 - * Renders HTML button from deprecated Mailchimp block attributes.
233 - *
234 - * @param array $attr Mailchimp block attributes.
235 - * @return string
236 - */
237 -function render_deprecated_button( $attr ) {
238 - $default = esc_html__( 'Join my email list', 'jetpack' );
239 - $text = empty( $attr['submitButtonText'] ) ? $default : $attr['submitButtonText'];
240 - $button_styles = array();
241 -
242 - if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
243 - array_push(
244 - $button_styles,
245 - sprintf(
246 - 'background-color: %s',
247 - sanitize_hex_color( $attr['customBackgroundButtonColor'] )
248 - )
249 - );
104 + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-mailchimp-helper.php';
105 + if ( is_wp_error( \Jetpack_Mailchimp_Helper::save_audience( $audience ) ) ) {
106 + add_settings_error( 'general', 'settings_updated', __( 'Settings save failed.', 'jetpack' ), 'error' );
250 107 }
251 -
252 - if ( ! empty( $attr['customTextButtonColor'] ) ) {
253 - array_push(
254 - $button_styles,
255 - sprintf(
256 - 'color: %s',
257 - sanitize_hex_color( $attr['customTextButtonColor'] )
258 - )
259 - );
260 - }
261 -
262 - $button_styles = implode( ';', $button_styles );
263 - $button_classes = 'components-button is-button is-primary ';
264 -
265 - if ( ! empty( $attr['submitButtonClasses'] ) ) {
266 - $button_classes .= $attr['submitButtonClasses'];
267 - }
268 -
269 - return sprintf(
270 - '<p><button type="submit" class="%s" style="%s">%s</button></p>',
271 - esc_attr( $button_classes ),
272 - esc_attr( $button_styles ),
273 - wp_kses_post( $text )
274 - );
275 108 }