Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/ecommerce/GuestCheckout/CookieManager.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Handle guest cookies
4
+
*
5
+
* @package TutorPro\GuestCheckout
6
+
* @author Themeum <support@themeum.com>
7
+
* @link https://themeum.com
8
+
* @since 3.3.0
9
+
*/
10
+
11
+
namespace TutorPro\Ecommerce\GuestCheckout;
12
+
13
+
use TUTOR\Input;
14
+
15
+
if ( ! defined( 'ABSPATH' ) ) {
16
+
exit;
17
+
}
18
+
19
+
/**
20
+
* Manage guest cookies
21
+
*/
22
+
class CookieManager {
23
+
24
+
const COOKIE_BILLING_COUNTRY = 'tutor_guest_billing_country';
25
+
const COOKIE_BILLING_STATE = 'tutor_guest_billing_state';
26
+
const COOKIE_GUEST_CART = 'tutor_guest_cart';
27
+
28
+
/**
29
+
* Cookie expiry time in seconds (30 days)
30
+
*
31
+
* @since 3.3.0
32
+
*
33
+
* @var int
34
+
*/
35
+
const COOKIE_EXPIRY = DAY_IN_SECONDS * 30;
36
+
37
+
/**
38
+
* Set cart item
39
+
*
40
+
* @since 3.3.0
41
+
*
42
+
* @param int $item_id Item id to set.
43
+
*
44
+
* @throws \Exception If the course is already in the cart.
45
+
*
46
+
* @return void
47
+
*/
48
+
public static function set_cart_item( $item_id ) {
49
+
$current_cart = self::get_cart_items();
50
+
51
+
if ( ! in_array( $item_id, $current_cart ) ) {
52
+
$current_cart[] = $item_id;
53
+
setcookie(
54
+
self::COOKIE_GUEST_CART,
55
+
json_encode( $current_cart ),
56
+
time() + self::COOKIE_EXPIRY,
57
+
'/',
58
+
'',
59
+
false,
60
+
true
61
+
);
62
+
$_COOKIE[ self::COOKIE_GUEST_CART ] = json_encode( $current_cart );
63
+
} else {
64
+
throw new \Exception( __( 'The course is already in the cart.', 'tutor-pro' ) );
65
+
}
66
+
}
67
+
68
+
/**
69
+
* Get all course IDs from the cookie
70
+
*
71
+
* @since 3.3.0
72
+
*
73
+
* @return array
74
+
*/
75
+
public static function get_cart_items() {
76
+
if ( isset( $_COOKIE[ self::COOKIE_GUEST_CART ] ) ) {
77
+
$cart = json_decode( wp_unslash( $_COOKIE[ self::COOKIE_GUEST_CART ] ), true );
78
+
79
+
// Ensure the cart is always an array.
80
+
return is_array( $cart ) ? $cart : array();
81
+
}
82
+
83
+
return array();
84
+
}
85
+
86
+
/**
87
+
* Update cart as per data provided
88
+
*
89
+
* @since 3.3.0
90
+
*
91
+
* @param array $cart_data Cart data to update.
92
+
*
93
+
* @return void
94
+
*/
95
+
public static function update_cart( array $cart_data ) {
96
+
$cart_data = wp_json_encode( $cart_data );
97
+
setcookie(
98
+
self::COOKIE_GUEST_CART,
99
+
$cart_data,
100
+
time() + self::COOKIE_EXPIRY,
101
+
'/',
102
+
'',
103
+
false,
104
+
true
105
+
);
106
+
$_COOKIE[ self::COOKIE_GUEST_CART ] = $cart_data;
107
+
}
108
+
109
+
/**
110
+
* Set the billing country in a cookie.
111
+
*
112
+
* @since 3.3.0
113
+
*
114
+
* @param string $country_name The country code to set.
115
+
* @param int $expiry Expiry time in seconds (default: 1 day).
116
+
*/
117
+
public static function set_billing_country( $country_name, $expiry = DAY_IN_SECONDS ) {
118
+
if ( ! empty( $country_name ) ) {
119
+
setcookie( self::COOKIE_BILLING_COUNTRY, $country_name, time() + $expiry, '/', '', false, true );
120
+
$_COOKIE[ self::COOKIE_BILLING_COUNTRY ] = $country_name;
121
+
}
122
+
}
123
+
124
+
/**
125
+
* Set the billing state in a cookie.
126
+
*
127
+
* @since 3.3.0
128
+
*
129
+
* @param string $state_name The state code to set.
130
+
* @param int $expiry Expiry time in seconds (default: 1 day).
131
+
*/
132
+
public static function set_billing_state( $state_name, $expiry = DAY_IN_SECONDS ) {
133
+
if ( ! empty( $state_name ) ) {
134
+
setcookie( self::COOKIE_BILLING_STATE, $state_name, time() + $expiry, '/', '', false, true );
135
+
$_COOKIE[ self::COOKIE_BILLING_STATE ] = $state_name;
136
+
}
137
+
}
138
+
139
+
/**
140
+
* Get the billing country in a cookie.
141
+
*
142
+
* @since 3.3.0
143
+
*
144
+
* @return string
145
+
*/
146
+
public static function get_billing_country() {
147
+
return isset( $_COOKIE[ self::COOKIE_BILLING_COUNTRY ] ) ? Input::sanitize( $_COOKIE[ self::COOKIE_BILLING_COUNTRY ] ) : '';
148
+
}
149
+
150
+
/**
151
+
* Get the billing state in a cookie.
152
+
*
153
+
* @since 3.3.0
154
+
*
155
+
* @return string
156
+
*/
157
+
public static function get_billing_state() {
158
+
return isset( $_COOKIE[ self::COOKIE_BILLING_STATE ] ) ? Input::sanitize( $_COOKIE[ self::COOKIE_BILLING_STATE ] ) : '';
159
+
}
160
+
161
+
/**
162
+
* Clear the billing country cookie.
163
+
*
164
+
* @since 3.3.0
165
+
*/
166
+
public static function clear_billing_country() {
167
+
setcookie( self::COOKIE_BILLING_COUNTRY, '', time() - 3600, '/', '', false, true );
168
+
unset( $_COOKIE[ self::COOKIE_BILLING_COUNTRY ] );
169
+
}
170
+
171
+
/**
172
+
* Clear the billing state cookie.
173
+
*
174
+
* @since 3.3.0
175
+
*/
176
+
public static function clear_billing_state() {
177
+
setcookie( self::COOKIE_BILLING_STATE, '', time() - 3600, '/', '', false, true );
178
+
unset( $_COOKIE[ self::COOKIE_BILLING_STATE ] );
179
+
}
180
+
181
+
/**
182
+
* Clear the billing state cookie.
183
+
*
184
+
* @since 3.3.0
185
+
*/
186
+
public static function clear_cart() {
187
+
setcookie( self::COOKIE_GUEST_CART, '', time() - 3600, '/', '', false, true );
188
+
unset( $_COOKIE[ self::COOKIE_GUEST_CART ] );
189
+
}
190
+
191
+
/**
192
+
* Clear all the cookies related with guest checkout.
193
+
*
194
+
* @since 3.3.0
195
+
*/
196
+
public static function clear_all() {
197
+
self::clear_billing_country();
198
+
self::clear_billing_state();
199
+
self::clear_cart();
200
+
}
201
+
}
202
+