Passed
Pull Request — master (#518)
by
unknown
06:06
created

WPInv_Item::set_trial_interval()   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'             => '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
		'is_one_time_recurring'=> null,
53
        'recurring_period'     => null,
54
        'recurring_interval'   => null,
55
        'recurring_limit'      => null,
56
        'is_free_trial'        => null,
57
        'trial_period'         => null,
58
        'trial_interval'       => null,
59
    );
60
61
    /**
62
	 * Stores meta in cache for future reads.
63
	 *
64
	 * A group must be set to to enable caching.
65
	 *
66
	 * @var string
67
	 */
68
	protected $cache_group = 'getpaid_items';
69
70
    /**
71
     * Stores a reference to the original WP_Post object
72
     * 
73
     * @var WP_Post
74
     */
75
    protected $post = null;
76
77
    /**
78
	 * Get the item if ID is passed, otherwise the item is new and empty.
79
	 *
80
	 * @param  int|object|WPInv_Item|WP_Post $item Item to read.
81
	 */
82
	public function __construct( $item = 0 ) {
83
		parent::__construct( $item );
84
85
		if ( ! empty( $item ) && is_numeric( $item ) && 'wpi_item' == get_post_type( $item ) ) {
86
			$this->set_id( $item );
87
		} elseif ( $item instanceof self ) {
88
			$this->set_id( $item->get_id() );
89
		} elseif ( ! empty( $item->ID ) ) {
90
			$this->set_id( $item->ID );
91
		} elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'custom_id' ) ) {
92
			$this->set_id( $item_id );
93
		} elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'name' ) ) {
94
			$this->set_id( $item_id );
95
		} else {
96
			$this->set_object_read( true );
97
		}
98
99
        // Load the datastore.
100
		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
101
102
		if ( $this->get_id() > 0 ) {
103
            $this->post = get_post( $this->get_id() );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_post($this->get_id()) can also be of type array. However, the property $post is declared as type WP_Post. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
104
            $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...
105
			$this->data_store->read( $this );
106
        }
107
108
	}
109
110
    /*
111
	|--------------------------------------------------------------------------
112
	| CRUD methods
113
	|--------------------------------------------------------------------------
114
	|
115
	| Methods which create, read, update and delete items from the database.
116
	|
117
    */
118
119
    /*
120
	|--------------------------------------------------------------------------
121
	| Getters
122
	|--------------------------------------------------------------------------
123
    */
124
125
    /**
126
	 * Get parent item ID.
127
	 *
128
	 * @since 1.0.19
129
	 * @param  string $context View or edit context.
130
	 * @return int
131
	 */
132
	public function get_parent_id( $context = 'view' ) {
133
		return (int) $this->get_prop( 'parent_id', $context );
134
    }
135
136
    /**
137
	 * Get item status.
138
	 *
139
	 * @since 1.0.19
140
	 * @param  string $context View or edit context.
141
	 * @return string
142
	 */
143
	public function get_status( $context = 'view' ) {
144
		return $this->get_prop( 'status', $context );
145
    }
146
147
    /**
148
	 * Get plugin version when the item was created.
149
	 *
150
	 * @since 1.0.19
151
	 * @param  string $context View or edit context.
152
	 * @return string
153
	 */
154
	public function get_version( $context = 'view' ) {
155
		return $this->get_prop( 'version', $context );
156
    }
157
158
    /**
159
	 * Get date when the item was created.
160
	 *
161
	 * @since 1.0.19
162
	 * @param  string $context View or edit context.
163
	 * @return string
164
	 */
165
	public function get_date_created( $context = 'view' ) {
166
		return $this->get_prop( 'date_created', $context );
167
    }
168
169
    /**
170
	 * Get GMT date when the item was created.
171
	 *
172
	 * @since 1.0.19
173
	 * @param  string $context View or edit context.
174
	 * @return string
175
	 */
176
	public function get_date_created_gmt( $context = 'view' ) {
177
        $date = $this->get_date_created( $context );
178
179
        if ( $date ) {
180
            $date = get_gmt_from_date( $date );
181
        }
182
		return $date;
183
    }
184
185
    /**
186
	 * Get date when the item was last modified.
187
	 *
188
	 * @since 1.0.19
189
	 * @param  string $context View or edit context.
190
	 * @return string
191
	 */
