Skip to content

Add an Autoloaded Options tab with evidence, a safety check, and WP-CLI - #187

Merged
PDowney merged 3 commits into
mainfrom
autoload-manager
Nov 28, 2026
Merged

PDowney merged 3 commits into
mainfrom
autoload-manager

Conversation

@PDowney

@PDowney PDowney commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Summary

This adds an Autoloaded Options tab to Settings › Site Optimizer. It lists the options WordPress loads on every page, largest first, and stops loading the ones you select.

  • Only the autoload flag changes, never a value. Each change is logged with the flag it replaced (option es_optimizer_autoload_changes, never autoloaded itself). Switching back on restores that exact flag (on, auto, auto-on or yes), and only while the stored flag is still off; if other code has changed it since, its change wins.
  • Locked: WordPress core options, widget_*, theme_mods_*, the roles row, alloptions and notoptions, and the plugin's own options. There is no override.
  • A second list shows what Site Optimizer switched off, and whether other code has since loaded an option again or deleted it.

Who owns an option. WordPress records no owner, so the tab gathers evidence:

  • a guess from the folder names and text domains of installed plugins, must-use plugins and themes;
  • a code search, run on request for the names on the current page, which reads the installed PHP files. It marks a name found nowhere as probably left behind by a removed plugin. It has a 20-second limit and keeps results for a day.
  • an optional recording, for 10 minutes or 1 hour, of which options page loads read, and which plugin or theme first read each one (from the call stack). It costs nothing when off: its switch is an option that is autoloaded only while it exists.

