· 5 years ago · Feb 02, 2021, 05:34 AM
1<?php
2
3define('EE_ADMIN', true);
4
5/**
6 * Description of eeadmin
7 *
8 * @author ElasticEmail
9 */
10class eeadmin
11{
12 /**
13 * Holds the values to be used in the fields callbacks
14 */
15 private $defaultOptions = ['ee_enable' => 'no', 'ee_apikey' => null, 'ee_emailtype' => 'marketing'],
16 $options,
17 $initAPI = false,
18 $subscribe_status = false;
19 public $theme_path;
20
21 /**
22 * Start up
23 */
24 public function __construct($pluginpath)
25 {
26 $this->theme_path = $pluginpath;
27 add_action('init', [$this, 'WooCommerce_email']);
28 add_action('init', [$this, 'WooCommerce_name']);
29 add_action('admin_init', [$this, 'init_options']);
30 add_action('plugins_loaded', [$this, 'eesender_load_textdomain']);
31 $this->options = get_option('ee_options', $this->defaultOptions);
32
33 add_action('admin_menu', [$this, 'add_menu']);
34 }
35
36 public function eesender_load_textdomain()
37 {
38 load_plugin_textdomain('elastic-email-sender', false, basename(dirname(__FILE__)) . '/languages');
39 }
40
41 // Added admin menu
42 public function add_menu()
43 {
44 add_action('admin_enqueue_scripts', array($this, 'custom_admin_scripts'));
45
46 add_menu_page('Elastic Email Sender', 'Elastic Email Sender', 'manage_options', 'elasticemail-settings', [$this, 'show_settings'], plugins_url('/assets/images/icon.png', dirname(__FILE__)));
47 add_submenu_page('elasticemail-settings', 'Settings', __('Settings', 'elastic-email-sender'), 'manage_options', 'elasticemail-settings', [$this, 'show_settings']);
48 add_submenu_page('elasticemail-settings', 'Reports', __('Reports', 'elastic-email-sender'), 'manage_options', 'elasticemail-report', [$this, 'show_reports']);
49 add_submenu_page('elasticemail-settings', 'Send Test', __('Send test', 'elastic-email-sender'), 'manage_options', 'elasticemail-send-test', [$this, 'show_sendtest']);
50 }
51
52 /**
53 *
54 */
55 public function custom_admin_scripts()
56 {
57 if (is_admin()) {
58
59 $plugin_path = plugins_url() . '/' . get_option('ees_plugin_dir_name');
60 wp_register_script('eesender-jquery', $plugin_path . '/assets/lib/jquery.min.js', '', 3.3, true);
61 wp_register_script('eesender-chart-script', $plugin_path . '/assets/lib/chart.min.js', '', 2.7, true);
62 wp_register_script('eesender-send-test', $plugin_path . '/assets/js/ees_sendTest.min.js', '', 1.1, true);
63 wp_register_style('eesender-bootstrap-grid', $plugin_path . '/assets/lib/bootstrap-grid.min.css', '', 4.1, false);
64 wp_register_style('eesender-css', $plugin_path . '/assets/css/ees_admin.min.css', '', 1.1, false);
65
66 wp_localize_script('eesender-send-test', 'ees_localize_data',
67 [
68 'token' => get_option('ee-apikey'),
69 'adminUrl' => get_admin_url()
70 ]
71 );
72 }
73 }
74
75
76 // Load Elastic Email settings
77 public function show_settings()
78 {
79 $this->initAPI();
80 try {
81 $accountAPI = new \ElasticEmailClient\Account();
82 $statusToSendEmailAPI = new \ElasticEmailClient\Account();
83 $error = null;
84 $account = $accountAPI->Load();
85 $this->statusToSendEmail();
86 } catch (ElasticEmailClient\ApiException $e) {
87 $error = $e->getMessage();
88 $account = array();
89 $statusToSendEmail = [];
90 }
91
92 if (isset($account['data']['statusnumber'])) {
93 if ($account['data']['statusnumber'] > 0) {
94 $accountstatus = $account['data']['statusnumber'];
95 } else {
96 $accountstatus = 'Please conect to Elastic Email API';
97 }
98 update_option('ees-connecting-status', 'connecting');
99 } else {
100 $accountstatus = 'Please connect to Elastic Email API';
101 update_option('ees-connecting-status', 'disconnected');
102 }
103
104 if (isset($account['data']['email'])) {
105 update_option('ee_from_email', $account['data']['email']);
106 update_option('ee_accountemail', $account['data']['email']);
107 }
108
109 $accountdailysendlimit = '';
110 if (isset($account['data']['actualdailysendlimit'])) {
111 $accountdailysendlimit = $account['data']['actualdailysendlimit'];
112 }
113
114 if (isset($account['data']['publicaccountid'])) {
115 $this->publicid = $account['data']['publicaccountid'];
116 update_option('ee_publicaccountid', $this->publicid);
117 }
118
119 if (isset($account['data']['enablecontactfeatures'])) {
120 update_option('ee_enablecontactfeatures', $account['data']['enablecontactfeatures']);
121 }
122
123 if (isset($account['data']['requiresemailcredits'])) {
124 $requiresemailcredits = $account['data']['requiresemailcredits'];
125 }
126
127 if (isset($account['data']['emailcredits'])) {
128 $emailcredits = $account['data']['emailcredits'];
129 }
130
131 if (isset($account['data']['requiresemailcredits'])) {
132 $requiresemailcredits = $account['data']['requiresemailcredits'];
133 }
134
135 if (isset($account['data']['issub'])) {
136 $issub = $account['data']['issub'];
137 }
138
139 if (isset($account['data']['apikey'])) {
140 update_option('ee-apikey', $account['data']['apikey']);
141 }
142
143 if (get_option('ee_accountemail') !== null) {
144 $status = 'A';
145 $this->addToUserList($status);
146 }
147
148 require_once($this->theme_path . '/template/t-ees_admin.php');
149 return;
150 }
151
152 public function addToUserList($status)
153 {
154 try {
155 $addToUserListAPI = new \ElasticEmailClient\Contact();
156 $error = null;
157 $sourceUrl = get_site_url();
158 $addToUserList = $addToUserListAPI->Add('d0bcb758-a55c-44bc-927c-34f48d5db864', get_option('ee_accountemail'), ['55c8fa37-4c77-45d0-8675-0937d034c605'], [], $status, get_site_url(), null, null, null, null, null, false, null, null, [], null);
159 } catch (Exception $ex) {
160 $addToUserList = [];
161 }
162 }
163
164 public function statusToSendEmail()
165 {
166 $this->initAPI();
167 try {
168 $statusToSendEmailAPI = new \ElasticEmailClient\Account();
169 $error = null;
170 $statusToSendEmail = $statusToSendEmailAPI->GetAccountAbilityToSendEmail();
171 update_option('elastic-email-to-send-status', $statusToSendEmail['data']);
172 } catch (Exception $ex) {
173 $statusToSendEmail = [];
174 }
175 return;
176 }
177
178 //Initialization Elastic Email API
179 public function initAPI()
180 {
181 if ($this->initAPI === true) {
182 return;
183 }
184
185 //Loads Elastic Email Client
186 chmod($this->theme_path . '/api', 0777);
187 require_once($this->theme_path . '/api/ElasticEmailClient.php');
188 if (empty($this->options['ee_apikey']) === false) {
189 \ElasticEmailClient\ApiClient::SetApiKey($this->options['ee_apikey']);
190 }
191 $this->initAPI = true;
192 }
193
194 public function show_reports()
195 {
196 $this->initAPI();
197
198 if (isset($_POST['daterange'])) {
199 $daterangeselect = $_POST['daterange'];
200 if ($daterangeselect === 'last-mth') {
201 $from = date('c', strtotime('-30 days'));
202 $to = date('c');
203 }
204 if ($daterangeselect === 'last-wk') {
205 $from = date('c', strtotime('-7 days'));
206 $to = date('c');
207 }
208 if ($daterangeselect === 'last-2wk') {
209 $from = date('c', strtotime('-14 days'));
210 $to = date('c');
211 }
212 } else {
213 $from = date('c', strtotime('-30 days'));
214 $to = date('c');
215 }
216
217 try {
218 $LogAPI = new \ElasticEmailClient\Log();
219 $error = null;
220 $LogAPI_json = $LogAPI->Summary($from, $to, null, null, null);
221
222 if ($LogAPI_json['data']['logstatussummary']['emailtotal'] !== null) {
223 $total = $LogAPI_json['data']['logstatussummary']['emailtotal'];
224 $delivered = $LogAPI_json['data']['logstatussummary']['delivered'];
225 $opened = $LogAPI_json['data']['logstatussummary']['opened'];
226 $bounced = $LogAPI_json['data']['logstatussummary']['bounced'];
227 $clicked = $LogAPI_json['data']['logstatussummary']['clicked'];
228 $unsubscribed = $LogAPI_json['data']['logstatussummary']['unsubscribed'];
229 } else {
230 $total = 1;
231 $delivered = 1;
232 $opened = 1;
233 $bounced = 1;
234 $clicked = 1;
235 $unsubscribed = 1;
236 }
237
238 } catch (ElasticEmailClient\ApiException $e) {
239 $error = $e->getMessage();
240 $LogList = [];
241 }
242 //Loads the settings template
243 require_once($this->theme_path . '/template/t-ees_reports.php');
244 return;
245 }
246
247 public function show_sendtest()
248 {
249 require_once($this->theme_path . '/template/t-ees_sendtest.php');
250 return;
251 }
252
253 //Initialization custom options
254 public function init_options()
255 {
256 register_setting(
257 'ee_option_group', //Option group
258 'ee_options', //Option name
259 [$this, 'valid_options'] //Sanitize callback
260 );
261 //INIT SECTION
262 add_settings_section(
263 'setting_section_id',
264 null,
265 null,
266 'ee-settings'
267 );
268
269 //INIT FIELD
270 add_settings_field(
271 'ee_enable',
272 __('Select mailer:', 'elastic-email-sender'),
273 [$this, 'enable_input'],
274 'ee-settings',
275 'setting_section_id',
276 [
277 'input_name' => 'ee_enable'
278 ]
279 );
280
281 add_settings_field(
282 'ee_apikey',
283 __('Elastic Email API Key:', 'elastic-email-sender'),
284 [$this, 'input_apikey'],
285 'ee-settings',
286 'setting_section_id',
287 [
288 'input_name' => 'ee_apikey',
289 'width' => 280
290 ]
291 );
292
293 add_settings_field(
294 'ee_emailtype',
295 __('Email type:', 'elastic-email-sender'),
296 [$this, 'emailtype_input'],
297 'ee-settings',
298 'setting_section_id',
299 [
300 'input_name' => 'ee_emailtype'
301 ]
302 );
303
304 if (is_plugin_active('woocommerce/woocommerce.php')) {
305 add_settings_field(
306 'ee_override_wooCommerce',
307 __('Override', 'elastic-email-sender'),
308 [$this, 'override_wooCommerce_input'],
309 'ee-settings',
310 'setting_section_id',
311 [
312 'input_name' => 'ee_override_wooCommerce',
313 'width' => 280
314 ]
315 );
316 }
317
318 add_settings_field(
319 'ee_from_name_config',
320 __('From name (default empty):', 'elastic-email-sender'),
321 [$this, 'from_name_config_input'],
322 'ee-settings', 'setting_section_id',
323 [
324 'input_name' => 'ee_from_name_config',
325 'width' => 280
326 ]
327 );
328
329 add_settings_field(
330 'ee_from_email_config',
331 __('Email FROM (default empty):', 'elastic-email-sender'),
332 [$this, 'from_email_config_input'],
333 'ee-settings', 'setting_section_id',
334 [
335 'input_name' => 'ee_from_email_config',
336 'width' => 280
337 ]
338 );
339
340 }
341
342 /**
343 * Validation plugin options during their update data
344 * @param type $input
345 * @return type
346 */
347 public function valid_options($input)
348 {
349 // If api key have * then use old api key
350 if (strpos($input['ee_apikey'], '*') !== false) {
351 $input['ee_apikey'] = $this->options['ee_apikey'];
352 } else {
353 $input['ee_apikey'] = sanitize_key($input['ee_apikey']);
354 }
355
356 if ($input['ee_enable'] !== 'yes') {
357 $input['ee_enable'] = 'no';
358 }
359 return $input;
360 }
361
362 /**
363 * Get the apikey option and print one of its values
364 */
365 public function input_apikey($arg)
366 {
367 $apikey = $this->options[$arg['input_name']];
368 if (empty($apikey) === false) {
369 $apikey = '**********' . substr($apikey, strlen($apikey) - 5, strlen($apikey));
370 }
371 printf('
372 <input
373 type="text"
374 id="title"
375 name="ee_options[' . $arg['input_name'] . ']"
376 value="' . $apikey . '"
377 style="%s"
378 />',
379 (isset($arg['width']) && $arg['width'] > 0) ? 'width:' . $arg['width'] . 'px' : '');
380 }
381
382 /**
383 * Displays the settings mailer
384 */
385 public function enable_input($arg)
386 {
387 if (!isset($this->options[$arg['input_name']]) || empty($this->options[$arg['input_name']])) {
388 $value = 'no';
389 } else {
390 $value = $this->options[$arg['input_name']];
391 }
392
393 echo '<div class="ee-admin-settings-radio-block">
394 <div class="ee-admin-settings-radio-item">
395 <input
396 type="radio"
397 name="ee_options[' . $arg['input_name'] . ']"
398 value="yes"
399 ' . (($value === 'yes') ? 'checked' : '') . '
400 />
401 <span>' . __('Send all WordPress emails via Elastic Email API.', 'elastic-email-sender') . '</span>
402 </div>
403
404 <div class="ee-admin-settings-radio-item">
405 <input
406 type="radio"
407 name="ee_options[' . $arg['input_name'] . ']"
408 value="no"
409 ' . (($value === 'no') ? 'checked' : '') . '
410 />
411 <span>' . __('Use the defaults Wordpress function to send emails.', 'elastic-email-sender') . '</span>
412 </div>
413 </div>';
414 }
415
416 /**
417 * Displays the settings email type
418 */
419 public function emailtype_input($arg)
420 {
421 if (!isset($this->options[$arg['input_name']]) || empty($this->options[$arg['input_name']])) {
422 $type = 'marketing';
423 } else {
424 $type = $this->options[$arg['input_name']];
425 }
426 echo '
427 <div class="ee-admin-settings-radio-inline">
428 <input
429 type="radio"
430 name="ee_options[' . $arg['input_name'] . ']"
431 value="marketing"
432 ' . (($type === 'marketing') ? 'checked' : '') . '
433 />
434 <span>' . __('Marketing', 'elastic-email-sender') . '</span>
435
436 <input
437 type="radio"
438 name="ee_options[' . $arg['input_name'] . ']"
439 value="transactional"
440 ' . (($type === 'transactional') ? 'checked' : '') . '
441 />
442 <span>' . __('Transactional', 'elastic-email-sender') . '</span>
443 </div>';
444 }
445
446 /**
447 * Displays the settings from name
448 */
449 public function from_name_config_input($arg)
450 {
451 if (!isset($this->options[$arg['input_name']]) || empty($this->options[$arg['input_name']])) {
452 $config_from_name = '';
453 update_option('ee_config_from_name', null);
454 } else {
455 $config_from_name = $this->options[$arg['input_name']];
456 update_option('ee_config_from_name', $config_from_name);
457 /**Adding filter to override wp_mail_from_name field , if the option is checked */
458 if (get_option('ee_config_override_wooCommerce')) {
459 do_action('WooCommerce_name');
460 }
461 }
462 echo '<input
463 type="text"
464 name="ee_options[' . $arg['input_name'] . ']"
465 placeholder="' . __('From name', 'elastic-email-sender') . '"
466 value="' . $config_from_name . '"
467 style="width:' . $arg['width'] . 'px"
468 />';
469 }
470
471 /**
472 * Displays the settings email FROM
473 */
474 public function from_email_config_input($arg)
475 {
476 if (!isset($this->options[$arg['input_name']]) || empty($this->options[$arg['input_name']])) {
477 $config_from_email = '';
478 update_option('ee_config_from_email', null);
479
480 } else {
481 $config_from_email = $this->options[$arg['input_name']];
482 update_option('ee_config_from_email', $config_from_email);
483 /**Adding filter to override wp_mail_from field , if the option is checked */
484 if (get_option('ee_config_override_wooCommerce')) {
485 do_action('WooCommerce_email');
486 }
487
488 }
489 echo '<input
490 type="text"
491 name="ee_options[' . $arg['input_name'] . ']"
492 placeholder="' . __('Email address FROM', 'elastic-email-sender') . '"
493 value="' . $config_from_email . '"
494 style="width:' . $arg['width'] . 'px"
495 />';
496 }
497
498 /**
499 * Display checkbox to override WooCommerce email 'from' and 'fromName'
500 */
501 public function override_wooCommerce_input($arg)
502 {
503 if (!isset($this->options[$arg['input_name']]) || empty($this->options[$arg['input_name']])) {
504 update_option('ee_config_override_wooCommerce', 0);
505 $override = 0;
506 } else {
507 update_option('ee_config_override_wooCommerce', 1);
508 $override = 1;
509 }
510 echo '<div class="ee-admin-settings-radio-block">
511 <input
512 type="checkbox"
513 name="ee_options['. $arg['input_name'] .']"
514 value="yes" ' . (($override === 1) ? 'checked' : '') . '
515 />
516 <span>'. __('WooCommerce fields "Email from" and " From name"', 'elastic-email-sender') .'</span>
517 </div>';
518 }
519
520 /**function that sets sender email based on the FROM email input , also setting FROM email to send test feature */
521 public function set_sender_email()
522 {
523 $sender = get_option('ee_from_email');
524 if (!empty(get_option('ee_config_from_email'))) {
525 $sender = get_option('ee_config_from_email');
526 }
527 return $sender;
528 }
529
530 /** function that sets from name based on the form name input , also setting FROM name to send test feature */
531 public function set_sender_name()
532 {
533 $sender = 'Wordpress';
534 if (!empty(get_option('ee_config_from_name'))) {
535 $sender = get_option('ee_config_from_name');
536 }
537 return $sender;
538 }
539
540 /** function that based on override option and setted FROM email input adds filter for wp_mail_from to override wooCommerce settings */
541 public function WooCommerce_email()
542 {
543 if (get_option('ee_config_override_wooCommerce') && !empty(get_option('ee_config_from_email'))) {
544 $wooCommerce_email_original_email = get_option('woocommerce_email_from_address');
545 if (!get_option('ee_config_woocommerce_original_email')) {
546 add_option('ee_config_woocommerce_original_email', $wooCommerce_email_original_email);
547 }
548
549 update_option('woocommerce_email_from_address', $this->set_sender_email());
550 } else {
551 if (get_option('ee_config_woocommerce_original_email')) {
552 update_option('woocommerce_email_from_address', get_option('ee_config_woocommerce_original_email'));
553 delete_option('ee_config_woocommerce_original_email');
554 }
555 }
556 }
557
558 /** function that based on override option and setted FROM name input adds filter for wp_mail_from_name to override wooCommerce settings */
559 public function WooCommerce_name()
560 {
561 if (get_option('ee_config_override_wooCommerce') && !empty(get_option('ee_config_from_name'))) {
562 $wooCommerce_email_original_name = get_option('woocommerce_email_from_name');
563 if (!get_option('ee_config_woocommerce_original_name')) {
564 add_option('ee_config_woocommerce_original_name', $wooCommerce_email_original_name);
565 }
566 update_option('woocommerce_email_from_name', $this->set_sender_name());
567 } else {
568 if (get_option('ee_config_woocommerce_original_name')) {
569 update_option('woocommerce_email_from_name', get_option('ee_config_woocommerce_original_name'));
570 delete_option('ee_config_woocommerce_original_name');
571 }
572 }
573 }
574}