192
	public function get_date_modified( $context = 'view' ) {
193
		return $this->get_prop( 'date_modified', $context );
194
    }
195
196
    /**
197
	 * Get GMT date when the item was last modified.
198
	 *
199
	 * @since 1.0.19
200
	 * @param  string $context View or edit context.
201
	 * @return string
202
	 */
203
	public function get_date_modified_gmt( $context = 'view' ) {
204
        $date = $this->get_date_modified( $context );
205
206
        if ( $date ) {
207
            $date = get_gmt_from_date( $date );
208
        }
209
		return $date;
210
    }
211
212
    /**
213
	 * Get the item name.
214
	 *
215
	 * @since 1.0.19
216
	 * @param  string $context View or edit context.
217
	 * @return string
218
	 */
219
	public function get_name( $context = 'view' ) {
220
		return $this->get_prop( 'name', $context );
221
    }
222
223
    /**
224
	 * Alias of self::get_name().
225
	 *
226
	 * @since 1.0.19
227
	 * @param  string $context View or edit context.
228
	 * @return string
229
	 */
230
	public function get_title( $context = 'view' ) {
231
		return $this->get_name( $context );
232
    }
233
234
    /**
235
	 * Get the item description.
236
	 *
237
	 * @since 1.0.19
238
	 * @param  string $context View or edit context.
239
	 * @return string
240
	 */
241
	public function get_description( $context = 'view' ) {
242
		return $this->get_prop( 'description', $context );
243
    }
244
245
    /**
246
	 * Alias of self::get_description().
247
	 *
248
	 * @since 1.0.19
249
	 * @param  string $context View or edit context.
250
	 * @return string
251
	 */
252
	public function get_excerpt( $context = 'view' ) {
253
		return $this->get_description( $context );
254
    }
255
256
    /**
257
	 * Alias of self::get_description().
258
	 *
259
	 * @since 1.0.19
260
	 * @param  string $context View or edit context.
261
	 * @return string
262
	 */
263
	public function get_summary( $context = 'view' ) {
264
		return $this->get_description( $context );
265
    }
266
267
    /**
268
	 * Get the owner of the item.
269
	 *
270
	 * @since 1.0.19
271
	 * @param  string $context View or edit context.
272
	 * @return int
273
	 */
274
	public function get_author( $context = 'view' ) {
275
		return (int) $this->get_prop( 'author', $context );
276
	}
277
	
278
	/**
279
	 * Alias of self::get_author().
280
	 *
281
	 * @since 1.0.19
282
	 * @param  string $context View or edit context.
283
	 * @return int
284
	 */
285
	public function get_owner( $context = 'view' ) {
286
		return $this->get_author( $context );
287
    }
288
289
    /**
290
	 * Get the price of the item.
291
	 *
292
	 * @since 1.0.19
293
	 * @param  string $context View or edit context.
294
	 * @return float
295
	 */
296
	public function get_price( $context = 'view' ) {
297
        return wpinv_sanitize_amount( $this->get_prop( 'price', $context ) );
298
	}
299
	
300
	/**
301
	 * Get the inital price of the item.
302
	 *
303
	 * @since 1.0.19
304
	 * @param  string $context View or edit context.
305
	 * @return float
306
	 */
307
	public function get_initial_price( $context = 'view' ) {
308
309
		$price = (float) $this->get_price( $context );
310
311
		if ( $this->has_free_trial() ) {
312
			$price = 0;
313
		}
314
315
        return wpinv_sanitize_amount( apply_filters( 'wpinv_get_initial_item_price', $price, $this ) );
316
    }
317
318
    /**
319
	 * Returns a formated price.
320
	 *
321
	 * @since 1.0.19
322
	 * @param  string $context View or edit context.
323
	 * @return string
324
	 */
325
    public function get_the_price() {
326
        return wpinv_price( $this->get_price() );
327
	}
328
329
	/**
330
	 * Returns the formated initial price.
331
	 *
332
	 * @since 1.0.19
333
	 * @param  string $context View or edit context.
334
	 * @return string
335
	 */
336
    public function get_the_initial_price() {
337
        return wpinv_price( $this->get_initial_price() );
338
    }
339
340
    /**
341
	 * Get the VAT rule of the item.
342
	 *
343
	 * @since 1.0.19
344
	 * @param  string $context View or edit context.
345
	 * @return string
346
	 */
