-
Notifications
You must be signed in to change notification settings - Fork 1
/
wa11y.php
executable file
·491 lines (398 loc) · 12.3 KB
/
wa11y.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
* wA11y toolbox plugin for WordPress.
*
* Plugin Name: wA11y - The Web Accessibility Toolbox
* Plugin URI: https://wordpress.org/plugins/wa11y
* Description: A toolbox of resources to help you improve the accessibility of your WordPress website.
* Version: 1.0.3
* Author: Rachel Cherry
* Author URI: https://bamadesigner.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: wa11y
* Domain Path: /languages
*
* @package wA11y
* @link https://github.com/bamadesigner/wA11y
* @author Rachel Cherry
* @copyright 2016-2020 Rachel Cherry
* @license GPL v2 or later
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// We only need you in the admin.
if ( is_admin() ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/admin.php';
}
/**
* The class that powers general plugin functionality.
*
* Class Wa11y
* Package wA11y
*
* @since 1.0.0
*/
class Wa11y {
/**
* The current version of the plugin.
*
* @since 1.0.1
* @var string
*/
public $plugin_version = '1.0.3';
/**
* Whether or not this plugin is network active.
*
* @since 1.0.0
* @var boolean
*/
public $is_network_active;
/**
* The plugin settings.
*
* @since 1.0.0
* @var array
*/
protected $settings;
/**
* List of enabled tools.
*
* @since 1.0.0
* @var array
*/
protected $enabled_tools;
/**
* Will hold status of if
* we can load tools.
*
* @since 1.0.0
* @var array
*/
protected $can_load_tools;
/**
* Holds the class instance.
*
* @since 1.0.0
* @var Wa11y
*/
private static $instance;
/**
* Returns the instance of this class.
*
* @since 1.0.0
* @return Wa11y
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
/**
* Method to keep our instance from
* being cloned or unserialized.
*
* @since 1.0.0
* @return void
*/
private function __clone() {}
private function __wakeup() {}
/**
* Start your engines.
*
* @since 1.0.0
*/
protected function __construct() {
// Is this plugin network active?
$this->is_network_active = is_multisite() && ( $plugins = get_site_option( 'active_sitewide_plugins' ) ) && isset( $plugins['wA11y/wa11y.php'] );
// Load our text domain.
add_action( 'init', array( $this, 'textdomain' ) );
// Add items to the toolbar.
add_action( 'admin_bar_menu', array( $this, 'add_to_toolbar' ), 100 );
// Load front-end styles/scripts.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles_scripts' ) );
}
/**
* Internationalization FTW.
* Load our textdomain.
*
* @since 1.0.0
*/
public function textdomain() {
load_plugin_textdomain( 'wa11y', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Get wA11y settings.
*
* @since 1.0.0
* @filter 'wa11y_settings' - array with the settings.
* @param string - $tool_key - if set, will retrieve settings for a particular tool.
* @return array - the settings.
*/
public function get_settings( $tool_key = '' ) {
// If settings are already defined...
if ( isset( $this->settings ) ) {
// Return settings for a specific tool...
if ( ! empty( $tool_key ) ) {
if ( isset( $this->settings['tools'] ) && isset( $this->settings['tools'][ $tool_key ] ) ) {
return $this->settings['tools'][ $tool_key ];
}
// We don't have settings for this tool.
return array();
}
// Otherwise return everything.
return $this->settings;
}
// Define the default settings.
$default_settings = array(
'enable_tools' => array(),
'tools' => array(
'tota11y' => array(
'load_user_roles' => array( 'administrator' ),
'load_user_capability' => null,
'load_in_admin' => 0,
),
'wave' => array(
'load_user_roles' => array( 'administrator' ),
'load_user_capability' => null,
'load_admin_edit' => 0,
),
),
);
// Get the settings from the database and run through a filter.
$settings = apply_filters( 'wa11y_settings', get_option( 'wa11y_settings', $default_settings ) );
// Store the settings.
$this->settings = wp_parse_args( $settings, $default_settings );
// Return settings for a specific tool...
if ( ! empty( $tool_key ) ) {
if ( isset( $this->settings['tools'] ) && isset( $this->settings['tools'][ $tool_key ] ) ) {
return $this->settings['tools'][ $tool_key ];
}
// We don't have settings for this tool.
return array();
}
// Return the settings.
return $this->settings;
}
/**
* Returns array of enabled tools.
*
* @since 1.0.0
* @return array of enabled tools
*/
public function get_enabled_tools() {
// If already defined, get out of here.
if ( isset( $this->enabled_tools ) ) {
return $this->enabled_tools;
}
// Get our settings.
$settings = $this->get_settings();
// Get enabled tools.
if ( isset( $settings['enable_tools'] ) && ! empty( $settings['enable_tools'] ) ) {
// Make sure its an array.
if ( ! is_array( $settings['enable_tools'] ) ) {
$settings['enable_tools'] = explode( ', ', $settings['enable_tools'] );
}
// Return the enabled list.
return $this->enabled_tools = $settings['enable_tools'];
}
// We have no enabled tools.
return $this->enabled_tools = array();
}
/**
* Add items to the toolbar.
*
* @since 1.0.0
* @filter 'wa11y_wave_url' - string containing the WAVE evaluation URL. Passes the $post object if it exists.
* @param WP_Admin_Bar - $wp_admin_bar - WP_Admin_Bar instance, passed by reference.
*/
public function add_to_toolbar( $wp_admin_bar ) {
// Only need to worry about this stuff if we have enabled tools.
$enabled_tools = $this->get_enabled_tools();
if ( empty( $enabled_tools ) ) {
return;
}
// Will hold all of the wA11y nodes.
$wa11y_nodes = array();
// Process each enabled tool.
foreach ( $enabled_tools as $tool ) {
switch ( $tool ) {
case 'wave':
// Can we load WAVE?
if ( $this->can_load_wave() ) {
// Get WAVE settings.
$settings = wa11y()->get_settings( 'wave' );
// Make sure its OK to add to the toolbar.
if ( ! empty( $settings['load_in_toolbar'] ) && true == $settings['load_in_toolbar'] ) {
// Build the WAVE url.
$wave_url = get_permalink();
// Filter the WAVE url - includes $post object if it exists.
$wave_url = apply_filters( 'wa11y_wave_url', $wave_url, get_post() );
// Add WAVE node if we have a URL.
if ( ! empty( $wave_url ) ) {
$wa11y_nodes[] = array(
'id' => 'wa11y-wave',
'title' => sprintf( __( 'View %s evaluation', 'wa11y' ), 'WAVE' ),
'href' => 'http://wave.webaim.org/report#/' . urlencode( $wave_url ),
'meta' => array( 'target' => '_blank' ),
);
}
}
}
break;
}
}
// If we have nodes to add...
if ( ! empty( $wa11y_nodes ) ) {
// Add parent wA11y node.
$wp_admin_bar->add_node( array(
'id' => 'wa11y',
'title' => '<span aria-hidden="true">wA11y</span><span class="screen-reader-text">Wally</span>',
'parent' => false,
));
// Add child nodes.
foreach ( $wa11y_nodes as $node ) {
$wp_admin_bar->add_node( array_merge( $node, array( 'parent' => 'wa11y' ) ) );
}
}
}
/**
* Enqueue any styles/scripts needed for the front-end.
*
* Pretty much loading the scripts needed for our tools.
*
* @since 1.0.0
* @filter 'wa11y_load_tota11y' - boolean on whether or not to load the tota11y tool. Passes the tota11y settings.
*/
public function enqueue_styles_scripts() {
// If tota11y is enabled...
if ( wa11y()->can_load_tota11y() ) {
// This file belongs in the header.
wp_enqueue_script( 'tota11y', plugins_url( '/tools/tota11y/tota11y.min.js', __FILE__ ), array(), $this->plugin_version );
}
}
/**
* Checks whether or not tota11y is set to load.
*
* @since 1.0.0
* @filter 'wa11y_load_tota11y' - boolean on whether or not to load the tota11y tool. Passes the tota11y settings.
* @return boolean - true if we're set to load tota11y, otherwise false
*/
public function can_load_tota11y() {
// If already defined, get out of here.
if ( isset( $this->can_load_tools['tota11y'] ) ) {
return $this->can_load_tools['tota11y'];
}
// Get enabled tools.
$enabled_tools = $this->get_enabled_tools();
// If tota11y isn't enabled...
if ( empty( $enabled_tools ) || ! in_array( 'tota11y', $enabled_tools ) ) {
return $this->can_load_tools['tota11y'] = false;
}
// Get tota11y settings.
$settings = $this->get_settings( 'tota11y' );
// By default, only load tota11y if the user is logged in.
$load_tota11y = is_user_logged_in();
// If we're still passing tests, are we supposed to load tota11y in the admin?
if ( $load_tota11y && is_admin() ) {
$load_tota11y = isset( $settings['load_in_admin'] ) && $settings['load_in_admin'] > 0;
}
// If we're still passing tests, keep checking.
if ( $load_tota11y ) {
// If a set user role, then load tota11y.
if ( ! empty( $settings['load_user_roles'] ) ) {
$load_tota11y = $this->is_user_in_user_roles( $settings['load_user_roles'] );
}
// If user capability is set, turn off if not capable.
if ( ! empty( $settings['load_user_capability'] ) ) {
$load_tota11y = current_user_can( $settings['load_user_capability'] );
}
}
// Filter whether or not to load tota11y - passes the tota11y settings.
return $this->can_load_tools['tota11y'] = apply_filters( 'wa11y_load_tota11y', $load_tota11y, $settings );
}
/**
* Checks whether or not WAVE is set to load.
*
* @since 1.0.0
* @filter 'wa11y_load_wave' - boolean on whether or not to load the WAVE tool. Passes the WAVE settings.
* @return boolean - true if we're set to load WAVE, otherwise false
*/
public function can_load_wave() {
// If already defined, get out of here.
if ( isset( $this->can_load_tools['wave'] ) ) {
return $this->can_load_tools['wave'];
}
// Get enabled tools.
$enabled_tools = $this->get_enabled_tools();
// If WAVE isn't enabled...
if ( ! in_array( 'wave', $enabled_tools ) ) {
return $this->can_load_tools['wave'] = false;
}
// Get WAVE settings.
$settings = $this->get_settings( 'wave' );
// By default, only load WAVE if the user is logged in.
$load_wave = is_user_logged_in();
// Only for published posts.
if ( 'publish' != get_post_status() ) {
$load_wave = false;
}
// If we're still passing tests, keep checking.
if ( $load_wave ) {
// If a set user role, then load WAVE.
if ( ! empty( $settings['load_user_roles'] ) ) {
$load_wave = $this->is_user_in_user_roles( $settings['load_user_roles'] );
}
// If user capability is set, turn off if not capable.
if ( ! empty( $settings['load_user_capability'] ) ) {
$load_wave = current_user_can( $settings['load_user_capability'] );
}
}
// Filter whether or not to load WAVE - passes the WAVE settings.
return $this->can_load_tools['wave'] = apply_filters( 'wa11y_load_wave', $load_wave, $settings );
}
/**
* Tests if current user is one of the
* user roles passed to the function.
*
* @since 1.0.0
* @param array - $user_roles - an array of user roles to test against.
* @return boolean - true if user is one of passed user roles, false otherwise
*/
public function is_user_in_user_roles( $user_roles ) {
// Make sure we have user roles.
if ( empty( $user_roles ) ) {
return false;
}
// Get current user.
if ( ( $current_user = wp_get_current_user() )
&& ( $current_user_roles = isset( $current_user->roles ) ? $current_user->roles : false )
&& is_array( $current_user_roles ) ) {
// Find out if they share values.
$user_roles_intersect = array_intersect( $user_roles, $current_user_roles );
// If they intersect, user is in user roles so return true.
if ( ! empty( $user_roles_intersect ) ) {
return true;
}
}
return false;
}
}
/**
* Returns the instance of our main Wa11y class.
*
* Will come in handy when we need to access the
* class to retrieve data throughout the plugin.
*
* @since 1.0.0
* @return Wa11y
*/
function wa11y() {
return Wa11y::instance();
}
// Let's get this show on the road.
wa11y();