| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\SiteBuilder\Rest; |
| 3 |
|
| 4 |
use Elementor\App\Modules\SiteBuilder\Services\Connect_Auth_Service; |
| 5 |
use Elementor\Plugin; |
| 6 |
use WP_Error; |
| 7 |
use WP_Http; |
| 8 |
use WP_REST_Response; |
| 9 |
use WP_REST_Server; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
class Rest_Api { |
| 16 |
const API_NAMESPACE = 'elementor/v1'; |
| 17 |
const API_BASE = 'site-builder'; |
| 18 |
const SNAPSHOT_MAX_KEYS = 20; |
| 19 |
|
| 20 |
public function register_routes(): void { |
| 21 |
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/home-screen', [ |
| 22 |
'methods' => WP_REST_Server::READABLE, |
| 23 |
'callback' => [ $this, 'get_home_screen' ], |
| 24 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 25 |
] ); |
| 26 |
|
| 27 |
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/auth', [ |
| 28 |
'methods' => WP_REST_Server::READABLE, |
| 29 |
'callback' => [ $this, 'get_auth_credentials' ], |
| 30 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 31 |
] ); |
| 32 |
|
| 33 |
register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/snapshot', [ |
| 34 |
[ |
| 35 |
'methods' => WP_REST_Server::READABLE, |
| 36 |
'callback' => [ $this, 'get_snapshot' ], |
| 37 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 38 |
], |
| 39 |
[ |
| 40 |
'methods' => WP_REST_Server::EDITABLE, |
| 41 |
'callback' => [ $this, 'update_snapshot' ], |
| 42 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 43 |
'args' => [ |
| 44 |
'value' => [ |
| 45 |
'required' => true, |
| 46 |
'type' => 'object', |
| 47 |
'validate_callback' => fn( $value ) => is_array( $value ), |
| 48 |
'sanitize_callback' => fn( $value ) => is_array( $value ) ? $value : [], |
| 49 |
], |
| 50 |
], |
| 51 |
], |
| 52 |
] ); |
| 53 |
} |
| 54 |
|
| 55 |
public function get_home_screen() { |
| 56 |
$app = $this->get_connect_app(); |
| 57 |
|
| 58 |
if ( ! $app || ! $app->is_connected() ) { |
| 59 |
return new WP_Error( 'site_builder_unavailable', 'Site builder is not connected.', [ 'status' => WP_Http::SERVICE_UNAVAILABLE ] ); |
| 60 |
} |
| 61 |
|
| 62 |
$data = $app->get_home_screen(); |
| 63 |
|
| 64 |
if ( is_wp_error( $data ) ) { |
| 65 |
return $data; |
| 66 |
} |
| 67 |
|
| 68 |
return new WP_REST_Response( $data, WP_Http::OK ); |
| 69 |
} |
| 70 |
|
| 71 |
public function get_auth_credentials() { |
| 72 |
$connect_auth = ( new Connect_Auth_Service() )->get_connect_auth(); |
| 73 |
|
| 74 |
if ( ! $connect_auth ) { |
| 75 |
return new WP_Error( |
| 76 |
'auth_unavailable', |
| 77 |
'Authentication credentials not available', |
| 78 |
[ 'status' => WP_Http::SERVICE_UNAVAILABLE ] |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
$response = new WP_REST_Response( [ |
| 83 |
'success' => true, |
| 84 |
'data' => $connect_auth, |
| 85 |
], WP_Http::OK ); |
| 86 |
|
| 87 |
$response->header( 'Cache-Control', 'no-store, private' ); |
| 88 |
|
| 89 |
return $response; |
| 90 |
} |
| 91 |
|
| 92 |
public function get_snapshot() { |
| 93 |
$snapshot = get_option( 'elementor_site_builder_snapshot', [] ); |
| 94 |
return new WP_REST_Response( [ |
| 95 |
'success' => true, |
| 96 |
'data' => [ 'value' => $snapshot ], |
| 97 |
], WP_Http::OK ); |
| 98 |
} |
| 99 |
|
| 100 |
public function update_snapshot( $request ) { |
| 101 |
$value = $request->get_param( 'value' ); |
| 102 |
$sanitized = is_array( $value ) ? $value : []; |
| 103 |
|
| 104 |
if ( count( $sanitized ) > self::SNAPSHOT_MAX_KEYS ) { |
| 105 |
return new WP_REST_Response( [ |
| 106 |
'success' => false, |
| 107 |
'data' => [ 'message' => 'Snapshot exceeds maximum allowed size.' ], |
| 108 |
], WP_Http::BAD_REQUEST ); |
| 109 |
} |
| 110 |
|
| 111 |
$success = update_option( 'elementor_site_builder_snapshot', $sanitized, false ); |
| 112 |
|
| 113 |
if ( $success || get_option( 'elementor_site_builder_snapshot' ) === $sanitized ) { |
| 114 |
return new WP_REST_Response( [ |
| 115 |
'success' => true, |
| 116 |
'data' => [ 'message' => 'Snapshot updated successfully.' ], |
| 117 |
], WP_Http::OK ); |
| 118 |
} |
| 119 |
|
| 120 |
return new WP_REST_Response( [ |
| 121 |
'success' => false, |
| 122 |
'data' => [ 'message' => 'Failed to update snapshot.' ], |
| 123 |
], WP_Http::INTERNAL_SERVER_ERROR ); |
| 124 |
} |
| 125 |
|
| 126 |
protected function get_connect_app() { |
| 127 |
if ( ! Plugin::$instance->common ) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
$connect = Plugin::$instance->common->get_component( 'connect' ); |
| 132 |
|
| 133 |
if ( ! $connect ) { |
| 134 |
return null; |
| 135 |
} |
| 136 |
|
| 137 |
return $connect->get_app( 'site-builder' ); |
| 138 |
} |
| 139 |
} |
| 140 |
|