|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* GetPaid_Subscription_Data_Store class file. |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
8
|
|
|
exit; |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Subscription Data Store: Stored in a custom table. |
|
13
|
|
|
* |
|
14
|
|
|
* @version 1.0.19 |
|
15
|
|
|
*/ |
|
16
|
|
|
class GetPaid_Subscription_Data_Store { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A map of database fields to data types. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 1.0.19 |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $database_fields_to_data_type = array( |
|
25
|
|
|
'id' => '%d', |
|
26
|
|
|
'customer_id' => '%d', |
|
27
|
|
|
'frequency' => '%d', |
|
28
|
|
|
'period' => '%s', |
|
29
|
|
|
'initial_amount' => '%s', |
|
30
|
|
|
'recurring_amount' => '%s', |
|
31
|
|
|
'bill_times' => '%d', |
|
32
|
|
|
'transaction_id' => '%s', |
|
33
|
|
|
'parent_payment_id' => '%d', |
|
34
|
|
|
'product_id' => '%d', |
|
35
|
|
|
'created' => '%s', |
|
36
|
|
|
'expiration' => '%s', |
|
37
|
|
|
'trial_period' => '%s', |
|
38
|
|
|
'status' => '%s', |
|
39
|
|
|
'profile_id' => '%s', |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
/* |
|
43
|
|
|
|-------------------------------------------------------------------------- |
|
44
|
|
|
| CRUD Methods |
|
45
|
|
|
|-------------------------------------------------------------------------- |
|
46
|
|
|
*/ |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Method to create a new subscription in the database. |
|
50
|
|
|
* |
|
51
|
|
|
* @param WPInv_Subscription $subscription Subscription object. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function create( &$subscription ) { |
|
54
|
|
|
global $wpdb; |
|
55
|
|
|
|
|
56
|
|
|
if ( is_null( $subscription->get_date_created() ) ) { |
|
|
|
|
|
|
57
|
|
|
$subscription->set_date_created( current_time('mysql') ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$values = array(); |
|
61
|
|
|
$formats = array(); |
|
62
|
|
|
|
|
63
|
|
|
$fields = $this->database_fields_to_data_type; |
|
64
|
|
|
unset( $fields['id'] ); |
|
65
|
|
|
|
|
66
|
|
|
foreach ( $fields as $key => $format ) { |
|
67
|
|
|
$method = "get_$key"; |
|
68
|
|
|
$values[] = $subscription->$method( 'edit' ); |
|
69
|
|
|
$formats[] = $format; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $fields, $formats ); |
|
73
|
|
|
|
|
74
|
|
|
if ( $result ) { |
|
75
|
|
|
$subscription->set_id( $wpdb->insert_id ); |
|
76
|
|
|
$subscription->apply_changes(); |
|
77
|
|
|
wp_cache_delete( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
78
|
|
|
do_action( 'getpaid_new_subscription', $subscription ); |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Method to read a subscription from the database. |
|
87
|
|
|
* |
|
88
|
|
|
* @param WPInv_Subscription $subscription Subscription object. |
|
89
|
|
|
* |
|
90
|
|
|
*/ |
|
91
|
|
|
public function read( &$subscription ) { |
|
92
|
|
|
global $wpdb; |
|
93
|
|
|
|
|
94
|
|
|
$subscription->set_defaults(); |
|
95
|
|
|
|
|
96
|
|
|
if ( ! $subscription->get_id() ) { |
|
97
|
|
|
$subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
98
|
|
|
$subscription->set_id( 0 ); |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Maybe retrieve from the cache. |
|
103
|
|
|
$raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
104
|
|
|
|
|
105
|
|
|
// If not found, retrieve from the db. |
|
106
|
|
|
if ( false === $raw_subscription ) { |
|
107
|
|
|
|
|
108
|
|
|
$raw_subscription = $wpdb->get_row( |
|
109
|
|
|
$wpdb->prepare( |
|
110
|
|
|
"SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
111
|
|
|
$subscription->get_id() |
|
112
|
|
|
) |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
// Update the cache with our data |
|
116
|
|
|
wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ( ! $raw_subscription ) { |
|
121
|
|
|
throw new Exception( __( 'Invalid subscription ID.', 'invoicing' ) ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
125
|
|
|
$method = "set_$key"; |
|
126
|
|
|
$subscription->$method( $raw_subscription->$key ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$subscription->set_object_read( true ); |
|
130
|
|
|
do_action( 'getpaid_read_subscription', $subscription->get_id(), $subscription ); |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Method to update a subscription in the database. |
|
136
|
|
|
* |
|
137
|
|
|
* @param WPInv_Subscription $subscription Subscription object. |
|
138
|
|
|
*/ |
|
139
|
|
|
public function update( &$subscription ) { |
|
140
|
|
|
global $wpdb; |
|
141
|
|
|
|
|
142
|
|
|
if ( null === $subscription->get_date_created( 'edit' ) ) { |
|
|
|
|
|
|
143
|
|
|
$subscription->set_date_created( current_time('mysql') ); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$changes = $subscription->get_changes(); |
|
147
|
|
|
$values = array(); |
|
148
|
|
|
$format = array(); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
151
|
|
|
if ( array_key_exists( $key, $changes ) ) { |
|
152
|
|
|
$method = "get_$key"; |
|
153
|
|
|
$values[] = $subscription->$method( 'edit' ); |
|
154
|
|
|
$formats[] = $format; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if ( empty( $values ) ) { |
|
159
|
|
|
return; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$wpdb->update( |
|
163
|
|
|
$wpdb->prefix . 'wpinv_subscriptions', |
|
164
|
|
|
$values, |
|
165
|
|
|
array( |
|
166
|
|
|
'id' => $subscription->get_id(), |
|
167
|
|
|
), |
|
168
|
|
|
$formats |
|
|
|
|
|
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
// Apply the changes. |
|
172
|
|
|
$subscription->apply_changes(); |
|
173
|
|
|
|
|
174
|
|
|
// Delete cache. |
|
175
|
|
|
wp_cache_delete( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
176
|
|
|
|
|
177
|
|
|
// Fire a hook. |
|
178
|
|
|
do_action( 'getpaid_update_subscription', $subscription->get_id(), $subscription ); |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Method to delete a subscription from the database. |
|
184
|
|
|
* |
|
185
|
|
|
* @param WPInv_Subscription $subscription |
|
186
|
|
|
*/ |
|
187
|
|
|
public function delete( &$subscription ) { |
|
188
|
|
|
global $wpdb; |
|
189
|
|
|
|
|
190
|
|
|
$wpdb->query( |
|
191
|
|
|
$wpdb->prepare( |
|
192
|
|
|
"DELETE FROM {$wpdb->prefix}getpaid_subscriptions |
|
193
|
|
|
WHERE id = %d", |
|
194
|
|
|
$subscription->get_id() |
|
195
|
|
|
) |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
// Delete cache. |
|
199
|
|
|
wp_cache_delete( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
200
|
|
|
|
|
201
|
|
|
// Fire a hook. |
|
202
|
|
|
do_action( 'getpaid_delete_subscription', $subscription->get_id(), $subscription ); |
|
203
|
|
|
|
|
204
|
|
|
$subscription->set_id( 0 ); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/* |
|
208
|
|
|
|-------------------------------------------------------------------------- |
|
209
|
|
|
| Additional Methods |
|
210
|
|
|
|-------------------------------------------------------------------------- |
|
211
|
|
|
*/ |
|
212
|
|
|
} |
|
213
|
|
|
|