Completed
Pull Request — master (#859)
by Devin
19:40
created

Give_Donate_Form::update_meta()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
c 0
b 0
f 0
nc 7
nop 2
dl 0
loc 30
ccs 0
cts 0
cp 0
crap 30
rs 8.439
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
333
	 */
334
	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...
335
336
		$donation_form = WP_Post::get_instance( $_id );
337
338
		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...
339 2
	}
340
341 2
	/**
342
	 * Given the donation form data, let's set the variables
343 2
	 *
344
	 * @since  1.5
345 2
	 * @access private
346
	 *
347 1
	 * @param  WP_Post $donation_form WP_Post Object for the donation form.
348
	 *
349 1
	 * @return bool                   If the setup was successful or not.
350
	 */
351 2
	private function setup_donation_form( $donation_form ) {
352
353
		if ( ! is_object( $donation_form ) ) {
354
			return false;
355 2
		}
356
357 2
		if ( ! is_a( $donation_form, 'WP_Post' ) ) {
358
			return false;
359
		}
360
361
		if ( 'give_forms' !== $donation_form->post_type ) {
362
			return false;
363
		}
364
365
		foreach ( $donation_form as $key => $value ) {
366
367
			switch ( $key ) {
368
369
				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...
370
					$this->$key = $value;
371
					break;
372
373
			}
374
375
		}
376
377
		return true;
378
379
	}
380
381
	/**
382
	 * Magic __get function to dispatch a call to retrieve a private property
383
	 *
384
	 * @since 1.0
385
	 * @access public
386
	 *
387
	 * @param  string $key
388
	 *
389
	 * @return mixed
390
	 * @throws Exception
391
	 */
392
	public function __get( $key ) {
393
394
		if ( method_exists( $this, 'get_' . $key ) ) {
395
396 53
			return call_user_func( array( $this, 'get_' . $key ) );
397
398 53
		} else {
399 53
400
			/* translators: %s: property key */
401 53
			return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) );
402 36
403 36
		}
404
405
	}
406
407
	/**
408
	 * Creates a donation form
409
	 *
410
	 * @since  1.5
411 53
	 * @access public
412
	 *
413
	 * @param  array $data Array of attributes for a donation form.
414
	 *
415
	 * @return mixed       False if data isn't passed and class not instantiated for creation, or New Form ID.
416
	 */
417
	public function create( $data = array() ) {
418
419
		if ( $this->id != 0 ) {
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Give_Donate_Form>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
420
			return false;
421
		}
422
423
		$defaults = array(
424
			'post_type'   => 'give_forms',
425
			'post_status' => 'draft',
426
			'post_title'  => esc_html__( 'New Donation Form', 'give' )
427
		);
428
429
		$args = wp_parse_args( $data, $defaults );
430
431
		/**
432
		 * Fired before a donation form is created
433
		 *
434
		 * @param array $args The post object arguments used for creation.
435
		 */
436
		do_action( 'give_form_pre_create', $args );
437
438
		$id = wp_insert_post( $args, true );
439
440
		$donation_form = WP_Post::get_instance( $id );
441
442
		/**
443 42
		 * Fired after a donation form is created
444
		 *
445 42
		 * @param int   $id   The post ID of the created item.
446
		 * @param array $args The post object arguments used for creation.
447 42
		 */
448 23
		do_action( 'give_form_post_create', $id, $args );
449 23
450
		return $this->setup_donation_form( $donation_form );
451 42
452
	}
453 42
454
	/**
455
	 * Retrieve the ID
456
	 *
457
	 * @since  1.0
458 42
	 * @access public
459
	 *
460 42
	 * @return int    Donation form ID.
461
	 */
462
	public function get_ID() {
463
		return $this->ID;
464
	}
465
466
	/**
467
	 * Retrieve the donation form name
468
	 *
469
	 * @since  1.5
470
	 * @access public
471
	 *
472
	 * @return string Donation form name.
473 42
	 */
474
	public function get_name() {
475 42
		return get_the_title( $this->ID );
476 42
	}
