first commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\App\Modules\Onboarding\Data;
|
||||
|
||||
use Elementor\App\Modules\Onboarding\Data\Endpoints\Install_Pro;
|
||||
use Elementor\App\Modules\Onboarding\Data\Endpoints\Install_Theme;
|
||||
use Elementor\App\Modules\Onboarding\Data\Endpoints\Pro_Install_Screen;
|
||||
use Elementor\App\Modules\Onboarding\Data\Endpoints\User_Choices;
|
||||
use Elementor\App\Modules\Onboarding\Data\Endpoints\User_Progress;
|
||||
use Elementor\Data\V2\Base\Controller as Base_Controller;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Controller extends Base_Controller {
|
||||
|
||||
public function get_name(): string {
|
||||
return 'onboarding';
|
||||
}
|
||||
|
||||
public function register_endpoints(): void {
|
||||
$this->register_endpoint( new User_Progress( $this ) );
|
||||
$this->register_endpoint( new User_Choices( $this ) );
|
||||
$this->register_endpoint( new Pro_Install_Screen( $this ) );
|
||||
$this->register_endpoint( new Install_Pro( $this ) );
|
||||
$this->register_endpoint( new Install_Theme( $this ) );
|
||||
}
|
||||
|
||||
public function get_items_permissions_check( $request ) {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
public function get_item_permissions_check( $request ) {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
public function create_items_permissions_check( $request ) {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
public function create_item_permissions_check( $request ) {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
public function update_items_permissions_check( $request ) {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
public function update_item_permissions_check( $request ) {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\App\Modules\Onboarding\Data\Endpoints;
|
||||
|
||||
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base;
|
||||
use Elementor\Modules\ProInstall\Plugin_Installer;
|
||||
use Elementor\Plugin;
|
||||
use Elementor\Utils;
|
||||
use WP_REST_Server;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Install_Pro extends Endpoint_Base {
|
||||
|
||||
public function get_name(): string {
|
||||
return 'install-pro';
|
||||
}
|
||||
|
||||
public function get_format(): string {
|
||||
return 'onboarding';
|
||||
}
|
||||
|
||||
protected function register(): void {
|
||||
parent::register();
|
||||
|
||||
$this->register_items_route( WP_REST_Server::CREATABLE );
|
||||
}
|
||||
|
||||
public function create_items( $request ) {
|
||||
if ( Utils::has_pro() || Utils::is_pro_installed_and_not_active() ) {
|
||||
return [
|
||||
'data' => [
|
||||
'success' => true,
|
||||
'message' => 'already_installed',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$connect = Plugin::$instance->common->get_component( 'connect' );
|
||||
|
||||
if ( ! $connect ) {
|
||||
return new \WP_Error(
|
||||
'connect_unavailable',
|
||||
__( 'Connect module is not available.', 'elementor' ),
|
||||
[ 'status' => 500 ]
|
||||
);
|
||||
}
|
||||
|
||||
$pro_install_app = $connect->get_app( 'pro-install' );
|
||||
|
||||
if ( ! $pro_install_app || ! $pro_install_app->is_connected() ) {
|
||||
return new \WP_Error(
|
||||
'not_connected',
|
||||
__( 'You must be connected to install Elementor Pro.', 'elementor' ),
|
||||
[ 'status' => 400 ]
|
||||
);
|
||||
}
|
||||
|
||||
$download_link = $pro_install_app->get_download_link();
|
||||
|
||||
if ( empty( $download_link ) ) {
|
||||
return new \WP_Error(
|
||||
'no_subscription',
|
||||
__( 'There are no available subscriptions at the moment.', 'elementor' ),
|
||||
[ 'status' => 400 ]
|
||||
);
|
||||
}
|
||||
|
||||
$plugin_installer = new Plugin_Installer( 'elementor-pro', $download_link );
|
||||
$result = $plugin_installer->install();
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return new \WP_Error(
|
||||
'install_failed',
|
||||
$result->get_error_message(),
|
||||
[ 'status' => 500 ]
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
'data' => [
|
||||
'success' => true,
|
||||
'message' => 'installed',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\App\Modules\Onboarding\Data\Endpoints;
|
||||
|
||||
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base;
|
||||
use WP_REST_Server;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Install_Theme extends Endpoint_Base {
|
||||
|
||||
const ALLOWED_THEMES = [ 'hello-elementor', 'hello-biz' ];
|
||||
|
||||
public function get_name(): string {
|
||||
return 'install-theme';
|
||||
}
|
||||
|
||||
public function get_format(): string {
|
||||
return 'onboarding';
|
||||
}
|
||||
|
||||
protected function register(): void {
|
||||
parent::register();
|
||||
|
||||
$this->register_items_route( WP_REST_Server::CREATABLE );
|
||||
}
|
||||
|
||||
public function create_items( $request ) {
|
||||
$permission = $this->check_permission();
|
||||
if ( is_wp_error( $permission ) ) {
|
||||
return $permission;
|
||||
}
|
||||
|
||||
$params = $request->get_json_params();
|
||||
$theme_slug = $params['theme_slug'] ?? '';
|
||||
|
||||
if ( empty( $theme_slug ) || ! in_array( $theme_slug, self::ALLOWED_THEMES, true ) ) {
|
||||
return new \WP_Error(
|
||||
'invalid_theme',
|
||||
__( 'Invalid or unsupported theme.', 'elementor' ),
|
||||
[ 'status' => 400 ]
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'install_themes' ) || ! current_user_can( 'switch_themes' ) ) {
|
||||
return new \WP_Error(
|
||||
'insufficient_permissions',
|
||||
__( 'You do not have permission to install themes.', 'elementor' ),
|
||||
[ 'status' => 403 ]
|
||||
);
|
||||
}
|
||||
|
||||
$theme = wp_get_theme( $theme_slug );
|
||||
|
||||
if ( ! $theme->exists() ) {
|
||||
if ( ! function_exists( 'request_filesystem_credentials' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
}
|
||||
|
||||
if ( ! class_exists( '\Theme_Upgrader' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
|
||||
}
|
||||
|
||||
if ( ! class_exists( '\WP_Ajax_Upgrader_Skin' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
|
||||
}
|
||||
|
||||
$skin = new \WP_Ajax_Upgrader_Skin();
|
||||
$upgrader = new \Theme_Upgrader( $skin );
|
||||
$result = $upgrader->install( "https://downloads.wordpress.org/theme/{$theme_slug}.latest-stable.zip" );
|
||||
|
||||
if ( is_wp_error( $result ) || ! $result ) {
|
||||
return new \WP_Error(
|
||||
'theme_install_failed',
|
||||
__( 'Failed to install the theme.', 'elementor' ),
|
||||
[ 'status' => 500 ]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
switch_theme( $theme_slug );
|
||||
|
||||
return [
|
||||
'data' => [
|
||||
'success' => true,
|
||||
'message' => 'theme_installed',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function check_permission() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return new \WP_Error(
|
||||
'rest_forbidden',
|
||||
__( 'Sorry, you are not allowed to access onboarding data.', 'elementor' ),
|
||||
[ 'status' => 403 ]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\App\Modules\Onboarding\Data\Endpoints;
|
||||
|
||||
use Elementor\App\Modules\Onboarding\Module;
|
||||
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Pro_Install_Screen extends Endpoint_Base {
|
||||
|
||||
public function get_name(): string {
|
||||
return 'pro-install-screen';
|
||||
}
|
||||
|
||||
public function get_format(): string {
|
||||
return 'onboarding';
|
||||
}
|
||||
|
||||
protected function register(): void {
|
||||
parent::register();
|
||||
|
||||
$this->register_items_route();
|
||||
}
|
||||
|
||||
public function get_items( $request ) {
|
||||
return [
|
||||
'data' => [
|
||||
'shouldShowProInstallScreen' => Module::should_show_pro_install_screen(),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\App\Modules\Onboarding\Data\Endpoints;
|
||||
|
||||
use Elementor\App\Modules\Onboarding\Storage\Onboarding_Progress_Manager;
|
||||
use Elementor\App\Modules\Onboarding\Validation\User_Choices_Validator;
|
||||
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base;
|
||||
use WP_REST_Server;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class User_Choices extends Endpoint_Base {
|
||||
|
||||
public function get_name(): string {
|
||||
return 'user-choices';
|
||||
}
|
||||
|
||||
public function get_format(): string {
|
||||
return 'onboarding';
|
||||
}
|
||||
|
||||
protected function register(): void {
|
||||
parent::register();
|
||||
|
||||
$this->register_items_route( WP_REST_Server::EDITABLE );
|
||||
}
|
||||
|
||||
public function get_items( $request ) {
|
||||
$permission = $this->check_permission();
|
||||
if ( is_wp_error( $permission ) ) {
|
||||
return $permission;
|
||||
}
|
||||
|
||||
$manager = Onboarding_Progress_Manager::instance();
|
||||
$choices = $manager->get_choices();
|
||||
|
||||
return [
|
||||
'data' => $choices->to_array(),
|
||||
];
|
||||
}
|
||||
|
||||
public function update_items( $request ) {
|
||||
$permission = $this->check_permission();
|
||||
if ( is_wp_error( $permission ) ) {
|
||||
return $permission;
|
||||
}
|
||||
|
||||
$params = $request->get_json_params();
|
||||
|
||||
$validator = new User_Choices_Validator();
|
||||
$validated = $validator->validate( $params ?? [] );
|
||||
|
||||
if ( is_wp_error( $validated ) ) {
|
||||
return $validated;
|
||||
}
|
||||
|
||||
$manager = Onboarding_Progress_Manager::instance();
|
||||
$choices = $manager->update_choices( $validated );
|
||||
|
||||
return [
|
||||
'data' => 'success',
|
||||
'choices' => $choices->to_array(),
|
||||
];
|
||||
}
|
||||
|
||||
private function check_permission() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return new \WP_Error(
|
||||
'rest_forbidden',
|
||||
__( 'Sorry, you are not allowed to access onboarding data.', 'elementor' ),
|
||||
[ 'status' => 403 ]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\App\Modules\Onboarding\Data\Endpoints;
|
||||
|
||||
use Elementor\App\Modules\Onboarding\Module;
|
||||
use Elementor\App\Modules\Onboarding\Storage\Onboarding_Progress_Manager;
|
||||
use Elementor\App\Modules\Onboarding\Validation\User_Progress_Validator;
|
||||
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base;
|
||||
use WP_REST_Server;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class User_Progress extends Endpoint_Base {
|
||||
|
||||
public function get_name(): string {
|
||||
return 'user-progress';
|
||||
}
|
||||
|
||||
public function get_format(): string {
|
||||
return 'onboarding';
|
||||
}
|
||||
|
||||
protected function register(): void {
|
||||
parent::register();
|
||||
|
||||
$this->register_items_route( WP_REST_Server::EDITABLE );
|
||||
}
|
||||
|
||||
public function get_items( $request ) {
|
||||
$permission = $this->check_permission();
|
||||
if ( is_wp_error( $permission ) ) {
|
||||
return $permission;
|
||||
}
|
||||
|
||||
$manager = Onboarding_Progress_Manager::instance();
|
||||
$progress = $manager->get_progress();
|
||||
|
||||
return [
|
||||
'data' => $progress->to_array(),
|
||||
'meta' => [
|
||||
'had_unexpected_exit' => $progress->had_unexpected_exit( Module::has_user_finished_onboarding() ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function update_items( $request ) {
|
||||
$permission = $this->check_permission();
|
||||
if ( is_wp_error( $permission ) ) {
|
||||
return $permission;
|
||||
}
|
||||
|
||||
$params = $request->get_json_params();
|
||||
|
||||
$validator = new User_Progress_Validator();
|
||||
$validated = $validator->validate( $params ?? [] );
|
||||
|
||||
if ( is_wp_error( $validated ) ) {
|
||||
return $validated;
|
||||
}
|
||||
|
||||
$manager = Onboarding_Progress_Manager::instance();
|
||||
$progress = $manager->update_progress( $validated );
|
||||
|
||||
return [
|
||||
'data' => 'success',
|
||||
'progress' => $progress->to_array(),
|
||||
];
|
||||
}
|
||||
|
||||
private function check_permission() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return new \WP_Error(
|
||||
'rest_forbidden',
|
||||
__( 'Sorry, you are not allowed to access onboarding data.', 'elementor' ),
|
||||
[ 'status' => 403 ]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user