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

120 lines 3.1 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()
31 {
32 return $this->saveViewAnalytics();
33 }
34
35 /**
36 * Save form view analytics data
37 * @return void
38 */
39 private function saveViewAnalytics()
40 {
41 $formId = $this->app->request->get('formId');
42 if (!$form = wpFluent()->table('fluentform_forms')->find($formId)) {
43 return;
44 }
45
46 $userId = null;
47 if ($user = wp_get_current_user()) {
48 $userId = $user->ID;
49 }
50
51 $browser = new Browser;
52
53 $data = array(
54 'count' => 1,
55 'form_id' => $formId,
56 'user_id' => $userId,
57 'ip' => $this->app->request->getIp(),
58 'browser' => $browser->getBrowser(),
59 'platform' => $browser->getPlatform(),
60 'created_at' => date('Y-m-d H:i:s'),
61 'source_url' => $this->app->request->server('HTTP_REFERER'),
62 );
63
64 $query = wpFluent()
65 ->table('fluentform_form_analytics')
66 ->where('ip', $data['ip'])
67 ->where('form_id', $data['form_id'])
68 ->where('source_url', $data['source_url']);
69
70 try {
71 if (($record = $query->first())) {
72 $query->update(array('count' => ++$record->count));
73 } else {
74 wpFluent()->table('fluentform_form_analytics')->insert($data);
75 $this->increaseTotalViews($formId);
76 }
77
78 wp_send_json_success(array(
79 'status' => true,
80 ), 200);
81
82 } catch(\Exception $e) {
83 wp_send_json_success(array(
84 'status' => false,
85 ), 200);
86 }
87 }
88
89 /**
90 * Store (create/update) total view of a form
91 * @param int $formId
92 * @return void
93 */
94 private function increaseTotalViews($formId)
95 {
96 // check if meta is already exists or not
97 $hasCount = wpFluent()
98 ->table('fluentform_form_meta')
99 ->where('meta_key', '_total_views')
100 ->where('form_id', $formId)
101 -> first();
102
103 if($hasCount) {
104 wpFluent()
105 ->table('fluentform_form_meta')
106 ->where('id', $hasCount->id)
107 ->update(array(
108 'value' => intval($hasCount->value) + 1
109 ));
110 } else {
111 wpFluent()
112 ->table('fluentform_form_meta')
113 ->insert(array(
114 'value' => 1,
115 'form_id' => $formId,
116 'meta_key' => '_total_views'
117 ));
118 }
119 }
120 }