Passed
Pull Request — master (#371)
by Brian
101:52
created

WPInv_Item::get_vat_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Item Class
8
 *
9
 */
10
class WPInv_Item  extends GetPaid_Data {
11
12
    /**
13
	 * Which data store to load.
14
	 *
15
	 * @var string
16
	 */
17
    protected $data_store_name = 'item';
18
19
    /**
20
	 * This is the name of this object type.
21
	 *
22
	 * @var string
23
	 */
24
	protected $object_type = 'item';
25
26
    /**
27
	 * Item Data array. This is the core item data exposed in APIs.
28
	 *
29
	 * @since 1.0.19
30
	 * @var array
31
	 */
32
	protected $data = array(
33
		'parent_id'            => 0,
34
		'status'               => 'draft',
35
		'version'              => '',
36
		'date_created'         => null,
37
        'date_modified'        => null,
38
        'name'                 => '',
39
        'description'          => '',
40
        'author'               => 1,
41
        'price'                => 0,
42
        'vat_rule'             => null,
43
        'vat_class'            => null,
44
        'type'                 => 'custom',
45
        'custom_id'            => null,
46
        'custom_name'          => null,
47
        'custom_singular_name' => null,
48
        'is_editable'          => 1,
49
        'is_dynamic_pricing'   => null,
50
        'minimum_price'        => null,
51
        'is_recurring'         => null,
52
        'recurring_period'     => null,
53
        'recurring_interval'   => null,
54
        'recurring_limit'      => null,
55
        'is_free_trial'        => null,
56
        'trial_period'         => null,
57
        'trial_interval'       => null,
58
    );
59
60
    /**
61
	 * Stores meta in cache for future reads.
62
	 *
63
	 * A group must be set to to enable caching.
64
	 *
65
	 * @var string
66
	 */
67
	protected $cache_group = 'getpaid_items';
68
69
    /**
70
     * Stores a reference to the original WP_Post object
71
     * 
72
     * @var WP_Post
73
     */
74
    protected $post = null; 
75
76
    /**
77
	 * Get the item if ID is passed, otherwise the item is new and empty.
78
	 *
79
	 * @param  int|object|WPInv_Item|WP_Post $item Item to read.
80
	 */
81
	public function __construct( $item = 0 ) {
82
		parent::__construct( $item );
83
84
		if ( is_numeric( $item ) && $item > 0 ) {
85
			$this->set_id( $item );
86
		} elseif ( $item instanceof self ) {
87
			$this->set_id( $item->get_id() );
88
		} elseif ( ! empty( $item->ID ) ) {
89
			$this->set_id( $item->ID );
90
		} else {
91
			$this->set_object_read( true );
92
		}
93
94
        // Load the datastore.
95
		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
96
97
		if ( $this->get_id() > 0 ) {
98
            $this->post = get_post( $this->get_id() );
99
            $this->ID   = $this->get_id();
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
100
			$this->data_store->read( $this );
101
        }
102
103
	}
104
105
    /*
106
	|--------------------------------------------------------------------------
107
	| CRUD methods
108
	|--------------------------------------------------------------------------
109
	|
110
	| Methods which create, read, update and delete items from the database.
111
	|
112
    */
113
114
    /*
115
	|--------------------------------------------------------------------------
116
	| Getters
117
	|--------------------------------------------------------------------------
118
    */
119
120
    /**
121
	 * Get parent item ID.
122
	 *
123
	 * @since 1.0.19
124
	 * @param  string $context View or edit context.
125
	 * @return int
126
	 */
127
	public function get_parent_id( $context = 'view' ) {
128
		return (int) $this->get_prop( 'parent_id', $context );
129
    }
130
131
    /**
132
	 * Get item status.
133
	 *
134
	 * @since 1.0.19
135
	 * @param  string $context View or edit context.
136
	 * @return string
137
	 */
138
	public function get_status( $context = 'view' ) {
139
		return $this->get_prop( 'status', $context );
140
    }
141
142
    /**
143
	 * Get plugin version when the item was created.
144
	 *
145
	 * @since 1.0.19
146
	 * @param  string $context View or edit context.
147
	 * @return string
148
	 */
149
	public function get_version( $context = 'view' ) {
150
		return $this->get_prop( 'version', $context );
151
    }
152
153
    /**
154
	 * Get date when the item was created.
155
	 *
156
	 * @since 1.0.19
157
	 * @param  string $context View or edit context.
158
	 * @return string
159
	 */
160
	public function get_date_created( $context = 'view' ) {
161
		return $this->get_prop( 'date_created', $context );
162
    }
163
164
    /**
165
	 * Get GMT date when the item was created.
166
	 *
167
	 * @since 1.0.19
168
	 * @param  string $context View or edit context.
169
	 * @return string
170
	 */
171
	public function get_date_created_gmt( $context = 'view' ) {
172
        $date = $this->get_date_created( $context );
173
174
        if ( $date ) {
175
            $date = get_gmt_from_date( $date );
176
        }
177
		return $date;
178
    }
179
180
    /**
181
	 * Get date when the item was last modified.
182
	 *
183
	 * @since 1.0.19
184
	 * @param  string $context View or edit context.
185
	 * @return string
186
	 */
187
	public function get_date_modified( $context = 'view' ) {
188
		return $this->get_prop( 'date_modified', $context );
189
    }
190
191
    /**
192
	 * Get GMT date when the item was last modified.
193
	 *
194
	 * @since 1.0.19
195
	 * @param  string $context View or edit context.
196
	 * @return string
197
	 */
198
	public function get_date_modified_gmt( $context = 'view' ) {
199
        $date = $this->get_date_modified( $context );
200
201
        if ( $date ) {
202
            $date = get_gmt_from_date( $date );
203
        }
204
		return $date;
205
    }
206
207
    /**
208
	 * Get the item name.
209
	 *
210
	 * @since 1.0.19
211
	 * @param  string $context View or edit context.
212
	 * @return string
213
	 */
214
	public function get_name( $context = 'view' ) {
215
		return $this->get_prop( 'name', $context );
216
    }
217
218
    /**
219
	 * Alias of self::get_name().
220
	 *
221
	 * @since 1.0.19
222
	 * @param  string $context View or edit context.
223
	 * @return string
224
	 */
225
	public function get_title( $context = 'view' ) {
226
		return $this->get_name( $context );
227
    }
228
229
    /**
230
	 * Get the item description.
231
	 *
232
	 * @since 1.0.19
233
	 * @param  string $context View or edit context.
234
	 * @return string
235
	 */
236
	public function get_description( $context = 'view' ) {
237
		return $this->get_prop( 'description', $context );
238
    }
239
240
    /**
241
	 * Alias of self::get_description().
242
	 *
243
	 * @since 1.0.19
244
	 * @param  string $context View or edit context.
245
	 * @return string
246
	 */
247
	public function get_excerpt( $context = 'view' ) {
248
		return $this->get_description( $context );
249
    }
250
251
    /**
252
	 * Alias of self::get_description().
253
	 *
254
	 * @since 1.0.19
255
	 * @param  string $context View or edit context.
256
	 * @return string
257
	 */
258
	public function get_summary( $context = 'view' ) {
259
		return $this->get_description( $context );
260
    }
261
262
    /**
263
	 * Get the owner of the item.
264
	 *
265
	 * @since 1.0.19
266
	 * @param  string $context View or edit context.
267
	 * @return string
268
	 */
269
	public function get_author( $context = 'view' ) {
270
		return (int) $this->get_prop( 'author', $context );
271
    }
272
273
    /**
274
	 * Get the price of the item.
275
	 *
276
	 * @since 1.0.19
277
	 * @param  string $context View or edit context.
278
	 * @return float
279
	 */
280
	public function get_price( $context = 'view' ) {
281
        return (float) wpinv_sanitize_amount( $this->get_prop( 'price', $context ) );
282
    }
283
284
    /**
285
	 * Returns a formated price.
286
	 *
287
	 * @since 1.0.19
288
	 * @param  string $context View or edit context.
289
	 * @return string
290
	 */
291
    public function get_the_price() {
292
        $item_price = wpinv_price( wpinv_format_amount( $this->get_price() ) );
293
294
        return apply_filters( 'wpinv_get_the_item_price', $item_price, $this->ID );
295
    }
296
297
    /**
298
	 * Get the VAT rule of the item.
299
	 *
300
	 * @since 1.0.19
301
	 * @param  string $context View or edit context.
302
	 * @return string
303
	 */
304
	public function get_vat_rule( $context = 'view' ) {
305
        return $this->get_prop( 'vat_rule', $context );
306
    }
307
308
    /**
309
	 * Get the VAT class of the item.
310
	 *
311
	 * @since 1.0.19
312
	 * @param  string $context View or edit context.
313
	 * @return string
314
	 */
315
	public function get_vat_class( $context = 'view' ) {
316
        return $this->get_prop( 'vat_class', $context );
317
    }
318
319
    /**
320
	 * Get the type of the item.
321
	 *
322
	 * @since 1.0.19
323
	 * @param  string $context View or edit context.
324
	 * @return string
325
	 */
326
	public function get_type( $context = 'view' ) {
327
        return $this->get_prop( 'type', $context );
328
    }
329
330
    /**
331
	 * Get the custom id of the item.
332
	 *
333
	 * @since 1.0.19
334
	 * @param  string $context View or edit context.
335
	 * @return string
336
	 */
337
	public function get_custom_id( $context = 'view' ) {
338
        return $this->get_prop( 'custom_id', $context );
339
    }
340
341
    /**
342
	 * Get the custom name of the item.
343
	 *
344
	 * @since 1.0.19
345
	 * @param  string $context View or edit context.
346
	 * @return string
347
	 */
348
	public function get_custom_name( $context = 'view' ) {
349
        return $this->get_prop( 'custom_name', $context );
350
    }
351
352
    /**
353
	 * Get the custom singular name of the item.
354
	 *
355
	 * @since 1.0.19
356
	 * @param  string $context View or edit context.
357
	 * @return string
358
	 */
359
	public function get_custom_singular_name( $context = 'view' ) {
360
        return $this->get_prop( 'custom_singular_name', $context );
361
    }
362
363
    /**
364
	 * Checks if an item is editable..
365
	 *
366
	 * @since 1.0.19
367
	 * @param  string $context View or edit context.
368
	 * @return int
369
	 */
370
	public function get_is_editable( $context = 'view' ) {
371
        return (int) $this->get_prop( 'is_editable', $context );
372
    }
373
374
    /**
375
	 * Alias of self::get_is_editable().
376
	 *
377
	 * @since 1.0.19
378
	 * @param  string $context View or edit context.
379
	 * @return int
380
	 */
381
	public function get_editable( $context = 'view' ) {
382
		return $this->get_is_editable( $context );
383
    }
384
385
    /**
386
	 * Checks if dynamic pricing is enabled.
387
	 *
388
	 * @since 1.0.19
389
	 * @param  string $context View or edit context.
390
	 * @return int
391
	 */
392
	public function get_is_dynamic_pricing( $context = 'view' ) {
393
        return (int) $this->get_prop( 'is_dynamic_pricing', $context );
394
    }
395
396
    /**
397
	 * Returns the minimum price if dynamic pricing is enabled.
398
	 *
399
	 * @since 1.0.19
400
	 * @param  string $context View or edit context.
401
	 * @return float
402
	 */
403
	public function get_minimum_price( $context = 'view' ) {
404
        return (float) wpinv_sanitize_amount( $this->get_prop( 'minimum_price', $context ) );
405
    }
406
407
    /**
408
	 * Checks if this is a recurring item.
409
	 *
410
	 * @since 1.0.19
411
	 * @param  string $context View or edit context.
412
	 * @return int
413
	 */
414
	public function get_is_recurring( $context = 'view' ) {
415
        return (int) $this->get_prop( 'is_recurring', $context );
416
    }
417
418
    /**
419
	 * Get the recurring period.
420
	 *
421
	 * @since 1.0.19
422
	 * @param  bool $full Return abbreviation or in full.
423
	 * @return string
424
	 */
425
	public function get_recurring_period( $full = false ) {
426
        $period = $this->get_prop( 'recurring_period', 'view' );
427
428
        if ( $full && ! is_bool( $full ) ) {
0 ignored issues
show
introduced by
The condition is_bool($full) is always true.
Loading history...
429
            $full = false;
430
        }
431
432
        return getpaid_sanitize_recurring_period( $period, $full );
433
    }
434
435
    /**
436
	 * Get the recurring interval.
437
	 *
438
	 * @since 1.0.19
439
	 * @param  string $context View or edit context.
440
	 * @return int
441
	 */
442
	public function get_recurring_interval( $context = 'view' ) {
443
		$interval = absint( $this->get_prop( 'recurring_interval', $context ) );
444
445
		if ( $interval < 1 ) {
446
			$interval = 1;
447
		}
448
449
        return $interval;
450
    }
451
452
    /**
453
	 * Get the recurring limit.
454
	 *
455
	 * @since 1.0.19
456
	 * @param  string $context View or edit context.
457
	 * @return int
458
	 */
459
	public function get_recurring_limit( $context = 'view' ) {
460
        return (int) $this->get_prop( 'recurring_limit', $context );
461
    }
462
463
    /**
464
	 * Checks if we have a free trial.
465
	 *
466
	 * @since 1.0.19
467
	 * @param  string $context View or edit context.
468
	 * @return int
469
	 */
470
	public function get_is_free_trial( $context = 'view' ) {
471
        return (int) $this->get_prop( 'is_free_trial', $context );
472
    }
473
474
    /**
475
	 * Alias for self::get_is_free_trial().
476
	 *
477
	 * @since 1.0.19
478
	 * @param  string $context View or edit context.
479
	 * @return int
480
	 */
481
	public function get_free_trial( $context = 'view' ) {
482
        return $this->get_is_free_trial( $context );
483
    }
484
485
    /**
486
	 * Get the trial period.
487
	 *
488
	 * @since 1.0.19
489
	 * @param  bool $full Return abbreviation or in full.
490
	 * @return string
491
	 */
492
	public function get_trial_period( $full = false ) {
493
        $period = $this->get_prop( 'trial_period', 'view' );
494
495
        if ( $full && ! is_bool( $full ) ) {
0 ignored issues
show
introduced by
The condition is_bool($full) is always true.
Loading history...
496
            $full = false;
497
        }
498
499
        return getpaid_sanitize_recurring_period( $period, $full );
500
    }
501
502
    /**
503
	 * Get the trial interval.
504
	 *
505
	 * @since 1.0.19
506
	 * @param  string $context View or edit context.
507
	 * @return int
508
	 */
509
	public function get_trial_interval( $context = 'view' ) {
510
        return (int) $this->get_prop( 'trial_interval', $context );
511
    }
512
513
    /**
514
     * Margic method for retrieving a property.
515
     */
516
    public function __get( $key ) {
517
518
        // Check if we have a helper method for that.
519
        if ( method_exists( $this, 'get_' . $key ) ) {
520
            return call_user_func( array( $this, 'get_' . $key ) );
521
        }
522
523
        // Check if the key is in the associated $post object.
524
        if ( ! empty( $this->post ) && isset( $this->post->$key ) ) {
525
            return $this->post->$key;
526
        }
527
528
        return $this->get_prop( $key );
529
530
    }
531
532
    /*
533
	|--------------------------------------------------------------------------
534
	| Setters
535
	|--------------------------------------------------------------------------
536
	|
537
	| Functions for setting order data. These should not update anything in the
538
	| database itself and should only change what is stored in the class
539
	| object.
540
    */
541
542
    /**
543
	 * Set parent order ID.
544
	 *
545
	 * @since 1.0.19
546
	 */
547
	public function set_parent_id( $value ) {
548
		if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) {
549
			return;
550
		}
551
		$this->set_prop( 'parent_id', absint( $value ) );
552
	}
553
554
    /**
555
	 * Sets item status.
556
	 *
557
	 * @since 1.0.19
558
	 * @param  string $status New status.
559
	 * @return array details of change.
560
	 */
561
	public function set_status( $status ) {
562
        $old_status = $this->get_status();
563
564
        $this->set_prop( 'status', $status );
565
566
		return array(
567
			'from' => $old_status,
568
			'to'   => $status,
569
		);
570
    }
571
572
    /**
573
	 * Set plugin version when the item was created.
574
	 *
575
	 * @since 1.0.19
576
	 */
577
	public function set_version( $value ) {
578
		$this->set_prop( 'version', $value );
579
    }
580
581
    /**
582
	 * Set date when the item was created.
583
	 *
584
	 * @since 1.0.19
585
	 * @param string $value Value to set.
586
     * @return bool Whether or not the date was set.
587
	 */
588
	public function set_date_created( $value ) {
589
        $date = strtotime( $value );
590
591
        if ( $date ) {
592
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
593
            return true;
594
        }
595
596
        return false;
597
598
    }
599
600
    /**
601
	 * Set date when the item was last modified.
602
	 *
603
	 * @since 1.0.19
604
	 * @param string $value Value to set.
605
     * @return bool Whether or not the date was set.
606
	 */
607
	public function set_date_modified( $value ) {
608
        $date = strtotime( $value );
609
610
        if ( $date ) {
611
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
612
            return true;
613
        }
614
615
        return false;
616
617
    }
618
619
    /**
620
	 * Set the item name.
621
	 *
622
	 * @since 1.0.19
623
	 * @param  string $value New name.
624
	 */
625
	public function set_name( $value ) {
626
        $name = sanitize_text_field( $value );
627
		$this->set_prop( 'name', $name );
628
    }
629
630
    /**
631
	 * Alias of self::set_name().
632
	 *
633
	 * @since 1.0.19
634
	 * @param  string $value New name.
635
	 */
636
	public function set_title( $value ) {
637
		$this->set_name( $value );
638
    }
639
640
    /**
641
	 * Set the item description.
642
	 *
643
	 * @since 1.0.19
644
	 * @param  string $value New description.
645
	 */
646
	public function set_description( $value ) {
647
        $description = wp_kses_post( $value );
648
		return $this->set_prop( 'description', $description );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('description', $description) targeting GetPaid_Data::set_prop() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
649
    }
650
651
    /**
652
	 * Alias of self::set_description().
653
	 *
654
	 * @since 1.0.19
655
	 * @param  string $value New description.
656
	 */
657
	public function set_excerpt( $value ) {
658
		$this->set_description( $value );
659
    }
660
661
    /**
662
	 * Alias of self::set_description().
663
	 *
664
	 * @since 1.0.19
665
	 * @param  string $value New description.
666
	 */
667
	public function set_summary( $value ) {
668
		$this->set_description( $value );
669
    }
670
671
    /**
672
	 * Set the owner of the item.
673
	 *
674
	 * @since 1.0.19
675
	 * @param  int $value New author.
676
	 */
677
	public function set_author( $value ) {
678
		$this->set_prop( 'author', (int) $value );
679
    }
680
681
    /**
682
	 * Set the price of the item.
683
	 *
684
	 * @since 1.0.19
685
	 * @param  float $value New price.
686
]	 */
687
	public function set_price( $value ) {
688
        $this->set_prop( 'price', (float) wpinv_sanitize_amount( $value ) );
689
    }
690
691
    /**
692
	 * Set the VAT rule of the item.
693
	 *
694
	 * @since 1.0.19
695
	 * @param  string $value new rule.
696
	 */
697
	public function set_vat_rule( $value ) {
698
        $this->set_prop( 'vat_rule', $value );
699
    }
700
701
    /**
702
	 * Set the VAT class of the item.
703
	 *
704
	 * @since 1.0.19
705
	 * @param  string $value new class.
706
	 */
707
	public function set_vat_class( $value ) {
708
        $this->set_prop( 'vat_class', $value );
709
    }
710
711
    /**
712
	 * Set the type of the item.
713
	 *
714
	 * @since 1.0.19
715
	 * @param  string $value new item type.
716
	 * @return string
717
	 */
718
	public function set_type( $value ) {
719
720
        if ( empty( $value ) ) {
721
            $value = 'custom';
722
        }
723
724
        $this->set_prop( 'type', $value );
725
    }
726
727
    /**
728
	 * Set the custom id of the item.
729
	 *
730
	 * @since 1.0.19
731
	 * @param  string $value new custom id.
732
	 */
733
	public function set_custom_id( $value ) {
734
        $this->set_prop( 'custom_id', $value );
735
    }
736
737
    /**
738
	 * Set the custom name of the item.
739
	 *
740
	 * @since 1.0.19
741
	 * @param  string $value new custom name.
742
	 */
743
	public function set_custom_name( $value ) {
744
        $this->set_prop( 'custom_name', $value );
745
    }
746
747
    /**
748
	 * Set the custom singular name of the item.
749
	 *
750
	 * @since 1.0.19
751
	 * @param  string $value new custom singular name.
752
	 */
753
	public function set_custom_singular_name( $value ) {
754
        $this->set_prop( 'custom_singular_name', $value );
755
    }
756
757
    /**
758
	 * Sets if an item is editable..
759
	 *
760
	 * @since 1.0.19
761
	 * @param  int|bool $value whether or not the item is editable.
762
	 */
763
	public function set_is_editable( $value ) {
764
		if ( is_numeric( $value ) ) {
765
			$this->set_prop( 'is_editable', (int) $value );
766
		}
767
    }
768
769
    /**
770
	 * Sets if dynamic pricing is enabled.
771
	 *
772
	 * @since 1.0.19
773
	 * @param  int|bool $value whether or not dynamic pricing is allowed.
774
	 */
775
	public function set_is_dynamic_pricing( $value ) {
776
        $this->set_prop( 'is_dynamic_pricing', (int) $value );
777
    }
778
779
    /**
780
	 * Sets the minimum price if dynamic pricing is enabled.
781
	 *
782
	 * @since 1.0.19
783
	 * @param  float $value minimum price.
784
	 */
785
	public function set_minimum_price( $value ) {
786
        $this->set_prop( 'minimum_price',  (float) wpinv_sanitize_amount( $value ) );
787
    }
788
789
    /**
790
	 * Sets if this is a recurring item.
791
	 *
792
	 * @since 1.0.19
793
	 * @param  int|bool $value whether or not dynamic pricing is allowed.
794
	 */
795
	public function set_is_recurring( $value ) {
796
        $this->set_prop( 'is_recurring', (int) $value );
797
    }
798
799
    /**
800
	 * Set the recurring period.
801
	 *
802
	 * @since 1.0.19
803
	 * @param  string $value new period.
804
	 */
805
	public function set_recurring_period( $value ) {
806
        $this->set_prop( 'recurring_period', $value );
807
    }
808
809
    /**
810
	 * Set the recurring interval.
811
	 *
812
	 * @since 1.0.19
813
	 * @param  int $value recurring interval.
814
	 */
815
	public function set_recurring_interval( $value ) {
816
        return $this->set_prop( 'recurring_interval', (int) $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('recurri...interval', (int)$value) targeting GetPaid_Data::set_prop() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
817
    }
818
819
    /**
820
	 * Get the recurring limit.
821
	 * @since 1.0.19
822
	 * @param  int $value The recurring limit.
823
	 * @return int
824
	 */
825
	public function set_recurring_limit( $value ) {
826
        $this->set_prop( 'recurring_limit', (int) $value );
827
    }
828
829
    /**
830
	 * Checks if we have a free trial.
831
	 *
832
	 * @since 1.0.19
833
	 * @param  int|bool $value whether or not it has a free trial.
834
	 */
835
	public function set_is_free_trial( $value ) {
836
        $this->set_prop( 'is_free_trial', (int) $value );
837
    }
838
839
    /**
840
	 * Set the trial period.
841
	 *
842
	 * @since 1.0.19
843
	 * @param  string $value trial period.
844
	 */
845
	public function set_trial_period( $value ) {
846
        $this->set_prop( 'trial_period', $value );
847
    }
848
849
    /**
850
	 * Set the trial interval.
851
	 *
852
	 * @since 1.0.19
853
	 * @param  int $value trial interval.
854
	 */
855
	public function set_trial_interval( $value ) {
856
        $this->set_prop( 'trial_interval', $value );
857
    }
858
859
    /**
860
     * Create an item. For backwards compatibilty.
861
     * 
862
     * @deprecated
863
     */
864
    public function create( $data = array() ) {
865
866
		// Set the properties.
867
		if ( is_array( $data ) ) {
868
			$this->set_props( $data );
869
		}
870
871
		// Save the item.
872
		$this->save();
873
874
		return true;
875
    }
876
877
    /**
878
     * Updates an item. For backwards compatibilty.
879
     * 
880
     * @deprecated
881
     */
882
    public function update( $data = array() ) {
883
        $this->create( $data );
0 ignored issues
show
Deprecated Code introduced by
The function WPInv_Item::create() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

883
        /** @scrutinizer ignore-deprecated */ $this->create( $data );
Loading history...
884
    }
885
886
    /*
887
	|--------------------------------------------------------------------------
888
	| Conditionals
889
	|--------------------------------------------------------------------------
890
	|
891
	| Checks if a condition is true or false.
892
	|
893
	*/
894
895
    /**
896
	 * Checks whether the item has enabled dynamic pricing.
897
	 *
898
	 * @since 1.0.19
899
	 * @return bool
900
	 */
901
	public function user_can_set_their_price() {
902
        return (bool) $this->get_is_dynamic_pricing();
903
	}
904
	
905
	/**
906
	 * Checks whether the item is recurring.
907
	 *
908
	 * @since 1.0.19
909
	 * @return bool
910
	 */
911
	public function is_recurring() {
912
        return (bool) $this->get_is_recurring();
913
    }
914
915
    /**
916
	 * Checks whether the item has a free trial.
917
	 *
918
	 * @since 1.0.19
919
	 * @return bool
920
	 */
921
    public function has_free_trial() {
922
        $has_trial = $this->is_recurring() && (bool) $this->get_free_trial() ? true : false;
923
        return (bool) apply_filters( 'wpinv_item_has_free_trial', $has_trial, $this->ID, $this );
924
    }
925
926
    /**
927
	 * Checks whether the item is free.
928
	 *
929
	 * @since 1.0.19
930
	 * @return bool
931
	 */
932
    public function is_free() {
933
        $is_free   = $this->get_price() == 0;
934
        return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID, $this );
935
    }
936
937
    /**
938
	 * Checks the item status against a passed in status.
939
	 *
940
	 * @param array|string $status Status to check.
941
	 * @return bool
942
	 */
943
	public function has_status( $status ) {
944
		$has_status = ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status;
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $has_status = (is_array(...t_status() === $status), Probably Intended Meaning: $has_status = is_array($...t_status() === $status)
Loading history...
945
		return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status );
946
    }
947
948
    /**
949
	 * Checks the item type against a passed in types.
950
	 *
951
	 * @param array|string $type Type to check.
952
	 * @return bool
953
	 */
954
	public function is_type( $type ) {
955
		$is_type = ( is_array( $type ) && in_array( $this->get_type(), $type, true ) ) || $this->get_type() === $type;
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $is_type = (is_array($ty...->get_type() === $type), Probably Intended Meaning: $is_type = is_array($typ...->get_type() === $type)
Loading history...
956
		return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type );
957
	}
958
959
    /**
960
	 * Checks whether the item is editable.
961
	 *
962
	 * @since 1.0.19
963
	 * @return bool
964
	 */
965
    public function is_editable() {
966
        $is_editable = $this->get_is_editable();
967
        return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID, $this );
968
	}
969
970
	/**
971
	 * Returns an array of cart fees.
972
	 */
973
	public function get_fees( $type = 'fee', $item_id = 0 ) {
974
        global $wpi_session;
975
        
976
        $fees = $wpi_session->get( 'wpi_cart_fees' );
977
978
        if ( ! wpinv_get_cart_contents() ) {
979
            // We can only get item type fees when the cart is empty
980
            $type = 'custom';
981
        }
982
983
        if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) {
984
            foreach( $fees as $key => $fee ) {
985
                if( ! empty( $fee['type'] ) && $type != $fee['type'] ) {
986
                    unset( $fees[ $key ] );
987
                }
988
            }
989
        }
990
991
        if ( ! empty( $fees ) && ! empty( $item_id ) ) {
992
            // Remove fees that don't belong to the specified Item
993
            foreach ( $fees as $key => $fee ) {
994
                if ( (int) $item_id !== (int)$fee['custom_id'] ) {
995
                    unset( $fees[ $key ] );
996
                }
997
            }
998
        }
999
1000
        if ( ! empty( $fees ) ) {
1001
            // Remove fees that belong to a specific item but are not in the cart
1002
            foreach( $fees as $key => $fee ) {
1003
                if( empty( $fee['custom_id'] ) ) {
1004
                    continue;
1005
                }
1006
1007
                if ( !wpinv_item_in_cart( $fee['custom_id'] ) ) {
1008
                    unset( $fees[ $key ] );
1009
                }
1010
            }
1011
        }
1012
1013
        return ! empty( $fees ) ? $fees : array();
1014
    }
1015
1016
    /**
1017
	 * Checks whether the item is purchasable.
1018
	 *
1019
	 * @since 1.0.19
1020
	 * @return bool
1021
	 */
1022
    public function can_purchase() {
1023
        $can_purchase = null !== $this->get_id();
1024
1025
        if ( ! current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) {
0 ignored issues
show
Bug Best Practice introduced by
The property post_status does not exist on WPInv_Item. Since you implemented __get, consider adding a @property annotation.
Loading history...
1026
            $can_purchase = false;
1027
        }
1028
1029
        return (bool) apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this );
1030
    }
1031
1032
    /**
1033
	 * Checks whether the item supports dynamic pricing.
1034
	 *
1035
	 * @since 1.0.19
1036
	 * @return bool
1037
	 */
1038
    public function supports_dynamic_pricing() {
1039
        return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this );
1040
    }
1041
}
1042