| 1 |
<?php |
| 2 |
namespace Elementor\Core\Common\Modules\Connect\Rest; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use WP_Http; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Elementor Library Connect REST API. |
| 13 |
* |
| 14 |
* REST API controller for handling library connect operations. |
| 15 |
*/ |
| 16 |
class Rest_Api { |
| 17 |
|
| 18 |
/** |
| 19 |
* REST API namespace. |
| 20 |
*/ |
| 21 |
const REST_NAMESPACE = 'elementor/v1'; |
| 22 |
|
| 23 |
/** |
| 24 |
* REST API base. |
| 25 |
*/ |
| 26 |
const REST_BASE = 'library'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Authentication mode. |
| 30 |
*/ |
| 31 |
const AUTH_MODE = 'rest'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Register REST API routes. |
| 35 |
* |
| 36 |
* @access public |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function register_routes() { |
| 40 |
register_rest_route( |
| 41 |
self::REST_NAMESPACE, |
| 42 |
self::REST_BASE . '/connect', |
| 43 |
[ |
| 44 |
[ |
| 45 |
'methods' => \WP_REST_Server::CREATABLE, |
| 46 |
'callback' => [ $this, 'connect' ], |
| 47 |
'permission_callback' => [ $this, 'connect_permissions_check' ], |
| 48 |
'args' => [ |
| 49 |
'token' => [ |
| 50 |
'required' => true, |
| 51 |
'type' => 'string', |
| 52 |
'description' => 'Connect CLI token', |
| 53 |
], |
| 54 |
], |
| 55 |
], |
| 56 |
] |
| 57 |
); |
| 58 |
|
| 59 |
register_rest_route( |
| 60 |
self::REST_NAMESPACE, |
| 61 |
self::REST_BASE . '/connect', |
| 62 |
[ |
| 63 |
[ |
| 64 |
'methods' => \WP_REST_Server::DELETABLE, |
| 65 |
'callback' => [ $this, 'disconnect' ], |
| 66 |
'permission_callback' => [ $this, 'connect_permissions_check' ], |
| 67 |
], |
| 68 |
] |
| 69 |
); |
| 70 |
} |
| 71 |
public function connect( \WP_REST_Request $request ) { |
| 72 |
$app = $this->get_connect_app(); |
| 73 |
if ( ! $app ) { |
| 74 |
return $this->elementor_library_app_not_available(); |
| 75 |
} |
| 76 |
|
| 77 |
$app->set_auth_mode( self::AUTH_MODE ); |
| 78 |
$_REQUEST['mode'] = self::AUTH_MODE; |
| 79 |
$_REQUEST['token'] = $request->get_param( 'token' ); |
| 80 |
|
| 81 |
try { |
| 82 |
$app->action_authorize(); |
| 83 |
$app->action_get_token(); |
| 84 |
|
| 85 |
if ( $app->is_connected() ) { |
| 86 |
return $this->success_response( |
| 87 |
[ 'message' => __( 'Connected successfully.', 'elementor' ) ], |
| 88 |
WP_Http::CREATED ); |
| 89 |
} else { |
| 90 |
return $this->error_response( |
| 91 |
'elementor_library_not_connected', |
| 92 |
__( 'Failed to connect to Elementor Library.', 'elementor' ), |
| 93 |
WP_Http::INTERNAL_SERVER_ERROR |
| 94 |
); |
| 95 |
} |
| 96 |
} catch ( \Exception $e ) { |
| 97 |
return $this->error_response( |
| 98 |
'elementor_library_connect_error', |
| 99 |
$e->getMessage(), |
| 100 |
WP_Http::INTERNAL_SERVER_ERROR |
| 101 |
); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function disconnect( \WP_REST_Request $request ) { |
| 106 |
$app = $this->get_connect_app(); |
| 107 |
if ( ! $app ) { |
| 108 |
return $this->elementor_library_app_not_available(); |
| 109 |
} |
| 110 |
|
| 111 |
$app->set_auth_mode( self::AUTH_MODE ); |
| 112 |
$_REQUEST['mode'] = self::AUTH_MODE; |
| 113 |
|
| 114 |
try { |
| 115 |
$app->action_disconnect(); |
| 116 |
return $this->success_response( |
| 117 |
[ 'message' => __( 'Disconnected successfully.', 'elementor' ) ], |
| 118 |
WP_Http::OK |
| 119 |
); |
| 120 |
} catch ( \Exception $e ) { |
| 121 |
return $this->error_response( |
| 122 |
'elementor_library_disconnect_error', |
| 123 |
$e->getMessage(), |
| 124 |
WP_Http::INTERNAL_SERVER_ERROR |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public function connect_permissions_check( \WP_REST_Request $request ) { |
| 130 |
return current_user_can( 'manage_options' ); |
| 131 |
} |
| 132 |
|
| 133 |
private function route_wrapper( callable $cb ) { |
| 134 |
try { |
| 135 |
$response = $cb(); |
| 136 |
} catch ( \Exception $e ) { |
| 137 |
return $this->error_response( |
| 138 |
'unexpected_error', |
| 139 |
__( 'Something went wrong', 'elementor' ), |
| 140 |
WP_Http::INTERNAL_SERVER_ERROR |
| 141 |
); |
| 142 |
} |
| 143 |
return $response; |
| 144 |
} |
| 145 |
|
| 146 |
private function error_response( $code, $message, $status = WP_Http::BAD_REQUEST ) { |
| 147 |
return new \WP_Error( |
| 148 |
$code, |
| 149 |
$message, |
| 150 |
[ 'status' => $status ] |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
private function success_response( $data = [], $status = WP_Http::OK ) { |
| 155 |
$response = rest_ensure_response( array_merge( [ 'success' => true ], $data ) ); |
| 156 |
$response->set_status( $status ); |
| 157 |
return $response; |
| 158 |
} |
| 159 |
|
| 160 |
private function elementor_library_app_not_available() { |
| 161 |
return $this->error_response( |
| 162 |
'elementor_library_app_not_available', |
| 163 |
__( 'Elementor Library app is not available.', 'elementor' ), |
| 164 |
WP_Http::INTERNAL_SERVER_ERROR |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get the connect app. |
| 170 |
* |
| 171 |
* @return \Elementor\Core\Common\Modules\Connect\Apps\Library|null |
| 172 |
*/ |
| 173 |
private function get_connect_app() { |
| 174 |
$connect = Plugin::$instance->common->get_component( 'connect' ); |
| 175 |
if ( ! $connect ) { |
| 176 |
return null; |
| 177 |
} |
| 178 |
$app = $connect->get_app( 'library' ); |
| 179 |
if ( ! $app ) { |
| 180 |
$connect->init(); |
| 181 |
$app = $connect->get_app( 'library' ); |
| 182 |
} |
| 183 |
return $app; |
| 184 |
} |
| 185 |
} |
| 186 |
|