PluginProbe
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation / trunk
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation vtrunk
1.4.48 1.4.47 1.4.46 1.4.45 1.4.44 1.4.43 1.4.42 1.4.41 1.4.40 1.4.39 1.4.38 1.4.37 1.4.36 1.1.2 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 64 releases
optin / includes / integrations / implementations / class-convert-kit.php

class-convert-kit.php in WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation trunk, at includes/integrations/implementations/class-convert-kit.php

268 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore
2
3 namespace OPTN\Includes\Integrations\Implementations;
4
5 use OPTN\Includes\Integrations\Base\BaseMailIntegration;
6 use OPTN\Includes\Utils\Cache;
7 use OPTN\Includes\Utils\Utils;
8 use WpOrg\Requests\Requests;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Convert Kit Integration class.
14 *
15 * @link https://developers.kit.com/v4
16 */
17 class ConvertKit extends BaseMailIntegration {
18
19 /**
20 * Get name
21 *
22 * @return string
23 */
24 public function get_name() {
25 return 'convertkit';
26 }
27
28 /**
29 * Adds a subscribe to convert kit
30 *
31 * @param array $data data.
32 * @return bool
33 *
34 * @link https://developers.kit.com/api-reference/subscribers/create-a-subscriber
35 * @link https://developers.kit.com/v4#tag-a-subscriber
36 */
37 public function add_subscriber( $data ): bool {
38
39 if ( ! isset( $data['fields'] ) || ! isset( $data['fields']['email'] ) ) {
40 return false;
41 }
42
43 $email = $data['fields']['email'];
44 unset( $data['fields']['email'] );
45 $body = array(
46 'email_address' => $email,
47 );
48 if ( ! empty( $data['fields']['first_name'] ) ) {
49 $body['first_name'] = $data['fields']['first_name'];
50 }
51 $fields = array();
52
53 if ( ! empty( $data['fields'] ) ) {
54 foreach ( $data['fields'] as $key => $value ) {
55 $fields[ $key ] = $value;
56 }
57 $body['fields'] = $fields;
58 }
59
60 $sub_res = Utils::get_remote_req_body(
61 wp_remote_post(
62 'https://api.kit.com/v4/subscribers',
63 array(
64 'body' => wp_json_encode( $body ),
65 'timeout' => 45,
66 'headers' => $this->get_headers(),
67 )
68 ),
69 array( 200, 201 ) // Response code should be either 200 or 201.
70 );
71
72 if ( ! empty( $sub_res->error ) ) {
73 return false;
74 }
75
76 $sub_id = isset( $sub_res->data['subscriber']['id'] ) ? $sub_res->data['subscriber']['id'] : null;
77
78 if ( isset( $data['list'] ) && is_array( $data['list'] ) && ! empty( $sub_id ) ) {
79
80 // Convert Kit doesn't support bulk with OAuth keys. So we need to make multiple requests.
81 foreach ( $data['list'] as $list_id ) {
82 $url = "https://api.kit.com/v4/tags/{$list_id}/subscribers/${sub_id}";
83
84 $requests[ $list_id ] = array(
85 'url' => $url,
86 'type' => 'POST',
87 'headers' => $this->get_headers(),
88 'data' => array(),
89 'options' => array(
90 'timeout' => 45,
91 ),
92 );
93 }
94
95 $responses = Requests::request_multiple( $requests );
96
97 foreach ( $responses as $list_id => $response ) {
98 if ( is_a( $response, 'Requests_Exception' ) ) {
99 return false;
100 }
101
102 if ( ! in_array( $response->status_code, array( 200, 201 ) ) ) { // phpcs:ignore
103 return false;
104 }
105 }
106 }
107
108 $data['fields']['email'] = $email;
109 $this->add_lead( $data );
110 return true;
111 }
112
113 /**
114 * Adds a lead to the leads table
115 *
116 * @param array $data data.
117 * @return void
118 */
119 public function add_lead( $data ) {
120
121 $fields = Utils::sanitize_json_array( $data['fields'] );
122 $fields['ip'] = Utils::get_user_ip();
123
124 $leads_data = array(
125 'email' => sanitize_email( $fields['email'] ),
126 'name' => $fields['first_name'] ?? null,
127 'conv_id' => intval( $data['conv_id'] ),
128 'data' => $fields,
129 'integration' => 'convertkit',
130 );
131
132 $this->db->add_lead( $leads_data );
133 }
134
135
136 /**
137 * Gets the fields for convert kit
138 *
139 * @param array $args receive data as array.
140 * @return array
141 *
142 * @link https://developers.kit.com/v4#list-custom-fields
143 */
144 public function get_fields( $args = array() ): array {
145
146 $use_cache = $args['use_cache'];
147 if ( $use_cache ) {
148 $res = $this->get_cached_fields();
149
150 if ( ! empty( $res ) ) {
151 return $res;
152 }
153 }
154
155 $res = wp_remote_get(
156 'https://api.kit.com/v4/custom_fields',
157 array(
158 'timeout' => 45,
159 'headers' => $this->get_headers(),
160 )
161 );
162
163 if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore
164 $body = wp_remote_retrieve_body( $res );
165 $body = json_decode( $body, true );
166
167 if ( isset( $body['custom_fields'] ) && is_array( $body['custom_fields'] ) ) {
168
169 $ret = array(
170 array(
171 'value' => 'email',
172 'label' => 'Email',
173 ),
174 array(
175 'value' => 'first_name',
176 'label' => 'First Name',
177 ),
178 );
179 foreach ( $body['custom_fields'] as $d ) {
180 $ret[] = array(
181 'value' => $d['key'],
182 'label' => $d['label'],
183 );
184 }
185
186 $this->set_fields_cache( $ret );
187
188 return $ret;
189 }
190 }
191
192 return array();
193 }
194
195 /**
196 * Gets list for convert kit
197 *
198 * @link https://developers.kit.com/v4#list-tags
199 * @param array $args receive data as array.
200 * @return array
201 */
202 public function get_lists( $args = array() ): array {
203 $use_cache = $args['use_cache'];
204
205 if ( $use_cache ) {
206 $res = $this->get_cached_lists();
207
208 if ( ! empty( $res ) ) {
209 return $res;
210 }
211 }
212
213 $url = add_query_arg(
214 array(
215 'per_page' => 1000,
216 ),
217 'https://api.kit.com/v4/tags',
218 );
219
220 $res = wp_remote_get(
221 $url,
222 array(
223 'timeout' => 45,
224 'headers' => $this->get_headers(),
225 )
226 );
227
228 if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore
229 $body = wp_remote_retrieve_body( $res );
230 $body = json_decode( $body, true );
231
232 if ( isset( $body['tags'] ) && is_array( $body['tags'] ) ) {
233
234 $ret = array();
235
236 foreach ( $body['tags'] as $d ) {
237 $ret[] = array(
238 'value' => $d['id'],
239 'label' => $d['name'],
240 );
241 }
242
243 $this->set_lists_cache( $ret );
244
245 return $ret;
246 }
247 }
248
249 return array();
250 }
251
252 /**
253 * Gets headers with authorization token
254 *
255 * @return array
256 */
257 protected function get_headers() {
258
259 $settings = $this->get_settings();
260
261 return array(
262 'Content-Type' => 'application/json',
263 'Accept' => 'application/json',
264 'X-Kit-Api-Key' => $settings['apiKey'],
265 );
266 }
267 }
268