Passed
Pull Request — master (#375)
by Brian
87:27
created

GetPaid_Payment_Form::get_author()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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 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
	 * @return GetPaid_Form_Item[]
226
	 */
227
	public function get_items( $context = 'view' ) {
228
		$items = $this->get_prop( 'items', $context );
229
230
		if ( empty( $items ) || ! is_array( $items ) ) {
231
            $items = wpinv_get_data( 'sample-payment-form-items' );
232
        }
233
234
		// Convert the items.
235
		$prepared = array();
236
237
		foreach ( $items as $key => $value ) {
238
239
			// $item_id => $quantity
240
			if ( is_numeric( $key ) && is_numeric( $value ) ) {
241
				$item   = new GetPaid_Form_Item( $key );
242
243
				if ( $item->can_purchase() ) {
244
					$item->set_quantity( $value );
245
					$prepared[] = $item;
246
				}
247
248
				continue;
249
			}
250
251
			if ( is_array( $value ) && isset( $value['id'] ) ) {
252
253
				$item = new GetPaid_Form_Item( $value['id'] );
254
255
				if ( ! $item->can_purchase() ) {
256
					continue;
257
				}
258
259
				// Cart items.
260
				if ( isset( $value['subtotal'] ) ) {
261
					$item->set_price( $value['subtotal'] );
262
					$item->set_quantity( $value['quantity'] );
263
					$prepared[] = $item;
264
					continue;
265
				}
266
267
				// Payment form item.
268
				$item->set_quantity( $value['quantity'] );
269
				$item->set_allow_quantities( $value['allow_quantities'] );
270
				$item->set_is_required( $value['required'] );
271
				$item->set_custom_description( $value['description'] );
272
				$prepared[] = $item;
273
				continue;
274
275
			}
276
		}
277
278
		return $prepared;
279
	}
280
281
	/**
282
	 * Get the total amount earned via this form.
283
	 *
284
	 * @since 1.0.19
285
	 * @param  string $context View or edit context.
286
	 * @return array
287
	 */
288
	public function get_earned( $context = 'view' ) {
289
		return $this->get_prop( 'earned', $context );
290
	}
291
292
	/**
293
	 * Get the total amount refunded via this form.
294
	 *
295
	 * @since 1.0.19
296
	 * @param  string $context View or edit context.
297
	 * @return array
298
	 */
299
	public function get_refunded( $context = 'view' ) {
300
		return $this->get_prop( 'refunded', $context );
301
	}
302
303
	/**
304
	 * Get the total amount cancelled via this form.
305
	 *
306
	 * @since 1.0.19
307
	 * @param  string $context View or edit context.
308
	 * @return array
309
	 */
310
	public function get_cancelled( $context = 'view' ) {
311
		return $this->get_prop( 'cancelled', $context );
312
	}
313
314
	/**
315
	 * Get the total amount failed via this form.
316
	 *
317
	 * @since 1.0.19
318
	 * @param  string $context View or edit context.
319
	 * @return array
320
	 */
321
	public function get_failed( $context = 'view' ) {
322
		return $this->get_prop( 'failed', $context );
323
	}
324
325
    /*
326
	|--------------------------------------------------------------------------
327
	| Setters
328
	|--------------------------------------------------------------------------
329
	|
330
	| Functions for setting order data. These should not update anything in the
331
	| database itself and should only change what is stored in the class
332
	| object.
333
    */
334
335
    /**
336
	 * Set plugin version when the item was created.
337
	 *
338
	 * @since 1.0.19
339
	 */
340
	public function set_version( $value ) {
341
		$this->set_prop( 'version', $value );
342
    }
343
344
    /**
345
	 * Set date when the item was created.
346
	 *
347
	 * @since 1.0.19
348
	 * @param string $value Value to set.
349
     * @return bool Whether or not the date was set.
350
	 */
351
	public function set_date_created( $value ) {
352
        $date = strtotime( $value );
353
354
        if ( $date ) {
355
            $this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) );
356
            return true;
357
        }
358
359
        return false;
360
361
    }
362
363
    /**
364
	 * Set date when the item was last modified.
365
	 *
366
	 * @since 1.0.19
367
	 * @param string $value Value to set.
368
     * @return bool Whether or not the date was set.
369
	 */
