Skip to content

Commit

Permalink
Merge pull request #508 from equalizedigital/fix/edac-summary-class
Browse files Browse the repository at this point in the history
Created Summary Class
  • Loading branch information
SteveJonesDev authored Feb 22, 2024
2 parents 0394456 + df197e7 commit 2008b93
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 176 deletions.
162 changes: 0 additions & 162 deletions accessibility-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,165 +185,3 @@ function edac_include_rules_files() {
}
}
edac_include_rules_files();

/**
* Summary Data
*
* @param int $post_id ID of the post.
* @return array
*/
function edac_summary( $post_id ) {
global $wpdb;
$table_name = edac_get_valid_table_name( $wpdb->prefix . 'accessibility_checker' );
$summary = array();

// Check if table exists.
if ( ! $table_name ) {
return $summary;
}

// Passed Tests.
$rules = edac_register_rules();

// if ANWW is active remove link_blank for details meta box.
if ( EDAC_ANWW_ACTIVE ) {
$rules = edac_remove_element_with_value( $rules, 'slug', 'link_blank' );
}

$rules_passed = array();

if ( $rules ) {
foreach ( $rules as $rule ) {
$postid = $post_id;
$siteid = get_current_blog_id();

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation.
$rule_count = $wpdb->get_var(
$wpdb->prepare(
'SELECT count(*) FROM %i where rule = %s and siteid = %d and postid = %d and ignre = %d',
$table_name,
$rule['slug'],
$siteid,
$postid,
0
)
);

if ( ! $rule_count ) {
$rules_passed[] = $rule['slug'];
}
}
}

$summary['passed_tests'] = round( count( $rules_passed ) / count( $rules ) * 100 );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation.
$summary['errors'] = (int) $wpdb->get_var(
$wpdb->prepare(
'SELECT count(*) FROM %i where siteid = %d and postid = %d and ruletype = %s and ignre = %d',
$table_name,
get_current_blog_id(),
$post_id,
'error',
0
)
);

// count warnings.
$warnings_parameters = array( get_current_blog_id(), $post_id, 'warning', 0 );
$warnings_where = 'WHERE siteid = siteid = %d and postid = %d and ruletype = %s and ignre = %d';
if ( EDAC_ANWW_ACTIVE ) {
array_push( $warnings_parameters, 'link_blank' );
$warnings_where .= ' and rule != %s';
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation.
$summary['warnings'] = (int) $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
'SELECT count(*) FROM ' . $table_name . ' ' . $warnings_where,
$warnings_parameters
)
);

// count ignored issues.
$ignored_parameters = array( get_current_blog_id(), $post_id, 1 );
$ignored_where = 'WHERE siteid = %d and postid = %d and ignre = %d';
if ( EDAC_ANWW_ACTIVE ) {
array_push( $ignored_parameters, 'link_blank' );
$ignored_where .= ' and rule != %s';
}

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation.
$summary['ignored'] = (int) $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared , WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
"SELECT count(*) FROM $table_name $ignored_where",
$ignored_parameters
)
);

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation.
$summary['contrast_errors'] = (int) $wpdb->get_var(
$wpdb->prepare(
'SELECT count(*) FROM %i where siteid = %d and postid = %d and rule = %s and ignre = %d',
$table_name,
get_current_blog_id(),
$post_id,
'color_contrast_failure',
0
)
);

// remove color contrast from errors count.
$summary['errors'] = $summary['errors'] - $summary['contrast_errors'];

// issue density.
$issue_count = $summary['warnings'] + $summary['errors'] + $summary['contrast_errors'];

$issue_density_array = get_post_meta( $post_id, '_edac_density_data' );

if ( is_array( $issue_density_array ) &&
count( $issue_density_array ) > 0 &&
count( $issue_density_array[0] ) > 0
) {

$element_count = $issue_density_array[0][0];
$content_length = $issue_density_array[0][1];
$issue_density = edac_get_issue_density( $issue_count, $element_count, $content_length );

update_post_meta( $post_id, '_edac_issue_density', $issue_density );
} else {
delete_post_meta( $post_id, '_edac_issue_density' );
}

// reading grade level.
$content_post = get_post( $post_id );

$content = $content_post->post_content;
$content = wp_filter_nohtml_kses( $content );
$content = str_replace( ']]>', ']]>', $content );

$summary['content_grade'] = 0;
if ( class_exists( 'DaveChild\TextStatistics\TextStatistics' ) ) {
$summary['content_grade'] = floor(
( new DaveChild\TextStatistics\TextStatistics() )->fleschKincaidGradeLevel( $content )
);
}

$summary['readability'] = 0 === $summary['content_grade']
? 'N/A'
: edac_ordinal( $summary['content_grade'] );

// simplified summary.
$summary['simplified_summary'] = (bool) ( get_post_meta( $post_id, '_edac_simplified_summary', true ) );

// save summary data as post meta.
update_post_meta( $post_id, '_edac_summary', $summary );
update_post_meta( $post_id, '_edac_summary_passed_tests', $summary['passed_tests'] );
update_post_meta( $post_id, '_edac_summary_errors', $summary['errors'] );
update_post_meta( $post_id, '_edac_summary_warnings', $summary['warnings'] );
update_post_meta( $post_id, '_edac_summary_ignored', $summary['ignored'] );
update_post_meta( $post_id, '_edac_summary_contrast_errors', $summary['contrast_errors'] );

return $summary;
}
6 changes: 4 additions & 2 deletions admin/class-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace EDAC\Admin;

use EDAC\Inc\Summary_Generator;

/**
* Class that handles ajax requests.
*/
Expand Down Expand Up @@ -70,9 +72,9 @@ public function summary() {
$html['password_protected'] = $notice_text;
$html['content'] .= '<div class="edac-summary-notice">' . $notice_text . '</div>';
}

$post_id = (int) $_REQUEST['post_id'];
$summary = edac_summary( $post_id );
$summary = ( new Summary_Generator( $post_id ) )->generate_summary();
$simplified_summary_text = '';
$simplified_summary_prompt = get_option( 'edac_simplified_summary_prompt' );
$simplified_summary = get_post_meta( $post_id, '_edac_simplified_summary', true ) ? get_post_meta( $post_id, '_edac_simplified_summary', true ) : '';
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion includes/classes/class-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function set_post_scan_results( $request ) {
edac_remove_corrected_posts( $post_id, $post->post_type, $pre = 2, 'js' );

// Update the summary info that is stored in meta this post.
edac_summary( $post_id );
( new Summary_Generator( $post_id ) )->generate_summary();

// store a record of this scan in the post's meta.
update_post_meta( $post_id, '_edac_post_checked_js', time() );
Expand Down
Loading

0 comments on commit 2008b93

Please sign in to comment.