-
Notifications
You must be signed in to change notification settings - Fork 18
/
pmpro-email-templates.php
502 lines (421 loc) · 16.8 KB
/
pmpro-email-templates.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php
/**
* Plugin Name: Paid Memberships Pro - Email Templates Add On
* Description: Define your own custom PMPro HTML Email Templates.
* Author: Stranger Studios
* Author URI: http://www.strangerstudios.com
* Plugin URI: http://www.paidmembershipspro.com/add-ons/plugins-wordpress-repository/email-templates-admin-editor/
* Version: 0.6.2
*/
/*
* Includes
*/
function pmproet_init() {
require_once(dirname(__FILE__) . '/includes/init.php');
}
add_action('init', 'pmproet_init');
/*
* Setup admin pages
*/
function pmproet_setup() {
add_submenu_page('pmpro-membershiplevels', __('Email Templates', 'pmproet'), __('Email Templates', 'pmproet'), 'manage_options', 'pmpro-email-templates', 'pmproet_admin_page');
}
add_action('admin_menu', 'pmproet_setup');
function pmproet_admin_page()
{
require_once( plugin_dir_path(__FILE__) ) . "adminpages/emailtemplates.php";
}
/*
Add page to admin bar
*/
function pmproet_admin_bar_menu() {
global $wp_admin_bar;
if ( !is_super_admin() || !is_admin_bar_showing() )
return;
$wp_admin_bar->add_menu( array(
'id' => 'pmpro-email-templates',
'parent' => 'paid-memberships-pro',
'title' => __( 'Email Templates', 'pmpro'),
'href' => get_admin_url(NULL, '/admin.php?page=pmpro-email-templates') ) );
}
add_action('admin_bar_menu', 'pmproet_admin_bar_menu', 1000);
//enqueue js/css
function pmproet_scripts() {
if (!empty($_REQUEST['page']) && $_REQUEST['page'] == 'pmpro-email-templates') {
wp_enqueue_script('pmproet', plugin_dir_url(__FILE__) . 'js/pmproet.js', array('jquery'), null, false);
wp_enqueue_style('pmproet', plugin_dir_url(__FILE__) . 'css/pmproet.css');
}
}
add_action('admin_enqueue_scripts', 'pmproet_scripts');
/*
* AJAX Functions
*/
//get template data
function pmproet_get_template_data() {
check_ajax_referer('pmproet', 'security');
global $pmproet_email_defaults;
$template = $_REQUEST['template'];
//get template data
$template_data['body'] = pmpro_getOption('email_' . $template . '_body');
$template_data['subject'] = pmpro_getOption('email_' . $template . '_subject');
$template_data['disabled'] = pmpro_getOption('email_' . $template . '_disabled');
if (empty($template_data['body'])) {
//if not found, load template
$template_data['body'] = pmproet_getTemplateBody($template);
}
if (empty($template_data['subject']) && $template != "header" && $template != "footer") {
$template_data['subject'] = $pmproet_email_defaults[$template]['subject'];
}
echo json_encode($template_data);
exit;
}
add_action('wp_ajax_pmproet_get_template_data', 'pmproet_get_template_data');
//save template data
function pmproet_save_template_data() {
check_ajax_referer('pmproet', 'security');
//update this template's settings
pmpro_setOption('email_' . $_REQUEST['template'] . '_subject', stripslashes($_REQUEST['subject']));
pmpro_setOption('email_' . $_REQUEST['template'] . '_body', stripslashes($_REQUEST['body']));
echo 'Template Saved';
exit;
}
add_action('wp_ajax_pmproet_save_template_data', 'pmproet_save_template_data');
//reset template data
function pmproet_reset_template_data() {
check_ajax_referer('pmproet', 'security');
global $pmproet_email_defaults;
$template = $_REQUEST['template'];
delete_option('pmpro_email_' . $template . '_subject');
delete_option('pmpro_email_' . $template . '_body');
$template_data['subject'] = $pmproet_email_defaults[$template]['subject'];
$template_data['body'] = pmproet_getTemplateBody($template);
echo json_encode($template_data);
exit;
}
add_action('wp_ajax_pmproet_reset_template_data', 'pmproet_reset_template_data');
// disable template
function pmproet_disable_template() {
check_ajax_referer('pmproet', 'security');
$template = $_REQUEST['template'];
$response['result'] = update_option('pmpro_email_' . $template . '_disabled', $_REQUEST['disabled']);
$response['status'] = $_REQUEST['disabled'];
echo json_encode($response);
exit;
}
add_action('wp_ajax_pmproet_disable_template', 'pmproet_disable_template');
//send test email
function pmproet_send_test() {
check_ajax_referer('pmproet', 'security');
global $pmproet_test_order_id, $current_user;
//setup test email
$test_email = new PMProEmail();
$test_email->to = $_REQUEST['email'];
$test_email->template = str_replace('email_', '', $_REQUEST['template']);
//add filter to change recipient
add_filter('pmpro_email_recipient', 'pmproet_test_pmpro_email_recipient');
//load test order
$pmproet_test_order_id = get_option('pmproet_test_order_id');
$test_order = new MemberOrder($pmproet_test_order_id);
//add notice to email body
add_filter('pmpro_email_body', 'pmproet_test_email_body');
//force the template
add_filter('pmpro_email_filter', 'pmproet_test_email_template', 5);
//figure out how to send the email
switch($test_email->template) {
case 'cancel':
$send_email = 'sendCancelEmail';
$params = array($current_user);
break;
case 'cancel_admin':
$send_email = 'sendCancelAdminEmail';
$params = array($current_user, $current_user->membership_level->id);
break;
case 'checkout_check':
case 'checkout_express':
case 'checkout_free':
case 'checkout_freetrial':
case 'checkout_paid':
case 'checkout_trial':
$send_email = 'sendCheckoutEmail';
$params = array($current_user, $test_order);
break;
case 'checkout_check_admin':
case 'checkout_express_admin':
case 'checkout_free_admin':
case 'checkout_freetrial_admin':
case 'checkout_paid_admin':
case 'checkout_trial_admin':
$send_email = 'sendCheckoutAdminEmail';
$params = array($current_user, $test_order);
break;
case 'billing':
$send_email = 'sendBillingEmail';
$params = array($current_user, $test_order);
break;
case 'billing_admin':
$send_email = 'sendBillingAdminEmail';
$params = array($current_user, $test_order);
break;
case 'billing_failure':
$send_email = 'sendBillingFailureEmail';
$params = array($current_user, $test_order);
break;
case 'billing_failure_admin':
$send_email = 'sendBillingFailureAdminEmail';
$params = array($current_user->user_email, $test_order);
break;
case 'credit_card_expiring':
$send_email = 'sendCreditCardExpiringEmail';
$params = array($current_user, $test_order);
break;
case 'invoice':
$send_email = 'sendInvoiceEmail';
$params = array($current_user, $test_order);
break;
case 'trial_ending':
$send_email = 'sendTrialEndingEmail';
$params = array($current_user);
break;
case 'membership_expired';
$send_email = 'sendMembershipExpiredEmail';
$params = array($current_user);
break;
case 'membership_expiring';
$send_email = 'sendMembershipExpiringEmail';
$params = array($current_user);
break;
default:
$send_email = 'sendEmail';
$params = array();
}
//send the email
$response = call_user_func_array(array($test_email, $send_email), $params);
//return the response
echo $response;
exit;
}
add_action('wp_ajax_pmproet_send_test', 'pmproet_send_test');
function pmproet_test_pmpro_email_recipient($email)
{
if(!empty($_REQUEST['email']))
$email = $_REQUEST['email'];
return $email;
}
/* Filter Subject and Body */
function pmproet_email_filter($email) {
global $pmproet_email_defaults;
//is this email disabled or is it not in the templates array?
if(pmpro_getOption('email_' . $email->template . '_disabled') == 'true')
return false;
//leave the email alone if it's not in the list of templates
if( ! $pmproet_email_defaults[$email->template] )
return $email;
$et_subject = pmpro_getOption('email_' . $email->template . '_subject');
$et_header = pmpro_getOption('email_header_body');
$et_body = pmpro_getOption('email_' . $email->template . '_body');
$et_footer = pmpro_getOption('email_footer_body');
if(!empty($et_subject))
$email->subject = $et_subject;
//is header disabled?
if(pmpro_getOption('email_header_disabled') != 'true') {
if(!empty($et_header))
$temp_content = $et_header;
else
$temp_content = pmproet_getTemplateBody('header');
}
if(!empty($et_body))
$temp_content .= $et_body;
else
$temp_content .= pmproet_getTemplateBody($email->template);
//is footer disabled?
if(pmpro_getOption('email_footer_disabled') != 'true') {
if(!empty($et_footer))
$temp_content .= $et_footer;
else
$temp_content .= pmproet_getTemplateBody('footer');
}
$email->body = $temp_content;
//replace data
foreach($email->data as $key => $value)
{
$email->body = str_replace("!!" . $key . "!!", $value, $email->body);
$email->subject = str_replace("!!" . $key . "!!", $value, $email->subject);
}
return $email;
}
add_filter('pmpro_email_filter', 'pmproet_email_filter');
//for test emails
function pmproet_test_email_body($body, $email) {
$body .= '<br><br><b>--- ' . __('THIS IS A TEST EMAIL', 'pmproet') . ' --</b>';
return $body;
}
function pmproet_test_email_template($email)
{
if(!empty($_REQUEST['template']))
$email->template = str_replace('email_', '', $_REQUEST['template']);
return $email;
}
/* Filter for Variables */
function pmproet_email_data($data, $email) {
global $current_user, $pmpro_currency_symbol, $wpdb;
if(!empty($data) && !empty($data['user_login']))
$user = get_user_by('login', $data['user_login']);
if(empty($user))
$user = $current_user;
$pmpro_user_meta = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user->ID . "' AND status='active'");
//make sure data is an array
if(!is_array($data))
$data = array();
//general data
$new_data['sitename'] = get_option("blogname");
$new_data['siteemail'] = pmpro_getOption("from_email");
if(empty($new_data['login_link']))
$new_data['login_link'] = wp_login_url();
$new_data['levels_link'] = pmpro_url("levels");
//user data
if(!empty($user))
{
$new_data['name'] = $user->display_name;
$new_data['user_login'] = $user->user_login;
$new_data['display_name'] = $user->display_name;
$new_data['user_email'] = $user->user_email;
}
//membership data
if(!empty($user->membership_level))
$new_data['enddate'] = date(get_option('date_format'), $user->membership_level->enddate);
//invoice data
if(!empty($data['invoice_id']))
{
$invoice = new MemberOrder($data['invoice_id']);
if(!empty($invoice))
{
$new_data['billing_name'] = $invoice->billing->name;
$new_data['billing_street'] = $invoice->billing->street;
$new_data['billing_city'] = $invoice->billing->city;
$new_data['billing_state'] = $invoice->billing->state;
$new_data['billing_zip'] = $invoice->billing->zip;
$new_data['billing_country'] = $invoice->billing->country;
$new_data['billing_phone'] = $invoice->billing->phone;
$new_data['cardtype'] = $invoice->cardtype;
$new_data['accountnumber'] = hideCardNumber($invoice->accountnumber);
$new_data['expirationmonth'] = $invoice->expirationmonth;
$new_data['expirationyear'] = $invoice->expirationyear;
$new_data['instructions'] = wpautop(pmpro_getOption('instructions'));
$new_data['invoice_id'] = $invoice->code;
$new_data['invoice_total'] = $pmpro_currency_symbol . number_format($invoice->total, 2);
$new_data['invoice_link'] = pmpro_url('invoice', '?invoice=' . $invoice->code);
//billing address
$new_data["billing_address"] = pmpro_formatAddress($invoice->billing->name,
$invoice->billing->street,
"", //address 2
$invoice->billing->city,
$invoice->billing->state,
$invoice->billing->zip,
$invoice->billing->country,
$invoice->billing->phone);
}
}
//membership change
if(!empty($user->membership_level) && !empty($user->membership_level->ID))
$new_data["membership_change"] = sprintf(__("The new level is %s.", "pmproet"), $user->membership_level->name);
else
$new_data["membership_change"] = __("Your membership has been cancelled", "pmproet");
if(!empty($user->membership_level) && !empty($user->membership_level->enddate))
$new_data["membership_change"] .= ". " . sprintf(__("This membership will expire on %s", "pmproet"), date(get_option('date_format'), $user->membership_level->enddate));
elseif(!empty($email->expiration_changed))
$new_data["membership_change"] .= ". " . __("This membership does not expire", "pmproet");
//membership expiration
$new_data['membership_expiration'] = '';
if(!empty($pmpro_user_meta->enddate))
$new_data['membership_expiration'] = "<p>" . sprintf(__("This membership will expire on %s.", "pmproet"), $pmpro_user_meta->enddate . "</p>\n");
//if others are used in the email look in usermeta
$et_body = pmpro_getOption('email_' . $email->template . '_body');
$templates_in_email = preg_match_all("/!!([^!]+)!!/", $et_body, $matches);
if(!empty($templates_in_email))
{
$matches = $matches[1];
foreach($matches as $match)
{
if(empty($new_data[$match]))
{
$usermeta = get_user_meta($user->ID, $match, true);
if(!empty($usermeta))
{
if(is_array($usermeta) && !empty($usermeta['fullurl']))
$new_data[$match] = $usermeta['fullurl'];
elseif(is_array($usermeta))
$new_data[$match] = implode(", ", $usermeta);
else
$new_data[$match] = $usermeta;
}
}
}
}
//now replace any new_data not already in data
foreach($new_data as $key => $value)
{
if(!isset($data[$key]))
$data[$key] = $value;
}
return $data;
}
add_filter('pmpro_email_data', 'pmproet_email_data', 10, 2);
/**
* Load the default email template.
*
* Checks theme, then template, then PMPro directory.
*
* @since 0.6
*
* @param $template string
*
* @return string
*/
function pmproet_getTemplateBody($template) {
global $pmproet_email_defaults;
// Defaults
$body = "";
$file = false;
// Load template
if(!empty($pmproet_email_defaults[$template]['body']))
$body = $pmproet_email_defaults[$template]['body'];
elseif ( file_exists( get_stylesheet_directory() . '/paid-memberships-pro/email/' . $template . '.html' ) ) {
$file = get_stylesheet_directory() . '/paid-memberships-pro/email/' . $template . '.html';
} elseif ( file_exists( get_template_directory() . '/paid-memberships-pro/email/' . $template . '.html') ) {
$file = get_template_directory() . '/paid-memberships-pro/email/' . $template . '.html';
} elseif( file_exists( PMPRO_DIR . '/email/' . $template . '.html')) {
$file = PMPRO_DIR . '/email/' . $template . '.html';
}
if( $file && ! $body ) {
ob_start();
require_once( $file );
$body = ob_get_contents();
ob_end_clean();
}
return $body;
}
/*
Function to add links to the plugin action links
*/
function pmproet_add_action_links($links) {
$new_links = array(
'<a href="' . get_admin_url(NULL, 'admin.php?page=pmpro-email-templates') . '">Settings</a>',
);
return array_merge($new_links, $links);
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'pmproet_add_action_links');
/*
Function to add links to the plugin row meta
*/
function pmproet_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-email-templates.php') !== false)
{
$new_links = array(
'<a href="' . esc_url('http://www.paidmembershipspro.com/add-ons/plugins-wordpress-repository/email-templates-admin-editor/') . '" title="' . esc_attr( __( 'View Documentation', 'pmproet' ) ) . '">' . __( 'Docs', 'pmproet' ) . '</a>',
'<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmproet' ) ) . '">' . __( 'Support', 'pmproet' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmproet_plugin_row_meta', 10, 2);