plugin-installer.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Utils\Onboard; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | class Plugin_Installer { |
| 8 | |
| 9 | private $plugin_file; |
| 10 | private $plugin_slug; |
| 11 | |
| 12 | public function __construct( $plugin_file ) { |
| 13 | $this->plugin_file = $plugin_file; |
| 14 | $this->plugin_slug = dirname( $plugin_file ); |
| 15 | $this->initialize_filesystem(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Install and activate a plugin. |
| 20 | */ |
| 21 | public function install_and_activate() { |
| 22 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 23 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 24 | } |
| 25 | |
| 26 | if ( is_plugin_active( $this->plugin_file ) ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if ( $this->is_plugin_installed() ) { |
| 31 | $ignore_silent_activation = ['popup-builder-block/popup-builder-block.php']; |
| 32 | $silent = in_array( $this->plugin_file, $ignore_silent_activation ) ? false : true; |
| 33 | |
| 34 | $activate = activate_plugin( $this->plugin_file, '', false, $silent ); |
| 35 | if ( ! is_wp_error( $activate ) ) { |
| 36 | return true; |
| 37 | } |
| 38 | } else { |
| 39 | $this->install_plugin(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Install the plugin using WP_Upgrader. |
| 45 | */ |
| 46 | private function install_plugin() { |
| 47 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 48 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 49 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 50 | |
| 51 | $plugin_info = $this->get_plugin_information( $this->plugin_slug ); |
| 52 | |
| 53 | if ( is_wp_error( $plugin_info ) || ! $plugin_info || empty( $plugin_info->download_link ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | $skin = new \ShopEngine\Utils\Onboard\Plugin_Skin(); |
| 58 | |
| 59 | $upgrader = new \Plugin_Upgrader( $skin ); |
| 60 | $upgrader->install( $plugin_info->download_link ); |
| 61 | |
| 62 | if ( $this->is_plugin_installed() ) { |
| 63 | $ignore_silent_activation = ['popup-builder-block/popup-builder-block.php']; |
| 64 | $silent = in_array( $this->plugin_file, $ignore_silent_activation ) ? false : true; |
| 65 | |
| 66 | $activate = activate_plugin( $this->plugin_file, '', false, $silent ); |
| 67 | if ( ! is_wp_error( $activate ) ) { |
| 68 | return true; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get plugin info from WordPress.org. |
| 75 | */ |
| 76 | private function get_plugin_information( $plugin_slug ) { |
| 77 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 78 | |
| 79 | $api = plugins_api( 'plugin_information', [ |
| 80 | 'slug' => $plugin_slug, |
| 81 | 'fields' => [ |
| 82 | 'download_link' => true, |
| 83 | ], |
| 84 | ] ); |
| 85 | |
| 86 | return $api; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if plugin is installed. |
| 91 | */ |
| 92 | private function is_plugin_installed() { |
| 93 | global $wp_filesystem; |
| 94 | return $wp_filesystem->exists( WP_PLUGIN_DIR . '/' . $this->plugin_file ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Init WordPress Filesystem API. |
| 99 | */ |
| 100 | private function initialize_filesystem() { |
| 101 | global $wp_filesystem; |
| 102 | |
| 103 | if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 104 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 105 | } |
| 106 | |
| 107 | WP_Filesystem(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Static method to bulk install/activate multiple plugins. |
| 112 | */ |
| 113 | public static function single_install_and_activate( string $plugin_file ) { |
| 114 | $installer = new self( $plugin_file ); |
| 115 | return $installer->install_and_activate(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Static method to bulk install/activate multiple plugins. |
| 120 | */ |
| 121 | public static function bulk_install_and_activate( array $plugin_files ) { |
| 122 | foreach ( $plugin_files as $plugin_file ) { |
| 123 | $installer = new self( $plugin_file ); |
| 124 | $installer->install_and_activate(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |