PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
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 trunk, at includes/funnel/analytics/data-updater.php

247 lines 4.8 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 * Current query.
32 *
33 * @var boolean.
34 */
35 public $check_current_query;
36
37 /**
38 * Data_Updater constructor.
39 *
40 * @since 1.1.0
41 */
42 public function __construct() {
43 $this->today_min_time = strtotime( gmdate( 'Y-m-d 0:0:0' ) );
44 }
45
46 /**
47 * Sets funnel id.
48 *
49 * @since 1.1.0
50 * @param int $funnel_id Funnel id.
51 */
52 public function set_funnel_id( $funnel_id ) {
53 $this->funnel_id = $funnel_id;
54 }
55
56 /**
57 * Adds new visit.
58 *
59 * @since 1.1.0
60 * @param bool $is_unique The type of visit.
61 */
62 public function add_new_visit( $is_unique = false ) {
63 $data = self::has_funnel_data();
64 $visit_column = 'visit';
65
66 if ( $is_unique ) {
67 $visit_column = 'unique_visit';
68 }
69
70 if ( $data ) {
71 global $wpdb;
72
73 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
74
75 $sql = "UPDATE `$table` SET {$visit_column} = %d WHERE funnel_id = %d AND applied_at > %s";
76
77 $this->check_current_query = false;
78
79 //phpcs:disable
80 $wpdb->query(
81 $wpdb->prepare( $sql, $data[ $visit_column ] + 1, $this->funnel_id, $this->today_min_time )
82 );
83 //phpcs:enable
84
85 return;
86 }
87
88 $this->insert_applied_funnel();
89 }
90
91 /**
92 * Check if has funnel data.
93 *
94 * @since 1.1.0
95 */
96 private function has_funnel_data() {
97 global $wpdb;
98
99 $database_prefix = Database::DATABASE_PREFIX;
100
101 //phpcs:disable
102 $result = $wpdb->get_results(
103 $wpdb->prepare(
104 "SELECT * from {$wpdb->prefix}{$database_prefix}applied_funnel
105 WHERE funnel_id = %s and applied_at > %s",
106 $this->funnel_id,
107 $this->today_min_time
108 )
109 , ARRAY_A );
110 //phpcs:enable
111
112 if ( empty( $result[0] ) ) {
113 return false;
114 }
115
116 return $result[0];
117 }
118
119 /**
120 * Inserts a new funnel query.
121 *
122 * @since 1.1.0
123 */
124 private function insert_applied_funnel() {
125 sellkit()->db->insert( 'applied_funnel', [
126 'visit' => 1,
127 'unique_visit' => 0,
128 'funnel_id' => $this->funnel_id,
129 'applied_at' => time(),
130 ] );
131 }
132
133 /**
134 * Adds new finish log.
135 *
136 * @since 1.1.0
137 */
138 public function add_new_finish_log() {
139 $data = self::has_funnel_data();
140
141 if ( $data ) {
142 global $wpdb;
143
144 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
145
146 $sql = "UPDATE `$table` SET is_finished_number = %d WHERE funnel_id = %d AND applied_at > %s";
147
148 //phpcs:disable
149 $wpdb->query(
150 $wpdb->prepare( $sql, $data[ 'is_finished_number' ] + 1, $this->funnel_id, $this->today_min_time )
151 );
152 //phpcs:enable
153 }
154 }
155
156 /**
157 * Adds new start log.
158 *
159 * @since 1.1.0
160 */
161 public function add_new_start_log() {
162 $data = self::has_funnel_data();
163
164 if ( empty( $data ) ) {
165 return;
166 }
167
168 global $wpdb;
169
170 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
171
172 $sql = "UPDATE `$table` SET is_started_number = %d WHERE funnel_id = %d AND applied_at > %s";
173
174 //phpcs:disable
175 $wpdb->query(
176 $wpdb->prepare( $sql, $data[ 'is_started_number' ] + 1, $this->funnel_id, $this->today_min_time )
177 );
178 //phpcs:enable
179 }
180
181 /**
182 * Adds new order log.
183 *
184 * @since 1.1.0
185 * @param object $order Order object.
186 * @param string $type Type of order.
187 */
188 public function add_new_order_log( $order, $type = false ) {
189 $data = self::has_funnel_data();
190
191 if ( empty( $data ) ) {
192 return;
193 }
194
195 global $wpdb;
196
197 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
198
199 $sql = "UPDATE `$table` SET `orders` = %d, revenue = %d WHERE funnel_id = %d AND applied_at > %s";
200
201 if ( empty( $data['revenue'] ) ) {
202 $data['revenue'] = 0;
203 }
204
205 //phpcs:disable
206 $wpdb->query(
207 $wpdb->prepare( $sql, $data[ 'orders' ] + 1, $data[ 'revenue' ] + $order->get_total(), $this->funnel_id, $this->today_min_time )
208 );
209
210 if ( 'upsell' === $type ) {
211 $wpdb->query(
212 $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 )
213 );
214 }
215
216 //phpcs:enable
217 }
218
219 /**
220 * Adds new order log for accepted upsell.
221 *
222 * @since 1.9.2
223 * @param double|int $total Upsell total price.
224 */
225 public function add_new_upsell_revenue_log( $total ) {
226 $data = self::has_funnel_data();
227
228 if ( empty( $data ) ) {
229 return;
230 }
231
232 global $wpdb;
233
234 $table = $wpdb->prefix . Database::DATABASE_PREFIX . 'applied_funnel';
235
236 if ( empty( $data['revenue'] ) ) {
237 $data['revenue'] = 0;
238 }
239
240 //phpcs:disable
241 $wpdb->query(
242 $wpdb->prepare( "UPDATE `$table` SET upsell_revenue = %s WHERE funnel_id = %d AND applied_at > %s", $data[ 'upsell_revenue' ] + $total, $this->funnel_id, $this->today_min_time )
243 );
244 //phpcs:enable
245 }
246 }
247