PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.7.9
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.7.9
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 / elementor / modules / optin / actions / growmatik.php

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

268 lines 7.3 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 Drip action by extending Action base and using CRM trait.
9 *
10 * @since 1.5.0
11 */
12 class Sellkit_Elementor_Optin_Action_Growmatik extends Sellkit_Elementor_Optin_Action_Base {
13 use Sellkit_Elementor_Optin_CRM;
14
15 private $api_key;
16
17 private $api_secret;
18
19 public static $instance;
20
21 public static function get_instance() {
22 if ( empty( static::$instance ) ) {
23 static::$instance = new static();
24 }
25
26 return static::$instance;
27 }
28
29 public function get_name() {
30 return 'growmatik';
31 }
32
33 public function get_title() {
34 return esc_html__( 'Growmatik', 'sellkit' );
35 }
36
37 protected function get_base_url() {
38 return 'https://api.growmatik.ai/public/v1/';
39 }
40
41 protected function get_headers() {
42 return [
43 'apiKey' => $this->api_key,
44 'Content-Type' => 'application/json',
45 'User-Agent' => $this->user_agent,
46 ];
47 }
48
49 protected function get_get_request_args() {
50 return [
51 'timeout' => 100,
52 'sslverify' => false,
53 'headers' => $this->get_headers(),
54 ];
55 }
56
57 public function add_controls( $widget ) {
58 $action = $this->get_name();
59
60 $this->add_api_controls( $widget, esc_html__( 'Site', 'sellkit' ) );
61 $this->add_field_mapping_controls( $widget );
62 $this->add_tag_control( $widget );
63
64 // We need to inject a new text control for API Secret.
65 $widget->start_injection( [ 'of' => "{$action}_custom_api_key" ] );
66 $widget->add_control( "{$action}_custom_api_secret",
67 [
68 'label' => esc_html__( 'Custom API Secret', 'sellkit' ),
69 'type' => 'text',
70 /* translators: Action name */
71 'description' => sprintf( esc_html__( 'Enter your %s API Secret for only this form.', 'sellkit' ), $this->get_title() ),
72 'condition' => [ "{$action}_api_key_source" => 'custom' ],
73 ]
74 );
75 $widget->end_injection();
76 }
77
78 public function run( AjaxHandler $ajax_handler ) {
79 $form_settings = $ajax_handler->form['settings'];
80
81 // Retrieve and check List.
82 $list_id = $form_settings[ $this->get_name() . '_list' ];
83 if ( empty( $list_id ) || in_array( $list_id, [ 'default', 'fetching', 'noList' ], true ) ) {
84 return $ajax_handler->add_response( 'admin_errors', $this->get_invalid_list_message( 'site' ) );
85 }
86
87 // Retireve and check API credentials.
88 $this->api_key = $this->get_api_param( $form_settings );
89 $this->api_secret = $this->get_api_param( $form_settings, 'secret' );
90 if ( empty( $this->api_key ) || empty( $this->api_secret ) ) {
91 return $ajax_handler->add_response( 'admin_errors', "{$this->get_title()}: " . esc_html__( 'Missing API credentials.', 'sellkit' ) );
92 }
93
94 // Try subscription.
95 $this->ajax_handler = $ajax_handler;
96 $subscriber = $this->create_subscriber_object();
97 $this->subscribe( $subscriber );
98 }
99
100 public function get_list( AjaxHandler $ajax_handler, $params ) {
101 $this->api_key = $this->get_api_param( $params );
102 $results = $this->send_get( 'site' );
103 $site = [];
104
105 // Growmatik has only one site per api key, so we don't need to loop over the response.
106 if ( ! empty( $results['body']['data'] ) ) {
107 $site_info = $results['body']['data'];
108
109 if ( ! $site_info['enabled'] ) {
110 return [ 'lists' => $site ];
111 }
112
113 $site[ $site_info['siteId'] ] = $site_info['siteName'];
114 }
115
116 $list = [ 'lists' => $site ];
117
118 return $ajax_handler->add_response( 'success', $list );
119 }
120
121 public function get_additional_data( AjaxHandler $ajax_handler, $params ) {
122 $this->api_key = $this->get_api_param( $params );
123
124 $data = [
125 'custom_fields' => $this->get_remote_custom_fields( $params['list_id'] ),
126 'tags' => $this->get_remote_tags( $params['list_id'] ),
127 ];
128
129 return $ajax_handler->add_response( 'success', $data );
130 }
131
132 private function get_remote_custom_fields() {
133 $results = $this->send_get( 'site/attributes' );
134 $custom_attrs = [];
135
136 if ( empty( $results['body']['data'] ) ) {
137 return $custom_attrs;
138 }
139
140 foreach ( $results['body']['data'] as $attr ) {
141 if ( 'custom' !== $attr['type'] ) {
142 continue;
143 }
144
145 $custom_attrs[ $attr['name'] ] = $attr['name'];
146 }
147
148 return $custom_attrs;
149 }
150
151 private function get_remote_tags() {
152 $results = $this->send_get( 'site/tags' );
153 $tags = [];
154
155 if ( empty( $results['body']['data'] ) ) {
156 return $tags;
157 }
158
159 foreach ( $results['body']['data'] as $tag ) {
160 $tags[ $tag['id'] ] = $tag['name'];
161 }
162
163 return $tags;
164 }
165
166 protected function get_default_remote_fields() {
167 return [
168 'required' => [
169 'email' => esc_html__( 'Email', 'sellkit' ),
170 ],
171 'optional' => [
172 'firstName' => esc_html__( 'First Name', 'sellkit' ),
173 'lastName' => esc_html__( 'Last Name', 'sellkit' ),
174 'userName' => esc_html__( 'User Name', 'sellkit' ),
175 'gender' => esc_html__( 'Gender', 'sellkit' ),
176 'address' => esc_html__( 'Address 1', 'sellkit' ),
177 'phoneNumber' => esc_html__( 'Phone', 'sellkit' ),
178 'country' => esc_html__( 'Country', 'sellkit' ),
179 'region' => esc_html__( 'Region', 'sellkit' ),
180 'city' => esc_html__( 'City', 'sellkit' ),
181 'euConsent' => esc_html__( 'EU Consent', 'sellkit' ),
182 'marketingEmailConsent' => esc_html__( 'Marketing Email Consent', 'sellkit' ),
183 ],
184 ];
185 }
186
187 protected function create_subscriber_object() {
188 $mapped_fields = $this->get_field_mappings();
189 $default_fields = $this->get_default_remote_fields();
190 $subscriber = [ 'data' => [] ];
191 $custom_attrs = [ 'data' => [] ];
192 $tags = [ 'tags' => [] ];
193 $email = null;
194
195 foreach ( $mapped_fields as $key => $value ) {
196 $is_custom =
197 ! array_key_exists( $key, $default_fields['required'] ) &&
198 ! array_key_exists( $key, $default_fields['optional'] );
199
200 if ( $is_custom ) {
201 $custom_attrs['data'][] = [
202 'name' => $key,
203 'value' => $value,
204 ];
205 continue;
206 }
207
208 $subscriber['user'][ $key ] = $value;
209
210 if ( 'email' === $key ) {
211 $email = $value;
212 }
213 }
214
215 if ( ! $email ) {
216 return [];
217 }
218
219 // Add tags if present.
220 $tag_settings = $this->ajax_handler->form['settings']['growmatik_tags'];
221 $tags['tags'] = is_array( $tag_settings ) ? $tag_settings : [ $tag_settings ];
222
223 // Further data.
224 $subscriber['apiSecret'] = $this->api_secret;
225 $tags['apiSecret'] = $this->api_secret;
226 $tags['email'] = $email;
227 $custom_attrs['apiSecret'] = $this->api_secret;
228 $custom_attrs['email'] = $email;
229
230 return [
231 'subscriber' => $subscriber,
232 'custom_attrs' => $custom_attrs,
233 'tags' => $tags,
234 ];
235 }
236
237 protected function subscribe( $subscriber_data ) {
238 if ( empty( $subscriber_data ) || empty( $subscriber_data['subscriber'] ) ) {
239 return;
240 }
241
242 $args = [
243 'method' => 'POST',
244 'timeout' => 100,
245 'headers' => $this->get_headers(),
246 'body' => wp_json_encode( $subscriber_data['subscriber'] ),
247 ];
248
249 $result = $this->send_post( 'contact', $args );
250
251 if ( $result['code'] < 200 || $result['code'] >= 300 ) {
252 return;
253 }
254
255 if ( ! empty( $subscriber_data['custom_attrs'] ) ) {
256 $args['body'] = wp_json_encode( $subscriber_data['custom_attrs'] );
257
258 $this->send_post( 'contact/attribute/email', $args );
259 }
260
261 if ( ! empty( $subscriber_data['tags'] ) ) {
262 $args['body'] = wp_json_encode( $subscriber_data['tags'] );
263
264 $this->send_post( 'contact/tags/email', $args );
265 }
266 }
267 }
268