PluginProbe
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation / trunk
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation vtrunk
1.4.48 1.4.47 1.4.46 1.4.45 1.4.44 1.4.43 1.4.42 1.4.41 1.4.40 1.4.39 1.4.38 1.4.37 1.4.36 1.1.2 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 64 releases
optin / includes / class-analytics.php

class-analytics.php in WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation trunk, at includes/class-analytics.php

288 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace OPTN\Includes;
4
5 use WP_REST_Request;
6 use OPTN\Includes\Db;
7 use OPTN\Includes\Utils\VisitorCount;
8 use OPTN\Includes\Utils\Utils;
9
10 /**
11 * Handles analytics for WowOptin
12 */
13 class Analytics {
14 use \OPTN\Includes\Traits\Singleton;
15
16 /**
17 * DB class instance
18 *
19 * @var \OPTN\Includes\Db $db db class
20 */
21 private $db;
22
23 /**
24 * VisitorCount class instance
25 *
26 * @var \OPTN\Includes\Utils\VisitorCount $visitor_count
27 */
28 private $visitor_count;
29
30 /**
31 * Constructor
32 */
33 public function __construct() {
34 $this->db = Db::get_instance();
35 $this->visitor_count = VisitorCount::get_instance();
36 }
37
38 /**
39 * Register public routes
40 *
41 * @return void
42 */
43 public function register_public_routes() {
44 register_rest_route(
45 'optn/v1',
46 '/update-analytics/',
47 array(
48 'methods' => 'POST',
49 'callback' => array( $this, 'handle_update_analytics' ),
50 'permission_callback' => '__return_true',
51 )
52 );
53
54 register_rest_route(
55 'optn/v1',
56 '/get-ipinfo/',
57 array(
58 'methods' => 'GET',
59 'callback' => array( $this, 'handle_get_ipinfo' ),
60 'permission_callback' => '__return_true',
61 )
62 );
63 }
64
65 /**
66 * Register admin routes
67 *
68 * @return void
69 */
70 public function register_admin_routes() {
71 register_rest_route(
72 'optn/v1',
73 '/get-quick-view-data/',
74 array(
75 'methods' => 'GET',
76 'callback' => array( $this, 'handle_get_quick_view_data' ),
77 'permission_callback' => array( $this, 'permission_callback' ),
78 )
79 );
80
81 register_rest_route(
82 'optn/v1',
83 '/get-valid-convs/',
84 array(
85 'methods' => 'GET',
86 'callback' => array( $this, 'handle_get_valid_convs' ),
87 'permission_callback' => array( $this, 'permission_callback' ),
88 )
89 );
90
91 register_rest_route(
92 'optn/v1',
93 '/get-impressions/',
94 array(
95 'methods' => 'POST',
96 'callback' => array( $this, 'handle_get_impression' ),
97 'permission_callback' => array( $this, 'permission_callback' ),
98 )
99 );
100
101 register_rest_route(
102 'optn/v1',
103 '/get-impressions-device/',
104 array(
105 'methods' => 'POST',
106 'callback' => array( $this, 'handle_get_impression_device' ),
107 'permission_callback' => array( $this, 'permission_callback' ),
108 )
109 );
110
111 register_rest_route(
112 'optn/v1',
113 '/get-conversions/',
114 array(
115 'methods' => 'POST',
116 'callback' => array( $this, 'handle_get_conversion' ),
117 'permission_callback' => array( $this, 'permission_callback' ),
118 )
119 );
120
121 register_rest_route(
122 'optn/v1',
123 '/get-sales/',
124 array(
125 'methods' => 'POST',
126 'callback' => array( $this, 'handle_get_sales' ),
127 'permission_callback' => array( $this, 'permission_callback' ),
128 )
129 );
130
131 register_rest_route(
132 'optn/v1',
133 '/get-pop-optins/',
134 array(
135 'methods' => 'POST',
136 'callback' => array( $this, 'handle_get_pop_optins' ),
137 'permission_callback' => array( $this, 'permission_callback' ),
138 )
139 );
140
141 register_rest_route(
142 'optn/v1',
143 '/get-geo-view/',
144 array(
145 'methods' => 'POST',
146 'callback' => array( $this, 'handle_get_geo_view' ),
147 'permission_callback' => array( $this, 'permission_callback' ),
148 )
149 );
150 }
151
152 public function handle_get_geo_view( $req ) {
153 $days = isset( $req['days'] ) ? intval( $req['days'] ) : 7;
154
155 $res = $this->db->get_geo_view_data( $days );
156
157 return rest_ensure_response( $res );
158 }
159
160 public function handle_get_pop_optins( $req ) {
161 $days = isset( $req['days'] ) ? intval( $req['days'] ) : 7;
162 $limit = isset( $req['limit'] ) ? intval( $req['limit'] ) : 6;
163 $page = isset( $req['page'] ) ? intval( $req['page'] ) : 1;
164
165 $res = $this->db->get_pop_optin( $days, $limit, $page );
166
167 return rest_ensure_response( $res );
168 }
169
170 public function handle_get_sales( WP_REST_Request $req ) {
171 $id = isset( $req['id'] ) ? intval( $req['id'] ) : null;
172 $days = isset( $req['days'] ) ? intval( $req['days'] ) : 7;
173
174 $res = $this->db->get_sales( $id, $days );
175 return rest_ensure_response( $res );
176 }
177
178 public function handle_get_conversion( WP_REST_Request $req ) {
179 $id = isset( $req['id'] ) ? intval( $req['id'] ) : null;
180 $days = isset( $req['days'] ) ? intval( $req['days'] ) : 7;
181
182 $res = $this->db->get_conversions( $id, $days );
183 return rest_ensure_response( $res );
184 }
185
186 public function handle_get_impression_device( WP_REST_Request $req ) {
187 $id = isset( $req['id'] ) ? intval( $req['id'] ) : null;
188 $days = isset( $req['days'] ) ? intval( $req['days'] ) : 7;
189
190 $res = $this->db->get_impressions_by_device( $id, $days );
191 return rest_ensure_response( $res );
192 }
193
194 public function handle_get_impression( WP_REST_Request $req ) {
195 $id = isset( $req['id'] ) ? intval( $req['id'] ) : null;
196 $days = isset( $req['days'] ) ? intval( $req['days'] ) : 7;
197 $unique = isset( $req['unique'] ) ? boolval( $req['unique'] ) : false;
198
199 $res = $this->db->get_impressions( $id, $days, $unique );
200 return rest_ensure_response( $res );
201 }
202
203 public function handle_get_valid_convs( WP_REST_Request $req ) {
204 $res = $this->db->get_post_for_select();
205 return $res;
206 }
207
208 public function handle_get_quick_view_data( WP_REST_Request $req ) {
209 $id = isset( $req['id'] ) ? intval( $req['id'] ) : null;
210 $days = isset( $req['days'] ) ? intval( $req['days'] ) : 7;
211
212 $db_quick_view = $this->db->get_quick_view_data( $id, $days );
213
214 $visitors = $this->visitor_count->get_stats();
215
216 $res = array_merge(
217 $db_quick_view,
218 $visitors,
219 );
220
221 return rest_ensure_response( $res );
222 }
223
224 public function handle_update_analytics( $request ) {
225 $id = isset( $request['id'] ) ? intval( $request['id'] ) : null;
226 $type = isset( $request['type'] ) ? sanitize_text_field( $request['type'] ) : '';
227 $goal_type = isset( $request['goal_type'] ) ? sanitize_text_field( $request['goal_type'] ) : '';
228 $value = isset( $request['value'] ) ? floatval( $request['value'] ) : null;
229 $path = isset( $request['path'] ) ? sanitize_url( $request['path'] ) : null;
230
231 $path = ! empty( $path ) ? ( str_ends_with( $path, '/' ) ? rtrim( $path, '/' ) : $path ) : null;
232
233 $res = '';
234 switch ( $type ) {
235 case 'view':
236 $res = $this->db->add_view( $id );
237 break;
238 case 'goal':
239 $res = $this->db->add_goal( $id, $goal_type, $value, $path );
240 break;
241 case 'social':
242 $res = $this->db->add_social_click( $id, $goal_type );
243 break;
244 }
245
246 return rest_ensure_response( $res );
247 }
248
249 public function handle_get_ipinfo() {
250 $user_ip = Utils::get_ip_from_request();
251
252 if ( ! empty( $user_ip ) ) {
253 return rest_ensure_response( 500 );
254 }
255
256 $url = "https://ipinfo.io/{$user_ip}/json";
257
258 $response = wp_remote_get( $url );
259
260 if ( is_wp_error( $response ) ) {
261 return rest_ensure_response( 500 );
262 }
263
264 $body = wp_remote_retrieve_body( $response );
265 $data = json_decode( $body );
266
267 $success = is_object( $data ) && isset( $data->ip ) && isset( $data->country );
268
269 $res = array(
270 'success' => $success,
271 );
272
273 if ( $success ) {
274 $res['data'] = array(
275 'ip' => $data->ip,
276 'country' => $data->country,
277 );
278 }
279
280 return rest_ensure_response( $res );
281 }
282
283
284 public function permission_callback() {
285 return current_user_can( OPTN_MIN_CAPABILITY );
286 }
287 }
288