Passed
Push — master ( 60f62d...f597b6 )
by Brian
04:44
created

GetPaid_Payment_Form::__construct()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 22
rs 9.2222
cc 6
nc 8
nop 1
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 invoice if the form is for an invoice..
58
	 *
59
	 * @var WPInv_Invoice
60
	 */
61
	public $invoice = 0;
62
63
    /**
64
     * Stores a reference to the original WP_Post object
65
     *
66
     * @var WP_Post
67
     */
68
    protected $post = null;
69
70
    /**
71
	 * Get the form if ID is passed, otherwise the form is new and empty.
72
	 *
73
	 * @param  int|object|GetPaid_Payment_Form|WP_Post $form Form to read.
74
	 */
75
	public function __construct( $form = 0 ) {
76
		parent::__construct( $form );
77
78
		if ( is_numeric( $form ) && $form > 0 ) {
79
			$this->set_id( $form );
80
		} elseif ( $form instanceof self ) {
81
82
			$this->set_id( $form->get_id() );
83
			$this->invoice = $form->invoice;
84
85
		} elseif ( ! empty( $form->ID ) ) {
86
			$this->set_id( $form->ID );
87
		} else {
88
			$this->set_object_read( true );
89
		}
90
91
        // Load the datastore.
92
		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
93
94
		if ( $this->get_id() > 0 ) {
95
            $this->post = get_post( $this->get_id() );
96
			$this->data_store->read( $this );
97
        }
98
99
	}
100
101
    /*
102
	|--------------------------------------------------------------------------
103
	| CRUD methods
104
	|--------------------------------------------------------------------------
105
	|
106
	| Methods which create, read, update and delete items from the database.
107
	|
108
    */
109
110
    /*
111
	|--------------------------------------------------------------------------
112
	| Getters
113
	|--------------------------------------------------------------------------
114
    */
115
116
    /**
117
	 * Get plugin version when the form was created.
118
	 *
119
	 * @since 1.0.19
120
	 * @param  string $context View or edit context.
121
	 * @return string
122
	 */
123
	public function get_version( $context = 'view' ) {
124
		return $this->get_prop( 'version', $context );
125
    }
126
127
    /**
128
	 * Get date when the form was created.
129
	 *
130
	 * @since 1.0.19
131
	 * @param  string $context View or edit context.
132
	 * @return string
133
	 */
134
	public function get_date_created( $context = 'view' ) {
135
		return $this->get_prop( 'date_created', $context );
136
    }
137
138
    /**
139
	 * Get GMT date when the form was created.
140
	 *
141
	 * @since 1.0.19
142
	 * @param  string $context View or edit context.
143
	 * @return string
144
	 */
145
	public function get_date_created_gmt( $context = 'view' ) {
146
        $date = $this->get_date_created( $context );
147
148
        if ( $date ) {
149
            $date = get_gmt_from_date( $date );
150
        }
151
		return $date;
152
    }
153
154
    /**
155
	 * Get date when the form was last modified.
156
	 *
157
	 * @since 1.0.19
158
	 * @param  string $context View or edit context.
159
	 * @return string
160
	 */
161
	public function get_date_modified( $context = 'view' ) {
162
		return $this->get_prop( 'date_modified', $context );
163
    }
164
165
    /**
166
	 * Get GMT date when the form was last modified.
167
	 *
168
	 * @since 1.0.19
169
	 * @param  string $context View or edit context.
170
	 * @return string
171
	 */
172
	public function get_date_modified_gmt( $context = 'view' ) {
173
        $date = $this->get_date_modified( $context );
174
175
        if ( $date ) {
176
            $date = get_gmt_from_date( $date );
177
        }
178
		return $date;
179
    }
180
181
    /**
182
	 * Get the form name.
183
	 *
184
	 * @since 1.0.19
185
	 * @param  string $context View or edit context.
186
	 * @return string
187
	 */
188
	public function get_name( $context = 'view' ) {
189
		return $this->get_prop( 'name', $context );
190
    }
191
192
    /**
193
	 * Alias of self::get_name().
194
	 *
195
	 * @since 1.0.19
196
	 * @param  string $context View or edit context.
197
	 * @return string
198
	 */
199
	public function get_title( $context = 'view' ) {
200
		return $this->get_name( $context );
201
	}
202
203
    /**
204
	 * Get the owner of the form.
205
	 *
206
	 * @since 1.0.19
207
	 * @param  string $context View or edit context.
208
	 * @return int
209
	 */
