ajax-functions.php ➔ give_load_checkout_fields()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
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
/**
186
 * Retrieve a states drop down
187
 *
188
 * @since  1.0
189
 *
190
 * @return void
191
 */
192
function give_ajax_get_states_field() {
193
	$states_found   = false;
194
	$show_field     = true;
195
	$states_require = true;
196
	// Get the Country code from the $_POST.
197
	$country = sanitize_text_field( $_POST['country'] );
198
199
	// Get the field name from the $_POST.
200
	$field_name = sanitize_text_field( $_POST['field_name'] );
201
202
	$label        = __( 'State', 'give' );
203
	$states_label = give_get_states_label();
204
205
	$default_state = '';
206
	if ( give_get_country() === $country ) {
207
		$default_state = give_get_state();
208
	}
209
210
	// Check if $country code exists in the array key for states label.
211
	if ( array_key_exists( $country, $states_label ) ) {
212
		$label = $states_label[ $country ];
213
	}
214
215
	if ( empty( $country ) ) {
216
		$country = give_get_country();
217
	}
218
219
	$states = give_get_states( $country );
220
	if ( ! empty( $states ) ) {
221
		$args = array(
222
			'name'             => $field_name,
223
			'id'               => $field_name,
224
			'class'            => $field_name . '  give-select',
225
			'options'          => $states,
226
			'show_option_all'  => false,
227
			'show_option_none' => false,
228
			'placeholder'      => $label,
229
			'selected'         => $default_state,
230
			'autocomplete'     => 'address-level1',
231
		);
232
		$data         = Give()->html->select( $args );
233
		$states_found = true;
234
	} else {
235
		$data = 'nostates';
236
237
		// Get the country list that does not have any states init.
238
		$no_states_country = give_no_states_country_list();
239
240
		// Check if $country code exists in the array key.
241
		if ( array_key_exists( $country, $no_states_country ) ) {
242
			$show_field = false;
243
		}
244
245
		// Get the country list that does not require states.
246
		$states_not_required_country_list = give_states_not_required_country_list();
247
248
		// Check if $country code exists in the array key.
249
		if ( array_key_exists( $country, $states_not_required_country_list ) ) {
250
			$states_require = false;
251
		}
252
	}
253
254
	$response = array(
255
		'success'        => true,
256
		'states_found'   => $states_found,
257
		'states_label'   => $label,
258
		'show_field'     => $show_field,
259
		'states_require' => $states_require,
260
		'data'           => $data,
261
		'default_state'  => $default_state,
262
		'city_require'   => ! array_key_exists( $country, give_city_not_required_country_list() ),
263
	);
264
	wp_send_json( $response );
265
}
266
267
add_action( 'wp_ajax_give_get_states', 'give_ajax_get_states_field' );
268
add_action( 'wp_ajax_nopriv_give_get_states', 'give_ajax_get_states_field' );
269
270
/**
271
 * Retrieve donation forms via AJAX for chosen dropdown search field.
272
 *
273
 * @since  1.0
274
 *
275
 * @return void
276
 */
277
function give_ajax_form_search() {
278
	$results = array();
279
	$search  = esc_sql( sanitize_text_field( $_POST['s'] ) );
280
281
	$args = array(
282
		'post_type'              => 'give_forms',
283
		's'                      => $search,
284
		'update_post_term_cache' => false,
285
		'update_post_meta_cache' => false,
286
		'cache_results'          => false,
287
		'no_found_rows'          => true,
288
		'post_status'            => 'publish',
289
		'orderby'                => 'title',
290
		'order'                  => 'ASC',
291
		'posts_per_page'         => empty( $search ) ? 30 : -1,
292
	);
293
294
	/**
295
	 * Filter to modify Ajax form search args
296
	 *
297
	 * @since 2.1
298
	 *
299
	 * @param array $args Query argument for WP_query
300
	 *
301
	 * @return array $args Query argument for WP_query
302
	 */
303
	$args = (array) apply_filters( 'give_ajax_form_search_args', $args );
304
305
	// get all the donation form.
306
	$query = new WP_Query( $args );
307
	if ( $query->have_posts() ) {
308
		while ( $query->have_posts() ) {
309
			$query->the_post();
310
			global $post;
311
312
			$results[] = array(
313
				'id'   => $post->ID,
314
				'name' => $post->post_title,
315
			);
316
		}
317
		wp_reset_postdata();
318
	}
319
320
	/**
321
	 * Filter to modify Ajax form search result
322
	 *
323
	 * @since 2.1
324
	 *
325
	 * @param array $results Contain the Donation Form id
326
	 *
327
	 * @return array $results Contain the Donation Form id
328
	 */
329
	$results = (array) apply_filters( 'give_ajax_form_search_response', $results );
330
331
	wp_send_json( $results );
332
}
333
334
add_action( 'wp_ajax_give_form_search', 'give_ajax_form_search' );
335
add_action( 'wp_ajax_nopriv_give_form_search', 'give_ajax_form_search' );
336
337
/**
338
 * Search the donors database via Ajax
339
 *
340
 * @since  1.0
341
 *
342
 * @return void
343
 */
