settings = $settings;
$this->is_pro = !empty($settings['pro_key']);
if (is_admin()) return;
// Images
if (!empty($settings['enable_images'])) {
add_filter('the_content', array($this, 'fix_image_alts'), 20);
add_filter('widget_text', array($this, 'fix_image_alts'), 20);
add_filter('post_thumbnail_html', array($this, 'fix_image_alts'), 20);
add_filter('wp_get_attachment_image_attributes', array($this, 'fix_attachment_alt'), 20, 2);
}
// Headings
if (!empty($settings['enable_headings'])) {
add_filter('the_content', array($this, 'fix_heading_hierarchy'), 15);
}
// Links
if (!empty($settings['enable_links'])) {
add_filter('the_content', array($this, 'fix_links'), 25);
add_filter('nav_menu_link_attributes', array($this, 'fix_nav_links'), 10, 4);
}
// Forms
if (!empty($settings['enable_forms'])) {
add_filter('the_content', array($this, 'fix_forms'), 25);
}
// Tables
if (!empty($settings['enable_tables'])) {
add_filter('the_content', array($this, 'fix_tables'), 25);
}
// ARIA landmarks
if (!empty($settings['enable_aria'])) {
add_action('wp_footer', array($this, 'inject_aria_js'), 5);
}
// Language
if (!empty($settings['enable_lang'])) {
add_filter('language_attributes', array($this, 'ensure_lang_attr'));
}
// Focus & Skip
if (!empty($settings['enable_focus'])) {
add_action('wp_enqueue_scripts', array($this, 'enqueue_focus_styles'));
add_action('wp_body_open', array($this, 'add_skip_link'));
add_filter('the_content', array($this, 'fix_interactive_elements'), 30);
}
// Contrast (Pro only)
if ($this->is_pro && !empty($settings['enable_contrast'])) {
add_action('wp_footer', array($this, 'inject_contrast_js'), 5);
}
// Frontend JS for dynamic content fixes
add_action('wp_footer', array($this, 'inject_frontend_js'), 99);
}
// =========================================================================
// IMAGE FIXES
// =========================================================================
/**
* Fix missing alt text in content with context-aware suggestions.
*/
public function fix_image_alts($content) {
if (is_admin()) return $content;
return preg_replace_callback('/
]*)>/i', function($matches) {
$attrs = $matches[1];
// Already has non-empty alt
if (preg_match('/\salt\s*=\s*["\']([^"\']+)["\']/', $attrs)) {
return $matches[0];
}
// Has alt="" with role=presentation (decorative) — keep it
if (preg_match('/\salt\s*=\s*["\']["\']/', $attrs) &&
preg_match('/role\s*=\s*["\']presentation["\']/', $attrs)) {
return $matches[0];
}
// Generate context-aware alt
$alt = $this->generate_smart_alt($attrs);
// Remove empty alt if present
$attrs = preg_replace('/\salt\s*=\s*["\']["\']/', '', $attrs);
return '
';
}, $content);
}
/**
* Fix alt text on attachment images.
*/
public function fix_attachment_alt($attr, $attachment) {
if (empty($attr['alt'])) {
$title = get_the_title($attachment->ID);
$attr['alt'] = $title ?: 'Image';
}
return $attr;
}
/**
* Generate smart alt text from filename and context.
*/
private function generate_smart_alt($attrs) {
// Try to get src
if (!preg_match('/src\s*=\s*["\']([^"\']+)["\']/', $attrs, $src_match)) {
return 'Image';
}
$src = $src_match[1];
$filename = pathinfo(basename($src), PATHINFO_FILENAME);
// Clean filename
$clean = preg_replace('/[-_]?\d+x\d+/', '', $filename); // Remove dimensions
$clean = preg_replace('/[-_][a-f0-9]{8,}$/', '', $clean); // Remove hashes
$clean = preg_replace('/[-_](?:scaled|rotated|cropped)/', '', $clean);
$clean = str_replace(array('-', '_'), ' ', $clean);
$clean = preg_replace('/\s+/', ' ', trim($clean));
$clean = ucwords(strtolower($clean));
// Try WordPress attachment
$attachment_id = attachment_url_to_postid($src);
if ($attachment_id) {
$stored_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if (!empty($stored_alt)) return $stored_alt;
$title = get_the_title($attachment_id);
if (!empty($title) && $title !== $filename) return $title;
$caption = wp_get_attachment_caption($attachment_id);
if (!empty($caption)) return $caption;
}
if (strlen($clean) >= 3) return $clean;
// Check class for context
if (preg_match('/class\s*=\s*["\']([^"\']+)["\']/', $attrs, $class_match)) {
$classes = $class_match[1];
if (strpos($classes, 'logo') !== false) return 'Site logo';
if (strpos($classes, 'avatar') !== false) return 'User avatar';
if (strpos($classes, 'icon') !== false) return 'Icon';
}
return 'Image';
}
// =========================================================================
// HEADING FIXES
// =========================================================================
/**
* Fix heading hierarchy — ensure no skipped levels.
*/
public function fix_heading_hierarchy($content) {
if (is_admin()) return $content;
$max_passes = 5; // Prevent infinite loops
$pass = 0;
do {
$changed = false;
$last_level = 1; // Theme provides H1
$pass++;
$content = preg_replace_callback('/]*)>(.*?)<\/h\1>/si',
function($matches) use (&$last_level, &$changed) {
$level = (int)$matches[1];
$attrs = $matches[2];
$inner = $matches[3];
if ($level > $last_level + 1) {
$new_level = $last_level + 1;
$changed = true;
$last_level = $new_level;
return "{$inner}";
}
$last_level = $level;
return $matches[0];
}, $content
);
} while ($changed && $pass < $max_passes);
return $content;
}
// =========================================================================
// LINK FIXES
// =========================================================================
/**
* Fix link accessibility issues.
*/
public function fix_links($content) {
if (is_admin()) return $content;
// Fix empty links
$content = preg_replace_callback('/]*)>(.*?)<\/a>/si', function($matches) {
$attrs = $matches[1];
$inner = $matches[2];
$text = trim(wp_strip_all_tags($inner));
// Already accessible
if (!empty($text) || preg_match('/aria-label\s*=/', $attrs)) {
return $matches[0];
}
// Image inside link — use img alt as label
if (preg_match('/
]*alt\s*=\s*["\']([^"\']+)["\']/', $inner, $alt_match)) {
return $matches[0]; // Image alt provides the name
}
// Generate label from href
$label = 'Link';
if (preg_match('/href\s*=\s*["\']([^"\']+)["\']/', $attrs, $href_match)) {
$href = $href_match[1];
$parsed = wp_parse_url($href);
$path = isset($parsed['path']) ? trim($parsed['path'], '/') : '';
if (!empty($path)) {
$label = ucwords(str_replace(array('-', '_', '/'), ' ', basename($path)));
} elseif (isset($parsed['host'])) {
$label = 'Visit ' . $parsed['host'];
}
}
return '' . $inner . '';
}, $content);
// Add rel="noopener noreferrer" to external target=_blank links
$content = preg_replace_callback('/]*target\s*=\s*["\']_blank["\'][^>]*)>/i', function($matches) {
$attrs = $matches[1];
if (preg_match('/rel\s*=.*noopener/', $attrs)) return $matches[0];
if (preg_match('/rel\s*=\s*["\']([^"\']*)["\']/', $attrs, $rel_match)) {
$new_rel = $rel_match[1] . ' noopener noreferrer';
$attrs = str_replace($rel_match[0], 'rel="' . trim($new_rel) . '"', $attrs);
} else {
$attrs .= ' rel="noopener noreferrer"';
}
return '';
}, $content);
return $content;
}
/**
* Fix navigation menu links.
*/
public function fix_nav_links($atts, $item, $args, $depth) {
// Add aria-current for current page
if (in_array('current-menu-item', $item->classes ?? array())) {
$atts['aria-current'] = 'page';
}
return $atts;
}
// =========================================================================
// FORM FIXES
// =========================================================================
/**
* Fix form elements without proper labels.
*/
public function fix_forms($content) {
if (is_admin()) return $content;
// Add aria-label to inputs without associated labels
$content = preg_replace_callback('/]*)>/i', function($matches) {
$attrs = $matches[1];
// Skip types that don't need labels
if (preg_match('/type\s*=\s*["\'](?:hidden|submit|button|image|reset)["\']/', $attrs)) {
return $matches[0];
}
// Already has label association
if (preg_match('/aria-label\s*=/', $attrs) || preg_match('/aria-labelledby\s*=/', $attrs)) {
return $matches[0];
}
// Generate label from placeholder, name, or type
$label = '';
if (preg_match('/placeholder\s*=\s*["\']([^"\']+)["\']/', $attrs, $m)) {
$label = $m[1];
} elseif (preg_match('/name\s*=\s*["\']([^"\']+)["\']/', $attrs, $m)) {
$label = ucwords(str_replace(array('-', '_', '[]'), array(' ', ' ', ''), $m[1]));
} elseif (preg_match('/type\s*=\s*["\']([^"\']+)["\']/', $attrs, $m)) {
$label = ucfirst($m[1]) . ' field';
} else {
$label = 'Input field';
}
return '';
}, $content);
// Fix selects
$content = preg_replace_callback('/