PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.10.4
GiveWP – Donation Plugin and Fundraising Platform v2.10.4
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 5 years ago api 5 years ago database 5 years ago deprecated 5 years ago donors 5 years ago emails 6 years ago forms 5 years ago frontend 6 years ago gateways 5 years ago libraries 6 years ago payments 5 years ago actions.php 5 years ago ajax-functions.php 5 years ago class-give-async-process.php 6 years ago class-give-background-updater.php 6 years ago class-give-cache-setting.php 5 years ago class-give-cache.php 6 years ago class-give-cli-commands.php 6 years ago class-give-comment.php 6 years ago class-give-cron.php 6 years ago class-give-donate-form.php 6 years ago class-give-donor.php 5 years ago class-give-email-access.php 5 years ago class-give-license-handler.php 5 years ago class-give-logging.php 5 years ago class-give-readme-parser.php 6 years ago class-give-roles.php 6 years ago class-give-scripts.php 5 years ago class-give-session.php 5 years ago class-give-stats.php 6 years ago class-give-template-loader.php 6 years ago class-give-tooltips.php 6 years ago class-give-translation.php 6 years ago class-notices.php 5 years ago country-functions.php 5 years ago currencies-list.php 6 years ago currency-functions.php 5 years ago error-tracking.php 6 years ago filters.php 5 years ago formatting.php 6 years ago install.php 5 years ago login-register.php 6 years ago misc-functions.php 5 years ago plugin-compatibility.php 6 years ago post-types.php 5 years ago price-functions.php 6 years ago process-donation.php 5 years ago setting-functions.php 6 years ago shortcodes.php 6 years ago template-functions.php 5 years ago user-functions.php 5 years ago
class-give-donate-form.php
1239 lines
1 <?php
2 /**
3 * Donate Form
4 *
5 * @package Give
6 * @subpackage Classes/Give_Donate_Form
7 * @copyright Copyright (c) 2015, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Give_Donate_Form Class.
19 *
20 * This class handles donation forms.
21 *
22 * @since 1.0
23 *
24 * @property $price
25 * @property $minimum_price
26 * @property $maximum_price
27 * @property $prices
28 * @property $goal
29 * @property $sales
30 * @property $earnings
31 * @property $post_type
32 */
33 class Give_Donate_Form {
34
35 /**
36 * The donation ID.
37 *
38 * @since 1.0
39 * @access public
40 *
41 * @var int
42 */
43 public $ID = 0;
44
45 /**
46 * The donation price.
47 *
48 * @since 1.0
49 * @access private
50 *
51 * @var float
52 */
53 private $price;
54
55 /**
56 * The minimum donation price.
57 *
58 * @since 1.3.6
59 * @access private
60 *
61 * @var float
62 */
63 private $minimum_price;
64
65 /**
66 * The maximum donation price.
67 *
68 * @since 2.0
69 * @access private
70 *
71 * @var float
72 */
73 private $maximum_price;
74
75 /**
76 * The donation prices, if Price Levels are enabled.
77 *
78 * @since 1.0
79 * @access private
80 *
81 * @var array
82 */
83 private $prices;
84
85 /**
86 * The donation goal.
87 *
88 * @since 1.0
89 * @access private
90 *
91 * @var float
92 */
93 private $goal;
94
95 /**
96 * The form's sale count.
97 *
98 * @since 1.0
99 * @access private
100 *
101 * @var int
102 */
103 private $sales;
104
105 /**
106 * The form's total earnings
107 *
108 * @since 1.0
109 * @access private
110 *
111 * @var float
112 */
113 private $earnings;
114
115 /**
116 * Declare the default properties in WP_Post as we can't extend it
117 * Anything we've declared above has been removed.
118 */
119
120 /**
121 * The post author
122 *
123 * @since 1.0
124 * @access public
125 *
126 * @var int
127 */
128 public $post_author = 0;
129
130 /**
131 * The post date
132 *
133 * @since 1.0
134 * @access public
135 *
136 * @var string
137 */
138 public $post_date = '0000-00-00 00:00:00';
139
140 /**
141 * The post GTM date
142 *
143 * @since 1.0
144 * @access public
145 *
146 * @var string
147 */
148 public $post_date_gmt = '0000-00-00 00:00:00';
149
150 /**
151 * The post content
152 *
153 * @since 1.0
154 * @access public
155 *
156 * @var string
157 */
158 public $post_content = '';
159
160 /**
161 * The post title
162 *
163 * @since 1.0
164 * @access public
165 *
166 * @var string
167 */
168 public $post_title = '';
169
170 /**
171 * The post excerpt
172 *
173 * @since 1.0
174 * @access public
175 *
176 * @var string
177 */
178 public $post_excerpt = '';
179
180 /**
181 * The post status
182 *
183 * @since 1.0
184 * @access public
185 *
186 * @var string
187 */
188 public $post_status = 'publish';
189
190 /**
191 * The comment status
192 *
193 * @since 1.0
194 * @access public
195 *
196 * @var string
197 */
198 public $comment_status = 'open';
199
200 /**
201 * The ping status
202 *
203 * @since 1.0
204 * @access public
205 *
206 * @var string
207 */
208 public $ping_status = 'open';
209
210 /**
211 * The post password
212 *
213 * @since 1.0
214 * @access public
215 *
216 * @var string
217 */
218 public $post_password = '';
219
220 /**
221 * The post name
222 *
223 * @since 1.0
224 * @access public
225 *
226 * @var string
227 */
228 public $post_name = '';
229
230 /**
231 * Ping
232 *
233 * @since 1.0
234 * @access public
235 *
236 * @var string
237 */
238 public $to_ping = '';
239
240 /**
241 * Pinged
242 *
243 * @since 1.0
244 * @access public
245 *
246 * @var string
247 */
248 public $pinged = '';
249
250 /**
251 * The post modified date
252 *
253 * @since 1.0
254 * @access public
255 *
256 * @var string
257 */
258 public $post_modified = '0000-00-00 00:00:00';
259
260 /**
261 * The post modified GTM date
262 *
263 * @since 1.0
264 * @access public
265 *
266 * @var string
267 */
268 public $post_modified_gmt = '0000-00-00 00:00:00';
269
270 /**
271 * The post filtered content
272 *
273 * @since 1.0
274 * @access public
275 *
276 * @var string
277 */
278 public $post_content_filtered = '';
279
280 /**
281 * The post parent
282 *
283 * @since 1.0
284 * @access public
285 *
286 * @var int
287 */
288 public $post_parent = 0;
289
290 /**
291 * The post GUID
292 *
293 * @since 1.0
294 * @access public
295 *
296 * @var string
297 */
298 public $guid = '';
299
300 /**
301 * The menu order
302 *
303 * @since 1.0
304 * @access public
305 *
306 * @var int
307 */
308 public $menu_order = 0;
309
310 /**
311 * The mime type0
312 *
313 * @since 1.0
314 * @access public
315 *
316 * @var string
317 */
318 public $post_mime_type = '';
319
320 /**
321 * The comment count
322 *
323 * @since 1.0
324 * @access public
325 *
326 * @var int
327 */
328 public $comment_count = 0;
329
330 /**
331 * Filtered
332 *
333 * @since 1.0
334 * @access public
335 *
336 * @var string
337 */
338 public $filter;
339
340 /**
341 * Class Constructor
342 *
343 * Set up the Give Donate Form Class.
344 *
345 * @since 1.0
346 * @access public
347 *
348 * @param int|bool $_id Post id. Default is false.
349 * @param array $_args Arguments passed.
350 */
351 public function __construct( $_id = false, $_args = array() ) {
352 $donation_form = WP_Post::get_instance( $_id );
353
354 $this->setup_donation_form( $donation_form );
355 }
356
357 /**
358 * Given the donation form data, let's set the variables
359 *
360 * @since 1.5
361 * @access private
362 *
363 * @param WP_Post $donation_form WP_Post Object for the donation form.
364 *
365 * @return bool If the setup was successful or not.
366 */
367 private function setup_donation_form( $donation_form ) {
368
369 // Bailout.
370 if (
371 ! ( $donation_form instanceof WP_Post )
372 || 'give_forms' !== $donation_form->post_type
373 ) {
374 return false;
375 }
376
377 Give_Forms_Query::update_meta_cache( array( $donation_form->ID ) );
378
379 foreach ( $donation_form as $key => $value ) {
380 $this->$key = $value;
381 }
382
383 return $donation_form->ID;
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 return $this->{"get_{$key}"}();
401 }
402
403 /* translators: %s: property key */
404 return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) );
405
406 }
407
408 /**
409 * Creates a donation form
410 *
411 * @since 1.5
412 * @access public
413 *
414 * @param array $data Array of attributes for a donation form.
415 *
416 * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID.
417 */
418 public function create( $data = array() ) {
419
420 if ( $this->id != 0 ) {
421 return false;
422 }
423
424 $defaults = array(
425 'post_type' => 'give_forms',
426 'post_status' => 'draft',
427 'post_title' => __( 'New Donation Form', 'give' ),
428 );
429
430 $args = wp_parse_args( $data, $defaults );
431
432 /**
433 * Fired before a donation form is created
434 *
435 * @param array $args The post object arguments used for creation.
436 */
437 do_action( 'give_form_pre_create', $args );
438
439 $id = wp_insert_post( $args, true );
440
441 $donation_form = WP_Post::get_instance( $id );
442
443 /**
444 * Fired after a donation form is created
445 *
446 * @param int $id The post ID of the created item.
447 * @param array $args The post object arguments used for creation.
448 */
449 do_action( 'give_form_post_create', $id, $args );
450
451 return $this->setup_donation_form( $donation_form );
452
453 }
454
455 /**
456 * Retrieve the ID
457 *
458 * @since 1.0
459 * @access public
460 *
461 * @return int Donation form ID.
462 */
463 public function get_ID() {
464 return $this->ID;
465 }
466
467 /**
468 * Retrieve the donation form name
469 *
470 * @since 1.5
471 * @access public
472 *
473 * @return string Donation form name.
474 */
475 public function get_name() {
476 return get_the_title( $this->ID );
477 }
478
479 /**
480 * Retrieve the price
481 *
482 * @since 1.0
483 * @access public
484 *
485 * @return float Price.
486 */
487 public function get_price() {
488
489 if ( ! isset( $this->price ) ) {
490
491 $this->price = give_maybe_sanitize_amount(
492 give_get_meta(
493 $this->ID,
494 '_give_set_price',
495 true
496 )
497 );
498
499 if ( ! $this->price ) {
500 $this->price = 0;
501 }
502 }
503
504 /**
505 * Override the donation form set price.
506 *
507 * @since 1.0
508 *
509 * @param string $price The donation form price.
510 * @param string|int $id The form ID.
511 */
512 return apply_filters( 'give_get_set_price', $this->price, $this->ID );
513 }
514
515 /**
516 * Retrieve the minimum price.
517 *
518 * @since 1.3.6
519 * @access public
520 *
521 * @return float Minimum price.
522 */
523 public function get_minimum_price() {
524
525 if ( ! isset( $this->minimum_price ) ) {
526
527 $this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_range_minimum', true );
528
529 // Give backward < 2.1
530 if ( empty( $this->minimum_price ) ) {
531 $this->minimum_price = give_get_meta( $this->ID, '_give_custom_amount_minimum', true );
532 }
533
534 if ( ! $this->is_custom_price_mode() ) {
535 $this->minimum_price = give_get_lowest_price_option( $this->ID );
536 }
537 }
538
539 return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID );
540 }
541
542 /**
543 * Retrieve the maximum price.
544 *
545 * @since 2.1
546 * @access public
547 *
548 * @return float Maximum price.
549 */
550 public function get_maximum_price() {
551
552 if ( ! isset( $this->maximum_price ) ) {
553 $this->maximum_price = give_get_meta( $this->ID, '_give_custom_amount_range_maximum', true, 999999.99 );
554
555 if ( ! $this->is_custom_price_mode() ) {
556 $this->maximum_price = give_get_highest_price_option( $this->ID );
557 }
558 }
559
560 return apply_filters( 'give_get_set_maximum_price', $this->maximum_price, $this->ID );
561 }
562
563 /**
564 * Retrieve the variable prices
565 *
566 * @since 1.0
567 * @access public
568 *
569 * @return array Variable prices.
570 */
571 public function get_prices() {
572
573 if ( ! isset( $this->prices ) ) {
574
575 $this->prices = give_get_meta( $this->ID, '_give_donation_levels', true );
576
577 }
578
579 /**
580 * Override multi-level prices
581 *
582 * @since 1.0
583 *
584 * @param array $prices The array of mulit-level prices.
585 * @param int|string $ID The ID of the form.
586 */
587 return apply_filters( 'give_get_donation_levels', $this->prices, $this->ID );
588
589 }
590
591 /**
592 * Get donation form level info
593 *
594 * @since 2.0.6
595 * @access public
596 *
597 * @param $price_id
598 *
599 * @return array|null
600 */
601 public function get_level_info( $price_id ) {
602 $level_info = array();
603
604 // Bailout.
605 if ( 'multi' !== $this->get_type() ) {
606 return null;
607 } elseif ( ! ( $levels = $this->get_prices() ) ) {
608 return $level_info;
609 }
610
611 foreach ( $levels as $level ) {
612 if ( $price_id === $level['_give_id']['level_id'] ) {
613 $level_info = $level;
614 break;
615 }
616 }
617
618 return $level_info;
619 }
620
621
622 /**
623 * Retrieve the goal
624 *
625 * @since 1.0
626 * @access public
627 *
628 * @return float Goal.
629 */
630 public function get_goal() {
631
632 if ( ! isset( $this->goal ) ) {
633
634 $goal_format = give_get_form_goal_format( $this->ID );
635
636 if ( ! $this->has_goal() ) {
637 $this->goal = '';
638
639 } elseif ( 'donation' === $goal_format ) {
640 $this->goal = give_get_meta( $this->ID, '_give_number_of_donation_goal', true );
641
642 } elseif ( 'donors' === $goal_format ) {
643 $this->goal = give_get_meta( $this->ID, '_give_number_of_donor_goal', true );
644
645 } elseif ( in_array( $goal_format, array( 'amount', 'percentage' ) ) ) {
646 $this->goal = give_get_meta( $this->ID, '_give_set_goal', true );
647 }
648 }
649
650 return apply_filters( 'give_get_set_goal', $this->goal, $this->ID );
651
652 }
653
654 /**
655 * Determine if single price mode is enabled or disabled
656 *
657 * @since 1.0
658 * @access public
659 *
660 * @return bool
661 */
662 public function is_single_price_mode() {
663
664 $option = give_get_meta( $this->ID, '_give_price_option', true );
665 $ret = 0;
666
667 if ( empty( $option ) || $option === 'set' ) {
668 $ret = 1;
669 }
670
671 /**
672 * Override the price mode for a donation when checking if is in single price mode.
673 *
674 * @since 1.0
675 *
676 * @param bool $ret Is donation form in single price mode?
677 * @param int|string $ID The ID of the donation form.
678 */
679 return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID );
680
681 }
682
683 /**
684 * Determine if custom price mode is enabled or disabled
685 *
686 * @since 1.6
687 * @access public
688 *
689 * @return bool
690 */
691 public function is_custom_price_mode() {
692
693 $option = give_get_meta( $this->ID, '_give_custom_amount', true );
694 $ret = 0;
695
696 if ( give_is_setting_enabled( $option ) ) {
697 $ret = 1;
698 }
699
700 /**
701 * Override the price mode for a donation when checking if is in custom price mode.
702 *
703 * @since 1.6
704 *
705 * @param bool $ret Is donation form in custom price mode?
706 * @param int|string $ID The ID of the donation form.
707 */
708 return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID );
709
710 }
711
712 /**
713 * Determine if custom price mode is enabled or disabled
714 *
715 * @since 1.8.18
716 * @access public
717 *
718 * @param string|float $amount Donation Amount.
719 *
720 * @return bool
721 */
722 public function is_custom_price( $amount ) {
723 $result = false;
724 $amount = give_maybe_sanitize_amount( $amount );
725
726 if ( $this->is_custom_price_mode() ) {
727
728 if ( 'set' === $this->get_type() ) {
729 if ( $amount !== $this->get_price() ) {
730 $result = true;
731 }
732 } elseif ( 'multi' === $this->get_type() ) {
733 $level_amounts = array_map( 'give_maybe_sanitize_amount', wp_list_pluck( $this->get_prices(), '_give_amount' ) );
734 $result = ! in_array( $amount, $level_amounts );
735 }
736 }
737
738 /**
739 * Filter to reset whether it is custom price or not.
740 *
741 * @param bool $result True/False.
742 * @param string|float $amount Donation Amount.
743 * @param int $this ->ID Form ID.
744 *
745 * @since 1.8.18
746 */
747 return (bool) apply_filters( 'give_is_custom_price', $result, $amount, $this->ID );
748 }
749
750 /**
751 * Has Variable Prices
752 *
753 * Determine if the donation form has variable prices enabled
754 *
755 * @since 1.0
756 * @access public
757 *
758 * @return bool
759 */
760 public function has_variable_prices() {
761
762 $option = give_get_meta( $this->ID, '_give_price_option', true );
763 $ret = 0;
764
765 if ( $option === 'multi' ) {
766 $ret = 1;
767 }
768
769 /**
770 * Filter: Override whether the donation form has variables prices.
771 *
772 * @param bool $ret Does donation form have variable prices?
773 * @param int|string $ID The ID of the donation form.
774 */
775 return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID );
776
777 }
778
779 /**
780 * Retrieve the donation form type, set or multi-level
781 *
782 * @since 1.5
783 * @access public
784 *
785 * @return string Type of donation form, either 'set' or 'multi'.
786 */
787 public function get_type() {
788
789 if ( ! isset( $this->type ) ) {
790
791 $this->type = give_get_meta( $this->ID, '_give_price_option', true );
792
793 if ( empty( $this->type ) ) {
794 $this->type = 'set';
795 }
796 }
797
798 return apply_filters( 'give_get_form_type', $this->type, $this->ID );
799
800 }
801
802 /**
803 * Get form tag classes.
804 *
805 * Provides the classes for the donation <form> html tag and filters for customization.
806 *
807 * @since 1.6
808 * @access public
809 *
810 * @param $args
811 *
812 * @return string
813 */
814 public function get_form_classes( $args ) {
815
816 $float_labels_option = give_is_float_labels_enabled( $args )
817 ? 'float-labels-enabled'
818 : '';
819
820 $form_classes_array = apply_filters(
821 'give_form_classes',
822 array(
823 'give-form',
824 'give-form-' . $this->ID,
825 'give-form-type-' . $this->get_type(),
826 $float_labels_option,
827 ),
828 $this->ID,
829 $args
830 );
831
832 // Remove empty class names.
833 $form_classes_array = array_filter( $form_classes_array );
834
835 return implode( ' ', $form_classes_array );
836
837 }
838
839 /**
840 * Get form wrap Classes.
841 *
842 * Provides the classes for the donation form div wrapper and filters for customization.
843 *
844 * @access public
845 *
846 * @param $args
847 *
848 * @return string
849 */
850 public function get_form_wrap_classes( $args ) {
851 $custom_class = array(
852 'give-form-wrap',
853 );
854
855 if ( $this->is_close_donation_form() ) {
856 $custom_class[] = 'give-form-closed';
857 } else {
858 $display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
859 ? $args['display_style']
860 : give_get_meta( $this->ID, '_give_payment_display', true );
861
862 $custom_class[] = "give-display-{$display_option}";
863
864 // If admin want to show only button for form then user inbuilt modal functionality.
865 if ( 'button' === $display_option ) {
866 $custom_class[] = 'give-display-button-only';
867 }
868 }
869
870 /**
871 * Filter the donation form classes.
872 *
873 * @since 1.0
874 */
875 $form_wrap_classes_array = (array) apply_filters( 'give_form_wrap_classes', $custom_class, $this->ID, $args );
876
877 return implode( ' ', $form_wrap_classes_array );
878
879 }
880
881 /**
882 * Get if form type set or not.
883 *
884 * @since 1.6
885 * @access public
886 *
887 * @return bool
888 */
889 public function is_set_type_donation_form() {
890 $form_type = $this->get_type();
891
892 return ( 'set' === $form_type ? true : false );
893 }
894
895 /**
896 * Get if form type multi or not.
897 *
898 * @since 1.6
899 * @access public
900 *
901 * @return bool True if form type is 'multi' and false otherwise.
902 */
903 public function is_multi_type_donation_form() {
904 $form_type = $this->get_type();
905
906 return ( 'multi' === $form_type ? true : false );
907
908 }
909
910 /**
911 * Retrieve the sale count for the donation form
912 *
913 * @since 1.0
914 * @access public
915 *
916 * @return int Donation form sale count.
917 */
918 public function get_sales() {
919
920 if ( ! isset( $this->sales ) ) {
921
922 if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) {
923 add_post_meta( $this->ID, '_give_form_sales', 0 );
924 } // End if
925
926 $this->sales = give_get_meta( $this->ID, '_give_form_sales', true );
927
928 if ( $this->sales < 0 ) {
929 // Never let sales be less than zero.
930 $this->sales = 0;
931 }
932 }
933
934 return $this->sales;
935
936 }
937
938 /**
939 * Increment the sale count by one
940 *
941 * @since 1.0
942 * @access public
943 *
944 * @param int $quantity The quantity to increase the donations by. Default is 1.
945 *
946 * @return int|false New number of total sales.
947 */
948 public function increase_sales( $quantity = 1 ) {
949
950 $sales = give_get_form_sales_stats( $this->ID );
951 $quantity = absint( $quantity );
952 $total_sales = $sales + $quantity;
953
954 if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
955
956 $this->sales = $total_sales;
957
958 return $this->sales;
959
960 }
961
962 return false;
963 }
964
965 /**
966 * Decrement the sale count by one
967 *
968 * @since 1.0
969 * @access public
970 *
971 * @param int $quantity The quantity to decrease by. Default is 1.
972 *
973 * @return int|false New number of total sales.
974 */
975 public function decrease_sales( $quantity = 1 ) {
976
977 $sales = give_get_form_sales_stats( $this->ID );
978
979 // Only decrease if not already zero
980 if ( $sales > 0 ) {
981
982 $quantity = absint( $quantity );
983 $total_sales = $sales - $quantity;
984
985 if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
986
987 $this->sales = $sales;
988
989 return $sales;
990
991 }
992 }
993
994 return false;
995
996 }
997
998 /**
999 * Retrieve the total earnings for the form
1000 *
1001 * @since 1.0
1002 * @access public
1003 *
1004 * @return float Donation form total earnings.
1005 */
1006 public function get_earnings() {
1007
1008 if ( ! isset( $this->earnings ) ) {
1009
1010 if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) {
1011 add_post_meta( $this->ID, '_give_form_earnings', 0 );
1012 }
1013
1014 $this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true );
1015
1016 if ( $this->earnings < 0 ) {
1017 // Never let earnings be less than zero
1018 $this->earnings = 0;
1019 }
1020 }
1021
1022 return $this->earnings;
1023
1024 }
1025
1026 /**
1027 * Increase the earnings by the given amount
1028 *
1029 * @since 1.0
1030 * @since 2.1 Pass the donation ID.
1031 *
1032 * @access public
1033 *
1034 * @param int $amount Amount of donation. Default is 0.
1035 * @param int $payment_id Donation ID.
1036 *
1037 * @return float|false
1038 */
1039 public function increase_earnings( $amount = 0, $payment_id = 0 ) {
1040
1041 $earnings = give_get_form_earnings_stats( $this->ID );
1042
1043 /**
1044 * Modify the earning amount when increasing.
1045 *
1046 * @since 2.1
1047 *
1048 * @param float $amount Earning amount.
1049 * @param int $form_id Donation form ID.
1050 * @param int $payment_id Donation ID.
1051 */
1052 $amount = apply_filters( 'give_increase_form_earnings_amount', $amount, $this->ID, $payment_id );
1053
1054 $new_amount = $earnings + (float) $amount;
1055
1056 if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1057
1058 $this->earnings = $new_amount;
1059
1060 return $this->earnings;
1061
1062 }
1063
1064 return false;
1065
1066 }
1067
1068 /**
1069 * Decrease the earnings by the given amount
1070 *
1071 * @since 1.0
1072 * @access public
1073 *
1074 * @param int $amount Amount of donation.
1075 * @param int $payment_id Donation ID.
1076 *
1077 * @return float|false
1078 */
1079 public function decrease_earnings( $amount, $payment_id = 0 ) {
1080
1081 $earnings = give_get_form_earnings_stats( $this->ID );
1082
1083 if ( $earnings > 0 ) {
1084
1085 /**
1086 * Modify the earning value when decreasing it.
1087 *
1088 * @since 2.1
1089 *
1090 * @param float $amount Earning amount.
1091 * @param int $form_id Donation Form ID.
1092 * @param int $payment_id Donation ID.
1093 */
1094 $amount = apply_filters( 'give_decrease_form_earnings_amount', $amount, $this->ID, $payment_id );
1095
1096 // Only decrease if greater than zero
1097 $new_amount = $earnings - (float) $amount;
1098
1099 if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1100 $this->earnings = $new_amount;
1101
1102 return $this->earnings;
1103 }
1104 }
1105
1106 return false;
1107
1108 }
1109
1110 /**
1111 * Determine if donation form closed or not
1112 *
1113 * Form will be close if:
1114 * a. form has fixed goal
1115 * b. close form when goal achieved cmb2 setting is set to 'Yes'
1116 * c. goal has been achieved
1117 *
1118 * @since 1.4.5
1119 * @access public
1120 *
1121 * @return bool
1122 */
1123 public function is_close_donation_form() {
1124 $is_closed = ( 'closed' === give_get_meta( $this->ID, '_give_form_status', true, 'open' ) );
1125
1126 // If manual upgrade not completed, proceed with backward compatible code.
1127 if ( ! give_has_upgrade_completed( 'v210_verify_form_status_upgrades' ) ) {
1128
1129 // Check for backward compatibility.
1130 $is_closed = $this->bc_210_is_close_donation_form();
1131 }
1132
1133 /**
1134 * Filter the close form result.
1135 *
1136 * @since 1.8
1137 */
1138 return apply_filters(
1139 'give_is_close_donation_form',
1140 $is_closed,
1141 $this
1142 );
1143
1144 }
1145
1146
1147 /**
1148 * Check whether donation form has goal or not
1149 *
1150 * @since 2.4.2
1151 * @access public
1152 *
1153 * @return bool
1154 */
1155 public function has_goal() {
1156 return give_is_setting_enabled(
1157 Give()->form_meta->get_meta(
1158 $this->ID,
1159 '_give_goal_option',
1160 true
1161 )
1162 );
1163 }
1164
1165 /**
1166 * Updates a single meta entry for the donation form
1167 *
1168 * @since 1.5
1169 * @access private
1170 *
1171 * @param string $meta_key The meta_key to update.
1172 * @param string|array|object $meta_value The value to put into the meta.
1173 *
1174 * @return bool The result of the update query.
1175 */
1176 private function update_meta( $meta_key = '', $meta_value = '' ) {
1177
1178 /* @var WPDB $wpdb */
1179 global $wpdb;
1180
1181 // Bailout.
1182 if ( empty( $meta_key ) ) {
1183 return false;
1184 }
1185
1186 if ( give_update_meta( $this->ID, $meta_key, $meta_value ) ) {
1187 return true;
1188 }
1189
1190 return false;
1191 }
1192
1193 /**
1194 * Backward Compatible function for is_close_donation_form()
1195 *
1196 * @since 2.1.0
1197 *
1198 * @return bool
1199 */
1200 private function bc_210_is_close_donation_form() {
1201
1202 $close_form = false;
1203 $is_goal_enabled = give_is_setting_enabled( give_get_meta( $this->ID, '_give_goal_option', true, 'disabled' ) );
1204
1205 // Proceed, if the form goal is enabled.
1206 if ( $is_goal_enabled ) {
1207
1208 $close_form_when_goal_achieved = give_is_setting_enabled( give_get_meta( $this->ID, '_give_close_form_when_goal_achieved', true, 'disabled' ) );
1209
1210 // Proceed, if close form when goal achieved option is enabled.
1211 if ( $close_form_when_goal_achieved ) {
1212
1213 $form = new Give_Donate_Form( $this->ID );
1214 $goal_format = give_get_form_goal_format( $this->ID );
1215
1216 // Verify whether the form is closed or not after processing data based on goal format.
1217 switch ( $goal_format ) {
1218 case 'donation':
1219 $closed = $form->get_goal() <= $form->get_sales();
1220 break;
1221 case 'donors':
1222 $closed = $form->get_goal() <= give_get_form_donor_count( $this->ID );
1223 break;
1224 default:
1225 $closed = $form->get_goal() <= $form->get_earnings();
1226 break;
1227 }
1228
1229 if ( $closed ) {
1230 $close_form = true;
1231 }
1232 }
1233 }
1234
1235 return $close_form;
1236 }
1237
1238 }
1239