pmpro_upgrade_1_8_9_3_ajax

We run this part of the update via AJAX from the updates page.

Description

pmpro_upgrade_1_8_9_3_ajax(); 

Usage

  1. if ( !function_exists( 'pmpro_upgrade_1_8_9_3_ajax' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/updates/upgrade_1_8_9_3.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = pmpro_upgrade_1_8_9_3_ajax(); 
  7.  

Defined (1)

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

/includes/updates/upgrade_1_8_9_3.php  
  1. function pmpro_upgrade_1_8_9_3_ajax() { 
  2. global $wpdb; 
  3.  
  4. $debug = false; 
  5. $run = true; 
  6.  
  7. //some vars 
  8. $all_levels = pmpro_getAllLevels(true, true); 
  9.  
  10. //keeping track of which user we're working on 
  11. $last_user_id = get_option('pmpro_upgrade_1_8_9_3_last_user_id', 0); 
  12.  
  13. //get all active users during the period where things may have been broken 
  14. $user_ids = $wpdb->get_col("SELECT user_id FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND modified > '2016-05-19' AND user_id > $last_user_idORDERBY user_id LIMIT 10"); 
  15.  
  16. //track progress 
  17. $first_load = get_transient('pmpro_updates_first_load'); 
  18. if($first_load) { 
  19. $total_users = $wpdb->get_var("SELECT COUNT(user_id) FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND modified > '2016-05-19'ORDERBY user_id"); 
  20. update_option('pmpro_upgrade_1_8_9_3_total', $total_users, 'no'); 
  21. $progress = 0; 
  22. } else { 
  23. $total_users = get_option('pmpro_upgrade_1_8_9_3_total', 0); 
  24. $progress = get_option('pmpro_upgrade_1_8_9_3_progress', 0); 
  25. update_option('pmpro_upgrade_1_8_9_3_progress', $progress + count($user_ids), 'no'); 
  26. global $pmpro_updates_progress; 
  27. if($total_users > 0) 
  28. $pmpro_updates_progress = "[" . $progress . "/" . $total_users . "]"; 
  29. else 
  30. $pmpro_updates_progress = ""; 
  31.  
  32. if(empty($user_ids)) { 
  33. //done with this update  
  34. pmpro_removeUpdate('pmpro_upgrade_1_8_9_3_ajax'); 
  35. delete_option('pmpro_upgrade_1_8_9_3_last_user_id'); 
  36. delete_option('pmpro_upgrade_1_8_9_3_total'); 
  37. delete_option('pmpro_upgrade_1_8_9_3_progress'); 
  38. } else { 
  39. foreach($user_ids as $user_id) { 
  40. $last_user_id = $user_id; //keeping track of the last user we processed 
  41. $user = get_userdata($user_id); 
  42.  
  43. //user not found for some reason 
  44. if(empty($user)) { 
  45. if($debug) 
  46. echo "User #" . $user_id . " not found.\n"; 
  47. continue; 
  48.  
  49. //get level 
  50. $user->membership_level = pmpro_getMembershipLevelForUser($user->ID); 
  51.  
  52. //has a start and end date already 
  53. if(!empty($user->membership_level->enddate) && !empty($user->membership_level->startdate)) { 
  54. if($debug) 
  55. echo "User #" . $user_id . ", " . $user->user_email . " already has a start and end date.\n"; 
  56. continue; 
  57.  
  58. //get order 
  59. $last_order = new MemberOrder(); 
  60. $last_order->getLastMemberOrder(); 
  61.  
  62. /** 
  63. Figure out if this user should have been given an end date. 
  64. The level my have an end date. 
  65. They might have used adiscountcode. 
  66. They might be using the set-expiration-dates code. 
  67. They might have custom code setting the end date. 
  68.   
  69. Let'ssetupsome vars as if we are at checkout. 
  70. Then pass recreate the level with the pmpro_checkout_level filter. 
  71. And use the end date there if there is one. 
  72. */ 
  73. global $pmpro_level, $discount_code, $discount_code_id; 
  74.  
  75. //level 
  76. $level_id = $user->membership_level->id; 
  77. $_REQUEST['level'] = $level_id; 
  78.  
  79. //gateway 
  80. if(!empty($last_order) && !empty($last_order->gateway)) 
  81. $_REQUEST[gateway] = $last_order->gateway; 
  82. else 
  83. $_REQUEST[gateway] = pmpro_getGateway(); 
  84.  
  85. //discount code 
  86. $discount_code_id = $user->membership_level->code_id; 
  87. $discount_code = $wpdb->get_var( "SELECT code FROM $wpdb->pmpro_discount_codes WHERE id = '" . $discount_code_id . "' LIMIT 1" ); 
  88.  
  89. //get level 
  90. if(!empty($discount_code_id)) { 
  91. $sqlQuery = "SELECT l.id, cl.*, l.name, l.description, l.allow_signups FROM $wpdb->pmpro_discount_codes_levels cl LEFT JOIN $wpdb->pmpro_membership_levels l ON cl.level_id = l.id LEFT JOIN $wpdb->pmpro_discount_codes dc ON dc.id = cl.code_id WHERE dc.code = '" . $discount_code . "' AND cl.level_id = '" . (int) $level_id . "' LIMIT 1"; 
  92.  
  93. $pmpro_level = $wpdb->get_row( $sqlQuery ); 
  94.  
  95. //if thediscountcode doesn't adjust the level, let's just get the straight level 
  96. if ( empty( $pmpro_level ) ) { 
  97. $pmpro_level = $all_levels[$level_id]; 
  98.  
  99. //filter adjustments to the level 
  100. $pmpro_level->code_id = $discount_code_id; 
  101. $pmpro_level = apply_filters( "pmpro_discount_code_level", $pmpro_level, $discount_code_id ); 
  102.  
  103. //no level yet, use default 
  104. if ( empty( $pmpro_level ) ) { 
  105. $pmpro_level = $all_levels[$level_id]; 
  106.  
  107. //no level for some reason 
  108. if(empty($pmpro_level) && empty($pmpro_level->id)) { 
  109. if($debug) 
  110. echo "No level found with ID #" . $level_id . " for user #" . $user_id . ", " . $user->user_email . ".\n"; 
  111. continue; 
  112.  
  113. //filter level 
  114. $pmpro_level = apply_filters( "pmpro_checkout_level", $pmpro_level ); 
  115.  
  116. if($debug) 
  117. echo "User #" . $user_id . ", " . $user->user_email . ". Fixing.\n"; 
  118.  
  119. //calculate and fix start date 
  120. if(empty($user->membership_level->startdate)) { 
  121. $startdate = $wpdb->get_var("SELECT modified FROM $wpdb->pmpro_memberships_users WHERE user_id = $user_id AND membership_id = $level_id AND status = 'active' LIMIT 1"); 
  122.  
  123. //filter 
  124. $filtered_startdate = apply_filters( "pmpro_checkout_start_date", $startdate, $user_id, $pmpro_level ); 
  125.  
  126. //only use filtered value if it's not 0 
  127. if(!empty($filtered_startdate) && $filtered_startdate != '0000-00-00 00:00:00' && $filtered_startdate != "'0000-00-00 00:00:00'") 
  128. $startdate = $filtered_startdate; 
  129.  
  130. if($debug) 
  131. echo "- Adding startdate " . $startdate . ".\n"; 
  132. if($run) { 
  133. $sqlQuery = "UPDATE $wpdb->pmpro_memberships_usersSETstartdate = '" . esc_sql($startdate) . "' WHERE user_id = $user_id AND membership_id = $level_id AND status = 'active' LIMIT 1"; 
  134. $wpdb->query($sqlQuery); 
  135. } else { 
  136. $startdate = date_i18n( "Y-m-d", $user->membership_level->startdate ); 
  137.  
  138. //calculate and fix the end date 
  139. if(empty($user->membership_level->enddate)) { 
  140. if ( ! empty( $pmpro_level->expiration_number ) ) { 
  141. $enddate = date_i18n( "Y-m-d", strtotime( "+ " . $pmpro_level->expiration_number . " " . $pmpro_level->expiration_period, $last_order->timestamp ) ); 
  142. } else { 
  143. $enddate = "NULL"; 
  144.  
  145. $enddate = apply_filters( "pmpro_checkout_end_date", $enddate, $user_id, $pmpro_level, $startdate ); 
  146.  
  147. if(!empty($enddate) && $enddate != "NULL") { 
  148. if($debug) 
  149. echo "- Adding enddate " . $enddate . ".\n"; 
  150. if($run) { 
  151. $sqlQuery = "UPDATE $wpdb->pmpro_memberships_usersSETenddate = '" . esc_sql($enddate) . "' WHERE user_id = $user_id AND membership_id = $level_id AND status = 'active' LIMIT 1"; 
  152. $wpdb->query($sqlQuery); 
  153.  
  154. //clear vars for next pass 
  155. $user_id = NULL; 
  156. $level_id = NULL; 
  157. $discount_code = NULL; 
  158. $discount_code_id = NULL; 
  159. $pmpro_level = NULL; 
  160. $last_order = NULL; 
  161. $startdate = NULL; 
  162. $filtered_startdate = NULL; 
  163. $enddate = NULL; 
  164.  
  165. echo "\n"; 
  166.  
  167. update_option('pmpro_upgrade_1_8_9_3_last_user_id', $last_user_id, 'no');