Test Failed
Push — feature/meta-tables ( a7dbcd...709186 )
by Ravinder
04:48
created

Give_Donate_Form::increase_sales()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 16
rs 9.4285
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
			$allow_custom_amount = give_get_meta( $this->ID, '_give_custom_amount', true );
533
			$this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_minimum', true );
534
535
			if ( ! give_is_setting_enabled( $allow_custom_amount ) ) {
536
				$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...
537
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
538
539
		}
540
541
		return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID );
542
	}
543
544
	/**
545
	 * Retrieve the variable prices
546
	 *
547
	 * @since  1.0
548
	 * @access public
549
	 *
550
	 * @return array  Variable prices.
551
	 */
552
	public function get_prices() {
553
554
		if ( ! isset( $this->prices ) ) {
555
556
			$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...
557
558
		}
559
560
		/**
561
		 * Override multi-level prices
562
		 *
563
		 * @since 1.0
564
		 *
565
		 * @param array      $prices The array of mulit-level prices.
566
		 * @param int|string $ID     The ID of the form.
567
		 */
568
		return apply_filters( 'give_get_donation_levels', $this->prices, $this->ID );
569
570
	}
571
572
	/**
573
	 * Retrieve the goal
574
	 *
575
	 * @since  1.0
576
	 * @access public
577
	 *
578
	 * @return float  Goal.
579
	 */
580 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...
581
582
		if ( ! isset( $this->goal ) ) {
583
584
			$this->goal = give_get_meta( $this->ID, '_give_set_goal', true );
585
586
			if ( ! $this->goal ) {
587
				$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...
588
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
589
590
		}
591
592
		return apply_filters( 'give_get_set_goal', $this->goal, $this->ID );
593
594
	}
595
596
	/**
597
	 * Determine if single price mode is enabled or disabled
598
	 *
599
	 * @since  1.0
600
	 * @access public
601
	 *
602
	 * @return bool
603
	 */
604 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...
605
606
		$option = give_get_meta( $this->ID, '_give_price_option', true );
607
		$ret    = 0;
608
609
		if ( empty( $option ) || $option === 'set' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
610
			$ret = 1;
611
		}
612
613
		/**
614
		 * Override the price mode for a donation when checking if is in single price mode.
615
		 *
616
		 * @since 1.0
617
		 *
618
		 * @param bool       $ret Is donation form in single price mode?
619
		 * @param int|string $ID The ID of the donation form.
620
		 */
621
		return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID );
622
623
	}
624
625
	/**
626
	 * Determine if custom price mode is enabled or disabled
627
	 *
628
	 * @since  1.6
629
	 * @access public
630
	 *
631
	 * @return bool
632
	 */
633
	public function is_custom_price_mode() {
634
635
		$option = give_get_meta( $this->ID, '_give_custom_amount', true );
636
		$ret    = 0;
637
638
		if ( give_is_setting_enabled( $option ) ) {
639
			$ret = 1;
640
		}
641
642
		/**
643
		 * Override the price mode for a donation when checking if is in custom price mode.
644
		 *
645
		 * @since 1.6
646
		 *
647
		 * @param bool       $ret Is donation form in custom price mode?
648
		 * @param int|string $ID  The ID of the donation form.
649
		 */
650
		return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID );
651
652
	}
653
654
	/**
655
	 * Has Variable Prices
656
	 *
657
	 * Determine if the donation form has variable prices enabled
658
	 *
659
	 * @since  1.0
660
	 * @access public
661
	 *
662
	 * @return bool
663
	 */
664 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...
665
666
		$option = give_get_meta( $this->ID, '_give_price_option', true );
667
		$ret    = 0;
668
669
		if ( $option === 'multi' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
670
			$ret = 1;
671
		}
672
673
		/**
674
		 * Filter: Override whether the donation form has variables prices.
675
		 *
676
		 * @param bool       $ret Does donation form have variable prices?
677
		 * @param int|string $ID  The ID of the donation form.
678
		 */
679
		return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID );
680
681
	}
682
683
	/**
684
	 * Retrieve the donation form type, set or multi-level
685
	 *
686
	 * @since  1.5
687
	 * @access public
688
	 *
689
	 * @return string Type of donation form, either 'set' or 'multi'.
690
	 */
691 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...
692
693
		if ( ! isset( $this->type ) ) {
694
695
			$this->type = give_get_meta( $this->ID, '_give_price_option', true );
696
697
			if ( empty( $this->type ) ) {
698
				$this->type = 'set';
699
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
700
701
		}
702
703
		return apply_filters( 'give_get_form_type', $this->type, $this->ID );
704
705
	}
706
707
	/**
708
	 * Get form tag classes.
709
	 *
710
	 * Provides the classes for the donation <form> html tag and filters for customization.
711
	 *
712
	 * @since  1.6
713
	 * @access public
714
	 *
715
	 * @param  $args
716
	 *
717
	 * @return string
718
	 */
