PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.2.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.2.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.2.0, at modules/content-generation/class-cartflows-ai-utils.php

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