Skip to content

Autoload WordPress Core classes using native PHP classloader with static classmap#12089

Open
sun wants to merge 253 commits into
WordPress:trunkfrom
sun:feat/autoload
Open

Autoload WordPress Core classes using native PHP classloader with static classmap#12089
sun wants to merge 253 commits into
WordPress:trunkfrom
sun:feat/autoload

Conversation

@sun

@sun sun commented Jun 4, 2026

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/60414

This resurrects and expands @aristath's great work in #3470

Use of AI Tools

AI assistance: Yes
Tool(s): Anthrophic Claude
Model(s): Sonnet-4.6
Used for: Updating code for latest trunk; final architecture and implementation were reviewed and edited by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

aristath and others added 16 commits August 9, 2024 11:47
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Adds WP_Exception, WP_Filter_Sentinel, WP_Block_Processor, WP_Block_Metadata_Registry, WP_Connector_Registry, WP_Icons_Registry, WP_URL_Pattern_Prefixer, WP_Speculation_Rules, the abilities-api classes, the ai-client classes, and the new REST API controllers (abilities v1, icons) to WP_Autoload::CLASSES_PATHS.

Also moves WP_Filter_Sentinel fallback require inside the !WP_Autoload guard in plugin.php, consistent with the WP_Hook treatment.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Revert wpdb from classmap: class-wpdb.php defines global constants
  (OBJECT, ARRAY_A, …) at file scope that must be available before the
  first DB query.  Restores require_once inside require_wp_db() in load.php.
- Revert wp_importer from classmap: class-wp-importer.php defines the
  global function get_cli_args() outside the class body; importer plugins
  load the file themselves.
- Document both exclusions and the multi-class-per-file list in the
  class-wp-autoload.php file docblock.
- Switch require → require_once in autoload_core() so that files mapping
  multiple classes (class-json.php, class-wp-http-streams.php, etc.) are
  never included twice, and add an explanatory comment.
- Add inline comment to wp-settings.php explaining why the safety-net
  require_once of class-wp-autoload.php is kept there.
- Fix test regex to match final/abstract/readonly class, interface, trait,
  and enum declarations, not just bare class.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…p-autoload.php.

WP_Autoload::register() is now called explicitly at each of the six bootstrap
entry points (wp-load.php, wp-settings.php, index.php, setup-config.php,
load-styles.php, load-scripts.php) so the class file is a pure definition with
no side effects on include.

Also restores the explicit require_once of class-wp-importer.php in
wp-admin/includes/import.php, matching the exclusion from the classmap
introduced in the previous commit.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Hi @sun! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props aristath.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

sun and others added 10 commits June 8, 2026 10:32
… autoloader check.

The WP autoloader's str_starts_with('simplepie') check intercepted the namespaced
SimplePie\SimplePie class name, fired require_once class-simplepie.php, and then
when library/SimplePie.php triggered a nested autoload for SimplePie\SimplePie the
same require_once was a no-op on re-entry — leaving SimplePie\SimplePie undefined.

Two fixes:
1. Restore the explicit class-simplepie.php bootstrap (with class_exists guard) at
   the top of fetch_feed(), matching trunk.  This registers SimplePie's own PSR-4
   autoloader and the legacy shim (which defines SIMPLEPIE_PCRE_HTML_ATTRIBUTE and
   other backward-compat constants) before the namespaced API is used.
2. Narrow the WP autoloader's simplepie branch to exclude names containing a
   backslash (i.e. SimplePie\* namespace classes), which SimplePie's own PSR-4
   autoloader handles.

Fixes: Class "SimplePie\SimplePie" not found in Tests_Feed_FetchFeed.
Fixes: Undefined constant "SIMPLEPIE_PCRE_HTML_ATTRIBUTE" in widget RSS tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add wp_phpmailer to classmap (class-only file, no side effects), exclude class-wp-autoload.php (cannot bootstrap itself) and class-wpdb.php (intentionally omitted, defines global constants) from the test completeness check.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…when upgrading from older WP.

When upgrading from a WordPress version that predates WP_Autoload,
the class is never loaded during the old bootstrap, causing a fatal
error the first time _preload_core_classes_and_interfaces() accesses
WP_Autoload::CLASSES_PATHS. Load the class file explicitly at the
start of the function (require_once is a no-op if already loaded).

Also guard with isset() before accessing CLASSES_PATHS so that
intentionally excluded classes such as wpdb are silently skipped
rather than triggering an undefined array key error.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ing from pre-autoloader WP. The function is called before new core files are copied to ABSPATH, so class-wp-autoload.php does not exist yet. Return early — upgrader classes are already in memory via the old bootstrap.
…bundled() causes ~5% regression. On servers with native sodium the compat shim is never loaded by trunk; removing the method eliminates 7k lines of PHP loaded on every request. External autoloaders (Requests, sodium_compat, SimplePie) remain registered by their existing call sites.
…ng class-wp-http.php. class-wp-http.php has top-level side effects (Requests autoloader registration, TLS cert path) that must run at bootstrap. Restore explicit require in wp-settings.php and exclude the file from the classmap, matching class-wpdb.php treatment.
…very request. Two changes: (1) autoload_core now checks CLASSES_PATHS first before avifinfo/simplepie prefix tests, and skips namespaced classes immediately; (2) class-wp-http.php re-prepends WP_Autoload after registering Requests so WP core lookups do not pay the cost of calling Requests::load first.
…s-wp-http.php. WpOrg\Requests\Autoload::register() appends (not prepends) despite its docblock claiming otherwise — the third arg to spl_autoload_register() is omitted, defaulting to prepend=false. The SPL stack order was already correct; the unregister+re-register added overhead without benefit.
… optimization. The str_contains('\\') early-return skipped Avifinfo\* classes, which are namespaced but have no separate autoloader. Carve out Avifinfo\* as the explicit exception inside the namespace branch.

@dmsnell dmsnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this is begging for reliable benchmarking to compare it against the static includes.

There are some ideas I would be interesting in exploring for the class map, including the creation of a WP_Token_Map for lookup, or even for creating a finite state machine for lookup.

The Taskusanakirja project recently implemented such a change and documented it, though its purpose was more about reducing memory requirements than lookup speed. I would be surprised if this approach or one like it is not much faster than PHP array key lookup, though without realistic benchmarks I wouldn’t say.

https://til.andrew-quinn.me/posts/replacing-a-3-gb-sqlite-database-with-a-7-mb-fst-finite-state-trandsucer-binary/

To that end it would be helpful in the WP_Autoload class to provide a method mapping class name to file in which it’s found. This would make it easier to build a benchmark testing different approaches for lookup.

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.

3 participants