/
class-helper.php
328 lines (285 loc) · 7.16 KB
/
class-helper.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* AnsPress email class.
*
* @package AnsPress
* @subpackage Email
* @since 4.1.0
* @author Rahul Aryan<support@anspress.net>
* @copyright Copyright (c) 2017, Rahul Aryan
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License
*/
namespace AnsPress\Addons\Email;
/**
* The email helper class.
*
* @since 4.1.0
* @since 4.1.8 Renamed class From Email to helper.
*/
class Helper {
/**
* The arguments.
*
* @var array
* @since 4.1.0
*/
public $args = array();
/**
* List of emails where to send notifications.
*
* @var array
*/
private $emails = array();
/**
* The email subject.
*
* @var string
* @since 4.1.0
*/
public $subject = '';
/**
* The email body.
*
* @var string
* @since 4.1.0
*/
public $body = '';
/**
* The email event.
*
* @var string
* @since 4.1.0
*/
public $event = '';
/**
* The email template.
*
* @var string
* @since 4.1.0
*/
public $template = '';
/**
* Email template tags.
*
* @var array
* @since 4.1.0
*/
public $template_tags = array();
/**
* The email header.
*
* @var array
*/
public $email_headers = array();
/**
* Initialize email class.
*
* @param string $event Event name.
* @param array $args Arguments.
*/
public function __construct( $event, $args = array() ) {
$this->event = $event;
$this->args = ap_parse_args(
$args,
array(
'users' => array(),
'subject' => '',
'tags' => array(
'site_name' => get_bloginfo( 'name' ),
'site_url' => get_bloginfo( 'url' ),
'site_description' => get_bloginfo( 'description' ),
),
'template' => array(),
'headers' => array(
'Content-Type: text/html; charset=utf-8',
),
)
);
$this->email_headers = $this->args['headers'];
unset( $this->args['headers'] );
// Add template tags.
if ( ! empty( $this->args['tags'] ) ) {
foreach ( $this->args['tags'] as $tag => $content ) {
$this->add_template_tag( $tag, $content );
}
}
}
/**
* Add an email to the currently sending list.
*
* @param string $email Email.
* @return void
*/
public function add_email( $email ) {
$email = trim( $email );
if ( ! in_array( $email, $this->emails, true ) ) {
/**
* Hook triggered before an email added to current sending list.
*
* @since 4.1.0
*/
do_action_ref_array( 'ap_before_email_to_list', array( $this ) );
$this->emails[] = $email;
}
}
/**
* Add user or email to current sending list.
*
* @param array $user_id User id or email.
* @return void
* @since 4.1.0
*/
public function add_user( $user_id ) {
if ( ! in_array( $user_id, $this->args['users'] ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$this->args['users'][] = $user_id;
}
}
/**
* Add template tags.
*
* @param string $tag Template tag key.
* @param string $content Tag content.
* @return void
* @since 4.1.0
*/
public function add_template_tag( $tag, $content ) {
$tag = '{' . sanitize_key( $tag ) . '}';
if ( ! isset( $this->template_tags[ $tag ] ) ) {
$this->template_tags[ $tag ] = $content;
/**
* Action triggered after adding email template tag.
*
* @since 4.1.0
*/
do_action_ref_array( 'ap_adding_email_tag', array( $this ) );
}
}
/**
* Add multiple template tags.
*
* @param array $tags Template tags.
* @return void
* @since 4.1.0
*/
public function add_template_tags( $tags ) {
foreach ( $tags as $tag => $content ) {
$this->add_template_tag( $tag, $content );
}
}
/**
* Get default template for an email.
*
* @return string
* @since 4.1.0
*/
public function get_default_template() {
/**
* Filter for adding default email template.
*
* @since 4.1.0
*/
return apply_filters( "ap_email_default_template_{$this->event}", '' );
}
/**
* Prepare template for email body based on event type.
*
* @return string
* @since 4.1.0
*/
public function prepare_template() {
$page_id = ap_opt( 'email_template_' . $this->event );
$_post = get_post( $page_id );
if ( $_post ) {
$this->template = apply_filters( 'the_content', $_post->post_content );
$this->subject = $_post->post_title;
}
// If template not found use default one.
if ( empty( $this->template ) ) {
$default_template = \AnsPress\Addons\Email::init()->get_default_template( $this->event );
$this->template = $default_template['body'];
$this->subject = $default_template['subject'];
}
$this->body = strtr( $this->template, $this->template_tags );
$this->subject = strtr( $this->subject, $this->template_tags );
$main_tags = array(
'email_title' => $this->subject,
'email_body' => $this->body,
'style' => file_get_contents( ap_get_theme_location( 'addons/email/style.css' ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
'site_name' => get_bloginfo( 'name' ),
);
/**
* This filter allows overriding `$main_tags`. Which is used in main
* email template.
*
* @param string $main_template Parsed template.
* @param object $email Current email object.
* @since 4.1.0
* @return string
*/
$main_tags = apply_filters_ref_array( 'ap_email_main_tags', array( $main_tags, $this ) );
foreach ( $main_tags as $key => $content ) {
$this->add_template_tag( $key, $content );
}
$main_template = file_get_contents( ap_get_theme_location( 'addons/email/template.html' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$main_template = strtr( $main_template, $this->template_tags );
/**
* Allows filtering email template after its prepared, passed be reference.
*
* @param string $main_template Parsed template.
* @param object $email Current email object.
* @since 4.1.0
* @return string
*/
$main_template = apply_filters_ref_array( 'ap_email_template_prepared', array( $main_template, $this ) );
return $main_template;
}
/**
* Get email ids of user. If email passed then add it to email
* property.
*
* @return void
* @since 4.1.0
*/
public function prepare_emails() {
global $wpdb;
if ( empty( $this->args['users'] ) ) {
return;
}
$user_ids = array();
foreach ( $this->args['users'] as $k => $id ) {
if ( is_email( $id ) ) {
$this->add_email( $id );
} elseif ( is_numeric( $id ) ) {
$user_ids[] = (int) $id;
}
}
$ids_str = esc_sql( sanitize_comma_delimited( $user_ids ) );
if ( empty( $ids_str ) ) {
return;
}
$emails = $wpdb->get_col( "SELECT user_email FROM {$wpdb->users} WHERE ID IN ({$ids_str})" ); // phpcs:ignore WordPress.DB
foreach ( $emails as $email ) {
$this->add_email( $email );
}
}
/**
* Send emails.
*
* @return boolean|\WP_Error
*/
public function send_emails() {
if ( defined( 'AP_DISABLE_EMAIL' ) && true === AP_DISABLE_EMAIL ) {
return;
}
$body = $this->prepare_template();
$this->prepare_emails();
if ( empty( $this->emails ) ) {
return new \WP_Error( 'no_emails' );
}
if ( ! empty( $this->emails ) ) {
foreach ( $this->emails as $email ) {
wp_mail( $email, $this->subject, $body, $this->email_headers );
}
}
}
}