PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.17
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.17
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / app / Modules / Form / Analytics.php

Analytics.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.17, at app/Modules/Form/Analytics.php

124 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Form;
4
5 use FluentForm\App\Services\Browser\Browser;
6 use FluentForm\Framework\Foundation\Application;
7
8 class Analytics
9 {
10 /**
11 * App instance
12 *
13 * @var \FluentForm\Framework\Foundation\Application
14 */
15 protected $app;
16
17 /**
18 * Build the instance
19 *
20 * @param \FluentForm\Framework\Foundation\Application $app
21 */
22 public function __construct(Application $app)
23 {
24 $this->app = $app;
25 }
26
27 /**
28 * Save form view analytics data
29 */
30 public function record($formId)
31 {
32 return $this->saveViewAnalytics($formId);
33 }
34
35 public function resetFormAnalytics()
36 {
37 $formId = intval($this->app->request->get('form_id'));
38 wpFluent()
39 ->table('fluentform_form_analytics')
40 ->where('form_id', $formId)
41 ->delete();
42
43 wpFluent()
44 ->table('fluentform_form_meta')
45 ->where('meta_key', '_total_views')
46 ->where('form_id', $formId)
47 ->delete();
48
49 wp_send_json_success([
50 'message' => __('Form Analytics has been successfully resetted'),
51 ], 200);
52 }
53
54 /**
55 * Save form view analytics data
56 */
57 private function saveViewAnalytics($formId)
58 {
59 $userId = null;
60 if ($user = wp_get_current_user()) {
61 $userId = $user->ID;
62 }
63 $browser = new Browser();
64 $data = [
65 'count' => 1,
66 'form_id' => $formId,
67 'user_id' => $userId,
68 'ip' => $this->app->request->getIp(),
69 'browser' => $browser->getBrowser(),
70 'platform' => $browser->getPlatform(),
71 'created_at' => current_time('mysql'),
72 'source_url' => $this->app->request->server('HTTP_REFERER'),
73 ];
74
75 $query = wpFluent()
76 ->table('fluentform_form_analytics')
77 ->where('ip', $data['ip'])
78 ->where('form_id', $data['form_id'])
79 ->where('source_url', $data['source_url']);
80
81 try {
82 if (($record = $query->first())) {
83 $query->update(['count' => ++$record->count]);
84 } else {
85 wpFluent()->table('fluentform_form_analytics')->insert($data);
86 $this->increaseTotalViews($formId);
87 }
88 } catch (\Exception $e) {
89 }
90 }
91
92 /**
93 * Store (create/update) total view of a form
94 *
95 * @param int $formId
96 */
97 private function increaseTotalViews($formId)
98 {
99 // check if meta is already exists or not
100 $hasCount = wpFluent()
101 ->table('fluentform_form_meta')
102 ->where('meta_key', '_total_views')
103 ->where('form_id', $formId)
104 ->first();
105
106 if ($hasCount) {
107 wpFluent()
108 ->table('fluentform_form_meta')
109 ->where('id', $hasCount->id)
110 ->update([
111 'value' => intval($hasCount->value) + 1,
112 ]);
113 } else {
114 wpFluent()
115 ->table('fluentform_form_meta')
116 ->insert([
117 'value' => 1,
118 'form_id' => $formId,
119 'meta_key' => '_total_views',
120 ]);
121 }
122 }
123 }
124