1
|
|
|
<?php |
2
|
|
|
// Exit if accessed directly |
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
4
|
|
|
|
5
|
|
|
class WPInv_Item { |
6
|
|
|
public $ID = 0; |
7
|
|
|
private $type; |
8
|
|
|
private $title; |
|
|
|
|
9
|
|
|
private $custom_id; |
|
|
|
|
10
|
|
|
private $price; |
11
|
|
|
private $status; |
|
|
|
|
12
|
|
|
private $custom_name; |
|
|
|
|
13
|
|
|
private $custom_singular_name; |
|
|
|
|
14
|
|
|
private $vat_rule; |
15
|
|
|
private $vat_class; |
16
|
|
|
private $editable; |
|
|
|
|
17
|
|
|
private $excerpt; |
|
|
|
|
18
|
|
|
private $is_recurring; |
|
|
|
|
19
|
|
|
private $recurring_period; |
|
|
|
|
20
|
|
|
private $recurring_interval; |
|
|
|
|
21
|
|
|
private $recurring_limit; |
|
|
|
|
22
|
|
|
private $free_trial; |
|
|
|
|
23
|
|
|
private $trial_period; |
|
|
|
|
24
|
|
|
private $trial_interval; |
|
|
|
|
25
|
|
|
|
26
|
|
|
public $post_author = 0; |
27
|
|
|
public $post_date = '0000-00-00 00:00:00'; |
28
|
|
|
public $post_date_gmt = '0000-00-00 00:00:00'; |
29
|
|
|
public $post_content = ''; |
30
|
|
|
public $post_title = ''; |
31
|
|
|
public $post_excerpt = ''; |
32
|
|
|
public $post_status = 'publish'; |
33
|
|
|
public $comment_status = 'open'; |
34
|
|
|
public $ping_status = 'open'; |
35
|
|
|
public $post_password = ''; |
36
|
|
|
public $post_name = ''; |
37
|
|
|
public $to_ping = ''; |
38
|
|
|
public $pinged = ''; |
39
|
|
|
public $post_modified = '0000-00-00 00:00:00'; |
40
|
|
|
public $post_modified_gmt = '0000-00-00 00:00:00'; |
41
|
|
|
public $post_content_filtered = ''; |
42
|
|
|
public $post_parent = 0; |
43
|
|
|
public $guid = ''; |
44
|
|
|
public $menu_order = 0; |
45
|
|
|
public $post_mime_type = ''; |
46
|
|
|
public $comment_count = 0; |
47
|
|
|
public $filter; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function __construct( $_id = false, $_args = array() ) { |
|
|
|
|
51
|
|
|
$item = WP_Post::get_instance( $_id ); |
52
|
|
|
return $this->setup_item( $item ); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function setup_item( $item ) { |
56
|
|
|
if( ! is_object( $item ) ) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if( ! is_a( $item, 'WP_Post' ) ) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if( 'wpi_item' !== $item->post_type ) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ( $item as $key => $value ) { |
69
|
|
|
switch ( $key ) { |
70
|
|
|
default: |
|
|
|
|
71
|
|
|
$this->$key = $value; |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function __get( $key ) { |
80
|
|
|
if ( method_exists( $this, 'get_' . $key ) ) { |
81
|
|
|
return call_user_func( array( $this, 'get_' . $key ) ); |
82
|
|
|
} else { |
83
|
|
|
return new WP_Error( 'wpinv-item-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) ); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function create( $data = array(), $wp_error = false ) { |
88
|
|
|
if ( $this->ID != 0 ) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$defaults = array( |
93
|
|
|
'post_type' => 'wpi_item', |
94
|
|
|
'post_status' => 'draft', |
95
|
|
|
'post_title' => __( 'New Invoice Item', 'invoicing' ) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$args = wp_parse_args( $data, $defaults ); |
99
|
|
|
|
100
|
|
|
do_action( 'wpinv_item_pre_create', $args ); |
101
|
|
|
|
102
|
|
|
$id = wp_insert_post( $args, $wp_error ); |
103
|
|
|
if ($wp_error && is_wp_error($id)) { |
104
|
|
|
return $id; |
105
|
|
|
} |
106
|
|
|
if ( !$id ) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$item = WP_Post::get_instance( $id ); |
111
|
|
|
|
112
|
|
View Code Duplication |
if (!empty($item) && !empty($data['meta'])) { |
113
|
|
|
$this->ID = $item->ID; |
114
|
|
|
$this->save_metas($data['meta']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Set custom id if not set. |
118
|
|
View Code Duplication |
if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
119
|
|
|
$this->save_metas( array( 'custom_id' => $id ) ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
do_action( 'wpinv_item_create', $id, $args ); |
123
|
|
|
|
124
|
|
|
return $this->setup_item( $item ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function update( $data = array(), $wp_error = false ) { |
128
|
|
|
if ( !$this->ID > 0 ) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$data['ID'] = $this->ID; |
133
|
|
|
|
134
|
|
|
do_action( 'wpinv_item_pre_update', $data ); |
135
|
|
|
|
136
|
|
|
$id = wp_update_post( $data, $wp_error ); |
137
|
|
|
if ($wp_error && is_wp_error($id)) { |
138
|
|
|
return $id; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ( !$id ) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$item = WP_Post::get_instance( $id ); |
146
|
|
View Code Duplication |
if (!empty($item) && !empty($data['meta'])) { |
147
|
|
|
$this->ID = $item->ID; |
148
|
|
|
$this->save_metas($data['meta']); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Set custom id if not set. |
152
|
|
View Code Duplication |
if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
153
|
|
|
$this->save_metas( array( 'custom_id' => $id ) ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
do_action( 'wpinv_item_update', $id, $data ); |
157
|
|
|
|
158
|
|
|
return $this->setup_item( $item ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function get_ID() { |
162
|
|
|
return $this->ID; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function get_name() { |
166
|
|
|
return get_the_title( $this->ID ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function get_title() { |
170
|
|
|
return get_the_title( $this->ID ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function get_status() { |
174
|
|
|
return get_post_status( $this->ID ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function get_summary() { |
178
|
|
|
return get_the_excerpt( $this->ID ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function get_price() { |
182
|
|
|
if ( ! isset( $this->price ) ) { |
183
|
|
|
$this->price = get_post_meta( $this->ID, '_wpinv_price', true ); |
184
|
|
|
|
185
|
|
|
if ( $this->price ) { |
186
|
|
|
$this->price = wpinv_sanitize_amount( $this->price ); |
187
|
|
|
} else { |
188
|
|
|
$this->price = 0; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return apply_filters( 'wpinv_get_item_price', $this->price, $this->ID ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
View Code Duplication |
public function get_vat_rule() { |
|
|
|
|
196
|
|
|
global $wpinv_euvat; |
197
|
|
|
|
198
|
|
|
if( !isset( $this->vat_rule ) ) { |
199
|
|
|
$this->vat_rule = get_post_meta( $this->ID, '_wpinv_vat_rule', true ); |
200
|
|
|
|
201
|
|
|
if ( empty( $this->vat_rule ) ) { |
202
|
|
|
$this->vat_rule = $wpinv_euvat->allow_vat_rules() ? 'digital' : 'physical'; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return apply_filters( 'wpinv_get_item_vat_rule', $this->vat_rule, $this->ID ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
public function get_vat_class() { |
|
|
|
|
210
|
|
|
if( !isset( $this->vat_class ) ) { |
211
|
|
|
$this->vat_class = get_post_meta( $this->ID, '_wpinv_vat_class', true ); |
212
|
|
|
|
213
|
|
|
if ( empty( $this->vat_class ) ) { |
214
|
|
|
$this->vat_class = '_standard'; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return apply_filters( 'wpinv_get_item_vat_class', $this->vat_class, $this->ID ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
View Code Duplication |
public function get_type() { |
|
|
|
|
222
|
|
|
if( ! isset( $this->type ) ) { |
223
|
|
|
$this->type = get_post_meta( $this->ID, '_wpinv_type', true ); |
224
|
|
|
|
225
|
|
|
if ( empty( $this->type ) ) { |
226
|
|
|
$this->type = 'custom'; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return apply_filters( 'wpinv_get_item_type', $this->type, $this->ID ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function get_custom_id() { |
234
|
|
|
$custom_id = get_post_meta( $this->ID, '_wpinv_custom_id', true ); |
235
|
|
|
|
236
|
|
|
return apply_filters( 'wpinv_get_item_custom_id', $custom_id, $this->ID ); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function get_custom_name() { |
240
|
|
|
$custom_name = get_post_meta( $this->ID, '_wpinv_custom_name', true ); |
241
|
|
|
|
242
|
|
|
return apply_filters( 'wpinv_get_item_custom_name', $custom_name, $this->ID ); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function get_custom_singular_name() { |
246
|
|
|
$custom_singular_name = get_post_meta( $this->ID, '_wpinv_custom_singular_name', true ); |
247
|
|
|
|
248
|
|
|
return apply_filters( 'wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function get_editable() { |
252
|
|
|
$editable = get_post_meta( $this->ID, '_wpinv_editable', true ); |
253
|
|
|
|
254
|
|
|
return apply_filters( 'wpinv_item_get_editable', $editable, $this->ID ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function get_excerpt() { |
258
|
|
|
$excerpt = get_the_excerpt( $this->ID ); |
259
|
|
|
|
260
|
|
|
return apply_filters( 'wpinv_item_get_excerpt', $excerpt, $this->ID ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function get_is_recurring() { |
264
|
|
|
$is_recurring = get_post_meta( $this->ID, '_wpinv_is_recurring', true ); |
265
|
|
|
|
266
|
|
|
return apply_filters( 'wpinv_item_get_is_recurring', $is_recurring, $this->ID ); |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
View Code Duplication |
public function get_recurring_period( $full = false ) { |
|
|
|
|
271
|
|
|
$period = get_post_meta( $this->ID, '_wpinv_recurring_period', true ); |
272
|
|
|
|
273
|
|
|
if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
274
|
|
|
$period = 'D'; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if ( $full ) { |
278
|
|
|
switch( $period ) { |
279
|
|
|
case 'D': |
280
|
|
|
$period = 'day'; |
281
|
|
|
break; |
282
|
|
|
case 'W': |
283
|
|
|
$period = 'week'; |
284
|
|
|
break; |
285
|
|
|
case 'M': |
286
|
|
|
$period = 'month'; |
287
|
|
|
break; |
288
|
|
|
case 'Y': |
289
|
|
|
$period = 'year'; |
290
|
|
|
break; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return apply_filters( 'wpinv_item_recurring_period', $period, $full, $this->ID ); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function get_recurring_interval() { |
298
|
|
|
$interval = (int)get_post_meta( $this->ID, '_wpinv_recurring_interval', true ); |
299
|
|
|
|
300
|
|
|
if ( !$interval > 0 ) { |
301
|
|
|
$interval = 1; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return apply_filters( 'wpinv_item_recurring_interval', $interval, $this->ID ); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function get_recurring_limit() { |
308
|
|
|
$limit = get_post_meta( $this->ID, '_wpinv_recurring_limit', true ); |
309
|
|
|
|
310
|
|
|
return (int)apply_filters( 'wpinv_item_recurring_limit', $limit, $this->ID ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function get_free_trial() { |
314
|
|
|
$free_trial = get_post_meta( $this->ID, '_wpinv_free_trial', true ); |
315
|
|
|
|
316
|
|
|
return apply_filters( 'wpinv_item_get_free_trial', $free_trial, $this->ID ); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
View Code Duplication |
public function get_trial_period( $full = false ) { |
|
|
|
|
320
|
|
|
$period = get_post_meta( $this->ID, '_wpinv_trial_period', true ); |
321
|
|
|
|
322
|
|
|
if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
323
|
|
|
$period = 'D'; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
if ( $full ) { |
327
|
|
|
switch( $period ) { |
328
|
|
|
case 'D': |
329
|
|
|
$period = 'day'; |
330
|
|
|
break; |
331
|
|
|
case 'W': |
332
|
|
|
$period = 'week'; |
333
|
|
|
break; |
334
|
|
|
case 'M': |
335
|
|
|
$period = 'month'; |
336
|
|
|
break; |
337
|
|
|
case 'Y': |
338
|
|
|
$period = 'year'; |
339
|
|
|
break; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return apply_filters( 'wpinv_item_trial_period', $period, $full, $this->ID ); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function get_trial_interval() { |
347
|
|
|
$interval = absint( get_post_meta( $this->ID, '_wpinv_trial_interval', true ) ); |
348
|
|
|
|
349
|
|
|
if ( !$interval > 0 ) { |
350
|
|
|
$interval = 1; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return apply_filters( 'wpinv_item_trial_interval', $interval, $this->ID ); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function get_the_price() { |
357
|
|
|
$item_price = wpinv_price( wpinv_format_amount( $this->price ) ); |
358
|
|
|
|
359
|
|
|
return apply_filters( 'wpinv_get_the_item_price', $item_price, $this->ID ); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function is_recurring() { |
363
|
|
|
$is_recurring = $this->get_is_recurring(); |
364
|
|
|
|
365
|
|
|
return (bool)apply_filters( 'wpinv_is_recurring_item', $is_recurring, $this->ID ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function has_free_trial() { |
369
|
|
|
$free_trial = $this->is_recurring() && $this->get_free_trial() ? true : false; |
370
|
|
|
|
371
|
|
|
return (bool)apply_filters( 'wpinv_item_has_free_trial', $free_trial, $this->ID ); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function is_free() { |
375
|
|
|
$is_free = false; |
376
|
|
|
|
377
|
|
|
$price = get_post_meta( $this->ID, '_wpinv_price', true ); |
378
|
|
|
|
379
|
|
|
if ( (float)$price == 0 ) { |
380
|
|
|
$is_free = true; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID ); |
384
|
|
|
|
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function is_package() { |
388
|
|
|
$is_package = $this->get_type() == 'package' ? true : false; |
389
|
|
|
|
390
|
|
|
return (bool) apply_filters( 'wpinv_is_package_item', $is_package, $this->ID ); |
391
|
|
|
|
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
public function is_editable() { |
395
|
|
|
$editable = $this->get_editable(); |
396
|
|
|
|
397
|
|
|
$is_editable = $editable === 0 || $editable === '0' ? false : true; |
398
|
|
|
|
399
|
|
|
return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID ); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function save_metas( $metas = array() ) { |
403
|
|
|
if ( empty( $metas ) ) { |
404
|
|
|
return false; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
foreach ( $metas as $meta_key => $meta_value ) { |
408
|
|
|
$meta_key = strpos($meta_key, '_wpinv_') !== 0 ? '_wpinv_' . $meta_key : $meta_key; |
409
|
|
|
|
410
|
|
|
$this->update_meta($meta_key, $meta_value); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return true; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) { |
417
|
|
|
if ( empty( $meta_key ) ) { |
418
|
|
|
return false; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
$meta_value = apply_filters( 'wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID ); |
422
|
|
|
|
423
|
|
|
return update_post_meta( $this->ID, $meta_key, $meta_value, $prev_value ); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
public function get_fees( $type = 'fee', $item_id = 0 ) { |
427
|
|
|
global $wpi_session; |
428
|
|
|
|
429
|
|
|
$fees = $wpi_session->get( 'wpi_cart_fees' ); |
430
|
|
|
|
431
|
|
|
if ( ! wpinv_get_cart_contents() ) { |
432
|
|
|
// We can only get item type fees when the cart is empty |
433
|
|
|
$type = 'custom'; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) { |
437
|
|
|
foreach( $fees as $key => $fee ) { |
438
|
|
View Code Duplication |
if( ! empty( $fee['type'] ) && $type != $fee['type'] ) { |
439
|
|
|
unset( $fees[ $key ] ); |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
if ( ! empty( $fees ) && ! empty( $item_id ) ) { |
445
|
|
|
// Remove fees that don't belong to the specified Item |
446
|
|
|
foreach ( $fees as $key => $fee ) { |
447
|
|
|
if ( (int) $item_id !== (int)$fee['custom_id'] ) { |
448
|
|
|
unset( $fees[ $key ] ); |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
if ( ! empty( $fees ) ) { |
454
|
|
|
// Remove fees that belong to a specific item but are not in the cart |
455
|
|
|
foreach( $fees as $key => $fee ) { |
456
|
|
|
if( empty( $fee['custom_id'] ) ) { |
457
|
|
|
continue; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
if ( !wpinv_item_in_cart( $fee['custom_id'] ) ) { |
461
|
|
|
unset( $fees[ $key ] ); |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
return ! empty( $fees ) ? $fees : array(); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
public function can_purchase() { |
470
|
|
|
$can_purchase = true; |
471
|
|
|
|
472
|
|
|
if ( !current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) { |
473
|
|
|
$can_purchase = false; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
return (bool)apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this ); |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.