Passed
Pull Request — master (#377)
by Brian
11:41
created

WPInv_Item::supports_dynamic_pricing()   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 0
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'             => 'digital',
43
        'vat_class'            => '_standard',
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 wpinv_sanitize_amount( $this->get_prop( 'price', $context ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression return wpinv_sanitize_am...rop('price', $context)) also could return the type string which is incompatible with the documented return type double.
Loading history...
282
	}
283
	
284
	/**
285
	 * Get the inital price of the item.
286
	 *
287
	 * @since 1.0.19
288
	 * @param  string $context View or edit context.
289
	 * @return float
290
	 */
291
	public function get_initial_price( $context = 'view' ) {
292
293
		$price = $this->get_price( $context );
294
295
		if ( $this->has_free_trial() ) {
296
			$price = wpinv_sanitize_amount( 0 );
297
		}
298
299
        return apply_filters( 'wpinv_get_initial_item_price', $price, $this );
300
    }
301
302
    /**
303
	 * Returns a formated price.
304
	 *
305
	 * @since 1.0.19
306
	 * @param  string $context View or edit context.
307
	 * @return string
308
	 */
309
    public function get_the_price() {
310
        return wpinv_price( wpinv_format_amount( $this->get_price() ) );
311
	}
312
313
	/**
314
	 * Returns the formated initial price.
315
	 *
316
	 * @since 1.0.19
317
	 * @param  string $context View or edit context.
318
	 * @return string
319
	 */
320
    public function get_the_initial_price() {
321
        return wpinv_price( wpinv_format_amount( $this->get_initial_price() ) );
322
    }
323
324
    /**
325
	 * Get the VAT rule of the item.
326
	 *
327
	 * @since 1.0.19
328
	 * @param  string $context View or edit context.
329
	 * @return string
330
	 */
331
	public function get_vat_rule( $context = 'view' ) {
332
        return $this->get_prop( 'vat_rule', $context );
333
    }
334
335
    /**
336
	 * Get the VAT class of the item.
337
	 *
338
	 * @since 1.0.19
339
	 * @param  string $context View or edit context.
340
	 * @return string
341
	 */
342
	public function get_vat_class( $context = 'view' ) {
343
        return $this->get_prop( 'vat_class', $context );
344
    }
345
346
    /**
347
	 * Get the type of the item.
348
	 *
349
	 * @since 1.0.19
350
	 * @param  string $context View or edit context.
351
	 * @return string
352
	 */
353
	public function get_type( $context = 'view' ) {
354
        return $this->get_prop( 'type', $context );
355
    }
356
357
    /**
358
	 * Get the custom id of the item.
359
	 *
360
	 * @since 1.0.19
361
	 * @param  string $context View or edit context.
362
	 * @return string
363
	 */
364
	public function get_custom_id( $context = 'view' ) {
365
        return $this->get_prop( 'custom_id', $context );
366
    }
367
368
    /**
369
	 * Get the custom name of the item.
370
	 *
371
	 * @since 1.0.19
372
	 * @param  string $context View or edit context.
373
	 * @return string
374
	 */
375
	public function get_custom_name( $context = 'view' ) {
376
        return $this->get_prop( 'custom_name', $context );
377
    }
378
379
    /**
380
	 * Get the custom singular name of the item.
381
	 *
382
	 * @since 1.0.19
383
	 * @param  string $context View or edit context.
384
	 * @return string
385
	 */
386
	public function get_custom_singular_name( $context = 'view' ) {
387
        return $this->get_prop( 'custom_singular_name', $context );
388
    }
389
390
    /**
391
	 * Checks if an item is editable..
392
	 *
393
	 * @since 1.0.19
394
	 * @param  string $context View or edit context.
395
	 * @return int
396
	 */
397
	public function get_is_editable( $context = 'view' ) {
398
        return (int) $this->get_prop( 'is_editable', $context );
399
    }
400
401
    /**
402
	 * Alias of self::get_is_editable().
403
	 *
404
	 * @since 1.0.19
405
	 * @param  string $context View or edit context.
406
	 * @return int
407
	 */
408
	public function get_editable( $context = 'view' ) {
409
		return $this->get_is_editable( $context );
410
    }
411
412
    /**
413
	 * Checks if dynamic pricing is enabled.
414
	 *
415
	 * @since 1.0.19
416
	 * @param  string $context View or edit context.
417
	 * @return int
418
	 */
419
	public function get_is_dynamic_pricing( $context = 'view' ) {
420
        return (int) $this->get_prop( 'is_dynamic_pricing', $context );
421
    }
422
423
    /**
424
	 * Returns the minimum price if dynamic pricing is enabled.
425
	 *
426
	 * @since 1.0.19
427
	 * @param  string $context View or edit context.
428
	 * @return float
429
	 */
430
	public function get_minimum_price( $context = 'view' ) {
431
        return wpinv_sanitize_amount( $this->get_prop( 'minimum_price', $context ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression return wpinv_sanitize_am...imum_price', $context)) also could return the type string which is incompatible with the documented return type double.
Loading history...
432
    }
433
434
    /**
435
	 * Checks if this is a recurring item.
436
	 *
437
	 * @since 1.0.19
438
	 * @param  string $context View or edit context.
439
	 * @return int
440
	 */
441
	public function get_is_recurring( $context = 'view' ) {
442
        return (int) $this->get_prop( 'is_recurring', $context );
443
	}
444
	
445
	/**
446
	 * Get the recurring price of the item.
447
	 *
448
	 * @since 1.0.19
449
	 * @param  string $context View or edit context.
450
	 * @return float
451
	 */
452
	public function get_recurring_price( $context = 'view' ) {
453
		$price = $this->get_price( $context );
454
        return wpinv_sanitize_amount( apply_filters( 'wpinv_get_recurring_item_price', $price, $this->ID ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression return wpinv_sanitize_am...e', $price, $this->ID)) also could return the type string which is incompatible with the documented return type double.
Loading history...
455
	}
456
457
	/**
458
	 * Get the first renewal date (in timestamps) of the item.
459
	 *
460
	 * @since 1.0.19
461
	 * @return int
462
	 */
463
	public function get_first_renewal_date() {
464
465
		$periods = array(
466
			'D' => 'days',
467
			'W' => 'weeks',
468
			'M' => 'months',
469
			'Y' => 'years',
470
		);
471
472
		$period   = $this->get_recurring_period();
473
		$interval = $this->get_recurring_interval();
474
475
		if ( $this->has_free_trial() ) {
476
			$period   = $this->get_trial_period();
477
			$interval = $this->get_trial_interval();
478
		}
479
480
		$period       = $periods[ $period ];
481
		$interval     = empty( $interval ) ? 1 : $interval;
482
		$next_renewal = strtotime( "+$interval $period", current_time( 'timestamp' ) );
483
        return apply_filters( 'wpinv_get_first_renewal_date', $next_renewal, $this );
484
    }
485
486
    /**
487
	 * Get the recurring period.
488
	 *
489
	 * @since 1.0.19
490
	 * @param  bool $full Return abbreviation or in full.
491
	 * @return string
492
	 */
493
	public function get_recurring_period( $full = false ) {
494
        $period = $this->get_prop( 'recurring_period', 'view' );
495
496
        if ( $full && ! is_bool( $full ) ) {
0 ignored issues
show
introduced by
The condition is_bool($full) is always true.
Loading history...
497
            $full = false;
498
        }
499
500
        return getpaid_sanitize_recurring_period( $period, $full );
501
    }
502
503
    /**
504
	 * Get the recurring interval.
505
	 *
506
	 * @since 1.0.19
507
	 * @param  string $context View or edit context.
508
	 * @return int
509
	 */
510
	public function get_recurring_interval( $context = 'view' ) {
511
		$interval = absint( $this->get_prop( 'recurring_interval', $context ) );
512
513
		if ( $interval < 1 ) {
514
			$interval = 1;
515
		}
516
517
        return $interval;
518
    }
519
520
    /**
521
	 * Get the recurring limit.
522
	 *
523
	 * @since 1.0.19
524
	 * @param  string $context View or edit context.
525
	 * @return int
526
	 */
527
	public function get_recurring_limit( $context = 'view' ) {
528
        return (int) $this->get_prop( 'recurring_limit', $context );
529
    }
530
531
    /**
532
	 * Checks if we have a free trial.
533
	 *
534
	 * @since 1.0.19
535
	 * @param  string $context View or edit context.
536
	 * @return int
537
	 */
538
	public function get_is_free_trial( $context = 'view' ) {
539
        return (int) $this->get_prop( 'is_free_trial', $context );
540
    }
541
542
    /**
543
	 * Alias for self::get_is_free_trial().
544
	 *
545
	 * @since 1.0.19
546
	 * @param  string $context View or edit context.
547
	 * @return int
548
	 */
549
	public function get_free_trial( $context = 'view' ) {
550
        return $this->get_is_free_trial( $context );
551
    }
552
553
    /**
554
	 * Get the trial period.
555
	 *
556
	 * @since 1.0.19
557
	 * @param  bool $full Return abbreviation or in full.
558
	 * @return string
559
	 */
560
	public function get_trial_period( $full = false ) {
561
        $period = $this->get_prop( 'trial_period', 'view' );
562
563
        if ( $full && ! is_bool( $full ) ) {
0 ignored issues
show
introduced by
The condition is_bool($full) is always true.
Loading history...
564
            $full = false;
565
        }
566
567
        return getpaid_sanitize_recurring_period( $period, $full );
568
    }
569
570
    /**
571
	 * Get the trial interval.
572
	 *
573
	 * @since 1.0.19
574
	 * @param  string $context View or edit context.
575
	 * @return int
576
	 */
577
	public function get_trial_interval( $context = 'view' ) {
578
        return (int) $this->get_prop( 'trial_interval', $context );
579
	}
580
	
581
	/**
582
	 * Get the item's edit url.
583
	 *
584
	 * @since 1.0.19
585
	 * @return string
586
	 */
587
	public function get_edit_url() {
588
        return get_edit_post_link( $this->get_id() );
589
	}
590
591
    /**
592
     * Margic method for retrieving a property.
593
     */
594
    public function __get( $key ) {
595
596
        // Check if we have a helper method for that.
597
        if ( method_exists( $this, 'get_' . $key ) ) {
598
            return call_user_func( array( $this, 'get_' . $key ) );
599
        }
600
601
        // Check if the key is in the associated $post object.
602
        if ( ! empty( $this->post ) && isset( $this->post->$key ) ) {
603
            return $this->post->$key;
604
        }
605
606
        return $this->get_prop( $key );
607
608
    }
609
610
    /*
611
	|--------------------------------------------------------------------------
612
	| Setters
613
	|--------------------------------------------------------------------------
614
	|
615
	| Functions for setting item data. These should not update anything in the
616
	| database itself and should only change what is stored in the class
617
	| object.
618
    */
619
620
    /**
621
	 * Set parent order ID.
622
	 *
623
	 * @since 1.0.19
624
	 */
625
	public function set_parent_id( $value ) {
626
		if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) {
627
			return;
628
		}
629
		$this->set_prop( 'parent_id', absint( $value ) );
630
	}
631
632
    /**
633
	 * Sets item status.
634
	 *
635
	 * @since 1.0.19
636
	 * @param  string $status New status.
637
	 * @return array details of change.
638
	 */
639
	public function set_status( $status ) {
640
        $old_status = $this->get_status();
641
642
        $this->set_prop( 'status', $status );
643
644
		return array(
645
			'from' => $old_status,
646
			'to'   => $status,
647
		);
648
    }
649
650
    /**
651
	 * Set plugin version when the item was created.
652
	 *
653
	 * @since 1.0.19
654
	 */
655
	public function set_version( $value ) {
656
		$this->set_prop( 'version', $value );
657
    }
658
659
    /**
660
	 * Set date when the item was created.
661
	 *
662
	 * @since 1.0.19
663
	 * @param string $value Value to set.
664
     * @return bool Whether or not the date was set.
665
	 */
666
	public function set_date_created( $value ) {
667
        $date = strtotime( $value );
668
669
        if ( $date ) {
670
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
671
            return true;
672
        }
673
674
        return false;
675
676
    }
677
678
    /**
679
	 * Set date when the item was last modified.
680
	 *
681
	 * @since 1.0.19
682
	 * @param string $value Value to set.
683
     * @return bool Whether or not the date was set.
684
	 */
685
	public function set_date_modified( $value ) {
686
        $date = strtotime( $value );
687
688
        if ( $date ) {
689
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
690
            return true;
691
        }
692
693
        return false;
694
695
    }
696
697
    /**
698
	 * Set the item name.
699
	 *
700
	 * @since 1.0.19
701
	 * @param  string $value New name.
702
	 */
703
	public function set_name( $value ) {
704
        $name = sanitize_text_field( $value );
705
		$this->set_prop( 'name', $name );
706
    }
707
708
    /**
709
	 * Alias of self::set_name().
710
	 *
711
	 * @since 1.0.19
712
	 * @param  string $value New name.
713
	 */
714
	public function set_title( $value ) {
715
		$this->set_name( $value );
716
    }
717
718
    /**
719
	 * Set the item description.
720
	 *
721
	 * @since 1.0.19
722
	 * @param  string $value New description.
723
	 */
724
	public function set_description( $value ) {
725
        $description = wp_kses_post( $value );
726
		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...
727
    }
728
729
    /**
730
	 * Alias of self::set_description().
731
	 *
732
	 * @since 1.0.19
733
	 * @param  string $value New description.
734
	 */
735
	public function set_excerpt( $value ) {
736
		$this->set_description( $value );
737
    }
738
739
    /**
740
	 * Alias of self::set_description().
741
	 *
742
	 * @since 1.0.19
743
	 * @param  string $value New description.
744
	 */
745
	public function set_summary( $value ) {
746
		$this->set_description( $value );
747
    }
748
749
    /**
750
	 * Set the owner of the item.
751
	 *
752
	 * @since 1.0.19
753
	 * @param  int $value New author.
754
	 */
755
	public function set_author( $value ) {
756
		$this->set_prop( 'author', (int) $value );
757
    }
758
759
    /**
760
	 * Set the price of the item.
761
	 *
762
	 * @since 1.0.19
763
	 * @param  float $value New price.
764
	 */
765
	public function set_price( $value ) {
766
        $this->set_prop( 'price', (float) wpinv_sanitize_amount( $value ) );
767
    }
768
769
    /**
770
	 * Set the VAT rule of the item.
771
	 *
772
	 * @since 1.0.19
773
	 * @param  string $value new rule.
774
	 */
775
	public function set_vat_rule( $value ) {
776
        $this->set_prop( 'vat_rule', $value );
777
    }
778
779
    /**
780
	 * Set the VAT class of the item.
781
	 *
782
	 * @since 1.0.19
783
	 * @param  string $value new class.
784
	 */
785
	public function set_vat_class( $value ) {
786
        $this->set_prop( 'vat_class', $value );
787
    }
788
789
    /**
790
	 * Set the type of the item.
791
	 *
792
	 * @since 1.0.19
793
	 * @param  string $value new item type.
794
	 * @return string
795
	 */
796
	public function set_type( $value ) {
797
798
        if ( empty( $value ) ) {
799
            $value = 'custom';
800
        }
801
802
        $this->set_prop( 'type', $value );
803
    }
804
805
    /**
806
	 * Set the custom id of the item.
807
	 *
808
	 * @since 1.0.19
809
	 * @param  string $value new custom id.
810
	 */
811
	public function set_custom_id( $value ) {
812
        $this->set_prop( 'custom_id', $value );
813
    }
814
815
    /**
816
	 * Set the custom name of the item.
817
	 *
818
	 * @since 1.0.19
819
	 * @param  string $value new custom name.
820
	 */
821
	public function set_custom_name( $value ) {
822
        $this->set_prop( 'custom_name', $value );
823
    }
824
825
    /**
826
	 * Set the custom singular name of the item.
827
	 *
828
	 * @since 1.0.19
829
	 * @param  string $value new custom singular name.
830
	 */
831
	public function set_custom_singular_name( $value ) {
832
        $this->set_prop( 'custom_singular_name', $value );
833
    }
834
835
    /**
836
	 * Sets if an item is editable..
837
	 *
838
	 * @since 1.0.19
839
	 * @param  int|bool $value whether or not the item is editable.
840
	 */
841
	public function set_is_editable( $value ) {
842
		if ( is_numeric( $value ) ) {
843
			$this->set_prop( 'is_editable', (int) $value );
844
		}
845
    }
846
847
    /**
848
	 * Sets if dynamic pricing is enabled.
849
	 *
850
	 * @since 1.0.19
851
	 * @param  int|bool $value whether or not dynamic pricing is allowed.
852
	 */
853
	public function set_is_dynamic_pricing( $value ) {
854
        $this->set_prop( 'is_dynamic_pricing', (int) $value );
855
    }
856
857
    /**
858
	 * Sets the minimum price if dynamic pricing is enabled.
859
	 *
860
	 * @since 1.0.19
861
	 * @param  float $value minimum price.
862
	 */
863
	public function set_minimum_price( $value ) {
864
        $this->set_prop( 'minimum_price',  (float) wpinv_sanitize_amount( $value ) );
865
    }
866
867
    /**
868
	 * Sets if this is a recurring item.
869
	 *
870
	 * @since 1.0.19
871
	 * @param  int|bool $value whether or not dynamic pricing is allowed.
872
	 */
873
	public function set_is_recurring( $value ) {
874
        $this->set_prop( 'is_recurring', (int) $value );
875
    }
876
877
    /**
878
	 * Set the recurring period.
879
	 *
880
	 * @since 1.0.19
881
	 * @param  string $value new period.
882
	 */
883
	public function set_recurring_period( $value ) {
884
        $this->set_prop( 'recurring_period', $value );
885
    }
886
887
    /**
888
	 * Set the recurring interval.
889
	 *
890
	 * @since 1.0.19
891
	 * @param  int $value recurring interval.
892
	 */
893
	public function set_recurring_interval( $value ) {
894
        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...
895
    }
896
897
    /**
898
	 * Get the recurring limit.
899
	 * @since 1.0.19
900
	 * @param  int $value The recurring limit.
901
	 * @return int
902
	 */
903
	public function set_recurring_limit( $value ) {
904
        $this->set_prop( 'recurring_limit', (int) $value );
905
    }
906
907
    /**
908
	 * Checks if we have a free trial.
909
	 *
910
	 * @since 1.0.19
911
	 * @param  int|bool $value whether or not it has a free trial.
912
	 */
913
	public function set_is_free_trial( $value ) {
914
        $this->set_prop( 'is_free_trial', (int) $value );
915
    }
916
917
    /**
918
	 * Set the trial period.
919
	 *
920
	 * @since 1.0.19
921
	 * @param  string $value trial period.
922
	 */
923
	public function set_trial_period( $value ) {
924
        $this->set_prop( 'trial_period', $value );
925
    }
926
927
    /**
928
	 * Set the trial interval.
929
	 *
930
	 * @since 1.0.19
931
	 * @param  int $value trial interval.
932
	 */
933
	public function set_trial_interval( $value ) {
934
        $this->set_prop( 'trial_interval', $value );
935
    }
936
937
    /**
938
     * Create an item. For backwards compatibilty.
939
     * 
940
     * @deprecated
941
	 * @return int item id
942
     */
943
    public function create( $data = array() ) {
944
945
		// Set the properties.
946
		if ( is_array( $data ) ) {
947
			$this->set_props( $data );
948
		}
949
950
		// Save the item.
951
		return $this->save();
952
953
    }
954
955
    /**
956
     * Updates an item. For backwards compatibilty.
957
     * 
958
     * @deprecated
959
	 * @return int item id
960
     */
961
    public function update( $data = array() ) {
962
        return $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

962
        return /** @scrutinizer ignore-deprecated */ $this->create( $data );
Loading history...
963
    }
964
965
    /*
966
	|--------------------------------------------------------------------------
967
	| Conditionals
968
	|--------------------------------------------------------------------------
969
	|
970
	| Checks if a condition is true or false.
971
	|
972
	*/
973
974
    /**
975
	 * Checks whether the item has enabled dynamic pricing.
976
	 *
977
	 * @since 1.0.19
978
	 * @return bool
979
	 */
980
	public function user_can_set_their_price() {
981
        return (bool) $this->get_is_dynamic_pricing();
982
	}
983
	
984
	/**
985
	 * Checks whether the item is recurring.
986
	 *
987
	 * @since 1.0.19
988
	 * @return bool
989
	 */
990
	public function is_recurring() {
991
        return (bool) $this->get_is_recurring();
992
    }
993
994
    /**
995
	 * Checks whether the item has a free trial.
996
	 *
997
	 * @since 1.0.19
998
	 * @return bool
999
	 */
1000
    public function has_free_trial() {
1001
        $has_trial = $this->is_recurring() && (bool) $this->get_free_trial() ? true : false;
1002
        return (bool) apply_filters( 'wpinv_item_has_free_trial', $has_trial, $this->ID, $this );
1003
    }
1004
1005
    /**
1006
	 * Checks whether the item is free.
1007
	 *
1008
	 * @since 1.0.19
1009
	 * @return bool
1010
	 */
1011
    public function is_free() {
1012
        $is_free   = $this->get_price() == 0;
1013
        return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID, $this );
1014
    }
1015
1016
    /**
1017
	 * Checks the item status against a passed in status.
1018
	 *
1019
	 * @param array|string $status Status to check.
1020
	 * @return bool
1021
	 */
1022
	public function has_status( $status ) {
1023
		$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...
1024
		return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status );
1025
    }
1026
1027
    /**
1028
	 * Checks the item type against a passed in types.
1029
	 *
1030
	 * @param array|string $type Type to check.
1031
	 * @return bool
1032
	 */
1033
	public function is_type( $type ) {
1034
		$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...
1035
		return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type );
1036
	}
1037
1038
    /**
1039
	 * Checks whether the item is editable.
1040
	 *
1041
	 * @since 1.0.19
1042
	 * @return bool
1043
	 */
1044
    public function is_editable() {
1045
        $is_editable = $this->get_is_editable();
1046
        return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID, $this );
1047
	}
