← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php
+69
-8
13.2.4
→
16.3-a.5
View file →
| @@ -6,10 +6,15 @@ | ||
| 6 | 6 | * @package automattic/jetpack |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | use Automattic\Jetpack\Connection\Client; |
| 10 | -use Automattic\Jetpack\Redirect; | |
| 10 | +use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 11 | +use Automattic\Jetpack\Status\Host; | |
| 11 | 12 | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit( 0 ); | |
| 15 | +} | |
| 16 | + | |
| 12 | 17 | /** |
| 13 | 18 | * Mailchimp: Get Mailchimp Status. |
| 14 | 19 | * API to determine if current site has linked Mailchimp account and mailing list selected. |
| 15 | 20 | * This API is meant to be used in Jetpack and on WPCOM. |
| @@ -53,8 +58,21 @@ | ||
| 53 | 58 | 'permission_callback' => '__return_true', |
| 54 | 59 | ), |
| 55 | 60 | ) |
| 56 | 61 | ); |
| 62 | + register_rest_route( | |
| 63 | + $this->namespace, | |
| 64 | + $this->rest_base . '/settings', | |
| 65 | + array( | |
| 66 | + array( | |
| 67 | + 'methods' => WP_REST_Server::READABLE, | |
| 68 | + 'callback' => array( $this, 'get_mailchimp_settings' ), | |
| 69 | + 'permission_callback' => function () { | |
| 70 | + return current_user_can( 'manage_options' ); | |
| 71 | + }, | |
| 72 | + ), | |
| 73 | + ) | |
| 74 | + ); | |
| 57 | 75 | } |
| 58 | 76 | |
| 59 | 77 | /** |
| 60 | 78 | * Check if MailChimp is set up properly. |
| @@ -90,15 +108,9 @@ | ||
| 90 | 108 | __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), |
| 91 | 109 | 403 |
| 92 | 110 | ); |
| 93 | 111 | } |
| 94 | - $connect_url = Redirect::get_url( | |
| 95 | - 'calypso-marketing-connections', | |
| 96 | - array( | |
| 97 | - 'site' => rawurlencode( $site_id ), | |
| 98 | - 'query' => 'mailchimp', | |
| 99 | - ) | |
| 100 | - ); | |
| 112 | + $connect_url = '/wp-admin/options-writing.php'; | |
| 101 | 113 | return array( |
| 102 | 114 | 'code' => $this->is_connected() ? 'connected' : 'not_connected', |
| 103 | 115 | 'connect_url' => $connect_url, |
| 104 | 116 | 'site_id' => $site_id, |
| @@ -121,12 +133,61 @@ | ||
| 121 | 133 | __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), |
| 122 | 134 | 403 |
| 123 | 135 | ); |
| 124 | 136 | } |
| 137 | + | |
| 138 | + // Do not attempt to fetch groups if Mailchimp is not connected. | |
| 139 | + if ( ! $this->is_connected() ) { | |
| 140 | + return new WP_Error( | |
| 141 | + 'mailchimp_not_connected', | |
| 142 | + __( 'Your site is not connected to Mailchimp yet.', 'jetpack' ), | |
| 143 | + 403 | |
| 144 | + ); | |
| 145 | + } | |
| 146 | + | |
| 125 | 147 | $path = sprintf( '/sites/%d/mailchimp/groups', absint( $site_id ) ); |
| 126 | 148 | $request = Client::wpcom_json_api_request_as_blog( $path ); |
| 127 | 149 | $body = wp_remote_retrieve_body( $request ); |
| 128 | 150 | return json_decode( $body ); |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Get the Mailchimp connection settings. | |
| 155 | + * | |
| 156 | + * @return mixed | |
| 157 | + */ | |
| 158 | + public function get_mailchimp_settings() { | |
| 159 | + $site_id = Connection_Manager::get_site_id(); | |
| 160 | + if ( is_wp_error( $site_id ) ) { | |
| 161 | + return new WP_Error( | |
| 162 | + 'unavailable_site_id', | |
| 163 | + __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), | |
| 164 | + 403 | |
| 165 | + ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + $settings = array(); | |
| 169 | + | |
| 170 | + if ( ( new Host() )->is_wpcom_simple() ) { | |
| 171 | + require_lib( 'mailchimp' ); | |
| 172 | + $api = new MailchimpApi( $site_id, get_current_user_id() ); | |
| 173 | + $settings = $api::get_settings( $site_id ); | |
| 174 | + $settings['audiences'] = $api->get_lists(); | |
| 175 | + } else { | |
| 176 | + $path = sprintf( '/sites/%d/mailchimp/settings', $site_id ); | |
| 177 | + $response = Client::wpcom_json_api_request_as_user( $path, '1.1', array(), null, 'rest' ); | |
| 178 | + if ( ! is_wp_error( $response ) ) { | |
| 179 | + $settings = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + $path = sprintf( '/sites/%d/mailchimp/lists', $site_id ); | |
| 183 | + $response = Client::wpcom_json_api_request_as_user( $path, '1.1', array(), null, 'rest' ); | |
| 184 | + if ( ! is_wp_error( $response ) ) { | |
| 185 | + $settings['audiences'] = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 186 | + } | |
| 187 | + } | |
| 188 | + | |
| 189 | + return $settings; | |
| 129 | 190 | } |
| 130 | 191 | } |
| 131 | 192 | |
| 132 | 193 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' ); |