210
	public function get_author( $context = 'view' ) {
211
		return (int) $this->get_prop( 'author', $context );
212
    }
213
214
    /**
215
	 * Get the elements that make up the form.
216
	 *
217
	 * @since 1.0.19
218
	 * @param  string $context View or edit context.
219
	 * @return array
220
	 */
221
	public function get_elements( $context = 'view' ) {
222
		$elements = $this->get_prop( 'elements', $context );
223
224
		if ( empty( $elements ) || ! is_array( $elements ) ) {
225
            return wpinv_get_data( 'sample-payment-form' );
226
		}
227
228
		// Ensure that all required elements exist.
229
		$_elements = array();
230
		foreach ( $elements as $element ) {
231
232
			if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) {
233
234
				$_elements[] = array(
235
					'text'        => __( 'Select Payment Method', 'invoicing' ),
236
					'id'          => 'gtscicd',
237
					'name'        => 'gtscicd',
238
					'type'        => 'gateway_select',
239
					'premade'     => true
240
			
241
				);
242
243
			}
244
245
			$_elements[] = $element;
246
247
		}
248
249
        return $_elements;
250
	}
251
252
	/**
253
	 * Get the items sold via the form.
254
	 *
255
	 * @since 1.0.19
256
	 * @param  string $context View or edit context.
257
	 * @param  string $return objects or arrays.
258
	 * @return GetPaid_Form_Item[]
259
	 */
260
	public function get_items( $context = 'view', $return = 'objects' ) {
261
		$items = $this->get_prop( 'items', $context );
262
263
		if ( empty( $items ) || ! is_array( $items ) ) {
264
            $items = wpinv_get_data( 'sample-payment-form-items' );
265
		}
266
267
		// Convert the items.
268
		$prepared = array();
269
270
		foreach ( $items as $key => $value ) {
271
272
			if ( $value instanceof GetPaid_Form_Item ) {
273
274
				if ( $value->can_purchase() ) {
275
					$prepared[] = $value;
276
				}
277
278
				continue;
279
280
			}
281
282
			// $item_id => $quantity
283
			if ( is_numeric( $key ) && is_numeric( $value ) ) {
284
				$item   = new GetPaid_Form_Item( $key );
285
286
				if ( $item->can_purchase() ) {
287
					$item->set_quantity( $value );
288
					$prepared[] = $item;
289
				}
290
291
				continue;
292
			}
293
294
			if ( is_array( $value ) && isset( $value['id'] ) ) {
295
296
				$item = new GetPaid_Form_Item( $value['id'] );
297
298
				if ( ! $item->can_purchase() ) {
299
					continue;
300
				}
301
302
				// Sub-total (Cart items).
303
				if ( isset( $value['subtotal'] ) ) {
304
					$item->set_price( $value['subtotal'] );
305
				}
306
307
				if ( isset( $value['quantity'] ) ) {
308
					$item->set_quantity( $value['quantity'] );
309
				}
310
311
				if ( isset( $value['allow_quantities'] ) ) {
312
					$item->set_allow_quantities( $value['allow_quantities'] );
313
				}
314
315
				if ( isset( $value['required'] ) ) {
316
					$item->set_is_required( $value['required'] );
317
				}
318
319
				if ( isset( $value['description'] ) ) {
320
					$item->set_custom_description( $value['description'] );
321
				}
322
323
				$prepared[] = $item;
324
				continue;
325
326
			}
327
		}
328
329
		if ( 'objects' == $return && 'view' == $context ) {
330
			return $prepared;
331
		}
332
333
		$items = array();
334
		foreach ( $prepared as $item ) {
335
			$items[] = $item->prepare_data_for_use();
336
		}
337
338
		return $items;
339
	}
340
341
	/**
342
	 * Get a single item belonging to the form.
343
	 *
344
	 * @since 1.0.19
345
	 * @param  int $item_id The item id to return.
346
	 * @return GetPaid_Form_Item|bool
347
	 */
348
	public function get_item( $item_id ) {
349
350
		if ( empty( $item_id ) || ! is_numeric( $item_id ) ) {
351
			return false;
352
		}
353
354
		foreach( $this->get_items() as $item ) {
355
			if ( $item->get_id() == (int) $item_id ) {
356
				return $item;
357
			}
358
		}
359
360
		return false;
361
362
	}
363
364
	/**
365
	 * Gets a single element.
366
	 *
367
	 * @since 1.0.19
368
	 * @param  string $element_type The element type to return.
369
	 * @return array|bool
370
	 */
