calculate_score(); wp_send_json_success( $results ); } /** * Calculate the performance score. * * @return array */ public function calculate_score() { $score = 0; $max_score = 100; $details = array(); // Optimization toggles points (up to 40 points). $optimizations = array( 'asmith_so_lazy_load' => 8, 'asmith_so_minify_html' => 7, 'asmith_so_defer_js' => 7, ); $active_optimizations = 0; foreach ( $optimizations as $option => $points ) { if ( get_option( $option, false ) ) { $score += $points; $active_optimizations++; } } $details['active_optimizations'] = $active_optimizations; $details['total_optimizations'] = count( $optimizations ); // Bloat removal points (up to 30 points, 3 per item). $bloat_items = array( 'asmith_so_remove_emoji', 'asmith_so_remove_embeds', 'asmith_so_remove_query_strings', 'asmith_so_remove_dashicons', 'asmith_so_remove_xmlrpc', 'asmith_so_remove_rest_links', 'asmith_so_remove_shortlinks', 'asmith_so_remove_wlw', 'asmith_so_remove_rsd', 'asmith_so_remove_rss', ); $active_bloat_removals = 0; foreach ( $bloat_items as $item ) { if ( get_option( $item, false ) ) { $score += 3; $active_bloat_removals++; } } $details['bloat_items_removed'] = $active_bloat_removals; $details['total_bloat_items'] = count( $bloat_items ); // Asset count scoring (up to 20 points). $asset_counts = get_transient( 'asmith_so_asset_counts' ); $scripts_count = isset( $asset_counts['scripts'] ) ? (int) $asset_counts['scripts'] : 0; $styles_count = isset( $asset_counts['styles'] ) ? (int) $asset_counts['styles'] : 0; // Fewer assets = higher score. if ( $scripts_count <= 10 ) { $score += 10; } elseif ( $scripts_count <= 20 ) { $score += 7; } elseif ( $scripts_count <= 30 ) { $score += 4; } else { $score += 1; } if ( $styles_count <= 5 ) { $score += 10; } elseif ( $styles_count <= 10 ) { $score += 7; } elseif ( $styles_count <= 20 ) { $score += 4; } else { $score += 1; } $details['scripts_count'] = $scripts_count; $details['styles_count'] = $styles_count; // Cap score at max. $score = min( $score, $max_score ); // Calculate grade. $grade = $this->score_to_grade( $score ); // Build recommendations. $recommendations = $this->build_recommendations( $details ); $results = array( 'score' => $score, 'grade' => $grade, 'details' => $details, 'recommendations' => $recommendations, 'timestamp' => time(), ); // Store results. set_transient( 'asmith_so_score', $results, DAY_IN_SECONDS ); update_option( 'asmith_so_last_scan', time() ); /** * Fires after the performance score is calculated. * * @param array $results The score results. */ do_action( 'asmith_so_after_score_calculation', $results ); return $results; } /** * Get the cached score results. * * @return array|false */ public function get_score() { return get_transient( 'asmith_so_score' ); } /** * Convert a numeric score to a letter grade. * * @param int $score The numeric score. * @return string */ private function score_to_grade( $score ) { if ( $score >= 90 ) { return 'A'; } elseif ( $score >= 80 ) { return 'B'; } elseif ( $score >= 60 ) { return 'C'; } elseif ( $score >= 40 ) { return 'D'; } return 'F'; } /** * Build recommendations based on current optimization state. * * @param array $details Score details. * @return array */ private function build_recommendations( $details ) { $recommendations = array(); if ( ! get_option( 'asmith_so_lazy_load', false ) ) { $recommendations[] = array( 'title' => __( 'Enable Lazy Loading', 'a-smith-speed-optimizer' ), 'message' => __( 'Lazy loading defers offscreen images and iframes, reducing initial page weight.', 'a-smith-speed-optimizer' ), 'priority' => 'high', ); } if ( ! get_option( 'asmith_so_defer_js', false ) ) { $recommendations[] = array( 'title' => __( 'Enable JS Deferral', 'a-smith-speed-optimizer' ), 'message' => __( 'Deferring JavaScript prevents render-blocking and improves time to first paint.', 'a-smith-speed-optimizer' ), 'priority' => 'high', ); } if ( ! get_option( 'asmith_so_minify_html', false ) ) { $recommendations[] = array( 'title' => __( 'Enable HTML Minification', 'a-smith-speed-optimizer' ), 'message' => __( 'Minifying HTML removes unnecessary whitespace and comments, reducing page size.', 'a-smith-speed-optimizer' ), 'priority' => 'medium', ); } if ( ! get_option( 'asmith_so_remove_emoji', false ) ) { $recommendations[] = array( 'title' => __( 'Remove Emoji Scripts', 'a-smith-speed-optimizer' ), 'message' => __( 'WordPress emoji scripts add an extra HTTP request on every page load.', 'a-smith-speed-optimizer' ), 'priority' => 'medium', ); } if ( $details['scripts_count'] > 20 ) { $recommendations[] = array( 'title' => __( 'Too Many Scripts', 'a-smith-speed-optimizer' ), /* translators: %d: number of scripts loaded */ 'message' => sprintf( __( 'Your site loads %d scripts. Consider deactivating unnecessary plugins.', 'a-smith-speed-optimizer' ), $details['scripts_count'] ), 'priority' => 'high', ); } if ( $details['styles_count'] > 15 ) { $recommendations[] = array( 'title' => __( 'Too Many Stylesheets', 'a-smith-speed-optimizer' ), /* translators: %d: number of stylesheets loaded */ 'message' => sprintf( __( 'Your site loads %d stylesheets. Consider consolidating or removing unused CSS.', 'a-smith-speed-optimizer' ), $details['styles_count'] ), 'priority' => 'medium', ); } return $recommendations; } }