| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\Onboarding\Data\Endpoints; |
| 4 |
|
| 5 |
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base; |
| 6 |
use Elementor\Modules\ProInstall\Plugin_Installer; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Utils; |
| 9 |
use WP_REST_Server; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Install_Pro extends Endpoint_Base { |
| 16 |
|
| 17 |
public function get_name(): string { |
| 18 |
return 'install-pro'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_format(): string { |
| 22 |
return 'onboarding'; |
| 23 |
} |
| 24 |
|
| 25 |
protected function register(): void { |
| 26 |
parent::register(); |
| 27 |
|
| 28 |
$this->register_items_route( WP_REST_Server::CREATABLE ); |
| 29 |
} |
| 30 |
|
| 31 |
public function create_items( $request ) { |
| 32 |
if ( Utils::has_pro() || Utils::is_pro_installed_and_not_active() ) { |
| 33 |
return [ |
| 34 |
'data' => [ |
| 35 |
'success' => true, |
| 36 |
'message' => 'already_installed', |
| 37 |
], |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
$connect = Plugin::$instance->common->get_component( 'connect' ); |
| 42 |
|
| 43 |
if ( ! $connect ) { |
| 44 |
return new \WP_Error( |
| 45 |
'connect_unavailable', |
| 46 |
__( 'Connect module is not available.', 'elementor' ), |
| 47 |
[ 'status' => 500 ] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
$pro_install_app = $connect->get_app( 'pro-install' ); |
| 52 |
|
| 53 |
if ( ! $pro_install_app || ! $pro_install_app->is_connected() ) { |
| 54 |
return new \WP_Error( |
| 55 |
'not_connected', |
| 56 |
__( 'You must be connected to install Elementor Pro.', 'elementor' ), |
| 57 |
[ 'status' => 400 ] |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
$download_link = $pro_install_app->get_download_link(); |
| 62 |
|
| 63 |
if ( empty( $download_link ) ) { |
| 64 |
return new \WP_Error( |
| 65 |
'no_subscription', |
| 66 |
__( 'There are no available subscriptions at the moment.', 'elementor' ), |
| 67 |
[ 'status' => 400 ] |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
$plugin_installer = new Plugin_Installer( 'elementor-pro', $download_link ); |
| 72 |
$result = $plugin_installer->install(); |
| 73 |
|
| 74 |
if ( is_wp_error( $result ) ) { |
| 75 |
return new \WP_Error( |
| 76 |
'install_failed', |
| 77 |
$result->get_error_message(), |
| 78 |
[ 'status' => 500 ] |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
return [ |
| 83 |
'data' => [ |
| 84 |
'success' => true, |
| 85 |
'message' => 'installed', |
| 86 |
], |
| 87 |
]; |
| 88 |
} |
| 89 |
} |
| 90 |
|