Passed
Pull Request — master (#375)
by Brian
121:12
created

GetPaid_Payment_Form::set_earned()   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
 * 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 form if ID is passed, otherwise the form is new and empty.
65
	 *
66
	 * @param  int|object|GetPaid_Payment_Form|WP_Post $form Form to read.
67
	 */
68
	public function __construct( $form = 0 ) {
69
		parent::__construct( $form );
70
71
		if ( is_numeric( $form ) && $form > 0 ) {
72
			$this->set_id( $form );
73
		} elseif ( $form instanceof self ) {
74
			$this->set_id( $form->get_id() );
75
		} elseif ( ! empty( $form->ID ) ) {
76
			$this->set_id( $form->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[]
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
				// Sub-total (Cart items).
265
				if ( isset( $value['subtotal'] ) ) {
266
					$item->set_price( $value['subtotal'] );
267
				}
268
269
				if ( isset( $value['quantity'] ) ) {
270
					$item->set_quantity( $value['quantity'] );
271
				}
272
273
				if ( isset( $value['allow_quantities'] ) ) {
274
					$item->set_allow_quantities( $value['allow_quantities'] );
275
				}
276
277
				if ( isset( $value['required'] ) ) {
278
					$item->set_is_required( $value['required'] );
279
				}
280
281
				if ( isset( $value['description'] ) ) {
282
					$item->set_custom_description( $value['description'] );
283
				}
284
285
				$prepared[] = $item;
286
				continue;
287
288
			}
289
		}
290
291
		if ( 'objects' == $return ) {
292
			return $prepared;
293
		}
294
295
		$items = array();
296
		foreach ( $prepared as $item ) {
297
			$items[] = $item->prepare_data_for_use();
298
		}
299
300
		return $items;
301
	}
302
303
	/**
304
	 * Get a single item belonging to the form.
305
	 *
306
	 * @since 1.0.19
307
	 * @param  int $item_id The item id to return.
308
	 * @return GetPaid_Form_Item|bool
309
	 */
310
	public function get_item( $item_id ) {
311
312
		if ( empty( $item_id ) || ! is_numeric( $item_id ) ) {
313
			return false;
314
		}
315
316
		foreach( $this->get_items() as $item ) {
317
			if ( $item->get_id() == (int) $item_id ) {
318
				return $item;
319
			}
320
		}
321
322
		return false;
323
324
	}
325
326
	/**
327
	 * Get the total amount earned via this form.
328
	 *
329
	 * @since 1.0.19
330
	 * @param  string $context View or edit context.
331
	 * @return array
332
	 */
333
	public function get_earned( $context = 'view' ) {
334
		return $this->get_prop( 'earned', $context );
335
	}
336
337
	/**
338
	 * Get the total amount refunded via this form.
339
	 *
340
	 * @since 1.0.19
341
	 * @param  string $context View or edit context.
342
	 * @return array
343
	 */
344
	public function get_refunded( $context = 'view' ) {
345
		return $this->get_prop( 'refunded', $context );
346
	}
347
348
	/**
349
	 * Get the total amount cancelled via this form.
350
	 *
351
	 * @since 1.0.19
352
	 * @param  string $context View or edit context.
353
	 * @return array
354
	 */
355
	public function get_cancelled( $context = 'view' ) {
356
		return $this->get_prop( 'cancelled', $context );
357
	}
358
359
	/**
360
	 * Get the total amount failed via this form.
361
	 *
362
	 * @since 1.0.19
363
	 * @param  string $context View or edit context.
364
	 * @return array
365
	 */
366
	public function get_failed( $context = 'view' ) {
367
		return $this->get_prop( 'failed', $context );
368
	}
369
370
    /*
371
	|--------------------------------------------------------------------------
372
	| Setters
373
	|--------------------------------------------------------------------------
374
	|
375
	| Functions for setting order data. These should not update anything in the
376
	| database itself and should only change what is stored in the class
377
	| object.
378
    */
379
380
    /**
381
	 * Set plugin version when the item was created.
382
	 *
383
	 * @since 1.0.19
384
	 */
385
	public function set_version( $value ) {
386
		$this->set_prop( 'version', $value );
387
    }
388
389
    /**
390
	 * Set date when the item was created.
391
	 *
392
	 * @since 1.0.19
393
	 * @param string $value Value to set.
394
     * @return bool Whether or not the date was set.
395
	 */
396
	public function set_date_created( $value ) {
397
        $date = strtotime( $value );
398
399
        if ( $date ) {
400
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
401
            return true;
402
        }
403
404
        return false;
405
406
    }
407
408
    /**
409
	 * Set date when the item was last modified.
410
	 *
411
	 * @since 1.0.19
412
	 * @param string $value Value to set.
413
     * @return bool Whether or not the date was set.
414
	 */
415
	public function set_date_modified( $value ) {
416
        $date = strtotime( $value );
417
418
        if ( $date ) {
419
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
420
            return true;
421
        }
422
423
        return false;
424
425
    }
426
427
    /**
428
	 * Set the item name.
429
	 *
430
	 * @since 1.0.19
431
	 * @param  string $value New name.
432
	 */
433
	public function set_name( $value ) {
434
		$this->set_prop( 'name', sanitize_text_field( $value ) );
435
    }
436
437
    /**
438
	 * Alias of self::set_name().
439
	 *
440
	 * @since 1.0.19
441
	 * @param  string $value New name.
442
	 */
443
	public function set_title( $value ) {
444
		$this->set_name( $value );
445
    }
446
447
    /**
448
	 * Set the owner of the item.
449
	 *
450
	 * @since 1.0.19
451
	 * @param  int $value New author.
452
	 */
453
	public function set_author( $value ) {
454
		$this->set_prop( 'author', (int) $value );
455
	}
456
457
	/**
458
	 * Set the form elements.
459
	 *
460
	 * @since 1.0.19
461
	 * @param  array $value Form elements.
462
	 */
463
	public function set_elements( $value ) {
464
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
465
			$this->set_prop( 'elements', $value );
466
		}
467
	}
