admin
5 years ago
api
6 years ago
database
6 years ago
deprecated
6 years ago
donors
5 years ago
emails
6 years ago
forms
6 years ago
frontend
6 years ago
gateways
6 years ago
libraries
6 years ago
payments
6 years ago
actions.php
6 years ago
ajax-functions.php
6 years ago
class-give-async-process.php
6 years ago
class-give-background-updater.php
6 years ago
class-give-cache-setting.php
6 years ago
class-give-cache.php
6 years ago
class-give-cli-commands.php
6 years ago
class-give-comment.php
6 years ago
class-give-cron.php
6 years ago
class-give-donate-form.php
6 years ago
class-give-donor.php
6 years ago
class-give-email-access.php
6 years ago
class-give-license-handler.php
6 years ago
class-give-logging.php
6 years ago
class-give-readme-parser.php
6 years ago
class-give-roles.php
6 years ago
class-give-scripts.php
6 years ago
class-give-session.php
6 years ago
class-give-stats.php
6 years ago
class-give-template-loader.php
6 years ago
class-give-tooltips.php
6 years ago
class-give-translation.php
6 years ago
class-notices.php
6 years ago
country-functions.php
6 years ago
currencies-list.php
6 years ago
currency-functions.php
6 years ago
error-tracking.php
6 years ago
filters.php
6 years ago
formatting.php
6 years ago
install.php
6 years ago
login-register.php
6 years ago
misc-functions.php
5 years ago
plugin-compatibility.php
6 years ago
post-types.php
6 years ago
price-functions.php
6 years ago
process-donation.php
6 years ago
setting-functions.php
6 years ago
shortcodes.php
6 years ago
template-functions.php
6 years ago
user-functions.php
6 years ago
ajax-functions.php
874 lines
| 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 = array( |
| 61 | 'sslverify' => false, |
| 62 | 'timeout' => 30, |
| 63 | 'body' => array( |
| 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 = array() ) { |
| 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 | array( |
| 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 = array( |
| 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 = array( |
| 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 | ); |
| 269 | wp_send_json( $response ); |
| 270 | } |
| 271 | |
| 272 | add_action( 'wp_ajax_give_get_states', 'give_ajax_get_states_field' ); |
| 273 | add_action( 'wp_ajax_nopriv_give_get_states', 'give_ajax_get_states_field' ); |
| 274 | |
| 275 | /** |
| 276 | * Retrieve donation forms via AJAX for chosen dropdown search field. |
| 277 | * |
| 278 | * @since 1.0 |
| 279 | * |
| 280 | * @return void |
| 281 | */ |
| 282 | function give_ajax_form_search() { |
| 283 | $results = array(); |
| 284 | $search = esc_sql( sanitize_text_field( $_POST['s'] ) ); |
| 285 | |
| 286 | $args = array( |
| 287 | 'post_type' => 'give_forms', |
| 288 | 's' => $search, |
| 289 | 'update_post_term_cache' => false, |
| 290 | 'update_post_meta_cache' => false, |
| 291 | 'cache_results' => false, |
| 292 | 'no_found_rows' => true, |
| 293 | 'post_status' => 'publish', |
| 294 | 'orderby' => 'title', |
| 295 | 'order' => 'ASC', |
| 296 | 'posts_per_page' => empty( $search ) ? 30 : -1, |
| 297 | ); |
| 298 | |
| 299 | /** |
| 300 | * Filter to modify Ajax form search args |
| 301 | * |
| 302 | * @since 2.1 |
| 303 | * |
| 304 | * @param array $args Query argument for WP_query |
| 305 | * |
| 306 | * @return array $args Query argument for WP_query |
| 307 | */ |
| 308 | $args = (array) apply_filters( 'give_ajax_form_search_args', $args ); |
| 309 | |
| 310 | // get all the donation form. |
| 311 | $query = new WP_Query( $args ); |
| 312 | if ( $query->have_posts() ) { |
| 313 | while ( $query->have_posts() ) { |
| 314 | $query->the_post(); |
| 315 | global $post; |
| 316 | |
| 317 | $results[] = array( |
| 318 | 'id' => $post->ID, |
| 319 | 'name' => $post->post_title, |
| 320 | ); |
| 321 | } |
| 322 | wp_reset_postdata(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Filter to modify Ajax form search result |
| 327 | * |
| 328 | * @since 2.1 |
| 329 | * |
| 330 | * @param array $results Contain the Donation Form id |
| 331 | * |
| 332 | * @return array $results Contain the Donation Form id |
| 333 | */ |
| 334 | $results = (array) apply_filters( 'give_ajax_form_search_response', $results ); |
| 335 | |
| 336 | wp_send_json( $results ); |
| 337 | } |
| 338 | |
| 339 | add_action( 'wp_ajax_give_form_search', 'give_ajax_form_search' ); |
| 340 | add_action( 'wp_ajax_nopriv_give_form_search', 'give_ajax_form_search' ); |
| 341 | |
| 342 | /** |
| 343 | * Search the donors database via Ajax |
| 344 | * |
| 345 | * @since 1.0 |
| 346 | * |
| 347 | * @return void |
| 348 | */ |
| 349 | function give_ajax_donor_search() { |
| 350 | global $wpdb; |
| 351 | |
| 352 | $search = esc_sql( sanitize_text_field( $_POST['s'] ) ); |
| 353 | $results = array(); |
| 354 | if ( ! current_user_can( 'view_give_reports' ) ) { |
| 355 | $donors = array(); |
| 356 | } else { |
| 357 | $donors = $wpdb->get_results( "SELECT id,name,email FROM $wpdb->donors WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%' LIMIT 50" ); |
| 358 | } |
| 359 | |
| 360 | if ( $donors ) { |
| 361 | foreach ( $donors as $donor ) { |
| 362 | |
| 363 | $results[] = array( |
| 364 | 'id' => $donor->id, |
| 365 | 'name' => $donor->name . ' (' . $donor->email . ')', |
| 366 | ); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | wp_send_json( $results ); |
| 371 | } |
| 372 | |
| 373 | add_action( 'wp_ajax_give_donor_search', 'give_ajax_donor_search' ); |
| 374 | |
| 375 | |
| 376 | /** |
| 377 | * Searches for users via ajax and returns a list of results |
| 378 | * |
| 379 | * @since 1.0 |
| 380 | * |
| 381 | * @return void |
| 382 | */ |
| 383 | function give_ajax_search_users() { |
| 384 | $results = array(); |
| 385 | |
| 386 | if ( current_user_can( 'manage_give_settings' ) ) { |
| 387 | |
| 388 | $search = esc_sql( sanitize_text_field( $_POST['s'] ) ); |
| 389 | |
| 390 | $get_users_args = array( |
| 391 | 'number' => 9999, |
| 392 | 'search' => $search . '*', |
| 393 | ); |
| 394 | |
| 395 | $get_users_args = apply_filters( 'give_search_users_args', $get_users_args ); |
| 396 | |
| 397 | $found_users = apply_filters( 'give_ajax_found_users', get_users( $get_users_args ), $search ); |
| 398 | $results = array(); |
| 399 | |
| 400 | if ( $found_users ) { |
| 401 | |
| 402 | foreach ( $found_users as $user ) { |
| 403 | |
| 404 | $results[] = array( |
| 405 | 'id' => $user->ID, |
| 406 | 'name' => esc_html( $user->user_login . ' (' . $user->user_email . ')' ), |
| 407 | ); |
| 408 | } |
| 409 | } |
| 410 | }// End if(). |
| 411 | |
| 412 | wp_send_json( $results ); |
| 413 | |
| 414 | } |
| 415 | |
| 416 | add_action( 'wp_ajax_give_user_search', 'give_ajax_search_users' ); |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Queries page by title and returns page ID and title in JSON format. |
| 421 | * |
| 422 | * Note: this function in for internal use. |
| 423 | * |
| 424 | * @since 2.1 |
| 425 | * |
| 426 | * @return string |
| 427 | */ |
| 428 | function give_ajax_pages_search() { |
| 429 | $data = array(); |
| 430 | $args = array( |
| 431 | 'post_type' => 'page', |
| 432 | 's' => give_clean( $_POST['s'] ), |
| 433 | ); |
| 434 | |
| 435 | $query = new WP_Query( $args ); |
| 436 | |
| 437 | // Query posts by title. |
| 438 | if ( $query->have_posts() ) { |
| 439 | while ( $query->have_posts() ) { |
| 440 | $query->the_post(); |
| 441 | |
| 442 | $data[] = array( |
| 443 | 'id' => get_the_ID(), |
| 444 | 'name' => get_the_title(), |
| 445 | ); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | wp_send_json( $data ); |
| 450 | } |
| 451 | |
| 452 | add_action( 'wp_ajax_give_pages_search', 'give_ajax_pages_search' ); |
| 453 | |
| 454 | /** |
| 455 | * Retrieve Categories via AJAX for chosen dropdown search field. |
| 456 | * |
| 457 | * @since 2.1 |
| 458 | * |
| 459 | * @return void |
| 460 | */ |
| 461 | function give_ajax_categories_search() { |
| 462 | $results = array(); |
| 463 | |
| 464 | /** |
| 465 | * Filter to modify Ajax tags search args |
| 466 | * |
| 467 | * @since 2.1 |
| 468 | * |
| 469 | * @param array $args argument for get_terms |
| 470 | * |
| 471 | * @return array $args argument for get_terms |
| 472 | */ |
| 473 | $args = (array) apply_filters( |
| 474 | 'give_forms_categories_dropdown_args', |
| 475 | array( |
| 476 | 'number' => 30, |
| 477 | 'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) ), |
| 478 | ) |
| 479 | ); |
| 480 | |
| 481 | $categories = get_terms( 'give_forms_category', $args ); |
| 482 | |
| 483 | foreach ( $categories as $category ) { |
| 484 | $results[] = array( |
| 485 | 'id' => $category->term_id, |
| 486 | 'name' => $category->name, |
| 487 | ); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Filter to modify Ajax tags search result |
| 492 | * |
| 493 | * @since 2.1 |
| 494 | * |
| 495 | * @param array $results Contain the categories id and name |
| 496 | * |
| 497 | * @return array $results Contain the categories id and name |
| 498 | */ |
| 499 | $results = (array) apply_filters( 'give_forms_categories_dropdown_responce', $results ); |
| 500 | |
| 501 | wp_send_json( $results ); |
| 502 | } |
| 503 | |
| 504 | add_action( 'wp_ajax_give_categories_search', 'give_ajax_categories_search' ); |
| 505 | |
| 506 | /** |
| 507 | * Retrieve Tags via AJAX for chosen dropdown search field. |
| 508 | * |
| 509 | * @since 2.1 |
| 510 | * |
| 511 | * @return void |
| 512 | */ |
| 513 | function give_ajax_tags_search() { |
| 514 | $results = array(); |
| 515 | |
| 516 | /** |
| 517 | * Filter to modify Ajax tags search args |
| 518 | * |
| 519 | * @since 2.1 |
| 520 | * |
| 521 | * @param array $args argument for get_terms |
| 522 | * |
| 523 | * @return array $args argument for get_terms |
| 524 | */ |
| 525 | $args = (array) apply_filters( |
| 526 | 'give_forms_tags_dropdown_args', |
| 527 | array( |
| 528 | 'number' => 30, |
| 529 | 'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) ), |
| 530 | ) |
| 531 | ); |
| 532 | |
| 533 | $categories = get_terms( 'give_forms_tag', $args ); |
| 534 | |
| 535 | foreach ( $categories as $category ) { |
| 536 | $results[] = array( |
| 537 | 'id' => $category->term_id, |
| 538 | 'name' => $category->name, |
| 539 | ); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Filter to modify Ajax tags search result |
| 544 | * |
| 545 | * @since 2.1 |
| 546 | * |
| 547 | * @param array $results Contain the tags id and name |
| 548 | * |
| 549 | * @return array $results Contain the tags id and name |
| 550 | */ |
| 551 | $results = (array) apply_filters( 'give_forms_tags_dropdown_responce', $results ); |
| 552 | |
| 553 | wp_send_json( $results ); |
| 554 | } |
| 555 | |
| 556 | add_action( 'wp_ajax_give_tags_search', 'give_ajax_tags_search' ); |
| 557 | |
| 558 | /** |
| 559 | * Check for Price Variations (Multi-level donation forms) |
| 560 | * |
| 561 | * @since 1.5 |
| 562 | * |
| 563 | * @return void |
| 564 | */ |
| 565 | function give_check_for_form_price_variations() { |
| 566 | |
| 567 | if ( ! current_user_can( 'edit_give_forms', get_current_user_id() ) ) { |
| 568 | die( '-1' ); |
| 569 | } |
| 570 | |
| 571 | $form_id = absint( $_POST['form_id'] ); |
| 572 | $form = get_post( $form_id ); |
| 573 | |
| 574 | if ( 'give_forms' !== $form->post_type ) { |
| 575 | die( '-2' ); |
| 576 | } |
| 577 | |
| 578 | if ( give_has_variable_prices( $form_id ) ) { |
| 579 | $variable_prices = give_get_variable_prices( $form_id ); |
| 580 | |
| 581 | if ( $variable_prices ) { |
| 582 | $ajax_response = '<select class="give_price_options_select give-select give-select" name="give_price_option">'; |
| 583 | |
| 584 | if ( isset( $_POST['all_prices'] ) ) { |
| 585 | $ajax_response .= '<option value="all">' . esc_html__( 'All Levels', 'give' ) . '</option>'; |
| 586 | } |
| 587 | |
| 588 | foreach ( $variable_prices as $key => $price ) { |
| 589 | |
| 590 | $level_text = ! empty( $price['_give_text'] ) ? esc_html( $price['_give_text'] ) : give_currency_filter( give_format_amount( $price['_give_amount'], array( 'sanitize' => false ) ) ); |
| 591 | |
| 592 | $ajax_response .= '<option value="' . esc_attr( $price['_give_id']['level_id'] ) . '">' . $level_text . '</option>'; |
| 593 | } |
| 594 | $ajax_response .= '</select>'; |
| 595 | echo $ajax_response; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | give_die(); |
| 600 | } |
| 601 | |
| 602 | add_action( 'wp_ajax_give_check_for_form_price_variations', 'give_check_for_form_price_variations' ); |
| 603 | |
| 604 | |
| 605 | /** |
| 606 | * Check for Variation Prices HTML (Multi-level donation forms) |
| 607 | * |
| 608 | * @since 1.6 |
| 609 | * |
| 610 | * @return void |
| 611 | */ |
| 612 | function give_check_for_form_price_variations_html() { |
| 613 | if ( ! current_user_can( 'edit_give_payments', get_current_user_id() ) ) { |
| 614 | wp_die(); |
| 615 | } |
| 616 | |
| 617 | $form_id = ! empty( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : false; |
| 618 | $payment_id = ! empty( $_POST['payment_id'] ) ? absint( $_POST['payment_id'] ) : false; |
| 619 | if ( empty( $form_id ) || empty( $payment_id ) ) { |
| 620 | wp_die(); |
| 621 | } |
| 622 | |
| 623 | $form = get_post( $form_id ); |
| 624 | if ( ! empty( $form->post_type ) && 'give_forms' !== $form->post_type ) { |
| 625 | wp_die(); |
| 626 | } |
| 627 | |
| 628 | if ( ! give_has_variable_prices( $form_id ) || ! $form_id ) { |
| 629 | esc_html_e( 'n/a', 'give' ); |
| 630 | } else { |
| 631 | $prices_atts = array(); |
| 632 | if ( $variable_prices = give_get_variable_prices( $form_id ) ) { |
| 633 | foreach ( $variable_prices as $variable_price ) { |
| 634 | $prices_atts[ $variable_price['_give_id']['level_id'] ] = give_format_amount( $variable_price['_give_amount'], array( 'sanitize' => false ) ); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // Variable price dropdown options. |
| 639 | $variable_price_dropdown_option = array( |
| 640 | 'id' => $form_id, |
| 641 | 'name' => 'give-variable-price', |
| 642 | 'chosen' => true, |
| 643 | 'show_option_all' => '', |
| 644 | 'show_option_none' => '', |
| 645 | 'select_atts' => 'data-prices=' . esc_attr( json_encode( $prices_atts ) ), |
| 646 | ); |
| 647 | |
| 648 | if ( $payment_id ) { |
| 649 | // Payment object. |
| 650 | $payment = new Give_Payment( $payment_id ); |
| 651 | |
| 652 | // Payment meta. |
| 653 | $payment_meta = $payment->get_meta(); |
| 654 | $variable_price_dropdown_option['selected'] = $payment_meta['price_id']; |
| 655 | } |
| 656 | |
| 657 | // Render variable prices select tag html. |
| 658 | give_get_form_variable_price_dropdown( $variable_price_dropdown_option, true ); |
| 659 | } |
| 660 | |
| 661 | give_die(); |
| 662 | } |
| 663 | |
| 664 | add_action( 'wp_ajax_give_check_for_form_price_variations_html', 'give_check_for_form_price_variations_html' ); |
| 665 | |
| 666 | /** |
| 667 | * Send Confirmation Email For Complete Donation History Access. |
| 668 | * |
| 669 | * @since 1.8.17 |
| 670 | * |
| 671 | * @return bool |
| 672 | */ |
| 673 | function give_confirm_email_for_donation_access() { |
| 674 | |
| 675 | // Verify Security using Nonce. |
| 676 | if ( ! check_ajax_referer( 'give_ajax_nonce', 'nonce' ) ) { |
| 677 | return false; |
| 678 | } |
| 679 | |
| 680 | // Bail Out, if email is empty. |
| 681 | if ( empty( $_POST['email'] ) ) { |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | $donor = Give()->donors->get_donor_by( 'email', give_clean( $_POST['email'] ) ); |
| 686 | if ( Give()->email_access->can_send_email( $donor->id ) ) { |
| 687 | $return = array(); |
| 688 | $email_sent = Give()->email_access->send_email( $donor->id, $donor->email ); |
| 689 | |
| 690 | $return['status'] = 'success'; |
| 691 | |
| 692 | if ( ! $email_sent ) { |
| 693 | $return['status'] = 'error'; |
| 694 | $return['message'] = Give_Notices::print_frontend_notice( |
| 695 | __( 'Unable to send email. Please try again.', 'give' ), |
| 696 | false, |
| 697 | 'error' |
| 698 | ); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Filter to modify access mail send notice |
| 703 | * |
| 704 | * @since 2.1.3 |
| 705 | * |
| 706 | * @param string Send notice message for email access. |
| 707 | * |
| 708 | * @return string $message Send notice message for email access. |
| 709 | */ |
| 710 | $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' ) ); |
| 711 | |
| 712 | $return['message'] = Give_Notices::print_frontend_notice( |
| 713 | $message, |
| 714 | false, |
| 715 | 'success' |
| 716 | ); |
| 717 | |
| 718 | } else { |
| 719 | $value = Give()->email_access->verify_throttle / 60; |
| 720 | $return['status'] = 'error'; |
| 721 | |
| 722 | /** |
| 723 | * Filter to modify email access exceed notices message. |
| 724 | * |
| 725 | * @since 2.1.3 |
| 726 | * |
| 727 | * @param string $message email access exceed notices message |
| 728 | * @param int $value email access exceed times |
| 729 | * |
| 730 | * @return string $message email access exceed notices message |
| 731 | */ |
| 732 | $message = (string) apply_filters( |
| 733 | 'give_email_access_requests_exceed_notice', |
| 734 | sprintf( |
| 735 | __( 'Too many access email requests detected. Please wait %s before requesting a new donation history access link.', 'give' ), |
| 736 | sprintf( _n( '%s minute', '%s minutes', $value, 'give' ), $value ) |
| 737 | ), |
| 738 | $value |
| 739 | ); |
| 740 | |
| 741 | $return['message'] = Give_Notices::print_frontend_notice( |
| 742 | $message, |
| 743 | false, |
| 744 | 'error' |
| 745 | ); |
| 746 | } |
| 747 | |
| 748 | echo json_encode( $return ); |
| 749 | give_die(); |
| 750 | } |
| 751 | |
| 752 | add_action( 'wp_ajax_nopriv_give_confirm_email_for_donations_access', 'give_confirm_email_for_donation_access' ); |
| 753 | |
| 754 | /** |
| 755 | * Render receipt by ajax |
| 756 | * Note: only for internal use |
| 757 | * |
| 758 | * @since 2.2.0 |
| 759 | */ |
| 760 | function __give_get_receipt() { |
| 761 | |
| 762 | $get_data = give_clean( filter_input_array( INPUT_GET ) ); |
| 763 | |
| 764 | if ( ! isset( $get_data['shortcode_atts'] ) ) { |
| 765 | give_die(); |
| 766 | } |
| 767 | |
| 768 | $atts = (array) json_decode( $get_data['shortcode_atts'] ); |
| 769 | $data = give_receipt_shortcode( $atts ); |
| 770 | |
| 771 | wp_send_json( $data ); |
| 772 | } |
| 773 | add_action( 'wp_ajax_get_receipt', '__give_get_receipt' ); |
| 774 | add_action( 'wp_ajax_nopriv_get_receipt', '__give_get_receipt' ); |
| 775 | |
| 776 | /** |
| 777 | * Get ajax url to render content from other website into thickbox |
| 778 | * Note: only for internal use |
| 779 | * |
| 780 | * @param array $args |
| 781 | * |
| 782 | * @return string |
| 783 | * @since 2.5.0 |
| 784 | */ |
| 785 | function give_modal_ajax_url( $args = array() ) { |
| 786 | $args = wp_parse_args( |
| 787 | $args, |
| 788 | array( |
| 789 | 'action' => 'give_get_content_by_ajax', |
| 790 | '_wpnonce' => wp_create_nonce( 'give_get_content_by_ajax' ), |
| 791 | ) |
| 792 | ); |
| 793 | |
| 794 | return add_query_arg( $args, admin_url( '/admin-ajax.php' ) ); |
| 795 | } |
| 796 | |
| 797 | |
| 798 | /** |
| 799 | * Return content from url |
| 800 | * Note: only for internal use |
| 801 | * |
| 802 | * @todo use get_version endpoint to read changelog or cache add-ons infro from update_plugins option |
| 803 | * |
| 804 | * @return string |
| 805 | * @since 2.5.0 |
| 806 | */ |
| 807 | function give_get_content_by_ajax_handler() { |
| 808 | check_admin_referer( 'give_get_content_by_ajax' ); |
| 809 | |
| 810 | if ( empty( $_GET['url'] ) ) { |
| 811 | die(); |
| 812 | } |
| 813 | |
| 814 | // Handle changelog render request. |
| 815 | if ( |
| 816 | ! empty( $_GET['show_changelog'] ) |
| 817 | && (int) give_clean( $_GET['show_changelog'] ) |
| 818 | ) { |
| 819 | $msg = __( 'Sorry, unable to load changelog.', 'give' ); |
| 820 | $url = urldecode_deep( give_clean( $_GET['url'] ) ); |
| 821 | |
| 822 | $response = wp_remote_get( $url ); |
| 823 | |
| 824 | if ( is_wp_error( $response ) ) { |
| 825 | echo "$msg<br><br><code>Error: {$response->get_error_message()}</code>"; |
| 826 | exit; |
| 827 | } |
| 828 | |
| 829 | $response = wp_remote_retrieve_body( $response ); |
| 830 | |
| 831 | if ( false === strpos( $response, '== Changelog ==' ) ) { |
| 832 | echo $msg; |
| 833 | exit; |
| 834 | } |
| 835 | |
| 836 | $changelog = explode( '== Changelog ==', $response ); |
| 837 | $changelog = end( $changelog ); |
| 838 | |
| 839 | echo give_get_format_md( $changelog ); |
| 840 | } |
| 841 | |
| 842 | do_action( 'give_get_content_by_ajax_handler' ); |
| 843 | |
| 844 | exit; |
| 845 | } |
| 846 | |
| 847 | add_action( 'wp_ajax_give_get_content_by_ajax', 'give_get_content_by_ajax_handler' ); |
| 848 | |
| 849 | |
| 850 | /** |
| 851 | * Get form template for ajax request. |
| 852 | * |
| 853 | * Note: only for internal use |
| 854 | * |
| 855 | * @since 2.7.0 |
| 856 | */ |
| 857 | function give_get_form_template_id() { |
| 858 | check_ajax_referer( 'give-donation-form-widget', 'security' ); |
| 859 | |
| 860 | $formId = isset( $_POST['formId'] ) ? absint( $_POST['formId'] ) : 0; |
| 861 | |
| 862 | // Send error response if form id does not mentioned. |
| 863 | if ( ! $formId ) { |
| 864 | wp_send_json_error(); |
| 865 | } |
| 866 | |
| 867 | $templateID = FormTemplateUtils::getActiveID( $formId ); |
| 868 | $templateID = $templateID ?: 'legacy'; |
| 869 | |
| 870 | wp_send_json_success( $templateID ); |
| 871 | } |
| 872 | add_action( 'wp_ajax_give_get_form_template_id', 'give_get_form_template_id' ); |
| 873 | add_action( 'wp_ajax_no_priv_give_get_form_template_id', 'give_get_form_template_id' ); |
| 874 |