WPCOM_JSON_API_Delete_Connection_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Delete 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_Delete_Connection_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. // /sites/%s/connections/$connection_id/delete 
  3. function callback( $path = '', $blog_id = 0 , $connection_id = 0 ) { 
  4. // Verify required Publicize Jetpack module is active 
  5. if ( ! class_exists( 'Publicize' ) || ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'publicize' ) ) ) { 
  6. return new WP_Error( 'missing_jetpack_module', 'The Publicize module must be activated in order to use this endpoint.', 400 ); 
  7.  
  8. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false ); 
  9. if ( is_wp_error( $blog_id ) ) { 
  10. return $blog_id; 
  11.  
  12. $current_user = wp_get_current_user(); 
  13. if ( ! $current_user->ID ) { 
  14. return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current user.', 403 ); 
  15.  
  16. // Attempt to find connection 
  17. $connection = WPCOM_JSON_API_Get_Connection_Endpoint::get_connection_by_id( $connection_id ); 
  18.  
  19. if ( empty( $connection ) ) { 
  20. return new WP_Error( 'unknown_connection', 'Connection not found.', 404 ); 
  21.  
  22. // Verify that user has permission to view this connection 
  23. if ( $current_user->ID != $connection['user_ID'] && 0 != $connection['user_ID'] ) { 
  24. return new WP_Error( 'authorization_required', 'You do not have permission to access this resource.', 403 ); 
  25.  
  26. // Remove publicize connections related to the connection 
  27. $publicize = new Publicize(); 
  28. $is_deleted = ( false !== $publicize->disconnect( $connection['service'], $connection_id ) ); 
  29.  
  30. if ( $is_deleted ) { 
  31. /** 
  32. * Fires when a Publicize connection is deleted. 
  33. * @module json-api 
  34. * @since 3.2.0 
  35. * @param int $connection_id Publicize connection ID. 
  36. */ 
  37. do_action( 'rest_api_delete_publicize_connection', $connection_id ); 
  38.  
  39. return array( 
  40. 'ID' => (int) $connection_id,  
  41. 'deleted' => $is_deleted 
  42. );