PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 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 All 507 releases
jetpack / _inc / lib / class-jetpack-mailchimp-helper.php

class-jetpack-mailchimp-helper.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at _inc/lib/class-jetpack-mailchimp-helper.php

74 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 16.3
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 }
74