| 1 |
<?php |
| 2 |
/** |
| 3 |
* AJAX Functions |
| 4 |
* |
| 5 |
* Process the front-end AJAX actions. |
| 6 |
* |
| 7 |
* @package Give |
| 8 |
* @subpackage Functions/AJAX |
| 9 |
* @copyright Copyright (c) 2016, GiveWP |
| 10 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 |
* @since 1.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
use Give\Helpers\Form\Template as FormTemplateUtils; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Check if AJAX works as expected |
| 23 |
* Note: Do not use this function before init hook. |
| 24 |
* |
| 25 |
* @since 1.0 |
| 26 |
* |
| 27 |
* @param bool $force Flag to test ajax by discarding cache result |
| 28 |
* |
| 29 |
* @return bool True if AJAX works, false otherwise |
| 30 |
*/ |
| 31 |
function give_test_ajax_works( $force = false ) { |
| 32 |
// Handle ajax. |
| 33 |
if ( doing_action( 'wp_ajax_nopriv_give_test_ajax' ) ) { |
| 34 |
wp_die( 0, 200 ); |
| 35 |
} |
| 36 |
|
| 37 |
// Check if the Airplane Mode plugin is installed. |
| 38 |
if ( class_exists( 'Airplane_Mode_Core' ) ) { |
| 39 |
|
| 40 |
$airplane = Airplane_Mode_Core::getInstance(); |
| 41 |
|
| 42 |
if ( method_exists( $airplane, 'enabled' ) ) { |
| 43 |
|
| 44 |
if ( $airplane->enabled() ) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
} else { |
| 48 |
|
| 49 |
if ( 'on' === $airplane->check_status() ) { |
| 50 |
return true; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
add_filter( 'block_local_requests', '__return_false' ); |
| 56 |
|
| 57 |
$works = Give_Cache::get( '_give_ajax_works', true ); |
| 58 |
|
| 59 |
if ( ! $works || $force ) { |
| 60 |
$params = [ |
| 61 |
'sslverify' => false, |
| 62 |
'timeout' => 30, |
| 63 |
'body' => [ |
| 64 |
'action' => 'give_test_ajax', |
| 65 |
], |
| 66 |
]; |
| 67 |
|
| 68 |
$ajax = wp_remote_post( give_get_ajax_url(), $params ); |
| 69 |
|
| 70 |
$works = true; |
| 71 |
|
| 72 |
if ( is_wp_error( $ajax ) ) { |
| 73 |
|
| 74 |
$works = false; |
| 75 |
|
| 76 |
} else { |
| 77 |
|
| 78 |
if ( empty( $ajax['response'] ) ) { |
| 79 |
$works = false; |
| 80 |
} |
| 81 |
|
| 82 |
if ( empty( $ajax['response']['code'] ) || 200 !== (int) $ajax['response']['code'] ) { |
| 83 |
$works = false; |
| 84 |
} |
| 85 |
|
| 86 |
if ( empty( $ajax['response']['message'] ) || 'OK' !== $ajax['response']['message'] ) { |
| 87 |
$works = false; |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! isset( $ajax['body'] ) || 0 !== (int) $ajax['body'] ) { |
| 91 |
$works = false; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if ( $works ) { |
| 96 |
Give_Cache::set( '_give_ajax_works', '1', DAY_IN_SECONDS, true ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Filter the output |
| 102 |
* |
| 103 |
* @since 1.0 |
| 104 |
*/ |
| 105 |
return apply_filters( 'give_test_ajax_works', $works ); |
| 106 |
} |
| 107 |
|
| 108 |
add_action( 'wp_ajax_nopriv_give_test_ajax', 'give_test_ajax_works' ); |
| 109 |
|
| 110 |
/** |
| 111 |
* Get AJAX URL |
| 112 |
* |
| 113 |
* @since 1.0 |
| 114 |
* |
| 115 |
* @param array $query |
| 116 |
* |
| 117 |
* @return string |
| 118 |
*/ |
| 119 |
function give_get_ajax_url( $query = [] ) { |
| 120 |
$scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin'; |
| 121 |
|
| 122 |
$current_url = give_get_current_page_url(); |
| 123 |
$ajax_url = admin_url( 'admin-ajax.php', $scheme ); |
| 124 |
|
| 125 |
if ( preg_match( '/^https/', $current_url ) && ! preg_match( '/^https/', $ajax_url ) ) { |
| 126 |
$ajax_url = preg_replace( '/^http/', 'https', $ajax_url ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! empty( $query ) ) { |
| 130 |
$ajax_url = add_query_arg( $query, $ajax_url ); |
| 131 |
} |
| 132 |
|
| 133 |
return apply_filters( 'give_ajax_url', $ajax_url ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Loads Checkout Login Fields via AJAX |
| 138 |
* |
| 139 |
* @since 1.0 |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
function give_load_checkout_login_fields() { |
| 144 |
/** |
| 145 |
* Fire when render login fields via ajax. |
| 146 |
* |
| 147 |
* @since 1.7 |
| 148 |
*/ |
| 149 |
do_action( 'give_donation_form_login_fields' ); |
| 150 |
|
| 151 |
give_die(); |
| 152 |
} |
| 153 |
|
| 154 |
add_action( 'wp_ajax_nopriv_give_checkout_login', 'give_load_checkout_login_fields' ); |
| 155 |
|
| 156 |
/** |
| 157 |
* Load Checkout Fields |
| 158 |
* |
| 159 |
* @since 1.3.6 |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
function give_load_checkout_fields() { |
| 164 |
$form_id = isset( $_POST['form_id'] ) ? $_POST['form_id'] : ''; |
| 165 |
|
| 166 |
ob_start(); |
| 167 |
|
| 168 |
/** |
| 169 |
* Fire to render registration/login form. |
| 170 |
* |
| 171 |
* @since 1.7 |
| 172 |
*/ |
| 173 |
do_action( 'give_donation_form_register_login_fields', $form_id ); |
| 174 |
|
| 175 |
$fields = ob_get_clean(); |
| 176 |
|
| 177 |
wp_send_json( |
| 178 |
[ |
| 179 |
'fields' => wp_json_encode( $fields ), |
| 180 |
'submit' => wp_json_encode( give_get_donation_form_submit_button( $form_id ) ), |
| 181 |
] |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
add_action( 'wp_ajax_give_cancel_login', 'give_load_checkout_fields' ); |
| 186 |
add_action( 'wp_ajax_nopriv_give_cancel_login', 'give_load_checkout_fields' ); |
| 187 |
add_action( 'wp_ajax_nopriv_give_checkout_register', 'give_load_checkout_fields' ); |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Retrieve a states drop down |
| 192 |
* |
| 193 |
* @since 1.0 |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
function give_ajax_get_states_field() { |
| 198 |
$states_found = false; |
| 199 |
$show_field = true; |
| 200 |
$states_require = true; |
| 201 |
// Get the Country code from the $_POST. |
| 202 |
$country = sanitize_text_field( $_POST['country'] ); |
| 203 |
|
| 204 |
// Get the field name from the $_POST. |
| 205 |
$field_name = sanitize_text_field( $_POST['field_name'] ); |
| 206 |
|
| 207 |
$label = __( 'State', 'give' ); |
| 208 |
$states_label = give_get_states_label(); |
| 209 |
|
| 210 |
$default_state = ''; |
| 211 |
if ( give_get_country() === $country ) { |
| 212 |
$default_state = give_get_state(); |
| 213 |
} |
| 214 |
|
| 215 |
// Check if $country code exists in the array key for states label. |
| 216 |
if ( array_key_exists( $country, $states_label ) ) { |
| 217 |
$label = $states_label[ $country ]; |
| 218 |
} |
| 219 |
|
| 220 |
if ( empty( $country ) ) { |
| 221 |
$country = give_get_country(); |
| 222 |
} |
| 223 |
|
| 224 |
$states = give_get_states( $country ); |
| 225 |
if ( ! empty( $states ) ) { |
| 226 |
$args = [ |
| 227 |
'name' => $field_name, |
| 228 |
'id' => $field_name, |
| 229 |
'class' => $field_name . ' give-select', |
| 230 |
'options' => $states, |
| 231 |
'show_option_all' => false, |
| 232 |
'show_option_none' => false, |
| 233 |
'placeholder' => $label, |
| 234 |
'selected' => $default_state, |
| 235 |
'autocomplete' => 'address-level1', |
| 236 |
]; |
| 237 |
$data = Give()->html->select( $args ); |
| 238 |
$states_found = true; |
| 239 |
} else { |
| 240 |
$data = 'nostates'; |
| 241 |
|
| 242 |
// Get the country list that does not have any states init. |
| 243 |
$no_states_country = give_no_states_country_list(); |
| 244 |
|
| 245 |
// Check if $country code exists in the array key. |
| 246 |
if ( array_key_exists( $country, $no_states_country ) ) { |
| 247 |
$show_field = false; |
| 248 |
} |
| 249 |
|
| 250 |
// Get the country list that does not require states. |
| 251 |
$states_not_required_country_list = give_states_not_required_country_list(); |
| 252 |
|
| 253 |
// Check if $country code exists in the array key. |
| 254 |
if ( array_key_exists( $country, $states_not_required_country_list ) ) { |
| 255 |
$states_require = false; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
$response = [ |
| 260 |
'success' => true, |
| 261 |
'states_found' => $states_found, |
| 262 |
'states_label' => $label, |
| 263 |
'show_field' => $show_field, |
| 264 |
'states_require' => $states_require, |
| 265 |
'data' => $data, |
| 266 |
'default_state' => $default_state, |
| 267 |
'city_require' => ! array_key_exists( $country, give_city_not_required_country_list() ), |
| 268 |
'zip_require' => ! array_key_exists( $country, give_get_country_list_without_postcodes() ), |
| 269 |
]; |
| 270 |
wp_send_json( $response ); |
| 271 |
} |
| 272 |
|
| 273 |
add_action( 'wp_ajax_give_get_states', 'give_ajax_get_states_field' ); |
| 274 |
add_action( 'wp_ajax_nopriv_give_get_states', 'give_ajax_get_states_field' ); |
| 275 |
|
| 276 |
/** |
| 277 |
* Retrieve donation forms via AJAX for chosen dropdown search field. |
| 278 |
* |
| 279 |
* @since 1.0 |
| 280 |
* |
| 281 |
* @return void |
| 282 |
*/ |
| 283 |
function give_ajax_form_search() { |
| 284 |
$results = []; |
| 285 |
$search = esc_sql( sanitize_text_field( $_POST['s'] ) ); |
| 286 |
|
| 287 |
$args = [ |
| 288 |
'post_type' => 'give_forms', |
| 289 |
's' => $search, |
| 290 |
'update_post_term_cache' => false, |
| 291 |
'update_post_meta_cache' => false, |
| 292 |
'cache_results' => false, |
| 293 |
'no_found_rows' => true, |
| 294 |
'post_status' => 'publish', |
| 295 |
'orderby' => 'title', |
| 296 |
'order' => 'ASC', |
| 297 |
'posts_per_page' => empty( $search ) ? 30 : -1, |
| 298 |
]; |
| 299 |
|
| 300 |
/** |
| 301 |
* Filter to modify Ajax form search args |
| 302 |
* |
| 303 |
* @since 2.1 |
| 304 |
* |
| 305 |
* @param array $args Query argument for WP_query |
| 306 |
* |
| 307 |
* @return array $args Query argument for WP_query |
| 308 |
*/ |
| 309 |
$args = (array) apply_filters( 'give_ajax_form_search_args', $args ); |
| 310 |
|
| 311 |
// get all the donation form. |
| 312 |
$query = new WP_Query( $args ); |
| 313 |
if ( $query->have_posts() ) { |
| 314 |
while ( $query->have_posts() ) { |
| 315 |
$query->the_post(); |
| 316 |
global $post; |
| 317 |
|
| 318 |
$results[] = [ |
| 319 |
'id' => $post->ID, |
| 320 |
'name' => $post->post_title, |
| 321 |
]; |
| 322 |
} |
| 323 |
wp_reset_postdata(); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Filter to modify Ajax form search result |
| 328 |
* |
| 329 |
* @since 2.1 |
| 330 |
* |
| 331 |
* @param array $results Contain the Donation Form id |
| 332 |
* |
| 333 |
* @return array $results Contain the Donation Form id |
| 334 |
*/ |
| 335 |
$results = (array) apply_filters( 'give_ajax_form_search_response', $results ); |
| 336 |
|
| 337 |
wp_send_json( $results ); |
| 338 |
} |
| 339 |
|
| 340 |
add_action( 'wp_ajax_give_form_search', 'give_ajax_form_search' ); |
| 341 |
add_action( 'wp_ajax_nopriv_give_form_search', 'give_ajax_form_search' ); |
| 342 |
|
| 343 |
/** |
| 344 |
* Search the donors database via Ajax |
| 345 |
* |
| 346 |
* @since 1.0 |
| 347 |
* |
| 348 |
* @return void |
| 349 |
*/ |
| 350 |
function give_ajax_donor_search() { |
| 351 |
global $wpdb; |
| 352 |
|
| 353 |
$search = esc_sql( sanitize_text_field( $_POST['s'] ) ); |
| 354 |
$results = []; |
| 355 |
if ( ! current_user_can( 'view_give_reports' ) ) { |
| 356 |
$donors = []; |
| 357 |
} else { |
| 358 |
$donors = $wpdb->get_results( "SELECT id,name,email FROM $wpdb->donors WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%' LIMIT 50" ); |
| 359 |
} |
| 360 |
|
| 361 |
if ( $donors ) { |
| 362 |
foreach ( $donors as $donor ) { |
| 363 |
|
| 364 |
$results[] = [ |
| 365 |
'id' => $donor->id, |
| 366 |
'name' => $donor->name . ' (' . $donor->email . ')', |
| 367 |
]; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
wp_send_json( $results ); |
| 372 |
} |
| 373 |
|
| 374 |
add_action( 'wp_ajax_give_donor_search', 'give_ajax_donor_search' ); |
| 375 |
|
| 376 |
|
| 377 |
/** |
| 378 |
* Searches for users via ajax and returns a list of results |
| 379 |
* |
| 380 |
* @since 1.0 |
| 381 |
* |
| 382 |
* @return void |
| 383 |
*/ |
| 384 |
function give_ajax_search_users() { |
| 385 |
$results = []; |
| 386 |
|
| 387 |
if ( current_user_can( 'manage_give_settings' ) ) { |
| 388 |
|
| 389 |
$search = esc_sql( sanitize_text_field( $_POST['s'] ) ); |
| 390 |
|
| 391 |
$get_users_args = [ |
| 392 |
'number' => 9999, |
| 393 |
'search' => $search . '*', |
| 394 |
]; |
| 395 |
|
| 396 |
$get_users_args = apply_filters( 'give_search_users_args', $get_users_args ); |
| 397 |
|
| 398 |
$found_users = apply_filters( 'give_ajax_found_users', get_users( $get_users_args ), $search ); |
| 399 |
$results = []; |
| 400 |
|
| 401 |
if ( $found_users ) { |
| 402 |
|
| 403 |
foreach ( $found_users as $user ) { |
| 404 |
|
| 405 |
$results[] = [ |
| 406 |
'id' => $user->ID, |
| 407 |
'name' => esc_html( $user->user_login . ' (' . $user->user_email . ')' ), |
| 408 |
]; |
| 409 |
} |
| 410 |
} |
| 411 |
}// End if(). |
| 412 |
|
| 413 |
wp_send_json( $results ); |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
add_action( 'wp_ajax_give_user_search', 'give_ajax_search_users' ); |
| 418 |
|
| 419 |
|
| 420 |
/** |
| 421 |
* Queries page by title and returns page ID and title in JSON format. |
| 422 |
* |
| 423 |
* Note: this function in for internal use. |
| 424 |
* |
| 425 |
* @since 2.1 |
| 426 |
* |
| 427 |
* @return string |
| 428 |
*/ |
| 429 |
function give_ajax_pages_search() { |
| 430 |
$data = []; |
| 431 |
$args = [ |
| 432 |
'post_type' => 'page', |
| 433 |
's' => give_clean( $_POST['s'] ), |
| 434 |
]; |
| 435 |
|
| 436 |
$query = new WP_Query( $args ); |
| 437 |
|
| 438 |
// Query posts by title. |
| 439 |
if ( $query->have_posts() ) { |
| 440 |
while ( $query->have_posts() ) { |
| 441 |
$query->the_post(); |
| 442 |
|
| 443 |
$data[] = [ |
| 444 |
'id' => get_the_ID(), |
| 445 |
'name' => get_the_title(), |
| 446 |
]; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
wp_send_json( $data ); |
| 451 |
} |
| 452 |
|
| 453 |
add_action( 'wp_ajax_give_pages_search', 'give_ajax_pages_search' ); |
| 454 |
|
| 455 |
/** |
| 456 |
* Retrieve Categories via AJAX for chosen dropdown search field. |
| 457 |
* |
| 458 |
* @since 2.1 |
| 459 |
* |
| 460 |
* @return void |
| 461 |
*/ |
| 462 |
function give_ajax_categories_search() { |
| 463 |
$results = []; |
| 464 |
|
| 465 |
/** |
| 466 |
* Filter to modify Ajax tags search args |
| 467 |
* |
| 468 |
* @since 2.1 |
| 469 |
* |
| 470 |
* @param array $args argument for get_terms |
| 471 |
* |
| 472 |
* @return array $args argument for get_terms |
| 473 |
*/ |
| 474 |
$args = (array) apply_filters( |
| 475 |
'give_forms_categories_dropdown_args', |
| 476 |
[ |
| 477 |
'number' => 30, |
| 478 |
'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) ), |
| 479 |
] |
| 480 |
); |
| 481 |
|
| 482 |
$categories = get_terms( 'give_forms_category', $args ); |
| 483 |
|
| 484 |
foreach ( $categories as $category ) { |
| 485 |
$results[] = [ |
| 486 |
'id' => $category->term_id, |
| 487 |
'name' => $category->name, |
| 488 |
]; |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Filter to modify Ajax tags search result |
| 493 |
* |
| 494 |
* @since 2.1 |
| 495 |
* |
| 496 |
* @param array $results Contain the categories id and name |
| 497 |
* |
| 498 |
* @return array $results Contain the categories id and name |
| 499 |
*/ |
| 500 |
$results = (array) apply_filters( 'give_forms_categories_dropdown_responce', $results ); |
| 501 |
|
| 502 |
wp_send_json( $results ); |
| 503 |
} |
| 504 |
|
| 505 |
add_action( 'wp_ajax_give_categories_search', 'give_ajax_categories_search' ); |
| 506 |
|
| 507 |
/** |
| 508 |
* Retrieve Tags via AJAX for chosen dropdown search field. |
| 509 |
* |
| 510 |
* @since 2.1 |
| 511 |
* |
| 512 |
* @return void |
| 513 |
*/ |
| 514 |
function give_ajax_tags_search() { |
| 515 |
$results = []; |
| 516 |
|
| 517 |
/** |
| 518 |
* Filter to modify Ajax tags search args |
| 519 |
* |
| 520 |
* @since 2.1 |
| 521 |
* |
| 522 |
* @param array $args argument for get_terms |
| 523 |
* |
| 524 |
* @return array $args argument for get_terms |
| 525 |
*/ |
| 526 |
$args = (array) apply_filters( |
| 527 |
'give_forms_tags_dropdown_args', |
| 528 |
[ |
| 529 |
'number' => 30, |
| 530 |
'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) ), |
| 531 |
] |
| 532 |
); |
| 533 |
|
| 534 |
$categories = get_terms( 'give_forms_tag', $args ); |
| 535 |
|
| 536 |
foreach ( $categories as $category ) { |
| 537 |
$results[] = [ |
| 538 |
'id' => $category->term_id, |
| 539 |
'name' => $category->name, |
| 540 |
]; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Filter to modify Ajax tags search result |
| 545 |
* |
| 546 |
* @since 2.1 |
| 547 |
* |
| 548 |
* @param array $results Contain the tags id and name |
| 549 |
* |
| 550 |
* @return array $results Contain the tags id and name |
| 551 |
*/ |
| 552 |
$results = (array) apply_filters( 'give_forms_tags_dropdown_responce', $results ); |
| 553 |
|
| 554 |
wp_send_json( $results ); |
| 555 |
} |
| 556 |
|
| 557 |
add_action( 'wp_ajax_give_tags_search', 'give_ajax_tags_search' ); |
| 558 |
|
| 559 |
/** |
| 560 |
* Check for Price Variations (Multi-level donation forms) |
| 561 |
* |
| 562 |
* @since 1.5 |
| 563 |
* |
| 564 |
* @return void |
| 565 |
*/ |
| 566 |
function give_check_for_form_price_variations() { |
| 567 |
|
| 568 |
if ( ! current_user_can( 'edit_give_forms', get_current_user_id() ) ) { |
| 569 |
die( '-1' ); |
| 570 |
} |
| 571 |
|
| 572 |
$form_id = absint( $_POST['form_id'] ); |
| 573 |
$form = get_post( $form_id ); |
| 574 |
|
| 575 |
if ( 'give_forms' !== $form->post_type ) { |
| 576 |
die( '-2' ); |
| 577 |
} |
| 578 |
|
| 579 |
if ( give_has_variable_prices( $form_id ) ) { |
| 580 |
$variable_prices = give_get_variable_prices( $form_id ); |
| 581 |
|
| 582 |
if ( $variable_prices ) { |
| 583 |
$ajax_response = '<select class="give_price_options_select give-select give-select" name="give_price_option">'; |
| 584 |
|
| 585 |
if ( isset( $_POST['all_prices'] ) ) { |
| 586 |
$ajax_response .= '<option value="all">' . esc_html__( 'All Levels', 'give' ) . '</option>'; |
| 587 |
} |
| 588 |
|
| 589 |
foreach ( $variable_prices as $key => $price ) { |
| 590 |
|
| 591 |
$level_text = ! empty( $price['_give_text'] ) ? esc_html( $price['_give_text'] ) : give_currency_filter( give_format_amount( $price['_give_amount'], [ 'sanitize' => false ] ) ); |
| 592 |
|
| 593 |
$ajax_response .= '<option value="' . esc_attr( $price['_give_id']['level_id'] ) . '">' . $level_text . '</option>'; |
| 594 |
} |
| 595 |
$ajax_response .= '</select>'; |
| 596 |
echo $ajax_response; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
give_die(); |
| 601 |
} |
| 602 |
|
| 603 |
add_action( 'wp_ajax_give_check_for_form_price_variations', 'give_check_for_form_price_variations' ); |
| 604 |
|
| 605 |
|
| 606 |
/** |
| 607 |
* Check for Variation Prices HTML (Multi-level donation forms) |
| 608 |
* |
| 609 |
* @since 1.6 |
| 610 |
* |
| 611 |
* @return void |
| 612 |
*/ |
| 613 |
function give_check_for_form_price_variations_html() { |
| 614 |
if ( ! current_user_can( 'edit_give_payments', get_current_user_id() ) ) { |
| 615 |
wp_die(); |
| 616 |
} |
| 617 |
|
| 618 |
$form_id = ! empty( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : false; |
| 619 |
$payment_id = ! empty( $_POST['payment_id'] ) ? absint( $_POST['payment_id'] ) : false; |
| 620 |
if ( empty( $form_id ) || empty( $payment_id ) ) { |
| 621 |
wp_die(); |
| 622 |
} |
| 623 |
|
| 624 |
$form = get_post( $form_id ); |
| 625 |
if ( ! empty( $form->post_type ) && 'give_forms' !== $form->post_type ) { |
| 626 |
wp_die(); |
| 627 |
} |
| 628 |
|
| 629 |
if ( ! give_has_variable_prices( $form_id ) || ! $form_id ) { |
| 630 |
esc_html_e( 'n/a', 'give' ); |
| 631 |
} else { |
| 632 |
$prices_atts = []; |
| 633 |
if ( $variable_prices = give_get_variable_prices( $form_id ) ) { |
| 634 |
foreach ( $variable_prices as $variable_price ) { |
| 635 |
$prices_atts[ $variable_price['_give_id']['level_id'] ] = give_format_amount( $variable_price['_give_amount'], [ 'sanitize' => false ] ); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
// Variable price dropdown options. |
| 640 |
$variable_price_dropdown_option = [ |
| 641 |
'id' => $form_id, |
| 642 |
'name' => 'give-variable-price', |
| 643 |
'chosen' => true, |
| 644 |
'show_option_all' => '', |
| 645 |
'show_option_none' => '', |
| 646 |
'select_atts' => 'data-prices=' . esc_attr( json_encode( $prices_atts ) ), |
| 647 |
]; |
| 648 |
|
| 649 |
if ( $payment_id ) { |
| 650 |
// Payment object. |
| 651 |
$payment = new Give_Payment( $payment_id ); |
| 652 |
|
| 653 |
// Payment meta. |
| 654 |
$payment_meta = $payment->get_meta(); |
| 655 |
$variable_price_dropdown_option['selected'] = $payment_meta['price_id']; |
| 656 |
} |
| 657 |
|
| 658 |
// Render variable prices select tag html. |
| 659 |
give_get_form_variable_price_dropdown( $variable_price_dropdown_option, true ); |
| 660 |
} |
| 661 |
|
| 662 |
give_die(); |
| 663 |
} |
| 664 |
|
| 665 |
add_action( 'wp_ajax_give_check_for_form_price_variations_html', 'give_check_for_form_price_variations_html' ); |
| 666 |
|
| 667 |
/** |
| 668 |
* Send Confirmation Email For Complete Donation History Access. |
| 669 |
* |
| 670 |
* @since 1.8.17 |
| 671 |
* |
| 672 |
* @return bool |
| 673 |
*/ |
| 674 |
function give_confirm_email_for_donation_access() { |
| 675 |
|
| 676 |
// Verify Security using Nonce. |
| 677 |
if ( ! check_ajax_referer( 'give_ajax_nonce', 'nonce' ) ) { |
| 678 |
return false; |
| 679 |
} |
| 680 |
|
| 681 |
// Bail Out, if email is empty. |
| 682 |
if ( empty( $_POST['email'] ) ) { |
| 683 |
return false; |
| 684 |
} |
| 685 |
|
| 686 |
$donor = Give()->donors->get_donor_by( 'email', give_clean( $_POST['email'] ) ); |
| 687 |
if ( Give()->email_access->can_send_email( $donor->id ) ) { |
| 688 |
$return = []; |
| 689 |
$email_sent = Give()->email_access->send_email( $donor->id, $donor->email ); |
| 690 |
|
| 691 |
$return['status'] = 'success'; |
| 692 |
|
| 693 |
if ( ! $email_sent ) { |
| 694 |
$return['status'] = 'error'; |
| 695 |
$return['message'] = Give_Notices::print_frontend_notice( |
| 696 |
__( 'Unable to send email. Please try again.', 'give' ), |
| 697 |
false, |
| 698 |
'error' |
| 699 |
); |
| 700 |
} |
| 701 |
|
| 702 |
/** |
| 703 |
* Filter to modify access mail send notice |
| 704 |
* |
| 705 |
* @since 2.1.3 |
| 706 |
* |
| 707 |
* @param string Send notice message for email access. |
| 708 |
* |
| 709 |
* @return string $message Send notice message for email access. |
| 710 |
*/ |
| 711 |
$message = (string) apply_filters( 'give_email_access_mail_send_notice', __( 'Please check your email and click on the link to access your complete donation history.', 'give' ) ); |
| 712 |
|
| 713 |
$return['message'] = Give_Notices::print_frontend_notice( |
| 714 |
$message, |
| 715 |
false, |
| 716 |
'success' |
| 717 |
); |
| 718 |
|
| 719 |
} else { |
| 720 |
$value = Give()->email_access->verify_throttle / 60; |
| 721 |
$return['status'] = 'error'; |
| 722 |
|
| 723 |
/** |
| 724 |
* Filter to modify email access exceed notices message. |
| 725 |
* |
| 726 |
* @since 2.1.3 |
| 727 |
* |
| 728 |
* @param string $message email access exceed notices message |
| 729 |
* @param int $value email access exceed times |
| 730 |
* |
| 731 |
* @return string $message email access exceed notices message |
| 732 |
*/ |
| 733 |
$message = (string) apply_filters( |
| 734 |
'give_email_access_requests_exceed_notice', |
| 735 |
sprintf( |
| 736 |
__( 'Too many access email requests detected. Please wait %s before requesting a new donation history access link.', 'give' ), |
| 737 |
sprintf( _n( '%s minute', '%s minutes', $value, 'give' ), $value ) |
| 738 |
), |
| 739 |
$value |
| 740 |
); |
| 741 |
|
| 742 |
$return['message'] = Give_Notices::print_frontend_notice( |
| 743 |
$message, |
| 744 |
false, |
| 745 |
'error' |
| 746 |
); |
| 747 |
} |
| 748 |
|
| 749 |
echo json_encode( $return ); |
| 750 |
give_die(); |
| 751 |
} |
| 752 |
|
| 753 |
add_action( 'wp_ajax_nopriv_give_confirm_email_for_donations_access', 'give_confirm_email_for_donation_access' ); |
| 754 |
|
| 755 |
/** |
| 756 |
* Render receipt by ajax |
| 757 |
* Note: only for internal use |
| 758 |
* |
| 759 |
* @since 2.2.0 |
| 760 |
*/ |
| 761 |
function __give_get_receipt() { |
| 762 |
|
| 763 |
$get_data = give_clean( filter_input_array( INPUT_GET ) ); |
| 764 |
|
| 765 |
if ( ! isset( $get_data['shortcode_atts'] ) ) { |
| 766 |
give_die(); |
| 767 |
} |
| 768 |
|
| 769 |
$atts = (array) json_decode( $get_data['shortcode_atts'] ); |
| 770 |
$data = give_receipt_shortcode( $atts ); |
| 771 |
|
| 772 |
wp_send_json( $data ); |
| 773 |
} |
| 774 |
add_action( 'wp_ajax_get_receipt', '__give_get_receipt' ); |
| 775 |
add_action( 'wp_ajax_nopriv_get_receipt', '__give_get_receipt' ); |
| 776 |
|
| 777 |
/** |
| 778 |
* Get ajax url to render content from other website into thickbox |
| 779 |
* Note: only for internal use |
| 780 |
* |
| 781 |
* @param array $args |
| 782 |
* |
| 783 |
* @return string |
| 784 |
* @since 2.5.0 |
| 785 |
*/ |
| 786 |
function give_modal_ajax_url( $args = [] ) { |
| 787 |
$args = wp_parse_args( |
| 788 |
$args, |
| 789 |
[ |
| 790 |
'action' => 'give_get_content_by_ajax', |
| 791 |
'_wpnonce' => wp_create_nonce( 'give_get_content_by_ajax' ), |
| 792 |
] |
| 793 |
); |
| 794 |
|
| 795 |
return add_query_arg( $args, admin_url( '/admin-ajax.php' ) ); |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
/** |
| 800 |
* Return content from url |
| 801 |
* Note: only for internal use |
| 802 |
* |
| 803 |
* @todo use get_version endpoint to read changelog or cache add-ons infro from update_plugins option |
| 804 |
* |
| 805 |
* @return string |
| 806 |
* @since 2.5.0 |
| 807 |
*/ |
| 808 |
function give_get_content_by_ajax_handler() { |
| 809 |
check_admin_referer( 'give_get_content_by_ajax' ); |
| 810 |
|
| 811 |
if ( empty( $_GET['url'] ) ) { |
| 812 |
die(); |
| 813 |
} |
| 814 |
|
| 815 |
// Handle changelog render request. |
| 816 |
if ( |
| 817 |
! empty( $_GET['show_changelog'] ) |
| 818 |
&& (int) give_clean( $_GET['show_changelog'] ) |
| 819 |
) { |
| 820 |
$msg = __( 'Sorry, unable to load changelog.', 'give' ); |
| 821 |
$url = urldecode_deep( give_clean( $_GET['url'] ) ); |
| 822 |
|
| 823 |
$response = wp_remote_get( $url ); |
| 824 |
|
| 825 |
if ( is_wp_error( $response ) ) { |
| 826 |
echo "$msg<br><br><code>Error: {$response->get_error_message()}</code>"; |
| 827 |
exit; |
| 828 |
} |
| 829 |
|
| 830 |
$response = wp_remote_retrieve_body( $response ); |
| 831 |
|
| 832 |
if ( false === strpos( $response, '== Changelog ==' ) ) { |
| 833 |
echo $msg; |
| 834 |
exit; |
| 835 |
} |
| 836 |
|
| 837 |
$changelog = explode( '== Changelog ==', $response ); |
| 838 |
$changelog = end( $changelog ); |
| 839 |
|
| 840 |
echo give_get_format_md( $changelog ); |
| 841 |
} |
| 842 |
|
| 843 |
do_action( 'give_get_content_by_ajax_handler' ); |
| 844 |
|
| 845 |
exit; |
| 846 |
} |
| 847 |
|
| 848 |
add_action( 'wp_ajax_give_get_content_by_ajax', 'give_get_content_by_ajax_handler' ); |
| 849 |
|
| 850 |
|
| 851 |
/** |
| 852 |
* Get form template for ajax request. |
| 853 |
* |
| 854 |
* Note: only for internal use |
| 855 |
* |
| 856 |
* @since 2.7.0 |
| 857 |
*/ |
| 858 |
function give_get_form_template_id() { |
| 859 |
check_ajax_referer( 'give-donation-form-widget', 'security' ); |
| 860 |
|
| 861 |
$formId = isset( $_POST['formId'] ) ? absint( $_POST['formId'] ) : 0; |
| 862 |
|
| 863 |
// Send error response if form id does not mentioned. |
| 864 |
if ( ! $formId ) { |
| 865 |
wp_send_json_error(); |
| 866 |
} |
| 867 |
|
| 868 |
$templateID = FormTemplateUtils::getActiveID( $formId ); |
| 869 |
$templateID = $templateID ?: 'legacy'; |
| 870 |
|
| 871 |
wp_send_json_success( $templateID ); |
| 872 |
} |
| 873 |
add_action( 'wp_ajax_give_get_form_template_id', 'give_get_form_template_id' ); |
| 874 |
add_action( 'wp_ajax_no_priv_give_get_form_template_id', 'give_get_form_template_id' ); |
| 875 |
|