477 42
478
	/**
479 42
	 * Retrieve the price
480
	 *
481 42
	 * @since  1.0
482
	 * @access public
483 42
	 *
484
	 * @return float  Price.
485
	 */
486
	public function get_price() {
487 1
488
		if ( ! isset( $this->price ) ) {
489
490
			$this->price = get_post_meta( $this->ID, '_give_set_price', true );
491
492
			if ( $this->price ) {
493
494
				$this->price = give_sanitize_amount( $this->price );
495
496
			} else {
497
498
				$this->price = 0;
499 19
500
			}
501 19
502
		}
503
504 19
		/**
505
		 * Override the donation form set price.
506 19
		 *
507 19
		 * @since 1.0
508
		 *
509 19
		 * @param string     $price The donation form price.
510
		 * @param string|int $id    The form ID.
511 5
		 */
512
		return apply_filters( 'give_get_set_price', $this->price, $this->ID );
513 5
	}
514
515
	/**
516
	 * Retrieve the minimum price.
517 15
	 *
518
	 * @since  1.3.6
519 16
	 * @access public
520
	 *
521
	 * @return float  Minimum price.
522
	 */
523
	public function get_minimum_price() {
524
525
		if ( ! isset( $this->minimum_price ) ) {
526
527
			$allow_custom_amount = get_post_meta( $this->ID, '_give_custom_amount', true );
528
			$this->minimum_price = get_post_meta( $this->ID, '_give_custom_amount_minimum', true );
529 43
530
			if ( $allow_custom_amount != 'no' && $this->minimum_price ) {
531 43
532
				$this->minimum_price = give_sanitize_amount( $this->minimum_price );
533 43
534 23
			} else {
535 23
536
				$this->minimum_price = 0;
537 43
538
			}
539 43
540
		}
541
542
		return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID );
543
	}
544 43
545
	/**
546 43
	 * Retrieve the variable prices
547
	 *
548
	 * @since  1.0
549
	 * @access public
550
	 *
551
	 * @return array  Variable prices.
552
	 */
553
	public function get_prices() {
554
555
		if ( ! isset( $this->prices ) ) {
556 42
557
			$this->prices = get_post_meta( $this->ID, '_give_donation_levels', true );
558 42
559 42
		}
560
561 42
		/**
562
		 * Override multi-level prices
563 42
		 *
564
		 * @since 1.0
565 42
		 *
566
		 * @param array $prices The array of mulit-level prices.
567
		 * @param int|string The ID of the form.
568
		 */
569 1
		return apply_filters( 'give_get_donation_levels', $this->prices, $this->ID );
570
571
	}
572
573
	/**
574
	 * Retrieve the goal
575
	 *
576
	 * @since  1.0
577
	 * @access public
578
	 *
579 19
	 * @return float  Goal.
580
	 */
581 19
	public function get_goal() {
582
583 19
		if ( ! isset( $this->goal ) ) {
584
585 19
			$this->goal = get_post_meta( $this->ID, '_give_set_goal', true );
586
587
			if ( $this->goal ) {
588 19
589
				$this->goal = give_sanitize_amount( $this->goal );
590 5
591
			} else {
592 5
593
				$this->goal = 0;
594
595
			}
596 15
597
		}
598 16
599
		return apply_filters( 'give_get_set_goal', $this->goal, $this->ID );
600
601
	}
602
603
	/**
604
	 * Determine if single price mode is enabled or disabled
605
	 *
606
	 * @since  1.0
607
	 * @access public
608
	 *
609
	 * @return bool
610
	 */
611
	public function is_single_price_mode() {
612
613
		$option = get_post_meta( $this->ID, '_give_price_options_mode', true );
614
		$ret    = 0;
615
616
		if ( empty( $option ) || $option === 'set' ) {
617
			$ret = 1;
618
		}
619
620
		/**
621
		 * Override the price mode for a donation when checking if is in single price mode.
622
		 *
623
		 * @since 1.0
624
		 *
625
		 * @param bool $ret Is donation form in single price mode?
626
		 * @param int|string The ID of the donation form.
627
		 */
628
		return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID );
629
630
	}
