PluginProbe
ezCache / trunk
ezCache vtrunk
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / Rest / PerformanceController.php

PerformanceController.php in ezCache trunk, at includes/Rest/PerformanceController.php

203 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Upress\EzCache\Rest;
3
4 use Upress\EzCache\Settings;
5 use Upress\EzCache\Preload;
6 use Upress\EzCache\DatabaseOptimizer;
7 use WP_REST_Request;
8 use Upress\EzCache\PremiumFeatures;
9 use Upress\EzCache\CriticalCSS;
10
11 class PerformanceController {
12
13 /**
14 * Performance settings keys
15 */
16 private static $performance_keys = [
17 'enable_preload',
18 'preload_on_cache_clear',
19 'preload_crawl_homepage_links',
20 'preload_sitemap_url',
21 'preload_batch_size',
22 'lazy_load_images',
23 'lazy_load_iframes',
24 'defer_js',
25 'defer_js_exclusions',
26 'remove_query_strings',
27 'dns_prefetch',
28 'preconnect',
29 'heartbeat_control',
30 'heartbeat_mode',
31 'cdn_enabled',
32 'cdn_url',
33 'db_cleanup_revisions',
34 'db_cleanup_auto_drafts',
35 'db_cleanup_trashed_posts',
36 'db_cleanup_spam_comments',
37 'db_cleanup_trashed_comments',
38 'db_cleanup_expired_transients',
39 'db_cleanup_orphan_postmeta',
40 'db_optimize_tables',
41 'db_cleanup_schedule',
42 // v2.2.0
43 'enable_redis_object_cache',
44 'enable_redis_fullpage',
45 'enable_critical_css',
46 'enable_speculative_loading',
47 'speculative_mode',
48 'enable_early_hints',
49 ];
50
51 /**
52 * Get performance settings and preload status
53 */
54 function show() {
55 $all_settings = Settings::get_settings();
56 $settings = [];
57
58 $bool_keys = [
59 'enable_preload', 'preload_on_cache_clear', 'preload_crawl_homepage_links',
60 'lazy_load_images', 'lazy_load_iframes', 'defer_js', 'remove_query_strings',
61 // NOTE: dns_prefetch and preconnect are multi-line TEXT fields (domain
62 // lists), not booleans — casting them here turned the textarea into
63 // "true"/"false". They are intentionally excluded.
64 'heartbeat_control', 'cdn_enabled',
65 'db_cleanup_revisions', 'db_cleanup_auto_drafts', 'db_cleanup_trashed_posts',
66 'db_cleanup_spam_comments', 'db_cleanup_trashed_comments', 'db_cleanup_expired_transients',
67 'db_cleanup_orphan_postmeta', 'db_optimize_tables',
68 'enable_redis_object_cache', 'enable_redis_fullpage', 'enable_critical_css',
69 'enable_speculative_loading', 'enable_early_hints',
70 ];
71
72 foreach ( self::$performance_keys as $key ) {
73 $val = isset( $all_settings->{$key} ) ? $all_settings->{$key} : null;
74 // Cast to boolean for checkbox fields
75 if ( in_array( $key, $bool_keys, true ) && $val !== null ) {
76 $val = (bool) $val;
77 }
78 $settings[ $key ] = $val;
79 }
80
81 $preload_status = [
82 'status' => 'idle',
83 'processed' => 0,
84 'total' => 0,
85 'remaining' => 0,
86 ];
87
88 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
89 $preload_status = Preload::instance()->get_status();
90 }
91
92 return wp_send_json_success( array_merge( $settings, [
93 'preload_status' => $preload_status,
94 ] ) );
95 }
96
97 /**
98 * Update performance settings
99 * @param WP_REST_Request $request
100 */
101 function update( $request ) {
102 $input = (array) $request->get_json_params();
103 $current_settings = (array) Settings::get_settings();
104
105 foreach ( self::$performance_keys as $key ) {
106 if ( isset( $input[ $key ] ) ) {
107 $current_settings[ $key ] = $this->sanitize_value( $key, $input[ $key ] );
108 }
109 }
110
111
112 Settings::set_settings( $current_settings );
113
114 return wp_send_json_success();
115 }
116
117 /**
118 * Run preload
119 */
120 function runPreload() {
121 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
122 Preload::instance()->start();
123 }
124
125 return wp_send_json_success( [ 'message' => 'Preload started' ] );
126 }
127
128 /**
129 * Stop preload
130 */
131 function stopPreload() {
132 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
133 Preload::instance()->stop();
134 }
135
136 return wp_send_json_success( [ 'message' => 'Preload stopped' ] );
137 }
138
139 /**
140 * Run database cleanup
141 */
142 function runDbCleanup( $request ) {
143 // The Vue UI posts an associative map ( { db_cleanup_revisions: true, … } )
144 // and does NOT persist the toggles before running, so the selected tasks
145 // must be taken from the request body. DatabaseOptimizer::clean() expects a
146 // flat list of enabled task keys, so filter out the false values and keep the
147 // keys. An empty body falls back to the saved settings.
148 $params = $request ? (array) $request->get_json_params() : [];
149 $tasks = ! empty( $params ) ? array_keys( array_filter( $params ) ) : null;
150
151 $optimizer = new DatabaseOptimizer();
152 $results = $optimizer->clean( $tasks );
153
154 return wp_send_json_success( [
155 'message' => 'Database cleanup completed',
156 'results' => $results,
157 ] );
158 }
159
160 /**
161 * Sanitize a value based on its key
162 */
163 private function sanitize_value( $key, $value ) {
164 // Boolean fields
165 $booleans = [
166 'enable_preload', 'preload_on_cache_clear', 'preload_crawl_homepage_links',
167 'lazy_load_images', 'lazy_load_iframes', 'defer_js', 'remove_query_strings',
168 'heartbeat_control', 'cdn_enabled',
169 'db_cleanup_revisions', 'db_cleanup_auto_drafts', 'db_cleanup_trashed_posts',
170 'db_cleanup_spam_comments', 'db_cleanup_trashed_comments',
171 'db_cleanup_expired_transients', 'db_cleanup_orphan_postmeta', 'db_optimize_tables',
172 // v2.2.0
173 'enable_redis_object_cache', 'enable_redis_fullpage', 'enable_critical_css',
174 'enable_speculative_loading', 'enable_early_hints',
175 ];
176
177 if ( in_array( $key, $booleans, true ) ) {
178 return (bool) $value;
179 }
180
181 // Integer fields
182 if ( $key === 'preload_batch_size' ) {
183 return max( 1, min( 50, (int) $value ) );
184 }
185
186 // Select fields
187 if ( $key === 'heartbeat_mode' ) {
188 return in_array( $value, [ 'reduce', 'disable' ], true ) ? $value : 'reduce';
189 }
190
191 if ( $key === 'db_cleanup_schedule' ) {
192 return in_array( $value, [ 'never', 'daily', 'weekly' ], true ) ? $value : 'never';
193 }
194
195 if ( $key === 'speculative_mode' ) {
196 return in_array( $value, [ 'conservative', 'moderate', 'eager' ], true ) ? $value : 'moderate';
197 }
198
199 // Text fields
200 return sanitize_textarea_field( $value );
201 }
202 }
203