Completed
Push — issue/2360 ( c1b5b4 )
by Ravinder
457:34 queued 450:36
created

Give_Donate_Form::is_custom_price()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 28
rs 9.1608
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 $prices
27
 * @property $goal
28
 * @property $sales
29
 * @property $earnings
30
 * @property $post_type
31
 */
32
class Give_Donate_Form {
33
34
	/**
35
	 * The donation ID.
36
	 *
37
	 * @since  1.0
38
	 * @access public
39
	 *
40
	 * @var    int
41
	 */
42
	public $ID = 0;
43
44
	/**
45
	 * The donation price.
46
	 *
47
	 * @since  1.0
48
	 * @access private
49
	 *
50
	 * @var    float
51
	 */
52
	private $price;
53
54
	/**
55
	 * The minimum donation price.
56
	 *
57
	 * @since  1.3.6
58
	 * @access private
59
	 *
60
	 * @var    float
61
	 */
62
	private $minimum_price;
63
64
	/**
65
	 * The donation prices, if Price Levels are enabled.
66
	 *
67
	 * @since  1.0
68
	 * @access private
69
	 *
70
	 * @var    array
71
	 */
72
	private $prices;
73
74
	/**
75
	 * The donation goal.
76
	 *
77
	 * @since  1.0
78
	 * @access private
79
	 *
80
	 * @var    float
81
	 */
82
	private $goal;
83
84
	/**
85
	 * The form's sale count.
86
	 *
87
	 * @since  1.0
88
	 * @access private
89
	 *
90
	 * @var    int
91
	 */
92
	private $sales;
93
94
	/**
95
	 * The form's total earnings
96
	 *
97
	 * @since  1.0
98
	 * @access private
99
	 *
100
	 * @var    float
101
	 */
102
	private $earnings;
103
104
	/**
105
	 * Declare the default properties in WP_Post as we can't extend it
106
	 * Anything we've declared above has been removed.
107
	 */
108
109
	/**
110
	 * The post author
111
	 *
112
	 * @since  1.0
113
	 * @access public
114
	 *
115
	 * @var    int
116
	 */
117
	public $post_author = 0;
118
119
	/**
120
	 * The post date
121
	 *
122
	 * @since  1.0
123
	 * @access public
124
	 *
125
	 * @var    string
126
	 */
127
	public $post_date = '0000-00-00 00:00:00';
128
129
	/**
130
	 * The post GTM date
131
	 *
132
	 * @since  1.0
133
	 * @access public
134
	 *
135
	 * @var    string
136
	 */
137
	public $post_date_gmt = '0000-00-00 00:00:00';
138
139
	/**
140
	 * The post content
141
	 *
142
	 * @since  1.0
143
	 * @access public
144
	 *
145
	 * @var    string
146
	 */
147
	public $post_content = '';
148
149
	/**
150
	 * The post title
151
	 *
152
	 * @since  1.0
153
	 * @access public
154
	 *
155
	 * @var    string
156
	 */
157
	public $post_title = '';
158
159
	/**
160
	 * The post excerpt
161
	 *
162
	 * @since  1.0
163
	 * @access public
164
	 *
165
	 * @var    string
166
	 */
167
	public $post_excerpt = '';
168
169
	/**
170
	 * The post status
171
	 *
172
	 * @since  1.0
173
	 * @access public
174
	 *
175
	 * @var    string
176
	 */
177
	public $post_status = 'publish';
178
179
	/**
180
	 * The comment status
181
	 *
182
	 * @since  1.0
183
	 * @access public
184
	 *
185
	 * @var    string
186
	 */
187
	public $comment_status = 'open';
188
189
	/**
190
	 * The ping status
191
	 *
192
	 * @since  1.0
193
	 * @access public
194
	 *
195
	 * @var    string
196
	 */
197
	public $ping_status = 'open';
198
199
	/**
200
	 * The post password
201
	 *
202
	 * @since  1.0
203
	 * @access public
204
	 *
205
	 * @var    string
206
	 */
207
	public $post_password = '';
208
209
	/**
210
	 * The post name
211
	 *
212
	 * @since  1.0
213
	 * @access public
214
	 *
215
	 * @var    string
216
	 */
217
	public $post_name = '';
218
219
	/**
220
	 * Ping
221
	 *
222
	 * @since  1.0
223
	 * @access public
224
	 *
225
	 * @var    string
226
	 */
227
	public $to_ping = '';
228
229
	/**
230
	 * Pinged
231
	 *
232
	 * @since  1.0
233
	 * @access public
234
	 *
235
	 * @var    string
236
	 */
237
	public $pinged = '';
238
239
	/**
240
	 * The post modified date
241
	 *
242
	 * @since  1.0
243
	 * @access public
244
	 *
245
	 * @var    string
246
	 */
247
	public $post_modified = '0000-00-00 00:00:00';
248
249
	/**
250
	 * The post modified GTM date
251
	 *
252
	 * @since  1.0
253
	 * @access public
254
	 *
255
	 * @var    string
256
	 */
257
	public $post_modified_gmt = '0000-00-00 00:00:00';
258
259
	/**
260
	 * The post filtered content
261
	 *
262
	 * @since  1.0
263
	 * @access public
264
	 *
265
	 * @var    string
266
	 */
267
	public $post_content_filtered = '';
268
269
	/**
270
	 * The post parent
271
	 *
272
	 * @since  1.0
273
	 * @access public
274
	 *
275
	 * @var    int
276
	 */
277
	public $post_parent = 0;
278
279
	/**
280
	 * The post GUID
281
	 *
282
	 * @since  1.0
283
	 * @access public
284
	 *
285
	 * @var    string
286
	 */
287
	public $guid = '';
288
289
	/**
290
	 * The menu order
291
	 *
292
	 * @since  1.0
293
	 * @access public
294
	 *
295
	 * @var    int
296
	 */
297
	public $menu_order = 0;
298
299
	/**
300
	 * The mime type0
301
	 *
302
	 * @since  1.0
303
	 * @access public
304
	 *
305
	 * @var    string
306
	 */
307
	public $post_mime_type = '';
308
309
	/**
310
	 * The comment count
311
	 *
312
	 * @since  1.0
313
	 * @access public
314
	 *
315
	 * @var    int
316
	 */
317
	public $comment_count = 0;
318
319
	/**
320
	 * Filtered
321
	 *
322
	 * @since  1.0
323
	 * @access public
324
	 *
325
	 * @var    string
326
	 */
327
	public $filter;
328
329
	/**
330
	 * Class Constructor
331
	 *
332
	 * Set up the Give Donate Form Class.
333
	 *
334
	 * @since  1.0
335
	 * @access public
336
	 *
337
	 * @param  bool  $_id   Post id. Default is false.
338
	 * @param  array $_args Arguments passed.
339
	 */
340
	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...
341
342
		$donation_form = WP_Post::get_instance( $_id );
343
344
		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...
345
	}