468
469
	/**
470
	 * Set the form items.
471
	 *
472
	 * @since 1.0.19
473
	 * @param  array $value Form elements.
474
	 */
475
	public function set_items( $value ) {
476
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
477
			$this->set_prop( 'items', $value );
478
		}
479
	}
480
481
	/**
482
	 * Set the total amount earned via this form.
483
	 *
484
	 * @since 1.0.19
485
	 * @param  float $value Amount earned.
486
	 * @return array
487
	 */
488
	public function set_earned( $value ) {
489
		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...
490
	}
491
492
	/**
493
	 * Set the total amount refunded via this form.
494
	 *
495
	 * @since 1.0.19
496
	 * @param  float $value Amount refunded.
497
	 * @return array
498
	 */
499
	public function set_refunded( $value ) {
500
		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...
501
	}
502
503
	/**
504
	 * Set the total amount cancelled via this form.
505
	 *
506
	 * @since 1.0.19
507
	 * @param  float $value Amount cancelled.
508
	 * @return array
509
	 */
510
	public function set_cancelled( $value ) {
511
		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...
512
	}
513
514
	/**
515
	 * Set the total amount failed via this form.
516
	 *
517
	 * @since 1.0.19
518
	 * @param  float $value Amount cancelled.
519
	 * @return array
520
	 */
521
	public function set_failed( $value ) {
522
		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...
523
	}
524
525
    /**
526
     * Create an item. For backwards compatibilty.
527
     *
528
     * @deprecated
529
	 * @return int item id
530
     */
531
    public function create( $data = array() ) {
532
533
		// Set the properties.
534
		if ( is_array( $data ) ) {
535
			$this->set_props( $data );
536
		}
537
538
		// Save the item.
539
		return $this->save();
540
541
    }
542
543
    /**
544
     * Updates an item. For backwards compatibilty.
545
     *
546
     * @deprecated
547
	 * @return int item id
548
     */
549
    public function update( $data = array() ) {
550
        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

550
        return /** @scrutinizer ignore-deprecated */ $this->create( $data );
Loading history...
551
    }
552
553
    /*
554
	|--------------------------------------------------------------------------
555
	| Conditionals
556
	|--------------------------------------------------------------------------
557
	|
558
	| Checks if a condition is true or false.
559
	|
560
	*/
561
562
    /**
563
	 * Checks whether this is the default payment form.
564
	 *
565
	 * @since 1.0.19
566
	 * @return bool
567
	 */
568
    public function is_default() {
569
        $is_default = $this->get_id() == wpinv_get_default_payment_form();
570
        return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this );
571
	}
572
573
    /**
574
	 * Checks whether the form is active.
575
	 *
576
	 * @since 1.0.19
577
	 * @return bool
578
	 */
579
    public function is_active() {
580
        $is_active = null !== $this->get_id();
581
582
        if ( $is_active && ! current_user_can( 'edit_post', $this->get_id() ) && $this->get_status() != 'publish' ) {
583
            $is_active = false;
584
        }
585
586
        return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this );
587
	}
588
589
	/**
590
	 * Checks whether the form has a given item.
591
	 *
592
	 * @since 1.0.19
593
	 * @return bool
594
	 */
595
    public function has_item( $item_id ) {
596
        return false !== $this->get_item( $item_id );
597
	}
598
599
	/**
600
	 * Displays the payment form.
601
	 *
602
	 * @param bool $echo whether to return or echo the value.
603
	 * @since 1.0.19
604
	 */
605
    public function display( $echo = true ) {
606
		global $invoicing;
607
608
		// Ensure that it is active.
609
		if ( ! $this->is_active() ) {
610
			$html = aui()->alert(
611
				array(
612
					'type'    => 'warning',
613
					'content' => __( 'This payment form is no longer active', 'invoicing' ),
614
				)
615
			);
616
617
			if ( $echo ) {
618
				echo $html;
619
				return;
620
			}
621
622
			return $html;
623
		}
624
    }
625
626
}
627