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
109
110
111
112
113
114
115
116
117
118
119
|
<?php // phpcs:ignoreFile
namespace AutomateWoo;
if ( ! defined( 'ABSPATH' ) ) exit;
/** * @class Cart_Factory * @since 2.9 */ class Cart_Factory extends Factory {
static $model = 'AutomateWoo\Cart';
/** * @param int $cart_id * @return Cart|bool */ static function get( $cart_id ) { return parent::get( $cart_id ); }
/** * @param $guest_id * @return Cart|bool */ static function get_by_guest_id( $guest_id ) {
if ( ! $guest_id ) return false;
if ( Cache::exists( $guest_id, 'cart_guest_id' ) ) { return static::get( Cache::get( $guest_id, 'cart_guest_id' ) ); }
$cart = new Cart(); $cart->get_by( 'guest_id', $guest_id );
if ( ! $cart->exists ) { Cache::set( $guest_id, 0, 'cart_guest_id' ); return false; }
return $cart; }
/** * @param $user_id * @return Cart|bool */ static function get_by_user_id( $user_id ) {
if ( ! $user_id ) return false;
if ( Cache::exists( $user_id, 'cart_user_id' ) ) { return static::get( Cache::get( $user_id, 'cart_user_id' ) ); }
$cart = new Cart(); $cart->get_by( 'user_id', $user_id );
if ( ! $cart->exists ) { Cache::set( $user_id, 0, 'cart_user_id' ); return false; }
return $cart; }
/** * @param $token * @return Cart|bool */ static function get_by_token( $token ) {
if ( ! $token ) return false;
$cart = new Cart(); $cart->get_by( 'token', $token );
if ( ! $cart->exists ) { return false; }
return $cart; }
/** * @param Cart $cart */ static function update_cache( $cart ) { parent::update_cache( $cart );
if ( $cart->get_guest_id() ) { Cache::set( $cart->get_guest_id(), $cart->get_id(), 'cart_guest_id' ); }
if ( $cart->get_user_id() ) { Cache::set( $cart->get_user_id(), $cart->get_id(), 'cart_user_id' ); } }
/** * @param Cart $cart */ static function clean_cache( $cart ) { parent::clean_cache( $cart );
static::clear_cached_prop( $cart, 'guest_id', 'cart_guest_id' ); static::clear_cached_prop( $cart, 'user_id', 'cart_user_id' ); }
}
|