PluginProbe
GetResponse Forms by Optin Cat / 1.3.9
GetResponse Forms by Optin Cat v1.3.9
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / eoi-activity.php

eoi-activity.php in GetResponse Forms by Optin Cat 1.3.9, at includes/eoi-activity.php

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