Passed
Pull Request — master (#375)
by Brian
76:01
created

GetPaid_Payment_Form::is_active()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Payment form class
8
 *
9
 */
10
class GetPaid_Payment_Form  extends GetPaid_Data {
11
12
    /**
13
	 * Which data store to load.
14
	 *
15
	 * @var string
16
	 */
17
    protected $data_store_name = 'payment_form';
18
19
    /**
20
	 * This is the name of this object type.
21
	 *
22
	 * @var string
23
	 */
24
	protected $object_type = 'payment_form';
25
26
    /**
27
	 * Form Data array. This is the core form data exposed in APIs.
28
	 *
29
	 * @since 1.0.19
30
	 * @var array
31
	 */
32
	protected $data = array(
33
		'status'               => 'draft',
34
		'version'              => '',
35
		'date_created'         => null,
36
        'date_modified'        => null,
37
        'name'                 => '',
38
        'author'               => 1,
39
        'elements'             => null,
40
		'items'                => null,
41
		'earned'               => 0,
42
		'refunded'             => 0,
43
		'cancelled'            => 0,
44
		'failed'               => 0,
45
	);
46
47
    /**
48
	 * Stores meta in cache for future reads.
49
	 *
50
	 * A group must be set to to enable caching.
51
	 *
52
	 * @var string
53
	 */
54
	protected $cache_group = 'getpaid_forms';
55
56
    /**
57
     * Stores a reference to the original WP_Post object
58
     *
59
     * @var WP_Post
60
     */
61
    protected $post = null;
62
63
    /**
64
	 * Get the item if ID is passed, otherwise the item is new and empty.
65
	 *
66
	 * @param  int|object|WPInv_Item|WP_Post $item Item to read.
67
	 */
68
	public function __construct( $item = 0 ) {
69
		parent::__construct( $item );
70
71
		if ( is_numeric( $item ) && $item > 0 ) {
72
			$this->set_id( $item );
73
		} elseif ( $item instanceof self ) {
74
			$this->set_id( $item->get_id() );
75
		} elseif ( ! empty( $item->ID ) ) {
76
			$this->set_id( $item->ID );
77
		} else {
78
			$this->set_object_read( true );
79
		}
80
81
        // Load the datastore.
82
		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
83
84
		if ( $this->get_id() > 0 ) {
85
            $this->post = get_post( $this->get_id() );
86
			$this->data_store->read( $this );
87
        }
88
89
	}
90
91
    /*
92
	|--------------------------------------------------------------------------
93
	| CRUD methods
94
	|--------------------------------------------------------------------------
95
	|
96
	| Methods which create, read, update and delete items from the database.
97
	|
98
    */
99
100
    /*
101
	|--------------------------------------------------------------------------
102
	| Getters
103
	|--------------------------------------------------------------------------
104
    */
105
106
    /**
107
	 * Get plugin version when the form was created.
108
	 *
109
	 * @since 1.0.19
110
	 * @param  string $context View or edit context.
111
	 * @return string
112
	 */
113
	public function get_version( $context = 'view' ) {
114
		return $this->get_prop( 'version', $context );
115
    }
116
117
    /**
118
	 * Get date when the form was created.
119
	 *
120
	 * @since 1.0.19
121
	 * @param  string $context View or edit context.
122
	 * @return string
123
	 */
124
	public function get_date_created( $context = 'view' ) {
125
		return $this->get_prop( 'date_created', $context );
126
    }
127
128
    /**
129
	 * Get GMT date when the form was created.
130
	 *
131
	 * @since 1.0.19
132
	 * @param  string $context View or edit context.
133
	 * @return string
134
	 */
135
	public function get_date_created_gmt( $context = 'view' ) {
136
        $date = $this->get_date_created( $context );
137
138
        if ( $date ) {
139
            $date = get_gmt_from_date( $date );
140
        }
141
		return $date;
142
    }
143
144
    /**
145
	 * Get date when the form was last modified.
146
	 *
147
	 * @since 1.0.19
148
	 * @param  string $context View or edit context.
149
	 * @return string
150
	 */
151
	public function get_date_modified( $context = 'view' ) {
152
		return $this->get_prop( 'date_modified', $context );
153
    }
154
155
    /**
156
	 * Get GMT date when the form was last modified.
157
	 *
158
	 * @since 1.0.19
159
	 * @param  string $context View or edit context.
160
	 * @return string
161
	 */
162
	public function get_date_modified_gmt( $context = 'view' ) {
163
        $date = $this->get_date_modified( $context );
164
165
        if ( $date ) {
166
            $date = get_gmt_from_date( $date );
167
        }
168
		return $date;
169
    }
170
171
    /**
172
	 * Get the form name.
173
	 *
174
	 * @since 1.0.19
175
	 * @param  string $context View or edit context.
176
	 * @return string
177
	 */
178
	public function get_name( $context = 'view' ) {
179
		return $this->get_prop( 'name', $context );
180
    }
181
182
    /**
183
	 * Alias of self::get_name().
184
	 *
185
	 * @since 1.0.19
186
	 * @param  string $context View or edit context.
187
	 * @return string
188
	 */
189
	public function get_title( $context = 'view' ) {
190
		return $this->get_name( $context );
191
	}
192
193
    /**
194
	 * Get the owner of the form.
195
	 *
196
	 * @since 1.0.19
197
	 * @param  string $context View or edit context.
198
	 * @return int
199
	 */
200
	public function get_author( $context = 'view' ) {
201
		return (int) $this->get_prop( 'author', $context );
202
    }
203
204
    /**
205
	 * Get the elements that make up the form.
206
	 *
207
	 * @since 1.0.19
208
	 * @param  string $context View or edit context.
209
	 * @return array
210
	 */
211
	public function get_elements( $context = 'view' ) {
212
		$elements = $this->get_prop( 'elements', $context );
213
214
		if ( empty( $elements ) || ! is_array( $elements ) ) {
215
            return wpinv_get_data( 'sample-payment-form' );
0 ignored issues
show
Bug Best Practice introduced by
The expression return wpinv_get_data('sample-payment-form') also could return the type true which is incompatible with the documented return type array.
Loading history...
216
        }
217
        return $elements;
218
	}
219
220
	/**
221
	 * Get the items sold via the form.
222
	 *
223
	 * @since 1.0.19
224
	 * @param  string $context View or edit context.
225
	 * @param  string $return objects or arrays.
226
	 * @return GetPaid_Form_Item[]|array
227
	 */
228
	public function get_items( $context = 'view', $return = 'objects' ) {
229
		$items = $this->get_prop( 'items', $context );
230
231
		if ( empty( $items ) || ! is_array( $items ) ) {
232
            $items = wpinv_get_data( 'sample-payment-form-items' );
233
		}
234
		
235
		if ( 'view' != $context ) {
236
			return $items;
237
		}
238
239
		// Convert the items.
240
		$prepared = array();
241
242
		foreach ( $items as $key => $value ) {
243
244
			// $item_id => $quantity
245
			if ( is_numeric( $key ) && is_numeric( $value ) ) {
246
				$item   = new GetPaid_Form_Item( $key );
247
248
				if ( $item->can_purchase() ) {
249
					$item->set_quantity( $value );
250
					$prepared[] = $item;
251
				}
252
253
				continue;
254
			}
255
256
			if ( is_array( $value ) && isset( $value['id'] ) ) {
257
258
				$item = new GetPaid_Form_Item( $value['id'] );
259
260
				if ( ! $item->can_purchase() ) {
261
					continue;
262
				}
263
264
				// Cart items.
265
				if ( isset( $value['subtotal'] ) ) {
266
					$item->set_price( $value['subtotal'] );
267
					$item->set_quantity( $value['quantity'] );
268
					$prepared[] = $item;
269
					continue;
270
				}
271
272
				// Payment form item.
273
				$item->set_quantity( $value['quantity'] );
274
				$item->set_allow_quantities( $value['allow_quantities'] );
275
				$item->set_is_required( $value['required'] );
276
				$item->set_custom_description( $value['description'] );
277
				$prepared[] = $item;
278
				continue;
279
280
			}
281
		}
282
283
284
		if ( 'objects' == $return ) {
285
			return $prepared;
286
		}
287
		
288
		$items = array();
289
		foreach ( $prepared as $item ) {
290
			$items[] = $item->prepare_data_for_use();
291
		}
292
293
		return $items;
294
	}
295
296
	/**
297
	 * Get the total amount earned via this form.
298
	 *
299
	 * @since 1.0.19
300
	 * @param  string $context View or edit context.
301
	 * @return array
302
	 */
303
	public function get_earned( $context = 'view' ) {
304
		return $this->get_prop( 'earned', $context );
305
	}
306
307
	/**
308
	 * Get the total amount refunded via this form.
309
	 *
310
	 * @since 1.0.19
311
	 * @param  string $context View or edit context.
312
	 * @return array
313
	 */
314
	public function get_refunded( $context = 'view' ) {
315
		return $this->get_prop( 'refunded', $context );
316
	}
317
318
	/**
319
	 * Get the total amount cancelled via this form.
320
	 *
321
	 * @since 1.0.19
322
	 * @param  string $context View or edit context.
323
	 * @return array
324
	 */
325
	public function get_cancelled( $context = 'view' ) {
326
		return $this->get_prop( 'cancelled', $context );
327
	}
328
329
	/**
330
	 * Get the total amount failed via this form.
331
	 *
332
	 * @since 1.0.19
333
	 * @param  string $context View or edit context.
334
	 * @return array
335
	 */
336
	public function get_failed( $context = 'view' ) {
337
		return $this->get_prop( 'failed', $context );
338
	}
339
340
    /*
341
	|--------------------------------------------------------------------------
342
	| Setters
343
	|--------------------------------------------------------------------------
344
	|
345
	| Functions for setting order data. These should not update anything in the
346
	| database itself and should only change what is stored in the class
347
	| object.
348
    */
349
350
    /**
351
	 * Set plugin version when the item was created.
352
	 *
353
	 * @since 1.0.19
354
	 */
355
	public function set_version( $value ) {
356
		$this->set_prop( 'version', $value );
357
    }
358
359
    /**
360
	 * Set date when the item was created.
361
	 *
362
	 * @since 1.0.19
363
	 * @param string $value Value to set.
364
     * @return bool Whether or not the date was set.
365
	 */
366
	public function set_date_created( $value ) {
367
        $date = strtotime( $value );
368
369
        if ( $date ) {
370
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
371
            return true;
372
        }
373
374
        return false;
375
376
    }
377
378
    /**
379
	 * Set date when the item was last modified.
380
	 *
381
	 * @since 1.0.19
382
	 * @param string $value Value to set.
383
     * @return bool Whether or not the date was set.
384
	 */
385
	public function set_date_modified( $value ) {
386
        $date = strtotime( $value );
387
388
        if ( $date ) {
389
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
390
            return true;
391
        }
392
393
        return false;
394
395
    }
396
397
    /**
398
	 * Set the item name.
399
	 *
400
	 * @since 1.0.19
401
	 * @param  string $value New name.
402
	 */
403
	public function set_name( $value ) {
404
		$this->set_prop( 'name', sanitize_text_field( $value ) );
405
    }
406
407
    /**
408
	 * Alias of self::set_name().
409
	 *
410
	 * @since 1.0.19
411
	 * @param  string $value New name.
412
	 */
413
	public function set_title( $value ) {
414
		$this->set_name( $value );
415
    }
416
417
    /**
418
	 * Set the owner of the item.
419
	 *
420
	 * @since 1.0.19
421
	 * @param  int $value New author.
422
	 */
423
	public function set_author( $value ) {
424
		$this->set_prop( 'author', (int) $value );
425
	}
426
427
	/**
428
	 * Set the form elements.
429
	 *
430
	 * @since 1.0.19
431
	 * @param  array $value Form elements.
432
	 */
433
	public function set_elements( $value ) {
434
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
435
			$this->set_prop( 'elements', $value );
436
		}
437
	}
438
439
	/**
440
	 * Set the form items.
441
	 *
442
	 * @since 1.0.19
443
	 * @param  array $value Form elements.
444
	 */
445
	public function set_items( $value ) {
446
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
447
			$this->set_prop( 'items', $value );
448
		}
449
	}
