Skip to content

Refactor: fix SQL interpolation, double file reads, silent failures, and @ error suppression#204

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/refactor-outdated-coding-practices
Draft

Refactor: fix SQL interpolation, double file reads, silent failures, and @ error suppression#204
Copilot wants to merge 2 commits intomainfrom
copilot/refactor-outdated-coding-practices

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 10, 2026

Several files contained embedded variables in raw SQL, a file-read bug, swallowed exceptions with no trace, and widespread @ suppression hiding real failures.

SQL — use $wpdb->prepare() for $time

DatabaseOptimizer.php interpolated $time directly into three SQL strings ('$time'). While time() is always an int, this violates WP coding standards. All three queries (one SELECT COUNT, two DELETE) now use $wpdb->prepare() with %d.

// Before
"DELETE a, b FROM $wpdb->options a ... AND a.option_value < '$time'"

// After
$wpdb->prepare("DELETE a, b FROM $wpdb->options a ... AND a.option_value < %d", $time)

Bug fix — double file_get_contents() in removeDropIns()

The original condition called file_get_contents($file) twice due to short-circuit evaluation:

// Reads file twice if first str_contains() returns false
str_contains(file_get_contents($file), "WPS-Cache") ||
str_contains(file_get_contents($file), "WPS Cache")

Fixed by reading once into $content, with an explicit error log if the read fails.

Silent exceptions → logged errors in HTMLCache::processOutput()

All five catch (\Throwable $e) {} blocks (CriticalCSS, JS, CDN, Font, Media) were completely silent. Each now calls $this->logError(). The redundant @ on $dom->loadHTML() is also removed — libxml_use_internal_errors(true) is already set immediately above it.

@ error suppression removed

Replaced silent-failure patterns with proper error handling across:

  • Plugin::installAdvancedCache()@copy()copy() + error_log() on failure
  • Plugin::secureCacheDirectory()@file_get_contents() / @file_put_contents() with error_log() on write failure; truthiness check corrected to !== false
  • Plugin::createRequiredDirectories()@file_put_contents() removed
  • AdminPanelManager::handleInstallObjectCache()@copy() / @chmod() removed
  • AdminPanelManager::handleRemoveObjectCache()@unlink() removed
  • uninstall.php@file_put_contents() removed (guarded by is_writable()); @opcache_reset() removed (guarded by function_exists())
Original prompt

look for files with unoptimized things or outdated coding practices that slow done or can create bugs. refactor thsoe parts very it can be improved or fix the bugs

Created from VS Code.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

… bugs

Co-authored-by: Jumaron <41344875+Jumaron@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor unoptimized code and fix potential bugs Refactor: fix SQL interpolation, double file reads, silent failures, and @ error suppression Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants