PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.17
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.17
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / classes / class-cartflows-session.php

class-cartflows-session.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.17, at classes/class-cartflows-session.php

246 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Session
4 *
5 * @package CartFlows
6 */
7
8 /**
9 * Initialization
10 *
11 * @since 1.0.0
12 */
13 class Cartflows_Session {
14
15
16 /**
17 * Member Variable
18 *
19 * @var instance
20 */
21 private static $instance;
22
23 /**
24 * Initiator
25 */
26 public static function get_instance() {
27 if ( ! isset( self::$instance ) ) {
28 self::$instance = new self;
29 }
30 return self::$instance;
31 }
32
33 /**
34 * Constructor
35 */
36 public function __construct() {
37
38 }
39
40 /**
41 * Set session
42 *
43 * @param int $flow_id flow ID.
44 * @param array $data trasient data.
45 */
46 function set_session( $flow_id, $data = array() ) {
47
48 if ( isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
49 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
50 } else {
51 $key = $flow_id . '_' . md5( time() . rand() );
52 }
53
54 // Set the browser cookie to expire in 30 minutes.
55 setcookie( 'cartflows_session_' . $flow_id, $key, time() + 30 * MINUTE_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
56
57 // Try to grab the transient from the database, if it exists.
58 $transient = $data;
59
60 // Store the transient, but expire in 30 minutes.
61 set_transient( 'cartflows_data_' . $key, $transient, 30 * MINUTE_IN_SECONDS );
62
63 wp_cache_set( 'cartflows_data_' . $key, $transient );
64
65 wcf()->logger->log( 'Flow-' . $flow_id . ' Session Set : ' . $key . ' ' . json_encode( $transient ) );
66 }
67
68 /**
69 * Update session
70 *
71 * @param int $flow_id flow ID.
72 * @param array $data trasient data.
73 */
74 function update_session( $flow_id, $data = array() ) {
75
76 if ( ! isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
77
78 $this->set_session( $flow_id, $data );
79 }
80
81 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
82
83 // Try to grab the transient from the database, if it exists.
84 $transient = get_transient( 'cartflows_data_' . $key );
85
86 // Set the browser cookie to expire in 30 minutes.
87 setcookie( 'cartflows_session_' . $flow_id, $key, time() + 30 * MINUTE_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
88
89 // Store the transient, but expire in 30 minutes.
90 set_transient( 'cartflows_data_' . $key, $transient, 30 * MINUTE_IN_SECONDS );
91
92 wp_cache_set( 'cartflows_data_' . $key, $transient );
93 }
94
95 /**
96 * Destroy session
97 *
98 * @param int $flow_id flow ID.
99 */
100 function destroy_session( $flow_id ) {
101
102 if ( isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
103
104 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
105
106 // Delete Transient.
107 delete_transient( 'cartflows_data_' . $key );
108
109 wp_cache_delete( 'cartflows_data_' . $key );
110
111 unset( $_COOKIE[ 'cartflows_session_' . $flow_id ] );
112
113 // empty value and expiration one hour before.
114 setcookie( 'cartflows_session_' . $flow_id, $key, time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
115
116 wcf()->logger->log( 'Flow-' . $flow_id . ' Session Destroyed : ' . $key );
117 }
118 }
119
120 /**
121 * Get session
122 */
123 function get_session() {
124
125 if ( isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
126
127 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
128
129 $data = get_transient( 'cartflows_data_' . $key );
130 }
131 }
132
133 /**
134 * Update transient data for cart flows.
135 *
136 * @param int $flow_id flow ID.
137 * @param array $data data.
138 */
139 function update_data( $flow_id, $data = array() ) {
140
141 if ( isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
142
143 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
144
145 // Try to grab the transient from the database, if it exists.
146 $transient = get_transient( 'cartflows_data_' . $key );
147
148 if ( ! is_array( $transient ) ) {
149 $transient = array();
150 }
151
152 $transient = array_merge( $transient, $data );
153
154 // Store the transient, but expire in 30 minutes.
155 set_transient( 'cartflows_data_' . $key, $transient, 30 * MINUTE_IN_SECONDS );
156
157 wp_cache_set( 'cartflows_data_' . $key, $transient );
158 }
159 }
160
161 /**
162 * Update transient data for cart flows.
163 *
164 * @param int $flow_id flow ID.
165 * @return bool
166 */
167 function get_data( $flow_id ) {
168
169 if ( isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
170
171 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
172
173 // Try to grab the transient from the database, if it exists.
174 $transient = get_transient( 'cartflows_data_' . $key );
175
176 if ( is_array( $transient ) ) {
177 return $transient;
178 }
179 }
180
181 return false;
182 }
183
184
185 /**
186 * Check if session is active.
187 *
188 * @param int $flow_id flow ID.
189 * @return bool
190 */
191 function is_active_session( $flow_id ) {
192
193 $is_active = false;
194
195 if ( isset( $_GET['wcf-sk'] ) && isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
196
197 $sk = sanitize_text_field( $_GET['wcf-sk'] );
198 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
199
200 if ( $sk === $key ) {
201
202 if ( isset( $_GET['wcf-order'] ) && isset( $_GET['wcf-key'] ) ) {
203
204 // Get the order.
205 $order_id = empty( $_GET['wcf-order'] ) ? 0 : intval( $_GET['wcf-order'] );
206 $order_key = empty( $_GET['wcf-key'] ) ? '' : wc_clean( wp_unslash( $_GET['wcf-key'] ) );
207
208 if ( $order_id > 0 ) {
209
210 $order = wc_get_order( $order_id );
211
212 if ( $order && $order->get_order_key() === $order_key ) {
213 $is_active = true;
214 }
215 }
216 }
217 }
218 }
219
220 return $is_active;
221 }
222
223 /**
224 * Get session key for flow
225 *
226 * @param int $flow_id flow ID.
227 * @return bool
228 */
229 function get_session_key( $flow_id ) {
230
231 if ( isset( $_COOKIE[ 'cartflows_session_' . $flow_id ] ) ) {
232
233 $key = (string) $_COOKIE[ 'cartflows_session_' . $flow_id ];
234
235 return $key;
236 }
237
238 return false;
239 }
240 }
241
242 /**
243 * Kicking this off by calling 'get_instance()' method
244 */
245 Cartflows_Session::get_instance();
246