Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: PMPro Cancel Next Payment Date
- Plugin URI:
- Description: Extends users membership to next expiration date before cancelling.
- Version:
- Author:
- Author URI:
- License:
- License URI:
- /*
- Change cancellation to set expiration date for next payment instead of cancelling immediately.
- Assumes monthly membership levels.
- Assumes orders are generated for each payment (i.e. your webhooks/etc are setup correctly).
- */
- //give users their level back with an expiration
- function my_pmpro_after_change_membership_level($level_id, $user_id)
- {
- //are we on the cancel page?
- global $pmpro_pages, $wpdb;
- if(is_page($pmpro_pages['cancel']) || ( (!empty($_POST['txn_type']) && in_array($_POST['txn_type'], array('recurring_payment_profile_cancel', 'subscr_cancel')))))
- {
- /*
- okay, let's give the user his old level back with an expiration based on his subscription date
- */
- //get last order
- $order = new MemberOrder();
- $order->getLastMemberOrder($user_id, "cancelled");
- //get the last level they had
- $level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = '" . $order->membership_id . "' AND user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1");
- //can't do this unless we find an order and level
- if(empty($order) || empty($level))
- return false;
- //last payment date
- $lastdate = date("Y-m-d", $order->timestamp);
- //next payment date
- if(function_exists('pmpro_getSetExpirationDate')) {
- //check for a subscription delay first
- $trial_days = pmpro_getSetExpirationDate($level->id);
- if(!empty($trial_days)) {
- //check last payment date to see if we're still in the trial
- if(current_time('timestamp') > strtotime('+ ' . $trial_days . ' days', $lastdate))
- $nextdate = strtotime('+ ' . $trial_days . ' days', $lastdate);
- }
- }
- else
- $nextdate = $wpdb->get_var("SELECT UNIX_TIMESTAMP('" . $lastdate . "' + INTERVAL " . $level->cycle_number . " " . $level->cycle_period . ")");
- //if the date in the future?
- if($nextdate - time() > 0)
- {
- //give them their level back with the expiration date set
- $old_level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = '" . $order->membership_id . "' AND user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1", ARRAY_A);
- $old_level['enddate'] = date("Y-m-d H:i:s", $nextdate);
- //disable this hook so we don't loop
- remove_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
- //change level
- pmpro_changeMembershipLevel($old_level, $user_id);
- //add the action back just in case
- add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
- //change message shown on cancel page
- add_filter("gettext", "my_gettext_cancel_text", 10, 3);
- }
- }
- }
- add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
- //this replaces the cancellation text so people know they'll still have access for a certain amount of time
- function my_gettext_cancel_text($translated_text, $text, $domain)
- {
- if($domain == "pmpro" && $text == "Your membership has been cancelled.")
- {
- global $current_user;
- $translated_text = "Your recurring subscription has been cancelled. Your active membership will expire on " . date(get_option("date_format"), pmpro_next_payment($current_user->ID, "cancelled")) . ".";
- }
- return $translated_text;
- }
- //want to update the cancellation email as well
- function my_pmpro_email_body($body, $email)
- {
- if($email->template == "cancel")
- {
- global $wpdb;
- $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . esc_sql($email->email) . "' LIMIT 1");
- if(!empty($user_id))
- {
- $expiration_date = pmpro_next_payment($user_id);
- //if the date in the future?
- if($expiration_date - time() > 0)
- {
- $body .= "<p>Your access will expire on " . date(get_option("date_format"), $expiration_date) . ".</p>";
- }
- }
- }
- return $body;
- }
- add_filter("pmpro_email_body", "my_pmpro_email_body", 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement