PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Abilities / GetStats.php

GetStats.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.1, at classes/Abilities/GetStats.php

151 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\Abilities;
5
6 /**
7 * MCP ability: returns optimization statistics for WP media and custom folders.
8 *
9 * @since 2.3.0
10 */
11 class GetStats extends AbstractAbility {
12
13 /**
14 * Returns the ability slug.
15 *
16 * @return string
17 */
18 public function get_id(): string {
19 return 'imagify/get-stats';
20 }
21
22 /**
23 * Returns the ability label.
24 *
25 * @return string
26 */
27 public function get_name(): string {
28 return 'Get Imagify optimization stats';
29 }
30
31 /**
32 * Register the ability with the WP Abilities API.
33 *
34 * @return void
35 */
36 public function register(): void {
37 if ( ! function_exists( 'wp_register_ability' ) ) {
38 return;
39 }
40
41 wp_register_ability(
42 'imagify/get-stats',
43 [
44 'label' => __( 'Get Imagify optimization stats', 'imagify' ),
45 'description' => __( 'Returns optimization statistics for WP media and custom folders.', 'imagify' ),
46 'category' => 'imagify',
47 'output_schema' => [
48 'type' => 'object',
49 'properties' => [
50 'wp' => [
51 'type' => 'object',
52 'properties' => [
53 'count_optimized' => [ 'type' => 'integer' ],
54 'count_errors' => [ 'type' => 'integer' ],
55 'original_size' => [ 'type' => 'integer' ],
56 'optimized_size' => [ 'type' => 'integer' ],
57 'savings_percent' => [ 'type' => 'number' ],
58 ],
59 ],
60 'custom-folders' => [
61 'type' => 'object',
62 'properties' => [
63 'count_optimized' => [ 'type' => 'integer' ],
64 'count_errors' => [ 'type' => 'integer' ],
65 'original_size' => [ 'type' => 'integer' ],
66 'optimized_size' => [ 'type' => 'integer' ],
67 'savings_percent' => [ 'type' => 'number' ],
68 ],
69 ],
70 ],
71 ],
72 'execute_callback' => [ $this, 'execute' ],
73 'permission_callback' => [ $this, 'check_permissions' ],
74 'meta' => [
75 'show_in_rest' => true,
76 'mcp' => [
77 'public' => true,
78 ],
79 'annotations' => [
80 'readonly' => true,
81 'destructive' => false,
82 'idempotent' => true,
83 ],
84 ],
85 ]
86 );
87 }
88
89 /**
90 * Check if the current user has permission to execute this ability.
91 *
92 * Routes through Imagify's capability abstraction so the `imagify_capacity`
93 * filter and multisite network-admin logic are honoured.
94 *
95 * @return bool True when the current user has the Imagify `manage` capability.
96 */
97 protected function has_permission(): bool {
98 return imagify_get_context( 'wp' )->current_user_can( 'manage' );
99 }
100
101 /**
102 * Execute the ability and return optimization statistics.
103 *
104 * @return array{
105 * wp: array{count_optimized: int, count_errors: int, original_size: int, optimized_size: int, savings_percent: float},
106 * custom-folders: array{count_optimized: int, count_errors: int, original_size: int, optimized_size: int, savings_percent: float}
107 * }
108 */
109 public function execute(): array {
110 $start_time = microtime( true );
111 $result = $this->do_execute();
112
113 $this->fire_executed( $result, $start_time );
114
115 return $result;
116 }
117
118 /**
119 * Internal execution logic for the ability.
120 *
121 * @return array{
122 * wp: array{count_optimized: int, count_errors: int, original_size: int, optimized_size: int, savings_percent: float},
123 * custom-folders: array{count_optimized: int, count_errors: int, original_size: int, optimized_size: int, savings_percent: float}
124 * }
125 */
126 private function do_execute(): array {
127 $cf_original_size = (int) \Imagify_Files_Stats::get_original_size();
128 $cf_optimized_size = (int) \Imagify_Files_Stats::get_optimized_size();
129 $cf_savings_percent = $cf_original_size > 0
130 ? round( ( ( $cf_original_size - $cf_optimized_size ) / $cf_original_size ) * 100, 1 )
131 : 0.0;
132
133 return [
134 'wp' => [
135 'count_optimized' => (int) imagify_count_optimized_attachments(),
136 'count_errors' => (int) imagify_count_error_attachments(),
137 'original_size' => (int) imagify_count_saving_data( 'original_size' ),
138 'optimized_size' => (int) imagify_count_saving_data( 'optimized_size' ),
139 'savings_percent' => (float) imagify_count_saving_data( 'percent' ),
140 ],
141 'custom-folders' => [
142 'count_optimized' => (int) \Imagify_Files_Stats::count_optimized_files(),
143 'count_errors' => (int) \Imagify_Files_Stats::count_error_files(),
144 'original_size' => $cf_original_size,
145 'optimized_size' => $cf_optimized_size,
146 'savings_percent' => (float) $cf_savings_percent,
147 ],
148 ];
149 }
150 }
151