PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / framework / settings / email.php

email.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at includes/framework/settings/email.php

546 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use WeDevs\ERP\Framework\ERP_Settings_Page;
4
5 /**
6 * Email settings class
7 */
8 class ERP_Email_Settings extends ERP_Settings_Page {
9
10 function __construct() {
11 $this->id = 'erp-email';
12 $this->label = __( 'Emails', 'erp' );
13 $this->sections = $this->get_sections();
14
15 add_action( 'erp_admin_field_notification_emails', [ $this, 'notification_emails' ] );
16 add_action( 'erp_admin_field_smtp_test_connection', [ $this, 'smtp_test_connection' ] );
17 add_action( 'erp_admin_field_imap_test_connection', [ $this, 'imap_test_connection' ] );
18 add_action( 'erp_admin_field_imap_status', [ $this, 'imap_status' ] );
19
20 add_action( 'erp_update_option', [ $this, 'cron_schedule' ] );
21 add_action( 'admin_footer', 'erp_email_settings_javascript' );
22 }
23
24 /**
25 * Get registered tabs
26 *
27 * @return array
28 */
29 public function get_sections() {
30 $sections = [
31 'general' => __( 'General', 'erp' ),
32 'smtp' => __( 'SMTP', 'erp' ),
33 'imap' => __( 'IMAP/POP3', 'erp' ),
34 ];
35
36 return apply_filters( 'erp_settings_email_sections', $sections );
37 }
38
39 /**
40 * Get sections fields
41 *
42 * @return array
43 */
44 public function get_section_fields( $section = '' ) {
45
46 $fields['general'][] = [
47 'title' => __( 'Email Sender Options', 'erp' ),
48 'type' => 'title',
49 'desc' => __( 'Email notification settings for ERP. Customize the look and feel of outgoing emails.', 'erp' )
50 ];
51
52 $fields['general'][] = [
53 'title' => __( '"From" Name', 'erp' ),
54 'id' => 'from_name',
55 'type' => 'text',
56 'default' => get_bloginfo( 'name' ),
57 'tooltip' => true,
58 'desc' => __( 'The senders name appears on the outgoing emails', 'erp' )
59 ];
60
61 $fields['general'][] = [
62 'title' => __( '"From" Address', 'erp' ),
63 'id' => 'from_email',
64 'type' => 'text',
65 'default' => get_option( 'admin_email' ),
66 'tooltip' => true,
67 'desc' => __( 'The senders email appears on the outgoing emails', 'erp' )
68 ];
69
70 $fields['general'][] = [
71 'title' => __( 'Header Image', 'erp' ),
72 'id' => 'header_image',
73 'type' => 'text',
74 'desc' => __( 'Upload a logo/banner and provide the URL here.', 'erp' ),
75 'tooltip' => true,
76 'custom_attributes' => [
77 'placeholder' => 'http://example.com/path/to/logo.png'
78 ]
79 ];
80
81 $fields['general'][] = [
82 'title' => __( 'Footer Text', 'erp' ),
83 'id' => 'footer_text',
84 'type' => 'textarea',
85 'css' => 'min-width:300px;',
86 'tooltip' => true,
87 'default' => sprintf( '%s - Powered by WP ERP', get_bloginfo( 'name' ) ),
88 'desc' => __( 'The text apears on each emails footer area.', 'erp' )
89 ];
90
91 $fields['general'][] = [
92 'type' => 'sectionend', 'id' => 'script_styling_options'
93 ];
94
95 $fields['general'][] = [
96 'title' => __( 'Notification Emails', 'erp' ),
97 'desc' => __( 'Email notifications sent from WP ERP are listed below. Click on an email to configure it.', 'erp' ),
98 'type' => 'title',
99 'id' => 'email_notification_settings'
100 ];
101
102 $fields['general'][] = [
103 'type' => 'notification_emails'
104 ];
105
106 $fields['general'][] = [
107 'type' => 'sectionend',
108 'id' => 'script_styling_options'
109 ];
110 // End general settings
111
112 $fields['smtp'][] = [
113 'title' => __( 'SMTP Options', 'erp' ),
114 'type' => 'title',
115 'desc' => __( 'Email outgoing settings for ERP.', 'erp' )
116 ];
117
118 $fields['smtp'][] = [
119 'title' => __( 'Enable SMTP', 'erp' ),
120 'id' => 'enable_smtp',
121 'type' => 'radio',
122 'options' => [ 'yes' => 'Yes', 'no' => 'No' ],
123 'default' => 'no'
124 ];
125
126 $fields['smtp'][] = [
127 'title' => __( 'Mail Server', 'erp' ),
128 'id' => 'mail_server',
129 'type' => 'text',
130 'custom_attributes' => [
131 'placeholder' => 'smtp.gmail.com'
132 ],
133 'desc' => __( 'SMTP host address.', 'erp' ),
134 ];
135
136 $fields['smtp'][] = [
137 'title' => __( 'Port', 'erp' ),
138 'id' => 'port',
139 'type' => 'text',
140 'desc' => __( 'SSL: 465<br> TLS: 587', 'erp' ),
141 ];
142
143 $fields['smtp'][] = [
144 'title' => __( 'Authentication', 'erp' ),
145 'id' => 'authentication',
146 'type' => 'select',
147 'desc' => __( 'Authentication type.', 'erp' ),
148 'options' => [ '' => __( 'None', 'erp'), 'ssl' => __( 'SSL', 'erp' ), 'tls' => __( 'TLS', 'erp') ],
149 ];
150
151 $fields['smtp'][] = [
152 'title' => __( 'Username', 'erp' ),
153 'id' => 'username',
154 'type' => 'text',
155 'custom_attributes' => [
156 'placeholder' => 'email@example.com'
157 ],
158 'desc' => __( 'Your email id.', 'erp' ),
159 ];
160
161 $fields['smtp'][] = [
162 'title' => __( 'Password', 'erp' ),
163 'id' => 'password',
164 'type' => 'password',
165 'desc' => __( 'Your email password.', 'erp' )
166 ];
167
168 $fields['smtp'][] = [
169 'title' => __( 'Debug', 'erp' ),
170 'id' => 'debug',
171 'type' => 'radio',
172 'options' => [ 'yes' => 'Yes', 'no' => 'No' ],
173 'default' => 'no'
174 ];
175
176 $fields['smtp'][] = [
177 'type' => 'smtp_test_connection',
178 ];
179
180 $fields['smtp'][] = [
181 'type' => 'sectionend',
182 'id' => 'script_styling_options'
183 ];
184 // End SMTP settings
185
186 $fields['imap'] = $this->get_imap_settings_fields();
187 // End IMAP settings
188
189 $fields = apply_filters( 'erp_settings_email_section_fields', $fields, $section );
190
191 $section = $section === false ? $fields['general'] : $fields[$section];
192
193 return $section;
194 }
195
196 function notification_emails() {
197 $email_templates = wperp()->emailer->get_emails();
198 ?>
199 <tr valign="top">
200 <td class="erp-settings-table-wrapper" colspan="2">
201 <table class="erp-settings-table widefat" cellspacing="0">
202 <thead>
203 <tr>
204 <?php
205 $columns = apply_filters( 'erp_email_setting_columns', array(
206 'name' => __( 'Email', 'erp' ),
207 'description' => __( 'Description', 'erp' ),
208 'actions' => ''
209 ) );
210
211 foreach ( $columns as $key => $column ) {
212 echo '<th class="erp-settings-table-' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
213 }
214 ?>
215 </tr>
216 </thead>
217 <tbody>
218 <?php
219 foreach ( $email_templates as $email_key => $email ) {
220 echo '<tr>';
221
222 foreach ( $columns as $key => $column ) {
223 switch ( $key ) {
224 case 'name' :
225 echo '<td class="erp-settings-table-' . esc_attr( $key ) . '">
226 <a href="' . admin_url( 'admin.php?page=erp-settings&tab=erp-email&section=general&sub_section=' . strtolower( $email_key ) ) . '">' . $email->get_title() . '</a>
227 </td>';
228 break;
229
230 case 'status':
231 case 'module':
232 case 'recipient':
233 echo '<td class="erp-settings-table-' . esc_attr( $key ) . '">
234
235 </td>';
236 break;
237
238 case 'description':
239 echo '<td class="erp-settings-table-' . esc_attr( $key ) . '">
240 <span class="help">' . $email->get_description() . '</span>
241 </td>';
242 break;
243
244 case 'actions' :
245 echo '<td class="erp-settings-table-' . esc_attr( $key ) . '">
246 <a class="button alignright" href="' . admin_url( 'admin.php?page=erp-settings&tab=erp-email&section=general&sub_section=' . strtolower( $email_key ) ) . '">' . __( 'Configure', 'erp' ) . '</a>
247 </td>';
248 break;
249
250 default :
251 do_action( 'erp_email_setting_column_' . $key, $email );
252 break;
253 }
254 }
255 }
256 ?>
257 </tbody>
258 </table>
259 </td>
260 </tr>
261 <?php
262 }
263
264 /**
265 * Display imap test connection button.
266 *
267 * @return void
268 */
269 public function smtp_test_connection() {
270 ?>
271 <tr valign="top">
272 <th scope="row" class="titledesc">
273 &nbsp;
274 </th>
275 <td class="forminp forminp-text">
276 <input type="email" id="smtp_test_email_address" class="regular-text" value="<?php echo get_option( 'admin_email' ); ?>" /><br>
277 <p class="description"><?php _e( 'An email address to test the connection.', 'erp' ); ?></p>
278 <a id="smtp-test-connection" class="button-secondary"><?php esc_attr_e( 'Send Test Email', 'erp' ); ?></a>
279 <span class="erp-loader" style="display: none;"></span>
280 <p class="description"><?php _e( 'Click on the above button before saving the settings.', 'erp' ); ?></p>
281 </td>
282 </tr>
283 <?php
284 }
285
286 /**
287 * Display imap test connection button.
288 *
289 * @return void
290 */
291 public function imap_test_connection() {
292 ?>
293 <tr valign="top">
294 <th scope="row" class="titledesc">
295 &nbsp;
296 </th>
297 <td class="forminp forminp-text">
298 <a id="imap-test-connection" class="button-secondary"><?php esc_attr_e( 'Test Connection', 'erp' ); ?></a>
299 <span class="erp-loader" style="display: none;"></span>
300 <p class="description"><?php _e( 'Click on the above button before saving the settings.', 'erp' ); ?></p>
301 </td>
302 </tr>
303 <?php
304 }
305
306 /**
307 * Imap connection status.
308 *
309 * @return void
310 */
311 public function imap_status() {
312 $options = get_option( 'erp_settings_erp-email_imap', [] );
313 $imap_status = (boolean) isset( $options['imap_status'] ) ? $options['imap_status'] : 0;
314 ?>
315 <tr valign="top">
316 <th scope="row" class="titledesc">
317 <?php _e( 'Status', 'erp' ); ?>
318 </th>
319 <td class="forminp forminp-text">
320 <span class="dashicons dashicons-<?php echo ( $imap_status ) ? 'yes green' : 'no red' ?>"></span><?php echo ( $imap_status ) ? __( 'Connected', 'erp' ) : __( 'Not Connected', 'erp' ); ?>
321 </td>
322 </tr>
323 <?php
324 }
325
326 /**
327 * Get IMAP Settings Fields.
328 *
329 * @return array
330 */
331 protected function get_imap_settings_fields() {
332 if ( ! extension_loaded( 'imap' ) || ! function_exists( 'imap_open' ) ) {
333 $fields[] = [
334 'title' => __( 'IMAP/POP3 Options', 'erp' ),
335 'type' => 'title',
336 'desc' => sprintf(
337 '%s' . __( 'Your server does not have PHP IMAP extension loaded. To enable this feature, please contact your hosting provider and ask to enable PHP IMAP extension.', 'erp' ) . '%s',
338 '<section class="notice notice-warning"><p>',
339 '</p></section>'
340 )
341 ];
342
343 return $fields;
344 }
345
346 $fields[] = [
347 'title' => __( 'IMAP/POP3 Options', 'erp' ),
348 'type' => 'title',
349 'desc' => __( 'Email incoming settings for ERP.', 'erp' )
350 ];
351
352 $fields[] = [
353 'type' => 'imap_status',
354 ];
355
356 $fields[] = [
357 'title' => __( 'Enable IMAP', 'erp' ),
358 'id' => 'enable_imap',
359 'type' => 'radio',
360 'options' => [ 'yes' => 'Yes', 'no' => 'No' ],
361 'default' => 'no'
362 ];
363
364 $schedules = wp_get_schedules();
365
366 $cron_schedules = [];
367 foreach( $schedules as $key => $value ) {
368 $cron_schedules[$key] = $value['display'];
369 }
370
371 $fields[] = [
372 'title' => __( 'Cron Schedule', 'erp' ),
373 'id' => 'schedule',
374 'type' => 'select',
375 'desc' => __( 'Interval time to run cron.', 'erp' ),
376 'options' => $cron_schedules,
377 'default' => 'hourly',
378 ];
379
380 $fields[] = [
381 'title' => __( 'Mail Server', 'erp' ),
382 'id' => 'mail_server',
383 'type' => 'text',
384 'custom_attributes' => [
385 'placeholder' => 'imap.gmail.com'
386 ],
387 'desc' => __( 'IMAP/POP3 host address.', 'erp' ),
388 ];
389
390 $fields[] = [
391 'title' => __( 'Username', 'erp' ),
392 'id' => 'username',
393 'type' => 'text',
394 'desc' => __( 'Your email id.', 'erp' ),
395 'custom_attributes' => [
396 'placeholder' => 'email@example.com'
397 ]
398 ];
399
400 $fields[] = [
401 'title' => __( 'Password', 'erp' ),
402 'id' => 'password',
403 'type' => 'password',
404 'desc' => __( 'Your email password.', 'erp' )
405 ];
406
407 $fields[] = [
408 'title' => __( 'Protocol', 'erp' ),
409 'id' => 'protocol',
410 'type' => 'select',
411 'desc' => __( 'Protocol type.', 'erp' ),
412 'options' => [ 'imap' => __( 'IMAP', 'erp' ), 'pop3' => __( 'POP3', 'erp') ],
413 'default' => 'imap',
414 ];
415
416 $fields[] = [
417 'title' => __( 'Port', 'erp' ),
418 'id' => 'port',
419 'type' => 'text',
420 'desc' => __( 'IMAP: 993<br> POP3: 995', 'erp' ),
421 ];
422
423 $fields[] = [
424 'title' => __( 'Authentication', 'erp' ),
425 'id' => 'authentication',
426 'type' => 'select',
427 'options' => [ 'ssl' => __( 'SSL', 'erp' ), 'tls' => __( 'TLS', 'erp'), 'notls' => __( 'None', 'erp') ],
428 'default' => 'ssl',
429 'desc' => __( 'Authentication type.', 'erp' ),
430 ];
431
432 $fields[] = [
433 'type' => 'imap_test_connection',
434 ];
435
436 $fields[] = [
437 'id' => 'imap_status',
438 'type' => 'hidden',
439 'default' => 0,
440 ];
441
442 $fields[] = [
443 'type' => 'sectionend',
444 'id' => 'script_styling_options'
445 ];
446
447 return $fields;
448 }
449
450 /**
451 * Set cron schedule event to check new inbound emails
452 *
453 * @return void
454 */
455 public function cron_schedule( $value ) {
456 if ( ! isset( $_GET['section'] ) || ( $_GET['section'] != 'imap' ) ) {
457 return;
458 }
459
460 if ( ! isset( $value['id'] ) || ( $value['id'] != 'schedule' ) ) {
461 return;
462 }
463
464 $recurrence = isset( $_POST['schedule'] ) ? $_POST['schedule'] : 'hourly';
465 wp_clear_scheduled_hook( 'erp_crm_inbound_email_scheduled_events' );
466 wp_schedule_event( time(), $recurrence, 'erp_crm_inbound_email_scheduled_events' );
467 }
468
469 /**
470 * Output the settings.
471 */
472 public function output( $section = false ) {
473 if ( ! isset( $_GET['sub_section'] ) ) {
474 parent::output( $section );
475
476 return;
477 }
478
479 $current_section = isset( $_GET['sub_section'] ) ? sanitize_key( $_GET['sub_section'] ) : false;
480
481 // Define emails that can be customised here
482 $email_templates = wperp()->emailer->get_emails();
483
484 if ( $current_section ) {
485 foreach ( $email_templates as $email_key => $email ) {
486 if ( strtolower( $email_key ) == $current_section ) {
487 $email->admin_options();
488 break;
489 }
490 }
491 } else {
492 parent::output();
493 }
494 }
495
496 function save( $section = false ) {
497 if ( isset( $_POST['_wpnonce']) && wp_verify_nonce( $_POST['_wpnonce'], 'erp-settings-nonce' ) ) {
498
499 if ( ! isset( $_GET['sub_section'] ) ) {
500 parent::save( $section );
501
502 return;
503 }
504
505 $current_section = isset( $_GET['sub_section'] ) ? sanitize_key( $_GET['sub_section'] ) : false;
506
507
508 // saving individual email settings
509 if ( $current_section ) {
510 $email_templates = wperp()->emailer->get_emails();
511
512 foreach ( $email_templates as $email_key => $email ) {
513 if ( strtolower( $email_key ) == $current_section ) {
514
515 $settings = $email->get_form_fields();
516 $update_options = array();
517
518 if ( $settings) {
519 foreach ($settings as $field) {
520 if ( ! isset( $field['id'] ) || ! isset( $_POST[ $field['id'] ] ) ) {
521 continue;
522 }
523
524 $option_value = $this->parse_option_value( $field );
525
526 if ( ! is_null( $option_value ) ) {
527 $update_options[ $field['id'] ] = $option_value;
528 }
529 }
530 }
531
532 update_option( $email->get_option_id(), $update_options );
533
534 break;
535 }
536 }
537
538 } else {
539 parent::save();
540 }
541 }
542 }
543 }
544
545 return new ERP_Email_Settings();
546