-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDisconnectController.php
95 lines (83 loc) · 2.2 KB
/
DisconnectController.php
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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers;
use Automattic\WooCommerce\GoogleListingsAndAds\API\TransportMethods;
use WP_REST_Request as Request;
use WP_REST_Response as Response;
defined( 'ABSPATH' ) || exit;
/**
* Class DisconnectController
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers
*/
class DisconnectController extends BaseController {
use EmptySchemaPropertiesTrait;
/**
* Register rest routes with WordPress.
*/
public function register_routes() {
$this->register_route(
'connections',
[
[
'methods' => TransportMethods::DELETABLE,
'callback' => $this->get_disconnect_callback(),
'permission_callback' => $this->get_permission_callback(),
],
]
);
}
/**
* Get the callback for disconnecting all the services.
*
* @return callable
*/
protected function get_disconnect_callback(): callable {
return function( Request $request ) {
$endpoints = [
'ads/connection',
'mc/connection',
'google/connect',
'jetpack/connect',
];
$errors = [];
$responses = [];
foreach ( $endpoints as $endpoint ) {
$response = $this->get_delete_response( $endpoint );
if ( 200 !== $response->get_status() ) {
$errors[ $response->get_matched_route() ] = $response->get_data();
} else {
$responses[ $response->get_matched_route() ] = $response->get_data();
}
}
return new Response(
[
'errors' => $errors,
'responses' => $responses,
],
empty( $errors ) ? 200 : 400
);
};
}
/**
* Run a DELETE request for a given path, and return the response.
*
* @param string $path The relative API path. Based on the shared namespace.
*
* @return Response
*/
protected function get_delete_response( string $path ): Response {
$path = ltrim( $path, '/' );
return $this->server->dispatch_request( new Request( 'DELETE', "/{$this->get_namespace()}/{$path}" ) );
}
/**
* Get the item schema name for the controller.
*
* Used for building the API response schema.
*
* @return string
*/
protected function get_schema_title(): string {
return 'disconnect_all_accounts';
}
}