|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* GetPaid_Invoice_Data_Store class file. |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
8
|
|
|
exit; |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Invoice Data Store: Stored in CPT. |
|
13
|
|
|
* |
|
14
|
|
|
* @version 1.0.19 |
|
15
|
|
|
*/ |
|
16
|
|
|
class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Data stored in meta keys, but not considered "meta" for a discount. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 1.0.19 |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $internal_meta_keys = array( |
|
25
|
|
|
'_wpi_discount_code', |
|
26
|
|
|
'_wpi_discount_amount', |
|
27
|
|
|
'_wpi_discount_start', |
|
28
|
|
|
'_wpi_discount_expiration', |
|
29
|
|
|
'_wpi_discount_type', |
|
30
|
|
|
'_wpi_discount_uses', |
|
31
|
|
|
'_wpi_discount_is_single_use', |
|
32
|
|
|
'_wpi_discount_items', |
|
33
|
|
|
'_wpi_discount_excluded_items', |
|
34
|
|
|
'_wpi_discount_max_uses', |
|
35
|
|
|
'_wpi_discount_is_recurring', |
|
36
|
|
|
'_wpi_discount_min_total', |
|
37
|
|
|
'_wpi_discount_max_total', |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* A map of meta keys to data props. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 1.0.19 |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $meta_key_to_props = array( |
|
48
|
|
|
'_wpi_discount_code' => 'code', |
|
49
|
|
|
'_wpi_discount_amount' => 'amount', |
|
50
|
|
|
'_wpi_discount_start' => 'start', |
|
51
|
|
|
'_wpi_discount_expiration' => 'expiration', |
|
52
|
|
|
'_wpi_discount_type' => 'type', |
|
53
|
|
|
'_wpi_discount_uses' => 'uses', |
|
54
|
|
|
'_wpi_discount_is_single_use' => 'is_single_use', |
|
55
|
|
|
'_wpi_discount_items' => 'items', |
|
56
|
|
|
'_wpi_discount_excluded_items' => 'excluded_items', |
|
57
|
|
|
'_wpi_discount_max_uses' => 'max_uses', |
|
58
|
|
|
'_wpi_discount_is_recurring' => 'is_recurring', |
|
59
|
|
|
'_wpi_discount_min_total' => 'min_total', |
|
60
|
|
|
'_wpi_discount_max_total' => 'max_total', |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
/* |
|
64
|
|
|
|-------------------------------------------------------------------------- |
|
65
|
|
|
| CRUD Methods |
|
66
|
|
|
|-------------------------------------------------------------------------- |
|
67
|
|
|
*/ |
|
68
|
|
|
/** |
|
69
|
|
|
* Method to create a new invoice in the database. |
|
70
|
|
|
* |
|
71
|
|
|
* @param WPInv_Invoice $invoice Invoice object. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function create( &$invoice ) { |
|
74
|
|
|
$invoice->set_version( WPINV_VERSION ); |
|
|
|
|
|
|
75
|
|
|
$invoice->set_date_created( current_time('mysql') ); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
// Create a new post. |
|
78
|
|
|
$id = wp_insert_post( |
|
79
|
|
|
apply_filters( |
|
80
|
|
|
'getpaid_new_invoice_data', |
|
81
|
|
|
array( |
|
82
|
|
|
'post_date' => $invoice->get_date_created( 'edit' ), |
|
83
|
|
|
'post_type' => $invoice->get_post_type( 'edit' ), |
|
|
|
|
|
|
84
|
|
|
'post_status' => $this->get_post_status( $invoice ), |
|
85
|
|
|
'ping_status' => 'closed', |
|
86
|
|
|
'post_author' => $invoice->get_user_id( 'edit' ), |
|
|
|
|
|
|
87
|
|
|
'post_title' => $invoice->get_number( 'edit' ), |
|
88
|
|
|
'post_excerpt' => $invoice->get_description( 'edit' ), |
|
89
|
|
|
'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
90
|
|
|
) |
|
91
|
|
|
), |
|
92
|
|
|
true |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
if ( $id && ! is_wp_error( $id ) ) { |
|
96
|
|
|
$invoice->set_id( $id ); |
|
|
|
|
|
|
97
|
|
|
$invoice->save_special(); |
|
98
|
|
|
$this->update_post_meta( $invoice ); |
|
99
|
|
|
$invoice->save_meta_data(); |
|
100
|
|
|
$invoice->apply_changes(); |
|
101
|
|
|
$this->clear_caches( $invoice ); |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ( is_wp_error( $id ) ) { |
|
106
|
|
|
$invoice->last_error = $id->get_error_message(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Method to save special invoice fields in the database. |
|
114
|
|
|
* |
|
115
|
|
|
* @param WPInv_Invoice $invoice Invoice object. |
|
116
|
|
|
*/ |
|
117
|
|
|
public function save_special( &$invoice ) { |
|
118
|
|
|
$invoice->set_version( WPINV_VERSION ); |
|
119
|
|
|
$invoice->set_date_created( current_time('mysql') ); |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
if ( $id && ! is_wp_error( $id ) ) { |
|
|
|
|
|
|
123
|
|
|
$invoice->set_id( $id ); |
|
124
|
|
|
$invoice->save_special(); |
|
125
|
|
|
$this->update_post_meta( $invoice ); |
|
126
|
|
|
$invoice->save_meta_data(); |
|
127
|
|
|
$invoice->apply_changes(); |
|
128
|
|
|
$this->clear_caches( $invoice ); |
|
129
|
|
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ( is_wp_error( $id ) ) { |
|
133
|
|
|
$invoice->last_error = $id->get_error_message(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Method to read an invoice from the database. |
|
141
|
|
|
* |
|
142
|
|
|
* @param WPInv_Invoice $invoice Invoice object. |
|
143
|
|
|
* |
|
144
|
|
|
*/ |
|
145
|
|
|
public function read( &$invoice ) { |
|
146
|
|
|
|
|
147
|
|
|
$invoice->set_defaults(); |
|
148
|
|
|
$invoice_object = get_post( $invoice->get_id() ); |
|
149
|
|
|
|
|
150
|
|
|
if ( ! $invoice->get_id() || ! $invoice_object || $invoice_object->post_type != 'wpi_invoice' ) { |
|
151
|
|
|
$invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$invoice->set_props( |
|
156
|
|
|
array( |
|
157
|
|
|
'date_created' => 0 < $discount_object->post_date_gmt ? $discount_object->post_date_gmt : null, |
|
|
|
|
|
|
158
|
|
|
'date_modified' => 0 < $discount_object->post_modified_gmt ? $discount_object->post_modified_gmt : null, |
|
159
|
|
|
'status' => $discount_object->post_status, |
|
160
|
|
|
'author' => $discount_object->post_author, |
|
161
|
|
|
'description' => $discount_object->post_excerpt, |
|
162
|
|
|
) |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
|
|
$this->read_object_data( $discount, $discount_object ); |
|
166
|
|
|
$discount->read_meta_data(); |
|
167
|
|
|
$discount->set_object_read( true ); |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Method to update a discount in the database. |
|
173
|
|
|
* |
|
174
|
|
|
* @param WPInv_Discount $discount Discount object. |
|
175
|
|
|
*/ |
|
176
|
|
|
public function update( &$discount ) { |
|
177
|
|
|
$discount->save_meta_data(); |
|
178
|
|
|
$discount->set_version( WPINV_VERSION ); |
|
179
|
|
|
|
|
180
|
|
|
if ( null === $discount->get_date_created( 'edit' ) ) { |
|
|
|
|
|
|
181
|
|
|
$discount->set_date_created( current_time('mysql') ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$changes = $discount->get_changes(); |
|
185
|
|
|
|
|
186
|
|
|
// Only update the post when the post data changes. |
|
187
|
|
|
if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) { |
|
188
|
|
|
$post_data = array( |
|
189
|
|
|
'post_date' => $discount->get_date_created( 'edit' ), |
|
190
|
|
|
'post_status' => $discount->get_status( 'edit' ), |
|
191
|
|
|
'post_title' => $discount->get_name( 'edit' ), |
|
192
|
|
|
'post_author' => $discount->get_author( 'edit' ), |
|
193
|
|
|
'post_modified' => $discount->get_date_modified( 'edit' ), |
|
194
|
|
|
'post_excerpt' => $discount->get_description( 'edit' ), |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* When updating this object, to prevent infinite loops, use $wpdb |
|
199
|
|
|
* to update data, since wp_update_post spawns more calls to the |
|
200
|
|
|
* save_post action. |
|
201
|
|
|
* |
|
202
|
|
|
* This ensures hooks are fired by either WP itself (admin screen save), |
|
203
|
|
|
* or an update purely from CRUD. |
|
204
|
|
|
*/ |
|
205
|
|
|
if ( doing_action( 'save_post' ) ) { |
|
206
|
|
|
$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) ); |
|
207
|
|
|
clean_post_cache( $discount->get_id() ); |
|
208
|
|
|
} else { |
|
209
|
|
|
wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) ); |
|
210
|
|
|
} |
|
211
|
|
|
$discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
212
|
|
|
} |
|
213
|
|
|
$this->update_post_meta( $discount ); |
|
214
|
|
|
$discount->apply_changes(); |
|
215
|
|
|
$this->clear_caches( $discount ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/* |
|
219
|
|
|
|-------------------------------------------------------------------------- |
|
220
|
|
|
| Additional Methods |
|
221
|
|
|
|-------------------------------------------------------------------------- |
|
222
|
|
|
*/ |
|
223
|
|
|
/** |
|
224
|
|
|
* Returns a list of all special fields. |
|
225
|
|
|
* |
|
226
|
|
|
* @param WPInv_Invoice $invoice Invoice object. |
|
227
|
|
|
*/ |
|
228
|
|
|
public function get_special_fields( $invoice ) { |
|
229
|
|
|
|
|
230
|
|
|
return array ( |
|
231
|
|
|
'post_id' => $invoice->get_id(), |
|
232
|
|
|
'number' => $invoice->get_number(), |
|
233
|
|
|
'key' => $invoice->get_key(), |
|
234
|
|
|
'type' => str_replace( 'wpi_', '', $invoice->get_post_type() ), |
|
235
|
|
|
'mode' => $invoice->get_mode(), |
|
236
|
|
|
'user_ip' => $invoice->get_user_ip(), |
|
237
|
|
|
'first_name' => $invoice->get_first_name(), |
|
238
|
|
|
'last_name' => $invoice->get_last_name(), |
|
239
|
|
|
'address' => $invoice->get_address(), |
|
240
|
|
|
'city' => $invoice->get_city(), |
|
241
|
|
|
'state' => $invoice->get_state(), |
|
242
|
|
|
'country' => $invoice->get_country(), |
|
243
|
|
|
'zip' => $invoice->get_zip(), |
|
244
|
|
|
'adddress_confirmed' => (int) $this->get_adddress_confirmed(), |
|
|
|
|
|
|
245
|
|
|
'gateway' => $invoice->get_gateway(), |
|
246
|
|
|
'transaction_id' => $invoice->get_transaction_id(), |
|
247
|
|
|
'currency' => $invoice->get_currency(), |
|
248
|
|
|
'subtotal' => $invoice->get_subtotal(), |
|
249
|
|
|
'tax' => $invoice->get_tax(), |
|
250
|
|
|
'fees_total' => $invoice->get_fees_total(), |
|
251
|
|
|
'total' => $invoice->get_total(), |
|
252
|
|
|
'discount' => $invoice->get_discount(), |
|
253
|
|
|
'discount_code' => $invoice->get_discount_code(), |
|
254
|
|
|
'disable_taxes' => (int) $invoice->get_disable_taxes(), |
|
255
|
|
|
'due_date' => $invoice->get_due_date(), |
|
256
|
|
|
'completed_date' => $invoice->get_completed_date(), |
|
257
|
|
|
'company' => $invoice->get_company(), |
|
258
|
|
|
'vat_number' => $invoice->get_vat_number(), |
|
259
|
|
|
'vat_rate' => $invoice->get_vat_rate(), |
|
260
|
|
|
'custom_meta' => $invoice->get_payment_meta(), |
|
|
|
|
|
|
261
|
|
|
); |
|
262
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
} |
|
266
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.