pmpro_getCancellations

Get cancellations by status.

Description

(null|int) pmpro_getCancellations( (constant) $period = null, (string) $levels = 'all', (array) $status = array() ); 

Returns (null|int)

- The # of cancellations for the period specified

Parameters (3)

0. $period — Optional. (constant) => null
- Either a string description ('today', this month,, this year)
1. $levels — Optional. (string) => 'all'
- Either an array of level IDs or the string all
2. $status — Optional. (array) => array()
- Array of statuses to fetch data for

Usage

  1. if ( !function_exists( 'pmpro_getCancellations' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/adminpages/reports/memberships.php'; 
  3.  
  4. // - Either a string description ('today', 'this month', 'this year') 
  5. $period = null; 
  6.  
  7. // - Either an array of level IDs or the string 'all' 
  8. $levels = 'all'; 
  9.  
  10. // - Array of statuses to fetch data for 
  11. $status = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = pmpro_getCancellations($period, $levels, $status); 
  15.  

Defined (1)

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

/adminpages/reports/memberships.php  
  1. function pmpro_getCancellations($period = null, $levels = 'all', $status = array('inactive', 'expired', 'cancelled', 'admin_cancelled') ) 
  2. //make sure status is an array 
  3. if(!is_array($status)) 
  4. $status = array($status); 
  5.  
  6. //check for a transient 
  7. $cache = get_transient( 'pmpro_report_memberships_cancellations' ); 
  8. $hash = md5($period . $levels . implode(', ', $status)); 
  9. if( ! empty( $cache ) && ! empty( $cache[$hash] ) ) 
  10. return $cache[$hash]; 
  11.  
  12. //figure out start date 
  13. $now = current_time('timestamp'); 
  14. $year = date_i18n("Y", $now); 
  15.  
  16. if( $period == 'today' ) 
  17. $startdate = date_i18n('Y-m-d', $now) . " 00:00:00"; 
  18. $enddate = "'" . date_i18n('Y-m-d', $now) . " 23:59:59'"; 
  19. elseif( $period == 'this month') 
  20. $startdate = date_i18n( 'Y-m', $now ) . '-01 00:00:00'; 
  21. $enddate = "CONCAT(LAST_DAY('" . date_i18n( 'Y-m', $now ) . '-01' ."'), ' 23:59:59')"; 
  22. elseif( $period == 'this year') 
  23. $startdate = date_i18n( 'Y', $now ) . '-01-01 00:00:00'; 
  24. $enddate = "'" . date_i18n( 'Y', $now ) . "-12-31 23:59:59'"; 
  25. else 
  26. //all time 
  27. $startdate = '1970-01-01'; //all time (no point in using a value prior to the start of the UNIX epoch) 
  28. $enddate = "'".strval(intval($year)+1) . "-01-01'"; 
  29.  
  30. /** 
  31. build query. 
  32. cancellations are marked in the memberships users table with status 'inactive', 'expired', 'cancelled', 'cancelled_admin' 
  33. we try to ignore cancellations when the user gets a new level with 24 hours (probably an upgrade or downgrade) 
  34. */ 
  35. global $wpdb; 
  36.  
  37. $sqlQuery = " 
  38. SELECT COUNT( DISTINCT mu1.user_id ) 
  39. FROM {$wpdb->pmpro_memberships_users} AS mu1  
  40. WHERE mu1.status IN('" . implode("', '", $status) . "') 
  41. AND mu1.enddate >= '" . $startdate . "' 
  42. AND mu1.enddate <= " . $enddate . " 
  43. "; 
  44.  
  45. //restrict by level 
  46. if(!empty($levels) && $levels != 'all') { 
  47.  
  48. // the levels provided wasn't in array form 
  49. if ( ! is_array($levels) ) { 
  50.  
  51. $levels = array($levels); 
  52.  
  53. $sqlQuery .= "AND mu1.membership_id IN(" . implode(", ", $levels) . ") "; 
  54.  
  55. /** 
  56. * Filter query to get cancellation numbers in signups vs cancellations detailed report. 
  57. * @since 1.8.8 
  58. * @param string $sqlQuery The current SQL 
  59. * @param string $period Period for report. today, this month, this year, empty string for all time. 
  60. * @param array(int) $levels Level IDs to include in report. 
  61. * @param array(string) $status Statuses to include as cancelled. 
  62. */ 
  63. $sqlQuery = apply_filters('pmpro_reports_get_cancellations_sql', $sqlQuery, $period, $levels, $status); 
  64.  
  65. $cancellations = $wpdb->get_var($sqlQuery); 
  66.  
  67. //save in cache 
  68. if(!empty($cache) && !empty($cache[$hash])) 
  69. $cache[$hash] = $cancellations; 
  70. elseif(!empty($cache)) 
  71. $cache[$hash] = $cancellations; 
  72. else 
  73. $cache = array($hash => $cancellations); 
  74.  
  75. set_transient("pmpro_report_memberships_cancellations", $cache, 3600*24); 
  76.  
  77. return $cancellations;