347
	public function get_vat_rule( $context = 'view' ) {
348
        return $this->get_prop( 'vat_rule', $context );
349
    }
350
351
    /**
352
	 * Get the VAT class of the item.
353
	 *
354
	 * @since 1.0.19
355
	 * @param  string $context View or edit context.
356
	 * @return string
357
	 */
358
	public function get_vat_class( $context = 'view' ) {
359
        return $this->get_prop( 'vat_class', $context );
360
    }
361
362
    /**
363
	 * Get the type of the item.
364
	 *
365
	 * @since 1.0.19
366
	 * @param  string $context View or edit context.
367
	 * @return string
368
	 */
369
	public function get_type( $context = 'view' ) {
370
        return $this->get_prop( 'type', $context );
371
    }
372
373
    /**
374
	 * Get the custom id of the item.
375
	 *
376
	 * @since 1.0.19
377
	 * @param  string $context View or edit context.
378
	 * @return string
379
	 */
380
	public function get_custom_id( $context = 'view' ) {
381
        return $this->get_prop( 'custom_id', $context );
382
    }
383
384
    /**
385
	 * Get the custom name of the item.
386
	 *
387
	 * @since 1.0.19
388
	 * @param  string $context View or edit context.
389
	 * @return string
390
	 */
391
	public function get_custom_name( $context = 'view' ) {
392
        return $this->get_prop( 'custom_name', $context );
393
    }
394
395
    /**
396
	 * Get the custom singular name of the item.
397
	 *
398
	 * @since 1.0.19
399
	 * @param  string $context View or edit context.
400
	 * @return string
401
	 */
402
	public function get_custom_singular_name( $context = 'view' ) {
403
        return $this->get_prop( 'custom_singular_name', $context );
404
    }
405
406
    /**
407
	 * Checks if an item is editable..
408
	 *
409
	 * @since 1.0.19
410
	 * @param  string $context View or edit context.
411
	 * @return int
412
	 */
413
	public function get_is_editable( $context = 'view' ) {
414
        return (int) $this->get_prop( 'is_editable', $context );
415
    }
416
417
    /**
418
	 * Alias of self::get_is_editable().
419
	 *
420
	 * @since 1.0.19
421
	 * @param  string $context View or edit context.
422
	 * @return int
423
	 */
424
	public function get_editable( $context = 'view' ) {
425
		return $this->get_is_editable( $context );
426
    }
427
428
    /**
429
	 * Checks if dynamic pricing is enabled.
430
	 *
431
	 * @since 1.0.19
432
	 * @param  string $context View or edit context.
433
	 * @return int
434
	 */
435
	public function get_is_dynamic_pricing( $context = 'view' ) {
436
        return (int) $this->get_prop( 'is_dynamic_pricing', $context );
437
    }
438
439
    /**
440
	 * Returns the minimum price if dynamic pricing is enabled.
441
	 *
442
	 * @since 1.0.19
443
	 * @param  string $context View or edit context.
444
	 * @return float
445
	 */
446
	public function get_minimum_price( $context = 'view' ) {
447
        return wpinv_sanitize_amount( $this->get_prop( 'minimum_price', $context ) );
448
    }
449
450
    /**
451
	 * Checks if this is a recurring item.
452
	 *
453
	 * @since 1.0.19
454
	 * @param  string $context View or edit context.
455
	 * @return int
456
	 */
457
	public function get_is_recurring( $context = 'view' ) {
458
        return (int) $this->get_prop( 'is_recurring', $context );
459
	}
460
461
	/**
462
	 * Checks if this is a one time recurring item.
463
	 *
464
	 * @since 1.0.19
465
	 * @param  string $context View or edit context.
466
	 * @return int
467
	 */
468
	public function get_is_one_time_recurring( $context = 'view' ) {
469
        return (int) $this->get_prop( 'is_one_time_recurring', $context );
470
	}
471
	
472
	/**
473
	 * Get the recurring price of the item.
474
	 *
475
	 * @since 1.0.19
476
	 * @param  string $context View or edit context.
477
	 * @return float
478
	 */
479
	public function get_recurring_price( $context = 'view' ) {
480
		$price = $this->get_price( $context );
481
        return wpinv_sanitize_amount( apply_filters( 'wpinv_get_recurring_item_price', $price, $this->ID ) );
482
	}
