| 1 |
<?php |
| 2 |
/** |
| 3 |
* AI Authentication. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
use CartflowsAdmin\AdminCore\Api\ApiBase; |
| 14 |
/** |
| 15 |
* Checkout Markup |
| 16 |
* |
| 17 |
* @since x.x.x |
| 18 |
*/ |
| 19 |
class Cartflows_Ai_API extends ApiBase { |
| 20 |
|
| 21 |
/** |
| 22 |
* Member Variable |
| 23 |
* |
| 24 |
* @var object instance |
| 25 |
*/ |
| 26 |
private static $instance; |
| 27 |
|
| 28 |
/** |
| 29 |
* Initiator |
| 30 |
*/ |
| 31 |
public static function get_instance() { |
| 32 |
|
| 33 |
if ( ! isset( self::$instance ) ) { |
| 34 |
self::$instance = new self(); |
| 35 |
} |
| 36 |
|
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor |
| 42 |
* |
| 43 |
* @since x.x.x |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register API routes. |
| 51 |
* |
| 52 |
* @since x.x.x |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function register_routes() { |
| 56 |
register_rest_route( |
| 57 |
$this->get_api_namespace(), |
| 58 |
'/ai/auth', |
| 59 |
array( |
| 60 |
'methods' => WP_REST_Server::CREATABLE, |
| 61 |
'callback' => array( $this, 'verify_auth' ), |
| 62 |
'permission_callback' => array( $this, 'validate_permission' ), |
| 63 |
'args' => array( |
| 64 |
'accessKey' => array( |
| 65 |
'required' => true, |
| 66 |
'type' => 'string', |
| 67 |
'sanitize_callback' => 'sanitize_text_field', |
| 68 |
), |
| 69 |
), |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
register_rest_route( |
| 74 |
$this->get_api_namespace(), |
| 75 |
'/ai/auth', |
| 76 |
array( |
| 77 |
'methods' => WP_REST_Server::READABLE, |
| 78 |
'callback' => array( $this, 'get_auth_url' ), |
| 79 |
'permission_callback' => array( $this, 'validate_permission' ), |
| 80 |
) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Verify Auth |
| 86 |
* |
| 87 |
* @since x.x.x |
| 88 |
* @param WP_REST_Request<array<string, mixed>> $request Request object. |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function verify_auth( $request ) { |
| 92 |
$access_key = $request->get_param( 'accessKey' ); |
| 93 |
|
| 94 |
if ( ! isset( $access_key ) || empty( $access_key ) ) { |
| 95 |
wp_send_json_error( array( 'message' => __( 'No access key provided.', 'cartflows' ) ) ); |
| 96 |
} |
| 97 |
|
| 98 |
$saved = Cartflows_Ai_Auth::get_instance()->save_auth( $access_key, Cartflows_Ai_Auth::get_instance()->key ); |
| 99 |
|
| 100 |
if ( is_wp_error( $saved ) && $saved instanceof WP_Error ) { |
| 101 |
wp_send_json_error( array( 'message' => $saved->get_error_message() ) ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( false === $saved ) { |
| 105 |
wp_send_json_error( array( 'message' => __( 'Failed to save authentication data.', 'cartflows' ) ) ); |
| 106 |
} |
| 107 |
|
| 108 |
wp_send_json_success( array( 'message' => __( 'Authentication data saved.', 'cartflows' ) ) ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Submit URLs to IndexNow API. |
| 113 |
* |
| 114 |
* @since x.x.x |
| 115 |
* @param WP_REST_Request<array<string, mixed>> $request Request object. |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function get_auth_url( $request ) { |
| 119 |
if ( Cartflows_Ai_Auth::get_instance()->get_auth_status() ) { |
| 120 |
wp_send_json_success( array( 'message' => __( 'Authentication is already completed.', 'cartflows' ) ) ); |
| 121 |
} |
| 122 |
|
| 123 |
$auth = Cartflows_Ai_Auth::get_instance()->get_auth_url(); |
| 124 |
|
| 125 |
if ( is_wp_error( $auth ) && $auth instanceof WP_Error ) { |
| 126 |
wp_send_json_error( array( 'message' => $auth->get_error_message() ) ); |
| 127 |
} else { |
| 128 |
wp_send_json_success( array( 'auth_url' => $auth ) ); |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Kicking this off by calling 'get_instance()' method |
| 137 |
*/ |
| 138 |
Cartflows_Ai_API::get_instance(); |
| 139 |
|