diff options
Diffstat (limited to 'plugins/jetpack/_inc/lib/plugins.php')
-rw-r--r-- | plugins/jetpack/_inc/lib/plugins.php | 103 |
1 files changed, 88 insertions, 15 deletions
diff --git a/plugins/jetpack/_inc/lib/plugins.php b/plugins/jetpack/_inc/lib/plugins.php index 9c8e3bc4..0bffc529 100644 --- a/plugins/jetpack/_inc/lib/plugins.php +++ b/plugins/jetpack/_inc/lib/plugins.php @@ -1,4 +1,4 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName /** * Plugins Library * @@ -9,8 +9,13 @@ * @autounit api plugins */ -include_once( 'class.jetpack-automatic-install-skin.php' ); +require_once 'class.jetpack-automatic-install-skin.php'; +use Automattic\Jetpack\A8c_Mc_Stats; + +/** + * Plugins management tools. + */ class Jetpack_Plugins { /** @@ -31,14 +36,13 @@ class Jetpack_Plugins { return $installed; } $plugin_id = self::get_plugin_id_by_slug( $slug ); - } else if ( is_plugin_active( $plugin_id ) ) { - return true; // Already installed and active + } elseif ( is_plugin_active( $plugin_id ) ) { + return true; // Already installed and active. } if ( ! current_user_can( 'activate_plugins' ) ) { return new WP_Error( 'not_allowed', __( 'You are not allowed to activate plugins on this site.', 'jetpack' ) ); } - $activated = activate_plugin( $plugin_id ); if ( is_wp_error( $activated ) ) { return $activated; @@ -64,23 +68,25 @@ class Jetpack_Plugins { $skin = new Jetpack_Automatic_Install_Skin(); $upgrader = new Plugin_Upgrader( $skin ); $zip_url = self::generate_wordpress_org_plugin_download_link( $slug ); + $mc_stats = new A8c_Mc_Stats(); $result = $upgrader->install( $zip_url ); if ( is_wp_error( $result ) ) { - return $result; + $mc_stats->add( 'install-plugin', "fail-$slug" ); + return $result; } - $plugin = Jetpack_Plugins::get_plugin_id_by_slug( $slug ); + $plugin = self::get_plugin_id_by_slug( $slug ); $error_code = 'install_error'; if ( ! $plugin ) { - $error = __( 'There was an error installing your plugin', 'jetpack' ); + $error = __( 'There was an error installing your plugin', 'jetpack' ); } if ( ! $result ) { - $error_code = $upgrader->skin->get_main_error_code(); - $message = $upgrader->skin->get_main_error_message(); - $error = $message ? $message : __( 'An unknown error occurred during installation', 'jetpack' ); + $error_code = $upgrader->skin->get_main_error_code(); + $message = $upgrader->skin->get_main_error_message(); + $error = $message ? $message : __( 'An unknown error occurred during installation', 'jetpack' ); } if ( ! empty( $error ) ) { @@ -89,17 +95,29 @@ class Jetpack_Plugins { $error_code = 'no_package'; } + $mc_stats->add( 'install-plugin', "fail-$slug" ); return new WP_Error( $error_code, $error, 400 ); } + $mc_stats->add( 'install-plugin', "success-$slug" ); return (array) $upgrader->skin->get_upgrade_messages(); } - protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) { + /** + * Get WordPress.org zip download link from a plugin slug + * + * @param string $plugin_slug Plugin slug. + */ + protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) { return "https://downloads.wordpress.org/plugin/$plugin_slug.latest-stable.zip"; - } + } - public static function get_plugin_id_by_slug( $slug ) { + /** + * Get the plugin ID (composed of the plugin slug and the name of the main plugin file) from a plugin slug. + * + * @param string $slug Plugin slug. + */ + public static function get_plugin_id_by_slug( $slug ) { // Check if get_plugins() function exists. This is required on the front end of the // site, since it is in a file that is normally only loaded in the admin. if ( ! function_exists( 'get_plugins' ) ) { @@ -111,6 +129,7 @@ class Jetpack_Plugins { if ( ! is_array( $plugins ) ) { return false; } + foreach ( $plugins as $plugin_file => $plugin_data ) { if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) { return $plugin_file; @@ -120,13 +139,67 @@ class Jetpack_Plugins { return false; } + /** + * Get the plugin slug from the plugin ID (composed of the plugin slug and the name of the main plugin file) + * + * @param string $plugin_file Plugin file (ID -- e.g. hello-dolly/hello.php). + */ protected static function get_slug_from_file_path( $plugin_file ) { // Similar to get_plugin_slug() method. $slug = dirname( $plugin_file ); if ( '.' === $slug ) { - $slug = preg_replace( "/(.+)\.php$/", "$1", $plugin_file ); + $slug = preg_replace( '/(.+)\.php$/', '$1', $plugin_file ); } return $slug; } + + /** + * Get the activation status for a plugin. + * + * @since 8.9.0 + * + * @param string $plugin_file The plugin file to check. + * @return string Either 'network-active', 'active' or 'inactive'. + */ + public static function get_plugin_status( $plugin_file ) { + if ( is_plugin_active_for_network( $plugin_file ) ) { + return 'network-active'; + } + + if ( is_plugin_active( $plugin_file ) ) { + return 'active'; + } + + return 'inactive'; + } + + /** + * Returns a list of all plugins in the site. + * + * @since 8.9.0 + * @uses get_plugins() + * + * @return array + */ + public static function get_plugins() { + if ( ! function_exists( 'get_plugins' ) ) { + require_once ABSPATH . 'wp-admin/includes/plugin.php'; + } + /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ + $plugins = apply_filters( 'all_plugins', get_plugins() ); + + if ( is_array( $plugins ) && ! empty( $plugins ) ) { + foreach ( $plugins as $plugin_slug => $plugin_data ) { + $plugins[ $plugin_slug ]['active'] = in_array( + self::get_plugin_status( $plugin_slug ), + array( 'active', 'network-active' ), + true + ); + } + return $plugins; + } + + return array(); + } } |