Test Failed
Push — issue/1920 ( c3a9e0 )
by Ravinder
441:16 queued 433:44
created

Give_Donate_Form::get_ID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Donate Form
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Donate_Form
7
 * @copyright   Copyright (c) 2015, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Give_Donate_Form Class.
19
 *
20
 * This class handles donation forms.
21
 *
22
 * @since 1.0
23
 *
24
 * @property $price
25
 * @property $minimum_price
26
 * @property $maximum_price
27
 * @property $prices
28
 * @property $goal
29
 * @property $sales
30
 * @property $earnings
31
 * @property $post_type
32
 */
33
class Give_Donate_Form {
34
35
	/**
36
	 * The donation ID.
37
	 *
38
	 * @since  1.0
39
	 * @access public
40
	 *
41
	 * @var    int
42
	 */
43
	public $ID = 0;
44
45
	/**
46
	 * The donation price.
47
	 *
48
	 * @since  1.0
49
	 * @access private
50
	 *
51
	 * @var    float
52
	 */
53
	private $price;
54
55
	/**
56
	 * The minimum donation price.
57
	 *
58
	 * @since  1.3.6
59
	 * @access private
60
	 *
61
	 * @var    float
62
	 */
63
	private $minimum_price;
64
65
	/**
66
	 * The maximum donation price.
67
	 *
68
	 * @since  2.0
69
	 * @access private
70
	 *
71
	 * @var    float
72
	 */
73
	private $maximum_price;
74
75
	/**
76
	 * The donation prices, if Price Levels are enabled.
77
	 *
78
	 * @since  1.0
79
	 * @access private
80
	 *
81
	 * @var    array
82
	 */
83
	private $prices;
84
85
	/**
86
	 * The donation goal.
87
	 *
88
	 * @since  1.0
89
	 * @access private
90
	 *
91
	 * @var    float
92
	 */
93
	private $goal;
94
95
	/**
96
	 * The form's sale count.
97
	 *
98
	 * @since  1.0
99
	 * @access private
100
	 *
101
	 * @var    int
102
	 */
103
	private $sales;
104
105
	/**
106
	 * The form's total earnings
107
	 *
108
	 * @since  1.0
109
	 * @access private
110
	 *
111
	 * @var    float
112
	 */
113
	private $earnings;
114
115
	/**
116
	 * Declare the default properties in WP_Post as we can't extend it
117
	 * Anything we've declared above has been removed.
118
	 */
119
120
	/**
121
	 * The post author
122
	 *
123
	 * @since  1.0
124
	 * @access public
125
	 *
126
	 * @var    int
127
	 */
128
	public $post_author = 0;
129
130
	/**
131
	 * The post date
132
	 *
133
	 * @since  1.0
134
	 * @access public
135
	 *
136
	 * @var    string
137
	 */
138
	public $post_date = '0000-00-00 00:00:00';
139
140
	/**
141
	 * The post GTM date
142
	 *
143
	 * @since  1.0
144
	 * @access public
145
	 *
146
	 * @var    string
147
	 */
148
	public $post_date_gmt = '0000-00-00 00:00:00';
149
150
	/**
151
	 * The post content
152
	 *
153
	 * @since  1.0
154
	 * @access public
155
	 *
156
	 * @var    string
157
	 */
158
	public $post_content = '';
159
160
	/**
161
	 * The post title
162
	 *
163
	 * @since  1.0
164
	 * @access public
165
	 *
166
	 * @var    string
167
	 */
168
	public $post_title = '';
169
170
	/**
171
	 * The post excerpt
172
	 *
173
	 * @since  1.0
174
	 * @access public
175
	 *
176
	 * @var    string
177
	 */
178
	public $post_excerpt = '';
179
180
	/**
181
	 * The post status
182
	 *
183
	 * @since  1.0
184
	 * @access public
185
	 *
186
	 * @var    string
187
	 */
188
	public $post_status = 'publish';
189
190
	/**
191
	 * The comment status
192
	 *
193
	 * @since  1.0
194
	 * @access public
195
	 *
196
	 * @var    string
197
	 */
198
	public $comment_status = 'open';
199
200
	/**
201
	 * The ping status
202
	 *
203
	 * @since  1.0
204
	 * @access public
205
	 *
206
	 * @var    string
207
	 */
208
	public $ping_status = 'open';
209
210
	/**
211
	 * The post password
212
	 *
213
	 * @since  1.0
214
	 * @access public
215
	 *
216
	 * @var    string
217
	 */
218
	public $post_password = '';
219
220
	/**
221
	 * The post name
222
	 *
223
	 * @since  1.0
224
	 * @access public
225
	 *
226
	 * @var    string
227
	 */
228
	public $post_name = '';
229
230
	/**
231
	 * Ping
232
	 *
233
	 * @since  1.0
234
	 * @access public
235
	 *
236
	 * @var    string
237
	 */
238
	public $to_ping = '';
239
240
	/**
241
	 * Pinged
242
	 *
243
	 * @since  1.0
244
	 * @access public
245
	 *
246
	 * @var    string
247
	 */
248
	public $pinged = '';
249
250
	/**
251
	 * The post modified date
252
	 *
253
	 * @since  1.0
254
	 * @access public
255
	 *
256
	 * @var    string
257
	 */
258
	public $post_modified = '0000-00-00 00:00:00';
259
260
	/**
261
	 * The post modified GTM date
262
	 *
263
	 * @since  1.0
264
	 * @access public
265
	 *
266
	 * @var    string
267
	 */
268
	public $post_modified_gmt = '0000-00-00 00:00:00';
269
270
	/**
271
	 * The post filtered content
272
	 *
273
	 * @since  1.0
274
	 * @access public
275
	 *
276
	 * @var    string
277
	 */
278
	public $post_content_filtered = '';
279
280
	/**
281
	 * The post parent
282
	 *
283
	 * @since  1.0
284
	 * @access public
285
	 *
286
	 * @var    int
287
	 */
288
	public $post_parent = 0;
289
290
	/**
291
	 * The post GUID
292
	 *
293
	 * @since  1.0
294
	 * @access public
295
	 *
296
	 * @var    string
297
	 */
298
	public $guid = '';
299
300
	/**
301
	 * The menu order
302
	 *
303
	 * @since  1.0
304
	 * @access public
305
	 *
306
	 * @var    int
307
	 */
308
	public $menu_order = 0;
309
310
	/**
311
	 * The mime type0
312
	 *
313
	 * @since  1.0
314
	 * @access public
315
	 *
316
	 * @var    string
317
	 */
318
	public $post_mime_type = '';
319
320
	/**
321
	 * The comment count
322
	 *
323
	 * @since  1.0
324
	 * @access public
325
	 *
326
	 * @var    int
327
	 */
328
	public $comment_count = 0;
329
330
	/**
331
	 * Filtered
332
	 *
333
	 * @since  1.0
334
	 * @access public
335
	 *
336
	 * @var    string
337
	 */
338
	public $filter;
339
340
	/**
341
	 * Class Constructor
342
	 *
343
	 * Set up the Give Donate Form Class.
344
	 *
345
	 * @since  1.0
346
	 * @access public
347
	 *
348
	 * @param  bool  $_id   Post id. Default is false.
349
	 * @param  array $_args Arguments passed.
350
	 */
351
	public function __construct( $_id = false, $_args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $_args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
352
353
		$donation_form = WP_Post::get_instance( $_id );
354
355
		return $this->setup_donation_form( $donation_form );
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
356
	}
357
358
	/**
359
	 * Given the donation form data, let's set the variables
360
	 *
361
	 * @since  1.5
362
	 * @access private
363
	 *
364
	 * @param  WP_Post $donation_form WP_Post Object for the donation form.
365
	 *
366
	 * @return bool                   If the setup was successful or not.
367
	 */
368
	private function setup_donation_form( $donation_form ) {
369
370
		if ( ! is_object( $donation_form ) ) {
371
			return false;
372
		}
373
374
		if ( ! is_a( $donation_form, 'WP_Post' ) ) {
375
			return false;
376
		}
377
378
		if ( 'give_forms' !== $donation_form->post_type ) {
379
			return false;
380
		}
381
382
		foreach ( $donation_form as $key => $value ) {
383
384
			switch ( $key ) {
385
386
				default:
0 ignored issues
show
Unused Code introduced by
default: $this->{$key} = $value; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
387
					$this->$key = $value;
388
					break;
389
390
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
391
392
		}
393
394
		return true;
395
396
	}
397
398
	/**
399
	 * Magic __get function to dispatch a call to retrieve a private property
400
	 *
401
	 * @since  1.0
402
	 * @access public
403
	 *
404
	 * @param  string $key
405
	 *
406
	 * @return mixed
407
	 */
408 View Code Duplication
	public function __get( $key ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
409
410
		if ( method_exists( $this, 'get_' . $key ) ) {
411
412
			return call_user_func( array( $this, 'get_' . $key ) );
413
414
		} else {
415
416
			/* translators: %s: property key */
417
			return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) );
418
419
		}
420
421
	}
