class-give-forms-query.php
6 years ago
functions.php
1 year ago
template.php
9 months ago
widget.php
1 year ago
functions.php
1623 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Give Form Functions |
| 4 | * |
| 5 | * @package GiveWP |
| 6 | * @subpackage Includes/Forms |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.1 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | use Give\Helpers\Form\Utils as FormUtils; |
| 18 | |
| 19 | /** |
| 20 | * Filter: Do not show the Give shortcut button on Give Forms CPT |
| 21 | * |
| 22 | * @return bool |
| 23 | */ |
| 24 | function give_shortcode_button_condition() { |
| 25 | |
| 26 | global $typenow; |
| 27 | |
| 28 | if ( $typenow != 'give_forms' ) { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | add_filter( 'give_shortcode_button_condition', 'give_shortcode_button_condition' ); |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Get the form ID from the form $args |
| 40 | * |
| 41 | * @param array $args |
| 42 | * |
| 43 | * @return int|false |
| 44 | */ |
| 45 | function get_form_id_from_args( $args ) { |
| 46 | |
| 47 | if ( isset( $args['form_id'] ) && $args['form_id'] != 0 ) { |
| 48 | |
| 49 | return intval( $args['form_id'] ); |
| 50 | } |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks whether floating labels is enabled for the form ID in $args |
| 57 | * |
| 58 | * @since 1.1 |
| 59 | * |
| 60 | * @param array $args |
| 61 | * |
| 62 | * @return bool |
| 63 | */ |
| 64 | function give_is_float_labels_enabled( $args ) { |
| 65 | |
| 66 | $float_labels = ''; |
| 67 | |
| 68 | if ( ! empty( $args['float_labels'] ) ) { |
| 69 | $float_labels = $args['float_labels']; |
| 70 | } |
| 71 | |
| 72 | if ( empty( $float_labels ) ) { |
| 73 | $float_labels = give_get_meta( $args['form_id'], '_give_form_floating_labels', true ); |
| 74 | } |
| 75 | |
| 76 | if ( empty( $float_labels ) || ( 'global' === $float_labels ) ) { |
| 77 | $float_labels = give_get_option( 'floatlabels', 'disabled' ); |
| 78 | } |
| 79 | |
| 80 | // If the form is using a non-legacy form template, do not use floating labels |
| 81 | if ( ! FormUtils::isLegacyForm( $args['form_id'] ) ) { |
| 82 | $float_labels = 'disabled'; |
| 83 | } |
| 84 | |
| 85 | return give_is_setting_enabled( $float_labels ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Determines if a user can checkout or not |
| 90 | * |
| 91 | * Allows themes and plugins to set donation checkout conditions |
| 92 | * |
| 93 | * @since 1.0 |
| 94 | * |
| 95 | * @return bool Can user checkout? |
| 96 | */ |
| 97 | function give_can_checkout() { |
| 98 | |
| 99 | $can_checkout = true; |
| 100 | |
| 101 | return (bool) apply_filters( 'give_can_checkout', $can_checkout ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Retrieve the Success page URI |
| 106 | * |
| 107 | * @access public |
| 108 | * @since 1.0 |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | function give_get_success_page_uri() { |
| 113 | $give_options = give_get_settings(); |
| 114 | |
| 115 | $success_page = isset( $give_options['success_page'] ) |
| 116 | ? get_permalink( absint( $give_options['success_page'] ) ) |
| 117 | : get_bloginfo( 'url' ); |
| 118 | |
| 119 | return apply_filters( 'give_get_success_page_uri', $success_page ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Determines if we're currently on the Success page. |
| 124 | * |
| 125 | * @since 1.0 |
| 126 | * |
| 127 | * @return bool True if on the Success page, false otherwise. |
| 128 | */ |
| 129 | function give_is_success_page() { |
| 130 | $give_options = give_get_settings(); |
| 131 | |
| 132 | $success_page = isset( $give_options['success_page'] ) ? is_page( $give_options['success_page'] ) : false; |
| 133 | |
| 134 | return apply_filters( 'give_is_success_page', $success_page ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @since 4.0.0 |
| 139 | * |
| 140 | * @return bool |
| 141 | */ |
| 142 | function give_is_campaign_page() { |
| 143 | $currentId = get_the_ID(); |
| 144 | |
| 145 | return (bool)get_post_meta( $currentId, 'give_campaign_id', true ); |
| 146 | } |
| 147 | /** |
| 148 | * Send To Success Page |
| 149 | * |
| 150 | * Sends the user to the success page. |
| 151 | * |
| 152 | * @param string $query_string |
| 153 | * |
| 154 | * @access public |
| 155 | * @since 1.0 |
| 156 | * @return void |
| 157 | */ |
| 158 | function give_send_to_success_page( $query_string = null ) { |
| 159 | $redirect = give_get_success_page_uri(); |
| 160 | |
| 161 | if ( $query_string ) { |
| 162 | $redirect .= $query_string; |
| 163 | } |
| 164 | |
| 165 | $gateway = isset( $_REQUEST['give-gateway'] ) ? give_clean( $_REQUEST['give-gateway'] ) : ''; |
| 166 | |
| 167 | wp_redirect( apply_filters( 'give_success_page_redirect', $redirect, $gateway, $query_string ) ); |
| 168 | give_die(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Send back to donation form. |
| 174 | * |
| 175 | * Used to redirect a user back to the donation form if there are errors present. |
| 176 | * |
| 177 | * @param array|string $args |
| 178 | * |
| 179 | * @access public |
| 180 | * @since 2.21.0 Auto set "payment-mode" in redirect url. |
| 181 | * @since 1.0 |
| 182 | * @return Void |
| 183 | */ |
| 184 | function give_send_back_to_checkout( $args = [] ) { |
| 185 | |
| 186 | $url = isset( $_POST['give-current-url'] ) ? sanitize_text_field( $_POST['give-current-url'] ) : ''; |
| 187 | $form_id = 0; |
| 188 | $defaults = []; |
| 189 | |
| 190 | // Set the form_id. |
| 191 | if ( isset( $_POST['give-form-id'] ) ) { |
| 192 | $defaults['form-id'] = (int) sanitize_text_field( $_POST['give-form-id'] ); |
| 193 | } |
| 194 | |
| 195 | // Set the payment mode. |
| 196 | if ( isset( $_POST['payment-mode'] ) ) { |
| 197 | $defaults['payment-mode'] = sanitize_text_field( $_POST['payment-mode'] ); |
| 198 | } |
| 199 | |
| 200 | // Need a URL to continue. If none, redirect back to single form. |
| 201 | if ( empty( $url ) ) { |
| 202 | wp_safe_redirect( get_permalink( $form_id ) ); |
| 203 | give_die(); |
| 204 | } |
| 205 | |
| 206 | // Set the $level_id. |
| 207 | if ( isset( $_POST['give-price-id'] ) ) { |
| 208 | $defaults['level-id'] = sanitize_text_field( $_POST['give-price-id'] ); |
| 209 | |
| 210 | // If custom, set amount |
| 211 | if( 'custom' === $defaults[ 'level-id' ] ) { |
| 212 | $defaults['custom-amount'] = sanitize_text_field( $_POST['give-amount'] ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Check for backward compatibility. |
| 217 | if ( is_string( $args ) ) { |
| 218 | $args = str_replace( '?', '', $args ); |
| 219 | } |
| 220 | |
| 221 | $args = wp_parse_args( $args, $defaults ); |
| 222 | |
| 223 | // Merge URL query with $args to maintain third-party URL parameters after redirect. |
| 224 | $redirect = add_query_arg( $args, $url ); |
| 225 | |
| 226 | // Precaution: don't allow any CC info. |
| 227 | $redirect = remove_query_arg( [ 'card_number', 'card_cvc' ], $redirect ); |
| 228 | |
| 229 | // Redirect them. |
| 230 | $redirect .= "#give-form-{$form_id}-wrap"; |
| 231 | |
| 232 | /** |
| 233 | * Filter the redirect url |
| 234 | */ |
| 235 | wp_safe_redirect( esc_url_raw( apply_filters( 'give_send_back_to_checkout', $redirect, $args ) ) ); |
| 236 | |
| 237 | give_die(); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get Success Page URL |
| 242 | * |
| 243 | * Gets the success page URL. |
| 244 | * |
| 245 | * @param string $query_string |
| 246 | * |
| 247 | * @access public |
| 248 | * @since 1.0 |
| 249 | * @return string |
| 250 | */ |
| 251 | function give_get_success_page_url( $query_string = null ) { |
| 252 | $success_page = give_get_success_page_uri(); |
| 253 | |
| 254 | if ( $query_string ) { |
| 255 | $success_page .= $query_string; |
| 256 | } |
| 257 | |
| 258 | return apply_filters( 'give_success_page_url', $success_page ); |
| 259 | |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get the URL of the Failed Donation Page. |
| 264 | * |
| 265 | * @since 1.0 |
| 266 | * |
| 267 | * @param bool $extras Extras to append to the URL. |
| 268 | * |
| 269 | * @return mixed Full URL to the Failed Donation Page, if present, home page if it doesn't exist. |
| 270 | */ |
| 271 | function give_get_failed_transaction_uri( $extras = false ) { |
| 272 | $give_options = give_get_settings(); |
| 273 | |
| 274 | // Remove question mark. |
| 275 | if ( 0 === strpos( $extras, '?' ) ) { |
| 276 | $extras = substr( $extras, 1 ); |
| 277 | } |
| 278 | |
| 279 | $extras_args = wp_parse_args( $extras ); |
| 280 | |
| 281 | // Set nonce if payment id exist in extra params. |
| 282 | if ( array_key_exists( 'payment-id', $extras_args ) ) { |
| 283 | $extras_args['_wpnonce'] = wp_create_nonce( "give-failed-donation-{$extras_args['payment-id']}" ); |
| 284 | $extras = http_build_query( $extras_args ); |
| 285 | } |
| 286 | |
| 287 | $uri = ! empty( $give_options['failure_page'] ) ? |
| 288 | trailingslashit( get_permalink( $give_options['failure_page'] ) ) : |
| 289 | home_url(); |
| 290 | |
| 291 | if ( $extras ) { |
| 292 | $uri .= "?{$extras}"; |
| 293 | } |
| 294 | |
| 295 | return apply_filters( 'give_get_failed_transaction_uri', $uri ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Determines if we're currently on the Failed Donation Page. |
| 300 | * |
| 301 | * @since 1.0 |
| 302 | * @return bool True if on the Failed Donation Page, false otherwise. |
| 303 | */ |
| 304 | function give_is_failed_transaction_page() { |
| 305 | $give_options = give_get_settings(); |
| 306 | $ret = isset( $give_options['failure_page'] ) ? is_page( $give_options['failure_page'] ) : false; |
| 307 | |
| 308 | return apply_filters( 'give_is_failure_page', $ret ); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Mark payments as Failed when returning to the Failed Donation Page |
| 313 | * |
| 314 | * @since 1.0 |
| 315 | * @since 1.8.16 Add security check |
| 316 | * |
| 317 | * @return bool |
| 318 | */ |
| 319 | function give_listen_for_failed_payments() { |
| 320 | $payment_id = ! empty( $_GET['payment-id'] ) ? absint( $_GET['payment-id'] ) : 0; |
| 321 | $nonce = ! empty( $_GET['_wpnonce'] ) ? give_clean( $_GET['_wpnonce'] ) : false; |
| 322 | |
| 323 | // Bailout. |
| 324 | if ( ! $payment_id || ! wp_verify_nonce( $nonce, "give-failed-donation-{$payment_id}" ) ) { |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | // Set payment status to failure |
| 329 | give_update_payment_status( $payment_id, 'failed' ); |
| 330 | } |
| 331 | |
| 332 | add_action( 'template_redirect', 'give_listen_for_failed_payments', 0 ); |
| 333 | |
| 334 | /** |
| 335 | * Retrieve the Donation History page URI |
| 336 | * |
| 337 | * @access public |
| 338 | * @since 1.7 |
| 339 | * |
| 340 | * @return string |
| 341 | */ |
| 342 | function give_get_history_page_uri() { |
| 343 | $give_options = give_get_settings(); |
| 344 | |
| 345 | $history_page = isset( $give_options['history_page'] ) ? get_permalink( absint( $give_options['history_page'] ) ) : get_bloginfo( 'url' ); |
| 346 | |
| 347 | return apply_filters( 'give_get_history_page_uri', $history_page ); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Determines if we're currently on the History page. |
| 352 | * |
| 353 | * @since 1.0 |
| 354 | * |
| 355 | * @return bool True if on the History page, false otherwise. |
| 356 | */ |
| 357 | function give_is_history_page() { |
| 358 | $give_options = give_get_settings(); |
| 359 | |
| 360 | $history_page = isset( $give_options['history_page'] ) ? absint( $give_options['history_page'] ) : 0; |
| 361 | |
| 362 | return apply_filters( 'give_is_history_page', is_page( $history_page ) ); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Check if a field is required |
| 367 | * |
| 368 | * @param string $field |
| 369 | * @param int $form_id |
| 370 | * |
| 371 | * @access public |
| 372 | * @since 1.0 |
| 373 | * @return bool |
| 374 | */ |
| 375 | function give_field_is_required( $field, $form_id ) { |
| 376 | |
| 377 | $required_fields = give_get_required_fields( $form_id ); |
| 378 | |
| 379 | return array_key_exists( $field, $required_fields ); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Record Donation In Log |
| 384 | * |
| 385 | * Stores log information for a donation. |
| 386 | * |
| 387 | * @since 1.0 |
| 388 | * |
| 389 | * @param int $give_form_id Give Form ID. |
| 390 | * @param int $payment_id Payment ID. |
| 391 | * @param bool|int $price_id Price ID, if any. |
| 392 | * @param string|null $donation_date The date of the donation. |
| 393 | * |
| 394 | * @since 2.12.0 default value for the $give_form_id parameter is removed to prevent PHP8 warnings. |
| 395 | * |
| 396 | * @return void |
| 397 | */ |
| 398 | function give_record_donation_in_log( $give_form_id, $payment_id, $price_id = false, $donation_date = null ) { |
| 399 | $log_data = [ |
| 400 | 'log_content' => 'Payment log info', |
| 401 | 'log_parent' => $payment_id, |
| 402 | 'log_type' => 'sale', |
| 403 | 'log_date' => isset( $donation_date ) ? $donation_date : null, |
| 404 | 'log_date_gmt' => isset( $donation_date ) ? $donation_date : null, |
| 405 | ]; |
| 406 | |
| 407 | $log_meta = [ |
| 408 | 'form_id' => $give_form_id, |
| 409 | 'price_id' => (int) $price_id, |
| 410 | ]; |
| 411 | |
| 412 | Give()->logs->insert_log( $log_data, $log_meta ); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /** |
| 417 | * Increases the donation total count of a donation form. |
| 418 | * |
| 419 | * @since 1.0 |
| 420 | * |
| 421 | * @param int $form_id Give Form ID |
| 422 | * @param int $quantity Quantity to increase donation count by |
| 423 | * |
| 424 | * @return bool|int |
| 425 | */ |
| 426 | function give_increase_donation_count( $form_id = 0, $quantity = 1 ) { |
| 427 | $quantity = (int) $quantity; |
| 428 | |
| 429 | /** @var \Give_Donate_Form $form */ |
| 430 | $form = new Give_Donate_Form( $form_id ); |
| 431 | |
| 432 | return $form->increase_sales( $quantity ); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Update the goal progress count of a donation form. |
| 437 | * |
| 438 | * @since 2.4.0 |
| 439 | * |
| 440 | * @param int $form_id Give Form ID |
| 441 | * |
| 442 | * @return void |
| 443 | */ |
| 444 | function give_update_goal_progress( $form_id = 0 ) { |
| 445 | |
| 446 | // Get goal option meta key |
| 447 | $is_goal_enabled = give_is_setting_enabled( give_get_meta( $form_id, '_give_goal_option', true, 'disabled' ) ); |
| 448 | |
| 449 | // Check, if the form goal is enabled. |
| 450 | if ( $is_goal_enabled ) { |
| 451 | $goal_stats = give_goal_progress_stats( $form_id ); |
| 452 | $form_goal_progress_value = ! empty( $goal_stats['progress'] ) ? $goal_stats['progress'] : 0; |
| 453 | } else { |
| 454 | $form_goal_progress_value = -1; |
| 455 | } |
| 456 | |
| 457 | give_update_meta( $form_id, '_give_form_goal_progress', $form_goal_progress_value ); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Decreases the sale count of a form. Primarily for when a donation is refunded. |
| 462 | * |
| 463 | * @since 1.0 |
| 464 | * |
| 465 | * @param int $form_id Give Form ID |
| 466 | * @param int $quantity Quantity to increase donation count by |
| 467 | * |
| 468 | * @return bool|int |
| 469 | */ |
| 470 | function give_decrease_donation_count( $form_id = 0, $quantity = 1 ) { |
| 471 | $quantity = (int) $quantity; |
| 472 | |
| 473 | /** @var \Give_Donate_Form $form */ |
| 474 | $form = new Give_Donate_Form( $form_id ); |
| 475 | |
| 476 | return $form->decrease_sales( $quantity ); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Increases the total earnings of a form. |
| 481 | * |
| 482 | * @since 1.0 |
| 483 | * |
| 484 | * @since 2.1 Pass donation id. |
| 485 | * |
| 486 | * @param int $give_form_id Give Form ID |
| 487 | * @param int $amount Earnings |
| 488 | * @param int $payment_id Donation ID. |
| 489 | * |
| 490 | * @since 2.12.0 default value for the $give_form_id parameter is removed to prevent PHP8 warnings. |
| 491 | * |
| 492 | * @return bool|int |
| 493 | */ |
| 494 | function give_increase_earnings( $give_form_id, $amount, $payment_id = 0 ) { |
| 495 | /** @var \Give_Donate_Form $form */ |
| 496 | $form = new Give_Donate_Form( $give_form_id ); |
| 497 | |
| 498 | return $form->increase_earnings( $amount, $payment_id ); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Decreases the total earnings of a form. |
| 503 | * |
| 504 | * Primarily for when a donation is refunded. |
| 505 | * |
| 506 | * @since 1.0 |
| 507 | * |
| 508 | * @since 2.1 Pass donation id. |
| 509 | * |
| 510 | * @param int $form_id Give Form ID |
| 511 | * @param int $amount Earnings |
| 512 | * @param int $payment_id Donation ID. |
| 513 | * |
| 514 | * @since 2.12.0 default value for the $form_id parameter is removed to prevent PHP8 warnings. |
| 515 | * |
| 516 | * @return bool|int |
| 517 | */ |
| 518 | function give_decrease_form_earnings( $form_id, $amount, $payment_id = 0 ) { |
| 519 | /** @var \Give_Donate_Form $form */ |
| 520 | $form = new Give_Donate_Form( $form_id ); |
| 521 | |
| 522 | return $form->decrease_earnings( $amount, $payment_id ); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | /** |
| 527 | * Returns the total earnings for a form. |
| 528 | * |
| 529 | * @since 1.0 |
| 530 | * |
| 531 | * @param int $form_id Give Form ID |
| 532 | * |
| 533 | * @return int $earnings Earnings for a certain form |
| 534 | */ |
| 535 | function give_get_form_earnings_stats( $form_id = 0 ) { |
| 536 | $give_form = new Give_Donate_Form( $form_id ); |
| 537 | |
| 538 | /** |
| 539 | * Filter the form earnings |
| 540 | * |
| 541 | * @since 1.8.17 |
| 542 | */ |
| 543 | return apply_filters( 'give_get_form_earnings_stats', $give_form->earnings, $form_id, $give_form ); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /** |
| 548 | * Return the sales number for a form. |
| 549 | * |
| 550 | * @since 1.0 |
| 551 | * |
| 552 | * @param int $give_form_id Give Form ID |
| 553 | * |
| 554 | * @return int $sales Amount of sales for a certain form |
| 555 | */ |
| 556 | function give_get_form_sales_stats( $give_form_id = 0 ) { |
| 557 | $give_form = new Give_Donate_Form( $give_form_id ); |
| 558 | |
| 559 | return $give_form->sales; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | /** |
| 564 | * Retrieves the average monthly sales for a specific donation form |
| 565 | * |
| 566 | * @since 1.0 |
| 567 | * |
| 568 | * @param int $form_id Form ID |
| 569 | * |
| 570 | * @return float $sales Average monthly sales |
| 571 | */ |
| 572 | function give_get_average_monthly_form_sales( $form_id = 0 ) { |
| 573 | $sales = give_get_form_sales_stats( $form_id ); |
| 574 | $release_date = get_post_field( 'post_date', $form_id ); |
| 575 | |
| 576 | $diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) ); |
| 577 | |
| 578 | $months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication |
| 579 | |
| 580 | if ( $months > 0 ) { |
| 581 | $sales = ( $sales / $months ); |
| 582 | } |
| 583 | |
| 584 | return $sales; |
| 585 | } |
| 586 | |
| 587 | |
| 588 | /** |
| 589 | * Retrieves the average monthly earnings for a specific form |
| 590 | * |
| 591 | * @since 1.0 |
| 592 | * |
| 593 | * @param int $form_id Form ID |
| 594 | * |
| 595 | * @return float $earnings Average monthly earnings |
| 596 | */ |
| 597 | function give_get_average_monthly_form_earnings( $form_id = 0 ) { |
| 598 | $earnings = give_get_form_earnings_stats( $form_id ); |
| 599 | $release_date = get_post_field( 'post_date', $form_id ); |
| 600 | |
| 601 | $diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) ); |
| 602 | |
| 603 | $months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication |
| 604 | |
| 605 | if ( $months > 0 ) { |
| 606 | $earnings = ( $earnings / $months ); |
| 607 | } |
| 608 | |
| 609 | return $earnings < 0 ? 0 : $earnings; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | /** |
| 614 | * Get Price Option Name (Text) |
| 615 | * |
| 616 | * Retrieves the name of a variable price option. |
| 617 | * |
| 618 | * @since 1.0 |
| 619 | * |
| 620 | * @param int $form_id ID of the donation form. |
| 621 | * @param int $price_id ID of the price option. |
| 622 | * @param int $payment_id payment ID for use in filters ( optional ). |
| 623 | * @param bool $use_fallback Outputs the level amount if no level text is provided. |
| 624 | * |
| 625 | * @return string $price_name Name of the price option |
| 626 | */ |
| 627 | function give_get_price_option_name( $form_id = 0, $price_id = 0, $payment_id = 0, $use_fallback = true ) { |
| 628 | |
| 629 | $prices = give_get_variable_prices( $form_id ); |
| 630 | $price_name = ''; |
| 631 | |
| 632 | if ( ! $prices ) { |
| 633 | return $price_name; |
| 634 | } |
| 635 | |
| 636 | foreach ( $prices as $price ) { |
| 637 | |
| 638 | if ( intval( $price['_give_id']['level_id'] ) === intval( $price_id ) ) { |
| 639 | |
| 640 | $price_text = apply_filters( 'give_form_level_text', isset( $price['_give_text'] ) ? $price['_give_text'] : '', $form_id, $price ); |
| 641 | $price_fallback = $use_fallback ? |
| 642 | give_currency_filter( |
| 643 | give_format_amount( |
| 644 | $price['_give_amount'], |
| 645 | [ 'sanitize' => false ] |
| 646 | ), |
| 647 | [ 'decode_currency' => true ] |
| 648 | ) : ''; |
| 649 | $price_name = ! empty( $price_text ) ? $price_text : $price_fallback; |
| 650 | |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | return apply_filters( 'give_get_price_option_name', $price_name, $form_id, $payment_id, $price_id ); |
| 655 | } |
| 656 | |
| 657 | |
| 658 | /** |
| 659 | * Retrieves a price from from low to high of a variable priced form |
| 660 | * |
| 661 | * @since 1.0 |
| 662 | * |
| 663 | * @param int $form_id ID of the form |
| 664 | * @param bool $formatted Flag to decide which type of price range string return |
| 665 | * |
| 666 | * @return string $range A fully formatted price range |
| 667 | */ |
| 668 | function give_price_range( $form_id = 0, $formatted = true ) { |
| 669 | $low = give_get_lowest_price_option( $form_id ); |
| 670 | $high = give_get_highest_price_option( $form_id ); |
| 671 | $order_type = ! empty( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'asc'; |
| 672 | |
| 673 | $range = sprintf( |
| 674 | '<span class="give_price_range_%1$s">%2$s</span><span class="give_price_range_sep"> – </span><span class="give_price_range_%3$s">%4$s</span>', |
| 675 | 'asc' === $order_type ? 'low' : 'high', |
| 676 | 'asc' === $order_type ? give_currency_filter( give_format_amount( $low, [ 'sanitize' => false ] ) ) : give_currency_filter( give_format_amount( $high, [ 'sanitize' => false ] ) ), |
| 677 | 'asc' === $order_type ? 'high' : 'low', |
| 678 | 'asc' === $order_type ? give_currency_filter( give_format_amount( $high, [ 'sanitize' => false ] ) ) : give_currency_filter( give_format_amount( $low, [ 'sanitize' => false ] ) ) |
| 679 | ); |
| 680 | |
| 681 | if ( ! $formatted ) { |
| 682 | $range = wp_strip_all_tags( $range ); |
| 683 | } |
| 684 | |
| 685 | return apply_filters( 'give_price_range', $range, $form_id, $low, $high ); |
| 686 | } |
| 687 | |
| 688 | |
| 689 | /** |
| 690 | * Get Lowest Price ID |
| 691 | * |
| 692 | * Retrieves the ID for the cheapest price option of a variable donation form |
| 693 | * |
| 694 | * @since 1.5 |
| 695 | * |
| 696 | * @param int $form_id ID of the donation |
| 697 | * |
| 698 | * @return int ID of the lowest price |
| 699 | */ |
| 700 | function give_get_lowest_price_id( $form_id = 0 ) { |
| 701 | |
| 702 | if ( empty( $form_id ) ) { |
| 703 | $form_id = get_the_ID(); |
| 704 | } |
| 705 | |
| 706 | if ( ! give_has_variable_prices( $form_id ) ) { |
| 707 | return give_get_form_price( $form_id ); |
| 708 | } |
| 709 | |
| 710 | $prices = give_get_variable_prices( $form_id ); |
| 711 | |
| 712 | $min = $min_id = 0; |
| 713 | |
| 714 | if ( ! empty( $prices ) ) { |
| 715 | |
| 716 | foreach ( $prices as $key => $price ) { |
| 717 | |
| 718 | if ( empty( $price['_give_amount'] ) ) { |
| 719 | continue; |
| 720 | } |
| 721 | |
| 722 | if ( ! isset( $min ) ) { |
| 723 | $min = $price['_give_amount']; |
| 724 | } else { |
| 725 | $min = min( $min, $price['_give_amount'] ); |
| 726 | } |
| 727 | |
| 728 | if ( $price['_give_amount'] == $min ) { |
| 729 | $min_id = $price['_give_id']['level_id']; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return (int) $min_id; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Retrieves cheapest price option of a variable priced form |
| 739 | * |
| 740 | * @since 1.0 |
| 741 | * |
| 742 | * @param int $form_id ID of the form |
| 743 | * |
| 744 | * @return float Amount of the lowest price |
| 745 | */ |
| 746 | function give_get_lowest_price_option( $form_id = 0 ) { |
| 747 | if ( empty( $form_id ) ) { |
| 748 | $form_id = get_the_ID(); |
| 749 | } |
| 750 | |
| 751 | if ( ! give_has_variable_prices( $form_id ) ) { |
| 752 | return give_get_form_price( $form_id ); |
| 753 | } |
| 754 | |
| 755 | if ( ! ( $low = get_post_meta( $form_id, '_give_levels_minimum_amount', true ) ) ) { |
| 756 | // Backward compatibility. |
| 757 | $prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' ); |
| 758 | $low = ! empty( $prices ) ? min( $prices ) : 0; |
| 759 | } |
| 760 | |
| 761 | return give_maybe_sanitize_amount( $low ); |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Retrieves most expensive price option of a variable priced form |
| 766 | * |
| 767 | * @since 1.0 |
| 768 | * |
| 769 | * @param int $form_id ID of the form |
| 770 | * |
| 771 | * @return float Amount of the highest price |
| 772 | */ |
| 773 | function give_get_highest_price_option( $form_id = 0 ) { |
| 774 | |
| 775 | if ( empty( $form_id ) ) { |
| 776 | $form_id = get_the_ID(); |
| 777 | } |
| 778 | |
| 779 | if ( ! give_has_variable_prices( $form_id ) ) { |
| 780 | return give_get_form_price( $form_id ); |
| 781 | } |
| 782 | |
| 783 | if ( ! ( $high = get_post_meta( $form_id, '_give_levels_maximum_amount', true ) ) ) { |
| 784 | // Backward compatibility. |
| 785 | $prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' ); |
| 786 | $high = ! empty( $prices ) ? max( $prices ) : 0; |
| 787 | } |
| 788 | |
| 789 | return give_maybe_sanitize_amount( $high ); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Returns the price of a form, but only for non-variable priced forms. |
| 794 | * |
| 795 | * @since 1.0 |
| 796 | * |
| 797 | * @param int $form_id ID number of the form to retrieve a price for |
| 798 | * |
| 799 | * @return mixed string|int Price of the form |
| 800 | */ |
| 801 | function give_get_form_price( $form_id = 0 ) { |
| 802 | |
| 803 | if ( empty( $form_id ) ) { |
| 804 | return false; |
| 805 | } |
| 806 | |
| 807 | $form = new Give_Donate_Form( $form_id ); |
| 808 | |
| 809 | return $form->__get( 'price' ); |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Returns the minimum price amount of a form, only enforced for the custom amount input. |
| 814 | * |
| 815 | * @since 1.3.6 |
| 816 | * |
| 817 | * @param int $form_id ID number of the form to retrieve the minimum price for |
| 818 | * |
| 819 | * @return mixed string|int Minimum price of the form |
| 820 | */ |
| 821 | function give_get_form_minimum_price( $form_id = 0 ) { |
| 822 | |
| 823 | if ( empty( $form_id ) ) { |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | $form = new Give_Donate_Form( $form_id ); |
| 828 | |
| 829 | return $form->get_minimum_price(); |
| 830 | |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Return the maximum price amount of form. |
| 835 | * |
| 836 | * @since 2.1 |
| 837 | * |
| 838 | * @param int $form_id Donate Form ID |
| 839 | * |
| 840 | * @return bool|float |
| 841 | */ |
| 842 | function give_get_form_maximum_price( $form_id = 0 ) { |
| 843 | |
| 844 | if ( empty( $form_id ) ) { |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | $form = new Give_Donate_Form( $form_id ); |
| 849 | |
| 850 | return $form->get_maximum_price(); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Displays a formatted price for a donation form |
| 855 | * |
| 856 | * @since 1.0 |
| 857 | * |
| 858 | * @param int $form_id ID of the form price to show |
| 859 | * @param bool $echo Whether to echo or return the results |
| 860 | * @param bool|int $price_id Optional price id for variable pricing |
| 861 | * |
| 862 | * @return int $formatted_price |
| 863 | */ |
| 864 | function give_price( $form_id = 0, $echo = true, $price_id = false ) { |
| 865 | $price = 0; |
| 866 | |
| 867 | if ( empty( $form_id ) ) { |
| 868 | $form_id = get_the_ID(); |
| 869 | } |
| 870 | |
| 871 | if ( give_has_variable_prices( $form_id ) ) { |
| 872 | |
| 873 | $prices = give_get_variable_prices( $form_id ); |
| 874 | |
| 875 | if ( false !== $price_id ) { |
| 876 | |
| 877 | // loop through multi-prices to see which is default |
| 878 | foreach ( $prices as $price ) { |
| 879 | // this is the default price |
| 880 | if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) { |
| 881 | $price = (float) $price['_give_amount']; |
| 882 | }; |
| 883 | } |
| 884 | } else { |
| 885 | |
| 886 | $price = give_get_lowest_price_option( $form_id ); |
| 887 | } |
| 888 | } else { |
| 889 | |
| 890 | $price = give_get_form_price( $form_id ); |
| 891 | } |
| 892 | |
| 893 | $price = apply_filters( 'give_form_price', give_maybe_sanitize_amount( $price ), $form_id ); |
| 894 | $formatted_price = '<span class="give_price" id="give_price_' . $form_id . '">' . $price . '</span>'; |
| 895 | $formatted_price = apply_filters( 'give_form_price_after_html', $formatted_price, $form_id, $price ); |
| 896 | |
| 897 | if ( $echo ) { |
| 898 | echo $formatted_price; |
| 899 | } else { |
| 900 | return $formatted_price; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | add_filter( 'give_form_price', 'give_format_amount', 10 ); |
| 905 | add_filter( 'give_form_price', 'give_currency_filter', 20 ); |
| 906 | |
| 907 | |
| 908 | /** |
| 909 | * Retrieves the amount of a variable price option |
| 910 | * |
| 911 | * @since 1.0 |
| 912 | * |
| 913 | * @param int $form_id ID of the form |
| 914 | * @param int $price_id ID of the price option |
| 915 | * |
| 916 | * @return float $amount Amount of the price option |
| 917 | */ |
| 918 | function give_get_price_option_amount( $form_id = 0, $price_id = 0 ) { |
| 919 | $prices = give_get_variable_prices( $form_id ); |
| 920 | |
| 921 | $amount = 0.00; |
| 922 | |
| 923 | foreach ( $prices as $price ) { |
| 924 | if ( isset( $price['_give_id']['level_id'] ) && $price['_give_id']['level_id'] == $price_id ) { |
| 925 | $amount = isset( $price['_give_amount'] ) ? $price['_give_amount'] : 0.00; |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Filter the price amount |
| 932 | * |
| 933 | * @since 1.0 |
| 934 | */ |
| 935 | return apply_filters( |
| 936 | 'give_get_price_option_amount', |
| 937 | give_maybe_sanitize_amount( $amount, [ 'currency' => give_get_currency( $form_id ) ] ), |
| 938 | $form_id, |
| 939 | $price_id |
| 940 | ); |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * Returns the goal of a form |
| 945 | * |
| 946 | * @since 1.0 |
| 947 | * |
| 948 | * @param int $form_id ID number of the form to retrieve a goal for |
| 949 | * |
| 950 | * @return mixed string|int Goal of the form |
| 951 | */ |
| 952 | function give_get_form_goal( $form_id = 0 ) { |
| 953 | |
| 954 | if ( empty( $form_id ) ) { |
| 955 | return false; |
| 956 | } |
| 957 | |
| 958 | $form = new Give_Donate_Form( $form_id ); |
| 959 | |
| 960 | return $form->goal; |
| 961 | |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Returns the goal format of a form |
| 966 | * |
| 967 | * @since 2.0 |
| 968 | * |
| 969 | * @param int $form_id ID number of the form to retrieve a goal for |
| 970 | * |
| 971 | * @return mixed string|int Goal of the form |
| 972 | */ |
| 973 | function give_get_form_goal_format( $form_id = 0 ) { |
| 974 | |
| 975 | if ( empty( $form_id ) ) { |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | return give_get_meta( $form_id, '_give_goal_format', true ); |
| 980 | |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Display/Return a formatted goal for a donation form |
| 985 | * |
| 986 | * @since 1.0 |
| 987 | * |
| 988 | * @param int $form_id ID of the form price to show |
| 989 | * @param bool $echo Whether to echo or return the results |
| 990 | * |
| 991 | * @return string $formatted_goal |
| 992 | */ |
| 993 | function give_goal( $form_id = 0, $echo = true ) { |
| 994 | |
| 995 | if ( empty( $form_id ) ) { |
| 996 | $form_id = get_the_ID(); |
| 997 | } |
| 998 | |
| 999 | $goal = give_get_form_goal( $form_id ); |
| 1000 | $goal_format = give_get_form_goal_format( $form_id ); |
| 1001 | |
| 1002 | if ( 'donation' === $goal_format ) { |
| 1003 | $goal = "{$goal} donations"; |
| 1004 | } else { |
| 1005 | $goal = apply_filters( 'give_form_goal', give_maybe_sanitize_amount( $goal ), $form_id ); |
| 1006 | } |
| 1007 | |
| 1008 | $formatted_goal = sprintf( |
| 1009 | '<span class="give_price" id="give_price_%1$s">%2$s</span>', |
| 1010 | $form_id, |
| 1011 | $goal |
| 1012 | ); |
| 1013 | $formatted_goal = apply_filters( 'give_form_price_after_html', $formatted_goal, $form_id, $goal ); |
| 1014 | |
| 1015 | if ( $echo ) { |
| 1016 | echo $formatted_goal; |
| 1017 | } else { |
| 1018 | return $formatted_goal; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | add_filter( 'give_form_goal', 'give_format_amount', 10 ); |
| 1023 | add_filter( 'give_form_goal', 'give_currency_filter', 20 ); |
| 1024 | |
| 1025 | |
| 1026 | /** |
| 1027 | * Checks if users can only donate when logged in |
| 1028 | * |
| 1029 | * @since 1.0 |
| 1030 | * |
| 1031 | * @param int $form_id Give form ID |
| 1032 | * |
| 1033 | * @return bool $ret Whether or not the logged_in_only setting is set |
| 1034 | */ |
| 1035 | function give_logged_in_only( $form_id ) { |
| 1036 | // If _give_logged_in_only is set to enable then guest can donate from that specific form. |
| 1037 | // Otherwise it is member only donation form. |
| 1038 | $val = give_get_meta( $form_id, '_give_logged_in_only', true ); |
| 1039 | $val = ! empty( $val ) ? $val : 'enabled'; |
| 1040 | |
| 1041 | $ret = ! give_is_setting_enabled( $val ); |
| 1042 | |
| 1043 | return (bool) apply_filters( 'give_logged_in_only', $ret, $form_id ); |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | /** |
| 1048 | * Checks the option for the "Register / Login Option" |
| 1049 | * |
| 1050 | * @since 1.4.1 |
| 1051 | * |
| 1052 | * @param int $form_id |
| 1053 | * |
| 1054 | * @return string |
| 1055 | */ |
| 1056 | function give_show_login_register_option( $form_id ) { |
| 1057 | |
| 1058 | $show_register_form = give_get_meta( $form_id, '_give_show_register_form', true ); |
| 1059 | |
| 1060 | return apply_filters( 'give_show_register_form', $show_register_form, $form_id ); |
| 1061 | |
| 1062 | } |
| 1063 | |
| 1064 | |
| 1065 | /** |
| 1066 | * Get pre fill form field values. |
| 1067 | * |
| 1068 | * Note: this function will extract form field values from give_purchase session data. |
| 1069 | * |
| 1070 | * @since 1.8 |
| 1071 | * |
| 1072 | * @param int $form_id Form ID. |
| 1073 | * |
| 1074 | * @return array |
| 1075 | */ |
| 1076 | function _give_get_prefill_form_field_values( $form_id ) { |
| 1077 | $logged_in_donor_info = []; |
| 1078 | |
| 1079 | if ( is_user_logged_in() ) : |
| 1080 | $donor_data = get_userdata( get_current_user_id() ); |
| 1081 | $donor = new Give_Donor( get_current_user_id(), true ); |
| 1082 | $donor_address = $donor->get_donor_address(); |
| 1083 | $company_name = $donor->get_company_name(); |
| 1084 | |
| 1085 | $logged_in_donor_info = [ |
| 1086 | // First name. |
| 1087 | 'give_first' => $donor_data->first_name, |
| 1088 | |
| 1089 | // Last name. |
| 1090 | 'give_last' => $donor_data->last_name, |
| 1091 | |
| 1092 | // Title Prefix. |
| 1093 | 'give_title' => $donor->get_meta( '_give_donor_title_prefix', true ), |
| 1094 | |
| 1095 | // Company name. |
| 1096 | 'company_name' => $company_name, |
| 1097 | |
| 1098 | // Email. |
| 1099 | 'give_email' => $donor_data->user_email, |
| 1100 | |
| 1101 | // Street address 1. |
| 1102 | 'card_address' => $donor_address['line1'], |
| 1103 | |
| 1104 | // Street address 2. |
| 1105 | 'card_address_2' => $donor_address['line2'], |
| 1106 | |
| 1107 | // Country. |
| 1108 | 'billing_country' => $donor_address['country'], |
| 1109 | |
| 1110 | // State. |
| 1111 | 'card_state' => $donor_address['state'], |
| 1112 | |
| 1113 | // City. |
| 1114 | 'card_city' => $donor_address['city'], |
| 1115 | |
| 1116 | // Zipcode |
| 1117 | 'card_zip' => $donor_address['zip'], |
| 1118 | ]; |
| 1119 | endif; |
| 1120 | |
| 1121 | // Bailout: Auto fill form field values only form form which donor is donating. |
| 1122 | if ( |
| 1123 | empty( $_GET['form-id'] ) |
| 1124 | || ! $form_id |
| 1125 | || ( $form_id !== absint( $_GET['form-id'] ) ) |
| 1126 | ) { |
| 1127 | return $logged_in_donor_info; |
| 1128 | } |
| 1129 | |
| 1130 | // Get purchase data. |
| 1131 | $give_purchase_data = Give()->session->get( 'give_purchase' ); |
| 1132 | |
| 1133 | // Get donor info from form data. |
| 1134 | $give_donor_info_in_session = empty( $give_purchase_data['post_data'] ) |
| 1135 | ? [] |
| 1136 | : $give_purchase_data['post_data']; |
| 1137 | |
| 1138 | // Output. |
| 1139 | return wp_parse_args( $give_donor_info_in_session, $logged_in_donor_info ); |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * Get donor count of form |
| 1144 | * |
| 1145 | * @since 2.1.0 |
| 1146 | * |
| 1147 | * @param int $form_id |
| 1148 | * @param array $args |
| 1149 | * |
| 1150 | * @return int |
| 1151 | */ |
| 1152 | function give_get_form_donor_count( $form_id, $args = [] ) { |
| 1153 | global $wpdb; |
| 1154 | |
| 1155 | $cache_key = Give_Cache::get_key( "form_donor_count_{$form_id}", $args, false ); |
| 1156 | $donor_count = absint( Give_Cache::get_db_query( $cache_key ) ); |
| 1157 | |
| 1158 | if ( $form_id && ! $donor_count ) { |
| 1159 | // Set arguments. |
| 1160 | $args = wp_parse_args( |
| 1161 | $args, |
| 1162 | [ |
| 1163 | 'unique' => true, |
| 1164 | ] |
| 1165 | ); |
| 1166 | |
| 1167 | $donation_meta_table = Give()->payment_meta->table_name; |
| 1168 | $donation_id_col_name = Give()->payment_meta->get_meta_type() . '_id'; |
| 1169 | |
| 1170 | $distinct = $args['unique'] ? 'DISTINCT meta_value' : 'meta_value'; |
| 1171 | |
| 1172 | $query = $wpdb->prepare( |
| 1173 | " |
| 1174 | SELECT COUNT({$distinct}) |
| 1175 | FROM {$donation_meta_table} |
| 1176 | WHERE meta_key=%s |
| 1177 | AND {$donation_id_col_name} IN( |
| 1178 | SELECT {$donation_id_col_name} |
| 1179 | FROM {$donation_meta_table} as pm |
| 1180 | INNER JOIN {$wpdb->posts} as p |
| 1181 | ON pm.{$donation_id_col_name}=p.ID |
| 1182 | WHERE pm.meta_key=%s |
| 1183 | AND pm.meta_value=%s |
| 1184 | AND p.post_status=%s |
| 1185 | ) |
| 1186 | ", |
| 1187 | '_give_payment_donor_id', |
| 1188 | '_give_payment_form_id', |
| 1189 | $form_id, |
| 1190 | 'publish' |
| 1191 | ); |
| 1192 | |
| 1193 | $donor_count = absint( $wpdb->get_var( $query ) ); |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * Filter the donor count |
| 1198 | * |
| 1199 | * @since 2.1.0 |
| 1200 | */ |
| 1201 | $donor_count = apply_filters( 'give_get_form_donor_count', $donor_count, $form_id, $args ); |
| 1202 | |
| 1203 | return $donor_count; |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * Verify the form status. |
| 1208 | * |
| 1209 | * @param int $form_id Donation Form ID. |
| 1210 | * |
| 1211 | * @since 2.1 |
| 1212 | * |
| 1213 | * @return void |
| 1214 | */ |
| 1215 | function give_set_form_closed_status( $form_id ) { |
| 1216 | |
| 1217 | // Bailout. |
| 1218 | if ( empty( $form_id ) ) { |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | $open_form = false; |
| 1223 | $is_goal_enabled = give_is_setting_enabled( give_get_meta( $form_id, '_give_goal_option', true, 'disabled' ) ); |
| 1224 | |
| 1225 | // Proceed, if the form goal is enabled. |
| 1226 | if ( $is_goal_enabled ) { |
| 1227 | |
| 1228 | $close_form_when_goal_achieved = give_is_setting_enabled( give_get_meta( $form_id, '_give_close_form_when_goal_achieved', true, 'disabled' ) ); |
| 1229 | |
| 1230 | // Proceed, if close form when goal achieved option is enabled. |
| 1231 | if ( $close_form_when_goal_achieved ) { |
| 1232 | |
| 1233 | $form = new Give_Donate_Form( $form_id ); |
| 1234 | $goal_progress_stats = give_goal_progress_stats( $form ); |
| 1235 | |
| 1236 | // Verify whether the form is closed or not after processing data. |
| 1237 | $closed = $goal_progress_stats['raw_goal'] <= $goal_progress_stats['raw_actual']; |
| 1238 | |
| 1239 | // Update form meta if verified that the form is closed. |
| 1240 | if ( $closed ) { |
| 1241 | give_update_meta( $form_id, '_give_form_status', 'closed' ); |
| 1242 | } else { |
| 1243 | $open_form = true; |
| 1244 | } |
| 1245 | } else { |
| 1246 | $open_form = true; |
| 1247 | } |
| 1248 | } else { |
| 1249 | $open_form = true; |
| 1250 | } |
| 1251 | |
| 1252 | // If $open_form is true, then update form status to open. |
| 1253 | if ( $open_form ) { |
| 1254 | give_update_meta( $form_id, '_give_form_status', 'open' ); |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * Show Form Goal Stats in Admin ( Listing and Detail page ) |
| 1260 | * |
| 1261 | * @since 3.16.0 Remove "give_donate_form_get_sales" filter logic |
| 1262 | * @since 3.14.0 Use the "give_get_form_earnings_stats" filter to ensure the correct value will be displayed in the form progress bar |
| 1263 | * @since 2.19.0 Prevent divide by zero issue in goal percentage calculation logic. |
| 1264 | * |
| 1265 | * @since 2.1.0 |
| 1266 | * |
| 1267 | * @param int $form_id Form ID. |
| 1268 | * |
| 1269 | * @return string |
| 1270 | */ |
| 1271 | function give_admin_form_goal_stats( $form_id ) { |
| 1272 | $html = ''; |
| 1273 | $goal_stats = give_goal_progress_stats( $form_id ); |
| 1274 | $percent_complete = $goal_stats['raw_goal'] && is_numeric($goal_stats['raw_actual']) && is_numeric($goal_stats['raw_goal']) |
| 1275 | ? round(($goal_stats['raw_actual'] / $goal_stats['raw_goal']), 3) * 100 |
| 1276 | : 0; |
| 1277 | |
| 1278 | $html .= sprintf( |
| 1279 | '<div class="give-admin-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="%1$s"> |
| 1280 | <span style="width:%1$s%%;"></span> |
| 1281 | </div>', |
| 1282 | esc_attr( $goal_stats['progress'] ) |
| 1283 | ); |
| 1284 | |
| 1285 | $html .= sprintf( |
| 1286 | ( 'percentage' !== $goal_stats['format'] ) ? |
| 1287 | '<div class="give-goal-text"><span>%1$s</span> %2$s <a href="%3$s">%4$s</a> %5$s ' : |
| 1288 | '<div class="give-goal-text"><a href="%3$s">%1$s </a>', |
| 1289 | ( 'percentage' !== $goal_stats['format'] ) ? $goal_stats['actual'] : $percent_complete . '%', |
| 1290 | ( 'percentage' !== $goal_stats['format'] ) ? __( 'of', 'give' ) : '', |
| 1291 | esc_url( admin_url( "post.php?post={$form_id}&action=edit&give_tab=donation_goal_options" ) ), |
| 1292 | $goal_stats['goal'], |
| 1293 | ( 'donors' === $goal_stats['format'] ? __( 'donors', 'give' ) : ( 'donation' === $goal_stats['format'] ? __( 'donations', 'give' ) : '' ) ) |
| 1294 | ); |
| 1295 | |
| 1296 | $opacity = $goal_stats['raw_actual'] >= $goal_stats['raw_goal'] ? 1 : 0; |
| 1297 | $html .= sprintf( |
| 1298 | '<span style="opacity:%s" class="give-admin-goal-achieved"><span class="dashicons dashicons-star-filled"></span> %s</span>', |
| 1299 | apply_filters('give_admin_goal_progress_achieved_opacity', $opacity), |
| 1300 | __('Goal achieved', 'give') |
| 1301 | ); |
| 1302 | |
| 1303 | $html .= '</div>'; |
| 1304 | |
| 1305 | return $html; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * Get the default donation form's level id. |
| 1310 | * |
| 1311 | * @since 2.2.0 |
| 1312 | * |
| 1313 | * @param integer $form_id Donation Form ID. |
| 1314 | * |
| 1315 | * @return null | array |
| 1316 | */ |
| 1317 | function give_form_get_default_level( $form_id ) { |
| 1318 | $default_level = null; |
| 1319 | |
| 1320 | // If donation form has variable prices. |
| 1321 | if ( give_has_variable_prices( $form_id ) ) { |
| 1322 | /** |
| 1323 | * Filter the variable pricing |
| 1324 | * |
| 1325 | * @since 1.0 |
| 1326 | * @deprecated 2.2 Use give_get_donation_levels filter instead of give_form_variable_prices. |
| 1327 | * Check Give_Donate_Form::get_prices(). |
| 1328 | * |
| 1329 | * @param array $prices Array of variable prices. |
| 1330 | * @param int $form Form ID. |
| 1331 | */ |
| 1332 | $prices = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id ); |
| 1333 | |
| 1334 | // Go through each of the level and get the default level id. |
| 1335 | foreach ( $prices as $level ) { |
| 1336 | if ( |
| 1337 | isset( $level['_give_default'] ) |
| 1338 | && $level['_give_default'] === 'default' |
| 1339 | ) { |
| 1340 | $default_level = $level; |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /** |
| 1346 | * Filter the default donation level id. |
| 1347 | * |
| 1348 | * @since 2.2.0 |
| 1349 | * |
| 1350 | * @param array $default_level Default level price data. |
| 1351 | * @param integer $form_id Donation form ID. |
| 1352 | */ |
| 1353 | return apply_filters( 'give_form_get_default_level', $default_level, $form_id ); |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * Get the default level id. |
| 1358 | * |
| 1359 | * @since 2.2.0 |
| 1360 | * |
| 1361 | * @param array|integer $price_or_level_id Price level data. |
| 1362 | * @param boolean|integer $form_id Donation Form ID. |
| 1363 | * |
| 1364 | * @return boolean |
| 1365 | */ |
| 1366 | function give_is_default_level_id( $price_or_level_id, $form_id = 0 ) { |
| 1367 | $is_default = false; |
| 1368 | |
| 1369 | if ( |
| 1370 | ! empty( $form_id ) |
| 1371 | && is_numeric( $price_or_level_id ) |
| 1372 | ) { |
| 1373 | // Get default level id. |
| 1374 | $form_price_data = give_form_get_default_level( $form_id ); |
| 1375 | |
| 1376 | $is_default = ! is_null( $form_price_data ) && ( $price_or_level_id === absint( $form_price_data['_give_id']['level_id'] ) ); |
| 1377 | } |
| 1378 | |
| 1379 | $is_default = false === $is_default && is_array( $price_or_level_id ) ? |
| 1380 | ( isset( $price_or_level_id['_give_default'] ) && $price_or_level_id['_give_default'] === 'default' ) |
| 1381 | : $is_default; |
| 1382 | |
| 1383 | /** |
| 1384 | * Allow developers to modify the default level id checks. |
| 1385 | * |
| 1386 | * @since 2.2.0 |
| 1387 | * |
| 1388 | * @param bool $is_default True if it is default price level id otherwise false. |
| 1389 | * @param array|integer $price_or_level_id Price Data. |
| 1390 | */ |
| 1391 | return apply_filters( 'give_is_default_level_id', $is_default, $price_or_level_id ); |
| 1392 | } |
| 1393 | |
| 1394 | |
| 1395 | /** |
| 1396 | * Get Name Title Prefixes (a.k.a. Salutation) value. |
| 1397 | * |
| 1398 | * @param int $form_id Donation Form ID. |
| 1399 | * |
| 1400 | * @since 2.2.0 |
| 1401 | * |
| 1402 | * @return array |
| 1403 | */ |
| 1404 | function give_get_name_title_prefixes( $form_id = 0 ) { |
| 1405 | |
| 1406 | $name_title_prefix = give_is_name_title_prefix_enabled( $form_id ); |
| 1407 | $title_prefixes = give_get_option( 'title_prefixes', give_get_default_title_prefixes() ); |
| 1408 | |
| 1409 | // If form id exists, then fetch form specific title prefixes. |
| 1410 | if ( intval( $form_id ) > 0 && $name_title_prefix ) { |
| 1411 | |
| 1412 | $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true ); |
| 1413 | if ( 'global' !== $form_title_prefix ) { |
| 1414 | $form_title_prefixes = give_get_meta( $form_id, '_give_title_prefixes', true, give_get_default_title_prefixes() ); |
| 1415 | |
| 1416 | // Check whether the form based title prefixes exists or not. |
| 1417 | if ( is_array( $form_title_prefixes ) && count( $form_title_prefixes ) > 0 ) { |
| 1418 | $title_prefixes = $form_title_prefixes; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | return array_filter( (array) $title_prefixes ); |
| 1424 | } |
| 1425 | |
| 1426 | /** |
| 1427 | * Check whether the name title prefix is enabled or not. |
| 1428 | * |
| 1429 | * @param int $form_id Donation Form ID. |
| 1430 | * @param string $status Status to set status based on option value. |
| 1431 | * |
| 1432 | * @since 2.2.0 |
| 1433 | * |
| 1434 | * @return bool |
| 1435 | */ |
| 1436 | function give_is_name_title_prefix_enabled( $form_id = 0, $status = '' ) { |
| 1437 | if ( empty( $status ) ) { |
| 1438 | $status = [ 'required', 'optional' ]; |
| 1439 | } else { |
| 1440 | $status = [ $status ]; |
| 1441 | } |
| 1442 | |
| 1443 | $title_prefix_status = give_is_setting_enabled( give_get_option( 'name_title_prefix' ), $status ); |
| 1444 | |
| 1445 | if ( intval( $form_id ) > 0 ) { |
| 1446 | $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true ); |
| 1447 | |
| 1448 | if ( 'disabled' === $form_title_prefix ) { |
| 1449 | $title_prefix_status = false; |
| 1450 | } elseif ( in_array( $form_title_prefix, $status, true ) ) { |
| 1451 | $title_prefix_status = give_is_setting_enabled( $form_title_prefix, $status ); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | return $title_prefix_status; |
| 1456 | |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * Get Donor Name with Title Prefix |
| 1461 | * |
| 1462 | * @param int|Give_Donor $donor Donor Information. |
| 1463 | * |
| 1464 | * @since 2.2.0 |
| 1465 | * |
| 1466 | * @return object |
| 1467 | */ |
| 1468 | function give_get_name_with_title_prefixes( $donor ) { |
| 1469 | |
| 1470 | // Prepare Give_Donor object, if $donor is numeric. |
| 1471 | if ( is_numeric( $donor ) ) { |
| 1472 | $donor = new Give_Donor( $donor ); |
| 1473 | } |
| 1474 | |
| 1475 | $title_prefix = Give()->donor_meta->get_meta( $donor->id, '_give_donor_title_prefix', true ); |
| 1476 | |
| 1477 | // Update Donor name, if non empty title prefix. |
| 1478 | if ( ! empty( $title_prefix ) ) { |
| 1479 | $donor->name = give_get_donor_name_with_title_prefixes( $title_prefix, $donor->name ); |
| 1480 | } |
| 1481 | |
| 1482 | return $donor; |
| 1483 | } |
| 1484 | |
| 1485 | /** |
| 1486 | * This function will generate donor name with title prefix if it is required. |
| 1487 | * |
| 1488 | * @param string $title_prefix Title Prefix of Donor |
| 1489 | * @param string $name Donor Name. |
| 1490 | * |
| 1491 | * @since 2.2.0 |
| 1492 | * |
| 1493 | * @return string |
| 1494 | */ |
| 1495 | function give_get_donor_name_with_title_prefixes( $title_prefix, $name ) { |
| 1496 | |
| 1497 | $donor_name = $name; |
| 1498 | |
| 1499 | if ( ! empty( $title_prefix ) && ! empty( $name ) ) { |
| 1500 | $donor_name = "{$title_prefix} {$name}"; |
| 1501 | } |
| 1502 | |
| 1503 | return trim( $donor_name ); |
| 1504 | } |
| 1505 | |
| 1506 | /** |
| 1507 | * This function will fetch the default list of title prefixes. |
| 1508 | * |
| 1509 | * @since 2.2.0 |
| 1510 | * |
| 1511 | * @return array |
| 1512 | */ |
| 1513 | function give_get_default_title_prefixes() { |
| 1514 | /** |
| 1515 | * Filter the data |
| 1516 | * Set default title prefixes. |
| 1517 | * |
| 1518 | * @since 2.2.0 |
| 1519 | */ |
| 1520 | return apply_filters( |
| 1521 | 'give_get_default_title_prefixes', |
| 1522 | [ |
| 1523 | 'Mr.' => __( 'Mr.', 'give' ), |
| 1524 | 'Mrs.' => __( 'Mrs.', 'give' ), |
| 1525 | 'Ms.' => __( 'Ms.', 'give' ), |
| 1526 | ] |
| 1527 | ); |
| 1528 | } |
| 1529 | |
| 1530 | /** |
| 1531 | * This function will check whether the name title prefix field is required or not. |
| 1532 | * |
| 1533 | * @param int $form_id Donation Form ID. |
| 1534 | * |
| 1535 | * @since 2.2.0 |
| 1536 | * |
| 1537 | * @return bool |
| 1538 | */ |
| 1539 | function give_is_name_title_prefix_required( $form_id = 0 ) { |
| 1540 | |
| 1541 | // Bail out, if name title prefix is not enabled. |
| 1542 | if ( ! give_is_name_title_prefix_enabled( $form_id ) ) { |
| 1543 | return false; |
| 1544 | } |
| 1545 | |
| 1546 | $status = [ 'optional' ]; |
| 1547 | $is_optional = give_is_setting_enabled( give_get_option( 'name_title_prefix' ), $status ); |
| 1548 | |
| 1549 | if ( intval( $form_id ) > 0 ) { |
| 1550 | $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true ); |
| 1551 | |
| 1552 | if ( 'required' === $form_title_prefix ) { |
| 1553 | $is_optional = false; |
| 1554 | } elseif ( 'optional' === $form_title_prefix ) { |
| 1555 | $is_optional = true; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | return ( ! $is_optional ); |
| 1560 | } |
| 1561 | |
| 1562 | /** |
| 1563 | * Deletes form meta when the form is permanently deleted from the trash. |
| 1564 | * |
| 1565 | * @since 2.3.0 |
| 1566 | * |
| 1567 | * @param integer $id Donation Form ID which needs to be deleted. |
| 1568 | * |
| 1569 | * @return void |
| 1570 | */ |
| 1571 | function give_handle_form_meta_on_delete( $id ) { |
| 1572 | |
| 1573 | global $wpdb; |
| 1574 | |
| 1575 | $form = get_post( $id ); |
| 1576 | $get_data = give_clean( $_GET ); |
| 1577 | |
| 1578 | if ( |
| 1579 | 'give_forms' === $form->post_type && |
| 1580 | 'trash' === $form->post_status && |
| 1581 | ( |
| 1582 | ( isset( $get_data['action'] ) && 'delete' === $get_data['action'] ) || |
| 1583 | ! empty( $get_data['delete_all'] ) |
| 1584 | ) |
| 1585 | ) { |
| 1586 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->formmeta} WHERE form_id = '%d'", $form->ID ) ); |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | add_action( 'before_delete_post', 'give_handle_form_meta_on_delete', 10, 1 ); |
| 1591 | |
| 1592 | |
| 1593 | /** |
| 1594 | * Get the list of default parameters for the form shortcode. |
| 1595 | * |
| 1596 | * @since 3.2.1 Revert default display style to "onpage". |
| 1597 | * @since 2.4.1 |
| 1598 | * |
| 1599 | * @return array |
| 1600 | */ |
| 1601 | function give_get_default_form_shortcode_args() { |
| 1602 | $default = [ |
| 1603 | 'id' => '', |
| 1604 | 'show_title' => true, |
| 1605 | 'show_goal' => true, |
| 1606 | 'show_content' => '', |
| 1607 | 'float_labels' => '', |
| 1608 | 'display_style' => '', |
| 1609 | 'continue_button_title' => '', |
| 1610 | |
| 1611 | // This attribute belong to form template functionality. |
| 1612 | // You can use this attribute to set modal open button background color. |
| 1613 | 'button_color' => '#28C77B', |
| 1614 | ]; |
| 1615 | |
| 1616 | /** |
| 1617 | * Fire the filter |
| 1618 | */ |
| 1619 | $default = apply_filters( 'give_get_default_form_shortcode_args', $default ); |
| 1620 | |
| 1621 | return $default; |
| 1622 | } |
| 1623 |