|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Email Notification |
|
4
|
|
|
* |
|
5
|
|
|
* This class handles all email notification settings. |
|
6
|
|
|
* |
|
7
|
|
|
* @package Give |
|
8
|
|
|
* @subpackage Classes/Emails |
|
9
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
10
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
11
|
|
|
* @since 2.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Give_Email_Notifications |
|
16
|
|
|
*/ |
|
17
|
|
|
class Give_Email_Notifications { |
|
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 2.0 |
|
22
|
|
|
* @access static |
|
23
|
|
|
* @var |
|
24
|
|
|
*/ |
|
25
|
|
|
static private $instance; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Array of email notifications. |
|
29
|
|
|
* |
|
30
|
|
|
* @since 2.0 |
|
31
|
|
|
* @access private |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $emails = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Singleton pattern. |
|
38
|
|
|
* |
|
39
|
|
|
* @since 2.0 |
|
40
|
|
|
* @access private |
|
41
|
|
|
* Give_Payumoney_API constructor. |
|
42
|
|
|
*/ |
|
43
|
|
|
private function __construct() { |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @since 2.0 |
|
51
|
|
|
* @access static |
|
52
|
|
|
* @return static |
|
53
|
|
|
*/ |
|
54
|
|
|
static function get_instance() { |
|
|
|
|
|
|
55
|
|
|
if ( null === static::$instance ) { |
|
|
|
|
|
|
56
|
|
|
self::$instance = new static(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return self::$instance; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Setup dependencies |
|
64
|
|
|
* |
|
65
|
|
|
* @since 2.0 |
|
66
|
|
|
*/ |
|
67
|
|
|
public function init() { |
|
68
|
|
|
// Load files. |
|
69
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/ajax-handler.php'; |
|
70
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-setting-field.php'; |
|
71
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/filters.php'; |
|
72
|
|
|
|
|
73
|
|
|
// Load email notifications. |
|
74
|
|
|
$this->add_emails_notifications(); |
|
75
|
|
|
|
|
76
|
|
|
add_filter( 'give_metabox_form_data_settings', array( $this, 'add_metabox_setting_fields' ), 10, 2 ); |
|
77
|
|
|
add_action( 'init', array( $this, 'preview_email' ) ); |
|
78
|
|
|
add_action( 'init', array( $this, 'send_preview_email' ) ); |
|
79
|
|
|
|
|
80
|
|
|
/* @var Give_Email_Notification $email */ |
|
81
|
|
|
foreach ( $this->get_email_notifications() as $email ) { |
|
82
|
|
|
// Add section. |
|
83
|
|
|
add_filter( 'give_get_sections_emails', array( $email, 'add_section' ) ); |
|
84
|
|
|
add_filter( "give_hide_section_{$email->config['id']}_on_emails_page", array( $email, 'hide_section' ) ); |
|
85
|
|
|
|
|
86
|
|
|
if ( ! Give_Email_Notification_Util::is_email_preview_has_header( $email ) ) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
add_action( "give_{$email->config['id']}_email_preview", array( $this, 'email_preview_header' ) ); |
|
91
|
|
|
add_filter( "give_{$email->config['id']}_email_preview_data", array( $this, 'email_preview_data' ) ); |
|
92
|
|
|
add_filter( "give_{$email->config['id']}_email_preview_message", array( $this, 'email_preview_message' ), 1, 2 ); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add setting to metabox. |
|
99
|
|
|
* |
|
100
|
|
|
* @since 2.0 |
|
101
|
|
|
* @access public |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $settings |
|
104
|
|
|
* @param int $post_id |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function add_metabox_setting_fields( $settings, $post_id ) { |
|
109
|
|
|
$emails = $this->get_email_notifications(); |
|
110
|
|
|
|
|
111
|
|
|
// Bailout. |
|
112
|
|
|
if ( empty( $emails ) ) { |
|
113
|
|
|
return $settings; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Email notification setting. |
|
117
|
|
|
$settings['email_notification_options'] = array( |
|
118
|
|
|
'id' => 'email_notification_options', |
|
119
|
|
|
'title' => __( 'Email Notification', 'give' ), |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Filter the email notification settings. |
|
123
|
|
|
* |
|
124
|
|
|
* @since 2.0 |
|
125
|
|
|
*/ |
|
126
|
|
|
'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ), |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
return $settings; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Add email notifications |
|
134
|
|
|
* |
|
135
|
|
|
* @since 2.0 |
|
136
|
|
|
* @access private |
|
137
|
|
|
*/ |
|
138
|
|
|
private function add_emails_notifications() { |
|
139
|
|
|
$this->emails = array( |
|
140
|
|
|
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php', |
|
141
|
|
|
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php', |
|
142
|
|
|
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php', |
|
143
|
|
|
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php', |
|
144
|
|
|
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php', |
|
145
|
|
|
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php', |
|
146
|
|
|
include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php', |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Filter the email notifications. |
|
151
|
|
|
* |
|
152
|
|
|
* @since 2.0 |
|
153
|
|
|
*/ |
|
154
|
|
|
$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this ); |
|
155
|
|
|
|
|
156
|
|
|
// Bailout. |
|
157
|
|
|
if ( empty( $this->emails ) ) { |
|
158
|
|
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// Initiate email notifications. |
|
162
|
|
|
foreach ( $this->emails as $email ) { |
|
163
|
|
|
$email->init(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Get list of email notifications. |
|
170
|
|
|
* |
|
171
|
|
|
* @since 2.0 |
|
172
|
|
|
* @access public |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
public function get_email_notifications() { |
|
176
|
|
|
return $this->emails; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Displays the email preview |
|
182
|
|
|
* |
|
183
|
|
|
* @since 2.0 |
|
184
|
|
|
* @access public |
|
185
|
|
|
* @return bool|null |
|
186
|
|
|
*/ |
|
187
|
|
|
public function preview_email() { |
|
188
|
|
|
// Bailout. |
|
189
|
|
|
if ( ! Give_Email_Notification_Util::can_preview_email() ) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// Security check. |
|
194
|
|
|
give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' ); |
|
195
|
|
|
|
|
196
|
|
|
// Get email type. |
|
197
|
|
|
$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : ''; |
|
198
|
|
|
|
|
199
|
|
|
/* @var Give_Email_Notification $email */ |
|
200
|
|
|
foreach ( $this->get_email_notifications() as $email ) { |
|
201
|
|
|
if ( $email_type !== $email->config['id'] ) { |
|
202
|
|
|
continue; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
// Set form id. |
|
206
|
|
|
$form_id = empty( $_GET['form_id'] ) ? null : absint( $_GET['form_id'] ); |
|
207
|
|
|
|
|
208
|
|
|
// Call setup email data to apply filter and other thing to email. |
|
209
|
|
|
$email->setup_email_data(); |
|
210
|
|
|
|
|
211
|
|
|
// Decode message. |
|
212
|
|
|
$email_message = $email->preview_email_template_tags( $email->get_email_message( $form_id ) ); |
|
213
|
|
|
|
|
214
|
|
|
// Set email template. |
|
215
|
|
|
Give()->emails->html = true; |
|
|
|
|
|
|
216
|
|
|
Give()->emails->__set( 'template', $email->get_email_template( $form_id ) ); |
|
217
|
|
|
|
|
218
|
|
|
if ( 'text/plain' === $email->config['content_type'] ) { |
|
219
|
|
|
// Give()->emails->__set( 'html', false ); |
|
|
|
|
|
|
220
|
|
|
Give()->emails->__set( 'template', 'none' ); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
if ( $email_message = Give()->emails->build_email( $email_message ) ) { |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Filter the email preview data |
|
227
|
|
|
* |
|
228
|
|
|
* @since 2.0 |
|
229
|
|
|
* |
|
230
|
|
|
* @param array |
|
231
|
|
|
*/ |
|
232
|
|
|
$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() ); |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Fire the give_{$email_type}_email_preview action |
|
236
|
|
|
* |
|
237
|
|
|
* @since 2.0 |
|
238
|
|
|
*/ |
|
239
|
|
|
do_action( "give_{$email_type}_email_preview", $email ); |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Filter the email message |
|
243
|
|
|
* |
|
244
|
|
|
* @since 2.0 |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $email_message |
|
247
|
|
|
* @param array $email_preview_data |
|
248
|
|
|
* @param Give_Email_Notification $email |
|
249
|
|
|
*/ |
|
250
|
|
|
echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email ); |
|
251
|
|
|
|
|
252
|
|
|
exit(); |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
}// End foreach(). |
|
|
|
|
|
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Add header to donation receipt email preview |
|
260
|
|
|
* |
|
261
|
|
|
* @since 2.0 |
|
262
|
|
|
* @access public |
|
263
|
|
|
* |
|
264
|
|
|
* @param Give_Email_Notification $email |
|
265
|
|
|
*/ |
|
266
|
|
|
public function email_preview_header( $email ) { |
|
267
|
|
|
/** |
|
268
|
|
|
* Filter the all email preview headers. |
|
269
|
|
|
* |
|
270
|
|
|
* @since 2.0 |
|
271
|
|
|
* |
|
272
|
|
|
* @param Give_Email_Notification $email |
|
273
|
|
|
*/ |
|
274
|
|
|
$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email ); |
|
275
|
|
|
|
|
276
|
|
|
echo $email_preview_header; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Add email preview data |
|
281
|
|
|
* |
|
282
|
|
|
* @since 2.0 |
|
283
|
|
|
* @access public |
|
284
|
|
|
* |
|
285
|
|
|
* @param array $email_preview_data |
|
286
|
|
|
* |
|
287
|
|
|
* @return array |
|
288
|
|
|
*/ |
|
289
|
|
|
public function email_preview_data( $email_preview_data ) { |
|
290
|
|
|
$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ) ); |
|
291
|
|
|
$email_preview_data['user_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' ) ); |
|
292
|
|
|
|
|
293
|
|
|
return $email_preview_data; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Replace email template tags. |
|
298
|
|
|
* |
|
299
|
|
|
* @since 2.0 |
|
300
|
|
|
* @access public |
|
301
|
|
|
* |
|
302
|
|
|
* @param string $email_message |
|
303
|
|
|
* @param array $email_preview_data |
|
304
|
|
|
* |
|
305
|
|
|
* @return string |
|
306
|
|
|
*/ |
|
307
|
|
|
public function email_preview_message( $email_message, $email_preview_data ) { |
|
308
|
|
|
if ( |
|
309
|
|
|
! empty( $email_preview_data['payment_id'] ) |
|
310
|
|
|
|| ! empty( $email_preview_data['user_id'] ) |
|
311
|
|
|
) { |
|
312
|
|
|
$email_message = give_do_email_tags( $email_message, $email_preview_data ); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
return $email_message; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Displays the email preview |
|
320
|
|
|
* |
|
321
|
|
|
* @since 2.0 |
|
322
|
|
|
* @access public |
|
323
|
|
|
* @return bool|null |
|
324
|
|
|
*/ |
|
325
|
|
|
public function send_preview_email() { |
|
326
|
|
|
// Bailout. |
|
327
|
|
|
if ( ! Give_Email_Notification_Util::can_send_preview_email() ) { |
|
328
|
|
|
return false; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
// Security check. |
|
332
|
|
|
give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' ); |
|
333
|
|
|
|
|
334
|
|
|
// Get email type. |
|
335
|
|
|
$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' ); |
|
336
|
|
|
|
|
337
|
|
|
/* @var Give_Email_Notification $email */ |
|
338
|
|
|
foreach ( $this->get_email_notifications() as $email ) { |
|
339
|
|
|
if ( $email_type === $email->config['id'] && Give_Email_Notification_Util::is_email_preview( $email ) ) { |
|
340
|
|
|
$email->send_preview_email(); |
|
341
|
|
|
break; |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Load Give_Email_Notifications |
|
349
|
|
|
* |
|
350
|
|
|
* @since 2.0 |
|
351
|
|
|
* @access public |
|
352
|
|
|
*/ |
|
353
|
|
|
public function load() { |
|
354
|
|
|
add_action( 'init', array( $this, 'init' ), -1 ); |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
// Helper class. |
|
359
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php'; |
|
360
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php'; |
|
361
|
|
|
|
|
362
|
|
|
// Add backward compatibility. |
|
363
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/backward-compatibility.php'; |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Initialize functionality. |
|
367
|
|
|
*/ |
|
368
|
|
|
Give_Email_Notifications::get_instance()->load(); |
|
369
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.