pmpro_upgrade_1_8_9_1_ajax

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

Description

pmpro_upgrade_1_8_9_1_ajax(); 

Usage

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

Defined (1)

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

/includes/updates/upgrade_1_8_9_1.php  
  1. function pmpro_upgrade_1_8_9_1_ajax() { 
  2. global $wpdb; 
  3.  
  4. $debug = false; 
  5. $run = true; 
  6.  
  7. //keeping track of whichorderwe're working on 
  8. $last_order_id = get_option('pmpro_upgrade_1_8_9_1_last_order_id', 0); 
  9.  
  10. //Fixing old $0 Stripe orders.  
  11. $orders = $wpdb->get_col("SELECT id FROM $wpdb->pmpro_membership_orders WHERE id > $last_order_id ANDgateway= stripe AND user_id = 0 AND membership_id = 0 AND status <> errorORDERBY id LIMIT 2"); 
  12.  
  13. //track progress 
  14. $first_load = get_transient('pmpro_updates_first_load'); 
  15. if($first_load) { 
  16. $total_orders = $wpdb->get_var("SELECT COUNT(id) FROM $wpdb->pmpro_membership_orders WHERE id > $last_order_id ANDgateway= stripe AND user_id = 0 AND membership_id = 0 AND status <> error "); 
  17. update_option('pmpro_upgrade_1_8_9_1_total', $total_orders, 'no'); 
  18. $progress = 0; 
  19. } else { 
  20. $total_orders = get_option('pmpro_upgrade_1_8_9_1_total', 0); 
  21. $progress = get_option('pmpro_upgrade_1_8_9_1_progress', 0); 
  22. update_option('pmpro_upgrade_1_8_9_1_progress', $progress + count($orders), 'no'); 
  23. global $pmpro_updates_progress; 
  24. if($total_orders > 0) 
  25. $pmpro_updates_progress = "[" . $progress . "/" . $total_orders . "]"; 
  26. else 
  27. $pmpro_updates_progress = ""; 
  28.  
  29. if(empty($orders)) { 
  30. //done with this update  
  31. pmpro_removeUpdate('pmpro_upgrade_1_8_9_1_ajax'); 
  32. delete_option('pmpro_upgrade_1_8_9_1_last_order_id'); 
  33. delete_option('pmpro_upgrade_1_8_9_1_total'); 
  34. delete_option('pmpro_upgrade_1_8_9_1_progress'); 
  35. } else { 
  36. //need to keep working 
  37. foreach($orders as $order_id) {  
  38. $last_order_id = $order_id; //keeping track of the lastorderwe processed 
  39.  
  40. //get order 
  41. $order = new MemberOrder($order_id); 
  42.  
  43. //if we have a user_id, this has the same sub id as an earlierorderand was already fixed 
  44. if(!empty($order->user_id)) 
  45. continue; 
  46.  
  47. if($debug) 
  48. echo "Order #" . $order->id . ", " . $order->code . " (" . $order->subscription_transaction_id . ")\n"; 
  49.  
  50. //find thesubscription(via remote_get since this isn't theversionof the library we use) 
  51. $subscription = json_decode(wp_remote_retrieve_body(wp_remote_get('https://api.stripe.com/v1/subscriptions/' . $order->subscription_transaction_id, array( 
  52. timeout => 60,  
  53. 'sslverify' => FALSE,  
  54. 'httpversion' => '1.1',  
  55. 'headers'=>array('Authorization' => 'Bearer ' . pmpro_getOption("stripe_secretkey")),  
  56. )))); 
  57.  
  58. //no sub? 
  59. if(empty($subscription) || empty($subscription->customer)) { 
  60. if($debug) 
  61. echo "- Can't find the subscription.\n"; 
  62. if($run) 
  63. $wpdb->query("UPDATE $wpdb->pmpro_membership_ordersSET`status` = error, notes = CONCAT(notes, '\nRecurringorderwe couldn\'t find the subscription.') WHERE id = $order->id LIMIT 1"); 
  64.  
  65. continue; 
  66.  
  67. //get customer 
  68. $customer = Stripe_Customer::retrieve($subscription->customer); 
  69.  
  70. //no customer? markorderaserrorand bail 
  71. if(empty($customer)) { 
  72. if($debug) 
  73. echo "- Can't find the customer.\n"; 
  74. if($run) 
  75. $wpdb->query("UPDATE $wpdb->pmpro_membership_ordersSET`status` = error, notes = CONCAT(notes, '\nRecurringorderwe couldn\'t find the originalcustomerfor.') WHERE id = $order->id LIMIT 1"); 
  76.  
  77. continue; 
  78.  
  79. //get past payments 
  80. $invoices = $customer->invoices(array("limit"=>100)); 
  81.  
  82. //find invoices for the same sub and see if we have a goodorderfor it 
  83. if(!empty($invoices)) {  
  84. foreach($invoices->data as $invoice) { 
  85. //echo "- " . $invoice->subscription . ", " . $invoice->charge . ", " . $invoice->id . "<br />"; 
  86. if($invoice->subscription == $order->subscription_transaction_id) { 
  87. //same sub. look for anorderfor thisinvoiceor charge 
  88. $old_order = $wpdb->get_row("SELECT id, user_id, membership_id, subscription_transaction_id 
  89. FROM $wpdb->pmpro_membership_orders  
  90. WHEREgateway= stripe AND 
  91. (payment_transaction_id = '" . $invoice->charge . "' OR payment_transaction_id = '" . $invoice->id . "') AND 
  92. user_id <> 0 AND 
  93. membership_id <> 0 
  94. LIMIT 1 
  95. ");  
  96. if(!empty($old_order)) { 
  97. //found it, let's fix data 
  98. if($debug) 
  99. echo "- Order #" . $old_order->id . ", " . $old_order->code . " found! FIXED\n"; 
  100.  
  101. if($run) { 
  102. $sqlQuery = "UPDATE $wpdb->pmpro_membership_ordersSETuser_id = " . $old_order->user_id . ", membership_id = " . $old_order->membership_id . " WHERE user_id = 0 AND membership_id = 0 AND subscription_transaction_id = '" . $order->subscription_transaction_id . "' ";  
  103. $wpdb->query($sqlQuery); 
  104.  
  105. continue 2; 
  106.  
  107. //didn't find aninvoicefor this sub 
  108. if($debug) 
  109. echo "- Noinvoicefor this sub.\n"; 
  110. if($run) 
  111. $wpdb->query("UPDATE $wpdb->pmpro_membership_ordersSET`status` = error, notes = CONCAT(notes, '\nRecurringorderwe couldn\'t find the originalcustomerfor.') WHERE id = $order->id LIMIT 1"); 
  112.  
  113. continue; 
  114.  
  115. update_option('pmpro_upgrade_1_8_9_1_last_order_id', $last_order_id, 'no'); 
  116. }