1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
<?php
if (! class_exists ( 'PostmanWpMailBinder' )) {
class PostmanWpMailBinder {
private $logger;
private $bound;
private $bindError;
/**
* private singleton constructor
*/
private function __construct() {
$this->logger = new PostmanLogger ( get_class ( $this ) );
// load the dependencies
require_once 'PostmanWpMail.php';
require_once 'PostmanOptions.php';
require_once 'PostmanPreRequisitesCheck.php';
// register the bind status hook
add_filter ( 'postman_wp_mail_bind_status', array (
$this,
'postman_wp_mail_bind_status'
) );
}
/**
* Return the Singleton instance
*
* @return PostmanWpMailBinder
*/
public static function getInstance() {
static $inst = null;
if ($inst === null) {
$inst = new PostmanWpMailBinder ();
}
return $inst;
}
/**
* Returns the bind result
*/
public function postman_wp_mail_bind_status() {
$result = array (
'bound' => $this->bound,
'bind_error' => $this->bindError
);
return $result;
}
/**
* Important: bind() may be called multiple times
*
* Replace wp_mail() after making sure:
* 1) the plugin has not already bound to wp_mail and
* 2) wp_mail is available for use
* 3) the plugin is properly configured.
* 4) the plugin's prerequisites are met.
*/
function bind() {
if (! $this->bound) {
$ready = true;
if (function_exists ( 'wp_mail' )) {
// If the function exists, it's probably because another plugin has
// replaced the pluggable function first, and we set an error flag.
// this is an error message because it is a Bind error
if ($this->logger->isTrace ()) {
$this->logger->trace ( 'wp_mail is already bound, Postman can not use it' );
}
$this->bindError = true;
$ready = false;
}
if (! PostmanPreRequisitesCheck::isReady ()) {
// this is a debug message because it is not up to the Binder to report transport errors
if ($this->logger->isTrace ()) {
$this->logger->trace ( 'Prerequisite check failed' );
}
$ready = false;
}
if ($ready) {
if ($this->logger->isTrace ()) {
$this->logger->trace ( 'Binding to wp_mail()' );
}
$this->replacePluggableFunctionWpMail ();
}
}
}
/**
* The code to replace the pluggable wp_mail()
*
* If the function does not exist, then the replacement was successful
* and we set a success flag.
*
* @return boolean
*/
private function replacePluggableFunctionWpMail() {
/**
* The Postman drop-in replacement for the WordPress wp_mail() function
*
* @param string|array $to
* Array or comma-separated list of email addresses to send message.
* @param string $subject
* Email subject
* @param string $message
* Message contents
* @param string|array $headers
* Optional. Additional headers.
* @param string|array $attachments
* Optional. Files to attach.
* @return bool Whether the email contents were sent successfully.
*/
function wp_mail($to, $subject, $message, $headers = '', $attachments = array()) {
// create an instance of PostmanWpMail to send the message
$postmanWpMail = new PostmanWpMail ();
// send the mail
$result = $postmanWpMail->send ( $to, $subject, $message, $headers, $attachments );
// return the result
return $result;
}
$this->logger->debug ( 'Bound to wp_mail()' );
$this->bound = true;
}
public function isBound() {
return $this->bound;
}
public function isUnboundDueToException() {
return $this->bindError;
}
}
}
|