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