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