450
	
451
	/**
452
	 * Set the total amount earned via this form.
453
	 *
454
	 * @since 1.0.19
455
	 * @param  float $value Amount earned.
456
	 * @return array
457
	 */
458
	public function set_earned( $value ) {
459
		return $this->set_prop( 'earned', $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('earned', $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...
460
	}
461
462
	/**
463
	 * Set the total amount refunded via this form.
464
	 *
465
	 * @since 1.0.19
466
	 * @param  float $value Amount refunded.
467
	 * @return array
468
	 */
469
	public function set_refunded( $value ) {
470
		return $this->set_prop( 'refunded', $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('refunded', $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...
471
	}
472
473
	/**
474
	 * Set the total amount cancelled via this form.
475
	 *
476
	 * @since 1.0.19
477
	 * @param  float $value Amount cancelled.
478
	 * @return array
479
	 */
480
	public function set_cancelled( $value ) {
481
		return $this->set_prop( 'cancelled', $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('cancelled', $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...
482
	}
483
484
	/**
485
	 * Set the total amount failed via this form.
486
	 *
487
	 * @since 1.0.19
488
	 * @param  float $value Amount cancelled.
489
	 * @return array
490
	 */
491
	public function set_failed( $value ) {
492
		return $this->set_prop( 'failed', $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('failed', $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...
493
	}
494
495
    /**
496
     * Create an item. For backwards compatibilty.
497
     *
498
     * @deprecated
499
	 * @return int item id
500
     */
501
    public function create( $data = array() ) {
502
503
		// Set the properties.
504
		if ( is_array( $data ) ) {
505
			$this->set_props( $data );
506
		}
507
508
		// Save the item.
509
		return $this->save();
510
511
    }
512
513
    /**
514
     * Updates an item. For backwards compatibilty.
515
     *
516
     * @deprecated
517
	 * @return int item id
518
     */
519
    public function update( $data = array() ) {
520
        return $this->create( $data );
0 ignored issues
show
Deprecated Code introduced by
The function GetPaid_Payment_Form::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

520
        return /** @scrutinizer ignore-deprecated */ $this->create( $data );
Loading history...
521
    }
522
523
    /*
524
	|--------------------------------------------------------------------------
525
	| Conditionals
526
	|--------------------------------------------------------------------------
527
	|
528
	| Checks if a condition is true or false.
529
	|
530
	*/
531
532
    /**
533
	 * Checks whether this is the default payment form.
534
	 *
535
	 * @since 1.0.19
536
	 * @return bool
537
	 */
538
    public function is_default() {
539
        $is_default = $this->get_id() == wpinv_get_default_payment_form();
540
        return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->ID, $this );
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on GetPaid_Payment_Form. Did you maybe forget to declare it?
Loading history...
541
	}
542
543
    /**
544
	 * Checks whether the form is active.
545
	 *
546
	 * @since 1.0.19
547
	 * @return bool
548
	 */
549
    public function is_active() {
550
        $is_active = null !== $this->get_id();
551
552
        if ( ! current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) {
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on GetPaid_Payment_Form. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property post_status does not exist on GetPaid_Payment_Form. Did you maybe forget to declare it?
Loading history...
553
            $is_active = false;
554
        }
555
556
        return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this );
557
    }
558
559
}
560