class.wpcom-json-api-publicize-endpoint.php
6.94 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
class WPCOM_JSON_API_Get_Connections_Endpoint extends WPCOM_JSON_API_Endpoint {
// /sites/%s/connections
function callback( $path = '', $blog_id = 0 ) {
// Verify required Publicize Jetpack module is active
if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) {
return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 );
}
// Authenticate user
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
}
$current_user = wp_get_current_user();
if ( ! $current_user->ID ) {
return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 );
}
// Parse query arguments to determine if filtering is requested
$args = $this->query_args();
$service_filter = false;
if ( ! empty( $args['service'] ) ) {
$service_filter = $args['service'];
}
// Iterate over connected services
$publicize = new Publicize();
$connected_services = $publicize->get_services( 'connected' );
$output = array();
foreach( $connected_services as $service => $connections ) {
if ( false != $service_filter && $service_filter != $service ) {
continue;
}
foreach ( $connections as $connection_id => $connection ) {
$output[] = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection( $service, $connection );
}
}
return array( 'connections' => $output );
}
}
class WPCOM_JSON_API_Get_Connection_Endpoint extends WPCOM_JSON_API_Endpoint {
function get_connection_by_id( $connection_id ) {
$publicize = new Publicize();
$connected_services = $publicize->get_services( 'connected' );
foreach ( $connected_services as $service => $connections ) {
foreach ( $connections as $c => $connection ) {
if ( $connection_id == $publicize->get_connection_id( $connection ) ) {
return WPCOM_JSON_API_Get_Connection_Endpoint::get_connection( $service, $connections[ $c ] );
}
}
}
return false;
}
function get_connection( $service, $connection ) {
$publicize = new Publicize();
$connection_id = $publicize->get_connection_id( $connection );
if ( method_exists( $connection, 'get_meta' ) ) {
$connection_meta = $connection->get_meta();
$connection_data = (array) $connection->get_meta( 'connection_data' );
} else {
$connection_meta = $connection;
$connection_data = $connection['connection_data'];
}
return array(
'ID' => (int) $connection_id,
'token_ID' => (int) $connection_data['token_id'],
'conn_ID' => (int) $connection_id,
'site_ID' => (int) $connection_data['blog_id'],
'user_ID' => (int) $connection_data['user_id'],
'shared' => ( 0 == (int) $connection_data['user_id'] ) ? true : false,
'service' => $service,
'label' => $publicize->get_service_label( $service ),
'issued' => $connection_meta['issued'],
'expires' => $connection_meta['expires'],
'external_ID' => $connection_meta['external_id'],
'external_name' => $connection_meta['external_name'],
'external_display' => $publicize->get_display_name( $service, $connection ),
'URL' => $publicize->get_profile_link( $service, $connection ),
'status' => ( method_exists( $connection, 'is_expired' ) && $connection->is_expired( HOUR_IN_SECONDS ) ) ? 'broken' : 'ok',
'refresh_url' => $publicize->refresh_url( $service ),
'meta' => maybe_unserialize( $connection_data['meta'] ),
);
}
// /sites/%s/connections/$connection_id
function callback( $path = '', $blog_id = 0, $connection_id = 0 ) {
// Verify required Publicize Jetpack module is active
if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) {
return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 );
}
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
}
$current_user = wp_get_current_user();
if ( ! $current_user->ID ) {
return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 );
}
// Attempt to find connection
$connection = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection_by_id( $connection_id );
// Verify that user has permission to view this connection
if ( $current_user->ID != $connection['user_ID'] && 0 != $connection['user_ID'] ) {
return new WP_Error( 'authorization_required', 'You do not have permission to access this resource.', 403 );
}
if ( empty( $connection ) ) {
return new WP_Error( 'unknown_connection', 'Connection not found.', 404 );
}
return $connection;
}
}
class WPCOM_JSON_API_Delete_Connection_Endpoint extends WPCOM_JSON_API_Endpoint {
// /sites/%s/connections/$connection_id/delete
function callback( $path = '', $blog_id = 0 , $connection_id = 0 ) {
// Verify required Publicize Jetpack module is active
if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) {
return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 );
}
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
}
$current_user = wp_get_current_user();
if ( ! $current_user->ID ) {
return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 );
}
// Attempt to find connection
$connection = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection_by_id( $connection_id );
if ( empty( $connection ) ) {
return new WP_Error( 'unknown_connection', 'Connection not found.', 404 );
}
// Verify that user has permission to view this connection
if ( $current_user->ID != $connection['user_ID'] && 0 != $connection['user_ID'] ) {
return new WP_Error( 'authorization_required', 'You do not have permission to access this resource.', 403 );
}
// Remove publicize connections related to the connection
$publicize = new Publicize();
$is_deleted = ( false !== $publicize->disconnect( $connection['service'], $connection_id ) );
if ( $is_deleted ) {
/**
* Fires when a Publicize connection is deleted.
*
* @module json-api
*
* @since 3.2.0
*
* @param int $connection_id Publicize connection ID.
*/
do_action( 'rest_api_delete_publicize_connection', $connection_id );
}
return array(
'ID' => (int) $connection_id,
'deleted' => $is_deleted
);
}
}