Guest User

Untitled

a guest
Apr 24th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. add_action( "pmpro_after_change_membership_level", "e20r_cancel_at_end_of_period", 10, 2 );
  2. add_filter( "pmpro_email_body", "e20r_email_body_for_cancel_msg", 10, 2 );
  3.  
  4. /**
  5. * Change cancellation to set expiration date for next payment instead of cancelling immediately.
  6. * Assumes monthly membership levels.
  7. * Assumes orders are generated for each payment (i.e. your webhooks/etc are setup correctly).
  8. *
  9. * @param (int) $level_id -- The membership levelId we're changing
  10. * @param (int) $user_id -- The member we're changing levels for.
  11. */
  12. function e20r_cancel_at_end_of_period( $level_id, $user_id ) {
  13.  
  14. global $pmpro_pages, $wpdb;
  15.  
  16. // Make sure we're on the 'cancel' page (for PMPro).
  17.  
  18. if ( is_page( $pmpro_pages['cancel'] ) ) {
  19.  
  20. /*
  21. * okay, let's give the user his old level back with an expiration based on his subscription date
  22. */
  23.  
  24. /* Fetch the last order */
  25. $order = new MemberOrder();
  26. $order->getLastMemberOrder( $user_id, "cancelled" );
  27.  
  28. // Fetch the levelId representing their most recent (old) membership level.
  29. $level = $wpdb->get_row( $wpdb->prepare("
  30. SELECT *
  31. FROM {$wpdb->pmpro_memberships_users}
  32. WHERE membership_id = %d AND user_id = %d
  33. ORDER BY id DESC
  34. LIMIT 1
  35. ",
  36. $order->membership_id,
  37. $user_id )
  38. );
  39.  
  40. // Not allowed to do this unless there's both a levelId and an order located in the system
  41. // This is probably wrong since $order rarely (never?) is empty.
  42. if ( ( ! isset( $order->user_id ) ) || empty( $level ) ) {
  43. return false;
  44. }
  45.  
  46. if ( empty( $level->cylcle_number ) ) {
  47. return false;
  48. }
  49.  
  50. // Calculate the "last payment date" (using "today").
  51. $lastdate = date("Y-m-d", $order->timestamp);
  52.  
  53. //Calculating the "next scheduled date"
  54. $nextdate = $wpdb->get_var( $wpdb->prepare("
  55. SELECT UNIX_TIMESTAMP(%s + INTERVAL %d {$level->cycle_period})
  56. ",
  57. $lastdate,
  58. $level->cycle_number
  59. )
  60. );
  61.  
  62. //if the date in the future?
  63. if ( $nextdate - current_time('timestamp') > 0 ) {
  64.  
  65. // Set their level to the one they're currently at, but leave the expiration date set to their next billing date.
  66. $sql = $wpdb->prepare("
  67. SELECT *
  68. FROM $wpdb->pmpro_memberships_users
  69. WHERE ( membership_id = %d ) AND ( user_id = %d )
  70. ORDER BY id
  71. DESC LIMIT 1
  72. ",
  73. $order->membership_id,
  74. $user_id
  75. );
  76.  
  77. $old_level = $wpdb->get_row( $sql, ARRAY_A );
  78.  
  79. $old_level['enddate'] = date( "Y-m-d H:i:s", $nextdate );
  80.  
  81. //disable this hook so we don't loop
  82. remove_action( "pmpro_after_change_membership_level", "e20r_cancel_at_end_of_period", 10, 2 );
  83. remove_action( "pmpro_after_change_membership_level", "e20r_membership_level_config", 10, 2 );
  84. remove_action( "pmpro_after_change_membership_level", "e20r_clear_upcoming_appointments", 11, 2 );
  85. remove_action( "pmpro_after_change_membership_level", "e20r_after_change_membership_level", 10, 2);
  86.  
  87. //change level
  88. pmpro_changeMembershipLevel( $old_level, $user_id );
  89.  
  90. //add the action back just in case
  91. add_action( "pmpro_after_change_membership_level", "e20r_cancel_at_end_of_period", 10, 2 );
  92. add_action( "pmpro_after_change_membership_level", "e20r_membership_level_config", 10, 2 );
  93. add_action( "pmpro_after_change_membership_level", "e20r_clear_upcoming_appointments", 11, 2 );
  94. add_action( "pmpro_after_change_membership_level", "e20r_after_change_membership_level", 10, 2);
  95.  
  96. //change message shown on cancel page
  97. add_filter( "gettext", "e20r_gettext_cancel_text", 10, 3 );
  98. }
  99. }
  100. }
  101.  
  102. /**
  103. * @param $translated_text -- Text to replace/update
  104. * @param $text -- The text
  105. * @param $domain
  106. *
  107. * @return string
  108. */
  109. function e20r_gettext_cancel_text( $translated_text, $text, $domain ) {
  110.  
  111. if ( ( $domain == "pmpro" ) &&
  112. ( $text == "Your membership has been cancelled." ) ) {
  113.  
  114. global $current_user;
  115.  
  116. $translated_text = "Your membership has been cancelled. Your access to schedule and join new sessions will expire on " .
  117. date_i18n( get_option( "date_format" ), pmpro_next_payment( $current_user->ID, "cancelled" ) ) .
  118. ", but you may schedule new sessions until then. Once your membership expires, any scheduled sessions " .
  119. "will be removed automatically.";
  120. }
  121.  
  122. return $translated_text;
  123. }
  124.  
  125. /**
  126. * If the user cancels, send a message about when the cancellation will actually occur.
  127. *
  128. * @param (string) $body - The content of the message (we'll append the cancellation date to it)
  129. * @param (object) $email -- The email object.
  130. *
  131. * @return string -- The new body of the email message.
  132. */
  133. function e20r_email_body_for_cancel_msg( $body, $email ) {
  134.  
  135. if ( $email->template == "cancel" ) {
  136.  
  137. global $wpdb;
  138.  
  139. $user_id = $wpdb->get_var("
  140. SELECT ID
  141. FROM $wpdb->users
  142. WHERE user_email = '" . esc_sql( $email->email ) . "' LIMIT 1"
  143. );
  144.  
  145. if( ! empty( $user_id ) ) {
  146.  
  147. $expiration_date = pmpro_next_payment( $user_id );
  148.  
  149. // Only update the body of the message if the date is in the future.
  150. if ( $expiration_date - current_time('timestamp') > 0 ) {
  151.  
  152. $body .= "<p>Your access will expire on " . date( get_option( "date_format" ), $expiration_date ) . ".</p>";
  153. }
  154. }
  155. }
  156.  
  157. return $body;
  158. }
Add Comment
Please, Sign In to add comment