483
484
	/**
485
	 * Get the formatted recurring price of the item.
486
	 *
487
	 * @since 1.0.19
488
	 * @param  string $context View or edit context.
489
	 * @return string
490
	 */
491
    public function get_the_recurring_price() {
492
        return wpinv_price( $this->get_recurring_price() );
493
	}
494
495
	/**
496
	 * Get the first renewal date (in timestamps) of the item.
497
	 *
498
	 * @since 1.0.19
499
	 * @return int
500
	 */
501
	public function get_first_renewal_date() {
502
503
		$periods = array(
504
			'D' => 'days',
505
			'W' => 'weeks',
506
			'M' => 'months',
507
			'Y' => 'years',
508
		);
509
510
		$period   = $this->get_recurring_period();
511
		$interval = $this->get_recurring_interval();
512
513
		if ( $this->has_free_trial() ) {
514
			$period   = $this->get_trial_period();
515
			$interval = $this->get_trial_interval();
516
		}
517
518
		$period       = $periods[ $period ];
519
		$interval     = empty( $interval ) ? 1 : $interval;
520
		$next_renewal = strtotime( "+$interval $period", current_time( 'timestamp' ) );
521
        return apply_filters( 'wpinv_get_first_renewal_date', $next_renewal, $this );
522
    }
523
524
    /**
525
	 * Get the recurring period.
526
	 *
527
	 * @since 1.0.19
528
	 * @param  bool $full Return abbreviation or in full.
529
	 * @return string
530
	 */
531
	public function get_recurring_period( $full = false ) {
532
        $period = $this->get_prop( 'recurring_period', 'view' );
533
534
        if ( $full && ! is_bool( $full ) ) {
0 ignored issues
show
introduced by
The condition is_bool($full) is always true.
Loading history...
535
            $full = false;
536
        }
537
538
        return getpaid_sanitize_recurring_period( $period, $full );
539
    }
540
541
    /**
542
	 * Get the recurring interval.
543
	 *
544
	 * @since 1.0.19
545
	 * @param  string $context View or edit context.
546
	 * @return int
547
	 */
548
	public function get_recurring_interval( $context = 'view' ) {
549
		$interval = absint( $this->get_prop( 'recurring_interval', $context ) );
550
551
		if ( $interval < 1 ) {
552
			$interval = 1;
553
		}
554
555
        return $interval;
556
    }
557
558
    /**
559
	 * Get the recurring limit.
560
	 *
561
	 * @since 1.0.19
562
	 * @param  string $context View or edit context.
563
	 * @return int
564
	 */
565
	public function get_recurring_limit( $context = 'view' ) {
566
        return (int) $this->get_prop( 'recurring_limit', $context );
567
    }
568
569
    /**
570
	 * Checks if we have a free trial.
571
	 *
572
	 * @since 1.0.19
573
	 * @param  string $context View or edit context.
574
	 * @return int
575
	 */
576
	public function get_is_free_trial( $context = 'view' ) {
577
        return (int) $this->get_prop( 'is_free_trial', $context );
578
    }
579
580
    /**
581
	 * Alias for self::get_is_free_trial().
582
	 *
583
	 * @since 1.0.19
584
	 * @param  string $context View or edit context.
585
	 * @return int
586
	 */
587
	public function get_free_trial( $context = 'view' ) {
588
        return $this->get_is_free_trial( $context );
589
    }
590
591
    /**
592
	 * Get the trial period.
593
	 *
594
	 * @since 1.0.19
595
	 * @param  bool $full Return abbreviation or in full.
596
	 * @return string
597
	 */
598
	public function get_trial_period( $full = false ) {
599
        $period = $this->get_prop( 'trial_period', 'view' );
600
601
        if ( $full && ! is_bool( $full ) ) {
0 ignored issues
show
introduced by
The condition is_bool($full) is always true.
Loading history...
602
            $full = false;
603
        }
604
605
        return getpaid_sanitize_recurring_period( $period, $full );
606
    }
607
608
    /**
609
	 * Get the trial interval.
610
	 *
611
	 * @since 1.0.19
612
	 * @param  string $context View or edit context.
613
	 * @return int
614
	 */
615
	public function get_trial_interval( $context = 'view' ) {
616
        return (int) $this->get_prop( 'trial_interval', $context );
617
	}
618
	
