|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Mailchimp: Get Mailchimp Status. |
|
5
|
|
|
* API to determine if current site has linked Mailchimp account and mailing list selected. |
|
6
|
|
|
* This API is meant to be used in Jetpack and on WPCOM. |
|
7
|
|
|
* |
|
8
|
|
|
* @since 7.1 |
|
9
|
|
|
*/ |
|
10
|
|
|
class WPCOM_REST_API_V2_Endpoint_Mailchimp extends WP_REST_Controller { |
|
11
|
|
View Code Duplication |
public function __construct() { |
|
12
|
|
|
$this->namespace = 'wpcom/v2'; |
|
13
|
|
|
$this->rest_base = 'mailchimp'; |
|
14
|
|
|
$this->wpcom_is_wpcom_only_endpoint = true; |
|
15
|
|
|
|
|
16
|
|
|
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Called automatically on `rest_api_init()`. |
|
21
|
|
|
*/ |
|
22
|
|
View Code Duplication |
public function register_routes() { |
|
23
|
|
|
register_rest_route( |
|
24
|
|
|
$this->namespace, |
|
25
|
|
|
$this->rest_base, |
|
26
|
|
|
array( |
|
27
|
|
|
array( |
|
28
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
29
|
|
|
'callback' => array( $this, 'get_mailchimp_status' ), |
|
30
|
|
|
), |
|
31
|
|
|
) |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Check if MailChimp is set up properly. |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
View Code Duplication |
private function is_connected() { |
|
41
|
|
|
$option = get_option( 'jetpack_mailchimp' ); |
|
42
|
|
|
if ( ! $option ) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
$data = json_decode( $option, true ); |
|
46
|
|
|
if ( ! $data ) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
return isset( $data['follower_list_id'], $data['keyring_id'] ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the status of current blog's Mailchimp connection |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
* code:string (connected|unconnected), |
|
57
|
|
|
* connect_url:string |
|
58
|
|
|
* site_id:int |
|
59
|
|
|
*/ |
|
60
|
|
|
public function get_mailchimp_status() { |
|
61
|
|
|
$is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM ); |
|
62
|
|
|
$site_id = $is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' ); |
|
63
|
|
|
if ( ! $site_id ) { |
|
64
|
|
|
return new WP_Error( |
|
65
|
|
|
'unavailable_site_id', |
|
66
|
|
|
__( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), |
|
67
|
|
|
403 |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
$connect_url = sprintf( 'https://wordpress.com/sharing/%s', rawurlencode( $site_id ) ); |
|
71
|
|
|
return array( |
|
72
|
|
|
'code' => $this->is_connected() ? 'connected' : 'not_connected', |
|
73
|
|
|
'connect_url' => $connect_url, |
|
74
|
|
|
'site_id' => $site_id, |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' ); |
|
80
|
|
|
|