Include inactive plugins in system status

This commit is contained in:
Mike Jolley 2019-03-06 18:29:44 +00:00
parent 6f983fb4eb
commit 56fbcfd8af
2 changed files with 192 additions and 85 deletions

View File

@ -23,6 +23,7 @@ $environment = $system_status->get_environment_info();
$database = $system_status->get_database_info();
$post_type_counts = $system_status->get_post_type_counts();
$active_plugins = $system_status->get_active_plugins();
$inactive_plugins = $system_status->get_inactive_plugins();
$theme = $system_status->get_theme_info();
$security = $system_status->get_security_info();
$settings = $system_status->get_settings();
@ -603,6 +604,58 @@ $untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, 'minor
?>
</tbody>
</table>
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>
<th colspan="3" data-export-label="Inactive Plugins (<?php echo count( $inactive_plugins ); ?>)"><h2><?php esc_html_e( 'Inactive plugins', 'woocommerce' ); ?> (<?php echo count( $inactive_plugins ); ?>)</h2></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $inactive_plugins as $plugin ) {
if ( ! empty( $plugin['name'] ) ) {
$dirname = dirname( $plugin['plugin'] );
// Link the plugin name to the plugin url if available.
$plugin_name = esc_html( $plugin['name'] );
if ( ! empty( $plugin['url'] ) ) {
$plugin_name = '<a href="' . esc_url( $plugin['url'] ) . '" aria-label="' . esc_attr__( 'Visit plugin homepage', 'woocommerce' ) . '" target="_blank">' . $plugin_name . '</a>';
}
$version_string = '';
$network_string = '';
if ( strstr( $plugin['url'], 'woothemes.com' ) || strstr( $plugin['url'], 'woocommerce.com' ) ) {
if ( ! empty( $plugin['version_latest'] ) && version_compare( $plugin['version_latest'], $plugin['version'], '>' ) ) {
/* translators: %s: plugin latest version */
$version_string = ' &ndash; <strong style="color:red;">' . sprintf( esc_html__( '%s is available', 'woocommerce' ), $plugin['version_latest'] ) . '</strong>';
}
if ( false !== $plugin['network_activated'] ) {
$network_string = ' &ndash; <strong style="color:black;">' . esc_html__( 'Network enabled', 'woocommerce' ) . '</strong>';
}
}
$untested_string = '';
if ( array_key_exists( $plugin['plugin'], $untested_plugins ) ) {
$untested_string = ' &ndash; <strong style="color:red;">' . esc_html__( 'Not tested with the active version of WooCommerce', 'woocommerce' ) . '</strong>';
}
?>
<tr>
<td><?php echo wp_kses_post( $plugin_name ); ?></td>
<td class="help">&nbsp;</td>
<td>
<?php
/* translators: %s: plugin author */
printf( esc_html__( 'by %s', 'woocommerce' ), esc_html( $plugin['author_name'] ) );
echo ' &ndash; ' . esc_html( $plugin['version'] ) . $version_string . $untested_string . $network_string; // WPCS: XSS ok.
?>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>

View File

@ -341,6 +341,15 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
'type' => 'string',
),
),
'inactive_plugins' => array(
'description' => __( 'Inactive plugins.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view' ),
'readonly' => true,
'items' => array(
'type' => 'string',
),
),
'theme' => array(
'description' => __( 'Theme.', 'woocommerce' ),
'type' => 'object',
@ -548,13 +557,14 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
*/
public function get_item_mappings() {
return array(
'environment' => $this->get_environment_info(),
'database' => $this->get_database_info(),
'active_plugins' => $this->get_active_plugins(),
'theme' => $this->get_theme_info(),
'settings' => $this->get_settings(),
'security' => $this->get_security_info(),
'pages' => $this->get_pages(),
'environment' => $this->get_environment_info(),
'database' => $this->get_database_info(),
'active_plugins' => $this->get_active_plugins(),
'inactive_plugins' => $this->get_inactive_plugins(),
'theme' => $this->get_theme_info(),
'settings' => $this->get_settings(),
'security' => $this->get_security_info(),
'pages' => $this->get_pages(),
);
}
@ -771,14 +781,6 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
* @return array
*/
public function get_active_plugins() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
require_once ABSPATH . 'wp-admin/includes/update.php';
if ( ! function_exists( 'get_plugin_updates' ) ) {
return array();
}
// Get both site plugins and network plugins.
$active_plugins = (array) get_option( 'active_plugins', array() );
if ( is_multisite() ) {
$network_activated_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
@ -786,83 +788,135 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
}
$active_plugins_data = array();
$available_updates = get_plugin_updates();
foreach ( $active_plugins as $plugin ) {
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
$dirname = dirname( $plugin );
$version_latest = '';
$slug = explode( '/', $plugin );
$slug = explode( '.', end( $slug ) );
$slug = $slug[0];
if ( 'woocommerce' !== $slug && ( strstr( $data['PluginURI'], 'woothemes.com' ) || strstr( $data['PluginURI'], 'woocommerce.com' ) ) ) {
$version_data = get_transient( md5( $plugin ) . '_version_data' );
if ( false === $version_data ) {
$changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $dirname . '/changelog.txt' );
if ( 200 === wp_remote_retrieve_response_code( $changelog ) ) {
$cl_lines = explode( "\n", wp_remote_retrieve_body( $changelog ) );
if ( ! empty( $cl_lines ) ) {
foreach ( $cl_lines as $line_num => $cl_line ) {
if ( preg_match( '/^[0-9]/', $cl_line ) ) {
$date = str_replace( '.', '-', trim( substr( $cl_line, 0, strpos( $cl_line, '-' ) ) ) );
$version = preg_replace( '~[^0-9,.]~', '', stristr( $cl_line, 'version' ) );
$update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) );
$version_data = array(
'date' => $date,
'version' => $version,
'update' => $update,
'changelog' => $changelog,
);
set_transient( md5( $plugin ) . '_version_data', $version_data, DAY_IN_SECONDS );
break;
}
}
}
} else {
$args = (object) array(
'slug' => $dirname,
);
$request = array(
'action' => 'plugin_information',
'request' => serialize( $args ),
);
$plugin_info = wp_safe_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $request ) );
if ( 200 === wp_remote_retrieve_response_code( $plugin_info ) ) {
$body = maybe_unserialize( wp_remote_retrieve_body( $plugin_info ) );
if ( is_object( $body ) && isset( $body->sections['changelog'] ) ) {
$version_data = array(
'date' => $body->last_updated,
'version' => $body->version,
'update' => $body->sections['changelog'],
'changelog' => $body->sections['changelog'],
);
set_transient( md5( $plugin ) . '_version_data', $version_data, DAY_IN_SECONDS );
}
}
}
}
$version_latest = $version_data['version'];
} elseif ( isset( $available_updates[ $plugin ]->update->new_version ) ) {
$version_latest = $available_updates[ $plugin ]->update->new_version;
}
// convert plugin data to json response format.
$active_plugins_data[] = array(
'plugin' => $plugin,
'name' => $data['Name'],
'version' => $data['Version'],
'version_latest' => $version_latest,
'url' => $data['PluginURI'],
'author_name' => $data['AuthorName'],
'author_url' => esc_url_raw( $data['AuthorURI'] ),
'network_activated' => $data['Network'],
);
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
$active_plugins_data[] = $this->format_plugin_data( $plugin, $data );
}
return $active_plugins_data;
}
/**
* Get a list of inplugins active on the site.
*
* @return array
*/
public function get_inactive_plugins() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( ! function_exists( 'get_plugins' ) ) {
return array();
}
$plugins = get_plugins();
$active_plugins = (array) get_option( 'active_plugins', array() );
if ( is_multisite() ) {
$network_activated_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
$active_plugins = array_merge( $active_plugins, $network_activated_plugins );
}
$plugins_data = array();
foreach ( $plugins as $plugin => $data ) {
if ( in_array( $plugin, $active_plugins, true ) ) {
continue;
}
$plugins_data[] = $this->format_plugin_data( $plugin, $data );
}
return $plugins_data;
}
/**
* Format plugin data, including data on updates, into a standard format.
*
* @since 3.6.0
* @param string $plugin Plugin directory/file.
* @param array $data Plugin data from WP.
* @return array Formatted data.
*/
protected function format_plugin_data( $plugin, $data ) {
require_once ABSPATH . 'wp-admin/includes/update.php';
if ( ! function_exists( 'get_plugin_updates' ) ) {
return array();
}
if ( empty( $this->available_updates ) ) {
$this->available_updates = get_plugin_updates();
}
$dirname = dirname( $plugin );
$version_latest = '';
$slug = explode( '/', $plugin );
$slug = explode( '.', end( $slug ) );
$slug = $slug[0];
if ( 'woocommerce' !== $slug && ( strstr( $data['PluginURI'], 'woothemes.com' ) || strstr( $data['PluginURI'], 'woocommerce.com' ) ) ) {
$version_data = get_transient( md5( $plugin ) . '_version_data' );
if ( false === $version_data ) {
$changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $dirname . '/changelog.txt' );
if ( 200 === wp_remote_retrieve_response_code( $changelog ) ) {
$cl_lines = explode( "\n", wp_remote_retrieve_body( $changelog ) );
if ( ! empty( $cl_lines ) ) {
foreach ( $cl_lines as $line_num => $cl_line ) {
if ( preg_match( '/^[0-9]/', $cl_line ) ) {
$date = str_replace( '.', '-', trim( substr( $cl_line, 0, strpos( $cl_line, '-' ) ) ) );
$version = preg_replace( '~[^0-9,.]~', '', stristr( $cl_line, 'version' ) );
$update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) );
$version_data = array(
'date' => $date,
'version' => $version,
'update' => $update,
'changelog' => $changelog,
);
set_transient( md5( $plugin ) . '_version_data', $version_data, DAY_IN_SECONDS );
break;
}
}
}
} else {
$args = (object) array(
'slug' => $dirname,
);
$request = array(
'action' => 'plugin_information',
'request' => serialize( $args ),
);
$plugin_info = wp_safe_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $request ) );
if ( 200 === wp_remote_retrieve_response_code( $plugin_info ) ) {
$body = maybe_unserialize( wp_remote_retrieve_body( $plugin_info ) );
if ( is_object( $body ) && isset( $body->sections['changelog'] ) ) {
$version_data = array(
'date' => $body->last_updated,
'version' => $body->version,
'update' => $body->sections['changelog'],
'changelog' => $body->sections['changelog'],
);
set_transient( md5( $plugin ) . '_version_data', $version_data, DAY_IN_SECONDS );
}
}
}
}
$version_latest = $version_data['version'];
} elseif ( isset( $available_updates[ $plugin ]->update->new_version ) ) {
$version_latest = $available_updates[ $plugin ]->update->new_version;
}
return array(
'plugin' => $plugin,
'name' => $data['Name'],
'version' => $data['Version'],
'version_latest' => $version_latest,
'url' => $data['PluginURI'],
'author_name' => $data['AuthorName'],
'author_url' => esc_url_raw( $data['AuthorURI'] ),
'network_activated' => $data['Network'],
);
}
/**
* Get info on the current active theme, info on parent theme (if presnet)
* and a list of template overrides.