346
347
	/**
348
	 * Given the donation form data, let's set the variables
349
	 *
350
	 * @since  1.5
351
	 * @access private
352
	 *
353
	 * @param  WP_Post $donation_form WP_Post Object for the donation form.
354
	 *
355
	 * @return bool                   If the setup was successful or not.
356
	 */
357
	private function setup_donation_form( $donation_form ) {
358
359
		if ( ! is_object( $donation_form ) ) {
360
			return false;
361
		}
362
363
		if ( ! is_a( $donation_form, 'WP_Post' ) ) {
364
			return false;
365
		}
366
367
		if ( 'give_forms' !== $donation_form->post_type ) {
368
			return false;
369
		}
370
371
		foreach ( $donation_form as $key => $value ) {
372
373
			switch ( $key ) {
374
375
				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...
376
					$this->$key = $value;
377
					break;
378
379
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
380
381
		}
382
383
		return true;
384
385
	}
386
387
	/**
388
	 * Magic __get function to dispatch a call to retrieve a private property
389
	 *
390
	 * @since  1.0
391
	 * @access public
392
	 *
393
	 * @param  string $key
394
	 *
395
	 * @return mixed
396
	 */
397 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...
398
399
		if ( method_exists( $this, 'get_' . $key ) ) {
400
401
			return call_user_func( array( $this, 'get_' . $key ) );
402
403
		} else {
404
405
			/* translators: %s: property key */
406
			return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) );