422
423
	/**
424
	 * Creates a donation form
425
	 *
426
	 * @since  1.5
427
	 * @access public
428
	 *
429
	 * @param  array $data Array of attributes for a donation form.
430
	 *
431
	 * @return bool|int    False if data isn't passed and class not instantiated for creation, or New Form ID.
432
	 */
433
	public function create( $data = array() ) {
434
435
		if ( $this->id != 0 ) {
0 ignored issues
show
introduced by
Found "!= 0". Use Yoda Condition checks, you must
Loading history...
436
			return false;
437
		}
438
439
		$defaults = array(
440
			'post_type'   => 'give_forms',
441
			'post_status' => 'draft',
442
			'post_title'  => __( 'New Donation Form', 'give' ),
443
		);
444
445
		$args = wp_parse_args( $data, $defaults );
446
447
		/**
448
		 * Fired before a donation form is created
449
		 *
450
		 * @param array $args The post object arguments used for creation.
451
		 */
452
		do_action( 'give_form_pre_create', $args );
453
454
		$id = wp_insert_post( $args, true );
455
456
		$donation_form = WP_Post::get_instance( $id );
457
458
		/**
459
		 * Fired after a donation form is created
460
		 *
461
		 * @param int   $id   The post ID of the created item.
462
		 * @param array $args The post object arguments used for creation.
463
		 */
464
		do_action( 'give_form_post_create', $id, $args );
465
466
		return $this->setup_donation_form( $donation_form );
467
468
	}
