PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.0.4
GiveWP – Donation Plugin and Fundraising Platform v3.0.4
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 2 years ago api 3 years ago database 5 years ago deprecated 3 years ago donors 2 years ago emails 3 years ago forms 2 years ago frontend 6 years ago gateways 2 years ago libraries 2 years ago payments 2 years ago actions.php 5 years ago ajax-functions.php 2 years ago class-give-async-process.php 2 years ago class-give-background-updater.php 2 years ago class-give-cache-setting.php 2 years ago class-give-cache.php 3 years ago class-give-cli-commands.php 3 years ago class-give-comment.php 6 years ago class-give-cron.php 6 years ago class-give-donate-form.php 4 years ago class-give-donor.php 3 years ago class-give-email-access.php 5 years ago class-give-license-handler.php 4 years ago class-give-logging.php 5 years ago class-give-readme-parser.php 4 years ago class-give-roles.php 6 years ago class-give-scripts.php 3 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 4 years ago class-notices.php 2 years ago country-functions.php 5 years ago currencies-list.php 3 years ago currency-functions.php 3 years ago error-tracking.php 6 years ago filters.php 3 years ago formatting.php 2 years ago install.php 2 years ago login-register.php 4 years ago misc-functions.php 2 years ago plugin-compatibility.php 6 years ago post-types.php 5 years ago price-functions.php 6 years ago process-donation.php 4 years ago setting-functions.php 6 years ago shortcodes.php 2 years ago template-functions.php 4 years ago user-functions.php 3 years ago
class-give-donate-form.php
1240 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 = [] ) {
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( [ $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 = [] ) {
419
420 if ( $this->id != 0 ) {
421 return false;
422 }
423
424 $defaults = [
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 string $price_id
598 *
599 * @return array|null
600 */
601 public function get_level_info( $price_id ) {
602 $level_info = [];
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 * @since 2.19.0 Set default goal value to zero to prevent fatal error on PHP8.0.
627 * @access public
628 *
629 * @return float Goal.
630 */
631 public function get_goal() {
632
633 if ( ! isset( $this->goal ) ) {
634
635 $goal_format = give_get_form_goal_format( $this->ID );
636
637 if ( ! $this->has_goal() ) {
638 $this->goal = '';
639
640 } elseif ( 'donation' === $goal_format ) {
641 $this->goal = give_get_meta( $this->ID, '_give_number_of_donation_goal', true );
642
643 } elseif ( 'donors' === $goal_format ) {
644 $this->goal = give_get_meta( $this->ID, '_give_number_of_donor_goal', true );
645
646 } elseif ( in_array( $goal_format, [ 'amount', 'percentage' ] ) ) {
647 $this->goal = give_get_meta( $this->ID, '_give_set_goal', true );
648 }
649 }
650
651 return apply_filters( 'give_get_set_goal', $this->goal ?: 0, $this->ID );
652
653 }
654
655 /**
656 * Determine if single price mode is enabled or disabled
657 *
658 * @since 1.0
659 * @access public
660 *
661 * @return bool
662 */
663 public function is_single_price_mode() {
664
665 $option = give_get_meta( $this->ID, '_give_price_option', true );
666 $ret = 0;
667
668 if ( empty( $option ) || $option === 'set' ) {
669 $ret = 1;
670 }
671
672 /**
673 * Override the price mode for a donation when checking if is in single price mode.
674 *
675 * @since 1.0
676 *
677 * @param bool $ret Is donation form in single price mode?
678 * @param int|string $ID The ID of the donation form.
679 */
680 return (bool) apply_filters( 'give_single_price_option_mode', $ret, $this->ID );
681
682 }
683
684 /**
685 * Determine if custom price mode is enabled or disabled
686 *
687 * @since 1.6
688 * @access public
689 *
690 * @return bool
691 */
692 public function is_custom_price_mode() {
693
694 $option = give_get_meta( $this->ID, '_give_custom_amount', true );
695 $ret = 0;
696
697 if ( give_is_setting_enabled( $option ) ) {
698 $ret = 1;
699 }
700
701 /**
702 * Override the price mode for a donation when checking if is in custom price mode.
703 *
704 * @since 1.6
705 *
706 * @param bool $ret Is donation form in custom price mode?
707 * @param int|string $ID The ID of the donation form.
708 */
709 return (bool) apply_filters( 'give_custom_price_option_mode', $ret, $this->ID );
710
711 }
712
713 /**
714 * Determine if custom price mode is enabled or disabled
715 *
716 * @since 1.8.18
717 * @access public
718 *
719 * @param string|float $amount Donation Amount.
720 *
721 * @return bool
722 */
723 public function is_custom_price( $amount ) {
724 $result = false;
725 $amount = give_maybe_sanitize_amount( $amount );
726
727 if ( $this->is_custom_price_mode() ) {
728
729 if ( 'set' === $this->get_type() ) {
730 if ( $amount !== $this->get_price() ) {
731 $result = true;
732 }
733 } elseif ( 'multi' === $this->get_type() ) {
734 $level_amounts = array_map( 'give_maybe_sanitize_amount', wp_list_pluck( $this->get_prices(), '_give_amount' ) );
735 $result = ! in_array( $amount, $level_amounts );
736 }
737 }
738
739 /**
740 * Filter to reset whether it is custom price or not.
741 *
742 * @param bool $result True/False.
743 * @param string|float $amount Donation Amount.
744 * @param int $this ->ID Form ID.
745 *
746 * @since 1.8.18
747 */
748 return (bool) apply_filters( 'give_is_custom_price', $result, $amount, $this->ID );
749 }
750
751 /**
752 * Has Variable Prices
753 *
754 * Determine if the donation form has variable prices enabled
755 *
756 * @since 1.0
757 * @access public
758 *
759 * @return bool
760 */
761 public function has_variable_prices() {
762
763 $option = give_get_meta( $this->ID, '_give_price_option', true );
764 $ret = 0;
765
766 if ( $option === 'multi' ) {
767 $ret = 1;
768 }
769
770 /**
771 * Filter: Override whether the donation form has variables prices.
772 *
773 * @param bool $ret Does donation form have variable prices?
774 * @param int|string $ID The ID of the donation form.
775 */
776 return (bool) apply_filters( 'give_has_variable_prices', $ret, $this->ID );
777
778 }
779
780 /**
781 * Retrieve the donation form type, set or multi-level
782 *
783 * @since 1.5
784 * @access public
785 *
786 * @return string Type of donation form, either 'set' or 'multi'.
787 */
788 public function get_type() {
789
790 if ( ! isset( $this->type ) ) {
791
792 $this->type = give_get_meta( $this->ID, '_give_price_option', true );
793
794 if ( empty( $this->type ) ) {
795 $this->type = 'set';
796 }
797 }
798
799 return apply_filters( 'give_get_form_type', $this->type, $this->ID );
800
801 }
802
803 /**
804 * Get form tag classes.
805 *
806 * Provides the classes for the donation <form> html tag and filters for customization.
807 *
808 * @since 1.6
809 * @access public
810 *
811 * @param $args
812 *
813 * @return string
814 */
815 public function get_form_classes( $args ) {
816
817 $float_labels_option = give_is_float_labels_enabled( $args )
818 ? 'float-labels-enabled'
819 : '';
820
821 $form_classes_array = apply_filters(
822 'give_form_classes',
823 [
824 'give-form',
825 'give-form-' . $this->ID,
826 'give-form-type-' . $this->get_type(),
827 $float_labels_option,
828 ],
829 $this->ID,
830 $args
831 );
832
833 // Remove empty class names.
834 $form_classes_array = array_filter( $form_classes_array );
835
836 return implode( ' ', $form_classes_array );
837
838 }
839
840 /**
841 * Get form wrap Classes.
842 *
843 * Provides the classes for the donation form div wrapper and filters for customization.
844 *
845 * @access public
846 *
847 * @param $args
848 *
849 * @return string
850 */
851 public function get_form_wrap_classes( $args ) {
852 $custom_class = [
853 'give-form-wrap',
854 ];
855
856 if ( $this->is_close_donation_form() ) {
857 $custom_class[] = 'give-form-closed';
858 } else {
859 $display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) )
860 ? $args['display_style']
861 : give_get_meta( $this->ID, '_give_payment_display', true );
862
863 $custom_class[] = "give-display-{$display_option}";
864
865 // If admin want to show only button for form then user inbuilt modal functionality.
866 if ( 'button' === $display_option ) {
867 $custom_class[] = 'give-display-button-only';
868 }
869 }
870
871 /**
872 * Filter the donation form classes.
873 *
874 * @since 1.0
875 */
876 $form_wrap_classes_array = (array) apply_filters( 'give_form_wrap_classes', $custom_class, $this->ID, $args );
877
878 return implode( ' ', $form_wrap_classes_array );
879
880 }
881
882 /**
883 * Get if form type set or not.
884 *
885 * @since 1.6
886 * @access public
887 *
888 * @return bool
889 */
890 public function is_set_type_donation_form() {
891 $form_type = $this->get_type();
892
893 return ( 'set' === $form_type ? true : false );
894 }
895
896 /**
897 * Get if form type multi or not.
898 *
899 * @since 1.6
900 * @access public
901 *
902 * @return bool True if form type is 'multi' and false otherwise.
903 */
904 public function is_multi_type_donation_form() {
905 $form_type = $this->get_type();
906
907 return ( 'multi' === $form_type ? true : false );
908
909 }
910
911 /**
912 * Retrieve the sale count for the donation form
913 *
914 * @since 1.0
915 * @access public
916 *
917 * @return int Donation form sale count.
918 */
919 public function get_sales() {
920
921 if ( ! isset( $this->sales ) ) {
922
923 if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) {
924 add_post_meta( $this->ID, '_give_form_sales', 0 );
925 } // End if
926
927 $this->sales = give_get_meta( $this->ID, '_give_form_sales', true );
928
929 if ( $this->sales < 0 ) {
930 // Never let sales be less than zero.
931 $this->sales = 0;
932 }
933 }
934
935 return $this->sales;
936
937 }
938
939 /**
940 * Increment the sale count by one
941 *
942 * @since 1.0
943 * @access public
944 *
945 * @param int $quantity The quantity to increase the donations by. Default is 1.
946 *
947 * @return int|false New number of total sales.
948 */
949 public function increase_sales( $quantity = 1 ) {
950
951 $sales = give_get_form_sales_stats( $this->ID );
952 $quantity = absint( $quantity );
953 $total_sales = $sales + $quantity;
954
955 if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
956
957 $this->sales = $total_sales;
958
959 return $this->sales;
960
961 }
962
963 return false;
964 }
965
966 /**
967 * Decrement the sale count by one
968 *
969 * @since 1.0
970 * @access public
971 *
972 * @param int $quantity The quantity to decrease by. Default is 1.
973 *
974 * @return int|false New number of total sales.
975 */
976 public function decrease_sales( $quantity = 1 ) {
977
978 $sales = give_get_form_sales_stats( $this->ID );
979
980 // Only decrease if not already zero
981 if ( $sales > 0 ) {
982
983 $quantity = absint( $quantity );
984 $total_sales = $sales - $quantity;
985
986 if ( $this->update_meta( '_give_form_sales', $total_sales ) ) {
987
988 $this->sales = $sales;
989
990 return $sales;
991
992 }
993 }
994
995 return false;
996
997 }
998
999 /**
1000 * Retrieve the total earnings for the form
1001 *
1002 * @since 1.0
1003 * @access public
1004 *
1005 * @return float Donation form total earnings.
1006 */
1007 public function get_earnings() {
1008
1009 if ( ! isset( $this->earnings ) ) {
1010
1011 if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) {
1012 add_post_meta( $this->ID, '_give_form_earnings', 0 );
1013 }
1014
1015 $this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true );
1016
1017 if ( $this->earnings < 0 ) {
1018 // Never let earnings be less than zero
1019 $this->earnings = 0;
1020 }
1021 }
1022
1023 return $this->earnings;
1024
1025 }
1026
1027 /**
1028 * Increase the earnings by the given amount
1029 *
1030 * @since 1.0
1031 * @since 2.1 Pass the donation ID.
1032 *
1033 * @access public
1034 *
1035 * @param int $amount Amount of donation. Default is 0.
1036 * @param int $payment_id Donation ID.
1037 *
1038 * @return float|false
1039 */
1040 public function increase_earnings( $amount = 0, $payment_id = 0 ) {
1041
1042 $earnings = give_get_form_earnings_stats( $this->ID );
1043
1044 /**
1045 * Modify the earning amount when increasing.
1046 *
1047 * @since 2.1
1048 *
1049 * @param float $amount Earning amount.
1050 * @param int $form_id Donation form ID.
1051 * @param int $payment_id Donation ID.
1052 */
1053 $amount = apply_filters( 'give_increase_form_earnings_amount', $amount, $this->ID, $payment_id );
1054
1055 $new_amount = $earnings + (float) $amount;
1056
1057 if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1058
1059 $this->earnings = $new_amount;
1060
1061 return $this->earnings;
1062
1063 }
1064
1065 return false;
1066
1067 }
1068
1069 /**
1070 * Decrease the earnings by the given amount
1071 *
1072 * @since 1.0
1073 * @access public
1074 *
1075 * @param int $amount Amount of donation.
1076 * @param int $payment_id Donation ID.
1077 *
1078 * @return float|false
1079 */
1080 public function decrease_earnings( $amount, $payment_id = 0 ) {
1081
1082 $earnings = give_get_form_earnings_stats( $this->ID );
1083
1084 if ( $earnings > 0 ) {
1085
1086 /**
1087 * Modify the earning value when decreasing it.
1088 *
1089 * @since 2.1
1090 *
1091 * @param float $amount Earning amount.
1092 * @param int $form_id Donation Form ID.
1093 * @param int $payment_id Donation ID.
1094 */
1095 $amount = apply_filters( 'give_decrease_form_earnings_amount', $amount, $this->ID, $payment_id );
1096
1097 // Only decrease if greater than zero
1098 $new_amount = $earnings - (float) $amount;
1099
1100 if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) {
1101 $this->earnings = $new_amount;
1102
1103 return $this->earnings;
1104 }
1105 }
1106
1107 return false;
1108
1109 }
1110
1111 /**
1112 * Determine if donation form closed or not
1113 *
1114 * Form will be close if:
1115 * a. form has fixed goal
1116 * b. close form when goal achieved cmb2 setting is set to 'Yes'
1117 * c. goal has been achieved
1118 *
1119 * @since 1.4.5
1120 * @access public
1121 *
1122 * @return bool
1123 */
1124 public function is_close_donation_form() {
1125 $is_closed = ( 'closed' === give_get_meta( $this->ID, '_give_form_status', true, 'open' ) );
1126
1127 // If manual upgrade not completed, proceed with backward compatible code.
1128 if ( ! give_has_upgrade_completed( 'v210_verify_form_status_upgrades' ) ) {
1129
1130 // Check for backward compatibility.
1131 $is_closed = $this->bc_210_is_close_donation_form();
1132 }
1133
1134 /**
1135 * Filter the close form result.
1136 *
1137 * @since 1.8
1138 */
1139 return apply_filters(
1140 'give_is_close_donation_form',
1141 $is_closed,
1142 $this
1143 );
1144
1145 }
1146
1147
1148 /**
1149 * Check whether donation form has goal or not
1150 *
1151 * @since 2.4.2
1152 * @access public
1153 *
1154 * @return bool
1155 */
1156 public function has_goal() {
1157 return give_is_setting_enabled(
1158 Give()->form_meta->get_meta(
1159 $this->ID,
1160 '_give_goal_option',
1161 true
1162 )
1163 );
1164 }
1165
1166 /**
1167 * Updates a single meta entry for the donation form
1168 *
1169 * @since 1.5
1170 * @access private
1171 *
1172 * @param string $meta_key The meta_key to update.
1173 * @param string|array|object $meta_value The value to put into the meta.
1174 *
1175 * @return bool The result of the update query.
1176 */
1177 private function update_meta( $meta_key = '', $meta_value = '' ) {
1178
1179 /* @var WPDB $wpdb */
1180 global $wpdb;
1181
1182 // Bailout.
1183 if ( empty( $meta_key ) ) {
1184 return false;
1185 }
1186
1187 if ( give_update_meta( $this->ID, $meta_key, $meta_value ) ) {
1188 return true;
1189 }
1190
1191 return false;
1192 }
1193
1194 /**
1195 * Backward Compatible function for is_close_donation_form()
1196 *
1197 * @since 2.1.0
1198 *
1199 * @return bool
1200 */
1201 private function bc_210_is_close_donation_form() {
1202
1203 $close_form = false;
1204 $is_goal_enabled = give_is_setting_enabled( give_get_meta( $this->ID, '_give_goal_option', true, 'disabled' ) );
1205
1206 // Proceed, if the form goal is enabled.
1207 if ( $is_goal_enabled ) {
1208
1209 $close_form_when_goal_achieved = give_is_setting_enabled( give_get_meta( $this->ID, '_give_close_form_when_goal_achieved', true, 'disabled' ) );
1210
1211 // Proceed, if close form when goal achieved option is enabled.
1212 if ( $close_form_when_goal_achieved ) {
1213
1214 $form = new Give_Donate_Form( $this->ID );
1215 $goal_format = give_get_form_goal_format( $this->ID );
1216
1217 // Verify whether the form is closed or not after processing data based on goal format.
1218 switch ( $goal_format ) {
1219 case 'donation':
1220 $closed = $form->get_goal() <= $form->get_sales();
1221 break;
1222 case 'donors':
1223 $closed = $form->get_goal() <= give_get_form_donor_count( $this->ID );
1224 break;
1225 default:
1226 $closed = $form->get_goal() <= $form->get_earnings();
1227 break;
1228 }
1229
1230 if ( $closed ) {
1231 $close_form = true;
1232 }
1233 }
1234 }
1235
1236 return $close_form;
1237 }
1238
1239 }
1240