407
408
		}
409
410
	}
411
412
	/**
413
	 * Creates a donation form
414
	 *
415
	 * @since  1.5
416
	 * @access public
417
	 *
418
	 * @param  array $data Array of attributes for a donation form.
419
	 *
420
	 * @return bool|int    False if data isn't passed and class not instantiated for creation, or New Form ID.
421
	 */
422
	public function create( $data = array() ) {
423
424
		if ( $this->id != 0 ) {
0 ignored issues
show
introduced by
Found "!= 0". Use Yoda Condition checks, you must
Loading history...
425
			return false;
426
		}
427
428
		$defaults = array(
429
			'post_type'   => 'give_forms',
430
			'post_status' => 'draft',
431
			'post_title'  => __( 'New Donation Form', 'give' ),
432
		);
433
434
		$args = wp_parse_args( $data, $defaults );
435
436
		/**
437
		 * Fired before a donation form is created
438
		 *
439
		 * @param array $args The post object arguments used for creation.
440
		 */
441
		do_action( 'give_form_pre_create', $args );
442
443
		$id = wp_insert_post( $args, true );
444
445
		$donation_form = WP_Post::get_instance( $id );
446
447
		/**
448
		 * Fired after a donation form is created
449
		 *
450
		 * @param int   $id   The post ID of the created item.
451
		 * @param array $args The post object arguments used for creation.
452
		 */
453
		do_action( 'give_form_post_create', $id, $args );
454
455
		return $this->setup_donation_form( $donation_form );
456
457
	}
458
459
	/**
460
	 * Retrieve the ID
461
	 *
462
	 * @since  1.0
463
	 * @access public
464
	 *
465
	 * @return int    Donation form ID.
466
	 */
467
	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...
468
		return $this->ID;
469
	}
470
471
	/**
472
	 * Retrieve the donation form name
473
	 *
474
	 * @since  1.5
475
	 * @access public
476
	 *
477
	 * @return string Donation form name.
478
	 */
479
	public function get_name() {
480
		return get_the_title( $this->ID );
481
	}
482
483
	/**
484
	 * Retrieve the price
485
	 *
486
	 * @since  1.0
487
	 * @access public
488
	 *
489
	 * @return float  Price.
490
	 */
491 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...
492
493
		if ( ! isset( $this->price ) ) {
494
495
			$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...
496
				give_get_meta(
497
					$this->ID,
498
					'_give_set_price',
499
					true
500
				)
501
			);
502
503
			if ( ! $this->price ) {
504
				$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...
505
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
506
507
		}
508
509
		/**
510
		 * Override the donation form set price.
511
		 *
512
		 * @since 1.0
513
		 *
514
		 * @param string     $price The donation form price.
515
		 * @param string|int $id    The form ID.
516
		 */
517
		return apply_filters( 'give_get_set_price', $this->price, $this->ID );
518
	}
519
520
	/**
521
	 * Retrieve the minimum price.
522
	 *
523
	 * @since  1.3.6
524
	 * @access public
525
	 *
526
	 * @return float  Minimum price.
527
	 */
528
	public function get_minimum_price() {
529
530
		if ( ! isset( $this->minimum_price ) ) {
531
532
			$this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_minimum', true );
533
534
			if ( ! $this->is_custom_price_mode() ) {
535
				$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...
536
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
537
538
		}
539
540
		return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID );
541
	}
542
543
	/**
544
	 * Retrieve the variable prices
545
	 *
546
	 * @since  1.0
547
	 * @access public
548
	 *
549
	 * @return array  Variable prices.
550
	 */