344
function give_ajax_donor_search() {
345
	global $wpdb;
346
347
	$search  = esc_sql( sanitize_text_field( $_POST['s'] ) );
348
	$results = array();
349
	if ( ! current_user_can( 'view_give_reports' ) ) {
350
		$donors = array();
351
	} else {
352
		$donors = $wpdb->get_results( "SELECT id,name,email FROM $wpdb->donors WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%' LIMIT 50" );
353
	}
354
355
	if ( $donors ) {
356
		foreach ( $donors as $donor ) {
357
358
			$results[] = array(
359
				'id'   => $donor->id,
360
				'name' => $donor->name . ' (' . $donor->email . ')',
361
			);
362
		}
363
	}
364
365
	wp_send_json( $results );
366
}
367
368
add_action( 'wp_ajax_give_donor_search', 'give_ajax_donor_search' );
369
370
371
/**
372
 * Searches for users via ajax and returns a list of results
373
 *
374
 * @since  1.0
375
 *
376
 * @return void
377
 */
378
function give_ajax_search_users() {
379
	$results = array();
380
381
	if ( current_user_can( 'manage_give_settings' ) ) {
382
383
		$search = esc_sql( sanitize_text_field( $_POST['s'] ) );
384
385
		$get_users_args = array(
386
			'number' => 9999,
387
			'search' => $search . '*',
388
		);
389
390
		$get_users_args = apply_filters( 'give_search_users_args', $get_users_args );
391
392
		$found_users = apply_filters( 'give_ajax_found_users', get_users( $get_users_args ), $search );
393
		$results     = array();
394
395
		if ( $found_users ) {
396
397
			foreach ( $found_users as $user ) {
398
399
				$results[] = array(
400
					'id'   => $user->ID,
401
					'name' => esc_html( $user->user_login . ' (' . $user->user_email . ')' ),
402
				);
403
			}
404
		}
405
	}// End if().
406
407
	wp_send_json( $results );
408
409
}
410
411
add_action( 'wp_ajax_give_user_search', 'give_ajax_search_users' );
412
413
414
/**
415
 * Queries page by title and returns page ID and title in JSON format.
416
 *
417
 * Note: this function in for internal use.
418
 *
419
 * @since 2.1
420
 *
421
 * @return string
422
 */
423
function give_ajax_pages_search() {
424
	$data = array();
425
	$args = array(
426
		'post_type' => 'page',
427
		's'         => give_clean( $_POST['s'] ),
428
	);
429
430
	$query = new WP_Query( $args );
431
432
	// Query posts by title.
433
	if ( $query->have_posts() ) {
434
		while ( $query->have_posts() ) {
435
			$query->the_post();
436
437
			$data[] = array(
438
				'id'   => get_the_ID(),
439
				'name' => get_the_title(),
440
			);
441
		}
442
	}
443
444
	wp_send_json( $data );
445
}
446
447
add_action( 'wp_ajax_give_pages_search', 'give_ajax_pages_search' );
448
449
/**
450
 * Retrieve Categories via AJAX for chosen dropdown search field.
451
 *
452
 * @since  2.1
453
 *
454
 * @return void
455
 */
456 View Code Duplication
function give_ajax_categories_search() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
457
	$results = array();
