Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Mailchimp_Helper | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
72 | |
0.00% |
0 / 1 |
| save_audience | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Helper to save the Mailchimp block settings. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Connection\Client; |
| 9 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 10 | use Automattic\Jetpack\External_Connections; |
| 11 | use Automattic\Jetpack\Status\Host; |
| 12 | |
| 13 | /** |
| 14 | * Class Jetpack_Mailchimp_Helper |
| 15 | */ |
| 16 | class Jetpack_Mailchimp_Helper { |
| 17 | /** |
| 18 | * Saves the Mailchimp audience that block subscribers join, or clears the settings. |
| 19 | * |
| 20 | * @since $$next-version$$ |
| 21 | * |
| 22 | * @param string $audience Mailchimp list ID, or 'none' to clear the settings. |
| 23 | * @return true|WP_Error |
| 24 | */ |
| 25 | public static function save_audience( $audience ) { |
| 26 | $site_id = Connection_Manager::get_site_id(); |
| 27 | if ( is_wp_error( $site_id ) ) { |
| 28 | return $site_id; |
| 29 | } |
| 30 | |
| 31 | if ( $audience === 'none' ) { |
| 32 | $data = array( |
| 33 | 'follower_list_id' => '0', |
| 34 | 'keyring_id' => '0', |
| 35 | ); |
| 36 | } else { |
| 37 | $connection = External_Connections::get_connection( 'mailchimp' ); |
| 38 | if ( empty( $connection ) ) { |
| 39 | return new WP_Error( 'mailchimp_not_connected', __( 'Your site is not connected to Mailchimp yet.', 'jetpack' ) ); |
| 40 | } |
| 41 | $data = array( |
| 42 | 'follower_list_id' => $audience, |
| 43 | 'keyring_id' => $connection['ID'], |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 48 | require_lib( 'mailchimp' ); |
| 49 | $response = \MailchimpApi::save_settings( $site_id, $data ); |
| 50 | return is_wp_error( $response ) ? $response : true; |
| 51 | } |
| 52 | |
| 53 | $response = Client::wpcom_json_api_request_as_user( |
| 54 | sprintf( '/sites/%d/mailchimp/settings', $site_id ), |
| 55 | '1.1', |
| 56 | array( 'method' => 'POST' ), |
| 57 | $data, |
| 58 | 'rest' |
| 59 | ); |
| 60 | if ( is_wp_error( $response ) ) { |
| 61 | return $response; |
| 62 | } |
| 63 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 64 | $body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 65 | return new WP_Error( |
| 66 | $body['error'] ?? 'mailchimp_save_failed', |
| 67 | $body['message'] ?? __( 'Settings save failed.', 'jetpack' ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | } |