ApiActivateResponse.php
1.31 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
<?php
namespace ACP\Access\Rule;
use ACP;
use ACP\Access\Permissions;
use ACP\Access\Rule;
use WP_Error;
class ApiActivateResponse implements Rule {
/**
* @var ACP\API\Response
*/
protected $response;
public function __construct( ACP\API\Response $response ) {
$this->response = $response;
}
public function get_permissions() {
$permissions = new Permissions( $this->response->get( 'permissions' ) ?: [] );
if ( $this->response->has_error() ) {
$data = $this->response->get( 'data' );
if ( $data && is_array( $data['permissions'] ) ) {
$permissions = new Permissions( $data['permissions'] );
}
}
// `Usage` permissions are given when the API call fails.
if ( $this->response->has_error() && $this->has_http_error_code( $this->response->get_error() ) ) {
$permissions = $permissions->with_permission( Permissions::USAGE );
}
return $permissions;
}
/**
* @param WP_Error $error
*
* @return bool
* @see WP_Http
*/
private function has_http_error_code( WP_Error $error ): bool {
$http_error_codes = [
'http_failure', // no HTTP transports available
'http_request_not_executed', // User has blocked requests through HTTP
'http_request_failed', // any HTTP exceptions
];
return 0 !== count( array_intersect( $error->get_error_codes(), $http_error_codes ) );
}
}