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