|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Site Health status checks for Accessibility Checker. |
| 4 | + * |
| 5 | + * @since 1.29.0 |
| 6 | + * @package Accessibility_Checker |
| 7 | + */ |
| 8 | + |
| 9 | +namespace EDAC\Admin\SiteHealth; |
| 10 | + |
| 11 | +use EDAC\Admin\Scans_Stats; |
| 12 | +use EDAC\Admin\Settings; |
| 13 | + |
| 14 | +/** |
| 15 | + * Adds Site Health tests for Accessibility Checker. |
| 16 | + * |
| 17 | + * @since 1.29.0 |
| 18 | + */ |
| 19 | +class Checks { |
| 20 | + /** |
| 21 | + * Cached scan stats for this request. |
| 22 | + * |
| 23 | + * @var array|null |
| 24 | + */ |
| 25 | + private ?array $stats = null; |
| 26 | + |
| 27 | + /** |
| 28 | + * Init hooks. |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function init_hooks() { |
| 33 | + add_filter( 'site_status_tests', [ $this, 'register_tests' ] ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Register our tests with Site Health. |
| 38 | + * |
| 39 | + * @param array $tests Existing tests. |
| 40 | + * @return array |
| 41 | + */ |
| 42 | + public function register_tests( array $tests ): array { |
| 43 | + $tests['direct']['edac_issues'] = [ |
| 44 | + 'label' => __( 'Accessibility issues', 'accessibility-checker' ), |
| 45 | + 'test' => [ $this, 'test_for_issues' ], |
| 46 | + ]; |
| 47 | + |
| 48 | + $tests['direct']['edac_scanned'] = [ |
| 49 | + 'label' => __( 'Content checked for accessibility', 'accessibility-checker' ), |
| 50 | + 'test' => [ $this, 'test_content_scanned' ], |
| 51 | + ]; |
| 52 | + |
| 53 | + $tests['direct']['edac_post_types'] = [ |
| 54 | + 'label' => __( 'Post types configured for accessibility checks', 'accessibility-checker' ), |
| 55 | + 'test' => [ $this, 'test_post_types_configured' ], |
| 56 | + ]; |
| 57 | + |
| 58 | + return $tests; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the badge for Accessibility Checker site health tests. |
| 63 | + * |
| 64 | + * @param string $color The color for the badge (e.g., 'blue', 'orange', 'red', 'green'). |
| 65 | + * @return array |
| 66 | + */ |
| 67 | + protected function get_accessibility_badge( string $color = 'blue' ): array { |
| 68 | + return [ |
| 69 | + 'label' => __( 'Accessibility', 'accessibility-checker' ), |
| 70 | + 'color' => $color, |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get scan stats, caching for this request. |
| 76 | + * |
| 77 | + * @return array |
| 78 | + */ |
| 79 | + private function get_stats(): array { |
| 80 | + if ( null === $this->stats ) { |
| 81 | + $this->stats = ( new Scans_Stats() )->summary(); |
| 82 | + } |
| 83 | + return $this->stats; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the appropriate URL for viewing issues based on available features. |
| 88 | + * |
| 89 | + * @return array Array with 'url' and 'text' keys. |
| 90 | + */ |
| 91 | + private function get_issues_link(): array { |
| 92 | + // Check if Pro version is available and has the issues page. |
| 93 | + if ( defined( 'EDACP_VERSION' ) && defined( 'EDAC_KEY_VALID' ) && EDAC_KEY_VALID ) { |
| 94 | + return [ |
| 95 | + 'url' => admin_url( 'admin.php?page=accessibility_checker_issues' ), |
| 96 | + 'text' => __( 'View Issues', 'accessibility-checker' ), |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + // Fallback to the main welcome page for free version. |
| 101 | + return [ |
| 102 | + 'url' => admin_url( 'admin.php?page=accessibility_checker' ), |
| 103 | + 'text' => __( 'View Accessibility Checker', 'accessibility-checker' ), |
| 104 | + ]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Test if there are accessibility issues on the site. |
| 109 | + * |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + public function test_for_issues(): array { |
| 113 | + $stats = $this->get_stats(); |
| 114 | + $errors = absint( $stats['errors'] ?? 0 ); |
| 115 | + $warnings = absint( $stats['warnings'] ?? 0 ); |
| 116 | + |
| 117 | + if ( $errors > 0 || $warnings > 0 ) { |
| 118 | + $issues_link = $this->get_issues_link(); |
| 119 | + |
| 120 | + return [ |
| 121 | + 'status' => 'recommended', |
| 122 | + 'label' => __( 'Accessibility issues detected', 'accessibility-checker' ), |
| 123 | + 'description' => sprintf( |
| 124 | + // translators: 1: error count, 2: warning count. |
| 125 | + __( 'Accessibility Checker has detected %1$s errors and %2$s warnings.', 'accessibility-checker' ), |
| 126 | + number_format_i18n( $errors ), |
| 127 | + number_format_i18n( $warnings ) |
| 128 | + ), |
| 129 | + 'actions' => sprintf( |
| 130 | + '<p><a href="%1$s" class="button button-primary">%2$s</a></p>', |
| 131 | + esc_url( $issues_link['url'] ), |
| 132 | + esc_html( $issues_link['text'] ) |
| 133 | + ), |
| 134 | + 'test' => 'edac_issues', |
| 135 | + 'badge' => $this->get_accessibility_badge(), |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + return [ |
| 140 | + 'status' => 'good', |
| 141 | + 'label' => __( 'No accessibility issues detected', 'accessibility-checker' ), |
| 142 | + 'description' => __( 'Accessibility Checker has not found any issues in scanned content.', 'accessibility-checker' ), |
| 143 | + 'test' => 'edac_issues', |
| 144 | + 'badge' => $this->get_accessibility_badge(), |
| 145 | + ]; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test if any posts have been scanned. |
| 150 | + * |
| 151 | + * @return array |
| 152 | + */ |
| 153 | + public function test_content_scanned(): array { |
| 154 | + $stats = $this->get_stats(); |
| 155 | + $scanned = absint( $stats['posts_scanned'] ?? 0 ); |
| 156 | + |
| 157 | + if ( 0 === $scanned ) { |
| 158 | + return [ |
| 159 | + 'status' => 'recommended', |
| 160 | + 'label' => __( 'Content has not been checked', 'accessibility-checker' ), |
| 161 | + 'description' => __( 'No posts have been scanned yet. Run a full site scan to begin checking your content for accessibility issues.', 'accessibility-checker' ), |
| 162 | + 'actions' => sprintf( |
| 163 | + '<p><a href="%1$s" class="button button-primary">%2$s</a></p>', |
| 164 | + esc_url( admin_url( 'admin.php?page=accessibility_checker_full_site_scan' ) ), |
| 165 | + esc_html__( 'Start full site scan', 'accessibility-checker' ) |
| 166 | + ), |
| 167 | + 'test' => 'edac_scanned', |
| 168 | + 'badge' => $this->get_accessibility_badge(), |
| 169 | + ]; |
| 170 | + } |
| 171 | + |
| 172 | + return [ |
| 173 | + 'status' => 'good', |
| 174 | + 'label' => __( 'Content is being checked for accessibility', 'accessibility-checker' ), |
| 175 | + 'description' => sprintf( |
| 176 | + // translators: %s is the number of posts scanned. |
| 177 | + _n( 'Accessibility Checker has scanned %s post for accessibility issues.', 'Accessibility Checker has scanned %s posts for accessibility issues.', $scanned, 'accessibility-checker' ), |
| 178 | + number_format_i18n( $scanned ) |
| 179 | + ), |
| 180 | + 'test' => 'edac_scanned', |
| 181 | + 'badge' => $this->get_accessibility_badge(), |
| 182 | + ]; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Test if post types are configured for scanning. |
| 187 | + * |
| 188 | + * @return array |
| 189 | + */ |
| 190 | + public function test_post_types_configured(): array { |
| 191 | + $scannable_post_types = Settings::get_scannable_post_types(); |
| 192 | + |
| 193 | + if ( empty( $scannable_post_types ) ) { |
| 194 | + return [ |
| 195 | + 'status' => 'critical', |
| 196 | + 'label' => __( 'No post types selected for accessibility checking', 'accessibility-checker' ), |
| 197 | + 'description' => __( 'Accessibility Checker cannot scan any content because no post types have been selected. Without configured post types, no accessibility issues will be detected.', 'accessibility-checker' ), |
| 198 | + 'actions' => sprintf( |
| 199 | + '<p><a href="%1$s" class="button button-primary">%2$s</a></p>', |
| 200 | + esc_url( admin_url( 'admin.php?page=accessibility_checker_settings' ) ), |
| 201 | + esc_html__( 'Configure post types', 'accessibility-checker' ) |
| 202 | + ), |
| 203 | + 'test' => 'edac_post_types', |
| 204 | + 'badge' => $this->get_accessibility_badge( 'red' ), |
| 205 | + ]; |
| 206 | + } |
| 207 | + |
| 208 | + $post_type_count = count( $scannable_post_types ); |
| 209 | + $post_type_names = implode( ', ', $scannable_post_types ); |
| 210 | + |
| 211 | + return [ |
| 212 | + 'status' => 'good', |
| 213 | + 'label' => __( 'Post types are configured for accessibility checking', 'accessibility-checker' ), |
| 214 | + 'description' => sprintf( |
| 215 | + // translators: 1: number of post types, 2: comma-separated list of post type names. |
| 216 | + _n( 'Accessibility Checker is configured to scan %1$s post type: %2$s.', 'Accessibility Checker is configured to scan %1$s post types: %2$s.', $post_type_count, 'accessibility-checker' ), |
| 217 | + number_format_i18n( $post_type_count ), |
| 218 | + $post_type_names |
| 219 | + ), |
| 220 | + 'test' => 'edac_post_types', |
| 221 | + 'badge' => $this->get_accessibility_badge(), |
| 222 | + ]; |
| 223 | + } |
| 224 | +} |
0 commit comments