PluginProbe
GetResponse Forms by Optin Cat / 2.0.2
GetResponse Forms by Optin Cat v2.0.2
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 2.0.2, at includes/eoi-activity.php

252 lines 6.4 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 if ( !defined ( 'FCA_EOI_DISABLE_STATS_TRACKING' )) {
44 add_action( 'wp_ajax_fca_eoi_activity', array( $this, 'track_activity' ) );
45 add_action( 'wp_ajax_nopriv_fca_eoi_activity', array( $this, 'track_activity' ) );
46 }
47 }
48
49 public function get_text( $name, $category = null, $parameters = array() ) {
50 $plain_text = $this->text[ $name ];
51 $text = $category ? $plain_text[ $category ] : $plain_text;
52
53 if ( ! empty( $parameters ) ) {
54 $text = call_user_func_array( 'sprintf', array_merge( array( $text ), $parameters ) );
55 }
56
57 if ( $name == 'period' && empty( $parameters[0] ) ) {
58 $text = $this->text['all_time'];
59 }
60
61 return $text;
62 }
63
64 public function setup() {
65 $sql = "CREATE TABLE $this->table_name (
66 form_id INT(11) NOT NULL,
67 type ENUM('impression', 'conversion') NOT NULL,
68 timestamp TIMESTAMP NOT NULL,
69 day DATE NOT NULL,
70 KEY form_id (form_id),
71 KEY timestamp (timestamp),
72 KEY day (day)
73 );";
74
75 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
76 dbDelta( $sql );
77 }
78
79 public function track_activity() {
80
81 $nonce = sanitize_text_field( $_REQUEST['nonce'] );
82 $form_id = intval( $_REQUEST['form_id'] );
83
84 $nonceVerified = wp_verify_nonce( $nonce, 'fca_eoi_activity') == 1;
85 $idVerified = is_int( $form_id ) && $form_id > 0;
86
87 if ( $nonceVerified && $idVerified ) {
88
89 require_once FCA_EOI_PLUGIN_DIR . 'includes/classes/RobotDetector/RobotDetector.php';
90 $robot_detector = new RobotDetector();
91
92 if ( get_post( $form_id ) && !is_user_logged_in() && !$robot_detector->is_robot() ) {
93 $this->add_impression( $form_id );
94 wp_send_json_success( $form_id );
95 }
96 }
97 wp_send_json_error();
98
99 }
100
101 public function format_column_text( $column_name, $value ) {
102 if ( $column_name == 'conversion_rate' ) {
103 return number_format( $value * 100, 2 ) . '%';
104 } else {
105 return $value;
106 }
107 }
108
109 public function get_daily_stats( $day_interval ) {
110 if ( empty( $this->daily_stats ) ) {
111 global $wpdb;
112
113 $days = array();
114 $stats = array(
115 'impressions' => array(),
116 'conversions' => array(),
117 'totals' => array(
118 'impressions' => 0,
119 'conversions' => 0,
120 'conversion_rate' => 0
121 )
122 );
123
124 $now = time();
125 for ( $i = $day_interval - 1; $i >= 0; $i -- ) {
126 $time = $now - ( 86400 * $i );
127 $day = date( 'Y-m-d', $time );
128 $days[] = $day;
129
130 $stats['impressions'][ $day ] = 0;
131 $stats['conversions'][ $day ] = 0;
132 }
133
134 foreach ( array( 'impression', 'conversion' ) as $activity_type ) {
135 $query = $this->get_daily_stats_query( $activity_type, $day_interval );
136 foreach ( $wpdb->get_results( $query ) as $result ) {
137 $activity_type_plural = $activity_type . 's';
138
139 if ( ! array_key_exists( $result->day, $stats[ $activity_type_plural ] ) ) {
140 continue;
141 }
142
143 $total = (int) $result->total;
144
145 $stats[ $activity_type_plural ][ $result->day ] = $total;
146 $stats['totals'][ $activity_type_plural ] += $total;
147 }
148 }
149
150 $stats['totals']['conversion_rate'] = $this->calculate_conversion_rate(
151 $stats['totals']['impressions'],
152 $stats['totals']['conversions']
153 );
154
155 $this->daily_stats = $stats;
156 }
157
158 return $this->daily_stats;
159 }
160
161 public function get_form_stats( $day_interval ) {
162 if ( empty( $this->form_stats ) ) {
163 global $wpdb;
164
165 $stats = array();
166 $ids = array();
167
168 foreach ( array( 'impression', 'conversion' ) as $activity_type ) {
169 $stat = array();
170 $query = $this->get_form_stats_query( $activity_type, $day_interval );
171 foreach ( $wpdb->get_results( $query ) as $result ) {
172 $ids[ $result->form_id ] = true;
173 $stat[ $result->form_id ] = (int) $result->total;
174 }
175 $stats[ $activity_type . 's' ] = $stat;
176 }
177
178 foreach ( array_keys( $ids ) as $form_id ) {
179 $stats['conversion_rate'][ $form_id ] = $this->calculate_conversion_rate(
180 floatval( empty( $stats['impressions'][ $form_id ] ) ? 0 : $stats['impressions'][ $form_id ] ),
181 floatval( empty( $stats['conversions'][ $form_id ] ) ? 0 : $stats['conversions'][ $form_id ] )
182 );
183 }
184
185 $this->form_stats = $stats;
186 }
187
188 return $this->form_stats;
189 }
190
191 private function calculate_conversion_rate( $impressions, $conversions ) {
192 if ( $impressions < 1 ) {
193 return 0.0;
194 } else {
195 return $conversions / $impressions;
196 }
197 }
198
199 public function add_impression( $form_id ) {
200 $this->add_activity( $form_id, 'impression' );
201 }
202
203 public function add_conversion( $form_id ) {
204 $this->add_activity( $form_id, 'conversion' );
205 }
206
207 public function reset_stats( $form_id ) {
208 global $wpdb;
209
210 $wpdb->delete( $this->table_name, array( 'form_id' => $form_id ), '%d' );
211 }
212
213 private function add_activity( $form_id, $activity_type ) {
214
215 if ( !defined ( 'FCA_EOI_DISABLE_STATS_TRACKING' )) {
216
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 }
229
230 private function get_form_stats_query( $activity_type, $day_interval ) {
231 return $this->get_stats_query( 'form_id', $activity_type, $day_interval );
232 }
233
234 private function get_daily_stats_query( $activity_type, $day_interval ) {
235 return $this->get_stats_query( 'day', $activity_type, $day_interval );
236 }
237
238 private function get_stats_query( $field, $activity_type, $day_interval ) {
239 global $wpdb;
240
241 return $wpdb->prepare(
242 "SELECT `$field`, COUNT(*) AS `total` " .
243 "FROM `$this->table_name` " .
244 "WHERE " .
245 "`type` = %s " .
246 ( $day_interval
247 ? "AND `timestamp` >= DATE_SUB(NOW(), INTERVAL $day_interval DAY) "
248 : "") .
249 "GROUP BY `$field`", $activity_type );
250 }
251 }
252