PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.14
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.14, at app/Modules/Form/Analytics.php

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