PluginProbe
Mailrelay / 1.6
Mailrelay v1.6
3.0 3.0.1 3.0.2 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.8.1 2.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.1.3
mailrelay / mailrelay.php

mailrelay.php in Mailrelay 1.6, at mailrelay.php

591 lines 24.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Mailrelay
4 Plugin URI: http://mailrelay.com
5 Description: Easily sync your Wordpress users with Mailrelay.
6 Version: 1.6
7 Author: Mailrelay.com
8 */
9
10 if (!defined('ABSPATH')) {
11 die('Invalid access.');
12 }
13
14 if (!defined('MAILRELAY_PLUGIN_VERSION')) {
15 define('MAILRELAY_PLUGIN_VERSION', '1.6.0');
16 }
17
18 if (function_exists('is_admin') && is_admin()) {
19 function mailrelay_init() {
20 $result = load_plugin_textdomain('mailrelay', false, dirname(plugin_basename(__FILE__)) . '/languages');
21 }
22
23 function mailrelay_menu() {
24 add_menu_page('Mailrelay', 'Mailrelay', 'manage_options', 'mailrelay', null, plugins_url('mailrelay/mailrelay.png'));
25 add_submenu_page('mailrelay', __('Connection Settings', 'mailrelay'), __('Connection Settings', 'mailrelay'), 'manage_options', 'mailrelay_connection_settings', 'mailrelay_connection_settings');
26 add_submenu_page('mailrelay', __('Posts Settings', 'mailrelay'), __('Posts Settings', 'mailrelay'), 'manage_options', 'mailrelay_posts_settings', 'mailrelay_posts_settings');
27 add_submenu_page('mailrelay', __('Feeds Settings', 'mailrelay'), __('Feeds Settings', 'mailrelay'), 'manage_options', 'mailrelay_feeds_settings', 'mailrelay_feeds_settings');
28 add_submenu_page('mailrelay', __('Sync Users', 'mailrelay'), __('Sync Users', 'mailrelay'), 'manage_options', 'mailrelay_sync_users', 'mailrelay_sync_users');
29 remove_submenu_page('mailrelay', 'mailrelay');
30 }
31
32 function mailrelay_connection_settings() {
33 include 'connection_settings.php';
34 }
35
36 function mailrelay_posts_settings() {
37 include 'posts_settings.php';
38 }
39
40 function mailrelay_feeds_settings() {
41 include 'feeds_settings.php';
42 }
43
44 function mailrelay_sync_users() {
45 include 'sync_users.php';
46 }
47
48 function mailrelay_get_groups() {
49 // Initialize variables
50 $mailrelay_host = get_option('mailrelay_host');
51 $mailrelay_api_key = get_option('mailrelay_api_key');
52
53 // First thing, init
54 $url = 'https://'. $mailrelay_host .'/ccm/admin/api/version/2/&type=json';
55 $curl = curl_init($url);
56
57 // Call getGroups
58 $params = array(
59 'function' => 'getGroups',
60 'apiKey' => $mailrelay_api_key,
61 'sortField' => 'name',
62 'sortOrder' => 'ASC'
63 );
64 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
65 curl_setopt($curl, CURLOPT_POST, 1);
66 curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
67 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
68 curl_setopt($curl, CURLOPT_SSLVERSION, 3);
69
70 $headers = array(
71 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
72 );
73 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
74
75 $result = curl_exec($curl);
76 return json_decode($result);
77 }
78
79 function mailrelay_create_post_meta_box() {
80 add_meta_box('mailrelay-meta-box', 'Mailrelay', 'mailrelay_post_meta_box', 'post', 'normal', 'high');
81 }
82
83 function mailrelay_post_meta_box($object, $box) {
84 $mailrelay_group = get_post_meta($object->ID, 'mailrelay-group', true);
85 ?>
86 <p>
87 <label for="mailrelay-group"><?php echo _e('Please select a group to send this post', 'mailrelay'); ?></label>
88 <br /><br />
89 <select name="mailrelay-group" id="mailrelay-group" onchange="if (this.value != '') {document.getElementById('spanMailrelayTitle').style.display = '';} else {document.getElementById('spanMailrelayTitle').style.display = 'none';}">
90 <option value=""><?php echo _e("- Don't send -", 'mailrelay'); ?></option>
91 <?php
92 $result = mailrelay_get_groups();
93 if ($result && $result->status == 1) {
94 if (is_object($result)) {
95 $data = $result->data;
96 if (is_array($data)) {
97 foreach($data as $key => $value) {
98 ?>
99 <option value="<?php echo $data[$key]->id; ?>"<?php echo $mailrelay_group == $data[$key]->id ? ' selected="selected"' : ''; ?>><?php echo $data[$key]->name; ?></option>
100 <?php
101 }
102 }
103 }
104 }
105 ?>
106 </select>
107 <span id="spanMailrelayTitle" style="<?php echo $mailrelay_group == '' ? 'display:none;' : '' ?>">
108 <br /><br />
109 <label for="mailrelay-title"><?php echo _e('Alternative Title', 'mailrelay'); ?></label>
110 <br /><br />
111 <input type="text" name="mailrelay-title" value="<?php echo get_post_meta($object->ID, 'mailrelay-title', true); ?>" size="70" placeholder="<?php echo _e('If you enter a value in this field it will be used instead of post title', 'mailrelay'); ?>" />
112 </span>
113 <input type="hidden" name="mailrelay_meta_box_nonce" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>" />
114 </p>
115 <?php
116 }
117
118 function mailrelay_save_post_meta_box($post_id) {
119 // AJAX? Not used here
120 if (defined('DOING_AJAX') && DOING_AJAX) {
121 return;
122 }
123 // Check user permissions
124 if (!current_user_can('edit_post', $post_id)) {
125 return;
126 }
127 // Return if it's a post revision
128 if (false !== wp_is_post_revision($post_id)) {
129 return;
130 }
131 // Sanitizing the alternative title
132 $_REQUEST['mailrelay-title'] = trim($_REQUEST['mailrelay-title']);
133
134 if ($_REQUEST['mailrelay-group'] == '')
135 {
136 delete_post_meta($post_id, 'mailrelay-group');
137 delete_post_meta($post_id, 'mailrelay-title');
138 }
139 else
140 {
141 $post_meta = get_post_meta($post_id);
142 if (empty($post_meta))
143 {
144 add_post_meta($post_id, 'mailrelay-group', $_REQUEST['mailrelay-group'], true);
145 add_post_meta($post_id, 'mailrelay-title', $_REQUEST['mailrelay-title'], true);
146 }
147 else
148 {
149 update_post_meta($post_id, 'mailrelay-group', $_REQUEST['mailrelay-group']);
150 update_post_meta($post_id, 'mailrelay-title', $_REQUEST['mailrelay-title']);
151 }
152 }
153 }
154
155 function mailrelay_publish_post($post_id, $post) {
156 // Get custom field value
157 $mailrelay_group = (int)stripslashes(get_post_meta($post_id, 'mailrelay-group', true));
158 if ($mailrelay_group == 0) {
159 return;
160 }
161 $mailrelay_title = get_post_meta($post_id, 'mailrelay-title', true);
162
163 // These will be entered by user.
164 $mailrelay_host = get_option('mailrelay_host');
165 $mailrelay_api_key = get_option('mailrelay_api_key');
166 $mailrelay_unsubscribe = get_option('mailrelay_newsletter_unsubscribe');
167
168 // First thing, ger config
169 $url = 'https://'. $mailrelay_host .'/ccm/admin/api/version/2/&type=json';
170 $curl = curl_init($url);
171
172 // Post title
173 $post_title = $mailrelay_title != '' ? $mailrelay_title : get_the_title();
174
175 // Post content
176 $post_content = apply_filters('the_content', $post->post_content);
177 $post_content = trim(str_replace(']]>', ']]&gt;', $post_content));
178
179 // Call addCampaign
180 $params = array(
181 'function' => 'addCampaign',
182 'apiKey' => $mailrelay_api_key,
183 'subject' => $post_title,
184 'mailboxFromId' => (int)get_option('mailrelay_newsletter_from'),
185 'mailboxReplyId' => (int)get_option('mailrelay_newsletter_reply_to'),
186 'mailboxReportId' => (int)get_option('mailrelay_newsletter_report_to'),
187 'emailReport' => true,
188 'groups' => $mailrelay_group,
189 'text' => null,
190 'html' => mailrelay_build_template($post_id, $post_title, $post_content, $mailrelay_unsubscribe),
191 'packageId' => 6,
192 'campaignFolderId' => 1
193 );
194 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
195 curl_setopt($curl, CURLOPT_POST, 1);
196 curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
197 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
198 curl_setopt($curl, CURLOPT_SSLVERSION, 3);
199
200 $headers = array(
201 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
202 );
203 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
204
205 $result = curl_exec($curl);
206 $jsonResult = json_decode($result);
207
208 if (is_object($jsonResult)) {
209 if (isset($jsonResult->data) && $jsonResult->data != '') {
210 // Call sendCampaign
211 $params = array(
212 'function' => 'sendCampaign',
213 'apiKey' => $mailrelay_api_key,
214 'id' => $jsonResult->data
215 );
216 curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
217 $headers = array(
218 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
219 );
220 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
221 $result = curl_exec($curl);
222 }
223 }
224 }
225
226 function mailrelay_publish_feed($post_id, $post) {
227 // These will be entered by user.
228 $mailrelay_host = get_option('mailrelay_host');
229 $mailrelay_api_key = get_option('mailrelay_api_key');
230 $mailrelay_feeds_group = (int)get_option('mailrelay_feeds_group');
231 $mailrelay_unsubscribe = get_option('mailrelay_feed_unsubscribe');
232 if ($mailrelay_feeds_group == 0) {
233 return;
234 }
235
236 // First thing, ger config
237 $url = 'https://'. $mailrelay_host .'/ccm/admin/api/version/2/&type=json';
238 $curl = curl_init($url);
239
240 // Post title
241 $post_title = get_the_title();
242
243 // Post content
244 $post_content = apply_filters('the_content', $post->post_content);
245 $post_content = trim(str_replace(']]>', ']]&gt;', $post_content));
246
247 // Call addCampaign
248 $params = array(
249 'function' => 'addCampaign',
250 'apiKey' => $mailrelay_api_key,
251 'subject' => $post_title,
252 'mailboxFromId' => (int)get_option('mailrelay_feed_from'),
253 'mailboxReplyId' => (int)get_option('mailrelay_feed_reply_to'),
254 'mailboxReportId' => (int)get_option('mailrelay_feed_report_to'),
255 'emailReport' => true,
256 'groups' => $mailrelay_feeds_group,
257 'text' => null,
258 'html' => mailrelay_build_template($post_id, $post_title, $post_content, $mailrelay_unsubscribe),
259 'packageId' => 6,
260 'campaignFolderId' => 1
261 );
262 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
263 curl_setopt($curl, CURLOPT_POST, 1);
264 curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
265 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
266 curl_setopt($curl, CURLOPT_SSLVERSION, 3);
267
268 $headers = array(
269 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
270 );
271 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
272
273 $result = curl_exec($curl);
274 $jsonResult = json_decode($result);
275
276 if (is_object($jsonResult)) {
277 if (isset($jsonResult->data) && $jsonResult->data != '') {
278 // Call sendCampaign
279 $params = array(
280 'function' => 'sendCampaign',
281 'apiKey' => $mailrelay_api_key,
282 'id' => $jsonResult->data
283 );
284 curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
285 $headers = array(
286 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
287 );
288 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
289 $result = curl_exec($curl);
290 }
291 }
292 }
293
294 function mailrelay_do_feed($post_id, $post) {
295 $post_meta = get_post_meta($post_id);
296 if (!empty($post_meta)) {
297 if (get_post_meta($post_id, 'mailrelay-feed-processed', true) == '1') {
298 $is_new = false;
299 } else {
300 $is_new = true;
301 }
302 } else {
303 add_post_meta($post_id, 'mailrelay-feed-processed', '0', true);
304 $is_new = true;
305 }
306 if ($is_new) {
307 mailrelay_publish_feed($post_id, $post);
308 update_post_meta($post_id, 'mailrelay-feed-processed', '1');
309 }
310 }
311
312 function mailrelay_build_template($post_id, $post_title, $post_content, $mailrelay_unsubscribe) {
313 $post_content = substrhtml(nl2br($post_content), 0, 300) . '...';
314 $post_link = get_permalink($post_id);
315 if (strpos($post_link, '?') === false)
316 {
317 $post_link .= '?';
318 }
319 else
320 {
321 $post_link .= '&';
322 }
323 $post_link .= 'utm_source=newsletter&utm_medium=articulo&utm_campaign=blog';
324
325 if ($mailrelay_unsubscribe == '1') {
326 $unsubscribe_link = '<a href="[unsubscribe_url_direct]">' . __('Unsubscribe', 'mailrelay') . '</a>';
327 } else {
328 $unsubscribe_link = '';
329 }
330
331 $more = '' . __('View more', 'mailrelay') . '';
332 $date = '' . __('Posted: ', 'mailrelay') . date('Y-m-d H:i:s');
333
334 $aux = pathinfo(__FILE__);
335 $content = file_get_contents($aux['dirname'] . '/newletter.html');
336 $content = str_replace('{$title}', $post_title, $content);
337 $content = str_replace('{$date}', $date, $content);
338 $content = str_replace('{$content}', $post_content, $content);
339 $content = str_replace('{$link}', $post_link, $content);
340 $content = str_replace('{$more}', $more, $content);
341 $content = str_replace('{$unsubscribe}', $unsubscribe_link, $content);
342
343 return $content;
344 }
345
346 function substrhtml($str, $start, $len) {
347 $str_clean = substr(strip_tags($str),$start,$len);
348 $pos = strrpos($str_clean, ' ');
349 if ($pos === false)
350 {
351 $str_clean = substr(strip_tags($str),$start,$len);
352 }
353 else
354 {
355 $str_clean = substr(strip_tags($str),$start,$pos);
356 }
357
358 if (preg_match_all('/\<[^>]+>/is',$str,$matches,PREG_OFFSET_CAPTURE))
359 {
360 for($i=0;$i<count($matches[0]);$i++)
361 {
362 if($matches[0][$i][1] < $len)
363 {
364 $str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);
365 }
366 else if(preg_match('/\<[^>]+>$/is',$matches[0][$i][0]))
367 {
368 $str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);
369 break;
370 }
371 }
372 return $str_clean;
373 }
374 else
375 {
376 $string = substr($str,$start,$len);
377 $pos = strrpos($string, ' ');
378 if ($pos === false)
379 {
380 return substr($str,$start,$len);
381 }
382 return substr($str,$start,$pos);
383 }
384 }
385
386 add_action('init', 'mailrelay_init');
387 add_action('admin_menu', 'mailrelay_menu');
388
389 if (get_option('mailrelay_newsletter_post') == '1') {
390 add_action('admin_menu', 'mailrelay_create_post_meta_box');
391 add_action('save_post', 'mailrelay_save_post_meta_box', 10, 1);
392 add_action('publish_post', 'mailrelay_publish_post', 10, 2);
393 }
394
395 if (get_option('mailrelay_newsletter_feed') == '1') {
396 add_action('publish_post', 'mailrelay_do_feed', 10, 2);
397 }
398
399 if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'mailrelay_save_connection_settings')) {
400 //Form data sent
401 $mailrelay_host = $_REQUEST['mailrelay_host'];
402 update_option('mailrelay_host', trim($mailrelay_host));
403
404 $mailrelay_api_key = $_REQUEST['mailrelay_api_key'];
405 update_option('mailrelay_api_key', trim($mailrelay_api_key));
406
407 // These will be entered by user.
408 $mailrelay_host = get_option('mailrelay_host');
409 $mailrelay_api_key = get_option('mailrelay_api_key');
410
411 // First thing, init
412 $url = 'https://' . $mailrelay_host . '/ccm/admin/api/version/2/&type=json';
413 $curl = curl_init($url);
414
415 // Call getGroups
416 $params = array(
417 'function' => 'getGroups',
418 'apiKey' => $mailrelay_api_key,
419 'sortField' => 'name',
420 'sortOrder' => 'ASC'
421 );
422 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
423 curl_setopt($curl, CURLOPT_POST, 1);
424 curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
425 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
426 curl_setopt($curl, CURLOPT_SSLVERSION, 3);
427
428 $headers = array(
429 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
430 );
431 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
432
433 $result = curl_exec($curl);
434 $jsonResult = json_decode($result);
435
436 if (!$jsonResult || trim($jsonResult->status) != 1) {
437 global $message;
438 if (is_object($jsonResult)) {
439 if ($jsonResult->error != '') {
440 $message = '<div class="error"><p>' . $jsonResult->error . '</p></div>';
441 } else {
442 $message = '<div class="error"><p>Your account does not have an API key. Please, generate one in your Mailrelay\'s account: Settings -> API access -> Generate new API key.</p></div>';
443 }
444 } else {
445 $message = '<div class="error"><p>Invalid host. Please Retry.</p></div>';
446 }
447 } else {
448 $message = '<div class="updated"><p>Data saved succesfully.</p></div>';
449 }
450 }
451
452 if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'mailrelay_save_posts_settings')) {
453 //Form data sent
454 $mailrelay_newsletter_post = isset($_REQUEST['mailrelay_newsletter_post']) ? $_REQUEST['mailrelay_newsletter_post'] : '';
455 update_option('mailrelay_newsletter_post', $mailrelay_newsletter_post);
456
457 $mailrelay_newsletter_from = isset($_REQUEST['mailrelay_newsletter_from']) ? $_REQUEST['mailrelay_newsletter_from'] : '';
458 update_option('mailrelay_newsletter_from', $mailrelay_newsletter_from);
459
460 $mailrelay_newsletter_reply_to = isset($_REQUEST['mailrelay_newsletter_reply_to']) ? $_REQUEST['mailrelay_newsletter_reply_to'] : '';
461 update_option('mailrelay_newsletter_reply_to', $mailrelay_newsletter_reply_to);
462
463 $mailrelay_newsletter_report_to = isset($_REQUEST['mailrelay_newsletter_report_to']) ? $_REQUEST['mailrelay_newsletter_report_to'] : '';
464 update_option('mailrelay_newsletter_report_to', $mailrelay_newsletter_report_to);
465
466 $mailrelay_newsletter_unsubscribe = isset($_REQUEST['mailrelay_newsletter_unsubscribe']) ? $_REQUEST['mailrelay_newsletter_unsubscribe'] : '';
467 update_option('mailrelay_newsletter_unsubscribe', $mailrelay_newsletter_unsubscribe);
468
469 $message = '<div class="updated"><p>Data saved succesfully.</p></div>';
470 }
471
472 if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'mailrelay_save_feeds_settings')) {
473 //Form data sent
474 $mailrelay_newsletter_feed = isset($_REQUEST['mailrelay_newsletter_feed']) ? $_REQUEST['mailrelay_newsletter_feed'] : '';
475 update_option('mailrelay_newsletter_feed', $mailrelay_newsletter_feed);
476
477 $mailrelay_feeds_group = isset($_REQUEST['mailrelay_feeds_group']) ? $_REQUEST['mailrelay_feeds_group'] : '';
478 update_option('mailrelay_feeds_group', $mailrelay_feeds_group);
479
480 $mailrelay_feed_from = isset($_REQUEST['mailrelay_feed_from']) ? $_REQUEST['mailrelay_feed_from'] : '';
481 update_option('mailrelay_feed_from', $mailrelay_feed_from);
482
483 $mailrelay_feed_reply_to = isset($_REQUEST['mailrelay_feed_reply_to']) ? $_REQUEST['mailrelay_feed_reply_to'] : '';
484 update_option('mailrelay_feed_reply_to', $mailrelay_feed_reply_to);
485
486 $mailrelay_feed_report_to = isset($_REQUEST['mailrelay_feed_report_to']) ? $_REQUEST['mailrelay_feed_report_to'] : '';
487 update_option('mailrelay_feed_report_to', $mailrelay_feed_report_to);
488
489 $mailrelay_feed_unsubscribe = isset($_REQUEST['mailrelay_feed_unsubscribe']) ? $_REQUEST['mailrelay_feed_unsubscribe'] : '';
490 update_option('mailrelay_feed_unsubscribe', $mailrelay_feed_unsubscribe);
491
492 $message = '<div class="updated"><p>Data saved succesfully.</p></div>';
493 }
494
495 if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'mailrelay_sync_users_group')) {
496 $querystr = "SELECT * FROM $wpdb->users";
497 $users = $wpdb->get_results($querystr, OBJECT);
498 $groups = $_REQUEST['group'];
499
500 // These will be entered by user.
501 $mailrelay_host = get_option('mailrelay_host');
502 $mailrelay_api_key = get_option('mailrelay_api_key');
503
504 // First thing, init
505 $url = 'https://' . $mailrelay_host . '/ccm/admin/api/version/2/&type=json';
506 $curl = curl_init($url);
507
508 $added = 0;
509 $updated = 0;
510 $fail = 0;
511
512 foreach ($users as $user) {
513 $user->user_email;
514 // Call getSubscribers
515 $params = array(
516 'function' => 'getSubscribers',
517 'apiKey' => $mailrelay_api_key,
518 'email' => $user->user_email,
519 );
520 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
521 curl_setopt($curl, CURLOPT_POST, 1);
522 curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
523 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
524 curl_setopt($curl, CURLOPT_SSLVERSION, 3);
525
526 $headers = array(
527 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
528 );
529 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
530
531 $result = curl_exec($curl);
532 $jsonResult = json_decode($result);
533
534 if (count($jsonResult->data) > 0) {
535 $params = array(
536 'function' => 'updateSubscriber',
537 'apiKey' => $mailrelay_api_key,
538 'id' => $jsonResult->data[0]->id,
539 'email' => $user->user_email,
540 'name' => $user->display_name,
541 'groups' => $groups
542 );
543 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
544
545 $headers = array(
546 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
547 );
548 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
549
550 $result = curl_exec($curl);
551 $jsonResult = json_decode($result);
552
553 if ($jsonResult->status == 1) {
554 $updated++;
555 } else {
556 $fail++;
557 }
558 } else {
559 $params = array(
560 'function' => 'addSubscriber',
561 'apiKey' => $mailrelay_api_key,
562 'email' => $user->user_email,
563 'name' => $user->display_name,
564 'groups' => $groups
565 );
566
567 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
568
569 $headers = array(
570 'X-Request-Origin: Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version')
571 );
572 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
573
574 $result = curl_exec($curl);
575 $jsonResult = json_decode($result);
576
577 if ($jsonResult->status == 1) {
578 $added++;
579 } else {
580 $fail++;
581 }
582 }
583 }
584
585 $message = '<div class="updated"><p>The Mailrelay sync has finished successfully. Next you can check the results of the sync:<ul>';
586 $message .= '<li>New users synced:&nbsp;' . $added . '</li>';
587 $message .= '<li>Updated users:&nbsp;' . $updated . '</li>';
588 $message .= '<li>Failed users:&nbsp;' . $fail . '</li>';
589 $message .= '</ul></p></div>';
590 }
591 }