619
	/**
620
	 * Get the item's edit url.
621
	 *
622
	 * @since 1.0.19
623
	 * @return string
624
	 */
625
	public function get_edit_url() {
626
        return get_edit_post_link( $this->get_id() );
627
	}
628
629
	/**
630
	 * Given an item's name/custom id, it returns its id.
631
	 *
632
	 *
633
	 * @static
634
	 * @param string $value The item name or custom id.
635
	 * @param string $field Either name or custom_id.
636
	 * @param string $type in case you need to search for a given type.
637
	 * @since 1.0.15
638
	 * @return int
639
	 */
640
	public static function get_item_id_by_field( $value, $field = 'custom_id', $type = '' ) {
641
642
		// Trim the value.
643
		$value = sanitize_text_field( $value );
644
		if ( empty( $value ) ) {
645
			return 0;
646
		}
647
648
        // Valid fields.
649
        $fields = array( 'custom_id', 'name', 'slug' );
650
651
		// Ensure a field has been passed.
652
		if ( empty( $field ) || ! in_array( $field, $fields ) ) {
653
			return 0;
654
		}
655
656
		if ( $field == 'name' ) {
657
			$field = 'slug';
658
		}
659
660
		// Maybe retrieve from the cache.
661
		$item_id = wp_cache_get( $value, "getpaid_{$type}_item_{$field}s_to_item_ids" );
662
		if ( ! empty( $item_id ) ) {
663
			return $item_id;
664
		}
665
666
		// Fetch from the db.
667
		$items = array();
668
		if ( $field == 'slug' ) {
669
			$items = get_posts(
670
				array(
671
					'post_type'      => 'wpi_item',
672
					'name'           => $value,
673
					'posts_per_page' => 1,
674
					'post_status'    => 'any',
675
				)
676
			);
677
		}
678
679
		if ( $field =='custom_id' ) {
680
			$items = get_posts(
681
				array(
682
					'post_type'      => 'wpi_item',
683
					'posts_per_page' => 1,
684
					'post_status'    => 'any',
685
					'meta_query'     => array(
686
						array(
687
							'key'   => '_wpinv_type',
688
                			'value' => $type,
689
						),
690
						array(
691
							'key'   => '_wpinv_custom_id',
692
                			'value' => $value,
693
						)
694
					)
695
				)
696
			);
697
		}
698
699
		if ( empty( $items ) ) {
700
			return 0;
701
		}
702
703
		// Update the cache with our data
704
		wp_cache_set( $value, $items[0]->ID, "getpaid_{$type}_item_{$field}s_to_item_ids" );
705
706
		return $items[0]->ID;
707
    }
708
709
    /**
710
     * Margic method for retrieving a property.
711
     */
712
    public function __get( $key ) {
713
714
        // Check if we have a helper method for that.
715
        if ( method_exists( $this, 'get_' . $key ) ) {
716
            return call_user_func( array( $this, 'get_' . $key ) );
717
        }
718
719
        // Check if the key is in the associated $post object.
720
        if ( ! empty( $this->post ) && isset( $this->post->$key ) ) {
721
            return $this->post->$key;
722
        }
723
724
        return $this->get_prop( $key );
725
726
    }
727
728
    /*
729
	|--------------------------------------------------------------------------
730
	| Setters
731
	|--------------------------------------------------------------------------
732
	|
733
	| Functions for setting item data. These should not update anything in the
734
	| database itself and should only change what is stored in the class
735
	| object.
736
    */
737
738
    /**
739
	 * Set parent order ID.
740
	 *
741
	 * @since 1.0.19
742
	 */
743
	public function set_parent_id( $value ) {
744
		if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) {
745
			return;
746
		}
747
		$this->set_prop( 'parent_id', absint( $value ) );
748
	}
749
750
    /**
751
	 * Sets item status.
752
	 *
753
	 * @since 1.0.19
754
	 * @param  string $status New status.
755
	 * @return array details of change.
756
	 */
757
	public function set_status( $status ) {
758
        $old_status = $this->get_status();
759
760
        $this->set_prop( 'status', $status );
761
762
		return array(
763
			'from' => $old_status,
764
			'to'   => $status,
765
		);
766
    }
767
768
    /**
769
	 * Set plugin version when the item was created.
770
	 *
771
	 * @since 1.0.19
772
	 */
773
	public function set_version( $value ) {
774
		$this->set_prop( 'version', $value );
775
    }
