forked from strangerstudios/pmpro-set-expiration-dates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmpro-set-expiration-dates.php
executable file
·423 lines (357 loc) · 15.2 KB
/
pmpro-set-expiration-dates.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
<?php
/*
Plugin Name: Paid Memberships Pro - Set Expiration Dates Add On
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-set-expiration-dates/
Description: Set a specific expiration date (e.g. 2013-12-31) for a PMPro membership level or discount code.
Version: .5.1
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
Text Domain: pmpro-set-expiration-dates
Domain Path: /languages
*/
/* Load text domain */
function pmprosed_load_plugin_text_domain() {
load_plugin_textdomain( 'pmpro-set-expiration-dates', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'pmprosed_load_plugin_text_domain' );
/*
This first set of functions adds our fields to the edit membership levels page
*/
//add level cost text field to level price settings
function pmprosed_pmpro_membership_level_after_other_settings()
{
$level_id = intval($_REQUEST['edit']);
if ($level_id > 0)
$set_expiration_date = pmpro_getSetExpirationDate($level_id);
else
$set_expiration_date = "";
?>
<h3 class="topborder"><?php esc_html_e( 'Set Expiration Date', 'pmpro-set-expiration-dates' ); ?></h3>
<p><?php _e( 'To have this level expire on a specific date, enter it below in YYYY-MM-DD format', 'pmpro-set-expiration-dates' ); ?>. <strong><?php _e( 'Note:', 'pmpro-set-expiration-dates' ); ?></strong> <?php _e( 'You must also set an expiration date above (e.g. 1 Year) which will be overwritten by the value below.', 'pmpro-set-expiration-dates' ); ?></p>
<table>
<tbody class="form-table">
<tr>
<th scope="row" valign="top"><label for="set_expiration_date"><?php esc_html_e( 'Expiration Date', 'pmpro-set-expiration-dates' ); ?>:</label></th>
<td>
<input type="text" name="set_expiration_date" value="<?php echo esc_attr($set_expiration_date); ?>"/>
<br/>
<small><?php esc_html_e( 'YYYY-MM-DD format. Enter "Y" for current year, "Y2" for next year. M, M2 for current/next month.', 'pmpro-set-expiration-dates' ); ?>
</small>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action("pmpro_membership_level_after_other_settings", "pmprosed_pmpro_membership_level_after_other_settings");
//save level cost text when the level is saved/added
function pmprosed_pmpro_save_membership_level($level_id)
{
pmpro_saveSetExpirationDate($level_id, $_REQUEST['set_expiration_date']); //add level cost text for this level
}
add_action("pmpro_save_membership_level", "pmprosed_pmpro_save_membership_level");
/*
Function to replace Y and M/etc with actual dates
*/
function pmprosed_fixDate($set_date, $current_date = null)
{
// handle lower-cased y/m values.
$set_date = apply_filters( 'pmprosed_expiration_date_raw', strtoupper($set_date) );
// Change "M-" and "Y-" to "M1-" and "Y1-".
$set_date = preg_replace('/Y-/', 'Y1-', $set_date);
$set_date = preg_replace('/M-/', 'M1-', $set_date);
// Get number of months and years to add.
$m_pos = stripos( $set_date, 'M' );
$y_pos = stripos( $set_date, 'Y' );
if($m_pos !== false) {
$add_months = intval( pmpro_getMatches( '/M([0-9]*)/', $set_date, true ) );
}
if($y_pos !== false) {
$add_years = intval( pmpro_getMatches( '/Y([0-9]*)/', $set_date, true ) );
}
// Allow new dates to be set from a custom date.
if(empty($current_date)) $current_date = current_time( 'timestamp' );
// Get current date parts.
$current_y = intval(date('Y', $current_date));
$current_m = intval(date('m', $current_date));
$current_d = intval(date('d', $current_date));
// Get set date parts.
$date_parts = explode( '-', $set_date);
$set_y = intval($date_parts[0]);
$set_m = intval($date_parts[1]);
$set_d = intval($date_parts[2]);
// Get temporary date parts.
$temp_y = $set_y > 0 ? $set_y : $current_y;
$temp_m = $set_m > 0 ? $set_m : $current_m;
$temp_d = $set_d;
// Add months.
if(!empty($add_months)) {
for($i = 0; $i < $add_months; $i++) {
// If "M1", only add months if current date of month has already passed.
if(0 == $i) {
if($temp_d < $current_d) {
$temp_m++;
$add_months--;
}
} else {
$temp_m++;
}
// If we hit 13, reset to Jan of next year and subtract one of the years to add.
if($temp_m == 13) {
$temp_m = 1;
$temp_y++;
$add_years--;
}
}
}
// Add years.
if(!empty($add_years)) {
for($i = 0; $i < $add_years; $i++) {
// If "Y1", only add years if current date has already passed.
if(0 == $i) {
$temp_date = strtotime(date("{$temp_y}-{$temp_m}-{$temp_d}"));
if($temp_date < $current_date) {
$temp_y++;
$add_years--;
}
} else {
$temp_y++;
}
}
}
// Pad dates if necessary.
$temp_m = str_pad($temp_m, 2, '0', STR_PAD_LEFT);
$temp_d = str_pad($temp_d, 2, '0', STR_PAD_LEFT);
// Put it all together.
$set_date = date( "{$temp_y}-{$temp_m}-{$temp_d}" );
// Make sure we use the right day of the month for dates > 28
// From: http://stackoverflow.com/a/654378/1154321
$dotm = pmpro_getMatches('/\-([0-3][0-9]$)/', $set_date, true);
if ( $temp_m == '02' && intval($dotm) > 28 || intval($dotm) > 30 ) {
$set_date = date('Y-m-t', strtotime(substr($set_date, 0, 8) . "01"));
}
return apply_filters( 'pmprosed_expiration_date', $set_date );
}
/*
Update expiration date of level at checkout.
*/
function pmprosed_pmpro_checkout_level($level, $discount_code_id = null)
{
global $wpdb;
//in case the $level object has been cleared out already/etc
if(empty($level) || empty($level->id))
return $level;
if (empty($discount_code_id) && !empty($_REQUEST['discount_code'])) {
//get discount code passed in
$discount_code = preg_replace("/[^A-Za-z0-9\-]/", "", $_REQUEST['discount_code']);
if (!empty($discount_code)) {
$discount_code_id = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($discount_code) . "' LIMIT 1");
} else {
$discount_code_id = NULL;
}
}
//does this level have a set expiration date?
$set_expiration_date = pmpro_getSetExpirationDate($level->id, $discount_code_id);
//check for Y
if (strpos($set_expiration_date, "Y") !== false) {
$used_y = true;
}
if (!empty($set_expiration_date)) {
//replace vars
$set_expiration_date = pmprosed_fixDate($set_expiration_date);
//how many days until expiration
$todays_date = time();
$time_left = strtotime($set_expiration_date) - $todays_date;
if ($time_left > 0) {
$days_left = ceil($time_left / (60 * 60 * 24));
//update number and period
$level->expiration_number = $days_left;
$level->expiration_period = "Day";
return $level; //stop
} elseif (!empty($used_y)) {
$timestamp = strtotime($set_expiration_date);
//add one year to expiration date
$set_expiration_date = date("Y-m-d", mktime(0, 0, 0, date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp) + 1));
//how many days until expiration
$time_left = strtotime($set_expiration_date) - $todays_date;
$days_left = ceil($time_left / (60 * 60 * 24));
//update number and period
$level->expiration_number = $days_left;
$level->expiration_period = "Day";
return $level; //stop
} else {
//expiration already here, don't let people signup
$level = NULL;
return $level; //stop
}
}
return $level; //no change
}
add_filter("pmpro_checkout_level", "pmprosed_pmpro_checkout_level");
add_filter('pmpro_discount_code_level', 'pmprosed_pmpro_checkout_level', 10, 2);
/**
* Wrapper function for pmpro_ipnhandler_level to ensure that the right arguments are passed through.
* @since 0.6
*/
function pmprosed_pmpro_ipnhandler_level( $level, $user_id = null) {
return pmprosed_pmpro_checkout_level( $level_id, null );
}
add_filter( 'pmpro_ipnhandler_level', 'pmprosed_pmpro_ipnhandler_level', 10, 2 );
/*
This function will save a the set expiration dates into wp_options.
*/
function pmpro_saveSetExpirationDate($level_id, $set_expiration_date, $code_id = NULL)
{
if ($code_id) {
$key = "pmprosed_" . $level_id . "_" . $code_id;
} else {
$key = "pmprosed_" . $level_id;
}
update_option( $key, $set_expiration_date, false );
}
/*
This function will return the expiration date for a level or discount code/level combo
*/
function pmpro_getSetExpirationDate($level_id, $code_id = NULL)
{
if ($code_id) {
$key = "pmprosed_" . $level_id . "_" . $code_id;
} else {
$key = "pmprosed_" . $level_id;
}
return get_option($key, "");
}
/*
This next set of functions adds our field to the edit discount code page
*/
//add our field to level price settings
function pmprosed_pmpro_discount_code_after_level_settings($code_id, $level)
{
$set_expiration_date = pmpro_getSetExpirationDate($level->id, $code_id);
?>
<table>
<tbody class="form-table">
<tr>
<td>
<tr>
<th scope="row" valign="top"><label for="set_expiration_date"><?php esc_html_e( 'Expiration Date', 'pmpro-set-expiration-dates' ); ?>:</label></th>
<td>
<input type="text" name="set_expiration_date[]" value="<?php echo esc_attr($set_expiration_date); ?>"/>
<br/>
<small><?php esc_html_e( 'YYYY-MM-DD format. Enter "Y" for current year, "Y2" for next year. M, M2 for current/next month. Be sure to set an expiration date above as well.', 'pmpro-set-expiration-dates' ); ?>
</small>
</td>
</tr>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action("pmpro_discount_code_after_level_settings", "pmprosed_pmpro_discount_code_after_level_settings", 10, 2);
//save level cost text for the code when the code is saved/added
function pmprosed_pmpro_save_discount_code_level($code_id, $level_id)
{
$all_levels_a = $_REQUEST['all_levels']; //array of level ids checked for this code
$set_expiration_date_a = $_REQUEST['set_expiration_date']; //expiration dates for levels checked
if (!empty($all_levels_a)) {
$key = array_search($level_id, $all_levels_a); //which level is it in the list?
pmpro_saveSetExpirationDate($level_id, $set_expiration_date_a[$key], $code_id);
}
}
add_action("pmpro_save_discount_code_level", "pmprosed_pmpro_save_discount_code_level", 10, 2);
/**
* Show Set Expiration Dates on the level's list table.
* @since 0.6
*/
function pmprosed_membership_levels_table_extra_cols_header( $levels ) {
echo "<th>" . esc_html__( 'Set Expiration Date', 'pmpro-set-expiration-dates' ). "</th>";
}
add_action( 'pmpro_membership_levels_table_extra_cols_header', 'pmprosed_membership_levels_table_extra_cols_header', 15, 1 );
/**
* Show expiration date setting on levels list table.
* @since 0.6
*/
function pmprosed_membership_levels_table_extra_cols_body( $levels ) {
$expiration_date = pmpro_getSetExpirationDate( $levels->id, '' );
if ( ! empty( $expiration_date ) ) {
echo '<td>' . esc_html( $expiration_date ) . '</td>';
} else {
echo '<td>--</td>';
}
}
add_action( 'pmpro_membership_levels_table_extra_cols_body', 'pmprosed_membership_levels_table_extra_cols_body', 15, 1 );
/**
* Function to check if any levels have a past date and show a warning.
* @since 0.6
*/
function pmprosed_show_admin_notice_past_dates() {
global $msg, $msgt;
// get all levels
$levels = pmpro_getAllLevels(true,false);
$is_past = false;
$problem_levels = array();
foreach( $levels as $level ) {
if ( $level->allow_signups && pmprosed_is_past_date( $level->id ) ) {
$is_past = true;
$problem_levels[$level->id] = $level->id;
}
}
// Get all levels and their dates.
if ( $is_past ) {
$levels = implode(', ', $problem_levels );
$msg = -1;
$msgt = sprintf( __( "Warning: The following level(s) have a past expiration date: <strong>ID's - %s</strong>.", 'pmpro-set-expiration-dates' ), $levels );
}
}
if ( isset( $_REQUEST['page'] ) && 'pmpro-membershiplevels' == $_REQUEST['page'] && ! isset( $_REQUEST['edit'] ) ) {
add_action( 'admin_notices', 'pmprosed_show_admin_notice_past_dates' );
}
/**
* Get to see if a level's date is in the past
* @return bool if the date is in the past or not.
* @since 0.6
*/
function pmprosed_is_past_date( $level_id ) {
// Convert the date from level
$levels_date = pmpro_getSetExpirationDate( $level_id );
// if there's no expiration date just bail.
if ( empty( $levels_date ) ) {
return false;
}
$r = false;
$today = date('Y-m-d');
$levels_date = pmprosed_fixDate( $levels_date );
if ( $levels_date < $today ) {
$r = true;
}
return $r;
}
/*
Function to add links to the plugin row meta
*/
function pmprosed_plugin_row_meta($links, $file)
{
if (strpos($file, 'pmpro-set-expiration-dates.php') !== false) {
$set_links = array(
'<a href="' . esc_url('http://www.paidmembershipspro.com/add-ons/plugins-on-github/pmpro-expiration-date/') . '" title="' . esc_attr(__('View Documentation', 'pmpro-set-expiration-dates')) . '">' . esc_html__('Docs', 'pmpro-set-expiration-dates') . '</a>',
'<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr(__('Visit Customer Support Forum', 'pmpro-set-expiration-dates')) . '">' . esc_html__('Support', 'pmpro-set-expiration-dates') . '</a>',
);
$links = array_merge($links, $set_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmprosed_plugin_row_meta', 10, 2);
/*
Update expiration text on levels page.
*/
function pmprosed_pmpro_level_expiration_text($expiration_text, $level)
{
$set_expiration_date = !empty( $level ) ? pmpro_getSetExpirationDate($level->id) : null;
if (!empty($set_expiration_date)) {
$set_expiration_date = pmprosed_fixDate($set_expiration_date);
$expiration_text = esc_html__( 'Membership expires on', 'pmpro-set-expiration-dates' ) . ' ' . date_i18n(get_option('date_format'), strtotime($set_expiration_date, current_time('timestamp'))) . ".";
}
return $expiration_text;
}
add_filter('pmpro_level_expiration_text', 'pmprosed_pmpro_level_expiration_text', 10, 2);