PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.3.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.3.0
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / modules / content-generation / class-cartflows-ai-api.php

class-cartflows-ai-api.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.3.0, at modules/content-generation/class-cartflows-ai-api.php

148 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'args' => array(
81 'redirect_back' => array(
82 'required' => false,
83 'type' => 'string',
84 'sanitize_callback' => 'esc_url_raw',
85 ),
86 ),
87 )
88 );
89 }
90
91 /**
92 * Verify Auth
93 *
94 * @since x.x.x
95 * @param WP_REST_Request<array<string, mixed>> $request Request object.
96 * @return void
97 */
98 public function verify_auth( $request ) {
99 $access_key = $request->get_param( 'accessKey' );
100
101 if ( ! isset( $access_key ) || empty( $access_key ) ) {
102 wp_send_json_error( array( 'message' => __( 'No access key provided.', 'cartflows' ) ) );
103 }
104
105 $saved = Cartflows_Ai_Auth::get_instance()->save_auth( $access_key, Cartflows_Ai_Auth::get_instance()->key );
106
107 if ( is_wp_error( $saved ) && $saved instanceof WP_Error ) {
108 wp_send_json_error( array( 'message' => $saved->get_error_message() ) );
109 }
110
111 if ( false === $saved ) {
112 wp_send_json_error( array( 'message' => __( 'Failed to save authentication data.', 'cartflows' ) ) );
113 }
114
115 wp_send_json_success( array( 'message' => __( 'Authentication data saved.', 'cartflows' ) ) );
116 }
117
118 /**
119 * Submit URLs to IndexNow API.
120 *
121 * @since x.x.x
122 * @param WP_REST_Request<array<string, mixed>> $request Request object.
123 * @return void
124 */
125 public function get_auth_url( $request ) {
126 if ( Cartflows_Ai_Auth::get_instance()->get_auth_status() ) {
127 wp_send_json_success( array( 'message' => __( 'Authentication is already completed.', 'cartflows' ) ) );
128 }
129
130 $redirect_back = $request->get_param( 'redirect_back' );
131 $redirect_back = is_string( $redirect_back ) ? $redirect_back : '';
132 $auth = Cartflows_Ai_Auth::get_instance()->get_auth_url( $redirect_back );
133
134 if ( is_wp_error( $auth ) && $auth instanceof WP_Error ) {
135 wp_send_json_error( array( 'message' => $auth->get_error_message() ) );
136 } else {
137 wp_send_json_success( array( 'auth_url' => $auth ) );
138 }
139
140 }
141
142 }
143
144 /**
145 * Kicking this off by calling 'get_instance()' method
146 */
147 Cartflows_Ai_API::get_instance();
148