-
Notifications
You must be signed in to change notification settings - Fork 19
Add contrast information data collection and output for contrast failures #1589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
bfe3670
09db83d
65ed31f
1775768
821a948
4c200d2
3d5cdfb
c6dfad9
40a53b4
c081255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,11 +36,12 @@ class Insert_Rule_Data { | |
| * @param string|null $landmark The landmark type (main, header, footer, nav), optional. | ||
| * @param string|null $landmark_selector The landmark selector, optional. | ||
| * @param array $selectors An array of selectors that point to the object, optional. | ||
| * @param array|null $extra_data Optional extra data to store as JSON (e.g. color contrast values). | ||
| * | ||
| * @return void|int|\WP_Error The ID of the inserted record, void if no | ||
| * record was inserted or a WP_Error if the insert failed. | ||
| */ | ||
| public function insert( object $post, string $rule, string $ruletype, string $rule_obj, ?string $landmark = null, ?string $landmark_selector = null, array $selectors = [] ) { | ||
| public function insert( object $post, string $rule, string $ruletype, string $rule_obj, ?string $landmark = null, ?string $landmark_selector = null, array $selectors = [], ?array $extra_data = null ) { | ||
|
|
||
| if ( ! isset( $post->ID, $post->post_type ) | ||
| || empty( $rule ) | ||
|
|
@@ -66,6 +67,7 @@ public function insert( object $post, string $rule, string $ruletype, string $ru | |
| 'rule' => $rule, | ||
| 'ruletype' => $ruletype, | ||
| 'object' => esc_attr( $rule_obj ), | ||
| 'extra_data' => $extra_data ? wp_json_encode( $extra_data ) : null, | ||
| 'recordcheck' => 1, | ||
|
Comment on lines
67
to
71
|
||
| 'user' => get_current_user_id(), | ||
| 'ignre' => 0, | ||
|
|
@@ -111,7 +113,7 @@ public function insert( object $post, string $rule, string $ruletype, string $ru | |
| // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for adding data to database, caching not required for one time operation. | ||
| $wpdb->query( | ||
| $wpdb->prepare( | ||
| 'UPDATE %i SET recordcheck = %d, landmark = %s, landmark_selector = %s, object = %s, ancestry = %s, xpath = %s, ignre = %d WHERE siteid = %d and postid = %d and rule = %s and selector = %s and type = %s', | ||
| 'UPDATE %i SET recordcheck = %d, landmark = %s, landmark_selector = %s, object = %s, ancestry = %s, xpath = %s, ignre = %d, extra_data = %s WHERE siteid = %d and postid = %d and rule = %s and selector = %s and type = %s', | ||
| $table_name, | ||
| 1, | ||
| $rule_data['landmark'], | ||
|
|
@@ -120,6 +122,7 @@ public function insert( object $post, string $rule, string $ruletype, string $ru | |
| $rule_data['ancestry'], | ||
| $rule_data['xpath'], | ||
| $rule_data['ignre'], | ||
| $rule_data['extra_data'], | ||
| $rule_data['siteid'], | ||
| $rule_data['postid'], | ||
| $rule_data['rule'], | ||
|
|
@@ -160,6 +163,7 @@ public function insert( object $post, string $rule, string $ruletype, string $ru | |
| 'rule' => sanitize_text_field( $rule_data['rule'] ), | ||
| 'ruletype' => sanitize_text_field( $rule_data['ruletype'] ), | ||
| 'object' => esc_attr( $rule_data['object'] ), | ||
| 'extra_data' => isset( $rule_data['extra_data'] ) ? sanitize_text_field( $rule_data['extra_data'] ) : null, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using A more secure and reliable method would be to decode the JSON string, sanitize each value within the resulting array using appropriate functions (e.g., |
||
| 'recordcheck' => absint( $rule_data['recordcheck'] ), | ||
| 'user' => absint( $rule_data['user'] ), | ||
| 'ignre' => absint( $rule_data['ignre'] ), | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -536,7 +536,10 @@ public function set_post_scan_results( $request ) { | |||||||||||||||||||||||
| 'ancestry' => $violation['ancestry'] ?? [], | ||||||||||||||||||||||||
| 'xpath' => $violation['xpath'] ?? [], | ||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||
| ( new Insert_Rule_Data() )->insert( $post, $actual_rule_id, $impact, $html, $landmark, $landmark_selector, $selectors ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| $extra_data = isset( $violation['extraData'] ) && is_array( $violation['extraData'] ) ? $violation['extraData'] : null; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ( new Insert_Rule_Data() )->insert( $post, $actual_rule_id, $impact, $html, $landmark, $landmark_selector, $selectors, $extra_data ); | ||||||||||||||||||||||||
|
Comment on lines
+539
to
+542
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Fires after a rule is run against the content. | ||||||||||||||||||||||||
|
|
@@ -992,6 +995,9 @@ private function process_rules_for_details( $rules, $post_id, $table_name, $site | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| $result['ignre_user_name'] = $user_cache[ $user_id ]; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| if ( isset( $result['extra_data'] ) && null !== $result['extra_data'] ) { | ||||||||||||||||||||||||
| $result['extra_data'] = json_decode( $result['extra_data'], true ); | ||||||||||||||||||||||||
|
Comment on lines
+998
to
+999
|
||||||||||||||||||||||||
| if ( isset( $result['extra_data'] ) && null !== $result['extra_data'] ) { | |
| $result['extra_data'] = json_decode( $result['extra_data'], true ); | |
| if ( isset( $result['extra_data'] ) && '' !== $result['extra_data'] && null !== $result['extra_data'] ) { | |
| $decoded_extra_data = json_decode( $result['extra_data'], true ); | |
| if ( JSON_ERROR_NONE === json_last_error() && is_array( $decoded_extra_data ) ) { | |
| $result['extra_data'] = $decoded_extra_data; | |
| } else { | |
| $result['extra_data'] = null; | |
| } | |
| } else { | |
| $result['extra_data'] = null; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -777,6 +777,28 @@ class AccessibilityCheckerHighlight { | |
| </div>`; | ||
| } | ||
|
|
||
| // Color contrast data | ||
| if ( matchingObj.extra_data?.fgColor && matchingObj.extra_data?.bgColor ) { | ||
| const { fgColor, bgColor, contrastRatio, expectedContrastRatio } = matchingObj.extra_data; | ||
| content += ` | ||
| <div class="edac-highlight-panel-description-contrast"> | ||
| <div class="edac-highlight-panel-description-contrast-swatches"> | ||
| <div class="edac-highlight-panel-description-contrast-swatch"> | ||
| <div class="edac-highlight-panel-description-contrast-swatch-color" style="background-color: ${ fgColor }; color: ${ bgColor };">Aa</div> | ||
| <div class="edac-highlight-panel-description-contrast-swatch-label">${ __( 'Foreground', 'accessibility-checker' ) }<br>${ fgColor }</div> | ||
| </div> | ||
| <div class="edac-highlight-panel-description-contrast-swatch"> | ||
| <div class="edac-highlight-panel-description-contrast-swatch-color" style="background-color: ${ bgColor }; color: ${ fgColor };">Aa</div> | ||
| <div class="edac-highlight-panel-description-contrast-swatch-label">${ __( 'Background', 'accessibility-checker' ) }<br>${ bgColor }</div> | ||
| </div> | ||
| </div> | ||
| <div class="edac-highlight-panel-description-contrast-ratio"> | ||
| ${ __( 'Contrast ratio:', 'accessibility-checker' ) } <strong>${ contrastRatio }:1</strong> (${ __( 'required:', 'accessibility-checker' ) } ${ expectedContrastRatio }) | ||
| </div> | ||
| </div> | ||
| `; | ||
|
Comment on lines
+780
to
+799
|
||
| } | ||
|
|
||
| if ( this.fixes[ matchingObj.slug ] && window.edacFrontendHighlighterApp?.userCanFix ) { | ||
| // this is the markup to put in the modal. | ||
| content += ` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra_datais decoded from JSON without checking for decode errors. If the DB contains malformed JSON, this will quietly setextra_datatonulland can make frontend behavior inconsistent. Consider verifyingjson_last_error()(or using a helper) and only returning decoded data when valid.