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

184 lines 4.7 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( [
90 'settings' => $settings,
91 'preload_status' => $preload_status,
92 ] );
93 }
94
95 /**
96 * Update performance settings
97 * @param WP_REST_Request $request
98 */
99 function update( $request ) {
100 $input = (array) $request->get_json_params();
101 $current_settings = (array) Settings::get_settings();
102
103 foreach ( self::$performance_keys as $key ) {
104 if ( isset( $input[ $key ] ) ) {
105 $current_settings[ $key ] = $this->sanitize_value( $key, $input[ $key ] );
106 }
107 }
108
109
110 Settings::set_settings( $current_settings );
111
112 return wp_send_json_success();
113 }
114
115 /**
116 * Run preload
117 */
118 function runPreload() {
119 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
120 Preload::instance()->start();
121 }
122
123 return wp_send_json_success( [ 'message' => 'Preload started' ] );
124 }
125
126 /**
127 * Stop preload
128 */
129 function stopPreload() {
130 if ( class_exists( '\Upress\EzCache\Preload' ) ) {
131 Preload::instance()->stop();
132 }
133
134 return wp_send_json_success( [ 'message' => 'Preload stopped' ] );
135 }
136
137 /**
138 * Run database cleanup
139 */
140 function runDbCleanup() {
141 if ( class_exists( '\Upress\EzCache\DatabaseCleanup' ) ) {
142 DatabaseCleanup::instance()->run();
143 }
144
145 return wp_send_json_success( [ 'message' => 'Database cleanup completed' ] );
146 }
147
148 /**
149 * Sanitize a value based on its key
150 */
151 private function sanitize_value( $key, $value ) {
152 // Boolean fields
153 $booleans = [
154 'enable_preload', 'preload_on_cache_clear', 'preload_crawl_homepage_links',
155 'lazy_load_images', 'lazy_load_iframes', 'defer_js', 'remove_query_strings',
156 'heartbeat_control', 'cdn_enabled',
157 'db_cleanup_revisions', 'db_cleanup_auto_drafts', 'db_cleanup_trashed_posts',
158 'db_cleanup_spam_comments', 'db_cleanup_trashed_comments',
159 'db_cleanup_expired_transients', 'db_cleanup_orphan_postmeta', 'db_optimize_tables',
160 ];
161
162 if ( in_array( $key, $booleans, true ) ) {
163 return (bool) $value;
164 }
165
166 // Integer fields
167 if ( $key === 'preload_batch_size' ) {
168 return max( 1, min( 50, (int) $value ) );
169 }
170
171 // Select fields
172 if ( $key === 'heartbeat_mode' ) {
173 return in_array( $value, [ 'reduce', 'disable' ], true ) ? $value : 'reduce';
174 }
175
176 if ( $key === 'db_cleanup_schedule' ) {
177 return in_array( $value, [ 'never', 'daily', 'weekly' ], true ) ? $value : 'never';
178 }
179
180 // Text fields
181 return sanitize_textarea_field( $value );
182 }
183 }
184