370
	public function set_date_modified( $value ) {
371
        $date = strtotime( $value );
372
373
        if ( $date ) {
374
            $this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) );
375
            return true;
376
        }
377
378
        return false;
379
380
    }
381
382
    /**
383
	 * Set the item name.
384
	 *
385
	 * @since 1.0.19
386
	 * @param  string $value New name.
387
	 */
388
	public function set_name( $value ) {
389
		$this->set_prop( 'name', sanitize_text_field( $value ) );
390
    }
391
392
    /**
393
	 * Alias of self::set_name().
394
	 *
395
	 * @since 1.0.19
396
	 * @param  string $value New name.
397
	 */
398
	public function set_title( $value ) {
399
		$this->set_name( $value );
400
    }
401
402
    /**
403
	 * Set the owner of the item.
404
	 *
405
	 * @since 1.0.19
406
	 * @param  int $value New author.
407
	 */
408
	public function set_author( $value ) {
409
		$this->set_prop( 'author', (int) $value );
410
	}
411
412
	/**
413
	 * Set the form elements.
414
	 *
415
	 * @since 1.0.19
416
	 * @param  array $value Form elements.
417
	 */
418
	public function set_elements( $value ) {
419
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
420
			$this->set_prop( 'elements', $value );
421
		}
422
	}
423
424
	/**
425
	 * Set the form items.
426
	 *
427
	 * @since 1.0.19
428
	 * @param  array $value Form elements.
429
	 */
430
	public function set_items( $value ) {
431
		if ( is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
432
			$this->set_prop( 'items', $value );
433
		}
434
	}
435
	
436
	/**
437
	 * Set the total amount earned via this form.
438
	 *
439
	 * @since 1.0.19
440
	 * @param  float $value Amount earned.
441
	 * @return array
442
	 */
443
	public function set_earned( $value ) {
444
		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...
445
	}
446
447
	/**
448
	 * Set the total amount refunded via this form.
449
	 *
450
	 * @since 1.0.19
451
	 * @param  float $value Amount refunded.
452
	 * @return array
453
	 */
454
	public function set_refunded( $value ) {
455
		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...
456
	}
457
458
	/**
459
	 * Set the total amount cancelled via this form.
460
	 *
461
	 * @since 1.0.19
462
	 * @param  float $value Amount cancelled.
463
	 * @return array
464
	 */
465
	public function set_cancelled( $value ) {
466
		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...
467
	}
468
469
	/**
470
	 * Set the total amount failed via this form.
471
	 *
472
	 * @since 1.0.19
473
	 * @param  float $value Amount cancelled.
474
	 * @return array
475
	 */
476
	public function set_failed( $value ) {
477
		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...
478
	}
479
480
    /**
481
     * Create an item. For backwards compatibilty.
482
     *
483
     * @deprecated
484
	 * @return int item id
485
     */
486
    public function create( $data = array() ) {
487
488
		// Set the properties.
489
		if ( is_array( $data ) ) {
490
			$this->set_props( $data );
491
		}
492
493
		// Save the item.
494
		return $this->save();
495
496
    }
497
498
    /**
499
     * Updates an item. For backwards compatibilty.
500
     *
501
     * @deprecated
502
	 * @return int item id
503
     */
504
    public function update( $data = array() ) {
505
        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

505
        return /** @scrutinizer ignore-deprecated */ $this->create( $data );
Loading history...
506
    }
507
508
    /*
509
	|--------------------------------------------------------------------------
510
	| Conditionals
511
	|--------------------------------------------------------------------------
512
	|
513
	| Checks if a condition is true or false.
514
	|
515
	*/
516
517
    /**
518
	 * Checks whether this is the default payment form.
519
	 *
520
	 * @since 1.0.19
521
	 * @return bool
522
	 */
523
    public function is_default() {
524
        $is_default = $this->get_id() == wpinv_get_default_payment_form();
525
        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...
526
	}
527
528
    /**
529
	 * Checks whether the form is active.
530
	 *
531
	 * @since 1.0.19
532
	 * @return bool
533
	 */
534
    public function is_active() {
535
        $is_active = null !== $this->get_id();
536
537
        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...
538
            $is_active = false;
539
        }
540
541
        return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this );
542
    }
543
544
}
545