Skip to content

Commit 8ca97f7

Browse files
authored
Merge pull request #4 from Jumaron:working-wp-beta
Working-wp-beta
2 parents 3597826 + 9a2d25a commit 8ca97f7

23 files changed

+842
-701
lines changed

includes/advanced-cache-template.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class WPSAdvancedCache {
2121
];
2222

2323
private const CONTENT_TYPES = [
24-
'css' => 'text/css; charset=UTF-8',
25-
'js' => 'application/javascript; charset=UTF-8',
24+
'css' => 'text/css; charset=UTF-8',
25+
'js' => 'application/javascript; charset=UTF-8',
2626
'html' => 'text/html; charset=UTF-8'
2727
];
2828

@@ -73,7 +73,7 @@ private function shouldBypassCache(): bool {
7373
isset($_GET['preview']) ||
7474
!empty($_POST) ||
7575
is_admin() ||
76-
($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'GET' ||
76+
(($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'GET') ||
7777
!empty($_GET) || // Query parameters bypass cache
7878
(isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
7979
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') // AJAX requests
@@ -123,14 +123,15 @@ private function handleHtmlCache(): void {
123123
* Serves a cached file with appropriate headers
124124
*/
125125
private function serveCachedFile(string $file, string $content_type): bool {
126-
$content = @file_get_contents($file);
127-
if ($content === false) {
126+
// Instead of loading the file into a variable and echoing it,
127+
// we use readfile() to output the file directly.
128+
if (!file_exists($file)) {
128129
return false;
129130
}
130131

131132
$cache_time = filemtime($file);
132-
$etag = '"' . md5($content) . '"';
133-
133+
$etag = '"' . md5_file($file) . '"';
134+
134135
// Check if-none-match for browser caching
135136
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) === $etag) {
136137
header('HTTP/1.1 304 Not Modified');
@@ -142,10 +143,10 @@ private function serveCachedFile(string $file, string $content_type): bool {
142143
header('Cache-Control: public, max-age=' . self::YEAR_IN_SECONDS);
143144
header('ETag: ' . $etag);
144145
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $cache_time) . ' GMT');
145-
146+
146147
$this->setHeader('HIT');
147-
148-
echo $content;
148+
149+
readfile($file);
149150
exit;
150151
}
151152

@@ -188,4 +189,4 @@ private function getSettings(): array {
188189

189190
// Continue normal WordPress execution
190191
header('X-WPS-Cache: ERROR');
191-
}
192+
}

includes/object-cache.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22
/**
33
* WordPress Redis Object Cache Backend
44
*
@@ -1323,19 +1323,7 @@ private function connectPredis(array $config): void
13231323
*/
13241324
private function getSelectiveFlushScript(): string
13251325
{
1326-
return <<<LUA
1327-
local cursor = "0"
1328-
local count = 0
1329-
repeat
1330-
local result = redis.call('SCAN', cursor, 'MATCH', ARGV[1])
1331-
cursor = result[1]
1332-
local keys = result[2]
1333-
if #keys > 0 then
1334-
count = count + redis.call('DEL', unpack(keys))
1335-
end
1336-
until cursor == "0"
1337-
return count
1338-
LUA;
1326+
return "local cursor = \"0\"\nlocal count = 0\nrepeat\n local result = redis.call('SCAN', cursor, 'MATCH', ARGV[1])\n cursor = result[1]\n local keys = result[2]\n if #keys > 0 then\n count = count + redis.call('DEL', unpack(keys))\n end\nuntil cursor == \"0\"\nreturn count";
13391327
}
13401328

13411329
/**

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: Jumaron
33
Tags: caching, performance, HTML, Redis, Varnish
44
Requires at least: 6.3
55
Tested up to: 6.7
6-
Stable tag: 0.0.2
6+
Stable tag: 0.0.3
77
License: GPLv2 or later
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

src/Admin/AdminPanelManager.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public function enqueueAdminAssets(string $hook): void {
9090
WPSC_VERSION
9191
);
9292

93-
9493
wp_enqueue_script(
9594
'wpsc-admin-scripts',
9695
WPSC_PLUGIN_URL . 'assets/js/admin.js',
@@ -119,18 +118,19 @@ private function getJsConfig(): array {
119118
*/
120119
private function getJsStrings(): array {
121120
return [
122-
'confirm_clear_cache' => __('Are you sure you want to clear all caches?', 'wps-cache'),
123-
'confirm_install_dropin' => __('Are you sure you want to install the object cache drop-in?', 'wps-cache'),
124-
'confirm_remove_dropin' => __('Are you sure you want to remove the object cache drop-in?', 'wps-cache'),
125-
'loading' => __('Loading...', 'wps-cache'),
126-
'error' => __('An error occurred', 'wps-cache'),
127-
'success' => __('Operation completed successfully', 'wps-cache'),
128-
'preload_progress' => __('Preloading: %d%%', 'wps-cache'),
129-
'preload_complete' => __('Preloading completed', 'wps-cache'),
130-
'preload_error' => __('Error during preloading', 'wps-cache'),
131-
'export_error' => __('Error exporting settings', 'wps-cache'),
132-
'import_error' => __('Error importing settings', 'wps-cache'),
133-
'invalid_file' => __('Invalid settings file', 'wps-cache')
121+
'confirm_clear_cache' => __('Are you sure you want to clear all caches?', 'WPS-Cache'),
122+
'confirm_install_dropin' => __('Are you sure you want to install the object cache drop-in?', 'WPS-Cache'),
123+
'confirm_remove_dropin' => __('Are you sure you want to remove the object cache drop-in?', 'WPS-Cache'),
124+
'loading' => __('Loading...', 'WPS-Cache'),
125+
'error' => __('An error occurred', 'WPS-Cache'),
126+
'success' => __('Operation completed successfully', 'WPS-Cache'),
127+
/* translators: %d: percentage of preloading progress */
128+
'preload_progress' => __('Preloading: %d%%', 'WPS-Cache'),
129+
'preload_complete' => __('Preloading completed', 'WPS-Cache'),
130+
'preload_error' => __('Error during preloading', 'WPS-Cache'),
131+
'export_error' => __('Error exporting settings', 'WPS-Cache'),
132+
'import_error' => __('Error importing settings', 'WPS-Cache'),
133+
'invalid_file' => __('Invalid settings file', 'WPS-Cache')
134134
];
135135
}
136136

@@ -142,10 +142,12 @@ public function renderAdminPage(): void {
142142
return;
143143
}
144144

145-
$current_tab = $_GET['tab'] ?? 'settings';
145+
// Use TabManager's method to retrieve the active tab, which properly sanitizes
146+
// and verifies the nonce for the 'tab' query variable.
147+
$current_tab = $this->tab_manager->getCurrentTab();
146148
?>
147-
<div class="wrap">
148-
<h1><?php _e('WPS Cache', 'wps-cache'); ?></h1>
149+
<div class="wrap">
150+
<h1><?php esc_html_e('WPS Cache', 'WPS-Cache'); ?></h1>
149151

150152
<div class="wpsc-admin-container">
151153
<nav class="wpsc-tabs">

src/Admin/Analytics/AnalyticsManager.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,26 @@ public function handleAjaxGetCacheMetrics(): void {
109109
private function renderStatCards(array $redis_stats): void {
110110
$stats = [
111111
[
112-
'title' => __('Cache Hit Ratio', 'wps-cache'),
112+
'title' => __('Cache Hit Ratio', 'WPS-Cache'),
113113
'value' => isset($redis_stats['hit_ratio']) ?
114114
number_format_i18n($redis_stats['hit_ratio'], 2) . '%' : 'N/A',
115115
'metric' => 'hit_ratio'
116116
],
117117
[
118-
'title' => __('Memory Usage', 'wps-cache'),
118+
'title' => __('Memory Usage', 'WPS-Cache'),
119119
'value' => isset($redis_stats['memory_used']) ?
120120
size_format($redis_stats['memory_used']) : 'N/A',
121121
'metric' => 'memory_used'
122122
],
123123
[
124-
'title' => __('Cache Operations', 'wps-cache'),
124+
'title' => __('Cache Operations', 'WPS-Cache'),
125125
'value' => number_format_i18n(
126126
($redis_stats['hits'] ?? 0) + ($redis_stats['misses'] ?? 0)
127127
),
128128
'metric' => 'total_ops'
129129
],
130130
[
131-
'title' => __('Server Uptime', 'wps-cache'),
131+
'title' => __('Server Uptime', 'WPS-Cache'),
132132
'value' => isset($redis_stats['uptime']) ?
133133
human_time_diff(time() - $redis_stats['uptime']) : 'N/A'
134134
]
@@ -162,13 +162,13 @@ private function renderStatCard(array $stat): void {
162162
*/
163163
private function renderDetailedMetrics(array $redis_stats): void {
164164
?>
165-
<h3><?php _e('Detailed Metrics', 'wps-cache'); ?></h3>
165+
<h3><?php esc_html_e('Detailed Metrics', 'WPS-Cache'); ?></h3>
166166
<table class="widefat striped">
167167
<thead>
168168
<tr>
169-
<th><?php _e('Metric', 'wps-cache'); ?></th>
170-
<th><?php _e('Value', 'wps-cache'); ?></th>
171-
<th><?php _e('Trend', 'wps-cache'); ?></th>
169+
<th><?php esc_html_e('Metric', 'WPS-Cache'); ?></th>
170+
<th><?php esc_html_e('Value', 'WPS-Cache'); ?></th>
171+
<th><?php esc_html_e('Trend', 'WPS-Cache'); ?></th>
172172
</tr>
173173
</thead>
174174
<tbody id="detailed-metrics">
@@ -183,15 +183,15 @@ private function renderDetailedMetrics(array $redis_stats): void {
183183
*/
184184
private function renderMetricsTableRows(array $stats): void {
185185
$metrics = [
186-
'hits' => __('Cache Hits', 'wps-cache'),
187-
'misses' => __('Cache Misses', 'wps-cache'),
188-
'hit_ratio' => __('Hit Ratio', 'wps-cache'),
189-
'memory_used' => __('Memory Usage', 'wps-cache'),
190-
'memory_peak' => __('Peak Memory', 'wps-cache'),
191-
'total_connections' => __('Total Connections', 'wps-cache'),
192-
'connected_clients' => __('Connected Clients', 'wps-cache'),
193-
'evicted_keys' => __('Evicted Keys', 'wps-cache'),
194-
'expired_keys' => __('Expired Keys', 'wps-cache'),
186+
'hits' => __('Cache Hits', 'WPS-Cache'),
187+
'misses' => __('Cache Misses', 'WPS-Cache'),
188+
'hit_ratio' => __('Hit Ratio', 'WPS-Cache'),
189+
'memory_used' => __('Memory Usage', 'WPS-Cache'),
190+
'memory_peak' => __('Peak Memory', 'WPS-Cache'),
191+
'total_connections' => __('Total Connections', 'WPS-Cache'),
192+
'connected_clients' => __('Connected Clients', 'WPS-Cache'),
193+
'evicted_keys' => __('Evicted Keys', 'WPS-Cache'),
194+
'expired_keys' => __('Expired Keys', 'WPS-Cache'),
195195
];
196196

197197
foreach ($metrics as $key => $label) {
@@ -237,15 +237,14 @@ private function formatMetricValue(string $key, mixed $value): string {
237237
/**
238238
* Gets Redis cache statistics
239239
*/
240-
private function getRedisStats(): ?array {
240+
private function getRedisStats(): array {
241241
$redis_driver = $this->cache_manager->getDriver('redis');
242242
if (!$redis_driver instanceof RedisCache) {
243-
return null;
243+
return [];
244244
}
245-
246245
return $redis_driver->getStats();
247246
}
248-
247+
249248
/**
250249
* Gets HTML cache statistics
251250
*/

0 commit comments

Comments
 (0)