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-utils.php

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

195 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AI Initialization.
4 *
5 * @package cartflows
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12 /**
13 * AI Initialization.
14 *
15 * @since x.x.x
16 */
17 class CartFlows_Ai_Utils {
18
19 /**
20 * Singleton instance.
21 *
22 * @var self|null
23 */
24 private static $instance;
25
26 /**
27 * Initiator
28 *
29 * @return self
30 */
31 public static function get_instance() {
32 if ( ! isset( self::$instance ) ) {
33 self::$instance = new self();
34 }
35 return self::$instance;
36 }
37
38 /**
39 * Get Credit System API URL.
40 *
41 * @return string API URL.
42 * @since x.x.x
43 */
44 public static function get_credit_system_api_url() {
45 if ( ! defined( 'CARTFLOWS_CREDIT_SERVER_API' ) ) {
46 define( 'CARTFLOWS_CREDIT_SERVER_API', 'https://credits.startertemplates.com/' );
47 }
48 return trailingslashit( (string) CARTFLOWS_CREDIT_SERVER_API );
49 }
50
51 /**
52 * Get Auth Token.
53 *
54 * @since x.x.x
55 * @return string|WP_Error
56 */
57 public function get_auth_token() {
58 $auth_token = $this->get_auth_data( 'user_email' );
59 $token = '';
60
61 if ( ! is_wp_error( $auth_token ) && is_string( $auth_token ) ) {
62 $token = sanitize_text_field( $auth_token );
63 }
64
65 $token = apply_filters( 'cartflows_content_generation_auth_token', $token );
66
67 if ( empty( $token ) || is_wp_error( $token ) ) {
68 return new WP_Error( 'no_auth_token', __( 'No authentication token found. Please connect your account.', 'cartflows' ) );
69 }
70
71 if ( ! is_string( $token ) ) {
72 return new WP_Error( 'invalid_auth_token', __( 'Invalid authentication token format.', 'cartflows' ) );
73 }
74
75 return sanitize_text_field( $token );
76 }
77
78 /**
79 * Send GET request to service.
80 *
81 * @since x.x.x
82 * @param string $route API route.
83 * @param int $timeout Request timeout in seconds.
84 * @return array<string, mixed>|WP_Error API response or WP_Error.
85 */
86 public function send_get_request( $route, $timeout = 30 ) {
87 $auth_token = $this->get_auth_token();
88
89 if ( empty( $auth_token ) || is_wp_error( $auth_token ) ) {
90 return new WP_Error( 'no_auth_token', __( 'No authentication token found. Please connect your account.', 'cartflows' ) );
91 }
92
93 $url = $this->build_credit_system_url( $route );
94
95 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
96 $encoded_token = base64_encode( $auth_token );
97
98 $args = array(
99 'headers' => array(
100 'X-Token' => $encoded_token,
101 'Content-Type' => 'application/json; charset=utf-8',
102 ),
103 'timeout' => $timeout, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
104 );
105
106 if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
107 // Signature: vip_safe_wp_remote_get( $url, $fallback_value, $threshold, $timeout, $retry, $args ).
108 // $args (with the auth headers) is the 6th argument; the function ignores $args['timeout']
109 // and uses the 4th argument. An empty fallback keeps failures returning a WP_Error.
110 return vip_safe_wp_remote_get( $url, '', 3, $timeout, 20, $args );
111 }
112
113 // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
114 return wp_remote_get( $url, $args );
115 }
116
117 /**
118 * Send API request to service.
119 *
120 * @since x.x.x
121 * @param array<string, mixed> $request_data Request data to send.
122 * @param string $route API route.
123 * @param int $timeout Request timeout in seconds.
124 * @return array<string, mixed>|WP_Error API response or WP_Error.
125 */
126 public function send_api_request( $request_data, $route, $timeout = 30 ) {
127 $auth_token = $this->get_auth_token();
128
129 if ( empty( $auth_token ) || is_wp_error( $auth_token ) ) {
130 return new WP_Error( 'no_auth_token', __( 'No authentication token found. Please connect your account.', 'cartflows' ) );
131 }
132
133 $url = $this->build_credit_system_url( $route );
134
135 $body = wp_json_encode( $request_data );
136
137 if ( false === $body ) {
138 return new WP_Error( 'json_encode_error', __( 'Failed to encode request data to JSON.', 'cartflows' ) );
139 }
140
141 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
142 $encoded_token = base64_encode( $auth_token );
143
144 return wp_remote_post(
145 $url,
146 array(
147 'headers' => array(
148 'X-Token' => $encoded_token,
149 'Content-Type' => 'application/json; charset=utf-8',
150 ),
151 'body' => $body,
152 'timeout' => $timeout, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
153 )
154 );
155 }
156
157 /**
158 * Get Auth Data.
159 *
160 * @since x.x.x
161 * @param string $key Optional. Key to retrieve specific data.
162 * @return array<string, mixed>|string|WP_Error
163 */
164 protected function get_auth_data( $key = '' ) {
165 $auth_data = get_option( 'cartflows_auth', false );
166
167 if ( empty( $auth_data ) || ! is_array( $auth_data ) ) {
168 return new WP_Error( 'no_auth_data', __( 'No authentication data found.', 'cartflows' ) );
169 }
170
171 if ( ! empty( $key ) && is_string( $key ) ) {
172 $value = $auth_data[ $key ] ?? null;
173 return is_string( $value )
174 ? $value
175 : new WP_Error( 'no_key_found', __( 'No data found for the provided key.', 'cartflows' ) );
176 }
177
178 return $auth_data;
179 }
180
181 /**
182 * Build a normalized credit-system endpoint URL.
183 *
184 * @since 1.7.2
185 * @param string $route API route.
186 * @return string
187 */
188 protected function build_credit_system_url( $route ) {
189 $base = self::get_credit_system_api_url();
190 $route = ltrim( (string) $route, '/' );
191
192 return $base . $route;
193 }
194 }
195