776
777
    /**
778
	 * Set date when the item was created.
779
	 *
780
	 * @since 1.0.19
781
	 * @param string $value Value to set.
782
     * @return bool Whether or not the date was set.
783
	 */
784
	public function set_date_created( $value ) {
785
        $date = strtotime( $value );
786
787
        if ( $date ) {
788
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
789
            return true;
790
        }
791
792
        return false;
793
794
    }
795
796
    /**
797
	 * Set date when the item was last modified.
798
	 *
799
	 * @since 1.0.19
800
	 * @param string $value Value to set.
801
     * @return bool Whether or not the date was set.
802
	 */
803
	public function set_date_modified( $value ) {
804
        $date = strtotime( $value );
805
806
        if ( $date ) {
807
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
808
            return true;
809
        }
810
811
        return false;
812
813
    }
814
815
    /**
816
	 * Set the item name.
817
	 *
818
	 * @since 1.0.19
819
	 * @param  string $value New name.
820
	 */
821
	public function set_name( $value ) {
822
        $name = sanitize_text_field( $value );
823
		$this->set_prop( 'name', $name );
824
    }
825
826
    /**
827
	 * Alias of self::set_name().
828
	 *
829
	 * @since 1.0.19
830
	 * @param  string $value New name.
831
	 */
832
	public function set_title( $value ) {
833
		$this->set_name( $value );
834
    }
835
836
    /**
837
	 * Set the item description.
838
	 *
839
	 * @since 1.0.19
840
	 * @param  string $value New description.
841
	 */
842
	public function set_description( $value ) {
843
		$description = wp_kses_post( $value );
844
		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...
845
    }
846
847
    /**
848
	 * Alias of self::set_description().
849
	 *
850
	 * @since 1.0.19
851
	 * @param  string $value New description.
852
	 */
853
	public function set_excerpt( $value ) {
854
		$this->set_description( $value );
855
    }
856
857
    /**
858
	 * Alias of self::set_description().
859
	 *
860
	 * @since 1.0.19
861
	 * @param  string $value New description.
862
	 */
863
	public function set_summary( $value ) {
864
		$this->set_description( $value );
865
    }
866
867
    /**
868
	 * Set the owner of the item.
869
	 *
870
	 * @since 1.0.19
871
	 * @param  int $value New author.
872
	 */
873
	public function set_author( $value ) {
874
		$this->set_prop( 'author', (int) $value );
875
	}
876
	
877
	/**
878
	 * Alias of self::set_author().
879
	 *
880
	 * @since 1.0.19
881
	 * @param  int $value New author.
882
	 */
883
	public function set_owner( $value ) {
884
		$this->set_author( $value );
885
    }
886
887
    /**
888
	 * Set the price of the item.
889
	 *
890
	 * @since 1.0.19
891
	 * @param  float $value New price.
892
	 */
893
	public function set_price( $value ) {
894
        $this->set_prop( 'price', (float) wpinv_sanitize_amount( $value ) );
895
    }
896
897
    /**
898
	 * Set the VAT rule of the item.
899
	 *
900
	 * @since 1.0.19
901
	 * @param  string $value new rule.
902
	 */
903
	public function set_vat_rule( $value ) {
904
        $this->set_prop( 'vat_rule', $value );
905
    }
906
907
    /**
908
	 * Set the VAT class of the item.
909
	 *
910
	 * @since 1.0.19
911
	 * @param  string $value new class.
912
	 */
913
	public function set_vat_class( $value ) {
914
        $this->set_prop( 'vat_class', $value );
915
    }
916
917
    /**
918
	 * Set the type of the item.
919
	 *
920
	 * @since 1.0.19
921
	 * @param  string $value new item type.
922
	 * @return string
923
	 */
924
	public function set_type( $value ) {
925
926
        if ( empty( $value ) ) {
927
            $value = 'custom';
928
        }
929
930
        $this->set_prop( 'type', $value );
931
    }
932
933
    /**
934
	 * Set the custom id of the item.
935
	 *
936
	 * @since 1.0.19
937
	 * @param  string $value new custom id.
938
	 */
939
	public function set_custom_id( $value ) {
940
        $this->set_prop( 'custom_id', $value );
941
    }