719
	public function get_form_classes( $args ) {
720
721
		$float_labels_option = give_is_float_labels_enabled( $args )
722
			? 'float-labels-enabled'
723
			: '';
724
725
		$form_classes_array = apply_filters( 'give_form_classes', array(
726
			'give-form',
727
			'give-form-' . $this->ID,
728
			'give-form-type-' . $this->get_type(),
729
			$float_labels_option,
730
		), $this->ID, $args );
731
732
		// Remove empty class names.
733
		$form_classes_array = array_filter( $form_classes_array );
734
735
		return implode( ' ', $form_classes_array );
736
737
	}
738
739
	/**
740
	 * Get form wrap Classes.
741
	 *
742
	 * Provides the classes for the donation form div wrapper and filters for customization.
743
	 *
744
	 * @access public
745
	 *
746
	 * @param  $args
747
	 *
748
	 * @return string
749
	 */
750
	public function get_form_wrap_classes( $args ) {
751
752
		$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
753
			? $args['display_style']
754
			: give_get_meta( $this->ID, '_give_payment_display', true );
755
756
		// If admin want to show only button for form then user inbuilt modal functionality.
757
		if( 'button' === $display_option ) {
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...
758
			$display_option = 'modal give-display-button-only';
759
		}
760
761
		$form_wrap_classes_array = apply_filters( 'give_form_wrap_classes', array(
762
			'give-form-wrap',
763
			'give-display-' . $display_option,
764
		), $this->ID, $args );
765
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
766
767
		return implode( ' ', $form_wrap_classes_array );
768
769
	}
770
771
	/**
772
	 * Get if form type set or not.
773
	 *
774
	 * @since  1.6
775
	 * @access public
776
	 *
777
	 * @return bool
778
	 */
779
	public function is_set_type_donation_form() {
780
		$form_type = $this->get_type();
781
782
		return ( 'set' === $form_type ? true : false );
783
	}
784
785
	/**
786
	 * Get if form type multi or not.
787
	 *
788
	 * @since  1.6
789
	 * @access public
790
	 *
791
	 * @return bool True if form type is 'multi' and false otherwise.
792
	 */
793
	public function is_multi_type_donation_form() {
794
		$form_type = $this->get_type();
795
796
		return ( 'multi' === $form_type ? true : false );
797
798
	}
799
800
	/**
801
	 * Retrieve the sale count for the donation form
802
	 *
803
	 * @since  1.0
804
	 * @access public
805
	 *
806
	 * @return int    Donation form sale count.
807
	 */
808 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...
809
810
		if ( ! isset( $this->sales ) ) {
811
812
			if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) {
813
				add_post_meta( $this->ID, '_give_form_sales', 0 );
814
			} // End if
815
816
			$this->sales = give_get_meta( $this->ID, '_give_form_sales', true );
817
818
			if ( $this->sales < 0 ) {
819
				// Never let sales be less than zero
820
				$this->sales = 0;
821
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
822
823
		}
824
825
		return $this->sales;
826
827
	}
828
829
	/**
830
	 * Increment the sale count by one
831
	 *
832
	 * @since  1.0
833
	 * @access public
834
	 *
835
	 * @param  int $quantity The quantity to increase the donations by. Default is 1.
836
	 *
837
	 * @return int|false     New number of total sales.
838
	 */
839
	public function increase_sales( $quantity = 1 ) {
840
841
		$sales       = give_get_form_sales_stats( $this->ID );
842
		$quantity    = absint( $quantity );
843
		$total_sales = $sales + $quantity;
844
845
		if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
846
847
			$this->sales = $total_sales;
848
849
			return $this->sales;
850
851
		}
852
853
		return false;
854
	}
855
856
	/**
857
	 * Decrement the sale count by one
858
	 *
859
	 * @since  1.0
860
	 * @access public
861
	 *
862
	 * @param  int $quantity The quantity to decrease by. Default is 1.
863
	 *
864
	 * @return int|false     New number of total sales.
865
	 */
866
	public function decrease_sales( $quantity = 1 ) {
867
868
		$sales = give_get_form_sales_stats( $this->ID );
869
870
		// Only decrease if not already zero
871
		if ( $sales > 0 ) {
872
873
			$quantity    = absint( $quantity );
874
			$total_sales = $sales - $quantity;
875
876
			if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
877
878
				$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...
879
880
				return $sales;
881
882
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
883
884
		}
885
886
		return false;
887
888
	}
889
890
	/**
891
	 * Retrieve the total earnings for the form
892
	 *
893
	 * @since  1.0
894
	 * @access public
895
	 *
896
	 * @return float  Donation form total earnings.
897
	 */
