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