Advertisement
siamshogun

PMPro Cancel Next Payment Date

Nov 23rd, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: PMPro Cancel Next Payment Date
  4. Plugin URI:
  5. Description: Extends users membership to next expiration date before cancelling.
  6. Version:
  7. Author:
  8. Author URI:
  9. License:
  10. License URI:
  11. /*
  12. Change cancellation to set expiration date for next payment instead of cancelling immediately.
  13.  
  14. Assumes monthly membership levels.
  15.  
  16. Assumes orders are generated for each payment (i.e. your webhooks/etc are setup correctly).
  17. */
  18. //give users their level back with an expiration
  19. function my_pmpro_after_change_membership_level($level_id, $user_id)
  20. {
  21. //are we on the cancel page?
  22. global $pmpro_pages, $wpdb;
  23. if(is_page($pmpro_pages['cancel']) || ( (!empty($_POST['txn_type']) && in_array($_POST['txn_type'], array('recurring_payment_profile_cancel', 'subscr_cancel')))))
  24. {
  25. /*
  26. okay, let's give the user his old level back with an expiration based on his subscription date
  27. */
  28. //get last order
  29. $order = new MemberOrder();
  30. $order->getLastMemberOrder($user_id, "cancelled");
  31.  
  32. //get the last level they had
  33. $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");
  34.  
  35. //can't do this unless we find an order and level
  36. if(empty($order) || empty($level))
  37. return false;
  38.  
  39. //last payment date
  40. $lastdate = date("Y-m-d", $order->timestamp);
  41.  
  42. //next payment date
  43. if(function_exists('pmpro_getSetExpirationDate')) {
  44.  
  45. //check for a subscription delay first
  46. $trial_days = pmpro_getSetExpirationDate($level->id);
  47. if(!empty($trial_days)) {
  48.  
  49. //check last payment date to see if we're still in the trial
  50. if(current_time('timestamp') > strtotime('+ ' . $trial_days . ' days', $lastdate))
  51. $nextdate = strtotime('+ ' . $trial_days . ' days', $lastdate);
  52. }
  53. }
  54. else
  55. $nextdate = $wpdb->get_var("SELECT UNIX_TIMESTAMP('" . $lastdate . "' + INTERVAL " . $level->cycle_number . " " . $level->cycle_period . ")");
  56.  
  57. //if the date in the future?
  58. if($nextdate - time() > 0)
  59. {
  60. //give them their level back with the expiration date set
  61. $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);
  62. $old_level['enddate'] = date("Y-m-d H:i:s", $nextdate);
  63.  
  64. //disable this hook so we don't loop
  65. remove_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
  66.  
  67. //change level
  68. pmpro_changeMembershipLevel($old_level, $user_id);
  69.  
  70. //add the action back just in case
  71. add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
  72.  
  73. //change message shown on cancel page
  74. add_filter("gettext", "my_gettext_cancel_text", 10, 3);
  75. }
  76. }
  77. }
  78. add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2);
  79.  
  80. //this replaces the cancellation text so people know they'll still have access for a certain amount of time
  81. function my_gettext_cancel_text($translated_text, $text, $domain)
  82. {
  83. if($domain == "pmpro" && $text == "Your membership has been cancelled.")
  84. {
  85. global $current_user;
  86. $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")) . ".";
  87. }
  88.  
  89. return $translated_text;
  90. }
  91.  
  92. //want to update the cancellation email as well
  93. function my_pmpro_email_body($body, $email)
  94. {
  95. if($email->template == "cancel")
  96. {
  97. global $wpdb;
  98. $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . esc_sql($email->email) . "' LIMIT 1");
  99. if(!empty($user_id))
  100. {
  101. $expiration_date = pmpro_next_payment($user_id);
  102.  
  103. //if the date in the future?
  104. if($expiration_date - time() > 0)
  105. {
  106. $body .= "<p>Your access will expire on " . date(get_option("date_format"), $expiration_date) . ".</p>";
  107. }
  108. }
  109. }
  110.  
  111. return $body;
  112. }
  113. add_filter("pmpro_email_body", "my_pmpro_email_body", 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement