| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Autoload Checker |
| 4 |
Version: 1.2 |
| 5 |
Description: Checks the autoloaded data size and lists the top autoloaded data entries sorted by size. |
| 6 |
Author: Gerard Blanco |
| 7 |
Author URI: https://accelerawp.com |
| 8 |
License: GPLv2 or later |
| 9 |
License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 10 |
Text Domain: autoload-checker |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
# |
| 16 |
# Main Class: GRBLNC_Autoload_Checker - Solid prefix to avoid conflicts with other plugins |
| 17 |
# |
| 18 |
|
| 19 |
if( !class_exists( 'GRBLNC_Autoload_Checker' ) ){ |
| 20 |
|
| 21 |
class GRBLNC_Autoload_Checker{ |
| 22 |
|
| 23 |
function __construct(){ |
| 24 |
// Call autoload_size_menu function to load plugin menu in dashboard |
| 25 |
add_action( 'admin_menu', [ $this, 'autoload_size_menu' ] ); |
| 26 |
} |
| 27 |
|
| 28 |
// Create WordPress admin menu |
| 29 |
function autoload_size_menu() { |
| 30 |
$parent_slug = 'tools.php'; |
| 31 |
$page_title = 'Autoload Checker'; |
| 32 |
$menu_title = 'Autoload Checker'; |
| 33 |
$capability = 'manage_options'; |
| 34 |
$menu_slug = 'autoload-checker'; |
| 35 |
$function = [ $this, 'autoload_checker' ]; |
| 36 |
|
| 37 |
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); |
| 38 |
} |
| 39 |
|
| 40 |
// Create WordPress plugin page |
| 41 |
function autoload_checker() { |
| 42 |
?> |
| 43 |
<div class="wrap"> |
| 44 |
<style> |
| 45 |
.ac-flex-container { |
| 46 |
display: flex; |
| 47 |
gap: 20px; |
| 48 |
align-items: flex-start; |
| 49 |
} |
| 50 |
.ac-promo-box { |
| 51 |
width:300px; |
| 52 |
border: 1px solid #c3c4c7; |
| 53 |
background-color: white; |
| 54 |
box-shadow: 0 1px 1px rgba(0, 0, 0, .04); |
| 55 |
padding:15px; |
| 56 |
text-align:center; |
| 57 |
} |
| 58 |
|
| 59 |
/* Responsive: stack the promo box ABOVE the table */ |
| 60 |
@media (max-width: 638px) { |
| 61 |
.ac-flex-container { |
| 62 |
flex-wrap: wrap; |
| 63 |
} |
| 64 |
.ac-promo-box { |
| 65 |
order: -1; /* Moves promo box above the table */ |
| 66 |
width: 100% !important; |
| 67 |
} |
| 68 |
.ac-table-wrapper { |
| 69 |
width: 100%; |
| 70 |
} |
| 71 |
} |
| 72 |
</style> |
| 73 |
<h1><?php echo esc_html__( 'Total Autoload Size','autoload-checker' ); ?></h1> |
| 74 |
<?php |
| 75 |
global $wpdb; |
| 76 |
|
| 77 |
// Get autoloaded options |
| 78 |
$alloptions = wp_load_alloptions(); |
| 79 |
|
| 80 |
// Initialize total size and toplist array |
| 81 |
$total_size_bytes = 0; |
| 82 |
$autoload_toplist = []; |
| 83 |
|
| 84 |
// Calculate total size and build top 20 list |
| 85 |
foreach ( $alloptions as $option_name => $option_value ) { |
| 86 |
// Serialize if needed to calculate size with overhead |
| 87 |
if ( is_array( $option_value ) || is_object( $option_value ) ) { |
| 88 |
$option_value = maybe_serialize( $option_value ); |
| 89 |
} |
| 90 |
|
| 91 |
$size_bytes = strlen( (string) $option_value ); |
| 92 |
$total_size_bytes += $size_bytes; |
| 93 |
|
| 94 |
// Add to toplist array |
| 95 |
$autoload_toplist[] = (object) [ |
| 96 |
'option_name' => $option_name, |
| 97 |
'option_value_size' => $size_bytes / 1024, // Convert bytes to KB |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
// Sort toplist by size (descending) and get top 30 |
| 102 |
usort( $autoload_toplist, function( $a, $b ) { |
| 103 |
return $b->option_value_size <=> $a->option_value_size; |
| 104 |
}); |
| 105 |
$autoload_toplist = array_slice( $autoload_toplist, 0, 30 ); |
| 106 |
|
| 107 |
// Convert total size to KB |
| 108 |
$total_size_kb = round( $total_size_bytes / 1024 ); |
| 109 |
|
| 110 |
// Display total size |
| 111 |
?> |
| 112 |
<p style="font-weight:bold;font-size:1.5em;"><?php echo esc_html( $total_size_kb . ' KB' ); ?></p> |
| 113 |
|
| 114 |
<h2><?php echo esc_html__( 'Autoload Top List:', 'autoload-checker' ); ?></h2> |
| 115 |
<div class="ac-flex-container"> |
| 116 |
<div class="ac-table-wrapper"> |
| 117 |
<table style="max-width:600px;" class="widefat striped"> |
| 118 |
<thead><tr> |
| 119 |
<th scope="col"><?php echo esc_html__( '#', 'autoload-checker' ); ?></th> |
| 120 |
<th scope="col"><?php echo esc_html__( 'Option Name', 'autoload-checker' ); ?></th> |
| 121 |
<th scope="col"><?php echo esc_html__( 'Size', 'autoload-checker' ); ?></th> |
| 122 |
</tr></thead> |
| 123 |
<tbody> |
| 124 |
<?php foreach ( $autoload_toplist as $k => $option ) : |
| 125 |
$index = $k + 1; |
| 126 |
$size = round( $option->option_value_size ) . ' KB'; |
| 127 |
?> |
| 128 |
<tr> |
| 129 |
<td><?php echo esc_html( $index ); ?></td> |
| 130 |
<td><?php echo esc_html( $option->option_name ); ?></td> |
| 131 |
<td><?php echo esc_html( $size ); ?></td> |
| 132 |
</tr> |
| 133 |
<?php endforeach; ?> |
| 134 |
</tbody> |
| 135 |
</table> |
| 136 |
</div> |
| 137 |
<div class="ac-promo-box"> |
| 138 |
<a target="_blank" style="display:block;max-width:300px;margin:0 auto;" href="https://accelerawp.com/?utm_source=autoload+checker"><img src="<?php echo esc_url( plugins_url( 'logo.png', __FILE__ ) ); ?>" alt="Accelera Logo" style="max-width:100%;"></a> |
| 139 |
<p style="font-size:14px;"> |
| 140 |
Need help? Check out <a target="_blank" href="https://accelerawp.com/?utm_source=autoload+checker"><strong>Accelera</strong></a> for professional WordPress optimization services. |
| 141 |
</p> |
| 142 |
</div> |
| 143 |
</div> |
| 144 |
<?php |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
# Run the class |
| 150 |
new GRBLNC_Autoload_Checker; |
| 151 |
|
| 152 |
} |
| 153 |
?> |
| 154 |
|