results = array( 'images' => $this->scan_images(), 'headings' => $this->scan_headings(), 'links' => $this->scan_links(), 'forms' => $this->scan_forms(), 'tables' => $this->scan_tables(), 'aria' => $this->scan_aria(), 'lang' => $this->scan_lang(), 'focus' => $this->scan_focus(), 'contrast' => $this->scan_contrast(), ); return $this->results; } /** * Calculate compliance score (0-100). * Score is based on: is the fix enabled AND are there remaining issues? * Both conditions must be met for full points. */ public function calculate_score($results = null) { if (!$results) $results = $this->results; $weights = array( 'images' => 15, 'headings' => 12, 'links' => 12, 'forms' => 12, 'tables' => 5, 'aria' => 10, 'lang' => 5, 'focus' => 10, 'contrast' => 12, 'badge' => 7, ); $score = 0; $settings = get_option('sitefix_settings', array()); foreach ($weights as $key => $weight) { $enabled_key = 'enable_' . $key; $is_enabled = !empty($settings[$enabled_key]); // For badge, just check if enabled if ($key === 'badge') { if ($is_enabled) $score += $weight; continue; } $issues = isset($results[$key]) ? $results[$key]['count'] : 0; if ($is_enabled && $issues === 0) { // Fix enabled AND no issues = full points $score += $weight; } elseif ($is_enabled && $issues > 0) { // Fix enabled but issues remain = partial (fix is helping) $score += $weight * 0.6; } elseif (!$is_enabled && $issues === 0) { // Not enabled but no issues found = partial (naturally ok) $score += $weight * 0.4; } elseif (!$is_enabled && $issues <= 3) { // Not enabled, few issues = small credit $score += $weight * 0.2; } // Not enabled + many issues = 0 points } return min(100, max(0, round($score))); } /** * Get all published posts/pages/products. */ private function get_content_posts($limit = 100) { return get_posts(array( 'post_type' => array('post', 'page', 'product'), 'posts_per_page' => $limit, 'post_status' => 'publish', )); } /** * Scan for missing alt text on images. */ public function scan_images() { $issues = array('count' => 0, 'details' => array()); $posts = $this->get_content_posts(); foreach ($posts as $post) { $content = $post->post_content; // Find images without alt or with empty alt if (preg_match_all('/]*)>/i', $content, $matches)) { foreach ($matches[1] as $i => $attrs) { $has_alt = preg_match('/\salt\s*=\s*["\']([^"\']*)["\']/', $attrs, $alt_match); $alt_value = $has_alt ? $alt_match[1] : ''; // Skip decorative images (alt="") if ($has_alt && $alt_value === '' && preg_match('/role\s*=\s*["\']presentation["\']/', $attrs)) { continue; } if (!$has_alt || trim($alt_value) === '') { $src = ''; if (preg_match('/src\s*=\s*["\']([^"\']+)["\']/', $attrs, $src_match)) { $src = $src_match[1]; } $issues['details'][] = array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'type' => 'missing_alt', 'src' => $src, 'suggestion' => $this->generate_alt_suggestion($src, $post), ); $issues['count']++; } } } // Check featured image $thumb_id = get_post_thumbnail_id($post->ID); if ($thumb_id) { $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true); if (empty($alt)) { $issues['details'][] = array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'type' => 'featured_missing_alt', 'src' => wp_get_attachment_url($thumb_id), 'suggestion' => 'Featured image for ' . $post->post_title, ); $issues['count']++; } } } return $issues; } /** * Generate context-aware alt text suggestion. */ private function generate_alt_suggestion($src, $post) { if (empty($src)) return 'Image in ' . $post->post_title; $filename = pathinfo(basename($src), PATHINFO_FILENAME); // Clean up filename: remove dimensions, hashes, dashes $clean = preg_replace('/[-_]?\d+x\d+/', '', $filename); $clean = preg_replace('/[-_][a-f0-9]{8,}$/', '', $clean); $clean = str_replace(array('-', '_'), ' ', $clean); $clean = ucfirst(trim($clean)); if (strlen($clean) < 3) { return 'Image in ' . $post->post_title; } return $clean; } /** * Scan for heading hierarchy issues. */ public function scan_headings() { $issues = array('count' => 0, 'details' => array()); $posts = $this->get_content_posts(); foreach ($posts as $post) { $content = $post->post_content; if (preg_match_all('/]*>(.*?)<\/h\1>/si', $content, $matches)) { $levels = array_map('intval', $matches[1]); $last = 1; // Assume H1 exists in theme foreach ($levels as $idx => $level) { if ($level > $last + 1) { $heading_text = wp_strip_all_tags($matches[2][$idx]); $issues['details'][] = array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'type' => 'skipped_level', 'current' => $level, 'expected_max' => $last + 1, 'heading_text' => mb_substr($heading_text, 0, 50), ); $issues['count']++; } $last = $level; } // Check for multiple H1s $h1_count = count(array_filter($levels, function($l) { return $l === 1; })); if ($h1_count > 1) { $issues['details'][] = array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'type' => 'multiple_h1', 'count' => $h1_count, ); $issues['count']++; } } } return $issues; } /** * Scan for link accessibility issues. */ public function scan_links() { $issues = array('count' => 0, 'details' => array()); $posts = $this->get_content_posts(); foreach ($posts as $post) { $content = $post->post_content; // Empty links (no text, no aria-label, no img with alt) if (preg_match_all('/]*)>(.*?)<\/a>/si', $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $m) { $attrs = $m[1]; $inner = $m[2]; $text = trim(wp_strip_all_tags($inner)); $has_label = preg_match('/aria-label\s*=/', $attrs); $has_img_alt = preg_match('/]*alt\s*=\s*["\'][^"\']+["\']/', $inner); $has_title = preg_match('/title\s*=\s*["\'][^"\']+["\']/', $attrs); if (empty($text) && !$has_label && !$has_img_alt && !$has_title) { $href = ''; if (preg_match('/href\s*=\s*["\']([^"\']+)["\']/', $attrs, $href_match)) { $href = $href_match[1]; } $issues['details'][] = array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'type' => 'empty_link', 'href' => $href, ); $issues['count']++; } // Generic link text if (in_array(strtolower($text), array('click here', 'read more', 'here', 'more', 'link'))) { $issues['details'][] = array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'type' => 'generic_text', 'text' => $text, ); $issues['count']++; } // External links without indication if (preg_match('/href\s*=\s*["\']https?:\/\//', $attrs) && !preg_match('/href\s*=\s*["\']' . preg_quote(home_url(), '/') . '/', $attrs) && preg_match('/target\s*=\s*["\']_blank["\']/', $attrs) && !preg_match('/rel\s*=.*noopener/', $attrs)) { $issues['details'][] = array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'type' => 'external_no_rel', ); $issues['count']++; } } } } return $issues; } /** * Scan for form accessibility issues. */ public function scan_forms() { $issues = array('count' => 0, 'details' => array()); $posts = $this->get_content_posts(); foreach ($posts as $post) { $content = $post->post_content; // Find inputs without labels if (preg_match_all('/]*)>/i', $content, $matches)) { foreach ($matches[1] as $attrs) { // Skip hidden, submit, button types if (preg_match('/type\s*=\s*["\'](?:hidden|submit|button|image|reset)["\']/', $attrs)) continue; $has_id = preg_match('/id\s*=\s*["\']([^"\']+)["\']/', $attrs, $id_match); $has_label = preg_match('/aria-label\s*=/', $attrs) || preg_match('/aria-labelledby\s*=/', $attrs); $has_placeholder = preg_match('/placeholder\s*=/', $attrs); if (!$has_label && $has_id) { // Check if there's a