WPCOM_JSON_API_Get_Connection_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Get Connection Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-publicize-endpoint.php  
  1. class WPCOM_JSON_API_Get_Connection_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. function get_connection_by_id( $connection_id ) { 
  3. $publicize = new Publicize(); 
  4.  
  5. $connected_services = $publicize->get_services( 'connected' ); 
  6. foreach ( $connected_services as $service => $connections ) { 
  7. foreach ( $connections as $c => $connection ) { 
  8. if ( $connection_id == $publicize->get_connection_id( $connection ) ) { 
  9. return WPCOM_JSON_API_Get_Connection_Endpoint::get_connection( $service, $connections[ $c ] ); 
  10.  
  11. return false; 
  12.  
  13. function get_connection( $service, $connection ) { 
  14. $publicize = new Publicize(); 
  15.  
  16. $connection_id = $publicize->get_connection_id( $connection ); 
  17. if ( method_exists( $connection, 'get_meta' ) ) { 
  18. $connection_meta = $connection->get_meta(); 
  19. $connection_data = (array) $connection->get_meta( 'connection_data' ); 
  20. } else { 
  21. $connection_meta = $connection; 
  22. $connection_data = $connection['connection_data']; 
  23.  
  24. return array( 
  25. 'ID' => (int) $connection_id,  
  26. 'token_ID' => (int) $connection_data['token_id'],  
  27. 'conn_ID' => (int) $connection_id,  
  28. 'site_ID' => (int) $connection_data['blog_id'],  
  29. 'user_ID' => (int) $connection_data['user_id'],  
  30. 'shared' => ( 0 == (int) $connection_data['user_id'] ) ? true : false,  
  31. 'service' => $service,  
  32. 'label' => $publicize->get_service_label( $service ),  
  33. 'issued' => $connection_meta['issued'],  
  34. 'expires' => $connection_meta['expires'],  
  35. 'external_ID' => $connection_meta['external_id'],  
  36. 'external_name' => $connection_meta['external_name'],  
  37. 'external_display' => $publicize->get_display_name( $service, $connection ),  
  38. 'URL' => $publicize->get_profile_link( $service, $connection ),  
  39. 'status' => ( method_exists( $connection, 'is_expired' ) && $connection->is_expired( HOUR_IN_SECONDS ) ) ? 'broken' : 'ok',  
  40. 'refresh_url' => $publicize->refresh_url( $service ),  
  41. 'meta' => maybe_unserialize( $connection_data['meta'] ),  
  42. ); 
  43.  
  44. // /sites/%s/connections/$connection_id 
  45. function callback( $path = '', $blog_id = 0, $connection_id = 0 ) { 
  46. // Verify required Publicize Jetpack module is active 
  47. if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) { 
  48. return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 ); 
  49.  
  50. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false ); 
  51. if ( is_wp_error( $blog_id ) ) { 
  52. return $blog_id; 
  53.  
  54. $current_user = wp_get_current_user(); 
  55. if ( ! $current_user->ID ) { 
  56. return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 ); 
  57.  
  58. // Attempt to find connection 
  59. $connection = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection_by_id( $connection_id ); 
  60.  
  61. // Verify that user has permission to view this connection 
  62. if ( $current_user->ID != $connection['user_ID'] && 0 != $connection['user_ID'] ) { 
  63. return new WP_Error( 'authorization_required', 'You do not have permission to access this resource.', 403 ); 
  64.  
  65. if ( empty( $connection ) ) { 
  66. return new WP_Error( 'unknown_connection', 'Connection not found.', 404 ); 
  67.  
  68. return $connection;