942
943
    /**
944
	 * Set the custom name of the item.
945
	 *
946
	 * @since 1.0.19
947
	 * @param  string $value new custom name.
948
	 */
949
	public function set_custom_name( $value ) {
950
        $this->set_prop( 'custom_name', $value );
951
    }
952
953
    /**
954
	 * Set the custom singular name of the item.
955
	 *
956
	 * @since 1.0.19
957
	 * @param  string $value new custom singular name.
958
	 */
959
	public function set_custom_singular_name( $value ) {
960
        $this->set_prop( 'custom_singular_name', $value );
961
    }
962
963
    /**
964
	 * Sets if an item is editable..
965
	 *
966
	 * @since 1.0.19
967
	 * @param  int|bool $value whether or not the item is editable.
968
	 */
969
	public function set_is_editable( $value ) {
970
		$this->set_prop( 'is_editable', (int) $value );
971
    }
972
973
    /**
974
	 * Sets if dynamic pricing is enabled.
975
	 *
976
	 * @since 1.0.19
977
	 * @param  int|bool $value whether or not dynamic pricing is allowed.
978
	 */
979
	public function set_is_dynamic_pricing( $value ) {
980
        $this->set_prop( 'is_dynamic_pricing', (int) $value );
981
    }
982
983
    /**
984
	 * Sets the minimum price if dynamic pricing is enabled.
985
	 *
986
	 * @since 1.0.19
987
	 * @param  float $value minimum price.
988
	 */
989
	public function set_minimum_price( $value ) {
990
        $this->set_prop( 'minimum_price',  (float) wpinv_sanitize_amount( $value ) );
991
    }
992
993
    /**
994
	 * Sets if this is a recurring item.
995
	 *
996
	 * @since 1.0.19
997
	 * @param  int|bool $value whether or not dynamic pricing is allowed.
998
	 */
999
	public function set_is_recurring( $value ) {
1000
        $this->set_prop( 'is_recurring', (int) $value );
1001
    }
1002
1003
	/**
1004
	 * Sets if this is a recurring item.
1005
	 *
1006
	 * @since 1.0.19
1007
	 * @param  int|bool $value whether or not dynamic pricing is allowed.
1008
	 */
1009
	public function set_is_one_time_recurring( $value ) {
1010
        $this->set_prop( 'is_one_time_recurring', (int) $value );
1011
    }
1012
1013
    /**
1014
	 * Set the recurring period.
1015
	 *
1016
	 * @since 1.0.19
1017
	 * @param  string $value new period.
1018
	 */
1019
	public function set_recurring_period( $value ) {
1020
        $this->set_prop( 'recurring_period', $value );
1021
    }
1022
1023
    /**
1024
	 * Set the recurring interval.
1025
	 *
1026
	 * @since 1.0.19
1027
	 * @param  int $value recurring interval.
1028
	 */
1029
	public function set_recurring_interval( $value ) {
1030
        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...
1031
    }
1032
1033
    /**
1034
	 * Get the recurring limit.
1035
	 * @since 1.0.19
1036
	 * @param  int $value The recurring limit.
1037
	 * @return int
1038
	 */
1039
	public function set_recurring_limit( $value ) {
1040
        $this->set_prop( 'recurring_limit', (int) $value );
1041
    }
1042
1043
    /**
1044
	 * Checks if we have a free trial.
1045
	 *
1046
	 * @since 1.0.19
1047
	 * @param  int|bool $value whether or not it has a free trial.
1048
	 */
1049
	public function set_is_free_trial( $value ) {
1050
        $this->set_prop( 'is_free_trial', (int) $value );
1051
    }
1052
1053
    /**
1054
	 * Set the trial period.
1055
	 *
1056
	 * @since 1.0.19
1057
	 * @param  string $value trial period.
1058
	 */
1059
	public function set_trial_period( $value ) {
1060
        $this->set_prop( 'trial_period', $value );
1061
    }
1062
1063
    /**
1064
	 * Set the trial interval.
1065
	 *
1066
	 * @since 1.0.19
1067
	 * @param  int $value trial interval.
1068
	 */
1069
	public function set_trial_interval( $value ) {
1070
        $this->set_prop( 'trial_interval', $value );
1071
    }
1072
1073
    /**
1074
     * Create an item. For backwards compatibilty.
1075
     * 
1076
     * @deprecated
1077
	 * @return int item id
1078
     */
