1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
<?php
/**
* REST API endpoint for the Instagram connections.
*
* @package automattic/jetpack
* @since 8.5.0
*/
use Automattic\Jetpack\Connection\Client;
/**
* Instagram connections helper API.
*
* @since 8.5
*/
class WPCOM_REST_API_V2_Endpoint_Instagram_Gallery extends WP_REST_Controller {
/**
* Constructor.
*/
public function __construct() {
$this->namespace = 'wpcom/v2';
$this->rest_base = 'instagram-gallery';
$this->is_wpcom = false;
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$this->is_wpcom = true;
if ( ! class_exists( 'WPCOM_Instagram_Gallery_Helper' ) ) {
\jetpack_require_lib( 'instagram-gallery-helper' );
}
}
if ( ! class_exists( 'Jetpack_Instagram_Gallery_Helper' ) ) {
\jetpack_require_lib( 'class-jetpack-instagram-gallery-helper' );
}
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
/**
* Register the route.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
$this->rest_base . '/connect-url',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_instagram_connect_url' ),
'permission_callback' => '__return_true',
)
);
register_rest_route(
$this->namespace,
$this->rest_base . '/connections',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_instagram_connections' ),
'permission_callback' => 'is_user_logged_in',
)
);
register_rest_route(
$this->namespace,
$this->rest_base . '/gallery',
array(
'args' => array(
'access_token' => array(
'description' => __( 'An Instagram Keyring access token.', 'jetpack' ),
'type' => 'string',
'required' => true,
),
'count' => array(
'description' => __( 'How many Instagram posts?', 'jetpack' ),
'type' => 'int',
'required' => true,
'validate_callback' => function ( $param ) {
return is_numeric( $param );
},
),
),
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_instagram_gallery' ),
'permission_callback' => '__return_true',
)
);
}
/**
* Get the Instagram connect URL.
*
* @return mixed
*/
public function get_instagram_connect_url() {
if ( $this->is_wpcom ) {
return WPCOM_Instagram_Gallery_Helper::get_connect_url();
}
$site_id = Jetpack_Instagram_Gallery_Helper::get_site_id();
if ( is_wp_error( $site_id ) ) {
return $site_id;
}
$path = sprintf( '/sites/%d/external-services', $site_id );
$response = Client::wpcom_json_api_request_as_user( $path );
if ( is_wp_error( $response ) ) {
return $response;
}
$body = json_decode( wp_remote_retrieve_body( $response ) );
if ( ! property_exists( $body, 'services' ) || ! property_exists( $body->services, 'instagram-basic-display' ) ) {
return new WP_Error(
'bad_request',
__( 'An error occurred. Please try again later.', 'jetpack' ),
array( 'status' => 400 )
);
}
return $body->services->{ 'instagram-basic-display' }->connect_URL;
}
/**
* Get a list of stored Instagram connections for the current user.
*
* @return mixed
*/
public function get_instagram_connections() {
if ( $this->is_wpcom ) {
return WPCOM_Instagram_Gallery_Helper::get_connections();
}
$response = Client::wpcom_json_api_request_as_user( '/me/connections' );
if ( is_wp_error( $response ) ) {
return $response;
}
$body = json_decode( wp_remote_retrieve_body( $response ) );
$connections = array();
if ( isset( $body->connections ) && is_array( $body->connections ) ) {
foreach ( $body->connections as $connection ) {
if ( 'instagram-basic-display' === $connection->service && 'ok' === $connection->status ) {
$connections[] = array(
'token' => (string) $connection->ID,
'username' => $connection->external_name,
);
}
}
}
return $connections;
}
/**
* Get the Instagram Gallery.
*
* @param WP_REST_Request $request The request.
* @return mixed
*/
public function get_instagram_gallery( $request ) {
return Jetpack_Instagram_Gallery_Helper::get_instagram_gallery( $request['access_token'], $request['count'] );
}
}
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Instagram_Gallery' );
|