631
632
	/**
633
	 * Determine if custom price mode is enabled or disabled
634
	 *
635
	 * @since  1.6
636
	 * @access public
637
	 *
638 1
	 * @return bool
639
	 */
640 1
	public function is_custom_price_mode() {
641 1
642 1
		$option = get_post_meta( $this->ID, '_give_custom_amount', true );
643 1
		$ret    = 0;
644
645
		if ( $option === 'yes' ) {
646
			$ret = 1;
647
		}
648
649
		/**
650
		 * Override the price mode for a donation when checking if is in custom price mode.
651
		 *
652
		 * @since 1.6
653
		 *
654
		 * @param bool $ret Is donation form in custom price mode?
655
		 * @param int|string The ID of the donation form.
656
		 */
657
		return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID );
658 42
659
	}
660 42
661
	/**
662 42
	 * Has Variable Prices
663 1
	 *
664
	 * Determine if the donation form has variable prices enabled
665
	 *
666
	 * @since  1.0
667 42
	 * @access public
668
	 *
669 42
	 * @return bool
670 42
	 */
671 42
	public function has_variable_prices() {
672
673
		$option = get_post_meta( $this->ID, '_give_price_option', true );
674
		$ret    = 0;
675 42
676
		if ( $option === 'multi' ) {
677 42
			$ret = 1;
678
		}
679 42
680
		/**
681 42
		 * Filter: Override whether the donation form has variables prices.
682
		 *
683
		 * @param bool $ret Does donation form have variable prices?
684
		 * @param int|string The ID of the donation form.
685 16
		 */
686
		return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID );
687
688
	}
689
690
	/**
691
	 * Retrieve the donation form type, set or multi-level
692
	 *
693
	 * @since  1.5
694
	 * @access public
695
	 *
696
	 * @return string Type of donation form, either 'set' or 'multi'.
697
	 */
698
	public function get_type() {
699
700
		if ( ! isset( $this->type ) ) {
701
702
			$this->type = get_post_meta( $this->ID, '_give_price_option', true );
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
703
704
			if ( empty( $this->type ) ) {
705
				$this->type = 'set';
706
			}
707
708
		}
709
710
		return apply_filters( 'give_get_form_type', $this->type, $this->ID );
711
712
	}
713
714
	/**
715
	 * Get form tag classes.
716
	 *
717
	 * Provides the classes for the donation <form> html tag and filters for customization.
718
	 *
719
	 * @since  1.6
720
	 * @access public
721
	 *
722
	 * @param  $args
723
	 *
724
	 * @return string
725
	 */
726
	public function get_form_classes( $args ) {
727
728
		$float_labels_option = give_is_float_labels_enabled( $args )
729
			? 'float-labels-enabled'
730
			: '';
731
732
		$form_classes_array = apply_filters( 'give_form_classes', array(
733
			'give-form',
734
			'give-form-' . $this->ID,
735
			'give-form-type-' . $this->get_type(),
736
			$float_labels_option
737
		), $this->ID, $args );
738
739
		// Remove empty class names.
740
		$form_classes_array = array_filter( $form_classes_array );
741
742
		return implode( ' ', $form_classes_array );
743
744
	}
745
746
	/**
747
	 * Get form wrap Classes.
748
	 *
749
	 * Provides the classes for the donation form div wrapper and filters for customization.
750
	 *
751
	 * @access public
752
	 *
753
	 * @param  $args
754
	 *
755
	 * @return string
756
	 */
757
	public function get_form_wrap_classes( $args ) {
758
759
		$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
760
			? $args['display_style']
761
			: get_post_meta( $this->ID, '_give_payment_display', true );
762
763
		$form_wrap_classes_array = apply_filters( 'give_form_wrap_classes', array(
764
			'give-form-wrap',
765
			'give-display-' . $display_option
766
		), $this->ID, $args );
767
768
769
		return implode( ' ', $form_wrap_classes_array );
770
771
	}
