| 1 |
<?php |
| 2 |
namespace Elementor\Modules\ProInstall; |
| 3 |
|
| 4 |
class Plugin_Installer { |
| 5 |
|
| 6 |
private $plugin_slug; |
| 7 |
private $package_url; |
| 8 |
|
| 9 |
public function __construct( $plugin_slug, $package_url = null ) { |
| 10 |
$this->plugin_slug = $plugin_slug; |
| 11 |
$this->package_url = $package_url; |
| 12 |
} |
| 13 |
|
| 14 |
public function install() { |
| 15 |
$this->includes_dependencies(); |
| 16 |
|
| 17 |
$plugin_data = $this->get_plugin_path(); |
| 18 |
if ( empty( $plugin_data ) ) { |
| 19 |
$install_result = $this->do_install(); |
| 20 |
if ( null === $install_result || is_wp_error( $install_result ) ) { |
| 21 |
return new \WP_Error( 'cant_installed', esc_html__( 'There are no available subscriptions at the moment.', 'elementor' ) ); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
$is_activated = $this->activate(); |
| 26 |
if ( is_wp_error( $is_activated ) ) { |
| 27 |
return $is_activated; |
| 28 |
} |
| 29 |
|
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
private function get_package_url() { |
| 34 |
return $this->package_url; |
| 35 |
} |
| 36 |
|
| 37 |
private function includes_dependencies() { |
| 38 |
include_once ABSPATH . '/wp-admin/includes/admin.php'; |
| 39 |
include_once ABSPATH . '/wp-admin/includes/plugin-install.php'; |
| 40 |
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 41 |
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; |
| 42 |
include_once ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php'; |
| 43 |
} |
| 44 |
|
| 45 |
private function do_install() { |
| 46 |
$package_url = $this->get_package_url(); |
| 47 |
if ( empty( $package_url ) ) { |
| 48 |
return new \WP_Error( 'no_package_url', sprintf( 'The requested plugin `%s` has no package URL', $this->plugin_slug ) ); |
| 49 |
} |
| 50 |
|
| 51 |
$upgrader = new \Plugin_Upgrader( new \Automatic_Upgrader_Skin() ); |
| 52 |
|
| 53 |
return $upgrader->install( $package_url ); |
| 54 |
} |
| 55 |
|
| 56 |
private function get_plugin_path() { |
| 57 |
$plugins = get_plugins(); |
| 58 |
$installed_plugins = []; |
| 59 |
|
| 60 |
foreach ( $plugins as $path => $plugin ) { |
| 61 |
$path_parts = explode( '/', $path ); |
| 62 |
$slug = $path_parts[0]; |
| 63 |
$installed_plugins[ $slug ] = $path; |
| 64 |
} |
| 65 |
|
| 66 |
if ( empty( $installed_plugins[ $this->plugin_slug ] ) ) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
return $installed_plugins[ $this->plugin_slug ]; |
| 71 |
} |
| 72 |
|
| 73 |
public function activate() { |
| 74 |
$plugin_path = $this->get_plugin_path(); |
| 75 |
|
| 76 |
if ( ! $plugin_path ) { |
| 77 |
return new \WP_Error( 'no_installed', sprintf( 'The requested plugin `%s` is not installed', $this->plugin_slug ) ); |
| 78 |
} |
| 79 |
|
| 80 |
$activate_result = activate_plugin( $plugin_path ); |
| 81 |
|
| 82 |
if ( is_wp_error( $activate_result ) ) { |
| 83 |
return $activate_result; |
| 84 |
} |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
|