* Helper class for the site's plugins.
namespace Automattic\WooCommerce\Admin;
use ActionScheduler_DBStore;
use ActionScheduler_QueueRunner;
use Automatic_Upgrader_Skin;
use Automattic\WooCommerce\Admin\PluginsInstallLoggers\AsyncPluginsInstallLogger;
use Automattic\WooCommerce\Admin\PluginsInstallLoggers\PluginsInstallLogger;
use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
use Automattic\WooCommerce\Utilities\PluginUtil;
defined( 'ABSPATH' ) || exit;
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
* Subscription notices in Woo screens are shown in clear priority order, first
* expired, and if those don't exist, expiring, and finally if none of those exist,
* then missing. This keeps track of whether we can show the next set of notices.
public static $subscription_usage_notices_already_shown = false;
* The URL for the WooCommerce subscription page.
const WOO_SUBSCRIPTION_PAGE_URL = 'https://woocommerce.com/my-account/my-subscriptions/';
* The URL for the WooCommerce.com cart page.
const WOO_CART_PAGE_URL = 'https://woocommerce.com/cart/';
* The URL for the WooCommerce.com add payment method page.
const WOO_ADD_PAYMENT_METHOD_URL = 'https://woocommerce.com/my-account/add-payment-method/';
* Meta key for dismissing expired subscription notices.
const DISMISS_EXPIRED_SUBS_NOTICE = 'woo_subscription_expired_notice_dismiss';
* Meta key for dismissing expiring subscription notices
const DISMISS_EXPIRING_SUBS_NOTICE = 'woo_subscription_expiring_notice_dismiss';
* Meta key for dismissing missing subscription notices
const DISMISS_MISSING_SUBS_NOTICE = 'woo_subscription_missing_notice_dismiss';
* Meta key for dismissing disconnected notice
const DISMISS_DISCONNECT_NOTICE = 'woo_disconnect_notice_dismiss';
* Meta key for dismissing connected notice
const DISMISS_CONNECT_NOTICE = 'woo_connect_notice_dismiss';
public static function init() {
add_action( 'woocommerce_plugins_install_callback', array( __CLASS__, 'install_plugins' ), 10, 2 );
add_action( 'woocommerce_plugins_install_and_activate_async_callback', array( __CLASS__, 'install_and_activate_plugins_async_callback' ), 10, 3 );
add_action( 'woocommerce_plugins_activate_callback', array( __CLASS__, 'activate_plugins' ), 10, 2 );
add_action( 'admin_notices', array( __CLASS__, 'maybe_show_connect_notice_in_plugin_list' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_scripts_for_connect_notice' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_scripts_for_notices_in_plugins' ) );
* Get the path to the plugin file relative to the plugins directory from the plugin slug.
* E.g. 'woocommerce' returns 'woocommerce/woocommerce.php'
* @param string $slug Plugin slug to get path for.
* @return string|false The plugin path or false if the plugin is not installed.
public static function get_plugin_path_from_slug( $slug ) {
$plugins = get_plugins();
if ( strstr( $slug, '/' ) ) {
// The slug is already a plugin path.
foreach ( $plugins as $plugin_path => $data ) {
$path_parts = explode( '/', $plugin_path );
if ( $path_parts[0] === $slug ) {
* Get an array of installed plugin slugs.
public static function get_installed_plugin_slugs() {
function ( $plugin_path ) {
$path_parts = explode( '/', $plugin_path );
array_keys( get_plugins() )
* Get an array of installed plugins with their file paths as a key value pair.
public static function get_installed_plugins_paths() {
$plugins = get_plugins();
$installed_plugins = array();
foreach ( $plugins as $path => $plugin ) {
$path_parts = explode( '/', $path );
$installed_plugins[ $slug ] = $path;
return $installed_plugins;
* Get an array of active plugin slugs.
* The list will include both network active and site active plugins.
* @return array The list of active plugin slugs.
public static function get_active_plugin_slugs() {
function ( $absolute_path ) {
// Make the path relative to the plugins directory.
$plugin_path = str_replace( WP_PLUGIN_DIR . '/', '', $absolute_path );
// Split the path to get the plugin slug (aka the directory name).
$path_parts = explode( '/', $plugin_path );
// Use this method as it is the most bulletproof way to get the active plugins.
wc_get_container()->get( PluginUtil::class )->get_all_active_valid_plugins()
* Checks if a plugin is installed.
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
public static function is_plugin_installed( $plugin ) {
$plugin_path = self::get_plugin_path_from_slug( $plugin );
return $plugin_path ? array_key_exists( $plugin_path, get_plugins() ) : false;
* Checks if a plugin is active.
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
public static function is_plugin_active( $plugin ) {
$plugin_path = self::get_plugin_path_from_slug( $plugin );
return $plugin_path && \is_plugin_active( $plugin_path );
* @param string $plugin Path to the plugin file relative to the plugins directory or the plugin directory name.
public static function get_plugin_data( $plugin ) {
$plugin_path = self::get_plugin_path_from_slug( $plugin );
$plugins = get_plugins();
return isset( $plugins[ $plugin_path ] ) ? $plugins[ $plugin_path ] : false;
* Install an array of plugins.
* @param array $plugins Plugins to install.
* @param PluginsInstallLogger|null $logger an optional logger.
* @param string|null $source place where the request is coming from.
public static function install_plugins( $plugins, ?PluginsInstallLogger $logger = null, ?string $source = null ) {
* Filter the list of plugins to install.
* @param array $plugins A list of the plugins to install.
$plugins = apply_filters( 'woocommerce_admin_plugins_pre_install', $plugins );
if ( empty( $plugins ) || ! is_array( $plugins ) ) {
'woocommerce_plugins_invalid_plugins',
__( 'Plugins must be a non-empty array.', 'woocommerce' )
require_once ABSPATH . 'wp-admin/includes/plugin.php';
include_once ABSPATH . '/wp-admin/includes/admin.php';
include_once ABSPATH . '/wp-admin/includes/plugin-install.php';
include_once ABSPATH . '/wp-admin/includes/plugin.php';
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php';
$existing_plugins = self::get_installed_plugins_paths();
$installed_plugins = array();
$errors = new WP_Error();
$install_start_time = time();
foreach ( $plugins as $plugin ) {
$slug = sanitize_key( $plugin );
$logger && $logger->install_requested( $plugin );
if ( isset( $existing_plugins[ $slug ] ) ) {
$installed_plugins[] = $plugin;
$logger && $logger->installed( $plugin, 0 );
$start_time = microtime( true );
if ( is_wp_error( $api ) ) {
'error_message' => sprintf(
// translators: %s: plugin slug (example: woocommerce-services).
'The requested plugin `%s` could not be installed. Plugin API call failed.',
'api_error_message' => $api->get_error_message(),
wc_admin_record_tracks_event( 'install_plugin_error', $properties );
* Action triggered when a plugin API call failed.
* @param string $slug The plugin slug.
* @param WP_Error $api The API response.
do_action( 'woocommerce_plugins_install_api_error', $slug, $api );
$error_message = sprintf(
/* translators: %s: plugin slug (example: woocommerce-services) */
__( 'The requested plugin `%s` could not be installed. Plugin API call failed.', 'woocommerce' ),
$errors->add( $plugin, $error_message );
$logger && $logger->add_error( $plugin, $error_message );
* Action triggered before a plugin is installed.
do_action( 'woocommerce_plugins_install_before', $slug, $source );
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
$result = $upgrader->install( $api->download_link );
// result can be false or WP_Error.
$results[ $plugin ] = $result;
$time[ $plugin ] = round( ( microtime( true ) - $start_time ) * 1000 );
if ( is_wp_error( $result ) || is_null( $result ) ) {
'error_message' => sprintf(
/* translators: %s: plugin slug (example: woocommerce-services) */
'The requested plugin `%s` could not be installed.',
'api_version' => $api->version,
'api_download_link' => $api->download_link,
'upgrader_skin_message' => implode( ',', $upgrader->skin->get_upgrade_messages() ),
'result' => is_wp_error( $result ) ? $result->get_error_message() : 'null',
wc_admin_record_tracks_event( 'install_plugin_error', $properties );
* Action triggered when a plugin installation fails.
* @param string $slug The plugin slug.
* @param object $api The plugin API object.
* @param WP_Error|null $result The result of the plugin installation.
* @param Plugin_Upgrader $upgrader The plugin upgrader.
do_action( 'woocommerce_plugins_install_error', $slug, $api, $result, $upgrader );
$install_error_message = sprintf(
/* translators: %s: plugin slug (example: woocommerce-services) */
__( 'The requested plugin `%s` could not be installed. Upgrader install failed.', 'woocommerce' ),
$logger && $logger->add_error( $plugin, $install_error_message );
$installed_plugins[] = $plugin;
$logger && $logger->installed( $plugin, $time[ $plugin ] );
* Action triggered after a plugin is installed.
do_action( 'woocommerce_plugins_install_after', $slug, $source );
'installed' => $installed_plugins,
$logger && $logger->complete( array_merge( $data, array( 'start_time' => $install_start_time ) ) );
* Callback registered by OnboardingPlugins::install_and_activate_async.
* It is used to call install_plugins and activate_plugins with a custom logger.
* @param array $plugins A list of plugins to install.
* @param string $job_id An unique job I.D.
* @param string|null $source The source of the request.
public static function install_and_activate_plugins_async_callback( array $plugins, string $job_id, ?string $source = null ) {
$option_name = 'woocommerce_onboarding_plugins_install_and_activate_async_' . $job_id;
$logger = new AsyncPluginsInstallLogger( $option_name );
self::install_plugins( $plugins, $logger, $source );
self::activate_plugins( $plugins, $logger );
* Schedule plugin installation.
* @param array $plugins Plugins to install.
public static function schedule_install_plugins( $plugins ) {
if ( empty( $plugins ) || ! is_array( $plugins ) ) {
'woocommerce_plugins_invalid_plugins',
__( 'Plugins must be a non-empty array.', 'woocommerce' ),
WC()->queue()->schedule_single( time() + 5, 'woocommerce_plugins_install_callback', array( $plugins ) );
* Activate the requested plugins.
* @param array $plugins Plugins.
* @param PluginsInstallLogger|null $logger Logger.
* @return WP_Error|array Plugin Status
public static function activate_plugins( $plugins, ?PluginsInstallLogger $logger = null ) {
if ( empty( $plugins ) || ! is_array( $plugins ) ) {
'woocommerce_plugins_invalid_plugins',
__( 'Plugins must be a non-empty array.', 'woocommerce' ),
require_once ABSPATH . 'wp-admin/includes/plugin.php';
// the mollie-payments-for-woocommerce plugin calls `WP_Filesystem()` during it's activation hook, which crashes without this include.
require_once ABSPATH . 'wp-admin/includes/file.php';
* Filter the list of plugins to activate.
* @param array $plugins A list of the plugins to activate.
$plugins = apply_filters( 'woocommerce_admin_plugins_pre_activate', $plugins );
$plugin_paths = self::get_installed_plugins_paths();
$errors = new WP_Error();
$activated_plugins = array();
foreach ( $plugins as $plugin ) {
$path = isset( $plugin_paths[ $slug ] ) ? $plugin_paths[ $slug ] : false;
/* translators: %s: plugin slug (example: woocommerce-services) */
$message = sprintf( __( 'The requested plugin `%s`. is not yet installed.', 'woocommerce' ), $slug );
$logger && $logger->add_error( $plugin, $message );
$result = activate_plugin( $path );
if ( ! is_plugin_active( $path ) ) {
* Action triggered when a plugin activation fails.
* @param string $slug The plugin slug.
* @param null|WP_Error $result The result of the plugin activation.