| 1 |
<?php |
| 2 |
|
| 3 |
namespace SiteLeads\Features; |
| 4 |
|
| 5 |
use SiteLeads\Admin\Admin; |
| 6 |
use SiteLeads\Core\Singleton; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Rest { |
| 13 |
use Singleton; |
| 14 |
|
| 15 |
public function __construct() { |
| 16 |
add_action( 'rest_api_init', array( $this, 'registerRoutes' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
private function register( $path, $callback, $methods = 'GET' ) { |
| 21 |
register_rest_route( |
| 22 |
'siteleads/v1', |
| 23 |
$path, |
| 24 |
array( |
| 25 |
'methods' => $methods, |
| 26 |
'callback' => $callback, |
| 27 |
'permission_callback' => function () { |
| 28 |
return current_user_can( 'manage_options' ); |
| 29 |
}, |
| 30 |
) |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
public function registerRoutes() { |
| 35 |
$this->register( '/set-ai-api-key', array( $this, 'setAIApiKey' ), 'POST' ); |
| 36 |
} |
| 37 |
|
| 38 |
public function setAIApiKey( \WP_REST_Request $request ) { |
| 39 |
$key = sanitize_text_field( $request->get_param( 'key' ) ); |
| 40 |
$identifier = sanitize_text_field( $request->get_param( 'identifier' ) ); |
| 41 |
|
| 42 |
if ( empty( $key ) ) { |
| 43 |
return new \WP_Error( 'no_key', __( 'No key provided', 'siteleads' ), array( 'status' => 400 ) ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( empty( $identifier ) ) { |
| 47 |
return new \WP_Error( 'no_identifier', __( 'No identifier provided', 'siteleads' ), array( 'status' => 400 ) ); |
| 48 |
} |
| 49 |
|
| 50 |
update_option( 'siteleads_ai_api_key', $key ); |
| 51 |
update_option( 'siteleads_ai_key_identifier', $identifier ); |
| 52 |
|
| 53 |
return rest_ensure_response( |
| 54 |
array( |
| 55 |
'success' => true, |
| 56 |
'nextSettings' => Admin::getBackendData(), |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|