469
470
	/**
471
	 * Retrieve the ID
472
	 *
473
	 * @since  1.0
474
	 * @access public
475
	 *
476
	 * @return int    Donation form ID.
477
	 */
478
	public function get_ID() {
0 ignored issues
show
Coding Style introduced by
The function name get_ID is in camel caps, but expected get_i_d instead as per the coding standard.
Loading history...
479
		return $this->ID;
480
	}
481
482
	/**
483
	 * Retrieve the donation form name
484
	 *
485
	 * @since  1.5
486
	 * @access public
487
	 *
488
	 * @return string Donation form name.
489
	 */
490
	public function get_name() {
491
		return get_the_title( $this->ID );
492
	}
493
494
	/**
495
	 * Retrieve the price
496
	 *
497
	 * @since  1.0
498
	 * @access public
499
	 *
500
	 * @return float  Price.
501
	 */
502 View Code Duplication
	public function get_price() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
503
504
		if ( ! isset( $this->price ) ) {
505
506
			$this->price = give_maybe_sanitize_amount(
0 ignored issues
show
Documentation Bug introduced by
It seems like give_maybe_sanitize_amou...give_set_price', true)) can also be of type integer or string. However, the property $price is declared as type double. 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...
507
				give_get_meta(
508
					$this->ID,
509
					'_give_set_price',
510
					true
511
				)
512
			);
513
514
			if ( ! $this->price ) {
515
				$this->price = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $price was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
516
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
517
518
		}
519
520
		/**
521
		 * Override the donation form set price.
522
		 *
523
		 * @since 1.0
524
		 *
525
		 * @param string     $price The donation form price.
526
		 * @param string|int $id    The form ID.
527
		 */
528
		return apply_filters( 'give_get_set_price', $this->price, $this->ID );
529
	}
530
531
	/**
532
	 * Retrieve the minimum price.
533
	 *
534
	 * @since  1.3.6
535
	 * @access public
536
	 *
537
	 * @return float  Minimum price.
538
	 */
539 View Code Duplication
	public function get_minimum_price() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
540
541
		if ( ! isset( $this->minimum_price ) ) {
542
543
			$this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_range_minimum', true );
544
545
			// Give backward < 2.1
546
			if ( empty( $this->minimum_price ) ) {
547
				$this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_minimum', true );
548
			}
549
550
			if ( ! $this->is_custom_price_mode() ) {
551
				$this->minimum_price = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $minimum_price was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
552
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
553
554
		}
555
556
		return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID );
