* @package automattic/jetpack
namespace Automattic\Jetpack\Extensions\Mailchimp;
use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\External_Connections;
use Automattic\Jetpack\Status\Host;
if ( ! defined( 'ABSPATH' ) ) {
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
* registration if we need to.
function register_block() {
( defined( 'IS_WPCOM' ) && IS_WPCOM )
|| Jetpack::is_connection_ready()
Blocks::jetpack_register_block(
'render_callback' => __NAMESPACE__ . '\load_assets',
register_admin_settings();
add_action( 'init', __NAMESPACE__ . '\register_block' );
* Mailchimp block registration/dependency declaration.
* @param array $attr - Array containing the Mailchimp block attributes.
* @param string $content - Mailchimp block content.
function load_assets( $attr, $content ) {
if ( ! verify_connection() ) {
$values = get_attributes_with_defaults( $attr );
$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
: Jetpack_Options::get_option( 'id' );
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
$wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
$classes = ! empty( $wrapper_attributes['class'] ) ? $wrapper_attributes['class'] : '';
$amp_form_action = sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s/email_follow/amp/subscribe/', $blog_id );
$is_amp_request = Blocks::is_amp_request();
<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 ); ?>">
aria-describedby="wp-block-jetpack-mailchimp_consent-text"
<?php if ( $is_amp_request ) : ?>
action-xhr="<?php echo esc_url( $amp_form_action ); ?>"
on="submit-success:AMP.setState( { mailing_list_status: 'subscribed', mailing_list_email: event.response.email } )"
aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
<?php foreach ( is_array( $values['interests'] ) ? $values['interests'] : array() as $interest ) : ?>
name="interests[<?php echo esc_attr( $interest ); ?>]"
! empty( $values['signupFieldTag'] )
&& ! empty( $values['signupFieldValue'] )
name="merge_fields[<?php echo esc_attr( $values['signupFieldTag'] ); ?>]"
value="<?php echo esc_attr( $values['signupFieldValue'] ); ?>"
<?php echo render_button( $attr, $content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<p id="wp-block-jetpack-mailchimp_consent-text">
<?php echo wp_kses_post( $values['consentText'] ); ?>
<?php if ( $is_amp_request ) : ?>
<template type="amp-mustache">
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success wp-block-jetpack-mailchimp__is-amp">
<?php echo esc_html( $values['successLabel'] ); ?>
<template type="amp-mustache">
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error wp-block-jetpack-mailchimp__is-amp">
<?php echo esc_html( $values['errorLabel'] ); ?>
<template type="amp-mustache">
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing wp-block-jetpack-mailchimp__is-amp" role="status">
<?php echo esc_html( $values['processingLabel'] ); ?>
<?php if ( ! $is_amp_request ) : ?>
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
<?php echo esc_html( $values['processingLabel'] ); ?>
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
<?php echo esc_html( $values['successLabel'] ); ?>
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
<?php echo esc_html( $values['errorLabel'] ); ?>
* Mailchimp connection/list selection verification.
function verify_connection() {
$option = get_option( 'jetpack_mailchimp' );
$data = json_decode( $option, true );
return isset( $data['follower_list_id'] ) && isset( $data['keyring_id'] );
* Builds complete set of attributes using default values where needed.
* @param array $attr Saved set of attributes for the Mailchimp block.
function get_attributes_with_defaults( $attr ) {
'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
'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' ),
'processingLabel' => esc_html__( 'Processing…', 'jetpack' ),
'successLabel' => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
'errorLabel' => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
'signupFieldValue' => '',
foreach ( $defaults as $id => $default ) {
$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
* Renders the Mailchimp block button using inner block content if available
* otherwise generating the HTML button from deprecated attributes.
* @param array $attr Attributes for the Mailchimp block.
* @param string $content Mailchimp block content.
function render_button( $attr, $content ) {
if ( ! empty( $content ) ) {
$block_id = wp_unique_id( 'mailchimp-button-block-' );
return str_replace( 'mailchimp-widget-id', $block_id, $content );
return render_deprecated_button( $attr );
* Renders HTML button from deprecated Mailchimp block attributes.
* @param array $attr Mailchimp block attributes.
function render_deprecated_button( $attr ) {
$default = esc_html__( 'Join my email list', 'jetpack' );
$text = empty( $attr['submitButtonText'] ) ? $default : $attr['submitButtonText'];
$button_styles = array();
if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
sanitize_hex_color( $attr['customBackgroundButtonColor'] )
if ( ! empty( $attr['customTextButtonColor'] ) ) {
sanitize_hex_color( $attr['customTextButtonColor'] )
$button_styles = implode( ';', $button_styles );
$button_classes = 'components-button is-button is-primary ';
if ( ! empty( $attr['submitButtonClasses'] ) ) {
$button_classes .= $attr['submitButtonClasses'];
'<p><button type="submit" class="%s" style="%s">%s</button></p>',
esc_attr( $button_classes ),
esc_attr( $button_styles ),
* Registers the settings to manage the Mailchimp connection.
function register_admin_settings() {
'jetpack-mailchimp-admin-extra-settings',
Jetpack_Gutenberg::get_blocks_directory() . '/mailchimp/admin.js',
'textdomain' => 'jetpack',
External_Connections::add_settings_for_service(
'service' => 'mailchimp',
'title' => __( 'Mailchimp', 'jetpack' ),
'signup_link' => 'https://public-api.wordpress.com/rest/v1.1/sharing/mailchimp/signup',
'description' => __( 'Allow users to sign up to your Mailchimp mailing list.', 'jetpack' ),
'script' => 'jetpack-mailchimp-admin-extra-settings',
'wpcom' => 'https://wordpress.com/support/wordpress-editor/blocks/mailchimp-block/',
'jetpack' => 'mailchimp-block',
add_action( 'load-options.php', __NAMESPACE__ . '\update_settings' );
* Update the site options that are related to Mailchimp.
function update_settings() {
$action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : '';
$option_page = ! empty( $_REQUEST['option_page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['option_page'] ) ) : '';
$audience = ! empty( $_REQUEST['jetpack-mailchimp-audience'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['jetpack-mailchimp-audience'] ) ) : '';
if ( $action !== 'update' || $option_page !== 'writing' || ! current_user_can( 'manage_options' ) || $audience === '' ) {
check_admin_referer( 'writing-options' );
$site_id = Connection_Manager::get_site_id();
if ( is_wp_error( $site_id ) ) {
if ( $audience === 'none' ) {
'follower_list_id' => '0',
$connection = External_Connections::get_connection( 'mailchimp' );
if ( empty( $connection ) ) {
'follower_list_id' => $audience,
'keyring_id' => $connection['ID'],
if ( ( new Host() )->is_wpcom_simple() ) {
require_lib( 'mailchimp' );
$response = \MailchimpApi::save_settings( $site_id, $data );
$response = Client::wpcom_json_api_request_as_user(
sprintf( '/sites/%d/mailchimp/settings', $site_id ),
array( 'method' => 'POST' ),
if ( is_wp_error( $response ) ) {
add_settings_error( 'general', 'settings_updated', __( 'Settings save failed.', 'jetpack' ), 'error' );