-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathclass-mailchimp-api.php
180 lines (158 loc) · 4.91 KB
/
class-mailchimp-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Newspack's Mailchimp API handling.
*
* @package Newspack
*/
namespace Newspack;
defined( 'ABSPATH' ) || exit;
/**
* Mailchimp API Settings.
*/
class Mailchimp_API {
/** API Endpoint Placeholder, needs to replace <dc> by the right datacenter. */
const API_ENDPOINT_PLACEHOLDER = 'https://<dc>.api.mailchimp.com/3.0';
/**
* Constructor.
*/
public function __construct() {
add_action( 'rest_api_init', [ __CLASS__, 'register_api_endpoints' ] );
}
/**
* Register the endpoints.
*/
public static function register_api_endpoints() {
// Get Mailchimp auth status.
\register_rest_route(
NEWSPACK_API_NAMESPACE,
'/oauth/mailchimp',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'api_mailchimp_auth_status' ],
'permission_callback' => function() {
return current_user_can( 'manage_options' );
},
]
);
// Verify and save Mailchimp API Key.
\register_rest_route(
NEWSPACK_API_NAMESPACE,
'/oauth/mailchimp',
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'api_mailchimp_save_key' ],
'permission_callback' => function() {
return current_user_can( 'manage_options' );
},
'args' => [
'api_key' => [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'required' => true,
],
],
]
);
// Delete Mailchimp API Key.
\register_rest_route(
NEWSPACK_API_NAMESPACE,
'/oauth/mailchimp',
[
'methods' => \WP_REST_Server::DELETABLE,
'callback' => [ __CLASS__, 'api_mailchimp_delete_key' ],
'permission_callback' => function() {
return current_user_can( 'manage_options' );
},
]
);
}
/**
* Check whether we have an API key and its validity.
*
* @return WP_REST_Response
*/
public static function api_mailchimp_auth_status() {
// 'newspack_mailchimp_api_key' is a new option introduced to manage MC API key accross Newspack plugins.
// Keeping the old option for backwards compatibility.
$mailchimp_api_key = get_option( 'newspack_mailchimp_api_key', get_option( 'newspack_newsletters_mailchimp_api_key' ) );
$endpoint = self::get_api_endpoint_from_key( $mailchimp_api_key );
if ( ! $mailchimp_api_key || ! $endpoint ) {
return \rest_ensure_response( [] );
}
$key_is_valid_response = self::is_valid_api_key( $endpoint, $mailchimp_api_key );
return $key_is_valid_response;
}
/**
* Verify and save Mailchimp API key
*
* @param WP_REST_REQUEST $request call request.
* @return WP_REST_Response
*/
public static function api_mailchimp_save_key( $request ) {
$endpoint = self::get_api_endpoint_from_key( $request['api_key'] );
if ( ! $endpoint ) {
return new \WP_Error( 'wrong_parameter', __( 'Invalid Mailchimp API Key.', 'newspack' ) );
}
$key_is_valid_response = self::is_valid_api_key( $endpoint, $request['api_key'] );
if ( ! is_wp_error( $key_is_valid_response ) ) {
update_option( 'newspack_mailchimp_api_key', $request['api_key'] );
}
return $key_is_valid_response;
}
/**
* Delete Mailchimp API Key option.
*
* @return WP_REST_Response
*/
public static function api_mailchimp_delete_key() {
delete_option( 'newspack_mailchimp_api_key' );
return \rest_ensure_response( [] );
}
/**
* Get the right datacenter from the API key and set it in the endpoint.
*
* @param string $api_key Mailchimp API Key.
* @return string|boolean the API endpoint with the right datacenter.
*/
private static function get_api_endpoint_from_key( $api_key ) {
if ( strpos( $api_key, '-' ) === false ) {
return false;
}
list(, $data_center) = explode( '-', $api_key );
// Mailchimp API key format: abc123-datacenter.
return $data_center ? str_replace( '<dc>', $data_center, self::API_ENDPOINT_PLACEHOLDER ) : false;
}
/**
* Check API Key validity
*
* @param string $endpoint Mailchimp API Endpoint.
* @param string $api_key Mailchimp API Key.
* @return WP_REST_Response|WP_Error
*/
private static function is_valid_api_key( $endpoint, $api_key ) {
// Mailchimp API root endpoint returns details about the Mailchimp user account.
$response = wp_safe_remote_get(
$endpoint,
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => "Basic $api_key",
],
]
);
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
$parsed_response = json_decode( $response['body'], true );
return new \WP_Error(
'newspack_mailchimp_api',
array_key_exists( 'title', $parsed_response ) ? $parsed_response['title'] : __( 'Request failed.', 'newspack' )
);
}
$response_body = json_decode( $response['body'], true );
return \rest_ensure_response( [ 'username' => $response_body['username'] ] );
}
}
new Mailchimp_API();