557
	}
558
559
	/**
560
	 * Retrieve the maximum price.
561
	 *
562
	 * @since  2.1
563
	 * @access public
564
	 *
565
	 * @return float  Maximum price.
566
	 */
567
	public function get_maximum_price() {
568
569
		if ( ! isset( $this->maximum_price ) ) {
570
			$this->maximum_price = give_get_meta( $this->ID, '_give_custom_amount_range_maximum', true );
571
572
			if ( ! $this->is_custom_price_mode() ) {
573
				$this->maximum_price = 999999.99;
574
			}
575
		}
576
577
		return apply_filters( 'give_get_set_maximum_price', $this->maximum_price, $this->ID );
578
	}
579
580
	/**
581
	 * Retrieve the variable prices
582
	 *
583
	 * @since  1.0
584
	 * @access public
585
	 *
586
	 * @return array  Variable prices.
587
	 */
588
	public function get_prices() {
589
590
		if ( ! isset( $this->prices ) ) {
591
592
			$this->prices = give_get_meta( $this->ID, '_give_donation_levels', true );
0 ignored issues
show
Documentation Bug introduced by
It seems like give_get_meta($this->ID,...donation_levels', true) of type * is incompatible with the declared type array of property $prices.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
593
594
		}
595
596
		/**
597
		 * Override multi-level prices
598
		 *
599
		 * @since 1.0
600
		 *
601
		 * @param array      $prices The array of mulit-level prices.
602
		 * @param int|string $ID     The ID of the form.
603
		 */
604
		return apply_filters( 'give_get_donation_levels', $this->prices, $this->ID );
605
606
	}
607
608
	/**
609
	 * Retrieve the goal
610
	 *
611
	 * @since  1.0
612
	 * @access public
613
	 *
614
	 * @return float  Goal.
615
	 */
616 View Code Duplication
	public function get_goal() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
617
618
		if ( ! isset( $this->goal ) ) {
619
620
			if ( 'donation' === give_get_form_goal_format( $this->ID ) ) {
621
				$this->goal = give_get_meta( $this->ID, '_give_number_of_donation_goal', true );
622
			} else {
623
				$this->goal = give_get_meta( $this->ID, '_give_set_goal', true );
624
			}
625
626
			if ( ! $this->goal ) {
627
				$this->goal = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $goal was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
628
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
629
630
		}
631
632
		return apply_filters( 'give_get_set_goal', $this->goal, $this->ID );
633
634
	}
635
636
	/**
637
	 * Determine if single price mode is enabled or disabled
638
	 *
639
	 * @since  1.0
640
	 * @access public
641
	 *
642
	 * @return bool
643
	 */
644 View Code Duplication
	public function is_single_price_mode() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
645
646
		$option = give_get_meta( $this->ID, '_give_price_option', true );
647
		$ret    = 0;
648
649
		if ( empty( $option ) || $option === 'set' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
650
			$ret = 1;
651
		}
652
653
		/**
654
		 * Override the price mode for a donation when checking if is in single price mode.
655
		 *
656
		 * @since 1.0
657
		 *
658
		 * @param bool       $ret Is donation form in single price mode?
659
		 * @param int|string $ID The ID of the donation form.
660
		 */
661
		return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID );
662
663
	}
664
665
	/**
666
	 * Determine if custom price mode is enabled or disabled
667
	 *
668
	 * @since  1.6
669
	 * @access public
670
	 *
671
	 * @return bool
672
	 */
673
	public function is_custom_price_mode() {
674
675
		$option = give_get_meta( $this->ID, '_give_custom_amount', true );
676
		$ret    = 0;
677
678
		if ( give_is_setting_enabled( $option ) ) {
679
			$ret = 1;
680
		}
681
682
		/**
683
		 * Override the price mode for a donation when checking if is in custom price mode.
684
		 *
685
		 * @since 1.6
686
		 *
687
		 * @param bool       $ret Is donation form in custom price mode?
688
		 * @param int|string $ID  The ID of the donation form.
689
		 */
690
		return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID );
691
692
	}
693
694
	/**
695
	 * Determine if custom price mode is enabled or disabled
696
	 *
697
	 * @since  1.8.18
698
	 * @access public
699
	 *
700
	 * @param string|float $amount Donation Amount.
701
	 *
702
	 * @return bool
703
	 */
