-
Notifications
You must be signed in to change notification settings - Fork 0
/
a11y-tester.php
89 lines (75 loc) · 2.81 KB
/
a11y-tester.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
<?php
/**
* Plugin Name: A11y Tester
* Description: A plugin to test accessibility of any page or post.
* Version: 1.0.9
* Author: Joe Peterson
* Author URI: https://joepeterson.work
*/
function enqueue_a11y_scripts($hook)
{
if ('post.php' === $hook || 'post-new.php' === $hook) {
// axe-core is omitted as it will be loaded dynamically into the iframe
wp_enqueue_script('a11y-init', plugin_dir_url(__FILE__) . 'a11y-init.js', array('jquery'), '1.0', true);
$nonce = wp_create_nonce('a11y_nonce');
wp_localize_script('a11y-init', 'wpData', array('ajax_url' => admin_url('admin-ajax.php'), 'nonce' => $nonce));
$theme_css_path = get_stylesheet_directory() . '/a11y-tester/a11y-styles.css';
$theme_css_url = get_stylesheet_directory_uri() . '/a11y-tester/a11y-styles.css';
$plugin_css_url = plugin_dir_url(__FILE__) . 'a11y-styles.css';
if (file_exists($theme_css_path)) {
wp_enqueue_style('a11y-style', $theme_css_url, array(), '1.0');
} else {
wp_enqueue_style('a11y-style', $plugin_css_url, array(), '1.0');
}
}
}
add_action('admin_enqueue_scripts', 'enqueue_a11y_scripts');
function add_a11y_meta_box()
{
$args = array(
'public' => true,
);
$post_types = get_post_types($args);
foreach ($post_types as $post_type) {
add_meta_box('a11y_meta_box', 'Accessibility Tester', 'a11y_meta_box_content', $post_type, 'normal', 'high');
}
}
add_action('add_meta_boxes', 'add_a11y_meta_box');
function a11y_meta_box_content()
{
echo '<div class="inside"></div>';
}
add_action('wp_ajax_run_a11y_test', 'run_a11y_test_function');
function run_a11y_test_function()
{
check_ajax_referer('a11y_nonce', 'security');
if (!current_user_can('edit_posts')) {
wp_send_json_error('You do not have the necessary permissions.');
wp_die();
}
$post_id = intval($_POST['post_id']);
if ($post_id <= 0) {
wp_send_json_error('Invalid post ID');
wp_die();
}
$post_id = absint($post_id);
$url = get_permalink($post_id);
if (!$url) {
wp_send_json_error('Could not get the permalink');
wp_die();
}
wp_send_json_success(array('url' => $url));
wp_die();
}
function a11y_custom_plugin_links($links, $file)
{
if (plugin_basename(__FILE__) === $file) {
$row_meta = array(
'source' => '<a href="https://github.com/skullzarmy/a11y-tester-wordpress-plugin" target="_blank" rel="nofollow noopener">Source Code</a>',
'support' => '<a href="https://github.com/skullzarmy/a11y-tester-wordpress-plugin/issues" target="_blank" rel="nofollow noopener">Support</a>',
);
return array_merge($links, $row_meta);
}
return (array) $links;
}
add_filter('plugin_row_meta', 'a11y_custom_plugin_links', 10, 2);