551
	public function get_prices() {
552
553
		if ( ! isset( $this->prices ) ) {
554
555
			$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...
556
557
		}
558
559
		/**
560
		 * Override multi-level prices
561
		 *
562
		 * @since 1.0
563
		 *
564
		 * @param array      $prices The array of mulit-level prices.
565
		 * @param int|string $ID     The ID of the form.
566
		 */
567
		return apply_filters( 'give_get_donation_levels', $this->prices, $this->ID );
568
569
	}
570
571
	/**
572
	 * Retrieve the goal
573
	 *
574
	 * @since  1.0
575
	 * @access public
576
	 *
577
	 * @return float  Goal.
578
	 */
579
	public function get_goal() {
580
581
		if ( ! isset( $this->goal ) ) {
582
583
			$this->goal = give_get_meta( $this->ID, '_give_set_goal', true );
584
585
			if ( ! $this->goal ) {
586
				$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...
587
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
588
589
		}
590
591
		return apply_filters( 'give_get_set_goal', $this->goal, $this->ID );
592
593
	}
594
595
	/**
596
	 * Determine if single price mode is enabled or disabled
597
	 *
598
	 * @since  1.0
599
	 * @access public
600
	 *
601
	 * @return bool
602
	 */
603 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...
604
605
		$option = give_get_meta( $this->ID, '_give_price_option', true );
606
		$ret    = 0;
607
608
		if ( empty( $option ) || $option === 'set' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
609
			$ret = 1;
610
		}
611
612
		/**
613
		 * Override the price mode for a donation when checking if is in single price mode.
614
		 *
615
		 * @since 1.0
616
		 *
617
		 * @param bool       $ret Is donation form in single price mode?
618
		 * @param int|string $ID The ID of the donation form.
619
		 */
620
		return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID );
621
622
	}
623
624
	/**
625
	 * Determine if custom price mode is enabled or disabled
626
	 *
627
	 * @since  1.6
628
	 * @access public
629
	 *
630
	 * @return bool
631
	 */
632
	public function is_custom_price_mode() {
633
634
		$option = give_get_meta( $this->ID, '_give_custom_amount', true );
635
		$ret    = 0;
636
637
		if ( give_is_setting_enabled( $option ) ) {
638
			$ret = 1;
639
		}
640
641
		/**
642
		 * Override the price mode for a donation when checking if is in custom price mode.
643
		 *
644
		 * @since 1.6
645
		 *
646
		 * @param bool       $ret Is donation form in custom price mode?
647
		 * @param int|string $ID  The ID of the donation form.
648
		 */
649
		return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID );
650
651
	}
652
653
	/**
654
	 * Determine if custom price mode is enabled or disabled
655
	 *
656
	 * @since  1.8.18
657
	 * @access public
658
	 *
659
	 * @param string|float $amount Donation Amount.
660
	 *
661
	 * @return bool
662
	 */
663
	public function is_custom_price( $amount ) {
664
		$result = false;
665
		$amount = give_maybe_sanitize_amount( $amount );
666
667
		if ( $this->is_custom_price_mode() ) {
668
669
			if ( 'set' === $this->get_type() ) {
670
				if ( $amount !== $this->get_price() ) {
671
					$result = true;
672
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
673
674
			} elseif ( 'multi' === $this->get_type() ) {
675
				$level_amounts = array_map( 'give_maybe_sanitize_amount', wp_list_pluck( $this->get_prices(), '_give_amount' ) );
676
				$result        = ! in_array( $amount, $level_amounts );
677
			}
678
		}
679
680
		/**
681
		 * Filter to reset whether it is custom price or not.
682
		 *
683
		 * @param bool         $result True/False.
684
		 * @param string|float $amount Donation Amount.
685
		 * @param int          $this->ID Form ID.
686
		 *
687
		 * @since 1.8.18
688
		 */
689
		return (bool) apply_filters( 'give_is_custom_price', $result, $amount, $this->ID );
690
	}
691
692
	/**
693
	 * Has Variable Prices
694
	 *
695
	 * Determine if the donation form has variable prices enabled
696
	 *
697
	 * @since  1.0
698
	 * @access public
699
	 *
700
	 * @return bool
701
	 */
702 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...
703
704
		$option = give_get_meta( $this->ID, '_give_price_option', true );
705
		$ret    = 0;
706
707
		if ( $option === 'multi' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
708
			$ret = 1;
709
		}
710
711
		/**
712
		 * Filter: Override whether the donation form has variables prices.
713
		 *
714
		 * @param bool       $ret Does donation form have variable prices?
715
		 * @param int|string $ID  The ID of the donation form.
716
		 */
717
		return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID );
