pmpro_upgrade_1_8_8_ajax

If a site has > 100 orders then we run this pasrt of the update via AJAX from the updates page.

Description

pmpro_upgrade_1_8_8_ajax(); 

Usage

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

Defined (1)

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

/includes/updates/upgrade_1_8_8.php  
  1. function pmpro_upgrade_1_8_8_ajax() { 
  2. global $wpdb; 
  3.  
  4. //keeping track of whichorderwe're working on 
  5. $last_order_id = get_option('pmpro_upgrade_1_8_8_last_order_id', 0); 
  6.  
  7. //Fixing old $0 Stripe orders.  
  8. $orders = $wpdb->get_col("SELECT id FROM $wpdb->pmpro_membership_orders WHERE id > $last_order_id ANDgateway= stripe AND total = 0ORDERBY id LIMIT 2"); 
  9.  
  10. //track progress 
  11. $first_load = get_transient('pmpro_updates_first_load'); 
  12. if($first_load) { 
  13. $total_orders = $wpdb->get_var("SELECT COUNT(id) FROM $wpdb->pmpro_membership_orders WHERE id > $last_order_id ANDgateway= stripe AND total = 0"); 
  14. update_option('pmpro_upgrade_1_8_8_total', $total_orders, 'no'); 
  15. $progress = 0; 
  16. } else { 
  17. $total_orders = get_option('pmpro_upgrade_1_8_8_total', 0); 
  18. $progress = get_option('pmpro_upgrade_1_8_8_progress', 0); 
  19. update_option('pmpro_upgrade_1_8_8_progress', $progress + count($orders), 'no'); 
  20. global $pmpro_updates_progress; 
  21. if($total_orders > 0) 
  22. $pmpro_updates_progress = "[" . $progress . "/" . $total_orders . "]"; 
  23. else 
  24. $pmpro_updates_progress = ""; 
  25.  
  26. if(empty($orders)) { 
  27. //done with this update  
  28. pmpro_removeUpdate('pmpro_upgrade_1_8_8_ajax'); 
  29. delete_option('pmpro_upgrade_1_8_8_last_order_id'); 
  30. delete_option('pmpro_upgrade_1_8_8_total'); 
  31. delete_option('pmpro_upgrade_1_8_8_progress'); 
  32. } else { 
  33. //need to keep working 
  34. foreach($orders as $order_id) {  
  35. $last_order_id = $order_id; //keeping track of the lastorderwe processed 
  36.  
  37. //get order 
  38. $order = new MemberOrder($order_id); 
  39.  
  40. //get customer 
  41. $order->Gateway->getCustomer($order); 
  42.  
  43. //get all invoices 
  44. if(!empty($order->Gateway->customer)) { 
  45. try { 
  46. $invoices = $order->Gateway->customer->invoices(); 
  47. } catch(Exception $e) { 
  48. //probably no invoices, stay quiet 
  49.  
  50. //get our invoice 
  51. if(!empty($invoices)) { 
  52. try { 
  53. $invoice = $invoices->retrieve($order->payment_transaction_id); 
  54. } catch(Exception $e) { 
  55. //probably no invoice, stay quiet 
  56.  
  57. //get total 
  58. if(!empty($invoice)) { 
  59. if($invoice->total > 0) { 
  60. //invoice we accidentally saved $0 for. update the real total. 
  61. $order->subtotal = (! empty( $invoice->subtotal ) ? $invoice->subtotal / 100 : 0); 
  62. $order->tax = (! empty($invoice->tax) ? $invoice->tax / 100 : null); 
  63. $order->total = (! empty($invoice->total) ? $invoice->total / 100 : 0); 
  64. $order->saveOrder(); 
  65. } else { 
  66. //we don't want to track $0 invoices. delete it. 
  67. $order->deleteMe(); 
  68. }  
  69.  
  70. update_option('pmpro_upgrade_1_8_8_last_order_id', $last_order_id, 'no'); 
  71. }