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 -196 13.5.216.3-a.1 View file →
@@ -8,13 +8,21 @@
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
21 +if ( ! defined( 'ABSPATH' ) ) {
22 + exit( 0 );
23 +}
24 +
17 25 /**
18 26 * Registers the block for use in Gutenberg
19 27 * This is done via an action so that we can disable
20 28 * registration if we need to.
@@ -29,8 +37,10 @@
29 37 array(
30 38 'render_callback' => __NAMESPACE__ . '\load_assets',
31 39 )
32 40 );
41 +
42 + register_admin_settings();
33 43 }
34 44 }
35 45 add_action( 'init', __NAMESPACE__ . '\register_block' );
36 46
@@ -36,8 +46,11 @@
36 46
37 47 /**
38 48 * Mailchimp block registration/dependency declaration.
39 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 + *
40 53 * @param array $attr - Array containing the Mailchimp block attributes.
41 54 * @param string $content - Mailchimp block content.
42 55 *
43 56 * @return string
@@ -42,221 +55,89 @@
42 55 *
43 56 * @return string
44 57 */
45 58 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;
59 + require_once __DIR__ . '/render.php';
60 + return load_assets_implementation( $attr, $content );
155 61 }
156 62
157 63 /**
158 - * Mailchimp connection/list selection verification.
159 - *
160 - * @return boolean
64 + * Registers the settings to manage the Mailchimp connection.
161 65 */
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 -}
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 + );
173 75
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' => '',
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 + )
191 89 );
192 90
193 - foreach ( $defaults as $id => $default ) {
194 - $values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
195 - }
196 -
197 - return $values;
91 + add_action( 'load-options.php', __NAMESPACE__ . '\update_settings' );
198 92 }
199 93
200 94 /**
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
95 + * Update the site options that are related to Mailchimp.
208 96 */
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 );
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;
213 103 }
214 104
215 - return render_deprecated_button( $attr );
216 -}
105 + check_admin_referer( 'writing-options' );
217 106
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();
107 + $site_id = Connection_Manager::get_site_id();
108 + if ( is_wp_error( $site_id ) ) {
109 + return;
110 + }
228 111
229 - if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
230 - array_push(
231 - $button_styles,
232 - sprintf(
233 - 'background-color: %s',
234 - sanitize_hex_color( $attr['customBackgroundButtonColor'] )
235 - )
112 + if ( $audience === 'none' ) {
113 + $data = array(
114 + 'follower_list_id' => '0',
115 + 'keyring_id' => '0',
236 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 + );
237 126 }
238 127
239 - if ( ! empty( $attr['customTextButtonColor'] ) ) {
240 - array_push(
241 - $button_styles,
242 - sprintf(
243 - 'color: %s',
244 - sanitize_hex_color( $attr['customTextButtonColor'] )
245 - )
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'
246 138 );
247 139 }
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'];
140 + if ( is_wp_error( $response ) ) {
141 + add_settings_error( 'general', 'settings_updated', __( 'Settings save failed.', 'jetpack' ), 'error' );
254 142 }
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 143 }