Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WPS-Cache/src/Admin/AdminPanelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public function handleInstallObjectCache(): void
if (file_exists($destination)) {
$this->redirectWithNotice("Drop-in already exists.", "error");
}
if (@copy($source, $destination)) {
@chmod($destination, 0644);
if (copy($source, $destination)) {
chmod($destination, 0644);
$this->redirectWithNotice(
"Object Cache Drop-in installed.",
"success",
Expand All @@ -176,7 +176,7 @@ public function handleRemoveObjectCache(): void

$file = WP_CONTENT_DIR . "/object-cache.php";
if (file_exists($file)) {
@unlink($file);
unlink($file);
wp_cache_flush();
$this->redirectWithNotice("Drop-in removed.", "success");
} else {
Expand Down
7 changes: 6 additions & 1 deletion WPS-Cache/src/Cache/Drivers/HTMLCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function processOutput(string $buffer): string
libxml_use_internal_errors(true);
$dom = new DOMDocument();
// Hack: force UTF-8
@$dom->loadHTML(
$dom->loadHTML(
'<?xml encoding="utf-8" ?>' . $content,
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD,
);
Expand All @@ -185,6 +185,7 @@ public function processOutput(string $buffer): string
$cssShaker = new CriticalCSSManager($this->settings);
$cssShaker->processDom($dom);
} catch (\Throwable $e) {
$this->logError("CriticalCSS processing failed", $e);
}
}

Expand All @@ -197,6 +198,7 @@ public function processOutput(string $buffer): string
$jsOpt = new JSOptimizer($this->settings);
$jsOpt->processDom($dom);
} catch (\Throwable $e) {
$this->logError("JS optimization failed", $e);
}
}

Expand All @@ -211,20 +213,23 @@ public function processOutput(string $buffer): string
$cdnManager = new CdnManager($this->settings);
$content = $cdnManager->process($content);
} catch (\Throwable $e) {
$this->logError("CDN rewrite failed", $e);
}

// 4. Font Optimization
try {
$fontOpt = new FontOptimizer($this->settings);
$content = $fontOpt->process($content);
} catch (\Throwable $e) {
$this->logError("Font optimization failed", $e);
}

// 5. Media Optimization
try {
$mediaOpt = new MediaOptimizer($this->settings);
$content = $mediaOpt->process($content);
} catch (\Throwable $e) {
$this->logError("Media optimization failed", $e);
}

// Add Timestamp & Signature
Expand Down
39 changes: 24 additions & 15 deletions WPS-Cache/src/Optimization/DatabaseOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ public function getStats(): array
// Optimization: Combined "Local" and "Site" checks into one query.
// This requires reading 'option_value' but does it in 1 round-trip instead of 2.
$expired_count = $wpdb->get_var(
"SELECT COUNT(*) FROM $wpdb->options
WHERE (option_name LIKE '\_transient\_timeout\_%' OR option_name LIKE '\_site\_transient\_timeout\_%')
AND option_value < '$time'"
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->options
WHERE (option_name LIKE '\_transient\_timeout\_%' OR option_name LIKE '\_site\_transient\_timeout\_%')
AND option_value < %d",
$time
)
);

// 2. All Transients (Local + Site)
Expand Down Expand Up @@ -201,24 +204,30 @@ public function processCleanup(array $items): int
// Matches _transient_timeout_KEY and joins to _transient_KEY
// _transient_timeout_ is 19 chars long. SUBSTRING is 1-based, so start at 20.
$wpdb->query(
"DELETE a, b FROM $wpdb->options a
LEFT JOIN $wpdb->options b ON (
b.option_name = CONCAT('_transient_', SUBSTRING(a.option_name, 20))
)
WHERE a.option_name LIKE '\_transient\_timeout\_%'
AND a.option_value < '$time'"
$wpdb->prepare(
"DELETE a, b FROM $wpdb->options a
LEFT JOIN $wpdb->options b ON (
b.option_name = CONCAT('_transient_', SUBSTRING(a.option_name, 20))
)
WHERE a.option_name LIKE '\_transient\_timeout\_%'
AND a.option_value < %d",
$time
)
);

