forked from strangerstudios/pmpro-variable-pricing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmpro-variable-pricing.php
executable file
·482 lines (418 loc) · 16.6 KB
/
pmpro-variable-pricing.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
<?php
/*
Plugin Name: Paid Memberships Pro - Variable Pricing Add On
Plugin URI: https://www.paidmembershipspro.com/add-ons/variable-pricing-add-on/
Description: Allow customers to set their own price when checking out for your membership levels.
Version: .4.4
Author: Paid Memberships Pro
Author URI: https://www.paidmembershipspro.com
Text Domain: pmpro-variable-pricing
*/
/*
The Plan
- Override level cost text on checkout to show text field to set price.
- Use that price when checking out.
- Price overrides the initial payment and any billing amount for the level.
- Leaves trial, billing cycle, and expiration stuff alone.
- Add "min price" and "max price" fields to edit level page.
- Set price is the "suggested price"
*/
/*
Load plugin textdomain.
*/
function pmprovp_load_textdomain() {
load_plugin_textdomain( 'pmpro-variable-pricing', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'pmprovp_load_textdomain' );
/**
* Get settings for a specified level.
*/
function pmprovp_get_settings( $level_id ) {
$defaults = array(
'variable_pricing' => 0,
'min_price' => '',
'max_price' => '',
'suggested_price' => '',
);
$settings = get_option( "pmprovp_{$level_id}", $defaults );
$settings = array_merge( $defaults, $settings ); // make sure newly added settings have defaults appended
return $settings;
}
/*
Min Price and Max Price Fields on the edit levels page
*/
// fields on edit page
function pmprovp_pmpro_membership_level_after_other_settings() {
global $pmpro_currency_symbol;
$level_id = intval( $_REQUEST['edit'] );
if ( $level_id > 0 ) {
$vpfields = pmprovp_get_settings( $level_id );
$variable_pricing = $vpfields['variable_pricing'];
$min_price = $vpfields['min_price'];
$max_price = $vpfields['max_price'];
$suggested_price = $vpfields['suggested_price'];
} else {
$variable_pricing = 0;
$min_price = '';
$max_price = '';
$suggested_price = '';
}
?>
<h3 class="topborder"><?php esc_html_e( 'Variable Pricing', 'pmpro-variable-pricing' ); ?></h3>
<p><?php esc_html_e( 'If variable pricing is enabled, users will be able to set their own price at checkout. That price will override any initial payment and billing amount values you set on this level. You can set the minimum, maxium, and suggested price for this level.', 'pmpro-variable-pricing' ); ?></p>
<table>
<tbody class="form-table">
<tr>
<th scope="row" valign="top"><label for="pmprovp_variable_pricing"><?php _e( 'Enable:', 'pmpro-variable-pricing' ); ?></label></th>
<td>
<input type="checkbox" name="variable_pricing" id="pmprovp_variable_pricing" value="1" <?php checked( $variable_pricing, '1' ); ?> /> <label for="pmprovp_variable_pricing"><?php _e( 'Enable Variable Pricing', 'pmpro-variable-pricing' ); ?></label>
</td>
</tr>
<tr class="pmprovp_setting">
<th scope="row" valign="top"><label for="pmprovp_min_price"><?php _e( 'Min Price:', 'pmpro-variable-pricing' ); ?></label></th>
<td>
<?php echo esc_html( $pmpro_currency_symbol ); ?><input type="text" name="min_price" id="pmprovp_min_price" value="<?php echo esc_attr( $min_price ); ?>" />
</td>
</tr>
<tr class="pmprovp_setting">
<th scope="row" valign="top"><label for="pmprovp_max_price"><?php esc_html_e( 'Max Price:', 'pmpro-variable-pricing' ); ?></label></th>
<td>
<?php echo esc_html( $pmpro_currency_symbol ); ?><input type="text" name="max_price" id="pmprovp_max_price" value="<?php echo esc_attr( $max_price ); ?>" />
<?php esc_html_e( 'Leave this blank to allow any maximum amount.', 'pmpro-variable-pricing' ); ?>
</td>
</tr>
<tr class="pmprovp_setting">
<th scope="row" valign="top"><label for="pmprovp_suggested_price"><?php esc_html_e( 'Suggested Price:', 'pmpro-variable-pricing' ); ?></label></th>
<td>
<?php echo esc_html( $pmpro_currency_symbol ); ?><input type="text" name="suggested_price" id="pmprovp_suggested_price" value="<?php echo esc_attr( $suggested_price ); ?>" />
<?php esc_html_e( 'You may leave this blank.', 'pmpro-variable-pricing' ); ?>
</td>
</tr>
</tbody>
</table>
<script>
jQuery(document).ready(function(){
function pmprovp_toggleSettings() {
var pmprovp_enabled = jQuery('#pmprovp_variable_pricing:checked').val();
if(typeof pmprovp_enabled == 'undefined') {
//disabled
jQuery('tr.pmprovp_setting').hide();
} else {
//enabled
jQuery('tr.pmprovp_setting').show();
}
}
jQuery('#pmprovp_variable_pricing').change(function(){pmprovp_toggleSettings()});
pmprovp_toggleSettings();
});
</script>
<?php
}
add_action( 'pmpro_membership_level_after_other_settings', 'pmprovp_pmpro_membership_level_after_other_settings' );
// save level cost text when the level is saved/added
function pmprovp_pmpro_save_membership_level( $level_id ) {
$variable_pricing = intval( $_REQUEST['variable_pricing'] );
$min_price = preg_replace( '[^0-9\.]', '', $_REQUEST['min_price'] );
$max_price = preg_replace( '[^0-9\.]', '', $_REQUEST['max_price'] );
$suggested_price = preg_replace( '[^0-9\.]', '', $_REQUEST['suggested_price'] );
update_option(
'pmprovp_' . $level_id, array(
'variable_pricing' => $variable_pricing,
'min_price' => $min_price,
'max_price' => $max_price,
'suggested_price' => $suggested_price,
)
);
}
add_action( 'pmpro_save_membership_level', 'pmprovp_pmpro_save_membership_level' );
/*
Show form at checkout.
*/
// override level cost text on checkout page
function pmprovp_pmpro_level_cost_text( $text, $level ) {
global $pmpro_pages;
if ( is_page( $pmpro_pages['checkout'] ) && !did_action( 'pmpro_after_checkout' ) ) {
$vpfields = pmprovp_get_settings( $level->id );
if ( ! empty( $vpfields ) && ! empty( $vpfields['variable_pricing'] ) ) {
$text = '';
}
}
return $text;
}
add_filter( 'pmpro_level_cost_text', 'pmprovp_pmpro_level_cost_text', 10, 2 );
/**
* Display Variable Price details column on Membership Levels Settings Page
*
*/
function pmprovp_pmpro_membership_levels_table_extra_cols_header( $level ) {
?>
<th><?php _e( 'Variable Pricing', 'pmpro-variable-pricing' ); ?></th>
<?php
}
add_action( 'pmpro_membership_levels_table_extra_cols_header', 'pmprovp_pmpro_membership_levels_table_extra_cols_header' );
/**
* Display Variable Price level details on Membership Levels Settings Page
*
*/
function pmprovp_pmpro_membership_levels_table_extra_cols_body( $level ) { ?>
<td>
<?php
// get variable pricing info
$vpfields = pmprovp_get_settings( $level->id );
// No variable pricing? Show "--" and return.
if ( empty( $vpfields ) || empty( $vpfields['variable_pricing'] ) ) {
echo '--';
return;
}
// Get the variable pricing data from the level.
$min_price = $vpfields['min_price'];
$max_price = $vpfields['max_price'];
$suggested_price = $vpfields['suggested_price'];
// Setup price text description based on price ranges.
if ( ! empty( $max_price ) && ! empty( $min_price ) ) {
$price_text_description = sprintf(
__( 'Between %1$s and %2$s.', 'pmpro-variable-pricing' ),
esc_html( pmpro_formatPrice( $vpfields['min_price'] ) ),
esc_html( pmpro_formatPrice( $vpfields['max_price'] ) )
);
} elseif( ! empty( $min_price ) && empty( $max_price ) ) {
$price_text_description = sprintf(
__( 'Minimum price of %s or higher.', 'pmpro-variable-pricing' ),
esc_html( pmpro_formatPrice( $vpfields['min_price'] ) )
);
} elseif( ! empty( $max_price ) && empty( $min_price ) ) {
$price_text_description = sprintf(
__( '%s or lower.', 'pmpro-variable-pricing' ),
esc_html( pmpro_formatPrice( $vpfields['max_price'] ) )
);
} else {
$price_text_description = __( 'Any', 'pmpro-variable-pricing' );
}
// Show suggested price if specified.
if ( ! empty( $suggested_price ) ) {
$price_text_description .= '<br />';
$price_text_description .= sprintf(
__( 'Suggested Price: %s.', 'pmpro-variable-pricing' ),
esc_html( pmpro_formatPrice( $vpfields['suggested_price'] ) )
);
}
// Display the price text and suggested price in the row.
echo $price_text_description;
?>
</td>
<?php
}
add_action( 'pmpro_membership_levels_table_extra_cols_body', 'pmprovp_pmpro_membership_levels_table_extra_cols_body' );
// show form
function pmprovp_pmpro_checkout_after_level_cost() {
global $pmpro_level, $gateway, $pmpro_review, $pmpro_currencies, $pmpro_currency, $pmpro_currency_symbol;
// get variable pricing info
$vpfields = pmprovp_get_settings( $pmpro_level->id );
// no variable pricing? just return
if ( empty( $vpfields ) || empty( $vpfields['variable_pricing'] ) ) {
return;
}
// okay, now we're showing the form
$min_price = $vpfields['min_price'];
$max_price = $vpfields['max_price'];
$suggested_price = $vpfields['suggested_price'];
if ( isset( $_REQUEST['price'] ) ) {
$price = preg_replace( '[^0-9\.]', '', $_REQUEST['price'] );
} else {
$price = $suggested_price;
}
// setup price text description based on price ranges
if ( ! empty( $max_price ) && ! empty( $min_price ) ) {
$price_text_description = sprintf(
__( 'Enter a price between %1$s and %2$s.', 'pmpro-variable-pricing' ),
esc_html( pmpro_formatPrice( $vpfields['min_price'] ) ),
esc_html( pmpro_formatPrice( $vpfields['max_price'] ) )
);
} elseif( ! empty( $min_price ) && empty( $max_price ) ) {
$price_text_description = sprintf(
__( 'Enter a minimum price of %s or higher.', 'pmpro-variable-pricing' ),
esc_html( pmpro_formatPrice( $vpfields['min_price'] ) )
);
} elseif( ! empty( $max_price ) && empty( $min_price ) ) {
$price_text_description = sprintf(
__( 'Enter a price of %s or lower.', 'pmpro-variable-pricing' ),
esc_html( pmpro_formatPrice( $vpfields['max_price'] ) )
);
} else {
$price_text_description = __( 'Enter a price for your membership', 'pmpro-variable-pricing' );
}
/**
* @filter pmpropvp_checkout_price_description - Filter to modify the variable price description text
* @param string $price_text_description
*/
$price_text_description = apply_filters( 'pmpropvp_checkout_price_description', $price_text_description );
if ( empty( $pmpro_currencies[$pmpro_currency]['position'] ) || $pmpro_currencies[$pmpro_currency]['position'] == 'left' ) {
$price_text = sprintf(
__( 'Your price: %s', 'pmpro-variable-pricing' ),
esc_html( $pmpro_currency_symbol )
);
} else {
$price_text = __( 'Your price:', 'pmpro-variable-pricing' );
}
/**
* @filter pmprovp_checkout_price_input_label - Filter to modify the label for the Variable Price input box on the checkout page
* @param string $price_text
*/
$price_text = apply_filters( 'pmprovp_checkout_price_input_label', $price_text );
?>
<div class="pmprovp">
<p class="pmprovp_price_text_description"><?php echo esc_html( $price_text_description ); ?></p>
<p class="pmprovp_price_input"><?php echo esc_html( $price_text ); ?> <input type="text" id="price" name="price" size="10" value="<?php esc_attr_e( $price ); ?>" style="width:auto;" <?php if( $pmpro_review ) { ?> readonly <?php } ?>/> <?php if ( !empty( $pmpro_currencies[$pmpro_currency]['position'] ) && $pmpro_currencies[$pmpro_currency]['position'] == 'right' ) { echo $pmpro_currency_symbol; } ?>
<span id="pmprovp-warning" class="pmpro_message pmpro_alert" style="display:none;"><small><?php echo $price_text_description; ?></small></span></p>
</div> <!-- end .pmprovp -->
<?php
}
add_action( 'pmpro_checkout_after_level_cost', 'pmprovp_pmpro_checkout_after_level_cost' );
// set price
function pmprovp_pmpro_checkout_level( $level ) {
// Get variable pricing info.
$vpfields = pmprovp_get_settings( $level->id );
// Make sure level has variable pricing.
if ( empty( $vpfields ) || empty( $vpfields['variable_pricing'] ) ) {
return $level;
}
if ( isset( $_REQUEST['price'] ) ) {
$price = preg_replace( '[^0-9\.\,]', '', $_REQUEST['price'] );
}
if ( isset( $price ) ) {
$level->initial_payment = $price;
if ( $level->billing_amount > 0 ) {
$level->billing_amount = $price;
}
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'pmprovp_pmpro_checkout_level' );
/**
* Check if price is between min and max value.
* If no max value is set, set it to unlimited.
*/
function pmprovp_pmpro_registration_checks( $continue ) {
// only bother if we are continuing already
if ( $continue ) {
global $pmpro_currency_symbol, $pmpro_msg, $pmpro_msgt;
// was a price passed in?
if ( isset( $_REQUEST['price'] ) ) {
// get values
$level_id = intval( $_REQUEST['level'] );
$vpfields = pmprovp_get_settings( $level_id );
// Bail if the Variable Pricing is not set for this level.
if( empty( $vpfields['variable_pricing'] ) ){
return $continue;
}
// get price
$price = preg_replace( '[^0-9\.]', '', $_REQUEST['price'] );
// check that the price falls between the min and max
if ( (double) $price < (double) $vpfields['min_price'] ) {
$pmpro_msg = sprintf(
__( 'The lowest accepted price is %1$s%2$s. Please enter a new amount.', 'pmpro-variable-procing' ),
esc_html( $pmpro_currency_symbol ),
esc_html( $vpfields['min_price'] )
);
$pmpro_msgt = 'pmpro_error';
$continue = false;
} elseif ( ! empty( $vpfields['max_price'] ) && ( (double) $price > (double) $vpfields['max_price'] ) ) {
$pmpro_msg = sprintf(
__( 'The highest accepted price is %1$s%2$s. Please enter a new amount.', 'pmpro-variable-pricing' ),
esc_html( $pmpro_currency_symbol ),
esc_html( $vpfields['max_price'] )
);
$pmpro_msgt = 'pmpro_error';
$continue = false;
}
// all good!
}
}
return $continue;
}
add_filter( 'pmpro_registration_checks', 'pmprovp_pmpro_registration_checks' );
// save fields in session for PayPal Express/etc
function pmprovp_pmpro_paypalexpress_session_vars() {
if ( ! empty( $_REQUEST['price'] ) ) {
$_SESSION['price'] = $_REQUEST['price'];
} else {
$_SESSION['price'] = '';
}
}
add_action( 'pmpro_paypalexpress_session_vars', 'pmprovp_pmpro_paypalexpress_session_vars' );
add_action( 'pmpro_before_send_to_twocheckout', 'pmprovp_pmpro_paypalexpress_session_vars', 10, 2 );
// Load fields from session if available.
function pmprovp_init_load_session_vars() {
if(function_exists('pmpro_start_session')) {
pmpro_start_session();
}
if ( empty( $_REQUEST['price'] ) && ! empty( $_SESSION['price'] ) ) {
$_REQUEST['price'] = $_SESSION['price'];
}
}
add_action( 'init', 'pmprovp_init_load_session_vars', 5 );
/**
* Register and set variables for JavaScript
*/
function pmprovp_load_scripts() {
global $gateway, $pmpro_level;
if ( empty( $pmpro_level ) ) {
return;
}
// get variable pricing info
$vpfields = pmprovp_get_settings( $pmpro_level->id );
// no variable pricing? just return
if ( empty( $vpfields ) || empty( $vpfields['variable_pricing'] ) ) {
return;
}
// Bail if PMPro is not loaded.
if ( ! function_exists( 'pmpro_getOption' ) ) {
return;
}
if ( empty( $gateway ) ) {
$gateway = pmpro_getOption( 'gateway' );
}
wp_register_script( 'pmprovp', plugins_url( 'javascript/pmpro-variable-pricing.js', __FILE__ ), array( 'jquery' ), '0.4', true );
wp_localize_script(
'pmprovp', 'pmprovp', array(
'settings' => array(
'gateway' => pmpro_getGateway(),
'gateway_billing' => ( in_array( $gateway, array( 'paypalexpress', 'twocheckout' ) ) !== false ) ? 'false' : 'true',
),
'pricing_billing' => ! pmpro_isLevelFree( $pmpro_level ) ? 'true' : 'false',
'vp_data' => wp_json_encode( $vpfields )
)
);
}
add_action( 'wp_enqueue_scripts', 'pmprovp_load_scripts', 5 );
/**
* Split register/localize and enqueue operation to simplify unhooking JS from plugin if needed
*/
function pmprovp_enqueue_scripts() {
global $pmpro_level;
if ( empty( $pmpro_level ) ) {
return;
}
// get variable pricing info
$vpfields = pmprovp_get_settings( $pmpro_level->id );
// no variable pricing? just return
if ( empty( $vpfields ) || empty( $vpfields['variable_pricing'] ) ) {
return;
}
wp_enqueue_script( 'pmprovp' );
}
add_action( 'wp_enqueue_scripts', 'pmprovp_enqueue_scripts', 15 );
/*
Function to add links to the plugin row meta
*/
function pmprovp_plugin_row_meta( $links, $file ) {
if ( strpos( $file, 'pmpro-variable-pricing.php' ) !== false ) {
$new_links = array(
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/add-ons/variable-pricing-add-on/' ) . '" title="' . esc_attr( __( 'View Documentation', 'pmpro' ) ) . '">' . __( 'Docs', 'paid-memberships-pro' ) . '</a>',
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/support/' ) . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'paid-memberships-pro' ) ) . '">' . __( 'Support', 'paid-memberships-pro' ) . '</a>',
);
$links = array_merge( $links, $new_links );
}
return $links;
}
add_filter( 'plugin_row_meta', 'pmprovp_plugin_row_meta', 10, 2 );