704
	public function is_custom_price( $amount ) {
705
		$result = false;
706
		$amount = give_maybe_sanitize_amount( $amount );
707
708
		if ( $this->is_custom_price_mode() ) {
709
710
			if ( 'set' === $this->get_type() ) {
711
				if ( $amount !== $this->get_price() ) {
712
					$result = true;
713
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
714
715
			} elseif ( 'multi' === $this->get_type() ) {
716
				$level_amounts = array_map( 'give_maybe_sanitize_amount', wp_list_pluck( $this->get_prices(), '_give_amount' ) );
717
				$result        = ! in_array( $amount, $level_amounts );
718
			}
719
		}
720
721
		/**
722
		 * Filter to reset whether it is custom price or not.
723
		 *
724
		 * @param bool         $result True/False.
725
		 * @param string|float $amount Donation Amount.
726
		 * @param int          $this->ID Form ID.
727
		 *
728
		 * @since 1.8.18
729
		 */
730
		return (bool) apply_filters( 'give_is_custom_price', $result, $amount, $this->ID );
731
	}
732
733
	/**
734
	 * Has Variable Prices
735
	 *
736
	 * Determine if the donation form has variable prices enabled
737
	 *
738
	 * @since  1.0
739
	 * @access public
740
	 *
741
	 * @return bool
742
	 */
743 View Code Duplication
	public function has_variable_prices() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
744
745
		$option = give_get_meta( $this->ID, '_give_price_option', true );
746
		$ret    = 0;
747
748
		if ( $option === 'multi' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
749
			$ret = 1;
750
		}
751
752
		/**
753
		 * Filter: Override whether the donation form has variables prices.
754
		 *
755
		 * @param bool       $ret Does donation form have variable prices?
756
		 * @param int|string $ID  The ID of the donation form.
757
		 */
758
		return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID );
759
760
	}
761
762
	/**
763
	 * Retrieve the donation form type, set or multi-level
764
	 *
765
	 * @since  1.5
766
	 * @access public
767
	 *
768
	 * @return string Type of donation form, either 'set' or 'multi'.
769
	 */
770 View Code Duplication
	public function get_type() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
771
772
		if ( ! isset( $this->type ) ) {
773
774
			$this->type = give_get_meta( $this->ID, '_give_price_option', true );
775
776
			if ( empty( $this->type ) ) {
777
				$this->type = 'set';
778
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
779
780
		}
781
782
		return apply_filters( 'give_get_form_type', $this->type, $this->ID );
783
784
	}
785
786
	/**
787
	 * Get form tag classes.
788
	 *
789
	 * Provides the classes for the donation <form> html tag and filters for customization.
790
	 *
791
	 * @since  1.6
792
	 * @access public
793
	 *
794
	 * @param  $args
795
	 *
796
	 * @return string
797
	 */
798
	public function get_form_classes( $args ) {
799
800
		$float_labels_option = give_is_float_labels_enabled( $args )
801
			? 'float-labels-enabled'
802
			: '';
803
804
		$form_classes_array = apply_filters( 'give_form_classes', array(
805
			'give-form',
806
			'give-form-' . $this->ID,
807
			'give-form-type-' . $this->get_type(),
808
			$float_labels_option,
809
		), $this->ID, $args );
810
811
		// Remove empty class names.
812
		$form_classes_array = array_filter( $form_classes_array );
813
814
		return implode( ' ', $form_classes_array );
815
816
	}
817
818
	/**
819
	 * Get form wrap Classes.
820
	 *
821
	 * Provides the classes for the donation form div wrapper and filters for customization.
822
	 *
823
	 * @access public
824
	 *
825
	 * @param  $args
826
	 *
827
	 * @return string
828
	 */
829
	public function get_form_wrap_classes( $args ) {
830
		$custom_class = array(
831
			'give-form-wrap',
832
		);
833
834
		if ( $this->is_close_donation_form() ) {
835
			$custom_class[] = 'give-form-closed';
836
		} else{
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
837
			$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
838
				? $args['display_style']
839
				: give_get_meta( $this->ID, '_give_payment_display', true );
840
841
			$custom_class[] = "give-display-{$display_option}";
842
843
			// If admin want to show only button for form then user inbuilt modal functionality.
844
			if ( 'button' === $display_option ) {
845
				$custom_class[] = 'give-display-button-only';
846
			}
847
		}
848
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
849
850
		/**
851
		 * Filter the donation form classes.
852
		 *
853
		 * @since 1.0
854
		 */
855
		$form_wrap_classes_array = (array) apply_filters( 'give_form_wrap_classes', $custom_class, $this->ID, $args );
856
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
857
858
		return implode( ' ', $form_wrap_classes_array );
859
860
	}