718
719
	}
720
721
	/**
722
	 * Retrieve the donation form type, set or multi-level
723
	 *
724
	 * @since  1.5
725
	 * @access public
726
	 *
727
	 * @return string Type of donation form, either 'set' or 'multi'.
728
	 */
729 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...
730
731
		if ( ! isset( $this->type ) ) {
732
733
			$this->type = give_get_meta( $this->ID, '_give_price_option', true );
734
735
			if ( empty( $this->type ) ) {
736
				$this->type = 'set';
737
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
738
739
		}
740
741
		return apply_filters( 'give_get_form_type', $this->type, $this->ID );
742
743
	}
744
745
	/**
746
	 * Get form tag classes.
747
	 *
748
	 * Provides the classes for the donation <form> html tag and filters for customization.
749
	 *
750
	 * @since  1.6
751
	 * @access public
752
	 *
753
	 * @param  $args
754
	 *
755
	 * @return string
756
	 */
757
	public function get_form_classes( $args ) {
758
759
		$float_labels_option = give_is_float_labels_enabled( $args )
760
			? 'float-labels-enabled'
761
			: '';
762
763
		$form_classes_array = apply_filters( 'give_form_classes', array(
764
			'give-form',
765
			'give-form-' . $this->ID,
766
			'give-form-type-' . $this->get_type(),
767
			$float_labels_option,
768
		), $this->ID, $args );
769
770
		// Remove empty class names.
771
		$form_classes_array = array_filter( $form_classes_array );
772
773
		return implode( ' ', $form_classes_array );
774
775
	}
776
777
	/**
778
	 * Get form wrap Classes.
779
	 *
780
	 * Provides the classes for the donation form div wrapper and filters for customization.
781
	 *
782
	 * @access public
783
	 *
784
	 * @param  $args
785
	 *
786
	 * @return string
787
	 */
788
	public function get_form_wrap_classes( $args ) {
789
		$custom_class = array(
790
			'give-form-wrap',
791
		);
792
793
		if ( $this->is_close_donation_form() ) {
794
			$custom_class[] = 'give-form-closed';
795
		} 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...
796
			$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
797
				? $args['display_style']
798
				: give_get_meta( $this->ID, '_give_payment_display', true );
799
800
			$custom_class[] = "give-display-{$display_option}";
801
802
			// If admin want to show only button for form then user inbuilt modal functionality.
803
			if ( 'button' === $display_option ) {
804
				$custom_class[] = 'give-display-button-only';
805
			}
806
		}
807
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
808
809
		/**
810
		 * Filter the donation form classes.
811
		 *
812
		 * @since 1.0
813
		 */
814
		$form_wrap_classes_array = (array) apply_filters( 'give_form_wrap_classes', $custom_class, $this->ID, $args );
815
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
816
817
		return implode( ' ', $form_wrap_classes_array );
818
819
	}
820
821
	/**
822
	 * Get if form type set or not.
823
	 *
824
	 * @since  1.6
825
	 * @access public
826
	 *
827
	 * @return bool
828
	 */
829
	public function is_set_type_donation_form() {
830
		$form_type = $this->get_type();
831
832
		return ( 'set' === $form_type ? true : false );
833
	}
