EasyOptInsActivity

The Mailchimp Forms by Optin Cat EasyOptInsActivity class.

Defined (1)

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

/includes/eoi-activity.php  
  1. class EasyOptInsActivity { 
  2. private static $instance; 
  3.  
  4. public $settings; 
  5.  
  6. private $text; 
  7. private $table_name; 
  8. private $form_stats; 
  9. private $daily_stats; 
  10.  
  11. public static function get_instance() { 
  12. if ( ! self::$instance ) { 
  13. self::$instance = new self; 
  14.  
  15. return self::$instance; 
  16.  
  17. public function __construct() { 
  18. global $wpdb; 
  19.  
  20. $this->table_name = $wpdb->prefix . "fca_eoi_activity"; 
  21.  
  22. $this->text = array( 
  23. 'impressions' => array( 
  24. 'total' => __( 'Total Impressions' ),  
  25. 'form' => __( 'Form Impressions' ) 
  26. ),  
  27. 'conversions' => array( 
  28. 'total' => __( 'Total Conversions' ),  
  29. 'form' => __( 'Form Conversions' ) 
  30. ),  
  31. 'conversion_rate' => array( 
  32. 'total' => __( 'Conversion Rate' ),  
  33. 'form' => __( 'Conversion Rate' ),  
  34. ),  
  35. 'period' => __( 'Last %d days' ),  
  36. 'all_time' => __( 'All time' ) 
  37. ); 
  38.  
  39. if ( !defined ( 'FCA_EOI_DISABLE_STATS_TRACKING' )) { 
  40. add_action( 'wp_ajax_fca_eoi_activity', array( $this, 'track_activity' ) ); 
  41. add_action( 'wp_ajax_nopriv_fca_eoi_activity', array( $this, 'track_activity' ) ); 
  42. }  
  43.  
  44. public function get_text( $name, $category = null, $parameters = array() ) { 
  45. $plain_text = $this->text[ $name ]; 
  46. $text = $category ? $plain_text[ $category ] : $plain_text; 
  47.  
  48. if ( ! empty( $parameters ) ) { 
  49. $text = call_user_func_array( 'sprintf', array_merge( array( $text ), $parameters ) ); 
  50.  
  51. if ( $name == 'period' && empty( $parameters[0] ) ) { 
  52. $text = $this->text['all_time']; 
  53.  
  54. return $text; 
  55.  
  56. public function setup() { 
  57. $sql = "CREATE TABLE $this->table_name ( 
  58. form_id INT(11) NOT NULL,  
  59. type ENUM('impression', 'conversion') NOT NULL,  
  60. timestamp TIMESTAMP NOT NULL,  
  61. day DATE NOT NULL,  
  62. KEY form_id (form_id),  
  63. KEY timestamp (timestamp),  
  64. KEY day (day) 
  65. );"; 
  66.  
  67. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  68. dbDelta( $sql ); 
  69.  
  70. public function track_activity() { 
  71.  
  72. $nonce = sanitize_text_field( $_REQUEST['nonce'] ); 
  73. $form_id = intval( $_REQUEST['form_id'] ); 
  74.  
  75. $nonceVerified = wp_verify_nonce( $nonce, 'fca_eoi_activity') == 1; 
  76. $idVerified = is_int( $form_id ) && $form_id > 0; 
  77.  
  78. if ( $nonceVerified && $idVerified ) { 
  79.  
  80. require_once FCA_EOI_PLUGIN_DIR . 'includes/classes/RobotDetector/RobotDetector.php'; 
  81. $robot_detector = new RobotDetector(); 
  82.  
  83. if ( get_post( $form_id ) && !is_user_logged_in() && !$robot_detector->is_robot() ) { 
  84. $this->add_impression( $form_id ); 
  85. wp_send_json_success( $form_id ); 
  86. }  
  87. wp_send_json_error(); 
  88.  
  89.  
  90. public function format_column_text( $column_name, $value ) { 
  91. if ( $column_name == 'conversion_rate' ) { 
  92. return number_format( $value * 100, 2 ) . '%'; 
  93. } else { 
  94. return $value; 
  95.  
  96. public function get_daily_stats( $day_interval ) { 
  97. if ( empty( $this->daily_stats ) ) { 
  98. global $wpdb; 
  99.  
  100. $days = array(); 
  101. $stats = array( 
  102. 'impressions' => array(),  
  103. 'conversions' => array(),  
  104. 'totals' => array( 
  105. 'impressions' => 0,  
  106. 'conversions' => 0,  
  107. 'conversion_rate' => 0 
  108. ); 
  109.  
  110. $now = time(); 
  111. for ( $i = $day_interval - 1; $i >= 0; $i -- ) { 
  112. $time = $now - ( 86400 * $i ); 
  113. $day = date( 'Y-m-d', $time ); 
  114. $days[] = $day; 
  115.  
  116. $stats['impressions'][ $day ] = 0; 
  117. $stats['conversions'][ $day ] = 0; 
  118.  
  119. foreach ( array( 'impression', 'conversion' ) as $activity_type ) { 
  120. $query = $this->get_daily_stats_query( $activity_type, $day_interval ); 
  121. foreach ( $wpdb->get_results( $query ) as $result ) { 
  122. $activity_type_plural = $activity_type . 's'; 
  123.  
  124. if ( ! array_key_exists( $result->day, $stats[ $activity_type_plural ] ) ) { 
  125. continue; 
  126.  
  127. $total = (int) $result->total; 
  128.  
  129. $stats[ $activity_type_plural ][ $result->day ] = $total; 
  130. $stats['totals'][ $activity_type_plural ] += $total; 
  131.  
  132. $stats['totals']['conversion_rate'] = $this->calculate_conversion_rate( 
  133. $stats['totals']['impressions'],  
  134. $stats['totals']['conversions'] 
  135. ); 
  136.  
  137. $this->daily_stats = $stats; 
  138.  
  139. return $this->daily_stats; 
  140.  
  141. public function get_form_stats( $day_interval ) { 
  142. if ( empty( $this->form_stats ) ) { 
  143. global $wpdb; 
  144.  
  145. $stats = array(); 
  146. $ids = array(); 
  147.  
  148. foreach ( array( 'impression', 'conversion' ) as $activity_type ) { 
  149. $stat = array(); 
  150. $query = $this->get_form_stats_query( $activity_type, $day_interval ); 
  151. foreach ( $wpdb->get_results( $query ) as $result ) { 
  152. $ids[ $result->form_id ] = true; 
  153. $stat[ $result->form_id ] = (int) $result->total; 
  154. $stats[ $activity_type . 's' ] = $stat; 
  155.  
  156. foreach ( array_keys( $ids ) as $form_id ) { 
  157. $stats['conversion_rate'][ $form_id ] = $this->calculate_conversion_rate( 
  158. floatval( empty( $stats['impressions'][ $form_id ] ) ? 0 : $stats['impressions'][ $form_id ] ),  
  159. floatval( empty( $stats['conversions'][ $form_id ] ) ? 0 : $stats['conversions'][ $form_id ] ) 
  160. ); 
  161.  
  162. $this->form_stats = $stats; 
  163.  
  164. return $this->form_stats; 
  165.  
  166. private function calculate_conversion_rate( $impressions, $conversions ) { 
  167. if ( $impressions < 1 ) { 
  168. return 0.0; 
  169. } else { 
  170. return $conversions / $impressions; 
  171.  
  172. public function add_impression( $form_id ) { 
  173. $this->add_activity( $form_id, 'impression' ); 
  174.  
  175. public function add_conversion( $form_id ) { 
  176. $this->add_activity( $form_id, 'conversion' ); 
  177.  
  178. public function reset_stats( $form_id ) { 
  179. global $wpdb; 
  180.  
  181. $wpdb->delete( $this->table_name, array( 'form_id' => $form_id ), '%d' ); 
  182.  
  183. private function add_activity( $form_id, $activity_type ) { 
  184.  
  185. if ( !defined ( 'FCA_EOI_DISABLE_STATS_TRACKING' )) { 
  186.  
  187. global $wpdb; 
  188.  
  189. $time = current_time( 'mysql', 1 ); 
  190. $wpdb->insert( $this->table_name, array( 
  191. 'form_id' => $form_id,  
  192. 'type' => $activity_type,  
  193. 'timestamp' => $time,  
  194. 'day' => $time 
  195. ), array( '%d', '%s', '%s', '%s' ) ); 
  196.  
  197.  
  198. private function get_form_stats_query( $activity_type, $day_interval ) { 
  199. return $this->get_stats_query( 'form_id', $activity_type, $day_interval ); 
  200.  
  201. private function get_daily_stats_query( $activity_type, $day_interval ) { 
  202. return $this->get_stats_query( 'day', $activity_type, $day_interval ); 
  203.  
  204. private function get_stats_query( $field, $activity_type, $day_interval ) { 
  205. global $wpdb; 
  206.  
  207. return $wpdb->prepare( 
  208. "SELECT `$field`, COUNT(*) AS `total` " . 
  209. "FROM `$this->table_name` " . 
  210. "WHERE " . 
  211. "`type` = %s " . 
  212. ( $day_interval 
  213. ? "AND `timestamp` >= DATE_SUB(NOW(), INTERVAL $day_interval DAY) " 
  214. : "") . 
  215. "GROUP BY `$field`", $activity_type );