PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / campaign-monitor / includes / functions.php

functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.3, at integrations/campaign-monitor/includes/functions.php

325 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package AutomatorWP\CampaignMonitor\Functions
6 * @since 1.0.0
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Helper function to get the Campaign Monitor url
13 *
14 * @since 1.0.0
15 *
16 * @return string
17 */
18 function automatorwp_campaign_monitor_get_url() {
19 return 'https://api.createsend.com/api/v3.3/';
20 }
21
22 /**
23 * Helper function to get the Campaign Monitor API parameters
24 *
25 * @since 1.0.0
26 *
27 * @return array|false
28 */
29 function automatorwp_campaign_monitor_get_api() {
30
31 $url = automatorwp_campaign_monitor_get_url();
32 $client_id = automatorwp_campaign_monitor_get_option('client_id', '');
33 $api_key = automatorwp_campaign_monitor_get_option('api_key', '');
34
35 if (empty($client_id) || empty($api_key)) {
36 return false;
37 }
38
39 return array(
40 'url' => $url,
41 'client_id' => $client_id,
42 'api_key' => $api_key,
43 );
44
45 }
46
47
48 /**
49 * Get lists from Campaign Monitor
50 *
51 * @since 1.0.0
52 *
53 * @return array
54 */
55 function automatorwp_campaign_monitor_get_lists() {
56
57 $lists = array();
58
59 $api = automatorwp_campaign_monitor_get_api();
60
61 if ( !$api ) {
62 return $lists;
63 }
64
65 // To get lists
66 $response = wp_remote_get( $api['url'] . "clients/" . $api['client_id'] . "/lists.json", array(
67 'headers' => array(
68 'Authorization' => 'Basic ' . base64_encode( $api['api_key'] ),
69 )
70 ));
71
72 if ( is_wp_error( $response ) ) {
73 return $lists;
74 }
75
76 $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
77
78 // Check for errors in the response
79 if ( is_array( $response_body ) ) {
80
81 foreach ( $response_body as $list ) {
82
83 if ( isset( $list['ListID'] ) && isset( $list['Name'] ) ) {
84
85 $lists[] = array(
86 'id' => $list['ListID'],
87 'name' => $list['Name'],
88 );
89 }
90 }
91 }
92
93 return $lists;
94 }
95
96
97 /**
98 * Get list from Campaign Monitor
99 *
100 * @since 1.0.0
101 *
102 * @param stdClass $field
103 *
104 * @return array
105 */
106 function automatorwp_campaign_monitor_options_cb_list( $field ) {
107
108 // Setup vars
109 $value = $field->escaped_value;
110 $none_value = 'any';
111 $none_label = __( 'any list', 'automatorwp' );
112 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
113
114 if( ! empty( $value ) ) {
115 if( ! is_array( $value ) ) {
116 $value = array( $value );
117 }
118
119 foreach( $value as $list_id ) {
120
121 // Skip option none
122 if( $list_id === $none_value ) {
123 continue;
124 }
125
126 $options[$list_id] = automatorwp_campaign_monitor_get_list_title( $list_id );
127 }
128 }
129
130 return $options;
131 }
132
133 /**
134 * Get the list name
135 *
136 * @since 1.0.0
137 *
138 * @param string $list_id
139 *
140 * @return array
141 */
142 function automatorwp_campaign_monitor_get_list_title( $list_id ) {
143
144 if( empty( $list_id ) ) {
145 return '';
146 }
147
148 $api = automatorwp_campaign_monitor_get_api();
149
150 if ( !$api ) {
151 return '';
152 }
153
154 // To get list details
155 $response = wp_remote_get( $api['url'] . 'lists/' . $list_id . '.json', array(
156 'headers' => array(
157 'Authorization' => 'Basic ' . base64_encode( $api['api_key'] ) ),
158
159 )
160 );
161
162 if ( is_wp_error( $response ) ) {
163 return '';
164 }
165
166 $response_code = wp_remote_retrieve_response_code( $response );
167
168 if ( $response_code !== 200 ) {
169 return '';
170 }
171
172 $list = json_decode( wp_remote_retrieve_body( $response ), true );
173
174 return $list['Title'];
175 }
176
177 /**
178 * Get subscriber to list
179 *
180 * @since 1.0.0
181 *
182 * @param string $list_id
183 * @param string $subscriber_email
184 *
185 * @return array
186 */
187 function automatorwp_campaign_monitor_get_subscriber( $list_id, $subscriber_email ) {
188
189 $lists = array();
190
191 $api = automatorwp_campaign_monitor_get_api();
192
193 if ( !$api ) {
194 return $lists;
195 }
196
197 // To get subscriber
198 $url = $api['url'] . "subscribers/" . $list_id . ".json?email=" . urlencode( $subscriber_email );
199
200 $response = wp_remote_get( $url, array(
201 'headers' => array(
202 'Authorization' => 'Basic ' . base64_encode( $api['api_key'] ),
203 ),
204 ) );
205
206 if ( is_wp_error( $response ) ) {
207 return;
208 }
209
210 return $response;
211 }
212
213 /**
214 * Add subscriber to list
215 *
216 * @since 1.0.0
217 *
218 * @param string $list_id
219 * @param array $subscriber_data
220 *
221 * @return array
222 */
223 function automatorwp_campaign_monitor_add_subscriber( $list_id, $subscriber_data ) {
224
225 $lists = array();
226
227 $api = automatorwp_campaign_monitor_get_api();
228
229 if ( !$api ) {
230 return $lists;
231 }
232
233 //add subscriber
234 $url = $api['url'] . "subscribers/" . $list_id . ".json";
235
236 $response = wp_remote_post($url, array(
237 'headers' => array(
238 'Authorization' => 'Basic ' . base64_encode( $api['api_key'] ),
239 ),
240 'body' => json_encode( $subscriber_data ),
241 ));
242
243 if ( is_wp_error( $response ) ) {
244 return;
245 }
246
247 return $response;
248 }
249
250 /**
251 * Update subscriber to list
252 *
253 * @since 1.0.0
254 *
255 * @param string $list_id
256 * @param array $subscriber_data
257 * @param string $subscriber_email
258 *
259 * @return array
260 */
261 function automatorwp_campaign_monitor_update_subscriber( $list_id, $subscriber_data, $subscriber_email ) {
262
263 $lists = array();
264
265 $api = automatorwp_campaign_monitor_get_api();
266
267 if ( !$api ) {
268 return $lists;
269 }
270
271 // Update subscriber
272 $url = $api['url'] . "subscribers/" . $list_id . ".json?email=" . urlencode( $subscriber_email );
273
274 $response = wp_remote_request($url, array(
275 'method' => 'PUT',
276 'headers' => array(
277 'Authorization' => 'Basic ' . base64_encode( $api['api_key'] ),
278 ),
279 'body' => json_encode( $subscriber_data ),
280 ));
281
282
283 if ( is_wp_error( $response ) ) {
284 return;
285 }
286
287 return $response;
288 }
289
290 /**
291 * Remove subscriber from list
292 *
293 * @since 1.0.0
294 *
295 * @param string $list_id
296 * @param string $subscriber_email
297 *
298 * @return array
299 */
300 function automatorwp_campaign_monitor_delete_subscriber( $list_id, $subscriber_email ) {
301
302 $lists = array();
303
304 $api = automatorwp_campaign_monitor_get_api();
305
306 if ( !$api ) {
307 return $lists;
308 }
309
310 // To remove subscriber
311 $url = $api['url'] . "subscribers/" . $list_id . ".json?email=" . urlencode( $subscriber_email );
312
313 $response = wp_remote_request( $url, array(
314 'method' => 'DELETE',
315 'headers' => array(
316 'Authorization' => 'Basic ' . base64_encode($api['api_key'] ),
317 ),
318 ));
319
320 if (is_wp_error($response)) {
321 return;
322 }
323
324 return $response;
325 }