| 1 |
<?php |
| 2 |
|
| 3 |
namespace Pronamic\WordPress\PronamicClient; |
| 4 |
|
| 5 |
class PhpMailerModule { |
| 6 |
/** |
| 7 |
* Instance of this class. |
| 8 |
* |
| 9 |
* @since 1.4.0 |
| 10 |
* |
| 11 |
* @var Tracking |
| 12 |
*/ |
| 13 |
protected static $instance = null; |
| 14 |
|
| 15 |
/** |
| 16 |
* Plugin |
| 17 |
* |
| 18 |
* @var Plugin |
| 19 |
*/ |
| 20 |
private $plugin; |
| 21 |
|
| 22 |
/** |
| 23 |
* Sender. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $sender; |
| 28 |
|
| 29 |
/** |
| 30 |
* Construct PHPMailer module. |
| 31 |
* |
| 32 |
* @param Plugin $plugin |
| 33 |
*/ |
| 34 |
private function __construct( Plugin $plugin ) { |
| 35 |
$this->plugin = $plugin; |
| 36 |
|
| 37 |
// Init. |
| 38 |
\add_action( 'init', [ $this, 'init' ] ); |
| 39 |
|
| 40 |
// Admin. |
| 41 |
if ( \is_admin() ) { |
| 42 |
\add_action( 'admin_init', [ $this, 'admin_init' ], 20 ); |
| 43 |
} |
| 44 |
|
| 45 |
// Sender. |
| 46 |
$this->sender = \get_option( 'pronamic_client_phpmailer_sender' ); |
| 47 |
|
| 48 |
if ( ! empty( $this->sender ) ) { |
| 49 |
add_action( 'phpmailer_init', [ $this, 'phpmailer_sender' ] ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Initialize. |
| 55 |
*/ |
| 56 |
public function init() { |
| 57 |
register_setting( |
| 58 |
'pronamic_client', |
| 59 |
'pronamic_client_phpmailer_sender', |
| 60 |
[ |
| 61 |
'type' => 'string', |
| 62 |
'sanitize_callback' => 'sanitize_text_field', |
| 63 |
] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Admin initialize. |
| 69 |
*/ |
| 70 |
public function admin_init() { |
| 71 |
// Settings - General. |
| 72 |
add_settings_section( |
| 73 |
'pronamic_client_email', |
| 74 |
__( 'Email', 'pronamic-client' ), |
| 75 |
'__return_null', |
| 76 |
'pronamic_client' |
| 77 |
); |
| 78 |
|
| 79 |
add_settings_field( |
| 80 |
'pronamic_client_phpmailer_sender', |
| 81 |
__( 'PHPMailer Sender', 'pronamic-client' ), |
| 82 |
function ( $args ) { |
| 83 |
$args['type'] = 'email'; |
| 84 |
|
| 85 |
Admin::input_text( $args ); |
| 86 |
}, |
| 87 |
'pronamic_client', |
| 88 |
'pronamic_client_email', |
| 89 |
[ |
| 90 |
'label_for' => 'pronamic_client_phpmailer_sender', |
| 91 |
'classes' => 'regular-text', |
| 92 |
'description' => sprintf( |
| 93 |
/* translators: %s: <code>spf=neutral...</code> */ |
| 94 |
__( 'Optionally set a PHPMailer Sender e-mail address to, for example, resolve SPF neutral notifications such as: %s.', 'pronamic-client' ), |
| 95 |
sprintf( |
| 96 |
'<br /><code>%s</code>', |
| 97 |
'spf=neutral (●●●●●●●●.●●●: ●.●.●.● is neither permitted nor denied by best guess record for domain of ●●●●●●●●@●●●●●●●●.●●●) smtp.mailfrom=●●●●●●●●@●●●●●●●●.●●●' |
| 98 |
) |
| 99 |
), |
| 100 |
] |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Set PHPMailer sender. |
| 106 |
* |
| 107 |
* @since 1.4.0 |
| 108 |
* |
| 109 |
* @param PHPMailer $phpmailer PHPMailer object. |
| 110 |
* |
| 111 |
* @return PHPMailer |
| 112 |
*/ |
| 113 |
public function phpmailer_sender( $phpmailer ) { |
| 114 |
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 115 |
if ( ! empty( $phpmailer->Sender ) ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if ( empty( $this->sender ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! filter_var( $this->sender, FILTER_VALIDATE_EMAIL ) ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 128 |
$phpmailer->Sender = $this->sender; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Return an instance of this class. |
| 133 |
* |
| 134 |
* @since 1.1.0 |
| 135 |
* |
| 136 |
* @return object A single instance of this class. |
| 137 |
*/ |
| 138 |
public static function get_instance( $plugin = false ) { |
| 139 |
// If the single instance hasn't been set, set it now. |
| 140 |
if ( null === self::$instance ) { |
| 141 |
self::$instance = new self( $plugin ); |
| 142 |
} |
| 143 |
|
| 144 |
return self::$instance; |
| 145 |
} |
| 146 |
} |
| 147 |
|