| 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 |
* @since 3.11.0 sanitize $args |
| 819 |
*/ |
| 820 |
$args = give_clean($args); |
| 821 |
|
| 822 |
$float_labels_option = give_is_float_labels_enabled( $args ) |
| 823 |
? 'float-labels-enabled' |
| 824 |
: ''; |
| 825 |
|
| 826 |
$form_classes_array = apply_filters( |
| 827 |
'give_form_classes', |
| 828 |
[ |
| 829 |
'give-form', |
| 830 |
'give-form-' . $this->ID, |
| 831 |
'give-form-type-' . $this->get_type(), |
| 832 |
$float_labels_option, |
| 833 |
], |
| 834 |
$this->ID, |
| 835 |
$args |
| 836 |
); |
| 837 |
|
| 838 |
// Remove empty class names. |
| 839 |
$form_classes_array = array_filter( $form_classes_array ); |
| 840 |
|
| 841 |
/** |
| 842 |
* @since 3.11.0 sanitize attributes |
| 843 |
*/ |
| 844 |
$form_classes_array = array_map('esc_attr', $form_classes_array); |
| 845 |
|
| 846 |
return implode( ' ', $form_classes_array ); |
| 847 |
|
| 848 |
} |
| 849 |
|
| 850 |
/** |
| 851 |
* Get form wrap Classes. |
| 852 |
* |
| 853 |
* Provides the classes for the donation form div wrapper and filters for customization. |
| 854 |
* |
| 855 |
* @access public |
| 856 |
* |
| 857 |
* @param $args |
| 858 |
* |
| 859 |
* @return string |
| 860 |
*/ |
| 861 |
public function get_form_wrap_classes( $args ) { |
| 862 |
/** |
| 863 |
* @since 3.11.0 sanitize $args |
| 864 |
*/ |
| 865 |
$args = give_clean($args); |
| 866 |
|
| 867 |
$custom_class = [ |
| 868 |
'give-form-wrap', |
| 869 |
]; |
| 870 |
|
| 871 |
if ( $this->is_close_donation_form() ) { |
| 872 |
$custom_class[] = 'give-form-closed'; |
| 873 |
} else { |
| 874 |
$display_option = ( isset( $args['display_style'] ) && ! empty( $args['display_style'] ) ) |
| 875 |
? $args['display_style'] |
| 876 |
: give_get_meta( $this->ID, '_give_payment_display', true ); |
| 877 |
|
| 878 |
$custom_class[] = "give-display-{$display_option}"; |
| 879 |
|
| 880 |
// If admin want to show only button for form then user inbuilt modal functionality. |
| 881 |
if ( 'button' === $display_option ) { |
| 882 |
$custom_class[] = 'give-display-button-only'; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
/** |
| 887 |
* Filter the donation form classes. |
| 888 |
* |
| 889 |
* @since 1.0 |
| 890 |
*/ |
| 891 |
$form_wrap_classes_array = (array) apply_filters( 'give_form_wrap_classes', $custom_class, $this->ID, $args ); |
| 892 |
|
| 893 |
/** |
| 894 |
* @since 3.11.0 sanitize attributes |
| 895 |
*/ |
| 896 |
$form_wrap_classes_array = array_map('esc_attr', $form_wrap_classes_array); |
| 897 |
|
| 898 |
return implode( ' ', $form_wrap_classes_array ); |
| 899 |
|
| 900 |
} |
| 901 |
|
| 902 |
/** |
| 903 |
* Get if form type set or not. |
| 904 |
* |
| 905 |
* @since 1.6 |
| 906 |
* @access public |
| 907 |
* |
| 908 |
* @return bool |
| 909 |
*/ |
| 910 |
public function is_set_type_donation_form() { |
| 911 |
$form_type = $this->get_type(); |
| 912 |
|
| 913 |
return ( 'set' === $form_type ? true : false ); |
| 914 |
} |
| 915 |
|
| 916 |
/** |
| 917 |
* Get if form type multi or not. |
| 918 |
* |
| 919 |
* @since 1.6 |
| 920 |
* @access public |
| 921 |
* |
| 922 |
* @return bool True if form type is 'multi' and false otherwise. |
| 923 |
*/ |
| 924 |
public function is_multi_type_donation_form() { |
| 925 |
$form_type = $this->get_type(); |
| 926 |
|
| 927 |
return ( 'multi' === $form_type ? true : false ); |
| 928 |
|
| 929 |
} |
| 930 |
|
| 931 |
/** |
| 932 |
* Retrieve the sale count for the donation form |
| 933 |
* |
| 934 |
* @since 1.0 |
| 935 |
* @access public |
| 936 |
* |
| 937 |
* @return int Donation form sale count. |
| 938 |
*/ |
| 939 |
public function get_sales() { |
| 940 |
|
| 941 |
if ( ! isset( $this->sales ) ) { |
| 942 |
|
| 943 |
if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) { |
| 944 |
give_update_meta( $this->ID, '_give_form_sales', 0 ); |
| 945 |
} // End if |
| 946 |
|
| 947 |
$this->sales = give_get_meta( $this->ID, '_give_form_sales', true ); |
| 948 |
|
| 949 |
if ( $this->sales < 0 ) { |
| 950 |
// Never let sales be less than zero. |
| 951 |
$this->sales = 0; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
/** |
| 956 |
* @since 3.14.0 |
| 957 |
*/ |
| 958 |
return apply_filters('give_donate_form_get_sales', $this->sales, $this->ID); |
| 959 |
} |
| 960 |
|
| 961 |
/** |
| 962 |
* Increment the sale count by one |
| 963 |
* |
| 964 |
* @since 1.0 |
| 965 |
* @access public |
| 966 |
* |
| 967 |
* @param int $quantity The quantity to increase the donations by. Default is 1. |
| 968 |
* |
| 969 |
* @return int|false New number of total sales. |
| 970 |
*/ |
| 971 |
public function increase_sales( $quantity = 1 ) { |
| 972 |
|
| 973 |
$sales = give_get_form_sales_stats( $this->ID ); |
| 974 |
$quantity = absint( $quantity ); |
| 975 |
$total_sales = $sales + $quantity; |
| 976 |
|
| 977 |
if ( $this->update_meta( '_give_form_sales', $total_sales ) ) { |
| 978 |
|
| 979 |
$this->sales = $total_sales; |
| 980 |
|
| 981 |
return $this->sales; |
| 982 |
|
| 983 |
} |
| 984 |
|
| 985 |
return false; |
| 986 |
} |
| 987 |
|
| 988 |
/** |
| 989 |
* Decrement the sale count by one |
| 990 |
* |
| 991 |
* @since 1.0 |
| 992 |
* @access public |
| 993 |
* |
| 994 |
* @param int $quantity The quantity to decrease by. Default is 1. |
| 995 |
* |
| 996 |
* @return int|false New number of total sales. |
| 997 |
*/ |
| 998 |
public function decrease_sales( $quantity = 1 ) { |
| 999 |
|
| 1000 |
$sales = give_get_form_sales_stats( $this->ID ); |
| 1001 |
|
| 1002 |
// Only decrease if not already zero |
| 1003 |
if ( $sales > 0 ) { |
| 1004 |
|
| 1005 |
$quantity = absint( $quantity ); |
| 1006 |
$total_sales = $sales - $quantity; |
| 1007 |
|
| 1008 |
if ( $this->update_meta( '_give_form_sales', $total_sales ) ) { |
| 1009 |
|
| 1010 |
$this->sales = $sales; |
| 1011 |
|
| 1012 |
return $sales; |
| 1013 |
|
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
return false; |
| 1018 |
|
| 1019 |
} |
| 1020 |
|
| 1021 |
/** |
| 1022 |
* Retrieve the total earnings for the form |
| 1023 |
* |
| 1024 |
* @since 1.0 |
| 1025 |
* @access public |
| 1026 |
* |
| 1027 |
* @return float Donation form total earnings. |
| 1028 |
*/ |
| 1029 |
public function get_earnings() { |
| 1030 |
|
| 1031 |
if ( ! isset( $this->earnings ) ) { |
| 1032 |
|
| 1033 |
if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) { |
| 1034 |
give_update_meta( $this->ID, '_give_form_earnings', 0 ); |
| 1035 |
} |
| 1036 |
|
| 1037 |
$this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true ); |
| 1038 |
|
| 1039 |
if ( $this->earnings < 0 ) { |
| 1040 |
// Never let earnings be less than zero |
| 1041 |
$this->earnings = 0; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
return $this->earnings; |
| 1046 |
|
| 1047 |
} |
| 1048 |
|
| 1049 |
/** |
| 1050 |
* Increase the earnings by the given amount |
| 1051 |
* |
| 1052 |
* @since 1.0 |
| 1053 |
* @since 2.1 Pass the donation ID. |
| 1054 |
* |
| 1055 |
* @access public |
| 1056 |
* |
| 1057 |
* @param int $amount Amount of donation. Default is 0. |
| 1058 |
* @param int $payment_id Donation ID. |
| 1059 |
* |
| 1060 |
* @return float|false |
| 1061 |
*/ |
| 1062 |
public function increase_earnings( $amount = 0, $payment_id = 0 ) { |
| 1063 |
|
| 1064 |
$earnings = give_get_form_earnings_stats( $this->ID ); |
| 1065 |
|
| 1066 |
/** |
| 1067 |
* Modify the earning amount when increasing. |
| 1068 |
* |
| 1069 |
* @since 2.1 |
| 1070 |
* |
| 1071 |
* @param float $amount Earning amount. |
| 1072 |
* @param int $form_id Donation form ID. |
| 1073 |
* @param int $payment_id Donation ID. |
| 1074 |
*/ |
| 1075 |
$amount = apply_filters( 'give_increase_form_earnings_amount', $amount, $this->ID, $payment_id ); |
| 1076 |
|
| 1077 |
$new_amount = $earnings + (float) $amount; |
| 1078 |
|
| 1079 |
if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) { |
| 1080 |
|
| 1081 |
$this->earnings = $new_amount; |
| 1082 |
|
| 1083 |
return $this->earnings; |
| 1084 |
|
| 1085 |
} |
| 1086 |
|
| 1087 |
return false; |
| 1088 |
|
| 1089 |
} |
| 1090 |
|
| 1091 |
/** |
| 1092 |
* Decrease the earnings by the given amount |
| 1093 |
* |
| 1094 |
* @since 1.0 |
| 1095 |
* @access public |
| 1096 |
* |
| 1097 |
* @param int $amount Amount of donation. |
| 1098 |
* @param int $payment_id Donation ID. |
| 1099 |
* |
| 1100 |
* @return float|false |
| 1101 |
*/ |
| 1102 |
public function decrease_earnings( $amount, $payment_id = 0 ) { |
| 1103 |
|
| 1104 |
$earnings = give_get_form_earnings_stats( $this->ID ); |
| 1105 |
|
| 1106 |
if ( $earnings > 0 ) { |
| 1107 |
|
| 1108 |
/** |
| 1109 |
* Modify the earning value when decreasing it. |
| 1110 |
* |
| 1111 |
* @since 2.1 |
| 1112 |
* |
| 1113 |
* @param float $amount Earning amount. |
| 1114 |
* @param int $form_id Donation Form ID. |
| 1115 |
* @param int $payment_id Donation ID. |
| 1116 |
*/ |
| 1117 |
$amount = apply_filters( 'give_decrease_form_earnings_amount', $amount, $this->ID, $payment_id ); |
| 1118 |
|
| 1119 |
// Only decrease if greater than zero |
| 1120 |
$new_amount = $earnings - (float) $amount; |
| 1121 |
|
| 1122 |
if ( $this->update_meta( '_give_form_earnings', $new_amount ) ) { |
| 1123 |
$this->earnings = $new_amount; |
| 1124 |
|
| 1125 |
return $this->earnings; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
return false; |
| 1130 |
|
| 1131 |
} |
| 1132 |
|
| 1133 |
/** |
| 1134 |
* Determine if donation form closed or not |
| 1135 |
* |
| 1136 |
* Form will be close if: |
| 1137 |
* a. form has fixed goal |
| 1138 |
* b. close form when goal achieved cmb2 setting is set to 'Yes' |
| 1139 |
* c. goal has been achieved |
| 1140 |
* |
| 1141 |
* @since 1.4.5 |
| 1142 |
* @access public |
| 1143 |
* |
| 1144 |
* @return bool |
| 1145 |
*/ |
| 1146 |
public function is_close_donation_form() { |
| 1147 |
$is_closed = ( 'closed' === give_get_meta( $this->ID, '_give_form_status', true, 'open' ) ); |
| 1148 |
|
| 1149 |
// If manual upgrade not completed, proceed with backward compatible code. |
| 1150 |
if ( ! give_has_upgrade_completed( 'v210_verify_form_status_upgrades' ) ) { |
| 1151 |
|
| 1152 |
// Check for backward compatibility. |
| 1153 |
$is_closed = $this->bc_210_is_close_donation_form(); |
| 1154 |
} |
| 1155 |
|
| 1156 |
/** |
| 1157 |
* Filter the close form result. |
| 1158 |
* |
| 1159 |
* @since 1.8 |
| 1160 |
*/ |
| 1161 |
return apply_filters( |
| 1162 |
'give_is_close_donation_form', |
| 1163 |
$is_closed, |
| 1164 |
$this |
| 1165 |
); |
| 1166 |
|
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
/** |
| 1171 |
* Check whether donation form has goal or not |
| 1172 |
* |
| 1173 |
* @since 2.4.2 |
| 1174 |
* @access public |
| 1175 |
* |
| 1176 |
* @return bool |
| 1177 |
*/ |
| 1178 |
public function has_goal() { |
| 1179 |
return give_is_setting_enabled( |
| 1180 |
Give()->form_meta->get_meta( |
| 1181 |
$this->ID, |
| 1182 |
'_give_goal_option', |
| 1183 |
true |
| 1184 |
) |
| 1185 |
); |
| 1186 |
} |
| 1187 |
|
| 1188 |
/** |
| 1189 |
* Updates a single meta entry for the donation form |
| 1190 |
* |
| 1191 |
* @since 1.5 |
| 1192 |
* @access private |
| 1193 |
* |
| 1194 |
* @param string $meta_key The meta_key to update. |
| 1195 |
* @param string|array|object $meta_value The value to put into the meta. |
| 1196 |
* |
| 1197 |
* @return bool The result of the update query. |
| 1198 |
*/ |
| 1199 |
private function update_meta( $meta_key = '', $meta_value = '' ) { |
| 1200 |
|
| 1201 |
/* @var WPDB $wpdb */ |
| 1202 |
global $wpdb; |
| 1203 |
|
| 1204 |
// Bailout. |
| 1205 |
if ( empty( $meta_key ) ) { |
| 1206 |
return false; |
| 1207 |
} |
| 1208 |
|
| 1209 |
if ( give_update_meta( $this->ID, $meta_key, $meta_value ) ) { |
| 1210 |
return true; |
| 1211 |
} |
| 1212 |
|
| 1213 |
return false; |
| 1214 |
} |
| 1215 |
|
| 1216 |
/** |
| 1217 |
* Backward Compatible function for is_close_donation_form() |
| 1218 |
* |
| 1219 |
* @since 2.1.0 |
| 1220 |
* |
| 1221 |
* @return bool |
| 1222 |
*/ |
| 1223 |
private function bc_210_is_close_donation_form() { |
| 1224 |
|
| 1225 |
$close_form = false; |
| 1226 |
$is_goal_enabled = give_is_setting_enabled( give_get_meta( $this->ID, '_give_goal_option', true, 'disabled' ) ); |
| 1227 |
|
| 1228 |
// Proceed, if the form goal is enabled. |
| 1229 |
if ( $is_goal_enabled ) { |
| 1230 |
|
| 1231 |
$close_form_when_goal_achieved = give_is_setting_enabled( give_get_meta( $this->ID, '_give_close_form_when_goal_achieved', true, 'disabled' ) ); |
| 1232 |
|
| 1233 |
// Proceed, if close form when goal achieved option is enabled. |
| 1234 |
if ( $close_form_when_goal_achieved ) { |
| 1235 |
|
| 1236 |
$form = new Give_Donate_Form( $this->ID ); |
| 1237 |
$goal_format = give_get_form_goal_format( $this->ID ); |
| 1238 |
|
| 1239 |
// Verify whether the form is closed or not after processing data based on goal format. |
| 1240 |
switch ( $goal_format ) { |
| 1241 |
case 'donation': |
| 1242 |
$closed = $form->get_goal() <= $form->get_sales(); |
| 1243 |
break; |
| 1244 |
case 'donors': |
| 1245 |
$closed = $form->get_goal() <= give_get_form_donor_count( $this->ID ); |
| 1246 |
break; |
| 1247 |
default: |
| 1248 |
$closed = $form->get_goal() <= $form->get_earnings(); |
| 1249 |
break; |
| 1250 |
} |
| 1251 |
|
| 1252 |
if ( $closed ) { |
| 1253 |
$close_form = true; |
| 1254 |
} |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
return $close_form; |
| 1259 |
} |
| 1260 |
|
| 1261 |
} |
| 1262 |
|