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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307<?php
/**
* Name: A11yWatch
* Description: Quick crawl, sitemap, and single page scans.
*/
/**
* A11yWatch Fields
*/
function a11ywatch_fields()
{
$a11ywatch_fields = array(
// These fields are added to the database.
'db' => [
// Meta values.
'meta' => [
array(
'name' => 'a11ywatch_key',
'value' => '',
)
]
],
// These fields are HTML fields on the settings view.
'settings' => [
// Meta settings.
'meta' => [
array(
'name' => 'a11ywatch_key',
'label' => 'a11ywatch JWT (ie- 2fa31242wdda23efsdaf342)',
'type' => 'text',
)
]
]
);
// Return fields
return $a11ywatch_fields;
}
/**
* A11yWatch Tags
*/
function a11ywatch_tags()
{
// We don't know where helpers are being called, so we
// have to set the directory if it isn't already set.
if (!defined('__DIR__'))
define('__DIR__', dirname(dirname(__FILE__)));
// Read the JSON file - pulled from https://a11ywatch.webaim.org/api/docs?format=json
$a11ywatch_tag_json = file_get_contents(__DIR__ . '/a11ywatch_tags.json');
$a11ywatch_tags = json_decode($a11ywatch_tag_json, true);
// Convert a11ywatch format into Equalify format:
// tags [ array('slug' => $value, 'name' => $value, 'description' => $value) ]
$tags = array();
if (!empty($a11ywatch_tags)) {
foreach ($a11ywatch_tags as $a11ywatch_tag) {
// First, let's prepare the description, which is
// the summary and guidelines.
$description = '<p class="lead">' . $a11ywatch_tag['description'] . '</p>';
// Now lets put it all together into the Equalify format.
array_push(
$tags,
array(
'title' => $a11ywatch_tag['title'],
'category' => $a11ywatch_tag['category'],
'description' => $description,
'slug' => str_replace('.', '', $a11ywatch_tag['slug'])
)
);
}
}
// TODO: This is a temporary test of using error type as a tag
$tags[] = [
'title' => 'Error',
'category' => 'Error Level',
'description' => '',
'slug' => 'error',
];
$tags[] = [
'title' => 'Warning',
'category' => 'Error Level',
'description' => '',
'slug' => 'warning',
];
$tags[] = [
'title' => 'Notice',
'category' => 'Error Level',
'description' => '',
'slug' => 'notice',
];
// Return tags.
return $tags;
}
/**
* A11yWatch scan process request.
* Maps site URLs to A11yWatch API requests for processing.
*/
function a11ywatch_single_page_request($page_url)
{
// Lets specify everything Guzzle needs to create request.
$auth_token = DataAccess::get_meta_value('a11ywatch_key');
return [
'method' => 'POST',
'uri' => 'https://api.a11ywatch.com/api/scan',
'headers' => [
'Content-Type' => 'application/json',
'Transfer-Encoding' => 'chunked',
'Authorization' => $auth_token
],
'body' => json_encode(['url' => $page_url])
];
}
/**
* A11yWatch crawl process request.
* Maps site URLs to A11yWatch API requests for processing.
*/
function a11ywatch_crawl_request($page_url)
{
// Lets specify everything Guzzle needs to create request.
$auth_token = DataAccess::get_meta_value('a11ywatch_key');
return [
'method' => 'POST',
'uri' => 'https://api.a11ywatch.com/api/crawl',
'headers' => [
'Content-Type' => 'application/json',
'Transfer-Encoding' => 'chunked',
'Authorization' => $auth_token
],
'body' => json_encode([
'url' => $page_url,
"subdomains" => false,
"sitemap" => 0,
"tld" => false
]),
];
}
/**
* A11yWatch sitemap process request.
* Maps site URLs to A11yWatch API requests for processing.
*/
function a11ywatch_sitemap_request($page_url)
{
// Lets specify everything Guzzle needs to create request.
$auth_token = DataAccess::get_meta_value('a11ywatch_key');
return [
'method' => 'POST',
'uri' => 'https://api.a11ywatch.com/api/crawl',
'headers' => [
'Content-Type' => 'application/json',
'Transfer-Encoding' => 'chunked',
'Authorization' => $auth_token,
],
'body' => json_encode([
'url' => $page_url,
"subdomains" => false,
"sitemap" => 1,
"tld" => false
]),
];
}
/**
* A11yWatch Alerts
* @param string response_body
* @param string page_url
*/
function a11ywatch_single_page_alerts($response_body, $page_url)
{
// Our goal is to return alerts.
$a11ywatch_alerts = [];
// Decode JSON.
$scan_results = json_decode($response_body, true);
// Empty/unparsable response.
if (empty($scan_results)) {
// Add a fallback alert
$alert = [
'source' => 'a11ywatch',
'url' => $page_url,
'message' => 'a11ywatch returned an empty or unparsable response.',
];
$a11ywatch_alerts[] = $alert;
return $a11ywatch_alerts;
}
// Generate and return alerts
return alerts_from_a11ywatch_issues($scan_results, $page_url);
}
/**
* A11yWatch Crawl Alerts
* @param string response_body
* @param string page_url
*/
function a11ywatch_crawl_alerts($response_body, $page_url)
{
// Our goal is to return alerts.
$a11ywatch_alerts = [];
// Decode JSON.
$scan_results = json_decode($response_body, true);
// Empty/unparsable/non-array response.
if (empty($scan_results) || !is_array($scan_results)) {
$alert = [
'source' => 'a11ywatch',
'url' => $page_url,
'message' => 'a11ywatch returned an empty or unparsable response.',
];
$a11ywatch_alerts[] = $alert;
return $a11ywatch_alerts;
}
// Loop through response array to generate alerts.
// Expecting an entry for each page scanned.
foreach ($scan_results as $scan_result) {
$new_alerts = alerts_from_a11ywatch_issues($scan_result, $page_url);
$a11ywatch_alerts = array_merge($a11ywatch_alerts, $new_alerts);
}
return $a11ywatch_alerts;
}
/**
* A11yWatch Sitemap Alerts
* @param string response_body
* @param string page_url
*/
function a11ywatch_sitemap_alerts($response_body, $page_url)
{
// Same format as crawl results.
return a11ywatch_crawl_alerts($response_body, $page_url);
}
/**
* Helper method to map a11ywatch issues for a page to equalify alerts
* @param array scan_result
* @param string page_url
*/
function alerts_from_a11ywatch_issues($scan_result, $page_url)
{
// This array of alerts will be returned.
$alerts = [];
// Check that issues are where we expect in the response.
$response_data = $scan_result['data'] ?? [];
$url = $response_data['url'] ?? $page_url;
// Add a parsing error fallback alert.
$issues_found_in_response = array_key_exists('issues', $response_data);
if (!$issues_found_in_response) {
$alert = [
'source' => 'a11ywatch',
'url' => $url,
'message' => 'Could not parse a11ywatch response.',
'more_info' => json_encode($scan_result, JSON_PRETTY_PRINT),
];
$alerts[] = $alert;
return $alerts;
}
// Add all issues as alerts.
$a11ywatch_issues = $response_data['issues'];
foreach ($a11ywatch_issues as $issue) {
$alert = [
'source' => 'a11ywatch',
'url' => $url,
'tags' => $issue['type'], // type is error, warning, or notice
'message' => $issue['message'],
'more_info' => json_encode($issue, JSON_PRETTY_PRINT),
];
$alerts[] = $alert;
}
return $alerts;
}