-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathURLValidationProvider.php
More file actions
158 lines (142 loc) · 3.64 KB
/
URLValidationProvider.php
File metadata and controls
158 lines (142 loc) · 3.64 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Provides URL validation.
*
* @package AMP
* @since 2.1
*/
namespace AmpProject\AmpWP\Validation;
use AmpProject\AmpWP\Infrastructure\Service;
use AMP_Validation_Error_Taxonomy;
use AMP_Validation_Manager;
use WP_Error;
/**
* URLValidationProvider class.
*
* @since 2.1
* @internal
*/
final class URLValidationProvider implements Service {
/**
* The total number of validation errors, regardless of whether they were accepted.
*
* @var int
*/
private $total_errors = 0;
/**
* The total number of unaccepted validation errors.
*
* If an error has been accepted in the /wp-admin validation UI,
* it won't count toward this.
*
* @var int
*/
private $unaccepted_errors = 0;
/**
* The number of URLs crawled, regardless of whether they have validation errors.
*
* @var int
*/
private $number_validated = 0;
/**
* The validation counts by type, like template or post type.
*
* @var array[] {
* Validity by type.
*
* @type array $type {
* @type int $valid The number of valid URLs for this type.
* @type int $total The total number of URLs for this type, valid or invalid.
* }
* }
*/
private $validity_by_type = [];
/**
* Provides the total number of validation errors found.
*
* @return int
*/
public function get_total_errors() {
return $this->total_errors;
}
/**
* Provides the total number of unaccepted errors.
*
* @return int
*/
public function get_unaccepted_errors() {
return $this->unaccepted_errors;
}
/**
* Provides the number of URLs that have been checked.
*
* @return int
*/
public function get_number_validated() {
return $this->number_validated;
}
/**
* Provides the validity counts by type.
*
* @return array[]
*/
public function get_validity_by_type() {
return $this->validity_by_type;
}
/**
* Validates a URL, stores the results, and increments the counts.
*
* @see AMP_Validation_Manager::validate_url_and_store()
*
* @param string $url The URL to validate.
* @param string $type The type of template, post, or taxonomy.
* @return array|WP_Error Associative array containing validity result or a WP_Error on failure.
*/
public function get_url_validation( $url, $type ) {
$validity = AMP_Validation_Manager::validate_url_and_store( $url );
if ( is_wp_error( $validity ) ) {
return $validity;
}
$this->update_state_from_validity( $validity, $type );
return $validity;
}
/**
* Increments crawl counts from a validation result.
*
* @param array $validity Validity results.
* @param string $type The URL type.
*/
private function update_state_from_validity( $validity, $type ) {
$validation_errors = wp_list_pluck( $validity['results'], 'error' );
$unaccepted_error_count = count(
array_filter(
$validation_errors,
static function( $error ) {
$validation_status = AMP_Validation_Error_Taxonomy::get_validation_error_sanitization( $error );
return (
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS !== $validation_status['term_status']
&&
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS !== $validation_status['term_status']
);
}
)
);
if ( count( $validation_errors ) > 0 ) {
$this->total_errors++;
}
if ( $unaccepted_error_count > 0 ) {
$this->unaccepted_errors++;
}
$this->number_validated++;
if ( ! isset( $this->validity_by_type[ $type ] ) ) {
$this->validity_by_type[ $type ] = [
'valid' => 0,
'total' => 0,
];
}
$this->validity_by_type[ $type ]['total']++;
if ( 0 === $unaccepted_error_count ) {
$this->validity_by_type[ $type ]['valid']++;
}
}
}