| 1 |
<?php |
| 2 |
namespace Falcon; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die; |
| 5 |
|
| 6 |
use PHPMailer\PHPMailer\PHPMailer; |
| 7 |
|
| 8 |
class Email extends Base { |
| 9 |
public function __construct() { |
| 10 |
parent::__construct(); |
| 11 |
|
| 12 |
add_action( 'wp_ajax_falcon_test_smtp', [ $this, 'send_test_email' ] ); |
| 13 |
} |
| 14 |
|
| 15 |
protected $features = [ |
| 16 |
'no_admin_email_confirm', |
| 17 |
'no_update_emails', |
| 18 |
'no_new_user_emails', |
| 19 |
'no_password_reset_emails', |
| 20 |
'change_default_email', |
| 21 |
'smtp', |
| 22 |
]; |
| 23 |
|
| 24 |
public function no_admin_email_confirm() { |
| 25 |
add_filter( 'admin_email_check_interval', '__return_false' ); |
| 26 |
} |
| 27 |
|
| 28 |
public function no_update_emails() { |
| 29 |
add_filter( 'send_core_update_notification_email', '__return_false' ); |
| 30 |
add_filter( 'auto_plugin_update_send_email', '__return_false' ); |
| 31 |
add_filter( 'auto_theme_update_send_email', '__return_false' ); |
| 32 |
} |
| 33 |
|
| 34 |
public function no_new_user_emails() { |
| 35 |
add_filter( 'wp_send_new_user_notification_to_admin', '__return_false' ); |
| 36 |
} |
| 37 |
|
| 38 |
public function no_password_reset_emails() { |
| 39 |
remove_action( 'after_password_reset', 'wp_password_change_notification' ); |
| 40 |
add_filter( 'send_password_change_email', '__return_false' ); |
| 41 |
add_filter( 'woocommerce_disable_password_change_notification', '__return_false' ); |
| 42 |
} |
| 43 |
|
| 44 |
public function smtp(): void { |
| 45 |
add_action( 'phpmailer_init', [ $this, 'setup_smtp' ] ); |
| 46 |
} |
| 47 |
|
| 48 |
public function setup_smtp( PHPMailer $phpmailer ): void { |
| 49 |
$option = get_option( 'falcon', [] ); |
| 50 |
$smtp = $option['smtp'] ?? []; |
| 51 |
|
| 52 |
if ( empty( $smtp['host'] ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$phpmailer->isSMTP(); |
| 57 |
$phpmailer->Host = $smtp['host']; // phpcs:ignore |
| 58 |
|
| 59 |
if ( ! empty( $smtp['port'] ) ) { |
| 60 |
$phpmailer->Port = (int) $smtp['port']; // phpcs:ignore |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! empty( $smtp['username'] ) && ! empty( $smtp['password'] ) ) { |
| 64 |
$phpmailer->SMTPAuth = true; // phpcs:ignore |
| 65 |
$phpmailer->Username = $smtp['username']; // phpcs:ignore |
| 66 |
$phpmailer->Password = $smtp['password']; // phpcs:ignore |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! empty( $smtp['encryption'] ) ) { |
| 70 |
$phpmailer->SMTPSecure = $smtp['encryption'] === 'ssl' ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS; // phpcs:ignore |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function send_test_email(): void { |
| 75 |
check_ajax_referer( 'send-email' ); |
| 76 |
$result = wp_mail( wp_get_current_user()->user_email, __( 'Test email', 'falcon' ), __( 'This is a test email from Falcon.', 'falcon' ) ); |
| 77 |
if ( $result ) { |
| 78 |
wp_send_json_success( __( 'The email is sent successfully!', 'falcon' ) ); |
| 79 |
} |
| 80 |
wp_send_json_error( __( 'There is something wrong. Please check your configuration again.', 'falcon' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
public function change_default_email(): void { |
| 84 |
$option = get_option( 'falcon', [] ); |
| 85 |
$default_email = $option['default_email'] ?? []; |
| 86 |
$from_name = $default_email['from_name'] ?? get_bloginfo( 'name' ); |
| 87 |
$from_email = $default_email['from_email'] ?? get_option( 'admin_email' ); |
| 88 |
|
| 89 |
add_filter( 'wp_mail_from', function () use ( $from_email ) { |
| 90 |
return $from_email; |
| 91 |
} ); |
| 92 |
add_filter( 'wp_mail_from_name', function () use ( $from_name ) { |
| 93 |
return $from_name; |
| 94 |
} ); |
| 95 |
} |
| 96 |
} |
| 97 |
|