371
	public function get_element_type( $element_type ) {
372
373
		if ( empty( $element_type ) || ! is_scalar( $element_type ) ) {
374
			return false;
375
		}
376
377
		foreach ( $this->get_prop( 'elements' ) as $element ) {
378
379
			if ( $element['type'] == $element_type ) {
380
				return $element;
381
			}
382
383
		}
384
385
		return false;
386
387
	}
388
389
	/**
390
	 * Get the total amount earned via this form.
391
	 *
392
	 * @since 1.0.19
393
	 * @param  string $context View or edit context.
394
	 * @return array
395
	 */
396
	public function get_earned( $context = 'view' ) {
397
		return $this->get_prop( 'earned', $context );
398
	}
399
400
	/**
401
	 * Get the total amount refunded via this form.
402
	 *
403
	 * @since 1.0.19
404
	 * @param  string $context View or edit context.
405
	 * @return array
406
	 */
407
	public function get_refunded( $context = 'view' ) {
408
		return $this->get_prop( 'refunded', $context );
409
	}
410
411
	/**
412
	 * Get the total amount cancelled via this form.
413
	 *
414
	 * @since 1.0.19
415
	 * @param  string $context View or edit context.
416
	 * @return array
417
	 */
418
	public function get_cancelled( $context = 'view' ) {
419
		return $this->get_prop( 'cancelled', $context );
420
	}
421
422
	/**
423
	 * Get the total amount failed via this form.
424
	 *
425
	 * @since 1.0.19
426
	 * @param  string $context View or edit context.
427
	 * @return array
428
	 */
429
	public function get_failed( $context = 'view' ) {
430
		return $this->get_prop( 'failed', $context );
431
	}
432
433
    /*
434
	|--------------------------------------------------------------------------
435
	| Setters
436
	|--------------------------------------------------------------------------
437
	|
438
	| Functions for setting order data. These should not update anything in the
439
	| database itself and should only change what is stored in the class
440
	| object.
441
    */
442
443
    /**
444
	 * Set plugin version when the item was created.
445
	 *
446
	 * @since 1.0.19
447
	 */
448
	public function set_version( $value ) {
449
		$this->set_prop( 'version', $value );
450
    }
451
452
    /**
453
	 * Set date when the item was created.
454
	 *
455
	 * @since 1.0.19
456
	 * @param string $value Value to set.
457
     * @return bool Whether or not the date was set.
458
	 */
459
	public function set_date_created( $value ) {
460
        $date = strtotime( $value );
461
462
        if ( $date ) {
463
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
464
            return true;
465
        }
466
467
        return false;
468
469
    }
470
471
    /**
472
	 * Set date when the item was last modified.
473
	 *
474
	 * @since 1.0.19
475
	 * @param string $value Value to set.
476
     * @return bool Whether or not the date was set.
477
	 */
478
	public function set_date_modified( $value ) {
479
        $date = strtotime( $value );
480
481
        if ( $date ) {
482
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
483
            return true;
484
        }
485
486
        return false;
487
488
    }
489
490
    /**
491
	 * Set the item name.
492
	 *
493
	 * @since 1.0.19
494
	 * @param  string $value New name.
495
	 */
496
	public function set_name( $value ) {
497
		$this->set_prop( 'name', sanitize_text_field( $value ) );
498
    }
499
500
    /**
501
	 * Alias of self::set_name().
502
	 *
503
	 * @since 1.0.19
504
	 * @param  string $value New name.
505
	 */
506
	public function set_title( $value ) {
507
		$this->set_name( $value );
508
    }
509
510
    /**
511
	 * Set the owner of the item.
512
	 *
513
	 * @since 1.0.19
514
	 * @param  int $value New author.
515
	 */
516
	public function set_author( $value ) {
517
		$this->set_prop( 'author', (int) $value );
518
	}
519
520
	/**
521
	 * Set the form elements.
522
	 *
523
	 * @since 1.0.19
524
	 * @param  array $value Form elements.
525
	 */
526
	public function set_elements( $value ) {
527
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
528
			$this->set_prop( 'elements', $value );
529
		}
530
	}
531
532
	/**
533
	 * Set the form items.
534
	 *
535
	 * @since 1.0.19
536
	 * @param  array $value Form elements.
537
	 */
538
	public function set_items( $value ) {
539
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
540
			$this->set_prop( 'items', $value );
541
		}
542
	}
543
544
	/**
545
	 * Set the total amount earned via this form.
546
	 *
547
	 * @since 1.0.19
548
	 * @param  float $value Amount earned.
549
	 * @return array
550
	 */
