PluginProbe
ezCache / 2.5.2
ezCache v2.5.2
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 2.5.2, at includes/Rest/PerformanceController.php

190 lines 4.9 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\DatabaseCleanup;
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 'dns_prefetch', 'preconnect', 'heartbeat_control', 'cdn_enabled',
62 'db_cleanup_revisions', 'db_cleanup_auto_drafts', 'db_cleanup_trashed_posts',
63 'db_cleanup_spam_comments', 'db_cleanup_trashed_comments', 'db_cleanup_expired_transients',
64 'db_cleanup_orphan_postmeta', 'db_optimize_tables',
65 'enable_redis_object_cache', 'enable_redis_fullpage', 'enable_critical_css',
66 'enable_speculative_loading', 'enable_early_hints',
67 ];
68
69 foreach ( self::$performance_keys as $key ) {
70 $val = isset( $all_settings->{$key} ) ? $all_settings->{$key} : null;
71 // Cast to boolean for checkbox fields
72 if ( in_array( $key, $bool_keys, true ) && $val !== null ) {
73 $val = (bool) $val;
74 }
75 $settings[ $key ] = $val;
76 }
77
78 $preload_status = [
79 'status' => 'idle',
80 'processed' => 0,
81 'total' => 0,
82 'remaining' => 0,
83 ];
84
85 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
86 $preload_status = Preload::instance()->get_status();
87 }
88
89 return wp_send_json_success( array_merge( $settings, [
90 'preload_status' => $preload_status,
91 ] ) );
92 }
93
94 /**
95 * Update performance settings
96 * @param WP_REST_Request $request
97 */
98 function update( $request ) {
99 $input = (array) $request->get_json_params();
100 $current_settings = (array) Settings::get_settings();
101
102 foreach ( self::$performance_keys as $key ) {
103 if ( isset( $input[ $key ] ) ) {
104 $current_settings[ $key ] = $this->sanitize_value( $key, $input[ $key ] );
105 }
106 }
107
108
109 Settings::set_settings( $current_settings );
110
111 return wp_send_json_success();
112 }
113
114 /**
115 * Run preload
116 */
117 function runPreload() {
118 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
119 Preload::instance()->start();
120 }
121
122 return wp_send_json_success( [ 'message' => 'Preload started' ] );
123 }
124
125 /**
126 * Stop preload
127 */
128 function stopPreload() {
129 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
130 Preload::instance()->stop();
131 }
132
133 return wp_send_json_success( [ 'message' => 'Preload stopped' ] );
134 }
135
136 /**
137 * Run database cleanup
138 */
139 function runDbCleanup() {
140 if ( class_exists( '\Upress\EzCache\DatabaseCleanup' ) ) {
141 DatabaseCleanup::instance()->run();
142 }
143
144 return wp_send_json_success( [ 'message' => 'Database cleanup completed' ] );
145 }
146
147 /**
148 * Sanitize a value based on its key
149 */
150 private function sanitize_value( $key, $value ) {
151 // Boolean fields
152 $booleans = [
153 'enable_preload', 'preload_on_cache_clear', 'preload_crawl_homepage_links',
154 'lazy_load_images', 'lazy_load_iframes', 'defer_js', 'remove_query_strings',
155 'heartbeat_control', 'cdn_enabled',
156 'db_cleanup_revisions', 'db_cleanup_auto_drafts', 'db_cleanup_trashed_posts',
157 'db_cleanup_spam_comments', 'db_cleanup_trashed_comments',
158 'db_cleanup_expired_transients', 'db_cleanup_orphan_postmeta', 'db_optimize_tables',
159 // v2.2.0
160 'enable_redis_object_cache', 'enable_redis_fullpage', 'enable_critical_css',
161 'enable_speculative_loading', 'enable_early_hints',
162 ];
163
164 if ( in_array( $key, $booleans, true ) ) {
165 return (bool) $value;
166 }
167
168 // Integer fields
169 if ( $key === 'preload_batch_size' ) {
170 return max( 1, min( 50, (int) $value ) );
171 }
172
173 // Select fields
174 if ( $key === 'heartbeat_mode' ) {
175 return in_array( $value, [ 'reduce', 'disable' ], true ) ? $value : 'reduce';
176 }
177
178 if ( $key === 'db_cleanup_schedule' ) {
179 return in_array( $value, [ 'never', 'daily', 'weekly' ], true ) ? $value : 'never';
180 }
181
182 if ( $key === 'speculative_mode' ) {
183 return in_array( $value, [ 'conservative', 'moderate', 'eager' ], true ) ? $value : 'moderate';
184 }
185
186 // Text fields
187 return sanitize_textarea_field( $value );
188 }
189 }
190