PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.2
GiveWP – Donation Plugin and Fundraising Platform v2.0.2
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / class-give-donate-form.php
give / includes Last commit date
admin 8 years ago api 8 years ago deprecated 8 years ago donors 8 years ago emails 8 years ago forms 8 years ago gateways 8 years ago libraries 8 years ago payments 8 years ago actions.php 8 years ago ajax-functions.php 8 years ago class-give-async-process.php 8 years ago class-give-background-updater.php 8 years ago class-give-cache.php 8 years ago class-give-cli-commands.php 8 years ago class-give-cron.php 8 years ago class-give-db-donor-meta.php 8 years ago class-give-db-donors.php 8 years ago class-give-db-form-meta.php 8 years ago class-give-db-logs-meta.php 8 years ago class-give-db-logs.php 8 years ago class-give-db-meta.php 8 years ago class-give-db-payment-meta.php 8 years ago class-give-db.php 8 years ago class-give-donate-form.php 8 years ago class-give-donor.php 8 years ago class-give-email-access.php 8 years ago class-give-gravatars.php 8 years ago class-give-html-elements.php 8 years ago class-give-license-handler.php 8 years ago class-give-logging.php 8 years ago class-give-roles.php 8 years ago class-give-session.php 8 years ago class-give-stats.php 8 years ago class-give-template-loader.php 9 years ago class-give-tooltips.php 8 years ago class-give-translation.php 8 years ago class-notices.php 8 years ago country-functions.php 8 years ago currency-functions.php 8 years ago error-tracking.php 8 years ago filters.php 8 years ago formatting.php 8 years ago import-functions.php 8 years ago install.php 8 years ago login-register.php 8 years ago misc-functions.php 8 years ago plugin-compatibility.php 8 years ago post-types.php 8 years ago price-functions.php 8 years ago process-donation.php 8 years ago scripts.php 8 years ago shortcodes.php 8 years ago template-functions.php 8 years ago user-functions.php 8 years ago
class-give-donate-form.php
1126 lines
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 int|bool $_id Post id. Default is false.
338 * @param array $_args Arguments passed.
339 */
340 public function __construct( $_id = false, $_args = array() ) {
341
342 $donation_form = WP_Post::get_instance( $_id );
343
344 return $this->setup_donation_form( $donation_form );
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:
376 $this->$key = $value;
377 break;
378
379 }
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 public function __get( $key ) {
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 ) {
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() {
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 public function get_price() {
492
493 if ( ! isset( $this->price ) ) {
494
495 $this->price = give_maybe_sanitize_amount(
496 give_get_meta(
497 $this->ID,
498 '_give_set_price',
499 true
500 )
501 );
502
503 if ( ! $this->price ) {
504 $this->price = 0;
505 }
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;
536 }
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 );
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 if ( 'donation' === give_get_form_goal_format( $this->ID ) ) {
584 $this->goal = give_get_meta( $this->ID, '_give_number_of_donation_goal', true );
585 } else {
586 $this->goal = give_get_meta( $this->ID, '_give_set_goal', true );
587 }
588
589 if ( ! $this->goal ) {
590 $this->goal = 0;
591 }
592
593 }
594
595 return apply_filters( 'give_get_set_goal', $this->goal, $this->ID );
596
597 }
598
599 /**
600 * Determine if single price mode is enabled or disabled
601 *
602 * @since 1.0
603 * @access public
604 *
605 * @return bool
606 */
607 public function is_single_price_mode() {
608
609 $option = give_get_meta( $this->ID, '_give_price_option', true );
610 $ret = 0;
611
612 if ( empty( $option ) || $option === 'set' ) {
613 $ret = 1;
614 }
615
616 /**
617 * Override the price mode for a donation when checking if is in single price mode.
618 *
619 * @since 1.0
620 *
621 * @param bool $ret Is donation form in single price mode?
622 * @param int|string $ID The ID of the donation form.
623 */
624 return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID );
625
626 }
627
628 /**
629 * Determine if custom price mode is enabled or disabled
630 *
631 * @since 1.6
632 * @access public
633 *
634 * @return bool
635 */
636 public function is_custom_price_mode() {
637
638 $option = give_get_meta( $this->ID, '_give_custom_amount', true );
639 $ret = 0;
640
641 if ( give_is_setting_enabled( $option ) ) {
642 $ret = 1;
643 }
644
645 /**
646 * Override the price mode for a donation when checking if is in custom price mode.
647 *
648 * @since 1.6
649 *
650 * @param bool $ret Is donation form in custom price mode?
651 * @param int|string $ID The ID of the donation form.
652 */
653 return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID );
654
655 }
656
657 /**
658 * Determine if custom price mode is enabled or disabled
659 *
660 * @since 1.8.18
661 * @access public
662 *
663 * @param string|float $amount Donation Amount.
664 *
665 * @return bool
666 */
667 public function is_custom_price( $amount ) {
668 $result = false;
669 $amount = give_maybe_sanitize_amount( $amount );
670
671 if ( $this->is_custom_price_mode() ) {
672
673 if ( 'set' === $this->get_type() ) {
674 if ( $amount !== $this->get_price() ) {
675 $result = true;
676 }
677
678 } elseif ( 'multi' === $this->get_type() ) {
679 $level_amounts = array_map( 'give_maybe_sanitize_amount', wp_list_pluck( $this->get_prices(), '_give_amount' ) );
680 $result = ! in_array( $amount, $level_amounts );
681 }
682 }
683
684 /**
685 * Filter to reset whether it is custom price or not.
686 *
687 * @param bool $result True/False.
688 * @param string|float $amount Donation Amount.
689 * @param int $this->ID Form ID.
690 *
691 * @since 1.8.18
692 */
693 return (bool) apply_filters( 'give_is_custom_price', $result, $amount, $this->ID );
694 }
695
696 /**
697 * Has Variable Prices
698 *
699 * Determine if the donation form has variable prices enabled
700 *
701 * @since 1.0
702 * @access public
703 *
704 * @return bool
705 */
706 public function has_variable_prices() {
707
708 $option = give_get_meta( $this->ID, '_give_price_option', true );
709 $ret = 0;
710
711 if ( $option === 'multi' ) {
712 $ret = 1;
713 }
714
715 /**
716 * Filter: Override whether the donation form has variables prices.
717 *
718 * @param bool $ret Does donation form have variable prices?
719 * @param int|string $ID The ID of the donation form.
720 */
721 return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID );
722
723 }
724
725 /**
726 * Retrieve the donation form type, set or multi-level
727 *
728 * @since 1.5
729 * @access public
730 *
731 * @return string Type of donation form, either 'set' or 'multi'.
732 */
733 public function get_type() {
734
735 if ( ! isset( $this->type ) ) {
736
737 $this->type = give_get_meta( $this->ID, '_give_price_option', true );
738
739 if ( empty( $this->type ) ) {
740 $this->type = 'set';
741 }
742
743 }
744
745 return apply_filters( 'give_get_form_type', $this->type, $this->ID );
746
747 }
748
749 /**
750 * Get form tag classes.
751 *
752 * Provides the classes for the donation <form> html tag and filters for customization.
753 *
754 * @since 1.6
755 * @access public
756 *
757 * @param $args
758 *
759 * @return string
760 */
761 public function get_form_classes( $args ) {
762
763 $float_labels_option = give_is_float_labels_enabled( $args )
764 ? 'float-labels-enabled'
765 : '';
766
767 $form_classes_array = apply_filters( 'give_form_classes', array(
768 'give-form',
769 'give-form-' . $this->ID,
770 'give-form-type-' . $this->get_type(),
771 $float_labels_option,
772 ), $this->ID, $args );
773
774 // Remove empty class names.
775 $form_classes_array = array_filter( $form_classes_array );
776
777 return implode( ' ', $form_classes_array );
778
779 }
780
781 /**
782 * Get form wrap Classes.
783 *
784 * Provides the classes for the donation form div wrapper and filters for customization.
785 *
786 * @access public
787 *
788 * @param $args
789 *
790 * @return string
791 */
792 public function get_form_wrap_classes( $args ) {
793 $custom_class = array(
794 'give-form-wrap',
795 );
796
797 if ( $this->is_close_donation_form() ) {
798 $custom_class[] = 'give-form-closed';
799 } else{
800 $display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
801 ? $args['display_style']
802 : give_get_meta( $this->ID, '_give_payment_display', true );
803
804 $custom_class[] = "give-display-{$display_option}";
805
806 // If admin want to show only button for form then user inbuilt modal functionality.
807 if ( 'button' === $display_option ) {
808 $custom_class[] = 'give-display-button-only';
809 }
810 }
811
812
813 /**
814 * Filter the donation form classes.
815 *
816 * @since 1.0
817 */
818 $form_wrap_classes_array = (array) apply_filters( 'give_form_wrap_classes', $custom_class, $this->ID, $args );
819
820
821 return implode( ' ', $form_wrap_classes_array );
822
823 }
824
825 /**
826 * Get if form type set or not.
827 *
828 * @since 1.6
829 * @access public
830 *
831 * @return bool
832 */
833 public function is_set_type_donation_form() {
834 $form_type = $this->get_type();
835
836 return ( 'set' === $form_type ? true : false );
837 }
838
839 /**
840 * Get if form type multi or not.
841 *
842 * @since 1.6
843 * @access public
844 *
845 * @return bool True if form type is 'multi' and false otherwise.
846 */
847 public function is_multi_type_donation_form() {
848 $form_type = $this->get_type();
849
850 return ( 'multi' === $form_type ? true : false );
851
852 }
853
854 /**
855 * Retrieve the sale count for the donation form
856 *
857 * @since 1.0
858 * @access public
859 *
860 * @return int Donation form sale count.
861 */
862 public function get_sales() {
863
864 if ( ! isset( $this->sales ) ) {
865
866 if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) {
867 add_post_meta( $this->ID, '_give_form_sales', 0 );
868 } // End if
869
870 $this->sales = give_get_meta( $this->ID, '_give_form_sales', true );
871
872 if ( $this->sales < 0 ) {
873 // Never let sales be less than zero.
874 $this->sales = 0;
875 }
876
877 }
878
879 return $this->sales;
880
881 }
882
883 /**
884 * Increment the sale count by one
885 *
886 * @since 1.0
887 * @access public
888 *
889 * @param int $quantity The quantity to increase the donations by. Default is 1.
890 *
891 * @return int|false New number of total sales.
892 */
893 public function increase_sales( $quantity = 1 ) {
894
895 $sales = give_get_form_sales_stats( $this->ID );
896 $quantity = absint( $quantity );
897 $total_sales = $sales + $quantity;
898
899 if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
900
901 $this->sales = $total_sales;
902
903 return $this->sales;
904
905 }
906
907 return false;
908 }
909
910 /**
911 * Decrement the sale count by one
912 *
913 * @since 1.0
914 * @access public
915 *
916 * @param int $quantity The quantity to decrease by. Default is 1.
917 *
918 * @return int|false New number of total sales.
919 */
920 public function decrease_sales( $quantity = 1 ) {
921
922 $sales = give_get_form_sales_stats( $this->ID );
923
924 // Only decrease if not already zero
925 if ( $sales > 0 ) {
926
927 $quantity = absint( $quantity );
928 $total_sales = $sales - $quantity;
929
930 if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
931
932 $this->sales = $sales;
933
934 return $sales;
935
936 }
937
938 }
939
940 return false;
941
942 }
943
944 /**
945 * Retrieve the total earnings for the form
946 *
947 * @since 1.0
948 * @access public
949 *
950 * @return float Donation form total earnings.
951 */
952 public function get_earnings() {
953
954 if ( ! isset( $this->earnings ) ) {
955
956 if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) {
957 add_post_meta( $this->ID, '_give_form_earnings', 0 );
958 }
959
960 $this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true );
961
962 if ( $this->earnings < 0 ) {
963 // Never let earnings be less than zero
964 $this->earnings = 0;
965 }
966
967 }
968
969 return $this->earnings;
970
971 }
972
973 /**
974 * Increase the earnings by the given amount
975 *
976 * @since 1.0
977 * @access public
978 *
979 * @param int $amount Amount of donation. Default is 0.
980 *
981 * @return float|false
982 */
983 public function increase_earnings( $amount = 0 ) {
984
985 $earnings = give_get_form_earnings_stats( $this->ID );
986 $new_amount = $earnings + (float) $amount;
987
988 if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
989
990 $this->earnings = $new_amount;
991
992 return $this->earnings;
993
994 }
995
996 return false;
997
998 }
999
1000 /**
1001 * Decrease the earnings by the given amount
1002 *
1003 * @since 1.0
1004 * @access public
1005 *
1006 * @param int $amount Amount of donation.
1007 *
1008 * @return float|false
1009 */
1010 public function decrease_earnings( $amount ) {
1011
1012 $earnings = give_get_form_earnings_stats( $this->ID );
1013
1014 if ( $earnings > 0 ) {
1015 // Only decrease if greater than zero
1016 $new_amount = $earnings - (float) $amount;
1017
1018
1019 if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1020
1021 $this->earnings = $new_amount;
1022
1023 return $this->earnings;
1024
1025 }
1026
1027 }
1028
1029 return false;
1030
1031 }
1032
1033 /**
1034 * Determine if the donation is free or if the given price ID is free
1035 *
1036 * @since 1.0
1037 * @access public
1038 *
1039 * @param int $price_id Price ID. Default is false.
1040 *
1041 * @return bool
1042 */
1043 public function is_free( $price_id = false ) {
1044
1045 $is_free = false;
1046 $variable_pricing = give_has_variable_prices( $this->ID );
1047
1048 if ( $variable_pricing && ! is_null( $price_id ) && $price_id !== false ) {
1049 $price = give_get_price_option_amount( $this->ID, $price_id );
1050 } elseif ( ! $variable_pricing ) {
1051 $price = give_get_meta( $this->ID, '_give_set_price', true );
1052 }
1053
1054 if ( isset( $price ) && (float) $price == 0 ) {
1055 $is_free = true;
1056 }
1057
1058 return (bool) apply_filters( 'give_is_free_donation', $is_free, $this->ID, $price_id );
1059
1060 }
1061
1062 /**
1063 * Determine if donation form closed or not
1064 *
1065 * Form will be close if:
1066 * a. form has fixed goal
1067 * b. close form when goal achieved cmb2 setting is set to 'Yes'
1068 * c. goal has been achieved
1069 *
1070 * @since 1.4.5
1071 * @access public
1072 *
1073 * @return bool
1074 */
1075 public function is_close_donation_form() {
1076
1077 $goal_format = give_get_form_goal_format( $this->ID );
1078
1079 /**
1080 * Filter the close form result.
1081 *
1082 * @since 1.8
1083 */
1084 $is_close_form = apply_filters(
1085 'give_is_close_donation_form',
1086 (
1087 give_is_setting_enabled( give_get_meta( $this->ID, '_give_goal_option', true ) ) &&
1088 give_is_setting_enabled( give_get_meta( $this->ID, '_give_close_form_when_goal_achieved', true ) ) &&
1089 ( 'donation' === $goal_format ? $this->get_goal() <= $this->get_sales() : $this->get_goal() <= $this->get_earnings() )
1090 ),
1091 $this->ID
1092 );
1093
1094 return $is_close_form;
1095 }
1096
1097 /**
1098 * Updates a single meta entry for the donation form
1099 *
1100 * @since 1.5
1101 * @access private
1102 *
1103 * @param string $meta_key The meta_key to update.
1104 * @param string|array|object $meta_value The value to put into the meta.
1105 *
1106 * @return bool The result of the update query.
1107 */
1108 private function update_meta( $meta_key = '', $meta_value = '' ) {
1109
1110 /* @var WPDB $wpdb */
1111 global $wpdb;
1112
1113 // Bailout.
1114 if ( empty( $meta_key ) ) {
1115 return false;
1116 }
1117
1118 if ( give_update_meta( $this->ID, $meta_key, $meta_value ) ) {
1119 return true;
1120 }
1121
1122 return false;
1123 }
1124
1125 }
1126