861
862
	/**
863
	 * Get if form type set or not.
864
	 *
865
	 * @since  1.6
866
	 * @access public
867
	 *
868
	 * @return bool
869
	 */
870
	public function is_set_type_donation_form() {
871
		$form_type = $this->get_type();
872
873
		return ( 'set' === $form_type ? true : false );
874
	}
875
876
	/**
877
	 * Get if form type multi or not.
878
	 *
879
	 * @since  1.6
880
	 * @access public
881
	 *
882
	 * @return bool True if form type is 'multi' and false otherwise.
883
	 */
884
	public function is_multi_type_donation_form() {
885
		$form_type = $this->get_type();
886
887
		return ( 'multi' === $form_type ? true : false );
888
889
	}
890
891
	/**
892
	 * Retrieve the sale count for the donation form
893
	 *
894
	 * @since  1.0
895
	 * @access public
896
	 *
897
	 * @return int    Donation form sale count.
898
	 */
899 View Code Duplication
	public function get_sales() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
900
901
		if ( ! isset( $this->sales ) ) {
902
903
			if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) {
904
				add_post_meta( $this->ID, '_give_form_sales', 0 );
905
			} // End if
906
907
			$this->sales = give_get_meta( $this->ID, '_give_form_sales', true );
908
909
			if ( $this->sales < 0 ) {
910
				// Never let sales be less than zero.
911
				$this->sales = 0;
912
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
913
914
		}
915
916
		return $this->sales;
917
918
	}
919
920
	/**
921
	 * Increment the sale count by one
922
	 *
923
	 * @since  1.0
924
	 * @access public
925
	 *
926
	 * @param  int $quantity The quantity to increase the donations by. Default is 1.
927
	 *
928
	 * @return int|false     New number of total sales.
929
	 */
930
	public function increase_sales( $quantity = 1 ) {
931
932
		$sales       = give_get_form_sales_stats( $this->ID );
933
		$quantity    = absint( $quantity );
934
		$total_sales = $sales + $quantity;
935
936
		if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
937
938
			$this->sales = $total_sales;
939
940
			return $this->sales;
941
942
		}
943
944
		return false;
945
	}
946
947
	/**
948
	 * Decrement the sale count by one
949
	 *
950
	 * @since  1.0
951
	 * @access public
952
	 *
953
	 * @param  int $quantity The quantity to decrease by. Default is 1.
954
	 *
955
	 * @return int|false     New number of total sales.
956
	 */
957
	public function decrease_sales( $quantity = 1 ) {
958
959
		$sales = give_get_form_sales_stats( $this->ID );
960
961
		// Only decrease if not already zero
962
		if ( $sales > 0 ) {
963
964
			$quantity    = absint( $quantity );
965
			$total_sales = $sales - $quantity;
966
967
			if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
968
969
				$this->sales = $sales;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sales can also be of type double. However, the property $sales is declared as type integer. 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...
970
971
				return $sales;
972
973
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
974
975
		}
976
977
		return false;
978
979
	}
980
981
	/**
982
	 * Retrieve the total earnings for the form
983
	 *
984
	 * @since  1.0
985
	 * @access public
986
	 *
987
	 * @return float  Donation form total earnings.
988
	 */
989 View Code Duplication
	public function get_earnings() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
990
991
		if ( ! isset( $this->earnings ) ) {
992
993
			if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) {
994
				add_post_meta( $this->ID, '_give_form_earnings', 0 );
995
			}
996
997
			$this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true );
998
999
			if ( $this->earnings < 0 ) {
1000
				// Never let earnings be less than zero
1001
				$this->earnings = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $earnings was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1002
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1003
1004
		}
1005
1006
		return $this->earnings;
1007
1008
	}
1009
1010
	/**
1011
	 * Increase the earnings by the given amount
1012
	 *
1013
	 * @since  1.0
1014
	 * @access public
1015
	 *
1016
	 * @param  int $amount Amount of donation. Default is 0.
1017
	 *
1018
	 * @return float|false
1019
	 */
