-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathValidationCountsRestController.php
More file actions
140 lines (126 loc) · 3.87 KB
/
ValidationCountsRestController.php
File metadata and controls
140 lines (126 loc) · 3.87 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* ValidationCountsRestController class.
*
* @package AmpProject\AmpWP
* @since 2.1
*/
namespace AmpProject\AmpWP\Validation;
use AMP_Validated_URL_Post_Type;
use AMP_Validation_Error_Taxonomy;
use AmpProject\AmpWP\DevTools\UserAccess;
use WP_Error;
use WP_REST_Controller;
use AmpProject\AmpWP\Infrastructure\Delayed;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
/**
* Rest endpoint for fetching the total count of unreviewed validation URLs and validation errors.
*
* @since 2.1
* @internal
*/
final class ValidationCountsRestController extends WP_REST_Controller implements Delayed, Service, Registerable {
/** @var UserAccess DevTools User Access */
private $dev_tools_user_access;
/**
* Get the action to use for registering the service.
*
* @return string Registration action to use.
*/
public static function get_registration_action() {
return 'rest_api_init';
}
/**
* Constructor.
*
* @param UserAccess $user_access Instance of UserAccess class.
*/
public function __construct( UserAccess $user_access ) {
$this->namespace = 'amp/v1';
$this->rest_base = 'unreviewed-validation-counts';
$this->dev_tools_user_access = $user_access;
}
/**
* Registers all routes for the controller.
*/
public function register() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'args' => [],
'permission_callback' => [ $this, 'get_items_permissions_check' ],
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
}
/**
* Checks whether the current user has access to Dev Tools.
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has permission; WP_Error object otherwise.
*/
public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$dev_tools_access = $this->dev_tools_user_access->is_user_enabled();
if ( ! $dev_tools_access ) {
return new WP_Error(
'amp_rest_no_dev_tools_access',
__( 'Sorry, you are not allowed to view unreviewed counts for validation errors.', 'amp' ),
[ 'status' => rest_authorization_required_code() ]
);
}
return true;
}
/**
* Retrieves total unreviewed count for validation URLs and errors.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$unreviewed_validated_url_count = AMP_Validated_URL_Post_Type::get_validation_error_urls_count();
$unreviewed_validation_error_count = AMP_Validation_Error_Taxonomy::get_validation_error_count(
[
'group' => [
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS,
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS,
],
]
);
$response = [
'validated_urls' => $unreviewed_validated_url_count,
'errors' => $unreviewed_validation_error_count,
];
return rest_ensure_response( $response );
}
/**
* Retrieves the block type' schema, conforming to JSON Schema.
*
* @return array Item schema data.
*/
public function get_item_schema() {
return [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'amp-wp-validation-status',
'type' => 'object',
'properties' => [
'validation_urls' => [
'type' => 'integer',
'readonly' => true,
],
'errors' => [
'type' => 'integer',
'readonly' => true,
],
],
];
}
}