| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\Onboarding\Data\Endpoints; |
| 4 |
|
| 5 |
use Elementor\App\Modules\Onboarding\Module; |
| 6 |
use Elementor\App\Modules\Onboarding\Storage\Onboarding_Progress_Manager; |
| 7 |
use Elementor\App\Modules\Onboarding\Validation\User_Progress_Validator; |
| 8 |
use Elementor\Data\V2\Base\Endpoint as Endpoint_Base; |
| 9 |
use WP_REST_Server; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class User_Progress extends Endpoint_Base { |
| 16 |
|
| 17 |
public function get_name(): string { |
| 18 |
return 'user-progress'; |
| 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::EDITABLE ); |
| 29 |
} |
| 30 |
|
| 31 |
public function get_items( $request ) { |
| 32 |
$permission = $this->check_permission(); |
| 33 |
if ( is_wp_error( $permission ) ) { |
| 34 |
return $permission; |
| 35 |
} |
| 36 |
|
| 37 |
$manager = Onboarding_Progress_Manager::instance(); |
| 38 |
$progress = $manager->get_progress(); |
| 39 |
|
| 40 |
return [ |
| 41 |
'data' => $progress->to_array(), |
| 42 |
'meta' => [ |
| 43 |
'had_unexpected_exit' => $progress->had_unexpected_exit( Module::has_user_finished_onboarding() ), |
| 44 |
], |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
public function update_items( $request ) { |
| 49 |
$permission = $this->check_permission(); |
| 50 |
if ( is_wp_error( $permission ) ) { |
| 51 |
return $permission; |
| 52 |
} |
| 53 |
|
| 54 |
$params = $request->get_json_params(); |
| 55 |
|
| 56 |
$validator = new User_Progress_Validator(); |
| 57 |
$validated = $validator->validate( $params ?? [] ); |
| 58 |
|
| 59 |
if ( is_wp_error( $validated ) ) { |
| 60 |
return $validated; |
| 61 |
} |
| 62 |
|
| 63 |
$manager = Onboarding_Progress_Manager::instance(); |
| 64 |
$progress = $manager->update_progress( $validated ); |
| 65 |
|
| 66 |
return [ |
| 67 |
'data' => 'success', |
| 68 |
'progress' => $progress->to_array(), |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
private function check_permission() { |
| 73 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 74 |
return new \WP_Error( |
| 75 |
'rest_forbidden', |
| 76 |
__( 'Sorry, you are not allowed to access onboarding data.', 'elementor' ), |
| 77 |
[ 'status' => 403 ] |
| 78 |
); |
| 79 |
} |
| 80 |
return true; |
| 81 |
} |
| 82 |
} |
| 83 |
|