458
459
	/**
460
	 * Filter to modify Ajax tags search args
461
	 *
462
	 * @since 2.1
463
	 *
464
	 * @param array $args argument for get_terms
465
	 *
466
	 * @return array $args argument for get_terms
467
	 */
468
	$args = (array) apply_filters( 'give_forms_categories_dropdown_args', array(
469
		'number'     => 30,
470
		'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) )
471
	) );
472
473
	$categories = get_terms( 'give_forms_category', $args );
474
475
	foreach ( $categories as $category ) {
476
		$results[] = array(
477
			'id'   => $category->term_id,
478
			'name' => $category->name,
479
		);
480
	}
481
482
	/**
483
	 * Filter to modify Ajax tags search result
484
	 *
485
	 * @since 2.1
486
	 *
487
	 * @param array $results Contain the categories id and name
488
	 *
489
	 * @return array $results Contain the categories id and name
490
	 */
491
	$results = (array) apply_filters( 'give_forms_categories_dropdown_responce', $results );
492
493
	wp_send_json( $results );
494
}
495
496
add_action( 'wp_ajax_give_categories_search', 'give_ajax_categories_search' );
497
498
/**
499
 * Retrieve Tags via AJAX for chosen dropdown search field.
500
 *
501
 * @since  2.1
502
 *
503
 * @return void
504
 */
505 View Code Duplication
function give_ajax_tags_search() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
506
	$results = array();
507
508
	/**
509
	 * Filter to modify Ajax tags search args
510
	 *
511
	 * @since 2.1
512
	 *
513
	 * @param array $args argument for get_terms
514
	 *
515
	 * @return array $args argument for get_terms
516
	 */
517
	$args = (array) apply_filters( 'give_forms_tags_dropdown_args', array(
518
		'number'     => 30,
519
		'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) )
520
	) );
521
522
	$categories = get_terms( 'give_forms_tag', $args );
523
524
	foreach ( $categories as $category ) {
525
		$results[] = array(
526
			'id'   => $category->term_id,
527
			'name' => $category->name,
528
		);
529
	}
530
531
	/**
532
	 * Filter to modify Ajax tags search result
533
	 *
534
	 * @since 2.1
535
	 *
536
	 * @param array $results Contain the tags id and name
537
	 *
538
	 * @return array $results Contain the tags id and name
539
	 */
540
	$results = (array) apply_filters( 'give_forms_tags_dropdown_responce', $results );
541
542
	wp_send_json( $results );
543
}
544
545
add_action( 'wp_ajax_give_tags_search', 'give_ajax_tags_search' );
546
547
/**
548
 * Check for Price Variations (Multi-level donation forms)
549
 *
550
 * @since  1.5
551
 *
552
 * @return void
553
 */
554
function give_check_for_form_price_variations() {
555
556
	if ( ! current_user_can( 'edit_give_forms', get_current_user_id() ) ) {
557
		die( '-1' );
558
	}
559
560
	$form_id = absint( $_POST['form_id'] );
561
	$form    = get_post( $form_id );
562
563
	if ( 'give_forms' !== $form->post_type ) {
564
		die( '-2' );
565
	}
566
567
	if ( give_has_variable_prices( $form_id ) ) {
568
		$variable_prices = give_get_variable_prices( $form_id );
569
570
		if ( $variable_prices ) {
571
			$ajax_response = '<select class="give_price_options_select give-select give-select" name="give_price_option">';
572
573
			if ( isset( $_POST['all_prices'] ) ) {
574
				$ajax_response .= '<option value="all">' . esc_html__( 'All Levels', 'give' ) . '</option>';
575
			}
576
577
			foreach ( $variable_prices as $key => $price ) {
578
579
				$level_text = ! empty( $price['_give_text'] ) ? esc_html( $price['_give_text'] ) : give_currency_filter( give_format_amount( $price['_give_amount'], array( 'sanitize' => false ) ) );
580
581
				$ajax_response .= '<option value="' . esc_attr( $price['_give_id']['level_id'] ) . '">' . $level_text . '</option>';
582
			}
583
			$ajax_response .= '</select>';
584
			echo $ajax_response;
585
		}
586
	}
587
588
	give_die();
589
}
590
591
add_action( 'wp_ajax_give_check_for_form_price_variations', 'give_check_for_form_price_variations' );
592
593
594
/**
595
 * Check for Variation Prices HTML  (Multi-level donation forms)
596
 *
597
 * @since  1.6
598
 *
599
 * @return void
600
 */
