image-optimization
/
vendor
/
elementor
/
wp-one-package
/
src
/
Connect
/
Controllers
/
Authorization.php
Authorization.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.7, at vendor/elementor/wp-one-package/src/Connect/Controllers/Authorization.php
| 1 | <?php |
| 2 | |
| 3 | namespace ElementorOne\Connect\Controllers; |
| 4 | |
| 5 | use ElementorOne\Connect\Facade; |
| 6 | use ElementorOne\Common\RestError; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Class Authorization |
| 14 | * Handles all authorization-related REST API endpoints |
| 15 | */ |
| 16 | class Authorization { |
| 17 | |
| 18 | /** |
| 19 | * Facade instance |
| 20 | * @var Facade |
| 21 | */ |
| 22 | private Facade $facade; |
| 23 | |
| 24 | /** |
| 25 | * REST namespace |
| 26 | * @var string |
| 27 | */ |
| 28 | private string $namespace; |
| 29 | |
| 30 | /** |
| 31 | * REST base |
| 32 | * @var string |
| 33 | */ |
| 34 | private string $rest_base = 'connect'; |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * @param Facade $facade |
| 39 | */ |
| 40 | public function __construct( Facade $facade ) { |
| 41 | $this->facade = $facade; |
| 42 | $this->namespace = $this->facade->get_config( 'app_rest_namespace' ); |
| 43 | |
| 44 | add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Register all authorization-related routes |
| 49 | * @return void |
| 50 | */ |
| 51 | public function register_routes() { |
| 52 | // POST /connect/authorize |
| 53 | register_rest_route( |
| 54 | $this->namespace, |
| 55 | '/' . $this->rest_base . '/authorize', |
| 56 | [ |
| 57 | [ |
| 58 | 'methods' => \WP_REST_Server::CREATABLE, |
| 59 | 'callback' => [ $this, 'authorize' ], |
| 60 | 'permission_callback' => [ $this, 'check_permission' ], |
| 61 | 'args' => [ |
| 62 | 'clearSession' => [ |
| 63 | 'type' => 'boolean', |
| 64 | 'required' => false, |
| 65 | 'default' => false, |
| 66 | ], |
| 67 | ], |
| 68 | ], |
| 69 | ] |
| 70 | ); |
| 71 | |
| 72 | // POST /connect/disconnect |
| 73 | register_rest_route( |
| 74 | $this->namespace, |
| 75 | '/' . $this->rest_base . '/disconnect', |
| 76 | [ |
| 77 | [ |
| 78 | 'methods' => \WP_REST_Server::CREATABLE, |
| 79 | 'callback' => [ $this, 'disconnect' ], |
| 80 | 'permission_callback' => [ $this, 'check_permission' ], |
| 81 | ], |
| 82 | ] |
| 83 | ); |
| 84 | |
| 85 | // POST /connect/switch-domain |
| 86 | register_rest_route( |
| 87 | $this->namespace, |
| 88 | '/' . $this->rest_base . '/switch-domain', |
| 89 | [ |
| 90 | [ |
| 91 | 'methods' => \WP_REST_Server::CREATABLE, |
| 92 | 'callback' => [ $this, 'switch_domain' ], |
| 93 | 'permission_callback' => [ $this, 'check_permission' ], |
| 94 | ], |
| 95 | ] |
| 96 | ); |
| 97 | |
| 98 | // POST /connect/deactivate |
| 99 | register_rest_route( |
| 100 | $this->namespace, |
| 101 | '/' . $this->rest_base . '/deactivate', |
| 102 | [ |
| 103 | [ |
| 104 | 'methods' => \WP_REST_Server::CREATABLE, |
| 105 | 'callback' => [ $this, 'deactivate' ], |
| 106 | 'permission_callback' => [ $this, 'check_permission' ], |
| 107 | ], |
| 108 | ] |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Permission callback for all endpoints |
| 114 | * @param \WP_REST_Request $_request |
| 115 | * @return bool |
| 116 | */ |
| 117 | public function check_permission( \WP_REST_Request $_request ): bool { |
| 118 | $current_user_id = get_current_user_id(); |
| 119 | return $current_user_id > 0 && user_can( $current_user_id, 'manage_options' ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Handle authorize request |
| 124 | * @param \WP_REST_Request $request |
| 125 | * @return \WP_REST_Response|\WP_Error |
| 126 | */ |
| 127 | public function authorize( \WP_REST_Request $request ) { |
| 128 | $data = $this->facade->data(); |
| 129 | $utils = $this->facade->utils(); |
| 130 | |
| 131 | $clear_session = $request->get_param( 'clearSession' ); |
| 132 | |
| 133 | if ( $clear_session ) { |
| 134 | $data->clear_session( true ); |
| 135 | } elseif ( $utils->is_connected() ) { |
| 136 | return RestError::forbidden( 'You are already connected' ); |
| 137 | } |
| 138 | |
| 139 | $client_id = $data->get_client_id(); |
| 140 | $client_secret = $data->get_client_secret(); |
| 141 | |
| 142 | if ( ! $client_id || ! $client_secret ) { |
| 143 | try { |
| 144 | $client_id = $this->facade->service()->register_client(); |
| 145 | } catch ( \Throwable $th ) { |
| 146 | return RestError::internal_server_error( $th->getMessage() ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return $this->respond_success_json( $utils->get_authorize_url( $client_id ) ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Handle disconnect request |
| 155 | * @param \WP_REST_Request $_request |
| 156 | * @return \WP_REST_Response|\WP_Error |
| 157 | */ |
| 158 | public function disconnect( \WP_REST_Request $_request ) { |
| 159 | try { |
| 160 | $service = $this->facade->service(); |
| 161 | $service->disconnect(); |
| 162 | |
| 163 | return $this->respond_success_json(); |
| 164 | } catch ( \Throwable $th ) { |
| 165 | return RestError::internal_server_error( $th->getMessage() ); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Handle switch domain request |
| 171 | * @param \WP_REST_Request $_request |
| 172 | * @return \WP_REST_Response|\WP_Error |
| 173 | */ |
| 174 | public function switch_domain( \WP_REST_Request $_request ) { |
| 175 | try { |
| 176 | $data = $this->facade->data(); |
| 177 | $service = $this->facade->service(); |
| 178 | |
| 179 | $client_id = $data->get_client_id(); |
| 180 | |
| 181 | if ( ! $client_id ) { |
| 182 | return RestError::bad_request( 'Client ID not found' ); |
| 183 | } |
| 184 | |
| 185 | $service->switch_domain(); |
| 186 | |
| 187 | return $this->respond_success_json( [ 'message' => 'Domain updated!' ] ); |
| 188 | } catch ( \Throwable $th ) { |
| 189 | return RestError::internal_server_error( $th->getMessage() ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Handle deactivate request |
| 195 | * @param \WP_REST_Request $_request |
| 196 | * @return \WP_REST_Response|\WP_Error |
| 197 | */ |
| 198 | public function deactivate( \WP_REST_Request $_request ) { |
| 199 | try { |
| 200 | $service = $this->facade->service(); |
| 201 | $service->deactivate_license(); |
| 202 | |
| 203 | return $this->respond_success_json(); |
| 204 | } catch ( \Throwable $th ) { |
| 205 | return RestError::internal_server_error( $th->getMessage() ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Respond success JSON |
| 211 | * @param array $data The data to return |
| 212 | * @return \WP_REST_Response The response object |
| 213 | */ |
| 214 | private function respond_success_json( $data = [] ): \WP_REST_Response { |
| 215 | return new \WP_REST_Response([ |
| 216 | 'success' => true, |
| 217 | 'data' => $data, |
| 218 | ]); |
| 219 | } |
| 220 | } |
| 221 |