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