Skip to content

Commit

Permalink
Merge pull request #525 from equalizedigital/william/hotfix/frontend-…
Browse files Browse the repository at this point in the history
…highter-handle-filtered-rules

Solve some unexpected situations with frontend highlighter when rules are filtered off
  • Loading branch information
SteveJonesDev authored Mar 11, 2024
2 parents 34de85d + 6df7ece commit 428da3c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
4 changes: 2 additions & 2 deletions accessibility-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: Accessibility Checker
* Plugin URI: https://a11ychecker.com
* Description: Audit and check your website for accessibility before you hit publish. In-post accessibility scanner and guidance.
* Version: 1.9.1
* Version: 1.9.2
* Author: Equalize Digital
* Author URI: https://equalizedigital.com
* License: GPL-2.0+
Expand Down Expand Up @@ -41,7 +41,7 @@

// Current plugin version.
if ( ! defined( 'EDAC_VERSION' ) ) {
define( 'EDAC_VERSION', '1.9.1' );
define( 'EDAC_VERSION', '1.9.2' );
}

// Current database version.
Expand Down
14 changes: 11 additions & 3 deletions admin/class-frontend-highlight.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function get_issues( $post_id ) {
if ( ! $results ) {
return null;
}
return $results;

return Helpers::filter_results_to_only_active_rules( $results );
}

/**
Expand Down Expand Up @@ -76,8 +77,15 @@ public function ajax() {

$output = array();
foreach ( $results as $result ) {
$array = array();
$rule = edac_filter_by_value( $rules, 'slug', $result['rule'] );
$array = array();
$rule = edac_filter_by_value( $rules, 'slug', $result['rule'] );

// When rules are filtered out, they are not in the rules array and this can be empty. Skip when the rule
// is empty to avoid php warnings and passing null values to the frontend highlighter.
if ( ! $rule ) {
continue;
}

$rule_type = ( true === (bool) $result['ignre'] ) ? 'ignored' : $rule[0]['rule_type'];

$array['rule_type'] = $rule_type;
Expand Down
23 changes: 23 additions & 0 deletions admin/class-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,27 @@ public static function is_domain_loopback( $domain ) {

return false;
}

/**
* Filter out inactive rules from the results returned.
*
* @param array $results The results to filter.
*/
public static function filter_results_to_only_active_rules( $results ): array {
// determine which rules are active.
$active_rule_slugs = array_map(
function ( $rule ) {
return $rule['slug'];
},
edac_register_rules()
);

// filter out inactive rules from the results returned.
foreach ( $results as $index => $result ) {
if ( ! in_array( $result['rule'], $active_rule_slugs, true ) ) {
unset( $results[ $index ] );
}
}
return $results;
}
}
12 changes: 9 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
=== Equalize Digital Accessibility Checker - Audit Your Website for WCAG, ADA, and Section 508 Accessibility Errors ===
Contributors: equalizedigital, alh0319, stevejonesdev
Tags: accessibility, accessible, wcag, ada, WP accessibility, section 508, aoda, a11y, audit, readability, content analysis
Tags: accessibility, accessible, wcag, ada, WP accessibility
Requires at least: 6.2
Tested up to: 6.4.3
Stable tag: 1.9.1
Stable tag: 1.9.2
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html


Audit and check your website for accessibility before you hit publish. In-post accessibility scanner and guidance for WCAG compliance. No API or per page fees.
Ensure your site's accessibility with a pre-publish audit. Get in-post WCAG compliance help without API or per-page fees.

== Description ==

Expand Down Expand Up @@ -171,6 +171,12 @@ No, Accessibility Checker runs completely on your server and does not require yo

== Changelog ==

= 1.9.2 =
* Fixed: filtered rules are not passed to the frontend highlighter, avoiding 'null' state issues
* Updated: frontend highlighter buttons to be disabled until issues are confirmed
* Updated: frontend highlighter buttons to show only after we know there are issues to display
* Updated: frontend highlighter to not show buttons if none are returned

= 1.9.1 =
* Updated: `edac_include_rules_files to fire on init action to fix the `edac_filter_register_rules` filter timing

Expand Down
25 changes: 17 additions & 8 deletions src/frontendHighlighterApp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ class AccessibilityCheckerHighlight {
resolve( responseJson );
} else {
resolve(
responseJson.filter( ( item ) => ( item.id === self.urlParameter || item.rule_type !== 'ignored' ) )
responseJson.filter( ( item ) => {
// When rules are filtered off from php we can get null values for some properties
// here. This should be fixed upstream but handling it here as well for robustness.
if ( item.rule_type === null ) {
return false;
}

return ( item.id === self.urlParameter || item.rule_type !== 'ignored' );
} )
);
}
} else {
Expand Down Expand Up @@ -490,16 +498,13 @@ class AccessibilityCheckerHighlight {
this.panelControls.style.display = 'block';
this.panelToggle.style.display = 'none';

// previous and next buttons are disabled until we have issues to show.
this.nextButton.disabled = true;
this.previousButton.disabled = true;

// Get the issues for this page.
this.highlightAjax().then(
( json ) => {
if ( json.length === 0 ) {
this.nextButton.disabled = true;
this.previousButton.disabled = true;
} else {
this.nextButton.disabled = false;
this.previousButton.disabled = false;
}

this.issues = json;

Expand Down Expand Up @@ -782,6 +787,10 @@ class AccessibilityCheckerHighlight {
let textContent = 'No issues detected.';
if ( errorCount > 0 || warningCount > 0 || ignoredCount > 0 ) {
textContent = '';
// show buttons since we have issues.
this.nextButton.disabled = false;
this.previousButton.disabled = false;

if ( errorCount >= 0 ) {
textContent += errorCount + ' error' + ( errorCount === 1 ? '' : 's' ) + ', ';
}
Expand Down

0 comments on commit 428da3c

Please sign in to comment.