| 1 |
<?php |
| 2 |
namespace Manage\Modules\Api\Classes; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
class Optimization_Stats { |
| 9 |
|
| 10 |
public static function get_auto_drafts_count(): int { |
| 11 |
global $wpdb; |
| 12 |
|
| 13 |
try { |
| 14 |
$auto_drafts = $wpdb->get_var( |
| 15 |
"SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'auto-draft'" |
| 16 |
); |
| 17 |
|
| 18 |
return (int) $auto_drafts; |
| 19 |
} catch ( \Exception $e ) { |
| 20 |
return 0; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
public static function get_spam_comments_count(): int { |
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
try { |
| 28 |
$spam_comments = $wpdb->get_var( |
| 29 |
"SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam'" |
| 30 |
); |
| 31 |
|
| 32 |
return (int) $spam_comments; |
| 33 |
} catch ( \Exception $e ) { |
| 34 |
return 0; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public static function get_trashed_comments_count(): int { |
| 39 |
global $wpdb; |
| 40 |
|
| 41 |
try { |
| 42 |
$trashed_comments = $wpdb->get_var( |
| 43 |
"SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'trash'" |
| 44 |
); |
| 45 |
|
| 46 |
return (int) $trashed_comments; |
| 47 |
} catch ( \Exception $e ) { |
| 48 |
return 0; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public static function get_revisions_count(): int { |
| 53 |
global $wpdb; |
| 54 |
|
| 55 |
try { |
| 56 |
$revisions = $wpdb->get_var( |
| 57 |
"SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type = 'revision'" |
| 58 |
); |
| 59 |
|
| 60 |
return (int) $revisions; |
| 61 |
} catch ( \Exception $e ) { |
| 62 |
return 0; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public static function get_trashed_posts_count(): int { |
| 67 |
global $wpdb; |
| 68 |
|
| 69 |
try { |
| 70 |
$trashed_posts = $wpdb->get_var( |
| 71 |
"SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'trash'" |
| 72 |
); |
| 73 |
|
| 74 |
return (int) $trashed_posts; |
| 75 |
} catch ( \Exception $e ) { |
| 76 |
return 0; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public static function get_expired_transients_count(): int { |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
try { |
| 84 |
$current_time = time(); |
| 85 |
$expired_count = 0; |
| 86 |
|
| 87 |
// Count regular expired transients |
| 88 |
$timeout_options = $wpdb->get_var( |
| 89 |
$wpdb->prepare( |
| 90 |
"SELECT COUNT(*) FROM {$wpdb->options} |
| 91 |
WHERE option_name LIKE %s |
| 92 |
AND option_value < %d", |
| 93 |
'_transient_timeout_%', |
| 94 |
$current_time, |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
$expired_count += (int) $timeout_options; |
| 99 |
|
| 100 |
// Count site transients if multisite |
| 101 |
if ( is_multisite() ) { |
| 102 |
$site_timeout_options = $wpdb->get_var( |
| 103 |
$wpdb->prepare( |
| 104 |
"SELECT COUNT(*) FROM {$wpdb->options} |
| 105 |
WHERE option_name LIKE %s |
| 106 |
AND option_value < %d", |
| 107 |
'_site_transient_timeout_%', |
| 108 |
$current_time, |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
$expired_count += (int) $site_timeout_options; |
| 113 |
} |
| 114 |
|
| 115 |
return $expired_count; |
| 116 |
} catch ( \Exception $e ) { |
| 117 |
return 0; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public static function get_all_stats(): array { |
| 122 |
return [ |
| 123 |
'auto_drafts' => self::get_auto_drafts_count(), |
| 124 |
'spam_comments' => self::get_spam_comments_count(), |
| 125 |
'trashed_comments' => self::get_trashed_comments_count(), |
| 126 |
'revisions' => self::get_revisions_count(), |
| 127 |
'trashed_posts' => self::get_trashed_posts_count(), |
| 128 |
'expired_transients' => self::get_expired_transients_count(), |
| 129 |
]; |
| 130 |
} |
| 131 |
} |
| 132 |
|