Completed
Push — master-stable ( 123e21...46eadb )
by
unknown
62:16 queued 49:39
created

WPCOM_JSON_API_Get_Connection_Endpoint::callback()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 30
Code Lines 15

Duplication

Lines 6
Ratio 20 %
Metric Value
dl 6
loc 30
rs 4.9091
nc 6
cc 9
eloc 15
nop 3
1
<?php
2
3
class WPCOM_JSON_API_Get_Connections_Endpoint extends WPCOM_JSON_API_Endpoint {
4
	// /sites/%s/connections
5
	function callback( $path = '', $blog_id = 0 ) {
6
		// Verify required Publicize Jetpack module is active
7 View Code Duplication
		if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) {
8
			return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 );
9
		}
10
11
		// Authenticate user
12
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
13
		if ( is_wp_error( $blog_id ) ) {
14
			return $blog_id;
15
		}
16
17
		$current_user = wp_get_current_user();
18
		if ( ! $current_user->ID ) {
19
			return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 );
20
		}
21
22
		// Parse query arguments to determine if filtering is requested
23
		$args = $this->query_args();
24
		$service_filter = false;
25
		if ( ! empty( $args['service'] ) ) {
26
			$service_filter = $args['service'];
27
		}
28
29
		// Iterate over connected services
30
		$publicize = new Publicize();
31
		$connected_services = $publicize->get_services( 'connected' );
32
		$output = array();
33
		foreach( $connected_services as $service => $connections ) {
34
			if ( false != $service_filter && $service_filter != $service ) {
35
				continue;
36
			}
37
38
			foreach ( $connections as $connection_id => $connection ) {
39
				$output[] = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection( $service, $connection );
40
			}
41
		}
42
43
		return array( 'connections' => $output );
44
	}
45
}
46
47
class WPCOM_JSON_API_Get_Connection_Endpoint extends WPCOM_JSON_API_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
48
	function get_connection_by_id( $connection_id ) {
49
		$publicize = new Publicize();
50
51
		$connected_services = $publicize->get_services( 'connected' );
52
		foreach ( $connected_services as $service => $connections ) {
53
			foreach ( $connections as $c => $connection ) {
54
				if ( $connection_id == $publicize->get_connection_id( $connection ) ) {
55
					return WPCOM_JSON_API_Get_Connection_Endpoint::get_connection( $service, $connections[ $c ] );
56
				}
57
			}
58
		}
59
60
		return false;
61
	}
62
63
	function get_connection( $service, $connection ) {
64
		$publicize = new Publicize();
65
66
		$connection_id = $publicize->get_connection_id( $connection );
67
		if ( method_exists( $connection, 'get_meta' ) ) {
68
			$connection_meta = $connection->get_meta();
69
			$connection_data = (array) $connection->get_meta( 'connection_data' );
70
		} else {
71
			$connection_meta = $connection;
72
			$connection_data = $connection['connection_data'];
73
		}
74
75
		return array(
76
			'ID'               => (int) $connection_id,
77
			'token_ID'         => (int) $connection_data['token_id'],
78
			'conn_ID'          => (int) $connection_id,
79
			'site_ID'          => (int) $connection_data['blog_id'],
80
			'user_ID'          => (int) $connection_data['user_id'],
81
			'shared'           => ( 0 == (int) $connection_data['user_id'] ) ? true : false,
82
			'service'          => $service,
83
			'label'            => $publicize->get_service_label( $service ),
84
			'issued'           => $connection_meta['issued'],
85
			'expires'          => $connection_meta['expires'],
86
			'external_ID'      => $connection_meta['external_id'],
87
			'external_name'    => $connection_meta['external_name'],
88
			'external_display' => $publicize->get_display_name( $service, $connection ),
89
			'URL'              => $publicize->get_profile_link( $service, $connection ),
90
			'status'           => ( method_exists( $connection, 'is_expired' ) && $connection->is_expired( HOUR_IN_SECONDS ) ) ? 'broken' : 'ok',
91
			'refresh_url'      => $publicize->refresh_url( $service ),
0 ignored issues
show
Bug introduced by
The method refresh_url() does not seem to exist on object<Publicize>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
			'meta'             => maybe_unserialize( $connection_data['meta'] ),
93
		);
94
	}
95
96
	// /sites/%s/connections/$connection_id
97
	function callback( $path = '', $blog_id = 0, $connection_id = 0 ) {
98
		// Verify required Publicize Jetpack module is active
99 View Code Duplication
		if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) {
100
			return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 );
101
		}
102
103
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
104
		if ( is_wp_error( $blog_id ) ) {
105
			return $blog_id;
106
		}
107
108
		$current_user = wp_get_current_user();
109
		if ( ! $current_user->ID ) {
110
			return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 );
111
		}
112
113
		// Attempt to find connection
114
		$connection = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection_by_id( $connection_id );
115
116
		// Verify that user has permission to view this connection
117 View Code Duplication
		if ( $current_user->ID != $connection['user_ID'] && 0 != $connection['user_ID'] ) {
118
			return new WP_Error( 'authorization_required', 'You do not have permission to access this resource.', 403 );
119
		}
120
121
		if ( empty( $connection ) ) {
122
			return new WP_Error( 'unknown_connection', 'Connection not found.', 404 );
123
		}
124
125
		return $connection;
126
	}
127
}
128
129
class WPCOM_JSON_API_Delete_Connection_Endpoint extends WPCOM_JSON_API_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
130
	// /sites/%s/connections/$connection_id/delete
131
	function callback( $path = '', $blog_id = 0 , $connection_id = 0 ) {
132
		// Verify required Publicize Jetpack module is active
133 View Code Duplication
		if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) {
134
			return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 );
135
		}
136
137
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
138
		if ( is_wp_error( $blog_id ) ) {
139
			return $blog_id;
140
		}
141
142
		$current_user = wp_get_current_user();
143
		if ( ! $current_user->ID ) {
144
			return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 );
145
		}
146
147
		// Attempt to find connection
148
		$connection = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection_by_id( $connection_id );
149
150
		if ( empty( $connection ) ) {
151
			return new WP_Error( 'unknown_connection', 'Connection not found.', 404 );
152
		}
153
154
		// Verify that user has permission to view this connection
155 View Code Duplication
		if ( $current_user->ID != $connection['user_ID'] && 0 != $connection['user_ID'] ) {
156
			return new WP_Error( 'authorization_required', 'You do not have permission to access this resource.', 403 );
157
		}
158
159
		// Remove publicize connections related to the connection
160
		$publicize = new Publicize();
161
		$is_deleted = ( false !== $publicize->disconnect( $connection['service'], $connection_id ) );
0 ignored issues
show
Bug introduced by
The method disconnect() does not seem to exist on object<Publicize>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
163
		if ( $is_deleted ) {
164
			/**
165
			 * Fires when a Publicize connection is deleted.
166
			 *
167
			 * @module json-api
168
			 *
169
			 * @since 3.2.0
170
			 *
171
			 * @param int $connection_id Publicize connection ID.
172
			 */
173
			do_action( 'rest_api_delete_publicize_connection', $connection_id );
174
		}
175
176
		return array(
177
			'ID' => (int) $connection_id,
178
			'deleted' => $is_deleted
179
		);
180
	}
181
}
182