PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
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 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | extensions/blocks/mailchimp/mailchimp.php +77 -209 12.7.316.3-a.1 View file →
@@ -8,15 +8,20 @@
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\Connection\Client;
15 +use Automattic\Jetpack\Connection\Manager as Connection_Manager;
16 +use Automattic\Jetpack\External_Connections;
17 +use Automattic\Jetpack\Status\Host;
13 18 use Jetpack;
14 19 use Jetpack_Gutenberg;
15 -use Jetpack_Options;
16 20
17 -const FEATURE_NAME = 'mailchimp';
18 -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
21 +if ( ! defined( 'ABSPATH' ) ) {
22 + exit( 0 );
23 +}
19 24
20 25 /**
21 26 * Registers the block for use in Gutenberg
22 27 * This is done via an action so that we can disable
@@ -27,23 +32,15 @@
27 32 ( defined( 'IS_WPCOM' ) && IS_WPCOM )
28 33 || Jetpack::is_connection_ready()
29 34 ) {
30 35 Blocks::jetpack_register_block(
31 - BLOCK_NAME,
36 + __DIR__,
32 37 array(
33 38 '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 39 )
45 40 );
41 +
42 + register_admin_settings();
46 43 }
47 44 }
48 45 add_action( 'init', __NAMESPACE__ . '\register_block' );
49 46
@@ -49,8 +46,11 @@
49 46
50 47 /**
51 48 * Mailchimp block registration/dependency declaration.
52 49 *
50 + * The render implementation lives in render.php and is only loaded when the
51 + * block is actually rendered, keeping it out of the eager front-end path.
52 + *
53 53 * @param array $attr - Array containing the Mailchimp block attributes.
54 54 * @param string $content - Mailchimp block content.
55 55 *
56 56 * @return string
@@ -55,221 +55,89 @@
55 55 *
56 56 * @return string
57 57 */
58 58 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;
59 + require_once __DIR__ . '/render.php';
60 + return load_assets_implementation( $attr, $content );
168 61 }
169 62
170 63 /**
171 - * Mailchimp connection/list selection verification.
172 - *
173 - * @return boolean
64 + * Registers the settings to manage the Mailchimp connection.
174 65 */
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 -}
66 +function register_admin_settings() {
67 + Assets::register_script(
68 + 'jetpack-mailchimp-admin-extra-settings',
69 + Jetpack_Gutenberg::get_blocks_directory() . '/mailchimp/admin.js',
70 + JETPACK__PLUGIN_FILE,
71 + array(
72 + 'textdomain' => 'jetpack',
73 + )
74 + );
186 75
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' => '',
76 + External_Connections::add_settings_for_service(
77 + 'writing',
78 + array(
79 + 'service' => 'mailchimp',
80 + 'title' => __( 'Mailchimp', 'jetpack' ),
81 + 'signup_link' => 'https://public-api.wordpress.com/rest/v1.1/sharing/mailchimp/signup',
82 + 'description' => __( 'Allow users to sign up to your Mailchimp mailing list.', 'jetpack' ),
83 + 'script' => 'jetpack-mailchimp-admin-extra-settings',
84 + 'support_link' => array(
85 + 'wpcom' => 'https://wordpress.com/support/wordpress-editor/blocks/mailchimp-block/',
86 + 'jetpack' => 'mailchimp-block',
87 + ),
88 + )
204 89 );
205 90
206 - foreach ( $defaults as $id => $default ) {
207 - $values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
208 - }
209 -
210 - return $values;
91 + add_action( 'load-options.php', __NAMESPACE__ . '\update_settings' );
211 92 }
212 93
213 94 /**
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
95 + * Update the site options that are related to Mailchimp.
221 96 */
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 );
97 +function update_settings() {
98 + $action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : '';
99 + $option_page = ! empty( $_REQUEST['option_page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['option_page'] ) ) : '';
100 + $audience = ! empty( $_REQUEST['jetpack-mailchimp-audience'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['jetpack-mailchimp-audience'] ) ) : '';
101 + if ( $action !== 'update' || $option_page !== 'writing' || ! current_user_can( 'manage_options' ) || $audience === '' ) {
102 + return;
226 103 }
227 104
228 - return render_deprecated_button( $attr );
229 -}
105 + check_admin_referer( 'writing-options' );
230 106
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();
107 + $site_id = Connection_Manager::get_site_id();
108 + if ( is_wp_error( $site_id ) ) {
109 + return;
110 + }
241 111
242 - if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
243 - array_push(
244 - $button_styles,
245 - sprintf(
246 - 'background-color: %s',
247 - sanitize_hex_color( $attr['customBackgroundButtonColor'] )
248 - )
112 + if ( $audience === 'none' ) {
113 + $data = array(
114 + 'follower_list_id' => '0',
115 + 'keyring_id' => '0',
249 116 );
117 + } else {
118 + $connection = External_Connections::get_connection( 'mailchimp' );
119 + if ( empty( $connection ) ) {
120 + return;
121 + }
122 + $data = array(
123 + 'follower_list_id' => $audience,
124 + 'keyring_id' => $connection['ID'],
125 + );
250 126 }
251 127
252 - if ( ! empty( $attr['customTextButtonColor'] ) ) {
253 - array_push(
254 - $button_styles,
255 - sprintf(
256 - 'color: %s',
257 - sanitize_hex_color( $attr['customTextButtonColor'] )
258 - )
128 + if ( ( new Host() )->is_wpcom_simple() ) {
129 + require_lib( 'mailchimp' );
130 + $response = \MailchimpApi::save_settings( $site_id, $data );
131 + } else {
132 + $response = Client::wpcom_json_api_request_as_user(
133 + sprintf( '/sites/%d/mailchimp/settings', $site_id ),
134 + '1.1',
135 + array( 'method' => 'POST' ),
136 + $data,
137 + 'rest'
259 138 );
260 139 }
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'];
140 + if ( is_wp_error( $response ) ) {
141 + add_settings_error( 'general', 'settings_updated', __( 'Settings save failed.', 'jetpack' ), 'error' );
267 142 }
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 143 }