601
function give_check_for_form_price_variations_html() {
602
	if ( ! current_user_can( 'edit_give_payments', get_current_user_id() ) ) {
603
		wp_die();
604
	}
605
606
	$form_id    = ! empty( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : false;
607
	$payment_id = ! empty( $_POST['payment_id'] ) ? absint( $_POST['payment_id'] ) : false;
608
	if ( empty( $form_id ) || empty( $payment_id ) ) {
609
		wp_die();
610
	}
611
612
	$form = get_post( $form_id );
613
	if ( ! empty( $form->post_type ) && 'give_forms' !== $form->post_type ) {
614
		wp_die();
615
	}
616
617
	if ( ! give_has_variable_prices( $form_id ) || ! $form_id ) {
618
		esc_html_e( 'n/a', 'give' );
619
	} else {
620
		$prices_atts = array();
621 View Code Duplication
		if ( $variable_prices = give_get_variable_prices( $form_id ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
622
			foreach ( $variable_prices as $variable_price ) {
623
				$prices_atts[ $variable_price['_give_id']['level_id'] ] = give_format_amount( $variable_price['_give_amount'], array( 'sanitize' => false ) );
624
			}
625
		}
626
627
		// Variable price dropdown options.
628
		$variable_price_dropdown_option = array(
629
			'id'               => $form_id,
630
			'name'             => 'give-variable-price',
631
			'chosen'           => true,
632
			'show_option_all'  => '',
633
			'show_option_none' => '',
634
			'select_atts'      => 'data-prices=' . esc_attr( json_encode( $prices_atts ) ),
635
		);
636
637
		if ( $payment_id ) {
638
			// Payment object.
639
			$payment = new Give_Payment( $payment_id );
640
641
			// Payment meta.
642
			$payment_meta                               = $payment->get_meta();
643
			$variable_price_dropdown_option['selected'] = $payment_meta['price_id'];
644
		}
645
646
		// Render variable prices select tag html.
647
		give_get_form_variable_price_dropdown( $variable_price_dropdown_option, true );
648
	}
649
650
	give_die();
651
}
652
653
add_action( 'wp_ajax_give_check_for_form_price_variations_html', 'give_check_for_form_price_variations_html' );
654
655
/**
656
 * Send Confirmation Email For Complete Donation History Access.
657
 *
658
 * @since 1.8.17
659
 *
660
 * @return bool
661
 */
662
function give_confirm_email_for_donation_access() {
663
664
	// Verify Security using Nonce.
665
	if ( ! check_ajax_referer( 'give_ajax_nonce', 'nonce' ) ) {
666
		return false;
667
	}
668
669
	// Bail Out, if email is empty.
670
	if ( empty( $_POST['email'] ) ) {
671
		return false;
672
	}
673
674
	$donor = Give()->donors->get_donor_by( 'email', give_clean( $_POST['email'] ) );
0 ignored issues
show
Documentation introduced by
give_clean($_POST['email']) is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
675
	if ( Give()->email_access->can_send_email( $donor->id ) ) {
676
		$return     = array();
677
		$email_sent = Give()->email_access->send_email( $donor->id, $donor->email );
678
679
		$return['status']  = 'success';
680
681
		if ( ! $email_sent ) {
682
			$return['status']  = 'error';
683
			$return['message'] = Give_Notices::print_frontend_notice(
684
				__( 'Unable to send email. Please try again.', 'give' ),
685
				false,
686
				'error'
687
			);
688
		}
689
690
		/**
691
		 * Filter to modify access mail send notice
692
		 *
693
		 * @since 2.1.3
694
		 *
695
		 * @param string Send notice message for email access.
696
		 *
697
		 * @return  string $message Send notice message for email access.
698
		 */
699
		$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' ) );
700
701
		$return['message'] = Give_Notices::print_frontend_notice(
702
			$message,
703
			false,
704
			'success'
705
		);
706
707
708
	} else {
709
		$value             = Give()->email_access->verify_throttle / 60;
710
		$return['status']  = 'error';
711
712
		/**
713
		 * Filter to modify email access exceed notices message.
714
		 *
715
		 * @since 2.1.3
716
		 *
717
		 * @param string $message email access exceed notices message
718
		 * @param int $value email access exceed times
719
		 *
720
		 * @return string $message email access exceed notices message
721
		 */
722
		$message = (string) apply_filters(
723
			'give_email_access_requests_exceed_notice',
724
			sprintf(
725
				__( 'Too many access email requests detected. Please wait %s before requesting a new donation history access link.', 'give' ),
726
				sprintf( _n( '%s minute', '%s minutes', $value, 'give' ), $value )
727
			),
728
			$value
729
		);
730
731
		$return['message'] = Give_Notices::print_frontend_notice(
732
			$message,
733
			false,
734
			'error'
735
		);
736
	}
737
738
	echo json_encode( $return );
739
	give_die();
740
}
741
742
add_action( 'wp_ajax_nopriv_give_confirm_email_for_donations_access', 'give_confirm_email_for_donation_access' );
743
744
/**
745
 * Render receipt by ajax
746
 * Note: only for internal use
747
 *
748
 * @since 2.2.0
749
 */
750
function __give_get_receipt(){
751
	
752
	$get_data = give_clean( filter_input_array( INPUT_GET ) );
753
	
754
	if( ! isset( $get_data['shortcode_atts'] ) ) {
755
		give_die();
756
	}
757
758
	$atts = (array) json_decode( $get_data['shortcode_atts'] );
759
	$data = give_receipt_shortcode( $atts );
760
761
	wp_send_json( $data );
762
}
763
add_action( 'wp_ajax_get_receipt', '__give_get_receipt' );
764
add_action( 'wp_ajax_nopriv_get_receipt', '__give_get_receipt' );
765
766
/**
767
 * Get ajax url to render content from other website into thickbox
768
 * Note: only for internal use
769
 *
770
 * @param array $args
771
 *
772
 * @return string
773
 * @since 2.5.0
774
 */
775
function give_modal_ajax_url( $args = array() ) {
776
	$args = wp_parse_args(
777
		$args,
778
		array(
779
			'action'   => 'give_get_content_by_ajax',
780
			'_wpnonce' => wp_create_nonce( 'give_get_content_by_ajax' ),
781
		)
782
	);
783
784
	return add_query_arg( $args, admin_url( '/admin-ajax.php' ) );
785
}
786
787
788
/**
789
 * Return content from url
790
 * Note: only for internal use
791
 * @todo use get_version endpoint to read changelog or cache add-ons infro from update_plugins option
792
 *
793
 * @return string
794
 * @since 2.5.0
795
 *
796
 */
797
function give_get_content_by_ajax_handler() {
798
	check_admin_referer( 'give_get_content_by_ajax' );
799
800
	if ( empty( $_GET['url'] ) ) {
801
		die();
802
	}
803
804
	// Handle changelog render request.
805
	if(
806
		! empty( $_GET['show_changelog'] )
807
		&& (int) give_clean( $_GET['show_changelog'] )
808
	) {
809
		$msg = __( 'Sorry, unable to load changelog.', 'give' );
810
		$url = urldecode_deep( give_clean( $_GET['url'] ) );
811
812
		$response = wp_remote_get( $url );
813
814
		if ( is_wp_error( $response ) ) {
815
			echo "$msg<br><br><code>Error: {$response->get_error_message()}</code>" ;
816
			exit;
817
		}
818
819
		$response = wp_remote_retrieve_body( $response );
820
821
822
		if( false === strpos( $response,  '== Changelog ==' ) ) {
823
			echo $msg;
824
			exit;
825
		}
826
827
		$changelog = explode( '== Changelog ==', $response );
828
		$changelog = end( $changelog );
829
830
		echo give_get_format_md( $changelog );
831
	}
832
833
	do_action( 'give_get_content_by_ajax_handler' );
834
835
	exit;
836
}
837
838
add_action( 'wp_ajax_give_get_content_by_ajax', 'give_get_content_by_ajax_handler' );
839
840