esc_html__( 'Tried to install a zip or slug that was not in the allowed list', 'wp-security-audit-log' ), ) ); } // Check if the plugin is installed. if ( self::is_plugin_installed( $plugin_slug ) ) { // If plugin is installed but not active, activate it. if ( ! WP_Helper::is_plugin_active( $plugin_zip ) ) { self::run_activate( $plugin_slug ); self::activate( $plugin_zip ); $result = 'activated'; } else { $result = 'already_installed'; } } else { // No plugin found or plugin not present to be activated, so lets install it. self::install_plugin( $plugin_zip ); self::run_activate( $plugin_slug ); self::activate( $plugin_zip ); $result = 'success'; } // If we're installing our helper plugin, we also need to delete the nudge to install the helper plugin. if ( $helper_plugin_installation ) { \WSAL\Helpers\Settings_Helper::delete_option_value( 'show-helper-plugin-needed-nudge' ); } \wp_send_json( $result ); } /** * Install a plugin given a slug. * * @param string $plugin_zip URL to the direct zip file. * * @since 4.0.1 */ public static function install_plugin( $plugin_zip = '' ) { // bail early if we don't have a slug to work with. if ( empty( $plugin_zip ) ) { return; } // get the core plugin upgrader if not already in the runtime. if ( ! class_exists( 'Plugin_Upgrader' ) ) { include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; } // clear the cache so we're using fresh data. \wp_cache_flush(); $upgrader = new \Plugin_Upgrader(); $install_result = $upgrader->install( $plugin_zip ); if ( ! $install_result || is_wp_error( $install_result ) ) { if ( is_wp_error( $install_result ) ) { return $install_result->get_error_message(); } die(); } } /** * Activates a plugin that is available on the site. * * @param string $plugin_zip URL to the direct zip file. * * @return void * * @since 4.0.1 */ public static function activate( $plugin_zip = '' ) { // bail early if we don't have a slug to work with. if ( empty( $plugin_zip ) ) { return; } if ( WP_Helper::is_multisite() ) { return; } // get core plugin functions if they are not already in runtime. if ( ! function_exists( 'activate_plugin' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( ! WP_Helper::is_plugin_active( $plugin_zip ) ) { activate_plugin( $plugin_zip ); } } /** * Deactivate plugin by given slug * * @param string $plugin - The slug of the plugin to deactivate. * * @return boolean * * @since 4.6.0 */ public static function deactivate_plugin( string $plugin ): bool { if ( ! function_exists( 'deactivate_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( WP_Helper::is_plugin_active( $plugin ) ) { $network_wide = false; // Set to true if you want to deactivate the plugin network-wide (for multisite). if ( WP_Helper::is_multisite() ) { $network_wide = true; } $result = \deactivate_plugins( $plugin, false, $network_wide ); // Check if the plugin was deactivated. if ( \is_wp_error( $result ) ) { return false; } } return true; } /** * Activates a plugin that is available on the site. * * @method run_activate * @since 4.0.1 * @param string $plugin_slug slug for plugin. */ public static function run_activate( $plugin_slug = '' ) { // bail early if we don't have a slug to work with. if ( empty( $plugin_slug ) ) { return; } if ( WP_Helper::is_multisite() ) { $current = get_site_option( 'active_sitewide_plugins' ); } else { $current = get_option( 'active_plugins' ); } if ( ! in_array( $plugin_slug, $current, true ) ) { if ( WP_Helper::is_multisite() ) { $current[] = $plugin_slug; \activate_plugin( $plugin_slug, '', true ); } else { $current[] = $plugin_slug; \activate_plugin( $plugin_slug ); } } return null; } /** * Check if a plugin is installed. * * @param string $plugin_slug slug for plugin. * * @since 4.0.1 */ public static function is_plugin_installed( $plugin_slug = '' ) { // bail early if we don't have a slug to work with. if ( empty( $plugin_slug ) ) { return; } // get core plugin functions if not already in the runtime. if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $all_plugins = \get_plugins(); // true if plugin is already installed or false if not. if ( ! empty( $all_plugins[ $plugin_slug ] ) ) { return true; } else { return false; } } } }