PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 2.7.0
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v2.7.0
2.7.0 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 All 43 releases
sellkit / includes / funnel / contacts / base-contacts.php

base-contacts.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 2.7.0, at includes/funnel/contacts/base-contacts.php

200 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace Sellkit\Funnel\Contacts;
5
6 use Sellkit\Database;
7 use Sellkit_Funnel;
8
9 defined( 'ABSPATH' ) || die();
10
11 /**
12 * Base contacts class.
13 *
14 * @since 1.5.0
15 */
16 class Base_Contacts {
17
18 /**
19 * SellKit database.
20 *
21 * @var Database
22 * @since 1.5.0
23 */
24 public $db;
25
26 /**
27 * SellKit funnel class.
28 *
29 * @var Object|Sellkit_Funnel|null
30 * @since 1.5.0
31 */
32 public $sellkit_funnel;
33
34 /**
35 * Base_Contacts constructor.
36 *
37 * @since 1.5.0
38 */
39 public function __construct() {
40 $this->sellkit_funnel = Sellkit_Funnel::get_instance();
41 $this->db = new Database();
42 }
43
44 /**
45 * Created a new log.
46 *
47 * @since 1.5.0
48 */
49 public function add_new_log() {
50 $analytics_id = $this->db->insert( 'funnel_contact', [
51 'funnel_id' => $this->sellkit_funnel->funnel_id,
52 'user_id' => get_current_user_id(),
53 'updated_at' => time(),
54 'created_at' => time(),
55 ] );
56
57 self::start_session();
58
59 $_SESSION['entered_funnel_id'] = $analytics_id;
60
61 self::end_session();
62 }
63
64 /**
65 * Adds accept status.
66 *
67 * @param array $upsell_downsell_data Upsell and downsell data.
68 * @since 1.5.0
69 */
70 public static function step_is_passed( $upsell_downsell_data = [] ) {
71 self::start_session();
72
73 $database = new Database();
74 $funnel = Sellkit_Funnel::get_instance();
75
76 if ( ! empty( $upsell_downsell_data ) ) {
77 self::handle_contact_data_on_upsell_donwsell( $upsell_downsell_data, $database );
78 return;
79 }
80
81 if ( empty( $funnel->funnel_id ) ) {
82 self::end_session();
83 return;
84 }
85
86 $type = $funnel->current_step_data['type']['key'];
87 $old_values = [];
88
89 if ( ! isset( $_SESSION['entered_funnel_id'] ) ) {
90 self::end_session();
91 return;
92 }
93
94 $contact_id = absint( $_SESSION['entered_funnel_id'] );
95
96 self::end_session();
97
98 // Getting old data.
99 $result = $database->get( 'funnel_contact', [ 'id' => $contact_id ] );
100
101 if ( ! empty( $result[0][ $type ] ) ) {
102 $old_values = unserialize( $result[ 0 ][ $type ] ); // phpcs:ignore
103 }
104
105 $new_values = array_merge( $old_values, [ $funnel->current_step_data['page_id'] ] );
106
107 $database->update(
108 'funnel_contact',
109 [ $funnel->current_step_data['type']['key'] => $new_values ],
110 [ 'id' => $contact_id ]
111 );
112 }
113
114 /**
115 * Handles contact data on upsell and downsell.
116 *
117 * @param array $upsell_downsell_data Upsell and downsell data.
118 * @param object $database Database object.
119 * @since 1.9.2
120 */
121 public static function handle_contact_data_on_upsell_donwsell( $upsell_downsell_data, $database ) {
122 self::start_session();
123
124 if ( ! isset( $_SESSION['entered_funnel_id'] ) ) {
125 self::end_session();
126 return;
127 }
128
129 $contact_id = absint( $_SESSION['entered_funnel_id'] );
130
131 self::end_session();
132
133 $result = $database->get( 'funnel_contact', [ 'id' => $contact_id ] );
134
135 $old_values = [];
136
137 if ( ! empty( $result[0][ $upsell_downsell_data['key'] ] ) ) {
138 $old_values = unserialize( $result[ 0 ][ $upsell_downsell_data['key'] ] ); // phpcs:ignore
139 }
140
141 $new_values = array_merge( $old_values, [ $upsell_downsell_data['page_id'] ] );
142
143 $database->update(
144 'funnel_contact',
145 [ $upsell_downsell_data['key'] => $new_values ],
146 [ 'id' => $contact_id ]
147 );
148 }
149
150 /**
151 * Adds price to total spent column of this funnel.
152 *
153 * @param string $price Price value.
154 * @param int $funnel_id funnel id.
155 * @since 1.5.0
156 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
157 */
158 public function add_total_spent( $price, $funnel_id ) {
159 self::start_session();
160
161 $contact_id = isset( $_SESSION['entered_funnel_id'] ) ? absint( $_SESSION['entered_funnel_id'] ) : 0;
162
163 self::end_session();
164
165 // Getting old data.
166 $result = $this->db->get( 'funnel_contact', [ 'id' => $contact_id ] );
167 $old_spent = ! empty( $result[0]['total_spent'] ) ? $result[0]['total_spent'] : 0;
168
169 $new_spent = floatval( $old_spent ) + floatval( $price );
170
171 $this->db->update(
172 'funnel_contact',
173 [ 'total_spent' => $new_spent ],
174 [ 'id' => $contact_id ]
175 );
176 }
177
178 /**
179 * Starts a session if not already started.
180 *
181 * @since 2.6.0
182 */
183 public static function start_session() {
184 if ( ! headers_sent() && session_status() !== PHP_SESSION_ACTIVE ) {
185 session_start();
186 }
187 }
188
189 /**
190 * Ends the session if it is active.
191 *
192 * @since 2.6.0
193 */
194 public static function end_session() {
195 if ( session_status() === PHP_SESSION_ACTIVE ) {
196 session_write_close();
197 }
198 }
199 }
200