1079
    public function create( $data = array() ) {
1080
1081
		// Set the properties.
1082
		if ( is_array( $data ) ) {
1083
			$this->set_props( $data );
1084
		}
1085
1086
		// Save the item.
1087
		return $this->save();
1088
1089
    }
1090
1091
    /**
1092
     * Updates an item. For backwards compatibilty.
1093
     * 
1094
     * @deprecated
1095
	 * @return int item id
1096
     */
1097
    public function update( $data = array() ) {
1098
        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

1098
        return /** @scrutinizer ignore-deprecated */ $this->create( $data );
Loading history...
1099
    }
1100
1101
    /*
1102
	|--------------------------------------------------------------------------
1103
	| Conditionals
1104
	|--------------------------------------------------------------------------
1105
	|
1106
	| Checks if a condition is true or false.
1107
	|
1108
	*/
1109
1110
    /**
1111
	 * Checks whether the item has enabled dynamic pricing.
1112
	 *
1113
	 * @since 1.0.19
1114
	 * @return bool
1115
	 */
1116
	public function user_can_set_their_price() {
1117
        return (bool) $this->get_is_dynamic_pricing();
1118
	}
1119
	
1120
	/**
1121
	 * Checks whether the item is one time recurring.
1122
	 *
1123
	 * @since 1.0.19
1124
	 * @return bool
1125
	 */
1126
	public function is_one_time_recurring() {
1127
        return (bool) $this->get_is_one_time_recurring();
1128
    }
1129
1130
	/**
1131
	 * Checks whether the item is recurring.
1132
	 *
1133
	 * @since 1.0.19
1134
	 * @return bool
1135
	 */
1136
	public function is_recurring() {
1137
        return (bool) $this->get_is_recurring();
1138
    } 
1139
1140
    /**
1141
	 * Checks whether the item has a free trial.
1142
	 *
1143
	 * @since 1.0.19
1144
	 * @return bool
1145
	 */
1146
    public function has_free_trial() {
1147
        $has_trial = $this->is_recurring() && (bool) $this->get_free_trial() ? true : false;
1148
        return (bool) apply_filters( 'wpinv_item_has_free_trial', $has_trial, $this->ID, $this );
1149
    }
1150
1151
    /**
1152
	 * Checks whether the item is free.
1153
	 *
1154
	 * @since 1.0.19
1155
	 * @return bool
1156
	 */
1157
    public function is_free() {
1158
        $is_free   = $this->get_price() == 0;
1159
        return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID, $this );
1160
    }
1161
1162
    /**
1163
	 * Checks the item status against a passed in status.
1164
	 *
1165
	 * @param array|string $status Status to check.
1166
	 * @return bool
1167
	 */
1168
	public function has_status( $status ) {
1169
		$has_status = ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status;
1170
		return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status );
1171
    }
1172
1173
    /**
1174
	 * Checks the item type against a passed in types.
1175
	 *
1176
	 * @param array|string $type Type to check.
1177
	 * @return bool
1178
	 */
1179
	public function is_type( $type ) {
1180
		$is_type = ( is_array( $type ) && in_array( $this->get_type(), $type, true ) ) || $this->get_type() === $type;
1181
		return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type );
1182
	}
1183
1184
    /**
1185
	 * Checks whether the item is editable.
1186
	 *
1187
	 * @since 1.0.19
1188
	 * @return bool
1189
	 */
1190
    public function is_editable() {
1191
        $is_editable = $this->get_is_editable();
1192
        return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID, $this );
1193
	}
1194
1195
	/**
1196
	 * Returns an array of cart fees.
1197
	 */
1198
	public function get_fees() {
1199
        return array();
1200
    }
1201
1202
    /**
1203
	 * Checks whether the item is purchasable.
1204
	 *
1205
	 * @since 1.0.19
1206
	 * @return bool
1207
	 */
1208
    public function can_purchase() {
1209
        $can_purchase = $this->exists();
1210
1211
        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...
1212
            $can_purchase = false;
1213
        }
1214
1215
        return (bool) apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this );
1216
    }
1217
1218
    /**
1219
	 * Checks whether the item supports dynamic pricing.
1220
	 *
1221
	 * @since 1.0.19
1222
	 * @return bool
1223
	 */
1224
    public function supports_dynamic_pricing() {
1225
        return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this );
1226
    }
1227
}
1228