898 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...
899
900
		if ( ! isset( $this->earnings ) ) {
901
902
			if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) {
903
				add_post_meta( $this->ID, '_give_form_earnings', 0 );
904
			}
905
906
			$this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true );
907
908
			if ( $this->earnings < 0 ) {
909
				// Never let earnings be less than zero
910
				$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...
911
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
912
913
		}
914
915
		return $this->earnings;
916
917
	}
918
919
	/**
920
	 * Increase the earnings by the given amount
921
	 *
922
	 * @since  1.0
923
	 * @access public
924
	 *
925
	 * @param  int $amount Amount of donation. Default is 0.
926
	 *
927
	 * @return float|false
928
	 */
929 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...
930
931
		$earnings   = give_get_form_earnings_stats( $this->ID );
932
		$new_amount = $earnings + (float) $amount;
933
934
		if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
935
936
			$this->earnings = $new_amount;
937
938
			return $this->earnings;
939
940
		}
941
942
		return false;
943
944
	}
945
946
	/**
947
	 * Decrease the earnings by the given amount
948
	 *
949
	 * @since  1.0
950
	 * @access public
951
	 *
952
	 * @param  int $amount Amount of donation.
953
	 *
954
	 * @return float|false
955
	 */
956 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...
957
958
		$earnings = give_get_form_earnings_stats( $this->ID );
959
960
		if ( $earnings > 0 ) {
961
			// Only decrease if greater than zero
962
			$new_amount = $earnings - (float) $amount;
963
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
964
965
			if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
966
967
				$this->earnings = $new_amount;
968
969
				return $this->earnings;
970
971
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
972
973
		}
974
975
		return false;
976
977
	}
978
979
	/**
980
	 * Determine if the donation is free or if the given price ID is free
981
	 *
982
	 * @since  1.0
983
	 * @access public
984
	 *
985
	 * @param  int $price_id Price ID. Default is false.
986
	 *
987
	 * @return bool
988
	 */
989
	public function is_free( $price_id = false ) {
990
991
		$is_free          = false;
992
		$variable_pricing = give_has_variable_prices( $this->ID );
993
994
		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...
995
			$price = give_get_price_option_amount( $this->ID, $price_id );
996
		} elseif ( ! $variable_pricing ) {
997
			$price = give_get_meta( $this->ID, '_give_set_price', true );
998
		}
999
1000
		if ( isset( $price ) && (float) $price == 0 ) {
0 ignored issues
show
introduced by
Found "== 0". Use Yoda Condition checks, you must
Loading history...
1001
			$is_free = true;
1002
		}
1003
1004
		return (bool) apply_filters( 'give_is_free_donation', $is_free, $this->ID, $price_id );
1005
1006
	}
1007
1008
	/**
1009
	 * Determine if donation form closed or not
1010
	 *
1011
	 * Form will be close if:
1012
	 *  a. form has fixed goal
1013
	 *  b. close form when goal achieved cmb2 setting is set to 'Yes'
1014
	 *  c. goal has been achieved
1015
	 *
1016
	 * @since  1.4.5
1017
	 * @access public
1018
	 *
1019
	 * @return bool
1020
	 */
1021
	public function is_close_donation_form() {
1022
1023
		/**
1024
		 * Filter the close form result.
1025
		 *
1026
		 * @since 1.8
1027
		 */
1028
		$is_close_form = apply_filters(
1029
			'give_is_close_donation_form',
1030
			(
1031
			give_is_setting_enabled( give_get_meta( $this->ID, '_give_goal_option', true ) ) )
1032
			&& give_is_setting_enabled( give_get_meta( $this->ID, '_give_close_form_when_goal_achieved', true ) )
1033
			&& ( $this->get_goal() <= $this->get_earnings()
1034
			),
1035
			$this->ID
1036
		);
1037
1038
		return $is_close_form;
1039
	}
1040
1041
	/**
1042
	 * Updates a single meta entry for the donation form
1043
	 *
1044
	 * @since  1.5
1045
	 * @access private
1046
	 *
1047
	 * @param  string              $meta_key   The meta_key to update.
1048
	 * @param  string|array|object $meta_value The value to put into the meta.
1049
	 *
1050
	 * @return bool                            The result of the update query.
1051
	 */
1052
	private function update_meta( $meta_key = '', $meta_value = '' ) {
1053
1054
		/* @var WPDB $wpdb */
1055
		global $wpdb;
1056
1057
		// Bailout.
1058
		if ( empty( $meta_key ) ) {
1059
			return false;
1060
		}
1061
1062
		if ( give_update_meta( $this->ID, $meta_key, $meta_value  ) ) {
0 ignored issues
show
Bug introduced by
It seems like $meta_value defined by parameter $meta_value on line 1052 can also be of type array or object; however, give_update_meta() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
1063
			return true;
1064
		}
1065
1066
		return false;
1067
	}
1068
1069
}
1070