forked from strangerstudios/pmpro-donations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmpro-donations.php
490 lines (433 loc) · 16.1 KB
/
pmpro-donations.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
<?php
/*
Plugin Name: Paid Memberships Pro - Donations
Plugin URI: http://www.paidmembershipspro.com/add-ons/pmpro-donations/
Description: Allow customers to set an additional donation amount at checkout.
Version: .3.1
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
/*
Min Price and Max Price Fields on the edit levels page
*/
//fields on edit page
function pmprodon_pmpro_membership_level_after_other_settings()
{
global $pmpro_currency_symbol;
$level_id = intval($_REQUEST['edit']);
if($level_id > 0)
{
$donfields = get_option("pmprodon_" . $level_id, array('donations' => 0, 'min_price' => '', 'max_price' => ''));
$donations = $donfields['donations'];
$min_price = $donfields['min_price'];
$max_price = $donfields['max_price'];
$donations_text = $donfields['text'];
$dropdown_prices = $donfields['dropdown_prices'];
}
else
{
$donations = 0;
$min_price = '';
$max_price = '';
$donations_text = '';
$dropdown_prices = '';
}
?>
<h3 class="topborder">Donations</h3>
<p>If donations are enabled, users will be able to set an additional donation amount at checkout. That price will be added to any initial payment you set on this level. You can set the minimum and maxium amount allowed for gifts for this level.</p>
<table>
<tbody class="form-table">
<tr>
<th scope="row" valign="top"><label for="donations">Enable:</label></th>
<td>
<input type="checkbox" id="donations" name="donations" value="1" <?php checked($donations, "1");?> /> Enable Donations
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="donation_min_price">Min Amount:</label></th>
<td>
<?php echo $pmpro_currency_symbol?><input type="text" id="donation_min_price" name="donation_min_price" value="<?php echo esc_attr($min_price); ?>" />
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="donation_max_price">Max Amount:</label></th>
<td>
<?php echo $pmpro_currency_symbol?><input type="text" id="donation_max_price" name="donation_max_price" value="<?php echo esc_attr($max_price); ?>" />
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="dropdown_prices">Price Dropdown:</label></th>
<td>
<input type="text" id="dropdown_prices" name="dropdown_prices" size="60" value="<?php echo esc_attr($dropdown_prices); ?>" /><br /><small>Enter numbers separated by commas to popuplate a dropdown with suggested prices. Include "other" (all lowercase) in the list to allow users to enter their own amount.</small>
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="donations_text">Help Text:</label></th>
<td>
<textarea id="donations_text" name="donations_text" rows="5" cols="60"><?php echo esc_textarea($donations_text);?></textarea>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action("pmpro_membership_level_after_other_settings", "pmprodon_pmpro_membership_level_after_other_settings");
//save level cost text when the level is saved/added
function pmprodon_pmpro_save_membership_level($level_id)
{
if(!empty($_REQUEST['donations']))
$donations = 1;
else
$donations = 0;
$min_price = preg_replace("[^0-9\.]", "", $_REQUEST['donation_min_price']);
$max_price = preg_replace("[^0-9\.]", "", $_REQUEST['donation_max_price']);
$text = $_REQUEST['donations_text'];
$dropdown_prices = $_REQUEST['dropdown_prices'];
update_option("pmprodon_" . $level_id, array('donations' => $donations, 'min_price' => $min_price, 'max_price' => $max_price, 'text'=>$text, 'dropdown_prices'=>$dropdown_prices));
}
add_action("pmpro_save_membership_level", "pmprodon_pmpro_save_membership_level");
/*
Update donation amount if a dropdown value is used
*/
function pmprodon_init_dropdown_values()
{
if(!empty($_SESSION['donation_dropdown']) && $_SESSION['donation_dropdown'] != 'other')
$_SESSION['donation'] = $_SESSION['donation_dropdown'];
if(!empty($_REQUEST['donation_dropdown']) && $_REQUEST['donation_dropdown'] != 'other')
$_REQUEST['donation'] = $_REQUEST['donation_dropdown'];
if(!empty($_GET['donation_dropdown']) && $_GET['donation_dropdown'] != 'other')
$_GET['donation'] = $_GET['donation_dropdown'];
if(!empty($_POST['donation_dropdown']) && $_POST['donation_dropdown'] != 'other')
$_POST['donation'] = $_POST['donation_dropdown'];
}
add_action('init', 'pmprodon_init_dropdown_values', 1);
/*
Show form at checkout.
*/
function pmprodon_pmpro_checkout_after_level_cost()
{
global $pmpro_currency_symbol, $pmpro_level, $gateway, $pmpro_review;
//get variable pricing info
$donfields = get_option("pmprodon_" . $pmpro_level->id);
//no variable pricing? just return
if(empty($donfields) || empty($donfields['donations']))
return;
//okay, now we're showing the form
$min_price = $donfields['min_price'];
$max_price = $donfields['max_price'];
$dropdown_prices = $donfields['dropdown_prices'];
if(isset($_REQUEST['donation']))
$donation = preg_replace("[^0-9\.]", "", $_REQUEST['donation']);
elseif(isset($_SESSION['donation']))
$donation = preg_replace("[^0-9\.]", "", $_SESSION['donation']);
elseif(!empty($min_price))
$donation = $min_price;
else
$donation = "";
?>
<p>
Make a Gift
<?php
//check for dropdown
if(!empty($dropdown_prices))
{
//turn into an array
$dropdown_prices = str_replace(" ", "", $dropdown_prices);
$dropdown_prices = explode(",", $dropdown_prices);
//check for other option
$pmprodon_allow_other = array_search("other", $dropdown_prices);
if($pmprodon_allow_other !== false)
{
unset($dropdown_prices[$pmprodon_allow_other]);
$pmprodon_allow_other = true;
}
//show dropdown
sort($dropdown_prices);
?>
<select id="donation_dropdown" name="donation_dropdown" <?php if($pmpro_review) { ?>disabled="disabled"<?php } ?>>
<?php
foreach($dropdown_prices as $price)
{
?>
<option <?php selected($price, $donation);?> value="<?php echo esc_attr($price);?>"><?php echo pmpro_formatPrice((double)$price);?></option>
<?php
}
?>
<option value="other" <?php selected(true, !empty($donation) && !in_array($donation, $dropdown_prices));?>>Other</option>
</select>
<?php
}
?>
<span id="pmprodon_donation_input" <?php if(!empty($pmprodon_allow_other) && $_REQUEST['donation_dropdown'] != 'other') { ?>style="display: none;"<?php } ?>>
<?php echo $pmpro_currency_symbol;?> <input type="text" id="donation" name="donation" size="10" value="<?php echo esc_attr($donation);?>" <?php if($pmpro_review) { ?>disabled="disabled"<?php } ?> />
<?php if($pmpro_review) { ?><input type="hidden" name="donation" value="<?php echo esc_attr($donation);?>" /><?php } ?>
</span>
<br />
<?php
if(empty($pmpro_review))
{
if(!empty($donfields['text']))
echo $donfields['text'];
elseif(!empty($donfields['min_price']) && empty($donfields['max_price']))
{?>
Enter an amount <?php echo $pmpro_currency_symbol . $donfields['min_price'];?> or greater<?php
}
elseif(!empty($donfields['max_price']) && empty($donfields['min_price']))
{?>
Enter an amount <?php echo $pmpro_currency_symbol . $donfields['max_price'];?> or less<?php
}
elseif(!empty($donfields['max_price']) && !empty($donfields['min_price']))
{?>
Enter an amount between <?php echo $pmpro_currency_symbol . $donfields['min_price'];?> and <?php echo $pmpro_currency_symbol . $donfields['max_price'];
}
}
?>
</p>
<script>
//some vars for keeping track of whether or not we show billing
var pmpro_gateway_billing = <?php if(in_array($gateway, array("paypalexpress", "twocheckout")) !== false) echo "false"; else echo "true";?>;
var pmpro_pricing_billing = <?php if(!pmpro_isLevelFree($pmpro_level)) echo "true"; else echo "false";?>;
var pmpro_donation_billing = pmpro_pricing_billing;
//this script will hide show billing fields based on the price set
jQuery(document).ready(function() {
//bind other field toggle to dropdown change
jQuery('#donation_dropdown').change(function() {
pmprodon_toggleOther();
});
//bind check to price field
var pmprodon_price_timer;
jQuery('#donation').bind('keyup change', function() {
pmprodon_price_timer = setTimeout(pmprodon_checkForFree, 500);
});
if(jQuery('input[name=gateway]'))
{
jQuery('input[name=gateway]').bind('click', function() {
pmprodon_price_timer = setTimeout(pmprodon_checkForFree, 500);
});
}
//check when page loads too
pmprodon_toggleOther();
pmprodon_checkForFree();
});
function pmprodon_toggleOther()
{
//make sure there is a dropdown to check
if(!jQuery('#donation_dropdown').length)
return;
//get val
var donation_dropdown = jQuery('#donation_dropdown').val();
if(donation_dropdown == 'other')
jQuery('#pmprodon_donation_input').show();
else
jQuery('#pmprodon_donation_input').hide();
}
function pmprodon_checkForFree()
{
var donation = parseFloat(jQuery('#donation').val());
//does the gateway require billing?
if(jQuery('input[name=gateway]').length)
{
var no_billing_gateways = ['paypalexpress', 'twocheckout', 'check', 'paypalstandard'];
var gateway = jQuery('input[name=gateway]:checked').val();
if(no_billing_gateways.indexOf(gateway) > -1)
pmpro_gateway_billing = false;
else
pmpro_gateway_billing = true;
}
//is there a donation?
if(donation || pmpro_pricing_billing)
pmpro_donation_billing = true;
else
pmpro_donation_billing = false;
//figure out if we should show the billing fields
if(pmpro_gateway_billing && pmpro_donation_billing)
{
jQuery('#pmpro_billing_address_fields').show();
jQuery('#pmpro_payment_information_fields').show();
pmpro_require_billing = true;
}
else
{
jQuery('#pmpro_billing_address_fields').hide();
jQuery('#pmpro_payment_information_fields').hide();
pmpro_require_billing = false;
}
}
</script>
<?php
}
add_action('pmpro_checkout_after_level_cost', 'pmprodon_pmpro_checkout_after_level_cost');
//set price
function pmprodon_pmpro_checkout_level($level)
{
if(isset($_REQUEST['donation']))
$donation = preg_replace("[^0-9\.]", "", $_REQUEST['donation']);
else
return $level;
if(!empty($donation))
{
//save initial payment amount
global $pmprodon_original_initial_payment;
$pmprodon_original_initial_payment = $level->initial_payment;
//add donation
$level->initial_payment = $level->initial_payment + $donation;
}
return $level;
}
add_filter("pmpro_checkout_level", "pmprodon_pmpro_checkout_level", 99);
//check price is between min and max
function pmprodon_pmpro_registration_checks($continue)
{
//only bother if we are continuing already
if($continue)
{
global $pmpro_currency_symbol, $pmpro_msg, $pmpro_msgt;
//was a donation passed in?
if(isset($_REQUEST['donation']))
{
//get values
$level_id = intval($_REQUEST['level']);
$donfields = get_option("pmprodon_" . $level_id);
//make sure this level has variable pricing
if(empty($donfields) || empty($donfields['donations']))
{
$pmpro_msg = "Error: You tried to set the donation on a level that doesn't have donations. Please try again.";
$pmpro_msgt = "pmmpro_error";
}
//get price
$donation = preg_replace("[^0-9\.]", "", $_REQUEST['donation']);
//check that the donation falls between the min and max
if(!empty($donfields['min_price']) && (double)$donation < (double)$donfields['min_price'])
{
$pmpro_msg = "The lowest accepted donation is " . $pmpro_currency_symbol . $donfields['min_price'] . ". Please enter a new amount.";
$pmpro_msgt = "pmmpro_error";
$continue = false;
}
elseif(!empty($donfields['max_price']) && (double)$donation > (double)$donfields['max_price'])
{
$pmpro_msg = "The highest accepted donation is " . $pmpro_currency_symbol . $donfields['max_price'] . ". Please enter a new amount.";
$pmpro_msgt = "pmmpro_error";
$continue = false;
}
//all good!
}
}
return $continue;
}
add_filter("pmpro_registration_checks", "pmprodon_pmpro_registration_checks");
/*
override level cost text on checkout page
*/
function pmprodon_pmpro_level_cost_text($text, $level)
{
global $pmpro_pages, $pmprodon_original_initial_payment, $pmprodon_text_level_cost_updated;
if(is_page($pmpro_pages['checkout']) && !empty($pmprodon_original_initial_payment) && empty($pmprodon_text_level_cost_updated))
{
$olevel = $level;
$olevel->initial_payment = $pmprodon_original_initial_payment;
$pmprodon_text_level_cost_updated = true; //to prevent loops
$text = pmpro_getLevelCost($olevel);
}
return $text;
}
add_filter("pmpro_level_cost_text", "pmprodon_pmpro_level_cost_text", 10, 2);
/*
Save donation amount to order notes.
*/
function pmprodon_pmpro_checkout_order($order)
{
if(!empty($_REQUEST['donation']))
$donation = preg_replace("[^0-9\.]", "", $_REQUEST['donation']);
else
return $order;
if(!empty($donation) && strpos($order->notes, "Donation:") === false)
$order->notes .= "Donation:" . $donation . "\n";
return $order;
}
add_filter('pmpro_checkout_order', 'pmprodon_pmpro_checkout_order');
/*
Function to get donation and original price out of an order.
*/
function pmprodon_getPriceComponents($order)
{
$r = array("price" => $order->total, "donation"=> "");
if(!empty($order->notes) && strpos($order->notes, "Donation:") !== false)
{
$donation = pmpro_getMatches("/Donation\:([0-9\.]+)/", $order->notes, true);
$r['donation'] = $donation;
if($donation > 0)
$r['price'] = $order->total - $donation;
}
//filter added .2
$r = apply_filters('pmpro_donations_get_price_components', $r, $order);
return $r;
}
/*
Show order components on confirmation and invoice pages.
*/
function pmprodon_pmpro_invoice_bullets_bottom($order)
{
$components = pmprodon_getPriceComponents($order);
if(!empty($components['donation']))
{
?>
<li><strong><?php _e('Membership Cost', 'pmpro');?>:</strong> <?php echo pmpro_formatPrice($components['price']);?></li>
<li><strong><?php _e('Donation', 'pmpro');?>:</strong> <?php echo pmpro_formatPrice($components['donation']);?></li>
<?php
}
}
add_filter('pmpro_invoice_bullets_bottom', 'pmprodon_pmpro_invoice_bullets_bottom');
/*
Show order components in confirmation email.
*/
/*
adding meta fields to confirmation email
*/
function pmprodon_pmpro_email_filter($email)
{
global $wpdb;
//only update admin confirmation emails
if(strpos($email->template, "checkout") !== false)
{
//get the user_id from the email
$order_id = $email->data['invoice_id'];
if(!empty($order_id))
{
$order = new MemberOrder($order_id);
$components = pmprodon_getPriceComponents($order);
//add to bottom of email
if(!empty($components['donation']))
{
$email->body = preg_replace("/\<p\>\s*Invoice/", "<p>Donation Amount: " . pmpro_formatPrice($components['donation']) . "</p><p>Invoice", $email->body);
}
}
}
return $email;
}
add_filter("pmpro_email_filter", "pmprodon_pmpro_email_filter", 10, 2);
/*
Save donation amount into a session variable for PayPal Express.
*/
function pmprodon_pmpro_paypalexpress_session_vars()
{
//save our added fields in session while the user goes off to PayPal
if(isset($_REQUEST['donation_dropdown']))
$_SESSION['donation_dropdown'] = $_REQUEST['donation_dropdown'];
if(isset($_REQUEST['donation']))
$_SESSION['donation'] = $_REQUEST['donation'];
}
add_action("pmpro_paypalexpress_session_vars", "pmprodon_pmpro_paypalexpress_session_vars");
/*
Function to add links to the plugin row meta
*/
function pmprodon_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-donations.php') !== false)
{
$new_links = array(
'<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro' ) ) . '">' . __( 'Support', 'pmpro' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmprodon_plugin_row_meta', 10, 2);