834
835
	/**
836
	 * Get if form type multi or not.
837
	 *
838
	 * @since  1.6
839
	 * @access public
840
	 *
841
	 * @return bool True if form type is 'multi' and false otherwise.
842
	 */
843
	public function is_multi_type_donation_form() {
844
		$form_type = $this->get_type();
845
846
		return ( 'multi' === $form_type ? true : false );
847
848
	}
849
850
	/**
851
	 * Retrieve the sale count for the donation form
852
	 *
853
	 * @since  1.0
854
	 * @access public
855
	 *
856
	 * @return int    Donation form sale count.
857
	 */
858 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...
859
860
		if ( ! isset( $this->sales ) ) {
861
862
			if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) {
863
				add_post_meta( $this->ID, '_give_form_sales', 0 );
864
			} // End if
865
866
			$this->sales = give_get_meta( $this->ID, '_give_form_sales', true );
867
868
			if ( $this->sales < 0 ) {
869
				// Never let sales be less than zero.
870
				$this->sales = 0;
871
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
872
873
		}
874
875
		return $this->sales;
876
877
	}
878
879
	/**
880
	 * Increment the sale count by one
881
	 *
882
	 * @since  1.0
883
	 * @access public
884
	 *
885
	 * @param  int $quantity The quantity to increase the donations by. Default is 1.
886
	 *
887
	 * @return int|false     New number of total sales.
888
	 */
889
	public function increase_sales( $quantity = 1 ) {
890
891
		$sales       = give_get_form_sales_stats( $this->ID );
892
		$quantity    = absint( $quantity );
893
		$total_sales = $sales + $quantity;
894
895
		if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
896
897
			$this->sales = $total_sales;
898
899
			return $this->sales;
900
901
		}
902
903
		return false;
904
	}
905
906
	/**
907
	 * Decrement the sale count by one
908
	 *
909
	 * @since  1.0
910
	 * @access public
911
	 *
912
	 * @param  int $quantity The quantity to decrease by. Default is 1.
913
	 *
914
	 * @return int|false     New number of total sales.
915
	 */
916
	public function decrease_sales( $quantity = 1 ) {
917
918
		$sales = give_get_form_sales_stats( $this->ID );
919
920
		// Only decrease if not already zero
921
		if ( $sales > 0 ) {
922
923
			$quantity    = absint( $quantity );
924
			$total_sales = $sales - $quantity;
925
926
			if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
927
928
				$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...
929
930
				return $sales;
931
932
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
933
934
		}
935
936
		return false;
937
938
	}
939
940
	/**
941
	 * Retrieve the total earnings for the form
942
	 *
943
	 * @since  1.0
944
	 * @access public
945
	 *
946
	 * @return float  Donation form total earnings.
947
	 */
948 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...
949
950
		if ( ! isset( $this->earnings ) ) {
951
952
			if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) {
953
				add_post_meta( $this->ID, '_give_form_earnings', 0 );
954
			}
955
956
			$this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true );
957
958
			if ( $this->earnings < 0 ) {
959
				// Never let earnings be less than zero
960
				$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...
961
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
962
963
		}
964
965
		return $this->earnings;
966
967
	}
968
969
	/**
970
	 * Increase the earnings by the given amount
971
	 *
972
	 * @since  1.0
973
	 * @access public
974
	 *
975
	 * @param  int $amount Amount of donation. Default is 0.
976
	 *
977
	 * @return float|false
978
	 */
979 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...
980
981
		$earnings   = give_get_form_earnings_stats( $this->ID );
982
		$new_amount = $earnings + (float) $amount;
983
984
		if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
985
986
			$this->earnings = $new_amount;
987
988
			return $this->earnings;
989
990
		}
991
992
		return false;
993
994
	}
995
996
	/**
997
	 * Decrease the earnings by the given amount
998
	 *
999
	 * @since  1.0
1000
	 * @access public
1001
	 *
1002
	 * @param  int $amount Amount of donation.
1003
	 *
1004
	 * @return float|false
1005
	 */
