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-moosend.php

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

287 lines 7.1 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 http\Url;
6 use OPTN\Includes\Integrations\Base\BaseMailIntegration;
7 use OPTN\Includes\Utils\Cache;
8 use OPTN\Includes\Utils\Utils;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Moosend Integration class.
14 *
15 * @link https://docs.moosend.com/developers/api-documentation/en/index-en.html
16 */
17 class Moosend extends BaseMailIntegration {
18
19 /**
20 * Get name
21 *
22 * @return string
23 */
24 public function get_name() {
25 return 'moosend';
26 }
27
28 /**
29 * Adds a subscriber to Moosend by list id
30 *
31 * @param array $data data.
32 * @return bool
33 *
34 * @link https://docs.moosend.com/developers/api-documentation/en/index-en.html#UUID-ea43cb6a-a7ab-b638-dec2-ff7cedf8efa3
35 */
36 public function add_subscriber( $data ): bool {
37
38 if ( ! isset( $data['fields'] ) || ! isset( $data['fields']['Email'] ) ) {
39 return false;
40 }
41 $settings = $this->get_settings();
42
43 // Static fields array.
44 $static_fields = array( 'Email', 'Name', 'Mobile', 'Preferences' );
45
46 $body_template = array( 'HasExternalDoubleOptIn' => ! empty( $data['doubleOpt'] ) );
47
48 // Custom dynamic fields.
49 if ( ! empty( $data['fields'] ) ) {
50 $custom_fields = array();
51 foreach ( $data['fields'] as $key => $value ) {
52 if ( ! in_array( $key, $static_fields ) ) {
53 $custom_fields[] = "{$key}={$value}";
54 }
55 }
56
57 $body_template['CustomFields'] = $custom_fields;
58 }
59
60 // Add static field to request body.
61 foreach ( $static_fields as $field ) {
62 if ( ! empty( $data['fields'][ $field ] ) ) {
63 $body_template[ $field ] = $data['fields'][ $field ];
64 }
65 }
66
67 $success = false;
68
69 foreach ( $data['list'] as $list_id ) {
70 $body = $body_template;
71
72 $res = wp_remote_post(
73 'https://api.moosend.com/v3/subscribers/' . $list_id . '/subscribe.json?apiKey=' . $settings['apiKey'],
74 array(
75 'body' => wp_json_encode( $body ),
76 'timeout' => 45,
77 'headers' => $this->get_headers(),
78 )
79 );
80
81 // Response code should be either 200 or 201.
82 if ( ! is_wp_error( $res ) && in_array( wp_remote_retrieve_response_code( $res ), array( 200, 201 ) ) ) { // phpcs:ignore
83 $success = true;
84 }
85 }
86
87 if ( $success ) {
88 $this->add_lead( $data );
89 }
90
91 return $success;
92 }
93
94
95 /**
96 * Adds a lead to the leads table
97 *
98 * @param array $data data.
99 * @return void
100 */
101 public function add_lead( $data ) {
102
103 $fields = Utils::sanitize_json_array( $data['fields'] );
104 $fields['ip'] = Utils::get_user_ip();
105
106 $leads_data = array(
107 'email' => sanitize_email( $fields['email'] ),
108 'name' => $fields['name'] ?? null,
109 'conv_id' => intval( $data['conv_id'] ),
110 'data' => $fields,
111 'integration' => 'moosend',
112 );
113
114 $this->db->add_lead( $leads_data );
115 }
116
117
118 /**
119 * Gets the fields for Moosend by list id
120 *
121 * @param array $args receive data as array.
122 * @return array
123 * @link https://docs.moosend.com/developers/api-documentation/en/index-en.html#UUID-2a6284dc-a340-66fa-c213-7e6490529f03
124 */
125 public function get_fields( $args = array() ): array {
126
127 if ( empty( $args['list_id'] ) ) {
128 return array();
129 }
130 $use_cache = $args['use_cache'];
131
132 $list_ids = is_array( $args['list_id'] ) ? $args['list_id'] : array( $args['list_id'] );
133 $settings = $this->get_settings();
134 $unique_fields = array(
135 array(
136 'value' => 'Email',
137 'label' => 'Email',
138 ),
139 array(
140 'value' => 'Name',
141 'label' => 'Name',
142 ),
143 array(
144 'value' => 'Mobile',
145 'label' => 'Mobile',
146 ),
147 array(
148 'value' => 'Preferences',
149 'label' => 'Preferences',
150 ),
151 );
152
153 foreach ( $list_ids as $list_id ) {
154 if ( $use_cache ) {
155 $cached = Cache::get( 'optn_int_moosend_fields_' . $this->account_id . '_' . $list_id );
156 if ( ! empty( $cached ) ) {
157 $fields = json_decode( $cached, true );
158 $unique_fields = $this->merge_unique_fields( $unique_fields, $fields );
159 continue;
160 }
161 }
162
163 $res = wp_remote_get(
164 'https://api.moosend.com/v3/lists/' . $list_id . '/details.json?PageSize=1000&apiKey=' . $settings['apiKey'],
165 array(
166 'timeout' => 45,
167 'headers' => $this->get_headers(),
168 )
169 );
170
171 if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore
172 $body = wp_remote_retrieve_body( $res );
173 $body = json_decode( $body, true );
174
175 if ( isset( $body['Context']['CustomFieldsDefinition'] ) && is_array( $body['Context']['CustomFieldsDefinition'] ) ) {
176 $fields = array();
177 foreach ( $body['Context']['CustomFieldsDefinition'] as $d ) {
178 $fields[] = array(
179 'value' => $d['Name'],
180 'label' => $d['Name'],
181 );
182 }
183
184 // Cache the fields.
185 Cache::set( 'optn_int_moosend_fields_' . $this->account_id . '_' . $list_id, wp_json_encode( $fields ) );
186
187 $unique_fields = $this->merge_unique_fields( $unique_fields, $fields );
188 }
189 }
190 }
191
192 return $unique_fields;
193 }
194
195 /**
196 * Merge fields for multiple list
197 *
198 * @param array $existing_fields existing fields.
199 * @param array $new_fields new fields.
200 * @return array
201 */
202 private function merge_unique_fields( array $existing_fields, array $new_fields ): array {
203 foreach ( $new_fields as $field ) {
204 if ( ! array_key_exists( $field['value'], array_column( $existing_fields, 'value' ) ) ) {
205 $existing_fields[] = $field;
206 }
207 }
208
209 return $existing_fields;
210 }
211
212 /**
213 * Gets list for moosend
214 *
215 * @param array $args receive optional data as array.
216 * @return array
217 *
218 * @link https://docs.moosend.com/developers/api-documentation/en/index-en.html#UUID-0e1c5a07-da45-a495-2bd0-a1093a45a806
219 */
220 public function get_lists( $args = array() ): array {
221
222 if ( ! empty( $args['list_ids'] ) ) {
223 return array();
224 }
225 if ( $args['use_cache'] ) {
226 $res = $this->get_cached_lists();
227
228 if ( ! empty( $res ) ) {
229 return $res;
230 }
231 }
232 $settings = $this->get_settings();
233
234 $url = add_query_arg(
235 array(
236 'apiKey' => $settings['apiKey'],
237 'PageSize' => 1000,
238 ),
239 'https://api.moosend.com/v3/lists.json'
240 );
241
242 $res = wp_remote_get(
243 $url,
244 array(
245 'timeout' => 45,
246 'headers' => $this->get_headers(),
247 )
248 );
249
250 if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore
251 $body = wp_remote_retrieve_body( $res );
252 $body = json_decode( $body, true );
253
254 if ( isset( $body['Context']['MailingLists'] ) && is_array( $body['Context']['MailingLists'] ) ) {
255
256 $ret = array();
257
258 foreach ( $body['Context']['MailingLists'] as $d ) {
259 $ret[] = array(
260 'value' => $d['ID'],
261 'label' => $d['Name'],
262 );
263 }
264
265 $this->set_lists_cache( $ret );
266
267 return $ret;
268 }
269 }
270
271 return array();
272 }
273
274 /**
275 * Gets headers with authorization token
276 *
277 * @return array
278 */
279 protected function get_headers() {
280
281 return array(
282 'Content-Type' => 'application/json',
283 'Accept' => 'application/json',
284 );
285 }
286 }
287