Caldera_Forms_Email_TrackData

Get email stats.

Defined (1)

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

/classes/email/trackdata.php  
  1. class Caldera_Forms_Email_TrackData { 
  2.  
  3. /** 
  4. * Is stats array ready? 
  5. * @since 1.4.5 
  6. * @var bool 
  7. */ 
  8. protected $ready = false; 
  9.  
  10. /** 
  11. * Stats array 
  12. * @since 1.4.5 
  13. * @var array 
  14. */ 
  15. protected $stats = array( 
  16. 'sent' => 0,  
  17. 'failed' => 0,  
  18. 'total' => 0,  
  19. 'success_rate' => 0 
  20. ); 
  21.  
  22. /** 
  23. * Caldera_Forms_Email_TrackData constructor. 
  24. * @since 1.4.5 
  25. */ 
  26. public function __construct() { 
  27. $_stats = get_transient( __CLASS__ ); 
  28. if( ! empty( $_stats ) ) { 
  29. $this->prepare_stats(); 
  30. set_transient( __CLASS__, $_stats, 599 ); 
  31. $this->stats = $_stats; 
  32. $this->ready = true; 
  33.  
  34.  
  35. /** 
  36. * Get the total sent successfully 
  37. * Note: Bypasses cache 
  38. * @since 1.4.5 
  39. * @return int 
  40. */ 
  41. public function success() { 
  42. $events = Caldera_Forms_DB_Track::get_instance()->by_event( 'email_sent', false ); 
  43. $this->stats[ 'sent' ] = count( $events ); 
  44. return $this->stats[ 'sent' ]; 
  45.  
  46.  
  47. /** 
  48. * Get the total fails 
  49. * Note: Bypasses cache 
  50. * @since 1.4.5 
  51. * @return int 
  52. */ 
  53. public function failed() { 
  54. $events = Caldera_Forms_DB_Track::get_instance()->by_event( 'email_failed', false ); 
  55. $this->stats[ 'failed' ] = count( $events ); 
  56. return $this->stats[ 'failed' ]; 
  57.  
  58. /** 
  59. * Get all stats 
  60. * @since 1.4.5 
  61. * @param bool $bypass_cache Optional. If true, data is recalculated even if cached data is present. Default is false. 
  62. * @return array|mixed 
  63. */ 
  64. public function get_stats( $bypass_cache = false ) { 
  65. if ( false == $this->ready || $bypass_cache ) { 
  66. $this->prepare_stats(); 
  67.  
  68. return $this->stats; 
  69.  
  70. /** 
  71. * Prepare stats array with new queries 
  72. * @since 1.4.5 
  73. */ 
  74. protected function prepare_stats() { 
  75. if ( empty( $this->stats[ 'sent' ] ) ) { 
  76. $this->success(); 
  77. if ( empty( $this->stats[ 'failed' ] ) ) { 
  78. $this->failed(); 
  79.  
  80. $this->stats[ 'success_rate' ] = 100; 
  81.  
  82. if ( 0 != $this->stats[ 'failed' ] ) { 
  83. $total = $this->stats[ 'total' ] = $this->stats[ 'sent' ] + $this->stats[ 'failed' ]; 
  84. if ( 0 != $total && 0 != $this->stats[ 'sent' ] ) { 
  85. $this->stats[ 'success_rate' ] = round( $this->stats[ 'sent' ] / $total, 2 ); 
  86.  
  87. set_transient( __CLASS__, $this->stats, 599 ); 
  88.  
  89. $this->ready = true; 
  90.