'fix-first', 'label' => __( 'Fix first', 'easyable' ), 'weight' => 3.0 ); case 'serious': return array( 'key' => 'important', 'label' => __( 'Important', 'easyable' ), 'weight' => 2.0 ); case 'minor': return array( 'key' => 'nice', 'label' => __( 'Nice to fix', 'easyable' ), 'weight' => 0.25 ); case 'moderate': default: return array( 'key' => 'should', 'label' => __( 'Should fix', 'easyable' ), 'weight' => 1.0 ); } } /** * Compute a 0–100 score from issue rows. * * @param array $issues Rows with an `impact` key. * @return int */ public static function compute_score( $issues ) { $penalty = 0.0; foreach ( (array) $issues as $issue ) { $bucket = self::bucket_for_impact( isset( $issue['impact'] ) ? $issue['impact'] : 'moderate' ); $penalty += $bucket['weight']; } $score = 100 - (int) round( $penalty ); return max( 0, min( 100, $score ) ); } /** * Plain status sentence for a score. * * @param int $score 0–100. * @return string */ public static function score_label( $score ) { if ( $score >= 90 ) { return __( 'Looking great', 'easyable' ); } if ( $score >= 70 ) { return __( 'Needs work', 'easyable' ); } if ( $score >= 40 ) { return __( 'Needs a lot of work', 'easyable' ); } return __( 'Needs urgent attention', 'easyable' ); } /** * Stable signature for one issue instance (rule + page + element). * * @param string $rule_id Rule id. * @param string $page_url Page URL. * @param string $target CSS target. * @return string */ public static function signature( $rule_id, $page_url, $target ) { return md5( $rule_id . '|' . $page_url . '|' . $target ); } /** * The public URLs to scan, from the saved content-type setting. * * @param int $limit Safety cap. Default 500. * @return array */ public static function collect_urls( $limit = 500 ) { $settings = get_option( 'accscan_settings', array() ); $types = isset( $settings['post_types'] ) && is_array( $settings['post_types'] ) ? $settings['post_types'] : array( 'page', 'post' ); $types = array_filter( $types, function ( $type ) { return post_type_exists( $type ); } ); $out = array(); $out[] = array( 'url' => home_url( '/' ), 'title' => __( 'Homepage', 'easyable' ), 'type' => 'home', ); if ( $types ) { // IDs only + no meta/term cache priming: collecting the URL // list must stay fast even on sites with hundreds of posts. $ids = get_posts( array( 'post_type' => array_values( $types ), 'post_status' => 'publish', 'posts_per_page' => $limit, 'orderby' => 'date', 'order' => 'DESC', 'fields' => 'ids', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); foreach ( $ids as $post_id ) { $url = get_permalink( $post_id ); if ( ! $url || $url === home_url( '/' ) ) { continue; } $out[] = array( 'url' => $url, 'title' => get_the_title( $post_id ), 'type' => get_post_type( $post_id ), ); } } return array_slice( $out, 0, $limit + 1 ); } /** * Ignored signatures. * * @return string[] */ public static function ignored_signatures() { $ignored = get_option( 'accscan_ignored', array() ); return is_array( $ignored ) ? $ignored : array(); } /** * Issues of the latest completed scan, with ignored ones flagged. * * @return array{scan:object|null,issues:array} */ public static function latest_results() { global $wpdb; $scans_table = $wpdb->prefix . 'accscan_scans'; $issues_table = $wpdb->prefix . 'accscan_issues'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table names are built from $wpdb->prefix only; the query has no user input. $scan = $wpdb->get_row( "SELECT * FROM $scans_table WHERE status = 'complete' ORDER BY id DESC LIMIT 1" ); if ( ! $scan ) { return array( 'scan' => null, 'issues' => array() ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name is built from $wpdb->prefix only; the scan id value is prepared. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $issues_table WHERE scan_id = %d ORDER BY id ASC", $scan->id ), ARRAY_A ); $ignored = self::ignored_signatures(); foreach ( $rows as &$row ) { $row['ignored'] = in_array( $row['signature'], $ignored, true ) ? 1 : 0; } return array( 'scan' => $scan, 'issues' => $rows ? $rows : array() ); } /** * Count issues per bucket, excluding ignored ones. * * @param array $issues Issue rows. * @return array{fix-first:int,important:int,should:int,nice:int,total:int} */ public static function bucket_counts( $issues ) { $counts = array( 'fix-first' => 0, 'important' => 0, 'should' => 0, 'nice' => 0, 'total' => 0 ); foreach ( (array) $issues as $issue ) { if ( ! empty( $issue['ignored'] ) ) { continue; } $bucket = self::bucket_for_impact( isset( $issue['impact'] ) ? $issue['impact'] : 'moderate' ); $counts[ $bucket['key'] ]++; $counts['total']++; } return $counts; } }