PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
2.7.0 2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 All 43 releases
sellkit / includes / block-editor / blocks / optin / crm / getresponse.php

getresponse.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/block-editor/blocks/optin/crm/getresponse.php

246 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Sellkit\Blocks\Optin;
3
4 defined( 'ABSPATH' ) || die();
5
6 /**
7 * GetResponse class.
8 *
9 * @since 2.3.0
10 */
11 class GetResponse {
12 /**
13 * Object of helper class.
14 *
15 * @since 2.3.0
16 * @var object
17 */
18 private static $helper;
19
20 /**
21 * Run the Drip process.
22 *
23 * @param object $helper The helper class instance.
24 */
25 public static function run( $helper ) {
26 self::$helper = $helper;
27
28 if ( empty( self::$helper ) ) {
29 return;
30 }
31
32 self::handle_getresponse();
33 }
34
35 /**
36 * Handle getResponse.
37 *
38 * @since 2.3.0
39 */
40 private static function handle_getresponse() {
41 if ( empty( self::$helper->attributes['getResponse'] ) ) {
42 return self::$helper->add_response( 'admin_errors', esc_html__( 'GetResponse error: Missing configuration.', 'sellkit' ) );
43 }
44
45 self::$helper->check_api_params( 'getresponse', 'GetResponse' );
46
47 $mapping_fields = self::$helper->attributes['getResponse'];
48
49 self::$helper->check_required_fields( $mapping_fields, [ 'campaign', 'email' ] );
50
51 $fields = self::$helper->form_data['fields'];
52
53 $subscriber_data = self::map_fields_to_subscriber( $mapping_fields, $fields );
54
55 $response = self::subscribe( $subscriber_data );
56
57 if ( is_wp_error( $response ) ) {
58 return self::$helper->add_response( 'admin_errors', $response->get_error_message() );
59 }
60
61 $code = $response['code'];
62
63 if ( $code < 200 || $code >= 300 ) {
64 return self::$helper->add_response(
65 'admin_errors',
66 sprintf(
67 /* Translators: 1: CRM name 2: Error code 3: Error message */
68 esc_html__( '%1$s: Request error-%2$s -- %3$s', 'sellkit' ),
69 'GetResponse',
70 esc_html( $code ),
71 wp_remote_retrieve_response_message( $response )
72 ) . esc_html__( ' (issued by endpoint)', 'sellkit' )
73 );
74 }
75 }
76
77 /**
78 * Subscribe a contact to GetResponse.
79 *
80 * @param array $subscriber_data The subscriber data.
81 * @return array|WP_Error The response or WP_Error on failure.
82 */
83 private static function subscribe( $subscriber_data ) {
84 $endpoint = 'contacts';
85 $args = [
86 'method' => 'POST',
87 'timeout' => 100,
88 'sslverify' => false,
89 'headers' => self::get_headers(),
90 'body' => wp_json_encode( $subscriber_data ),
91 ];
92
93 $result = self::send_post( $endpoint, $args, 'temp_getresponse' );
94
95 if ( is_array( $result ) && 409 === $result['code'] ) {
96 unset( self::$helper->ajax_handler->response['admin_errors']['temp_getresponse'] );
97
98 $_result = self::send_get( "contacts?query[email]={$subscriber_data['email']}" );
99
100 if ( $_result['code'] < 200 || $_result['code'] >= 300 ) {
101 return self::$helper->add_response( 'admin_errors', esc_html__( 'GetResponse: Contact already exists, but cannot retrieve its ID.', 'sellkit' ) );
102 }
103
104 $contact_id = $_result['body'][0]['contactId'];
105
106 return self::send_post( "contacts/{$contact_id}", $args );
107 }
108
109 return $result;
110 }
111
112 /**
113 * Send a POST request to the specified endpoint.
114 *
115 * @param string $endpoint The API endpoint.
116 * @param array $args The request arguments.
117 * @param string $context A context identifier for error handling.
118 * @return array|WP_Error The response or WP_Error on failure.
119 */
120 private static function send_post( $endpoint, $args, $context = '' ) {
121 $api_url = 'https://api.getresponse.com/v3/' . $endpoint;
122 $response = wp_remote_post( $api_url, $args );
123
124 if ( is_wp_error( $response ) ) {
125 return $response;
126 }
127
128 $code = (int) wp_remote_retrieve_response_code( $response );
129
130 if ( $code < 200 || $code >= 300 ) {
131 self::$helper->add_response(
132 'admin_errors',
133 /* translators: %s Error code */
134 sprintf( esc_html__( 'GetResponse: Error in request, code: %s', 'sellkit' ), esc_html( $code ) ),
135 $context
136 );
137 }
138
139 return [
140 'code' => $code,
141 'body' => json_decode( wp_remote_retrieve_body( $response ), true ),
142 ];
143 }
144
145 /**
146 * Send a GET request to the specified endpoint.
147 *
148 * @param string $endpoint The API endpoint.
149 * @return array|WP_Error The response or WP_Error on failure.
150 */
151 private static function send_get( $endpoint ) {
152 $api_url = 'https://api.getresponse.com/v3/' . $endpoint;
153 $args = [
154 'method' => 'GET',
155 'timeout' => 100,
156 'sslverify' => false,
157 'headers' => self::get_headers(),
158 ];
159
160 $response = wp_remote_get( $api_url, $args );
161
162 if ( is_wp_error( $response ) ) {
163 return $response;
164 }
165
166 $code = (int) wp_remote_retrieve_response_code( $response );
167
168 return [
169 'code' => $code,
170 'body' => json_decode( wp_remote_retrieve_body( $response ), true ),
171 ];
172 }
173
174 /**
175 * Get headers for the API request.
176 *
177 * @return array The headers for the API request.
178 */
179 private static function get_headers() {
180 return [
181 'Content-Type' => 'application/json',
182 'User-Agent' => 'sellkit',
183 'X-Auth-Token' => 'api-key ' . self::$helper->api_key,
184 ];
185 }
186
187
188 /**
189 * Map fields to GetResponse subscriber data.
190 *
191 * @param array $mapping_fields The mapping fields.
192 * @param array $fields The form data fields.
193 * @return array The subscriber data.
194 * @SuppressWarnings(PHPMD.NPathComplexity)
195 */
196 private static function map_fields_to_subscriber( $mapping_fields, $fields ) {
197 $subscriber_data = [
198 'email' => isset( $fields[ $mapping_fields['email'] ] ) ? $fields[ $mapping_fields['email'] ] : '',
199 'campaign' => [ 'campaignId' => $mapping_fields['campaign'] ],
200 'dayOfCycle' => ! empty( $mapping_fields['dayOfCycle'] ) ? intval( $mapping_fields['dayOfCycle'] ) : 0,
201 'ipAddress' => self::$helper->get_client_ip(),
202 ];
203
204 foreach ( $mapping_fields as $index => $value ) {
205 if ( strpos( $index, 'name_' ) === 0 ) {
206 continue;
207 }
208
209 if ( ! empty( $fields[ $value ] ) ) {
210 $fields[ $value ] = self::$helper->get_address_field( $fields[ $value ], 'address' );
211 }
212
213 if ( 'name' === $index ) {
214 $subscriber_data['name'] = $fields[ $value ];
215 continue;
216
217 }
218
219 if ( ! in_array( $index, [ 'campaign', 'email', 'tags', 'name', 'dayOfCycle' ], true ) && ! empty( $fields[ $value ] ) ) {
220 $field_name = "name_{$index}";
221
222 $custom_value_name = '';
223
224 if ( ! empty( $mapping_fields[ $field_name ] ) ) {
225 $custom_value_name = $mapping_fields[ $field_name ];
226 }
227
228 $subscriber_data['customFieldValues'][] = [
229 'customFieldId' => $index,
230 'name' => $custom_value_name,
231 'value' => [ $fields[ $value ] ],
232 'type' => 'single_select',
233 'fieldType' => 'single_select',
234 'type' => 'string'
235 ];
236 }
237 }
238
239 if ( ! empty( $mapping_fields['tagsValue'] ) ) {
240 $subscriber_data['tags'] = [ $mapping_fields['tagsValue'] ];
241 }
242
243 return $subscriber_data;
244 }
245 }
246