1006 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...
1007
1008
		$earnings = give_get_form_earnings_stats( $this->ID );
1009
1010
		if ( $earnings > 0 ) {
1011
			// Only decrease if greater than zero
1012
			$new_amount = $earnings - (float) $amount;
1013
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1014
1015
			if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1016
1017
				$this->earnings = $new_amount;
1018
1019
				return $this->earnings;
1020
1021
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1022
1023
		}
1024
1025
		return false;
1026
1027
	}
1028
1029
	/**
1030
	 * Determine if the donation is free or if the given price ID is free
1031
	 *
1032
	 * @since  1.0
1033
	 * @access public
1034
	 *
1035
	 * @param  int $price_id Price ID. Default is false.
1036
	 *
1037
	 * @return bool
1038
	 */
1039
	public function is_free( $price_id = false ) {
1040
1041
		$is_free          = false;
1042
		$variable_pricing = give_has_variable_prices( $this->ID );
1043
1044
		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...
1045
			$price = give_get_price_option_amount( $this->ID, $price_id );
1046
		} elseif ( ! $variable_pricing ) {
1047
			$price = give_get_meta( $this->ID, '_give_set_price', true );
1048
		}
1049
1050
		if ( isset( $price ) && (float) $price == 0 ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
1051
			$is_free = true;
1052
		}
1053
1054
		return (bool) apply_filters( 'give_is_free_donation', $is_free, $this->ID, $price_id );
1055
1056
	}
1057
1058
	/**
1059
	 * Determine if donation form closed or not
1060
	 *
1061
	 * Form will be close if:
1062
	 *  a. form has fixed goal
1063
	 *  b. close form when goal achieved cmb2 setting is set to 'Yes'
1064
	 *  c. goal has been achieved
1065
	 *
1066
	 * @since  1.4.5
1067
	 * @access public
1068
	 *
1069
	 * @return bool
1070
	 */
1071
	public function is_close_donation_form() {
1072
1073
		/**
1074
		 * Filter the close form result.
1075
		 *
1076
		 * @since 1.8
1077
		 */
1078
		$is_close_form = apply_filters(
1079
			'give_is_close_donation_form',
1080
			(
1081
			give_is_setting_enabled( give_get_meta( $this->ID, '_give_goal_option', true ) ) )
1082
			&& give_is_setting_enabled( give_get_meta( $this->ID, '_give_close_form_when_goal_achieved', true ) )
1083
			&& ( $this->get_goal() <= $this->get_earnings()
1084
			),
1085
			$this->ID
1086
		);
1087
1088
		return $is_close_form;
1089
	}
1090
1091
	/**
1092
	 * Updates a single meta entry for the donation form
1093
	 *
1094
	 * @since  1.5
1095
	 * @access private
1096
	 *
1097
	 * @param  string              $meta_key   The meta_key to update.
1098
	 * @param  string|array|object $meta_value The value to put into the meta.
1099
	 *
1100
	 * @return bool                            The result of the update query.
1101
	 */
1102
	private function update_meta( $meta_key = '', $meta_value = '' ) {
1103
1104
		/* @var WPDB $wpdb */
1105
		global $wpdb;
1106
1107
		if ( empty( $meta_key ) ) {
1108
			return false;
1109
		}
1110
1111
		// Make sure if it needs to be serialized, we do
1112
		$meta_value = maybe_serialize( $meta_value );
1113
1114
		if ( is_numeric( $meta_value ) ) {
1115
			$value_type = is_float( $meta_value ) ? '%f' : '%d';
1116
		} else {
1117
			$value_type = "'%s'";
1118
		}
1119
1120
		$sql = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = $value_type WHERE post_id = $this->ID AND meta_key = '%s'", $meta_value, $meta_key );
1121
1122
		if ( $wpdb->query( $sql ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1123
1124
			clean_post_cache( $this->ID );
1125
1126
			return true;
1127
1128
		}
1129
1130
		return false;
1131
	}
1132
1133
}
1134