772
773
    /**
774
     * Get if form type set or not.
775
     *
776
     * @since  1.6
777
	 * @access public
778
     *
779
	 * @return bool
780
	 *
781
	public function is_set_type_donation_form() {
782
		$form_type = $this->get_type();
783
784
		return ( 'set' === $form_type ? true : false );
785
786
	}
787
788
	/**
789
	 * Get if form type multi or not.
790
	 *
791
	 * @since  1.6
792
	 * @access public
793
	 *
794
	 * @return bool True if form type is 'multi' and false otherwise.
795
	 */
796
	public function is_multi_type_donation_form() {
797
		$form_type = $this->get_type();
798
799
		return ( 'multi' === $form_type ? true : false );
800
801
	}
802
803
	/**
804
	 * Retrieve the sale count for the donation form
805
	 *
806
	 * @since  1.0
807
	 * @access public
808
	 *
809
	 * @return int    Donation form sale count.
810
	 */
811
	public function get_sales() {
812
813
		if ( ! isset( $this->sales ) ) {
814
815
			if ( '' == get_post_meta( $this->ID, '_give_form_sales', true ) ) {
816
				add_post_meta( $this->ID, '_give_form_sales', 0 );
817
			} // End if
818
819
			$this->sales = get_post_meta( $this->ID, '_give_form_sales', true );
820
821
			if ( $this->sales < 0 ) {
822
				// Never let sales be less than zero
823
				$this->sales = 0;
824
			}
825
826
		}
827
828
		return $this->sales;
829
830
	}
831
832
	/**
833
	 * Increment the sale count by one
834
	 *
835
	 * @since  1.0
836
	 * @access public
837
	 *
838
	 * @param  int $quantity The quantity to increase the donations by. Default is 1.
839
	 *
840
	 * @return int|false     New number of total sales.
841
	 */
842
	public function increase_sales( $quantity = 1 ) {
843
844
		$sales       = give_get_form_sales_stats( $this->ID );
845
		$quantity    = absint( $quantity );
846
		$total_sales = $sales + $quantity;
847
848
		if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
849
850
			$this->sales = $total_sales;
851
852
			return $this->sales;
853
854
		}
855
856
		return false;
857
	}
858
859
	/**
860
	 * Decrement the sale count by one
861
	 *
862
	 * @since  1.0
863
	 * @access public
864
	 *
865
	 * @param  int $quantity The quantity to decrease by. Default is 1.
866
	 *
867
	 * @return int|false     New number of total sales.
868
	 */
869
	public function decrease_sales( $quantity = 1 ) {
870
871
		$sales = give_get_form_sales_stats( $this->ID );
872
873
		// Only decrease if not already zero
874
		if ( $sales > 0 ) {
875
876
			$quantity    = absint( $quantity );
877
			$total_sales = $sales - $quantity;
878
879
			if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
880
881
				$this->sales = $sales;
882
883
				return $sales;
884
885
			}
886
887
		}
888
889
		return false;
890
891
	}
892
893
	/**
894
	 * Retrieve the total earnings for the form
895
	 *
896
	 * @since  1.0
897
	 * @access public
898
	 *
899
	 * @return float  Donation form total earnings.
900
	 */
901
	public function get_earnings() {
902
903
		if ( ! isset( $this->earnings ) ) {
904
905
			if ( '' == get_post_meta( $this->ID, '_give_form_earnings', true ) ) {
906
				add_post_meta( $this->ID, '_give_form_earnings', 0 );
907
			}
908
909
			$this->earnings = get_post_meta( $this->ID, '_give_form_earnings', true );
910
911
			if ( $this->earnings < 0 ) {
912
				// Never let earnings be less than zero
913
				$this->earnings = 0;
914
			}
915
916
		}
917
918
		return $this->earnings;
919
920
	}
921
922
	/**
923
	 * Increase the earnings by the given amount
924
	 *
925
	 * @since  1.0
926
	 * @access public
927
	 *
928
	 * @param  int    $amount Amount of donation. Default is 0.
929
	 *
930
	 * @return float|false
931
	 */
932
	public function increase_earnings( $amount = 0 ) {
933
934
		$earnings   = give_get_form_earnings_stats( $this->ID );
935
		$new_amount = $earnings + (float) $amount;
936
937
		if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
938
939
			$this->earnings = $new_amount;
940
941
			return $this->earnings;
942
943
		}
