forked from viewup/up-wp-cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
108 lines (92 loc) · 2.13 KB
/
init.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Handles the global $cart init, SESSIONS and COOKIES
*/
/**
* Initialize global CART and SESSION
*/
function upcart_init() {
// returns if no cass
if ( ! class_exists( UPWPCART_CLASS_NAME ) ) {
return;
}
global $cart;
/**
* Start session
*
* The cart must stay on the session to persists.
*/
// check if user passed cart cookie
if ( isset( $_COOKIE[ UPWPCART_COOKIE_NAME ] ) ) {
// start session
upwpcart_session_start();
// and get session's cart
$cart = $_SESSION[ UPWPCART_SESSION_NAME ];
// if no cart in session
if ( ! $cart ) {
// create new cart and set in session
$cart = upwpcart_new_main_cart();
$_SESSION[ UPWPCART_SESSION_NAME ] = $cart;
}
} else {
// if no cookie, creates empty cart
$cart = upwpcart_new_main_cart();
}
}
/**
* Initialize REST API
*/
function upcart_rest_api_init() {
global $cartApi;
global $cart;
if ( ! $cartApi ) {
$cartApi = new WPCartAPI( $cart );
}
$cartApi->register_routes();
}
/**
* Detect the main cart update and updates COOKIE
*
* @param WPCart $cart
*/
function upcart_cookie_update( $cart ) {
// checks if is the main cart instance
if ( $cart->getID() != UPWPCART_MAIN_ID ) {
return;
}
// checks if the cart is empty or has data
if ( $cart->isEmpty() ) {
// unset cookie if empty
unset( $_COOKIE[ UPWPCART_COOKIE_NAME ] );
setcookie( UPWPCART_COOKIE_NAME, null, - 3600, '/' );
} else {
// refresh cookie and session if not empty
setcookie( UPWPCART_COOKIE_NAME, 1, 0, '/' );
upwpcart_session_start();
$_SESSION[ UPWPCART_SESSION_NAME ] = $cart;
}
}
add_action( 'rest_api_init', 'upcart_rest_api_init' );
add_action( 'init', 'upcart_init' );
add_action( 'upwpcart_update', 'upcart_cookie_update' );
// HELPERS
/**
* Initialize session
* @return bool if is initialized or was already initialized
*/
function upwpcart_session_start() {
if ( ! session_id() ) {
session_start();
return true;
}
return false;
}
/**
* get a new main instance of cart (with ID and configs)
* @return WPCart
*/
function upwpcart_new_main_cart() {
return new WPCart( array(
'ID' => UPWPCART_MAIN_ID
) );
}