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 / elementor / modules / optin / actions / getresponse.php

getresponse.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/elementor/modules/optin/actions/getresponse.php

241 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || die();
4
5 use Sellkit_Elementor_Optin_Ajaxhandler as AjaxHandler;
6
7 /**
8 * Initializing the GetResponse action by extending Action base and using CRM trait.
9 *
10 * @since 1.5.0
11 */
12 class Sellkit_Elementor_Optin_Action_Getresponse extends Sellkit_Elementor_Optin_Action_Base {
13 use Sellkit_Elementor_Optin_CRM;
14
15 private $api_key;
16
17 public static $instance;
18
19 public static function get_instance() {
20 if ( empty( static::$instance ) ) {
21 static::$instance = new static();
22 }
23
24 return static::$instance;
25 }
26
27 public function get_name() {
28 return 'getresponse';
29 }
30
31 public function get_title() {
32 return esc_html__( 'GetResponse', 'sellkit' );
33 }
34
35 protected function get_base_url() {
36 return 'https://api.getresponse.com/v3/';
37 }
38
39 protected function get_headers() {
40 return [
41 'X-Auth-Token' => 'api-key ' . $this->api_key,
42 'Content-Type' => 'application/json',
43 'User-Agent' => $this->user_agent,
44 ];
45 }
46
47 protected function get_get_request_args() {
48 return [
49 'timeout' => 100,
50 'sslverify' => false,
51 'headers' => $this->get_headers(),
52 ];
53 }
54
55 public function add_controls( $widget ) {
56 $action = $this->get_name();
57
58 $this->add_api_controls( $widget, esc_html__( 'Campaign', 'sellkit' ) );
59 $this->add_field_mapping_controls( $widget );
60
61 $widget->add_control(
62 "{$action}_dayofcycle",
63 [
64 'label' => esc_html__( 'Day Of Cycle', 'sellkit' ),
65 'type' => 'number',
66 'min' => 0,
67 'conditions' => [
68 'terms' => [
69 [
70 'name' => "{$action}_list",
71 'operator' => '!in',
72 'value' => [ 'none', 'fetching', 'noList' ],
73 ],
74 ],
75 ],
76 ]
77 );
78
79 $this->add_tag_control( $widget );
80 }
81
82 public function run( AjaxHandler $ajax_handler ) {
83 $form_settings = $ajax_handler->form['settings'];
84
85 // Retrieve and check List.
86 $list_id = $form_settings[ $this->get_name() . '_list' ];
87 if ( empty( $list_id ) || in_array( $list_id, [ 'default', 'fetching', 'noList' ], true ) ) {
88 return $ajax_handler->add_response( 'admin_errors', $this->get_invalid_list_message( 'campaign' ) );
89 }
90
91 // Retireve and check API credentials.
92 $this->api_key = $this->get_api_param( $form_settings );
93 if ( empty( $this->api_key ) ) {
94 return $ajax_handler->add_response( 'admin_errors', "{$this->get_title()}: " . esc_html__( 'Missing API credentials.', 'sellkit' ) );
95 }
96
97 // Try subscription.
98 $this->ajax_handler = $ajax_handler;
99 $subscriber = $this->create_subscriber_object();
100 $this->subscribe( $subscriber );
101 }
102
103 public function get_list( AjaxHandler $ajax_handler, $params ) {
104 $this->api_key = $this->get_api_param( $params );
105 $results = $this->send_get( 'campaigns' );
106 $campaigns = [];
107
108 if ( 200 === $results['code'] && ! empty( $results['body'] ) ) {
109 foreach ( $results['body'] as $campaign ) {
110 if ( is_array( $campaign ) ) {
111 $campaigns[ $campaign['campaignId'] ] = $campaign['name'];
112 }
113 }
114 }
115
116 $list = [ 'lists' => $campaigns ];
117 return $ajax_handler->add_response( 'success', $list );
118 }
119
120 public function get_additional_data( AjaxHandler $ajax_handler, $params ) {
121 $this->api_key = $this->get_api_param( $params );
122
123 $data = [
124 'custom_fields' => $this->get_remote_custom_fields(),
125 'tags' => $this->get_remote_tags(),
126 ];
127
128 return $ajax_handler->add_response( 'success', $data );
129 }
130
131 private function get_remote_custom_fields() {
132 $results = $this->send_get( 'custom-fields' );
133 $custom_fields = [];
134
135 if ( empty( $results['body'] ) ) {
136 return $custom_fields;
137 }
138
139 foreach ( $results['body'] as $field ) {
140 if ( is_array( $field ) ) {
141 $custom_fields[ $field['customFieldId'] ] = $field['name'];
142 }
143 }
144
145 return $custom_fields;
146 }
147
148 private function get_remote_tags() {
149 $results = $this->send_get( 'tags' );
150 $tags = [];
151
152 if ( empty( $results['body'] ) ) {
153 return $tags;
154 }
155
156 foreach ( $results['body'] as $tag ) {
157 if ( is_array( $tag ) ) {
158 $tags[ $tag['tagId'] ] = $tag['name'];
159 }
160 }
161
162 return $tags;
163 }
164
165 protected function get_default_remote_fields() {
166 return [
167 'required' => [
168 'email' => esc_html__( 'Email', 'sellkit' ),
169 ],
170 'optional' => [
171 'name' => esc_html__( 'Name', 'sellkit' ),
172 ],
173 ];
174 }
175
176 protected function create_subscriber_object() {
177 $mapped_fields = $this->get_field_mappings();
178 $default_fields = $this->get_default_remote_fields();
179
180 foreach ( $mapped_fields as $key => $value ) {
181 $is_custom =
182 ! array_key_exists( $key, $default_fields['required'] ) &&
183 ! array_key_exists( $key, $default_fields['optional'] );
184
185 if ( $is_custom ) {
186 $subscriber['customFieldValues'][] = [
187 'customFieldId' => $key,
188 'value' => [ $value ],
189 ];
190
191 continue;
192 }
193
194 $subscriber[ $key ] = $value;
195 }
196
197 $settings = $this->ajax_handler->form['settings'];
198 $subscriber['ipAddress'] = $this->get_client_ip();
199 $subscriber['campaign'] = [ 'campaignId' => $settings['getresponse_list'] ];
200 $subscriber['dayOfCycle'] = null;
201
202 if ( isset( $settings['getresponse_dayofcycle'] ) ) {
203 $subscriber['dayOfCycle'] = intval( $settings['getresponse_dayofcycle'] );
204 }
205
206 //add tags if present
207 if ( ! empty( $settings['getresponse_tags'] ) ) {
208 $subscriber['tags'] = $settings['getresponse_tags'];
209 }
210
211 return $subscriber;
212 }
213
214 protected function subscribe( $subscriber_data ) {
215 $endpoint = 'contacts';
216 $args = [
217 'method' => 'POST',
218 'timeout' => 100,
219 'sslverify' => false,
220 'headers' => $this->get_headers(),
221 'body' => wp_json_encode( $subscriber_data ),
222 ];
223
224 $result = $this->send_post( $endpoint, $args, 'temp_getresponse' );
225
226 if ( 409 === $result['code'] ) {
227 unset( $this->ajax_handler->response['admin_errors']['temp_getresponse'] );
228
229 $_result = $this->send_get( "contacts?query[email]={$subscriber_data['email']}" );
230
231 if ( $_result['code'] < 200 || $_result['code'] >= 300 ) {
232 return $this->ajax_handler->add_response( 'admin_errors', esc_html__( 'GetResponse: Contact already exists, but cannot retrieve its ID.', 'sellkit' ) );
233 }
234
235 $contact_id = $_result['body'][0]['contactId'];
236
237 $this->send_post( "contacts/{$contact_id}", $args );
238 }
239 }
240 }
241