← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php
+104
-9
12.0.3
→
16.3-beta
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,35 @@ | ||
| 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 | + array( | |
| 74 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 75 | + 'callback' => array( $this, 'update_mailchimp_settings' ), | |
| 76 | + 'permission_callback' => function () { | |
| 77 | + return current_user_can( 'manage_options' ); | |
| 78 | + }, | |
| 79 | + 'args' => array( | |
| 80 | + 'follower_list_id' => array( | |
| 81 | + 'type' => 'string', | |
| 82 | + 'required' => true, | |
| 83 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 84 | + ), | |
| 85 | + ), | |
| 86 | + ), | |
| 87 | + ) | |
| 88 | + ); | |
| 57 | 89 | } |
| 58 | 90 | |
| 59 | 91 | /** |
| 60 | 92 | * Check if MailChimp is set up properly. |
| @@ -69,9 +101,9 @@ | ||
| 69 | 101 | $data = json_decode( $option, true ); |
| 70 | 102 | if ( ! $data ) { |
| 71 | 103 | return false; |
| 72 | 104 | } |
| 73 | - return isset( $data['follower_list_id'], $data['keyring_id'] ); | |
| 105 | + return isset( $data['follower_list_id'] ) && isset( $data['keyring_id'] ); | |
| 74 | 106 | } |
| 75 | 107 | |
| 76 | 108 | /** |
| 77 | 109 | * Get the status of current blog's Mailchimp connection |
| @@ -90,15 +122,9 @@ | ||
| 90 | 122 | __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), |
| 91 | 123 | 403 |
| 92 | 124 | ); |
| 93 | 125 | } |
| 94 | - $connect_url = Redirect::get_url( | |
| 95 | - 'calypso-marketing-connections', | |
| 96 | - array( | |
| 97 | - 'site' => rawurlencode( $site_id ), | |
| 98 | - 'query' => 'mailchimp', | |
| 99 | - ) | |
| 100 | - ); | |
| 126 | + $connect_url = '/wp-admin/options-writing.php'; | |
| 101 | 127 | return array( |
| 102 | 128 | 'code' => $this->is_connected() ? 'connected' : 'not_connected', |
| 103 | 129 | 'connect_url' => $connect_url, |
| 104 | 130 | 'site_id' => $site_id, |
| @@ -121,12 +147,81 @@ | ||
| 121 | 147 | __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), |
| 122 | 148 | 403 |
| 123 | 149 | ); |
| 124 | 150 | } |
| 151 | + | |
| 152 | + // Do not attempt to fetch groups if Mailchimp is not connected. | |
| 153 | + if ( ! $this->is_connected() ) { | |
| 154 | + return new WP_Error( | |
| 155 | + 'mailchimp_not_connected', | |
| 156 | + __( 'Your site is not connected to Mailchimp yet.', 'jetpack' ), | |
| 157 | + 403 | |
| 158 | + ); | |
| 159 | + } | |
| 160 | + | |
| 125 | 161 | $path = sprintf( '/sites/%d/mailchimp/groups', absint( $site_id ) ); |
| 126 | 162 | $request = Client::wpcom_json_api_request_as_blog( $path ); |
| 127 | 163 | $body = wp_remote_retrieve_body( $request ); |
| 128 | 164 | return json_decode( $body ); |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Get the Mailchimp connection settings. | |
| 169 | + * | |
| 170 | + * @return mixed | |
| 171 | + */ | |
| 172 | + public function get_mailchimp_settings() { | |
| 173 | + $site_id = Connection_Manager::get_site_id(); | |
| 174 | + if ( is_wp_error( $site_id ) ) { | |
| 175 | + return new WP_Error( | |
| 176 | + 'unavailable_site_id', | |
| 177 | + __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), | |
| 178 | + 403 | |
| 179 | + ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + $settings = array(); | |
| 183 | + | |
| 184 | + if ( ( new Host() )->is_wpcom_simple() ) { | |
| 185 | + require_lib( 'mailchimp' ); | |
| 186 | + $api = new MailchimpApi( $site_id, get_current_user_id() ); | |
| 187 | + $settings = $api::get_settings( $site_id ); | |
| 188 | + $settings['audiences'] = $api->get_lists(); | |
| 189 | + } else { | |
| 190 | + $path = sprintf( '/sites/%d/mailchimp/settings', $site_id ); | |
| 191 | + $response = Client::wpcom_json_api_request_as_user( $path, '1.1', array(), null, 'rest' ); | |
| 192 | + if ( ! is_wp_error( $response ) ) { | |
| 193 | + $settings = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 194 | + } | |
| 195 | + | |
| 196 | + $path = sprintf( '/sites/%d/mailchimp/lists', $site_id ); | |
| 197 | + $response = Client::wpcom_json_api_request_as_user( $path, '1.1', array(), null, 'rest' ); | |
| 198 | + if ( ! is_wp_error( $response ) ) { | |
| 199 | + $settings['audiences'] = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + return $settings; | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * Save the Mailchimp audience that block subscribers join. | |
| 208 | + * | |
| 209 | + * @since 16.3 | |
| 210 | + * | |
| 211 | + * @param WP_REST_Request $request The request. | |
| 212 | + * @return array|WP_Error | |
| 213 | + */ | |
| 214 | + public function update_mailchimp_settings( $request ) { | |
| 215 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-mailchimp-helper.php'; | |
| 216 | + | |
| 217 | + $audience = $request['follower_list_id']; | |
| 218 | + $result = Jetpack_Mailchimp_Helper::save_audience( $audience ); | |
| 219 | + if ( is_wp_error( $result ) ) { | |
| 220 | + return new WP_Error( $result->get_error_code(), $result->get_error_message(), array( 'status' => 400 ) ); | |
| 221 | + } | |
| 222 | + | |
| 223 | + return array( 'follower_list_id' => $audience ); | |
| 129 | 224 | } |
| 130 | 225 | } |
| 131 | 226 | |
| 132 | 227 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' ); |