PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.3.1
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.3.1
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / funnel / analytics / data-updater.php

data-updater.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.3.1, at includes/funnel/analytics/data-updater.php

212 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Funnel\Analytics;
4
5 use Sellkit\Database;
6
7 defined( 'ABSPATH' ) || die();
8
9 /**
10 * Class Data updater.
11 *
12 * @since 1.1.0
13 */
14 class Data_Updater {
15
16 /**
17 * Funnel id.
18 *
19 * @var string.
20 */
21 public $funnel_id;
22
23 /**
24 * Funnel id.
25 *
26 * @var string.
27 */
28 public $today_min_time;
29
30 /**
31 * Data_Updater constructor.
32 *
33 * @since 1.1.0
34 */
35 public function __construct() {
36 $this->today_min_time = strtotime( date( 'Y-m-d 0:0:0' ) );
37 }
38
39 /**
40 * Sets funnel id.
41 *
42 * @since 1.1.0
43 * @param int $funnel_id Funnel id.
44 */
45 public function set_funnel_id( $funnel_id ) {
46 $this->funnel_id = $funnel_id;
47 }
48
49 /**
50 * Adds new visit.
51 *
52 * @since 1.1.0
53 * @param bool $is_unique The type of visit.
54 */
55 public function add_new_visit( $is_unique = false ) {
56 $data = self::has_funnel_data();
57 $visit_column = 'visit';
58
59 if ( $is_unique ) {
60 $visit_column = 'unique_visit';
61 }
62
63 if ( $data ) {
64 global $wpdb;
65
66 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
67
68 $sql = "UPDATE `$table` SET {$visit_column} = %d WHERE funnel_id = %d AND applied_at > %s";
69
70 $this->check_current_query = false;
71
72 //phpcs:disable
73 $wpdb->query(
74 $wpdb->prepare( $sql, $data[ $visit_column ] + 1, $this->funnel_id, $this->today_min_time )
75 );
76 //phpcs:enable
77
78 return;
79 }
80
81 $this->insert_applied_funnel();
82 }
83
84 /**
85 * Check if has funnel data.
86 *
87 * @since 1.1.0
88 */
89 private function has_funnel_data() {
90 global $wpdb;
91
92 $database_prefix = Database::DATABASE_PREFIX;
93
94 //phpcs:disable
95 $result = $wpdb->get_results(
96 $wpdb->prepare(
97 "SELECT * from {$wpdb->prefix}{$database_prefix}applied_funnel
98 WHERE funnel_id = %s and applied_at > %s",
99 $this->funnel_id,
100 $this->today_min_time
101 )
102 , ARRAY_A );
103 //phpcs:enable
104
105 if ( empty( $result[0] ) ) {
106 return false;
107 }
108
109 return $result[0];
110 }
111
112 /**
113 * Inserts a new funnel query.
114 *
115 * @since 1.1.0
116 */
117 private function insert_applied_funnel() {
118 sellkit()->db->insert( 'applied_funnel', [
119 'visit' => 1,
120 'unique_visit' => 0,
121 'funnel_id' => $this->funnel_id,
122 'applied_at' => time(),
123 ] );
124 }
125
126 /**
127 * Adds new finish log.
128 *
129 * @since 1.1.0
130 */
131 public function add_new_finish_log() {
132 $data = self::has_funnel_data();
133
134 if ( $data ) {
135 global $wpdb;
136
137 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
138
139 $sql = "UPDATE `$table` SET is_finished_number = %d WHERE funnel_id = %d AND applied_at > %s";
140
141 //phpcs:disable
142 $wpdb->query(
143 $wpdb->prepare( $sql, $data[ 'is_finished_number' ] + 1, $this->funnel_id, $this->today_min_time )
144 );
145 //phpcs:enable
146 }
147 }
148
149 /**
150 * Adds new start log.
151 *
152 * @since 1.1.0
153 */
154 public function add_new_start_log() {
155 $data = self::has_funnel_data();
156
157 if ( empty( $data ) ) {
158 return;
159 }
160
161 global $wpdb;
162
163 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
164
165 $sql = "UPDATE `$table` SET is_started_number = %d WHERE funnel_id = %d AND applied_at > %s";
166
167 //phpcs:disable
168 $wpdb->query(
169 $wpdb->prepare( $sql, $data[ 'is_started_number' ] + 1, $this->funnel_id, $this->today_min_time )
170 );
171 //phpcs:enable
172 }
173
174 /**
175 * Adds new order log.
176 *
177 * @since 1.1.0
178 * @param object $order Order object.
179 * @param string $type Type of order.
180 */
181 public function add_new_order_log( $order, $type = false ) {
182 $data = self::has_funnel_data();
183
184 if ( empty( $data ) ) {
185 return;
186 }
187
188 global $wpdb;
189
190 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
191
192 $sql = "UPDATE `$table` SET `orders` = %d, revenue = %d WHERE funnel_id = %d AND applied_at > %s";
193
194 if ( empty( $data['revenue'] ) ) {
195 $data['revenue'] = 0;
196 }
197
198 //phpcs:disable
199 $wpdb->query(
200 $wpdb->prepare( $sql, $data[ 'orders' ] + 1, $data[ 'revenue' ] + $order->get_total(), $this->funnel_id, $this->today_min_time )
201 );
202
203 if ( 'upsell' === $type ) {
204 $wpdb->query(
205 $wpdb->prepare( "UPDATE `$table` SET upsell_revenue = %s WHERE funnel_id = %d AND applied_at > %s", $data[ 'upsell_revenue' ] + $order->get_total(), $this->funnel_id, $this->today_min_time )
206 );
207 }
208
209 //phpcs:enable
210 }
211 }
212