944
945
		return false;
946
947
	}
948
949
	/**
950
	 * Decrease the earnings by the given amount
951
	 *
952
	 * @since  1.0
953
	 * @access public
954
	 *
955
	 * @param  int    $amount Amount of donation.
956
	 * 
957
	 * @return float|false
958
	 */
959
	public function decrease_earnings( $amount ) {
960
961
		$earnings = give_get_form_earnings_stats( $this->ID );
962
963
		if ( $earnings > 0 ) {
964
			// Only decrease if greater than zero
965
			$new_amount = $earnings - (float) $amount;
966
967
968
			if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
969
970
				$this->earnings = $new_amount;
971
972
				return $this->earnings;
973
974
			}
975
976
		}
977
978
		return false;
979
980
	}
981
982
	/**
983
	 * Determine if the donation is free or if the given price ID is free
984
	 *
985
	 * @since  1.0
986
	 * @access public
987
	 *
988
	 * @param  int    $price_id Price ID. Default is false.
989
	 *
990
	 * @return bool
991
	 */
992
	public function is_free( $price_id = false ) {
993
994
		$is_free          = false;
995
		$variable_pricing = give_has_variable_prices( $this->ID );
996
997
		if ( $variable_pricing && ! is_null( $price_id ) && $price_id !== false ) {
998
			$price = give_get_price_option_amount( $this->ID, $price_id );
999
		} elseif ( ! $variable_pricing ) {
1000
			$price = get_post_meta( $this->ID, '_give_set_price', true );
1001
		}
1002
1003
		if ( isset( $price ) && (float) $price == 0 ) {
1004
			$is_free = true;
1005
		}
1006
1007
		return (bool) apply_filters( 'give_is_free_donation', $is_free, $this->ID, $price_id );
1008
1009
	}
1010
1011
	/**
1012
	 * Determine if donation form closed or not
1013
	 *
1014
	 * Form will be close if:
1015
	 *  a. form has fixed goal
1016
	 *  b. close form when goal achieved cmb2 setting is set to 'Yes'
1017
	 *  c. goal has been achieved
1018
	 *
1019
	 * @since  1.4.5
1020
	 * @access public
1021
	 *
1022
	 * @return bool
1023
	 */
1024
	public function is_close_donation_form() {
1025
		return (
1026
			       'yes' === get_post_meta( $this->ID, '_give_goal_option', true ) )
1027
		       && ( 'yes' === get_post_meta( $this->ID, '_give_close_form_when_goal_achieved', true ) )
1028
		       && ( $this->get_goal() <= $this->get_earnings()
1029
		       );
1030
	}
1031
1032
	/**
1033
	 * Updates a single meta entry for the donation form
1034
	 *
1035
	 * @since  1.5
1036
	 * @access private
1037
	 *
1038
	 * @param  string              $meta_key   The meta_key to update.
1039
	 * @param  string|array|object $meta_value The value to put into the meta.
1040
	 *
1041
	 * @return bool                            The result of the update query.
1042
	 */
1043
	private function update_meta( $meta_key = '', $meta_value = '' ) {
1044
1045
		/* @var WPDB $wpdb */
1046
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
1047
1048
		if ( empty( $meta_key ) ) {
1049
			return false;
1050
		}
1051
1052
		// Make sure if it needs to be serialized, we do
1053
		$meta_value = maybe_serialize( $meta_value );
1054
1055
		if ( is_numeric( $meta_value ) ) {
1056
			$value_type = is_float( $meta_value ) ? '%f' : '%d';
1057
		} else {
1058
			$value_type = "'%s'";
1059
		}
1060
1061
		$sql = $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = $value_type WHERE post_id = $this->ID AND meta_key = '%s'", $meta_value, $meta_key );
1062
1063
		if ( $wpdb->query( $sql ) ) {
1064
1065
			clean_post_cache( $this->ID );
1066
1067
			return true;
1068
1069
		}
1070
1071
		return false;
1072
	}
1073
1074
}
1075