[wp-trac] [WordPress Trac] #53670: Lazy-load admin functions in the new widgets editor REST API instead of bootstrapping the entire admin on every request

WordPress Trac noreply at wordpress.org
Thu Jul 15 15:43:08 UTC 2021


#53670: Lazy-load admin functions in the new widgets editor REST API instead of
bootstrapping the entire admin on every request
--------------------------+-----------------------------
 Reporter:  zieladam      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:  dev-feedback
  Focuses:                |
--------------------------+-----------------------------
 This is a follow up on https://github.com/WordPress/gutenberg/issues/33443

 As a summary, we added `require_once ABSPATH . 'wp-
 admin/includes/admin.php';` to a few REST API endpoints reponsible for
 rendering widgets. This solved the problem of fatal errors, but
 bootstraping wp-admin is slow and consumes resources. It wasn't a big
 problem in the classic widgets editor where it was done just once, but in
 the new one it would be done once per widget.

 There isn't any widget in core WordPress that uses admin functions so we
 only need to do it if there's a plugin or a theme which assumes the
 availability of these functions. An example of such plugin is the [Compact
 Archives plugin](https://wordpress.org/plugins/compact-archives/) which
 works just fine in the classic widgets editor.

 To use admin functions we need to load them first, and since
 https://github.com/WordPress/gutenberg/pull/33454 they are loaded eagerly.
 That's okay but not ideal Can we load them lazily instead?

 If we were talking about classes, we could just add an autoloader via
 spl_autoload_register and call it a day. Unfortunately, PHP does not
 support autoloading functions so no dice here.

 The only other way I can think of is having an auto-generated file with
 "proxy functions" that would only load the admin when that's necessary.
 Much like the patch below:

 {{{
 <?php
 diff --git a/src/wp-admin/includes/lazy-admin.php b/src/wp-admin/includes
 /lazy-admin.php
 index e69de29bb2..b85b9ca001 100644
 --- a/src/wp-admin/includes/lazy-admin.php
 +++ b/src/wp-admin/includes/lazy-admin.php
 +<?php
 +
 +// Ideally this file would be auto-generated
 +
 +function lazy_bootstrap_admin() {
 +       require_once __DIR__ . '/admin.php';
 +}
 +
 +// Bring the documentation over from the actual function
 +function is_plugin_active($name) {
 +       lazy_bootstrap_admin();
 +       return \Admin\is_plugin_active($name);
 +}


 diff --git a/src/wp-admin/includes/plugin.php b/src/wp-
 admin/includes/plugin.php
 index 6e332c247e..18aa080ef3 100644
 --- a/src/wp-admin/includes/plugin.php
 +++ b/src/wp-admin/includes/plugin.php
 @@ -5,7 +5,7 @@
   * @package WordPress
   * @subpackage Administration
   */
 -
 +namespace Admin;
  /**
   * Parses the plugin contents to retrieve plugin's metadata.
   *
 }}}

 This would enable auto-loading of all the things, whether we're in the
 widgets editor world or any other area of WordPress. I am curious if that
 could help speed up the entire WP by avoiding eager loading in principle.
 The price to pay would be an additional layer of complexity, but this
 isn't far off from what we already do with JS and CSS build pipelines –
 only here we're in the PHP world.

 Alternatively, we could deprecate the assumption that wp-admin has already
 been bootstrapped by the time widgets (or their forms) are being rendered.
 This doesn't solve anything for now, but it's a start.

 cc @peterwilsoncc @hellofromtonya @noisysocks

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53670>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list