1048
1049
	/**
1050
	 * Returns an array of cart fees.
1051
	 */
1052
	public function get_fees( $type = 'fee', $item_id = 0 ) {
1053
        global $wpi_session;
1054
        
1055
        $fees = $wpi_session->get( 'wpi_cart_fees' );
1056
1057
        if ( ! wpinv_get_cart_contents() ) {
1058
            // We can only get item type fees when the cart is empty
1059
            $type = 'custom';
1060
        }
1061
1062
        if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) {
1063
            foreach( $fees as $key => $fee ) {
1064
                if( ! empty( $fee['type'] ) && $type != $fee['type'] ) {
1065
                    unset( $fees[ $key ] );
1066
                }
1067
            }
1068
        }
1069
1070
        if ( ! empty( $fees ) && ! empty( $item_id ) ) {
1071
            // Remove fees that don't belong to the specified Item
1072
            foreach ( $fees as $key => $fee ) {
1073
                if ( (int) $item_id !== (int)$fee['custom_id'] ) {
1074
                    unset( $fees[ $key ] );
1075
                }
1076
            }
1077
        }
1078
1079
        if ( ! empty( $fees ) ) {
1080
            // Remove fees that belong to a specific item but are not in the cart
1081
            foreach( $fees as $key => $fee ) {
1082
                if( empty( $fee['custom_id'] ) ) {
1083
                    continue;
1084
                }
1085
1086
                if ( !wpinv_item_in_cart( $fee['custom_id'] ) ) {
1087
                    unset( $fees[ $key ] );
1088
                }
1089
            }
1090
        }
1091
1092
        return ! empty( $fees ) ? $fees : array();
1093
    }
1094
1095
    /**
1096
	 * Checks whether the item is purchasable.
1097
	 *
1098
	 * @since 1.0.19
1099
	 * @return bool
1100
	 */
1101
    public function can_purchase() {
1102
        $can_purchase = null !== $this->get_id();
1103
1104
        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...
1105
            $can_purchase = false;
1106
        }
1107
1108
        return (bool) apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this );
1109
    }
1110
1111
    /**
1112
	 * Checks whether the item supports dynamic pricing.
1113
	 *
1114
	 * @since 1.0.19
1115
	 * @return bool
1116
	 */
1117
    public function supports_dynamic_pricing() {
1118
        return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this );
1119
    }
1120
}
1121