1020 View Code Duplication
	public function increase_earnings( $amount = 0 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1021
1022
		$earnings   = give_get_form_earnings_stats( $this->ID );
1023
		$new_amount = $earnings + (float) $amount;
1024
1025
		if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1026
1027
			$this->earnings = $new_amount;
1028
1029
			return $this->earnings;
1030
1031
		}
1032
1033
		return false;
1034
1035
	}
1036
1037
	/**
1038
	 * Decrease the earnings by the given amount
1039
	 *
1040
	 * @since  1.0
1041
	 * @access public
1042
	 *
1043
	 * @param  int $amount Amount of donation.
1044
	 *
1045
	 * @return float|false
1046
	 */
1047 View Code Duplication
	public function decrease_earnings( $amount ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1048
1049
		$earnings = give_get_form_earnings_stats( $this->ID );
1050
1051
		if ( $earnings > 0 ) {
1052
			// Only decrease if greater than zero
1053
			$new_amount = $earnings - (float) $amount;
1054
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1055
1056
			if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1057
1058
				$this->earnings = $new_amount;
1059
1060
				return $this->earnings;
1061
1062
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1063
1064
		}
1065
1066
		return false;
1067
1068
	}
1069
1070
	/**
1071
	 * Determine if the donation is free or if the given price ID is free
1072
	 *
1073
	 * @since  1.0
1074
	 * @access public
1075
	 *
1076
	 * @param  int $price_id Price ID. Default is false.
1077
	 *
1078
	 * @return bool
1079
	 */
1080
	public function is_free( $price_id = false ) {
1081
1082
		$is_free          = false;
1083
		$variable_pricing = give_has_variable_prices( $this->ID );
1084
1085
		if ( $variable_pricing && ! is_null( $price_id ) && $price_id !== false ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
1086
			$price = give_get_price_option_amount( $this->ID, $price_id );
1087
		} elseif ( ! $variable_pricing ) {
1088
			$price = give_get_meta( $this->ID, '_give_set_price', true );
1089
		}
1090
1091
		if ( isset( $price ) && (float) $price == 0 ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
1092
			$is_free = true;
1093
		}
1094
1095
		return (bool) apply_filters( 'give_is_free_donation', $is_free, $this->ID, $price_id );
1096
1097
	}
1098
1099
	/**
1100
	 * Determine if donation form closed or not
1101
	 *
1102
	 * Form will be close if:
1103
	 *  a. form has fixed goal
1104
	 *  b. close form when goal achieved cmb2 setting is set to 'Yes'
1105
	 *  c. goal has been achieved
1106
	 *
1107
	 * @since  1.4.5
1108
	 * @access public
1109
	 *
1110
	 * @return bool
1111
	 */
1112
	public function is_close_donation_form() {
1113
1114
		$goal_format = give_get_form_goal_format( $this->ID );
1115
1116
		/**
1117
		 * Filter the close form result.
1118
		 *
1119
		 * @since 1.8
1120
		 */
1121
		$is_close_form = apply_filters(
1122
			'give_is_close_donation_form',
1123
			(
1124
				give_is_setting_enabled( give_get_meta( $this->ID, '_give_goal_option', true ) ) &&
1125
				give_is_setting_enabled( give_get_meta( $this->ID, '_give_close_form_when_goal_achieved', true ) ) &&
1126
				( 'donation' === $goal_format ? $this->get_goal() <= $this->get_sales() : $this->get_goal() <= $this->get_earnings() )
1127
			),
1128
			$this->ID
1129
		);
1130
1131
		return $is_close_form;
1132
	}
1133
1134
	/**
1135
	 * Updates a single meta entry for the donation form
1136
	 *
1137
	 * @since  1.5
1138
	 * @access private
1139
	 *
1140
	 * @param  string              $meta_key   The meta_key to update.
1141
	 * @param  string|array|object $meta_value The value to put into the meta.
1142
	 *
1143
	 * @return bool                            The result of the update query.
1144
	 */
1145
	private function update_meta( $meta_key = '', $meta_value = '' ) {
1146
1147
		/* @var WPDB $wpdb */
1148
		global $wpdb;
1149
1150
		// Bailout.
1151
		if ( empty( $meta_key ) ) {
1152
			return false;
1153
		}
1154
1155
		if ( give_update_meta( $this->ID, $meta_key, $meta_value  ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
1156
			return true;
1157
		}
1158
1159
		return false;
1160
	}
1161
1162
}
1163