| @@ -10,9 +10,12 @@ | ||
| 10 | 10 | namespace Automattic\Jetpack\Extensions\Mailchimp; |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Assets; |
| 13 | 13 | use Automattic\Jetpack\Blocks; |
| 14 | +use Automattic\Jetpack\Connection\Client; | |
| 15 | +use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 14 | 16 | use Automattic\Jetpack\External_Connections; |
| 17 | +use Automattic\Jetpack\Status\Host; | |
| 15 | 18 | use Jetpack; |
| 16 | 19 | use Jetpack_Gutenberg; |
| 17 | 20 | |
| 18 | 21 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -100,9 +103,41 @@ | ||
| 100 | 103 | } |
| 101 | 104 | |
| 102 | 105 | check_admin_referer( 'writing-options' ); |
| 103 | 106 | |
| 104 | - require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-mailchimp-helper.php'; | |
| 105 | - if ( is_wp_error( \Jetpack_Mailchimp_Helper::save_audience( $audience ) ) ) { | |
| 107 | + $site_id = Connection_Manager::get_site_id(); | |
| 108 | + if ( is_wp_error( $site_id ) ) { | |
| 109 | + return; | |
| 110 | + } | |
| 111 | + | |
| 112 | + if ( $audience === 'none' ) { | |
| 113 | + $data = array( | |
| 114 | + 'follower_list_id' => '0', | |
| 115 | + 'keyring_id' => '0', | |
| 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 | + ); | |
| 126 | + } | |
| 127 | + | |
| 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' | |
| 138 | + ); | |
| 139 | + } | |
| 140 | + if ( is_wp_error( $response ) ) { | |
| 106 | 141 | add_settings_error( 'general', 'settings_updated', __( 'Settings save failed.', 'jetpack' ), 'error' ); |
| 107 | 142 | } |
| 108 | 143 | } |