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.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 1.8.1 All 42 releases
sellkit / includes / block-editor / blocks / optin / crm / activecampaign.php

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

229 lines 6.0 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 * Active Campaign class.
8 *
9 * @since 2.3.0
10 */
11 class ActiveCampaign {
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 ActiveCampaign 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_active_campaign();
33 }
34
35 /**
36 * Handle Active Campaign.
37 *
38 * @since 2.3.0
39 */
40 private static function handle_active_campaign() {
41 if ( empty( self::$helper->attributes['activeCampaign'] ) ) {
42 return self::$helper->add_response( 'admin_errors', esc_html__( 'Active Campaign error: Missing configuration.', 'sellkit' ) );
43 }
44
45 self::$helper->check_api_params( 'activecampaign', 'Active Campaign', true );
46
47 $mapping_fields = self::$helper->attributes['activeCampaign'];
48 self::$helper->check_required_fields( $mapping_fields, [ 'list', 'Email' ] );
49
50 $fields = self::$helper->form_data['fields'];
51 $subscriber = self::map_fields_to_subscriber( $mapping_fields, $fields );
52
53 $response = self::send_request( 'contacts', $subscriber );
54
55 if ( is_wp_error( $response ) ) {
56 return self::$helper->add_response( 'admin_errors', $response->get_error_message() );
57 }
58
59 $code = (int) wp_remote_retrieve_response_code( $response );
60
61 if ( 422 === $code ) {
62 $response = self::sync_contact( $subscriber );
63
64 if ( is_wp_error( $response ) ) {
65 return self::$helper->add_response( 'admin_errors', $response->get_error_message() );
66 }
67
68 $code = (int) wp_remote_retrieve_response_code( $response );
69 }
70
71 if ( $code < 200 || $code >= 300 ) {
72 self::$helper->add_response(
73 'admin_errors',
74 sprintf(
75 /* Translators: 1: CRM name 2: Error code 3: Error message */
76 esc_html__( '%1$s: Request error-%2$s -- %3$s', 'sellkit' ),
77 'Active Campaign',
78 esc_html( $code ),
79 wp_remote_retrieve_response_message( $response )
80 ) . esc_html__( ' (issued by endpoint)', 'sellkit' )
81 );
82 } else {
83 $contact = json_decode( wp_remote_retrieve_body( $response ), true );
84
85 if ( ! empty( $contact ) ) {
86 $subscriber_id = $contact['contact']['id'];
87
88 self::add_subscriber_to_list( $subscriber_id );
89 self::add_tag_to_subscriber( $subscriber_id );
90 }
91 }
92 }
93
94 /**
95 * Map fields to subscriber data.
96 *
97 * @param array $mapping_fields The mapping fields.
98 * @param array $fields The form data fields.
99 * @return array The subscriber data.
100 */
101 private static function map_fields_to_subscriber( $mapping_fields, $fields ) {
102 $mapped_fields = [];
103
104 foreach ( $mapping_fields as $index => $value ) {
105 if ( ! empty( $fields[ $value ] ) ) {
106 $fields[ $value ] = self::$helper->get_address_field( $fields[ $value ], 'address' );
107 }
108
109 if ( ! empty( $value ) && isset( $fields[ $value ] ) && ! in_array( $index, [ 'list', 'tagsValue' ], true ) ) {
110 $mapped_fields[ lcfirst( $index ) ] = $fields[ $value ];
111 }
112 }
113
114 if ( empty( $mapped_fields['email'] ) && ! empty( $fields[ $mapping_fields['Email'] ] ) ) {
115 $mapped_fields['email'] = $fields[ $mapping_fields['Email'] ];
116 }
117
118 return [ 'contact' => $mapped_fields ];
119 }
120
121 /**
122 * Send POST request to ActiveCampaign API.
123 *
124 * @param string $endpoint The API endpoint.
125 * @param array $body The request body.
126 * @return array|WP_Error The response or WP_Error on failure.
127 */
128 private static function send_request( $endpoint, $body ) {
129 $args = [
130 'method' => 'POST',
131 'timeout' => 100,
132 'headers' => [
133 'Api-Token' => self::$helper->api_key,
134 'Accept' => 'application/json',
135 'Content-Type' => 'application/json',
136 ],
137 'body' => wp_json_encode( $body ),
138 ];
139
140 return wp_remote_post( trailingslashit( self::$helper->api_url ) . 'api/3/' . $endpoint, $args );
141 }
142
143 /**
144 * Add subscriber to list.
145 *
146 * @param int $subscriber_id The subscriber ID.
147 */
148 private static function add_subscriber_to_list( $subscriber_id ) {
149 $list_id = self::$helper->attributes['activeCampaign']['list'];
150 $body = [
151 'contactList' => [
152 'list' => strval( $list_id ),
153 'contact' => strval( $subscriber_id ),
154 'status' => '1',
155 ],
156 ];
157
158 $response = self::send_request( 'contactLists', $body );
159
160 if ( is_wp_error( $response ) ) {
161 return self::$helper->add_response( 'admin_errors', $response->get_error_message() );
162 }
163
164 self::handle_api_response( $response, 'List addition error' );
165 }
166
167 /**
168 * Add tag to subscriber.
169 *
170 * @param int $subscriber_id The subscriber ID.
171 */
172 private static function add_tag_to_subscriber( $subscriber_id ) {
173 $tag = self::$helper->attributes['activeCampaign']['tagsValue'];
174
175 if ( empty( $tag ) ) {
176 return;
177 }
178
179 $body = [
180 'contactTag' => [
181 'contact' => $subscriber_id,
182 'tag' => $tag,
183 ],
184 ];
185
186 $response = self::send_request( 'contactTags', $body );
187
188 if ( is_wp_error( $response ) ) {
189 return self::$helper->add_response( 'admin_errors', $response->get_error_message() );
190 }
191
192 self::handle_api_response( $response, 'Tag addition error' );
193 }
194
195 /**
196 * Sync the contact using the contact/sync endpoint.
197 *
198 * @param array $subscriber The subscriber data to sync.
199 * @return array|WP_Error The response or WP_Error on failure.
200 */
201 private static function sync_contact( $subscriber ) {
202 return self::send_request( 'contact/sync', $subscriber );
203 }
204
205 /**
206 * Handle API response errors.
207 *
208 * @param array $response The API response.
209 * @param string $context The context of the error.
210 */
211 private static function handle_api_response( $response, $context ) {
212 $code = (int) wp_remote_retrieve_response_code( $response );
213
214 if ( $code < 200 || $code >= 300 ) {
215 self::$helper->add_response(
216 'admin_errors',
217 sprintf(
218 /* Translators: 1: CRM name 2: Error code 3: Error message 4: Rest of message */
219 esc_html__( '%1$s: %2$s-%3$s -- %4$s', 'sellkit' ),
220 'Active Campaign',
221 $context,
222 esc_html( $code ),
223 wp_remote_retrieve_response_message( $response )
224 ) . esc_html__( ' (issued by endpoint)', 'sellkit' )
225 );
226 }
227 }
228 }
229