PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 13.0.2
Jetpack – WP Security, Backup, Speed, & Growth v13.0.2
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 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / _inc / lib / core-api / wpcom-endpoints / class-wpcom-rest-api-v2-endpoint-mailchimp.php
class-wpcom-rest-api-v2-endpoint-mailchimp.php
133 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API endpoints to interact with WordPress.com
4 * to get info from the Mailchimp API for use with the Mailchimp block.
5 *
6 * @package automattic/jetpack
7 */
8
9 use Automattic\Jetpack\Connection\Client;
10 use Automattic\Jetpack\Redirect;
11
12 /**
13 * Mailchimp: Get Mailchimp Status.
14 * API to determine if current site has linked Mailchimp account and mailing list selected.
15 * This API is meant to be used in Jetpack and on WPCOM.
16 *
17 * @since 7.1
18 */
19 class WPCOM_REST_API_V2_Endpoint_Mailchimp extends WP_REST_Controller {
20 /**
21 * Constructor.
22 */
23 public function __construct() {
24 $this->namespace = 'wpcom/v2';
25 $this->rest_base = 'mailchimp';
26 $this->wpcom_is_wpcom_only_endpoint = true;
27
28 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
29 }
30
31 /**
32 * Called automatically on `rest_api_init()`.
33 */
34 public function register_routes() {
35 register_rest_route(
36 $this->namespace,
37 $this->rest_base,
38 array(
39 array(
40 'methods' => WP_REST_Server::READABLE,
41 'callback' => array( $this, 'get_mailchimp_status' ),
42 'permission_callback' => '__return_true',
43 ),
44 )
45 );
46 register_rest_route(
47 $this->namespace,
48 $this->rest_base . '/groups',
49 array(
50 array(
51 'methods' => WP_REST_Server::READABLE,
52 'callback' => array( $this, 'get_mailchimp_groups' ),
53 'permission_callback' => '__return_true',
54 ),
55 )
56 );
57 }
58
59 /**
60 * Check if MailChimp is set up properly.
61 *
62 * @return bool
63 */
64 private function is_connected() {
65 $option = get_option( 'jetpack_mailchimp' );
66 if ( ! $option ) {
67 return false;
68 }
69 $data = json_decode( $option, true );
70 if ( ! $data ) {
71 return false;
72 }
73 return isset( $data['follower_list_id'] ) && isset( $data['keyring_id'] );
74 }
75
76 /**
77 * Get the status of current blog's Mailchimp connection
78 *
79 * @return mixed
80 * code:string (connected|unconnected),
81 * connect_url:string
82 * site_id:int
83 */
84 public function get_mailchimp_status() {
85 $is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM );
86 $site_id = $is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' );
87 if ( ! $site_id ) {
88 return new WP_Error(
89 'unavailable_site_id',
90 __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ),
91 403
92 );
93 }
94 $connect_url = Redirect::get_url(
95 'calypso-marketing-connections',
96 array(
97 'site' => rawurlencode( $site_id ),
98 'query' => 'mailchimp',
99 )
100 );
101 return array(
102 'code' => $this->is_connected() ? 'connected' : 'not_connected',
103 'connect_url' => $connect_url,
104 'site_id' => $site_id,
105 );
106 }
107
108 /**
109 * Get all Mailchimp groups for the accounted connected to the current blog
110 *
111 * @return mixed
112 * groups:array
113 * site_id:int
114 */
115 public function get_mailchimp_groups() {
116 $is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM );
117 $site_id = $is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' );
118 if ( ! $site_id ) {
119 return new WP_Error(
120 'unavailable_site_id',
121 __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ),
122 403
123 );
124 }
125 $path = sprintf( '/sites/%d/mailchimp/groups', absint( $site_id ) );
126 $request = Client::wpcom_json_api_request_as_blog( $path );
127 $body = wp_remote_retrieve_body( $request );
128 return json_decode( $body );
129 }
130 }
131
132 wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' );
133