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

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