// 2. Site Transients
// Matches _site_transient_timeout_KEY and joins to _site_transient_KEY
// _site_transient_timeout_ is 24 chars long. Start at 25.
$wpdb->query(
"DELETE a, b FROM $wpdb->options a
LEFT JOIN $wpdb->options b ON (
b.option_name = CONCAT('_site_transient_', SUBSTRING(a.option_name, 25))
)
WHERE a.option_name LIKE '\_site\_transient\_timeout\_%'
AND a.option_value < '$time'"
$wpdb->prepare(
"DELETE a, b FROM $wpdb->options a
LEFT JOIN $wpdb->options b ON (
b.option_name = CONCAT('_site_transient_', SUBSTRING(a.option_name, 25))
)
WHERE a.option_name LIKE '\_site\_transient\_timeout\_%'
AND a.option_value < %d",
$time
)
);

$count++;
Expand Down
29 changes: 19 additions & 10 deletions WPS-Cache/src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private function createRequiredDirectories(): void
// Critical for Nginx/IIS where .htaccess is ignored.
$silence = rtrim($path, "/") . "/index.php";
if (!file_exists($silence)) {
@file_put_contents($silence, "<?php // Silence is golden");
file_put_contents($silence, "<?php // Silence is golden");
}
}
}
Expand All @@ -369,17 +369,19 @@ private function secureCacheDirectory(): void
if (!file_exists($htaccess)) {
$shouldUpdate = true;
} else {
$content = @file_get_contents($htaccess);
$content = file_get_contents($htaccess);
if (
$content &&
$content !== false &&
str_contains($content, "Deny from all") &&
!str_contains($content, "<FilesMatch")
) {
$shouldUpdate = true;
}
}
if ($shouldUpdate) {
@file_put_contents($htaccess, self::HTACCESS_CONTENT);
if (file_put_contents($htaccess, self::HTACCESS_CONTENT) === false) {
error_log("WPS Cache: Failed to write .htaccess in cache directory.");
}
}
}

Expand Down Expand Up @@ -442,8 +444,8 @@ private function installAdvancedCache(): void
{
$src = WPSC_PLUGIN_DIR . "includes/advanced-cache-template.php";
$dest = WP_CONTENT_DIR . "/advanced-cache.php";
if (file_exists($src) && !file_exists($dest)) {
@copy($src, $dest);
if (file_exists($src) && !file_exists($dest) && !copy($src, $dest)) {
error_log("WPS Cache: Failed to install advanced-cache drop-in.");
}
}

Expand All @@ -454,12 +456,19 @@ private function removeDropIns(): void
WP_CONTENT_DIR . "/object-cache.php",
];
foreach ($files as $file) {
if (!file_exists($file)) {
continue;
}
$content = file_get_contents($file);
if ($content === false) {
error_log("WPS Cache: Failed to read drop-in file: $file");
continue;
}
if (
file_exists($file) &&
(str_contains(file_get_contents($file), "WPS-Cache") ||
str_contains(file_get_contents($file), "WPS Cache"))
str_contains($content, "WPS-Cache") ||
str_contains($content, "WPS Cache")
) {
@unlink($file);
unlink($file);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions WPS-Cache/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function wpsc_uninstall_rrmdir($dir)
$content,
);
if ($new_content !== $content) {
@file_put_contents($htaccess, $new_content);
file_put_contents($htaccess, $new_content);
}
}
}
Expand All @@ -92,7 +92,7 @@ function wpsc_uninstall_rrmdir($dir)
$content,
);
if ($new_content !== $content) {
@file_put_contents($config, $new_content);
file_put_contents($config, $new_content);
}
}

Expand All @@ -103,5 +103,5 @@ function wpsc_uninstall_rrmdir($dir)

// Clear opcode cache to ensure no old code remains in memory
if (function_exists("opcache_reset")) {
@opcache_reset();
opcache_reset();
}