pmpro_changeMembershipLevel

Create, add, remove or updates the membership level of the given user to the given level.

Description

pmpro_changeMembershipLevel( (int) $level, (constant) $user_id = NULL, (string) $old_level_status = 'inactive', (constant) $cancel_level = NULL ); 

$level may either be the ID or name of the desired membership_level. If $user_id is omitted, the value will be retrieved from $current_user.

Parameters (4)

0. $level (int)
ID of level to set as new level, use 0 to cancel membership
1. $user_id — Optional. (constant) => NULL
ID of the user to change levels for
2. $old_level_status — Optional. (string) => 'inactive'
The status to set for the row in the memberships users table. (e.g. inactive, cancelled, admin_cancelled, expired) Defaults to inactive.. $param int $cancel_level If set cancel just this one level instead of all active levels (to support Multiple Memberships per User) Return values: Success returns boolean true. Failure returns boolean false.
3. $cancel_level — Optional. (constant) => NULL
The cancel level.

Usage

  1. if ( !function_exists( 'pmpro_changeMembershipLevel' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/functions.php'; 
  3.  
  4. // ID of level to set as new level, use 0 to cancel membership 
  5. $level = -1; 
  6.  
  7. // ID of the user to change levels for 
  8. $user_id = NULL; 
  9. $old_level_status = 'inactive'; 
  10.  
  11. // The cancel level. 
  12. $cancel_level = NULL; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = pmpro_changeMembershipLevel($level, $user_id, $old_level_status, $cancel_level); 
  16.  

Defined (1)

The function is defined in the following location(s).

/includes/functions.php  
  1. function pmpro_changeMembershipLevel($level, $user_id = NULL, $old_level_status = 'inactive', $cancel_level = NULL) 
  2. global $wpdb; 
  3. global $current_user, $pmpro_error; 
  4.  
  5. if(empty($user_id)) 
  6. $user_id = $current_user->ID; 
  7.  
  8. if(empty($user_id)) 
  9. $pmpro_error = __("User ID not found.", 'paid-memberships-pro' ); 
  10. return false; 
  11.  
  12. //make sure user id is int for security 
  13. $user_id = intval($user_id); 
  14.  
  15. if(empty($level)) //cancelling membership 
  16. $level = 0; 
  17. else if(is_array($level)) 
  18. //custom level 
  19. else 
  20. $level_obj = pmpro_getLevel($level); 
  21. if(empty($level_obj)) 
  22. $pmpro_error = __("Invalid level.", 'paid-memberships-pro' ); 
  23. return false; 
  24. $level = $level_obj->id; 
  25.  
  26. //if it's a custom level, they're changing 
  27. if(!is_array($level)) 
  28. //are they even changing? 
  29. if(pmpro_hasMembershipLevel($level, $user_id)) { 
  30. $pmpro_error = __("not changing?", 'paid-memberships-pro' ); 
  31. return false; //not changing 
  32.  
  33. //get all active membershipships for this user 
  34. $old_levels = pmpro_getMembershipLevelsForUser($user_id); 
  35.  
  36.  
  37. //get level id 
  38. if(is_array($level)) 
  39. $level_id = $level['membership_id']; //custom level 
  40. else 
  41. $level_id = $level; //just id 
  42.  
  43. /** 
  44. * Action to run before the membership level changes. 
  45. * @param int $level_id ID of the level changed to. 
  46. * @param int $user_id ID of the user changed. 
  47. * @param array $old_levels array of prior levels the user belonged to. 
  48. * $param int $cancel_level ID of the level being cancelled if specified 
  49. */ 
  50. do_action("pmpro_before_change_membership_level", $level_id, $user_id, $old_levels, $cancel_level); 
  51.  
  52.  
  53. //deactivate old memberships based on the old_level_status passed in (updates pmpro_memberships_users table) 
  54. $pmpro_deactivate_old_levels = true; 
  55. /** 
  56. * Filter whether old levels should be deactivated or not. This supports the MMPU addon. 
  57. * Typically you'll want to hook into pmpro_before_change_membership_level  
  58. * or pmpro_after_change_membership_level later to run your own deactivation logic. 
  59. *  
  60. * @since 1.8.11 
  61. * @var $pmpro_deactivate_old_levels bool True or false if levels should be deactivated. Defaults to true. 
  62. */ 
  63. $pmpro_deactivate_old_levels = apply_filters("pmpro_deactivate_old_levels", $pmpro_deactivate_old_levels); 
  64.  
  65. //make sure we deactivate the specified level if it's passed in 
  66. if(!empty($cancel_level)) { 
  67. $pmpro_deactivate_old_levels = true; 
  68. $new_old_levels = array(); 
  69. foreach($old_levels as $key => $old_level) { 
  70. if($old_level->id == $cancel_level) { 
  71. $new_old_levels[] = $old_levels[$key]; 
  72. break; 
  73. $old_levels = $new_old_levels; 
  74.  
  75. if($old_levels && $pmpro_deactivate_old_levels) 
  76. foreach($old_levels as $old_level) { 
  77.  
  78. $sql = "UPDATE $wpdb->pmpro_memberships_usersSET`status`='$old_level_status', `enddate`='" . current_time('mysql') . "' WHERE `id`=".$old_level->subscription_id; 
  79.  
  80. if(!$wpdb->query($sql)) 
  81. $pmpro_error = __("Error interacting with database", 'paid-memberships-pro' ) . ": ".($wpdb->last_error?$wpdb->last_error:'unavailable'); 
  82.  
  83. return false; 
  84.  
  85. //should we cancel theirgatewaysubscriptions? 
  86. if(!empty($cancel_level)) { 
  87. $pmpro_cancel_previous_subscriptions = true; //don't filter cause we're doing just the one 
  88.  
  89. $other_order_ids = $wpdb->get_col("SELECT id FROM $wpdb->pmpro_membership_orders WHERE user_id = '" . $user_id . "' AND status = 'success' AND membership_id = '" . esc_sql($cancel_level) . "'ORDERBY id DESC"); 
  90. } else { 
  91. $pmpro_cancel_previous_subscriptions = true; 
  92. if(isset($_REQUEST['cancel_membership']) && $_REQUEST['cancel_membership'] == false) 
  93. $pmpro_cancel_previous_subscriptions = false; 
  94. $pmpro_cancel_previous_subscriptions = apply_filters("pmpro_cancel_previous_subscriptions", $pmpro_cancel_previous_subscriptions); 
  95.  
  96. $other_order_ids = $wpdb->get_col("SELECT id FROM $wpdb->pmpro_membership_orders WHERE user_id = '" . $user_id . "' AND status = 'success'ORDERBY id DESC"); 
  97.  
  98. //cancel any other subscriptions they have (updates pmpro_membership_orders table) 
  99. if($pmpro_cancel_previous_subscriptions && !empty($other_order_ids)) 
  100. {  
  101. foreach($other_order_ids as $order_id) 
  102. $c_order = new MemberOrder($order_id); 
  103. $c_order->cancel(); 
  104.  
  105. if(!empty($c_order->error)) 
  106. $pmpro_error = $c_order->error; 
  107.  
  108. //insert current membership 
  109. if(!empty($level)) //are we getting a new one or just cancelling the old ones 
  110. {  
  111. //make sure the dates are in good formats 
  112. if(is_array($level)) 
  113. //Better support mySQL Strict Mode by passing a proper enum value for cycle_period 
  114. if ($level['cycle_period'] == '') { $level['cycle_period'] = 0; } 
  115.  
  116. // clean up date formatting (string/not string) 
  117. $level['startdate'] = preg_replace('/\'/', '', $level['startdate']); 
  118. $level['enddate'] = preg_replace('/\'/', '', $level['enddate']); 
  119.  
  120. $sql = $wpdb->prepare(" 
  121. INSERT INTO {$wpdb->pmpro_memberships_users} 
  122. (`user_id`, `membership_id`, `code_id`, `initial_payment`, `billing_amount`, `cycle_number`, `cycle_period`, `billing_limit`, `trial_amount`, `trial_limit`, `startdate`, `enddate`) 
  123. VALUES 
  124. ( %d, %d, %d, %s, %s, %d, %s, %d, %s, %d, %s, %s )",  
  125. $level['user_id'], // integer 
  126. $level['membership_id'], // integer 
  127. $level['code_id'], // integer 
  128. $level['initial_payment'], // float (string) 
  129. $level['billing_amount'], // float (string) 
  130. $level['cycle_number'], // integer 
  131. $level['cycle_period'], // string (enum) 
  132. $level['billing_limit'], // integer 
  133. $level['trial_amount'], // float (string) 
  134. $level['trial_limit'], // integer 
  135. $level['startdate'], // string (date) 
  136. $level['enddate'] // string (date) 
  137. ); 
  138. else 
  139. $sql = $wpdb->prepare(" 
  140. INSERT INTO {$wpdb->pmpro_memberships_users} 
  141. ( `user_id`, `membership_id`, `code_id`, `initial_payment`, `billing_amount`, `cycle_number`, `cycle_period`, `billing_limit`, `trial_amount`, `trial_limit`, `startdate`, `enddate`) 
  142. VALUES  
  143. ( %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %s, %s )",  
  144. $user_id,  
  145. $level_id,  
  146. '0',  
  147. '0',  
  148. '0',  
  149. '0',  
  150. '0',  
  151. '0',  
  152. '0',  
  153. '0',  
  154. current_time('mysql'),  
  155. '0000-00-00 00:00:00' 
  156. ); 
  157.  
  158. if( false === $wpdb->query($sql) ) 
  159. $pmpro_error = sprintf( __("Error interacting with database: %s", 'paid-memberships-pro' ), (!empty($wpdb->last_error) ? $wpdb->last_error : 'unavailable' )); 
  160. return false; 
  161. }  
  162.  
  163. //remove cached level 
  164. global $all_membership_levels; 
  165. unset($all_membership_levels[$user_id]); 
  166.  
  167. //update user data and call action 
  168.  
  169. /** 
  170. * Action to run after the membership level changes. 
  171. * @param int $level_id ID of the level changed to. 
  172. * @param int $user_id ID of the user changed. 
  173. * $param int $cancel_level ID of the level being cancelled if specified. 
  174. */ 
  175. do_action("pmpro_after_change_membership_level", $level_id, $user_id, $cancel_level); 
  176. return true;