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