551
	public function set_earned( $value ) {
552
		return $this->set_prop( 'earned', (float) $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('earned', (double)$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...
553
	}
554
555
	/**
556
	 * Set the total amount refunded via this form.
557
	 *
558
	 * @since 1.0.19
559
	 * @param  float $value Amount refunded.
560
	 * @return array
561
	 */
562
	public function set_refunded( $value ) {
563
		return $this->set_prop( 'refunded', (float) $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('refunded', (double)$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...
564
	}
565
566
	/**
567
	 * Set the total amount cancelled via this form.
568
	 *
569
	 * @since 1.0.19
570
	 * @param  float $value Amount cancelled.
571
	 * @return array
572
	 */
573
	public function set_cancelled( $value ) {
574
		return $this->set_prop( 'cancelled', (float) $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('cancelled', (double)$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...
575
	}
576
577
	/**
578
	 * Set the total amount failed via this form.
579
	 *
580
	 * @since 1.0.19
581
	 * @param  float $value Amount cancelled.
582
	 * @return array
583
	 */
584
	public function set_failed( $value ) {
585
		return $this->set_prop( 'failed', (float) $value );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->set_prop('failed', (double)$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...
586
	}
587
588
    /**
589
     * Create an item. For backwards compatibilty.
590
     *
591
     * @deprecated
592
	 * @return int item id
593
     */
594
    public function create( $data = array() ) {
595
596
		// Set the properties.
597
		if ( is_array( $data ) ) {
598
			$this->set_props( $data );
599
		}
600
601
		// Save the item.
602
		return $this->save();
603
604
    }
605
606
    /**
607
     * Updates an item. For backwards compatibilty.
608
     *
609
     * @deprecated
610
	 * @return int item id
611
     */
612
    public function update( $data = array() ) {
613
        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

613
        return /** @scrutinizer ignore-deprecated */ $this->create( $data );
Loading history...
614
    }
615
616
    /*
617
	|--------------------------------------------------------------------------
618
	| Conditionals
619
	|--------------------------------------------------------------------------
620
	|
621
	| Checks if a condition is true or false.
622
	|
623
	*/
624
625
    /**
626
	 * Checks whether this is the default payment form.
627
	 *
628
	 * @since 1.0.19
629
	 * @return bool
630
	 */
631
    public function is_default() {
632
        $is_default = $this->get_id() == wpinv_get_default_payment_form();
633
        return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this );
634
	}
635
636
    /**
637
	 * Checks whether the form is active.
638
	 *
639
	 * @since 1.0.19
640
	 * @return bool
641
	 */
642
    public function is_active() {
643
        $is_active = 0 !== (int) $this->get_id();
644
645
        if ( $is_active && ! current_user_can( 'edit_post', $this->get_id() ) && $this->get_status() != 'publish' ) {
646
            $is_active = false;
647
        }
648
649
        return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this );
650
	}
651
652
	/**
653
	 * Checks whether the form has a given item.
654
	 *
655
	 * @since 1.0.19
656
	 * @return bool
657
	 */
658
    public function has_item( $item_id ) {
659
        return false !== $this->get_item( $item_id );
660
	}
661
662
	/**
663
	 * Checks whether the form has a given element.
664
	 *
665
	 * @since 1.0.19
666
	 * @return bool
667
	 */
668
    public function has_element_type( $element_type ) {
669
        return false !== $this->get_element_type( $element_type );
670
	}
671
672
	/**
673
	 * Checks whether this form is recurring or not.
674
	 *
675
	 * @since 1.0.19
676
	 * @return bool
677
	 */
678
    public function is_recurring() {
679
680
		if ( ! empty( $this->invoice ) ) {
681
			return $this->invoice->is_recurring();
682
		}
683
684
		foreach ( $this->get_items() as $item ) {
685
686
			if ( $item->is_recurring() ) {
687
				return true;
688
			}
689
690
		}
691
692
        return false;
693
	}
694
695
	/**
696
	 * Retrieves the form's html.
697
	 *
698
	 * @since 1.0.19
699
	 */
700
    public function get_html() {
701
702
		// Return the HTML.
703
		return wpinv_get_template_html( 'payment-forms/form.php', array( 'form' => $this ) );
704
705
	}
706
707
	/**
708
	 * Displays the payment form.
709
	 *
710
	 * @since 1.0.19
711
	 */
712
    public function display() {
713
		echo $this->get_html();
714
    }
715
716
}
717