|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains the notification email class. |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represents a single email type. |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
class GetPaid_Notification_Email { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Contains the type of this notification email. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
public $id; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Contains any object to use in filters. |
|
24
|
|
|
* |
|
25
|
|
|
* @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription |
|
26
|
|
|
*/ |
|
27
|
|
|
public $object; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $id Email Type. |
|
33
|
|
|
* @param mixed $object Optional. Associated object. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( $id, $object = false ) { |
|
36
|
|
|
$this->id = $id; |
|
37
|
|
|
$this->object = $object; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Retrieves an option |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get_option( $key ) { |
|
46
|
|
|
|
|
47
|
|
|
$key = "email_{$this->id}_$key"; |
|
48
|
|
|
$value = wpinv_get_option( $key, null ); |
|
49
|
|
|
|
|
50
|
|
|
if ( is_null( $value ) ) { |
|
|
|
|
|
|
51
|
|
|
$options = wpinv_get_emails(); |
|
52
|
|
|
|
|
53
|
|
|
if ( ! isset( $options[ $this->id ] ) || ! isset( $options[ $this->id ][ $key ] ) ) { |
|
54
|
|
|
return ''; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$value = isset( $options[ $this->id ][ $key ]['std'] ) ? $options[ $this->id ][ $key ]['std'] : ''; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $value; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Retrieves the email body. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function get_body() { |
|
69
|
|
|
$body = $this->get_option( 'body' ); |
|
70
|
|
|
return apply_filters( 'getpaid_get_email_body', $body, $this->id, $this->object ); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Retrieves the email subject. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function get_subject() { |
|
79
|
|
|
$subject = $this->get_option( 'subject' ); |
|
80
|
|
|
return apply_filters( 'getpaid_get_email_subject', $subject, $this->id, $this->object ); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Retrieves the email heading. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function get_heading() { |
|
89
|
|
|
$heading = $this->get_option( 'heading' ); |
|
90
|
|
|
return apply_filters( 'getpaid_get_email_heading', $heading, $this->id, $this->object ); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Checks if an email is active. |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function is_active() { |
|
99
|
|
|
$is_active = ! empty( $this->get_option( 'is_active' ) ); |
|
100
|
|
|
return apply_filters( 'getpaid_email_type_is_active', $is_active, $this->id, $this->object ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Checks if the site's admin should receive email notifications. |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
public function include_admin_bcc() { |
|
109
|
|
|
$include_admin_bcc = ! empty( $this->get_option( 'admin_bcc' ) ); |
|
110
|
|
|
return apply_filters( 'getpaid_email_type_include_admin_bcc', $include_admin_bcc, $this->id, $this->object ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Checks whether this email should be sent to the customer or admin. |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function is_admin_email() { |
|
119
|
|
|
$is_admin_email = in_array( $this->id, array( 'new_invoice', 'cancelled_invoice', 'failed_invoice' ) ); |
|
120
|
|
|
return apply_filters( 'getpaid_email_type_is_admin_email', $is_admin_email, $this->id, $this->object ); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns email attachments. |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public function get_attachments() { |
|
129
|
|
|
return apply_filters( 'getpaid_get_email_attachments', array(), $this->id, $this->object ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns an array of merge tags. |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public function get_merge_tags() { |
|
138
|
|
|
|
|
139
|
|
|
$merge_tags = array( |
|
140
|
|
|
'{site_title}' => wpinv_get_blogname(), |
|
141
|
|
|
'{date}' => date_i18n( get_option( 'date_format' ), current_time( 'timestamp' ) ), |
|
|
|
|
|
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
return apply_filters( 'getpaid_get_email_merge_tags', $merge_tags, $this->id, $this->object ); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Adds merge tags to a text. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string string $text |
|
151
|
|
|
* @param array $merge_tags |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function add_merge_tags( $text, $merge_tags = array() ) { |
|
155
|
|
|
|
|
156
|
|
|
foreach ( $merge_tags as $key => $value ) { |
|
157
|
|
|
$text = str_replace( $key, $value, $text ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return wptexturize( $text ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Returns the email content |
|
165
|
|
|
* |
|
166
|
|
|
* @param array $merge_tags |
|
167
|
|
|
* @param array $extra_args Extra template args |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function get_content( $merge_tags = array(), $extra_args = array() ) { |
|
171
|
|
|
|
|
172
|
|
|
$content = wpinv_get_template_html( |
|
173
|
|
|
"emails/wpinv-email-{$this->id}.php", |
|
174
|
|
|
array_merge( |
|
175
|
|
|
$extra_args, |
|
176
|
|
|
array( |
|
177
|
|
|
'invoice' => $this->object, // Backwards compat. |
|
178
|
|
|
'object' => $this->object, |
|
179
|
|
|
'email_type' => $this->id, |
|
180
|
|
|
'email_heading' => $this->add_merge_tags( $this->get_heading(), $merge_tags ), |
|
181
|
|
|
'sent_to_admin' => $this->is_admin_email(), |
|
182
|
|
|
'plain_text' => false, |
|
183
|
|
|
'message_body' => wpautop( $this->add_merge_tags( $this->get_body(), $merge_tags ) ), |
|
184
|
|
|
) |
|
185
|
|
|
) |
|
186
|
|
|
); |
|
187
|
|
|
|
|
188
|
|
|
return wpinv_email_style_body( $content ); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|