pmpro_getMRR

The Paid Memberships Pro pmpro getMRR function.

Description

pmpro_getMRR( $period, (string) $levels = 'all' ); 

Parameters (2)

0. $period
The period.
1. $levels — Optional. (string) => 'all'
The levels.

Usage

  1. if ( !function_exists( 'pmpro_getMRR' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/adminpages/reports/memberships.php'; 
  3.  
  4. // The period. 
  5. $period = null; 
  6.  
  7. // The levels. 
  8. $levels = 'all'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pmpro_getMRR($period, $levels); 
  12.  

Defined (1)

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

/adminpages/reports/memberships.php  
  1. function pmpro_getMRR($period, $levels = 'all') 
  2. //check for a transient 
  3. //$cache = get_transient("pmpro_report_mrr"); 
  4. if(!empty($cache) && !empty($cache[$period]) && !empty($cache[$period][$levels])) 
  5. return $cache[$period][$levels];  
  6.  
  7. //a sale is anorderwith status NOT IN refunded, review, token, error 
  8. if($period == "this month") 
  9. $startdate = date_i18n("Y-m") . "-01"; 
  10. elseif($period == "this year") 
  11. $startdate = date_i18n("Y") . "-01-01"; 
  12. else 
  13. $startdate = ""; 
  14.  
  15. $gateway_environment = pmpro_getOption("gateway_environment"); 
  16.  
  17. //build query 
  18. global $wpdb; 
  19. // Get total revenue 
  20. $sqlQuery = "SELECT SUM(total) FROM $wpdb->pmpro_membership_orders WHERE status NOT IN('refunded', 'review', token, error) AND timestamp >= '" . $startdate . "' AND gateway_environment = '" . esc_sql($gateway_environment) . "' "; 
  21.  
  22. //restrict by level 
  23. if(!empty($levels) && $levels != 'all') { 
  24. $sqlQuery .= "AND membership_id IN(" . $levels . ") ";  
  25.  
  26. $revenue = $wpdb->get_var($sqlQuery); 
  27.  
  28. //when was the first order 
  29. $first_order_timestamp = $wpdb->get_var("SELECT UNIX_TIMESTAMP(`timestamp`) FROM $wpdb->pmpro_membership_orders WHERE `timestamp` IS NOT NULL AND `timestamp` > '0000-00-00 00:00:00'ORDERBY `timestamp` LIMIT 1"); 
  30.  
  31. //if we don't have a timestamp, we can't do this 
  32. if(empty($first_order_timestamp)) 
  33. return false; 
  34.  
  35. //how many months ago was the first order 
  36. $months = $wpdb->get_var("SELECT PERIOD_DIFF('" . date_i18n("Ym") . "', '" . date_i18n("Ym", $first_order_timestamp) . "')"); 
  37.  
  38. /** this works in PHP 5.3+ without using MySQL to get the diff 
  39. $date1 = new DateTime(date_i18n("Y-m-d", $first_order_timestamp)); 
  40. $date2 = new DateTime(date_i18n("Y-m-d")); 
  41. $interval = $date1->diff($date2); 
  42. $years = intval($interval->format('%y')); 
  43. $months = $years*12 + intval($interval->format('%m')); 
  44. */ 
  45.  
  46. if($months > 0) 
  47. $mrr = $revenue / $months; 
  48. else 
  49. $mrr = 0; 
  50.  
  51. //save in cache 
  52. if(!empty($cache) && !empty($cache[$period])) 
  53. $cache[$period][$levels] = $mrr; 
  54. elseif(!empty($cache)) 
  55. $cache[$period] = array($levels => $mrr); 
  56. else 
  57. $cache = array($period => array($levels => $mrr)); 
  58.  
  59. set_transient("pmpro_report_mrr", $cache, 3600*24); 
  60.  
  61. return $mrr;