Upgrade/Install: Micro-optimizations for getting plugin_file in plugins loader loop.

RE: Plugins Dependencies.

The following micro-optimization improvements are included for finding each plugin's file relative to the plugins' directory within `wp-settings.php`:

* Move `trailingslashit()` before `foreach()`.

The path to the plugin directory is a constant. Invoking the `trailingslashit()` within the loop for each plugin is unnecessary and less performant.

This commit moves the plugin directory logic to before the loop. The result: the logic will now run 1x instead of Px where P represents the number of active and valid plugins to be loaded.

* Use `substr()` instead of `str_replace()` to extract the plugin's file relative to the plugins' directory.

`substr()` is more performant than `str_replace()`.

Why?

Per the PHP handbook:
>"This function returns a string or an array with all occurrences of search in subject replaced with the given replace value."

`str_replace()` searches the entire string to find and replace each substring occurrence.

whereas

>"Returns the portion of string specified by the offset and length parameters."

`substr()` starts at the given offset and stops at the given (or end of the) string length.

In other words, `substr()` iterates over less of and only a specific portion of the given input string, whereas `str_replace()` iterates through the entire string searching for matches (plural).

References:
* `str_replace()` https://www.php.net/manual/en/function.str-replace.php
* `substr()` https://www.php.net/manual/en/function.substr.php
* `strlen()` https://www.php.net/manual/en/function.strlen.php
* Show the comparison in action https://3v4l.org/TbQ9U.

Follow-up to [57545], [57592].

Props hellofromTonya, costdev.
Fixes #60510.
Built from https://develop.svn.wordpress.org/trunk@57606


git-svn-id: http://core.svn.wordpress.org/trunk@57107 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2024-02-12 23:10:12 +00:00
parent 5312d18ca2
commit 8d04dc7e1c
2 changed files with 5 additions and 4 deletions

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57605';
$wp_version = '6.5-alpha-57606';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -499,10 +499,11 @@ if ( ! is_multisite() && wp_is_fatal_error_handler_enabled() ) {
}
// Load active plugins.
$all_plugin_data = get_option( 'plugin_data', array() );
$failed_plugins = array();
$all_plugin_data = get_option( 'plugin_data', array() );
$failed_plugins = array();
$plugins_dir_strlen = strlen( trailingslashit( WP_PLUGIN_DIR ) );
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
$plugin_file = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $plugin );
$plugin_file = substr( $plugin, $plugins_dir_strlen );
$plugin_headers = $all_plugin_data[ $plugin_file ];
$errors = array();
$requirements = array(