WP_Debug_Data: Extract `wp-plugins` data into separate methods.

This is the eighth part in a larger modularization of the data in `WP_Debug_Data`. Previously this was a single massive method drawing in debug data from various groups of related data, where the groups were independent from each other.

This patch separates the eighth and ninth of twelve groups, the `wp-plugins-active` and `wp-plugins-inactive` info, into separate methods focused on that data.

Unlike the other patches in this series, the plugins data comes from a single source and is separated out into separate debug sections, so the active and inactive methods call a new shared method which provides raw data for both. Optimizations and refactors may occur in follow-up tickets.

This work precedes changes to make the `WP_Debug_Data` class more extensible for better use by plugin and theme code.

Developed in https://github.com/wordpress/wordpress-develop/pull/7458
Discussed in https://core.trac.wordpress.org/ticket/61648

Props apermo, dmsnell.
See #61648.

Built from https://develop.svn.wordpress.org/trunk@59172


git-svn-id: http://core.svn.wordpress.org/trunk@58567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dmsnell 2024-10-04 22:12:17 +00:00
parent df9b79e4cd
commit 510017d313
2 changed files with 154 additions and 121 deletions

View File

@ -82,8 +82,8 @@ class WP_Debug_Data {
'wp-parent-theme' => array(),
'wp-themes-inactive' => array(),
'wp-mu-plugins' => self::get_wp_mu_plugins(),
'wp-plugins-active' => array(),
'wp-plugins-inactive' => array(),
'wp-plugins-active' => self::get_wp_plugins_active(),
'wp-plugins-inactive' => self::get_wp_plugins_inactive(),
'wp-media' => self::get_wp_media(),
'wp-server' => self::get_wp_server(),
'wp-database' => self::get_wp_database(),
@ -188,18 +188,6 @@ class WP_Debug_Data {
'fields' => array(),
);
$info['wp-plugins-active'] = array(
'label' => __( 'Active Plugins' ),
'show_count' => true,
'fields' => array(),
);
$info['wp-plugins-inactive'] = array(
'label' => __( 'Inactive Plugins' ),
'show_count' => true,
'fields' => array(),
);
// Conditionally add debug information for multisite setups.
if ( is_multisite() ) {
$site_id = get_current_blog_id();
@ -325,112 +313,6 @@ class WP_Debug_Data {
);
}
// List all available plugins.
$plugins = get_plugins();
$plugin_updates = get_plugin_updates();
$transient = get_site_transient( 'update_plugins' );
$auto_updates = array();
$auto_updates_enabled = wp_is_auto_update_enabled_for_type( 'plugin' );
if ( $auto_updates_enabled ) {
$auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
}
foreach ( $plugins as $plugin_path => $plugin ) {
$plugin_part = ( is_plugin_active( $plugin_path ) ) ? 'wp-plugins-active' : 'wp-plugins-inactive';
$plugin_version = $plugin['Version'];
$plugin_author = $plugin['Author'];
$plugin_version_string = __( 'No version or author information is available.' );
$plugin_version_string_debug = 'author: (undefined), version: (undefined)';
if ( ! empty( $plugin_version ) && ! empty( $plugin_author ) ) {
/* translators: 1: Plugin version number. 2: Plugin author name. */
$plugin_version_string = sprintf( __( 'Version %1$s by %2$s' ), $plugin_version, $plugin_author );
$plugin_version_string_debug = sprintf( 'version: %s, author: %s', $plugin_version, $plugin_author );
} else {
if ( ! empty( $plugin_author ) ) {
/* translators: %s: Plugin author name. */
$plugin_version_string = sprintf( __( 'By %s' ), $plugin_author );
$plugin_version_string_debug = sprintf( 'author: %s, version: (undefined)', $plugin_author );
}
if ( ! empty( $plugin_version ) ) {
/* translators: %s: Plugin version number. */
$plugin_version_string = sprintf( __( 'Version %s' ), $plugin_version );
$plugin_version_string_debug = sprintf( 'author: (undefined), version: %s', $plugin_version );
}
}
if ( array_key_exists( $plugin_path, $plugin_updates ) ) {
/* translators: %s: Latest plugin version number. */
$plugin_version_string .= ' ' . sprintf( __( '(Latest version: %s)' ), $plugin_updates[ $plugin_path ]->update->new_version );
$plugin_version_string_debug .= sprintf( ' (latest version: %s)', $plugin_updates[ $plugin_path ]->update->new_version );
}
if ( $auto_updates_enabled ) {
if ( isset( $transient->response[ $plugin_path ] ) ) {
$item = $transient->response[ $plugin_path ];
} elseif ( isset( $transient->no_update[ $plugin_path ] ) ) {
$item = $transient->no_update[ $plugin_path ];
} else {
$item = array(
'id' => $plugin_path,
'slug' => '',
'plugin' => $plugin_path,
'new_version' => '',
'url' => '',
'package' => '',
'icons' => array(),
'banners' => array(),
'banners_rtl' => array(),
'tested' => '',
'requires_php' => '',
'compatibility' => new stdClass(),
);
$item = wp_parse_args( $plugin, $item );
}
$auto_update_forced = wp_is_auto_update_forced_for_item( 'plugin', null, (object) $item );
if ( ! is_null( $auto_update_forced ) ) {
$enabled = $auto_update_forced;
} else {
$enabled = in_array( $plugin_path, $auto_updates, true );
}
if ( $enabled ) {
$auto_updates_string = __( 'Auto-updates enabled' );
} else {
$auto_updates_string = __( 'Auto-updates disabled' );
}
/**
* Filters the text string of the auto-updates setting for each plugin in the Site Health debug data.
*
* @since 5.5.0
*
* @param string $auto_updates_string The string output for the auto-updates column.
* @param string $plugin_path The path to the plugin file.
* @param array $plugin An array of plugin data.
* @param bool $enabled Whether auto-updates are enabled for this item.
*/
$auto_updates_string = apply_filters( 'plugin_auto_update_debug_string', $auto_updates_string, $plugin_path, $plugin, $enabled );
$plugin_version_string .= ' | ' . $auto_updates_string;
$plugin_version_string_debug .= ', ' . $auto_updates_string;
}
$info[ $plugin_part ]['fields'][ sanitize_text_field( $plugin['Name'] ) ] = array(
'label' => $plugin['Name'],
'value' => $plugin_version_string,
'debug' => $plugin_version_string_debug,
);
}
// Populate the section for the currently active theme.
$theme_features = array();
@ -1288,6 +1170,157 @@ class WP_Debug_Data {
);
}
/**
* Gets the WordPress active plugins section of the debug data.
*
* @since 6.7.0
*
* @return array
*/
private static function get_wp_plugins_active(): array {
return array(
'label' => __( 'Active Plugins' ),
'show_count' => true,
'fields' => self::get_wp_plugins_raw_data()['wp-plugins-active'],
);
}
/**
* Gets the WordPress inactive plugins section of the debug data.
*
* @since 6.7.0
*
* @return array
*/
private static function get_wp_plugins_inactive(): array {
return array(
'label' => __( 'Inactive Plugins' ),
'show_count' => true,
'fields' => self::get_wp_plugins_raw_data()['wp-plugins-inactive'],
);
}
/**
* Gets the raw plugin data for the WordPress active and inactive sections of the debug data.
*
* @since 6.7.0
*
* @return array
*/
private static function get_wp_plugins_raw_data(): array {
// List all available plugins.
$plugins = get_plugins();
$plugin_updates = get_plugin_updates();
$transient = get_site_transient( 'update_plugins' );
$auto_updates = array();
$fields = array(
'wp-plugins-active' => array(),
'wp-plugins-inactive' => array(),
);
$auto_updates_enabled = wp_is_auto_update_enabled_for_type( 'plugin' );
if ( $auto_updates_enabled ) {
$auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
}
foreach ( $plugins as $plugin_path => $plugin ) {
$plugin_part = ( is_plugin_active( $plugin_path ) ) ? 'wp-plugins-active' : 'wp-plugins-inactive';
$plugin_version = $plugin['Version'];
$plugin_author = $plugin['Author'];
$plugin_version_string = __( 'No version or author information is available.' );
$plugin_version_string_debug = 'author: (undefined), version: (undefined)';
if ( ! empty( $plugin_version ) && ! empty( $plugin_author ) ) {
/* translators: 1: Plugin version number. 2: Plugin author name. */
$plugin_version_string = sprintf( __( 'Version %1$s by %2$s' ), $plugin_version, $plugin_author );
$plugin_version_string_debug = sprintf( 'version: %s, author: %s', $plugin_version, $plugin_author );
} else {
if ( ! empty( $plugin_author ) ) {
/* translators: %s: Plugin author name. */
$plugin_version_string = sprintf( __( 'By %s' ), $plugin_author );
$plugin_version_string_debug = sprintf( 'author: %s, version: (undefined)', $plugin_author );
}
if ( ! empty( $plugin_version ) ) {
/* translators: %s: Plugin version number. */
$plugin_version_string = sprintf( __( 'Version %s' ), $plugin_version );
$plugin_version_string_debug = sprintf( 'author: (undefined), version: %s', $plugin_version );
}
}
if ( array_key_exists( $plugin_path, $plugin_updates ) ) {
/* translators: %s: Latest plugin version number. */
$plugin_version_string .= ' ' . sprintf( __( '(Latest version: %s)' ), $plugin_updates[ $plugin_path ]->update->new_version );
$plugin_version_string_debug .= sprintf( ' (latest version: %s)', $plugin_updates[ $plugin_path ]->update->new_version );
}
if ( $auto_updates_enabled ) {
if ( isset( $transient->response[ $plugin_path ] ) ) {
$item = $transient->response[ $plugin_path ];
} elseif ( isset( $transient->no_update[ $plugin_path ] ) ) {
$item = $transient->no_update[ $plugin_path ];
} else {
$item = array(
'id' => $plugin_path,
'slug' => '',
'plugin' => $plugin_path,
'new_version' => '',
'url' => '',
'package' => '',
'icons' => array(),
'banners' => array(),
'banners_rtl' => array(),
'tested' => '',
'requires_php' => '',
'compatibility' => new stdClass(),
);
$item = wp_parse_args( $plugin, $item );
}
$auto_update_forced = wp_is_auto_update_forced_for_item( 'plugin', null, (object) $item );
if ( ! is_null( $auto_update_forced ) ) {
$enabled = $auto_update_forced;
} else {
$enabled = in_array( $plugin_path, $auto_updates, true );
}
if ( $enabled ) {
$auto_updates_string = __( 'Auto-updates enabled' );
} else {
$auto_updates_string = __( 'Auto-updates disabled' );
}
/**
* Filters the text string of the auto-updates setting for each plugin in the Site Health debug data.
*
* @since 5.5.0
*
* @param string $auto_updates_string The string output for the auto-updates column.
* @param string $plugin_path The path to the plugin file.
* @param array $plugin An array of plugin data.
* @param bool $enabled Whether auto-updates are enabled for this item.
*/
$auto_updates_string = apply_filters( 'plugin_auto_update_debug_string', $auto_updates_string, $plugin_path, $plugin, $enabled );
$plugin_version_string .= ' | ' . $auto_updates_string;
$plugin_version_string_debug .= ', ' . $auto_updates_string;
}
$fields[ $plugin_part ][ sanitize_text_field( $plugin['Name'] ) ] = array(
'label' => $plugin['Name'],
'value' => $plugin_version_string,
'debug' => $plugin_version_string_debug,
);
}
return $fields;
}
/**
* Gets the WordPress constants section of the debug data.
*

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.7-beta1-59171';
$wp_version = '6.7-beta1-59172';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.