business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-color.php
2 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
1 year ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-app-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-blog-stats.php
2 years ago
class-wpcom-rest-api-v2-endpoint-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-goodreads.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
2 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-subscriptions-count.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
2 years ago
class-wpcom-rest-api-v2-endpoint-profile.php
2 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
1 year ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
2 years ago
gutenberg-available-extensions.php
2 years ago
hello.php
4 years ago
memberships.php
2 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
2 years ago
publicize-services.php
3 years ago
service-api-keys.php
2 years ago
sites-posts-featured-media-url.php
2 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
143 lines
| 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 | |
| 126 | // Do not attempt to fetch groups if Mailchimp is not connected. |
| 127 | if ( ! $this->is_connected() ) { |
| 128 | return new WP_Error( |
| 129 | 'mailchimp_not_connected', |
| 130 | __( 'Your site is not connected to Mailchimp yet.', 'jetpack' ), |
| 131 | 403 |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | $path = sprintf( '/sites/%d/mailchimp/groups', absint( $site_id ) ); |
| 136 | $request = Client::wpcom_json_api_request_as_blog( $path ); |
| 137 | $body = wp_remote_retrieve_body( $request ); |
| 138 | return json_decode( $body ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' ); |
| 143 |