| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Connect\Rest; |
| 4 |
|
| 5 |
use ImageOptimization\Modules\Connect\Classes\{ |
| 6 |
Data, |
| 7 |
Route_Base, |
| 8 |
Service, |
| 9 |
}; |
| 10 |
|
| 11 |
use Throwable; |
| 12 |
use WP_REST_Request; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Switch_Domain |
| 20 |
*/ |
| 21 |
class Switch_Domain extends Route_Base { |
| 22 |
public string $path = 'switch_domain'; |
| 23 |
public const NONCE_NAME = 'wp_rest'; |
| 24 |
|
| 25 |
public function get_methods(): array { |
| 26 |
return [ 'POST' ]; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_name(): string { |
| 30 |
return 'switch_domain'; |
| 31 |
} |
| 32 |
|
| 33 |
public function POST( WP_REST_Request $request ) { |
| 34 |
$this->verify_nonce_and_capability( |
| 35 |
$request->get_param( self::NONCE_NAME ), |
| 36 |
self::NONCE_NAME |
| 37 |
); |
| 38 |
|
| 39 |
try { |
| 40 |
$client_id = Data::get_client_id(); |
| 41 |
|
| 42 |
if ( ! $client_id ) { |
| 43 |
return $this->respond_error_json( [ |
| 44 |
'message' => esc_html__( 'Client ID not found', 'image-optimization' ), |
| 45 |
'code' => 'bad_request', |
| 46 |
] ); |
| 47 |
} |
| 48 |
|
| 49 |
Service::update_redirect_uri(); |
| 50 |
|
| 51 |
return $this->respond_success_json( [ |
| 52 |
'message' => esc_html__( 'Domain updated!', 'image-optimization' ), |
| 53 |
] ); |
| 54 |
} catch ( Throwable $t ) { |
| 55 |
return $this->respond_error_json( [ |
| 56 |
'message' => $t->getMessage(), |
| 57 |
'code' => 'internal_server_error', |
| 58 |
] ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|