Safety:

  • After a switch-off, the plugin loads the home page (past the page cache) and the dashboard (with the administrator's login cookies), the way Site Health checks the site. If either answers with a server error, it undoes the change at once. If neither can be reached, it changes nothing and says so.
  • WP-CLI: wp site-optimizer autoload list and wp site-optimizer autoload restore [<option>...] [--all]. The command file has no other plugin dependency, so the restore also works on a site a plugin breaks:
    wp --skip-plugins --skip-themes --require=wp-content/plugins/enginescript-site-optimizer/includes/class-es-optimizer-autoload-command.php site-optimizer autoload restore --all
  • Uninstall restores every switched-off option on every site of a network before deleting anything. If one cannot be restored, nothing on that site is deleted and the uninstall stops. The tab, a row under the plugin on the Plugins screen (on a network, the network Plugins screen), and the FAQ say this beforehand. Deactivating changes nothing.

Every request needs manage_options and a nonce. Queries are prepared, LIKE wildcards in the search are escaped, and every option name, plugin name and message is escaped on output.

This adds php-stubs/wp-cli-stubs as a development dependency, and adds it to phpstan.neon and psalm.xml, so static analysis knows WP-CLI's API.

Testing

  • composer check-all passes: PHPCS, PHPMD, Psalm (level 1) and its taint analysis, PHPStan (max), Rector, and PHPUnit with 213 tests and 1032 assertions. There are two new test files, AutoloadTest and AutoloadInsightTest, plus new uninstall tests.
  • The workflow-generated WordPress suite, run locally in Docker with the job's own steps (PHP 8.3, MariaDB 11.8, WordPress 7.1.5), passes: 45 tests on a single site (9 network-only tests skipped) and 45 on a network. The 9 new tests cover:
    • the exact flags in a round trip;
    • hard option names (%, _, quotes, Unicode, 191 bytes) and locked options;
    • an option over 150 KB;
    • changes made by other code;
    • uninstall on a single site and on a network;
    • capability and nonce checks;
    • a failed check that undoes the change;
    • a recording through core's pre_option filter.
  • A browser test on WordPress 7.1.5 passes all 22 checks in Chromium, Firefox and WebKit:
    • the tab, paging, search, locked rows and an escaped name;
    • the code search, the recording, and switching off and on;
    • the Plugins screen warning;
    • a failing check undoing the change;
    • 0 axe-core violations.
  • The EngineScript server suite (a new local optimizer-autoload suite) passes all 46 checks, on Nginx with the FastCGI cache, PHP-FPM, MariaDB, the Redis object cache, and 28 active plugins. It verified that:
    • the Redis alloptions entry shrinks from 416 KB to 56 KB;
    • none of 40 requests sees a stale value;
    • the site, the REST API and WP-CLI keep working;
    • a page that reads a switched-off option makes no extra database query with Redis;
    • every flag survives the round trip;
    • the check catches a deliberately broken home page and undoes the change;
    • the rescue with every plugin skipped restores a site that answers HTTP 500;
    • the recording attributes astra-settings to the Astra theme;
    • the code search takes 0.6 s over all 28 plugins;
    • wp plugin uninstall restores every flag.
  • A security review found nothing at high or medium confidence. semgrep (p/php, p/security-audit), trivy, gitleaks and Plugin Check's PHPCS rulesets are clean.

The changelog entries are under "Unreleased", the README and readme.txt (including new FAQ entries) are updated, and the translation template is regenerated (217 strings).

🤖 Generated with Claude Code

A new tab on the Site Optimizer settings page lists the options WordPress
loads on every page, largest first, and stops loading the ones selected.
Only the autoload flag changes, never a value. Each change is logged with
the flag it replaced, so switching back on restores the exact flag
('on', 'auto', 'auto-on', or 'yes'). Options WordPress and the plugin need
are locked.

WordPress records no owner for an option, so the tab gathers evidence: a
guess from plugin and theme folder names and text domains; on request, a
search of the installed PHP code, which marks names found nowhere as left
behind; and an optional, time-limited recording of which options pages
read, and which plugin or theme read each one.

After a switch-off the plugin loads the home page and the dashboard and
undoes the change if either answers with a server error. WP-CLI commands
list and restore the changes; the command file also loads on its own, so
a restore works with every plugin and theme skipped.

Uninstall restores every switched-off option on every site before it
deletes anything; if one cannot be restored, nothing on that site is
deleted. The tab, the Plugins screen, and the FAQ say so beforehand.

Adds php-stubs/wp-cli-stubs as a development dependency, so that PHPStan
and Psalm know WP-CLI's API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for contributing to EngineScript Site Optimizer!

Before we review:

  • Have you tested your changes with WordPress 7.0+?
  • Are your changes compatible with PHP 8.3+?
  • Have you followed WordPress coding standards?
  • Did you update the CHANGELOG.md if needed?

Security Reminder
This plugin stores per-site settings and emits browser resource hints, so please ensure:

  • All user input is properly sanitized
  • All output is properly escaped
  • No security vulnerabilities are introduced

We'll review your PR soon.

@codacy-production

codacy-production Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 510 complexity · 0 duplication

Metric Results
Complexity 510
Duplication 0

View in Codacy

🟢 Coverage 95.66% diff coverage · -0.66% coverage variation

Metric Results
Coverage variation ✅ -0.66% coverage variation (-1.00%)
Diff coverage ✅ 95.66% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (d9e3fbd) 971 941 96.91%
Head commit (c09694f) 1998 (+1027) 1923 (+982) 96.25% (-0.66%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#187) 1036 991 95.66%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

PDowney and others added 2 commits November 28, 2026 02:17
…tests

Plugin Check did not recognise the WP-CLI command file's direct-access
guard. The guard now reads `defined( 'WP_CLI' ) || defined( 'ABSPATH' )
|| exit;`, which keeps the WP-CLI exception that --require needs, and
Rector skips the two rules that would rewrite it for this file.

New unit tests cover the WP-CLI commands, through a WP-CLI double, the
list of switched-off options, and the page links, which brings line
coverage back above the minimum. The code search check moves out of the
view reader, and reviewed false positives carry nosemgrep markers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers malformed stored usage and code search results, the minimum name
length of the code search, and each check result and recording notice,
which keeps total coverage within Codacy's allowed drop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PDowney
PDowney merged commit 6530eb6 into main Nov 28, 2026
26 checks passed
@github-actions
github-actions Bot deleted the autoload-manager branch November 28, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant