Test Failed
Push — release/2.1 ( d820cd...c669cb )
by Ravinder
05:21
created

ajax-functions.php ➔ give_ajax_form_search()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 48
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 2
nop 0
dl 0
loc 48
rs 9.125
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, WordImpress
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
 * @return bool True if AJAX works, false otherwise
26
 */
27
function give_test_ajax_works() {
28
	// Handle ajax.
29
	if ( doing_action( 'wp_ajax_nopriv_give_test_ajax' ) ) {
30
		wp_die( 0, 200 );
31
	}
32
33
	// Check if the Airplane Mode plugin is installed.
34
	if ( class_exists( 'Airplane_Mode_Core' ) ) {
35
36
		$airplane = Airplane_Mode_Core::getInstance();
37
38
		if ( method_exists( $airplane, 'enabled' ) ) {
39
40
			if ( $airplane->enabled() ) {
41
				return true;
42
			}
43
		} else {
44
45
			if ( 'on' === $airplane->check_status() ) {
46
				return true;
47
			}
48
		}
49
	}
50
51
	add_filter( 'block_local_requests', '__return_false' );
52
53
	if ( Give_Cache::get( '_give_ajax_works', true ) ) {
54
		return true;
55
	}
56
57
	$params = array(
58
		'sslverify' => false,
59
		'timeout'   => 30,
60
		'body'      => array(
61
			'action' => 'give_test_ajax',
62
		),
63
	);
64
65
	$ajax = wp_remote_post( give_get_ajax_url(), $params );
66
67
	$works = true;
68
69
	if ( is_wp_error( $ajax ) ) {
70
71
		$works = false;
72
73
	} else {
74
75
		if ( empty( $ajax['response'] ) ) {
76
			$works = false;
77
		}
78
79
		if ( empty( $ajax['response']['code'] ) || 200 !== (int) $ajax['response']['code'] ) {
80
			$works = false;
81
		}
82
83
		if ( empty( $ajax['response']['message'] ) || 'OK' !== $ajax['response']['message'] ) {
84
			$works = false;
85
		}
86
87
		if ( ! isset( $ajax['body'] ) || 0 !== (int) $ajax['body'] ) {
88
			$works = false;
89
		}
90
	}
91
92
	if ( $works ) {
93
		Give_Cache::set( '_give_ajax_works', '1', DAY_IN_SECONDS, true );
94
	}
95
96
	return apply_filters( 'give_test_ajax_works', $works );
97
}
98
99
add_action( 'wp_ajax_nopriv_give_test_ajax', 'give_test_ajax_works' );
100
101
/**
102
 * Get AJAX URL
103
 *
104
 * @since  1.0
105
 *
106
 * @param array $query
107
 *
108
 * @return string
109
 */
110
function give_get_ajax_url( $query = array() ) {
111
	$scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin';
112
113
	$current_url = give_get_current_page_url();
114
	$ajax_url    = admin_url( 'admin-ajax.php', $scheme );
115
116
	if ( preg_match( '/^https/', $current_url ) && ! preg_match( '/^https/', $ajax_url ) ) {
117
		$ajax_url = preg_replace( '/^http/', 'https', $ajax_url );
118
	}
119
120
	if ( ! empty( $query ) ) {
121
		$ajax_url = add_query_arg( $query, $ajax_url );
122
	}
123
124
	return apply_filters( 'give_ajax_url', $ajax_url );
125
}
126
127
/**
128
 * Loads Checkout Login Fields via AJAX
129
 *
130
 * @since  1.0
131
 *
132
 * @return void
133
 */
134
function give_load_checkout_login_fields() {
135
	/**
136
	 * Fire when render login fields via ajax.
137
	 *
138
	 * @since 1.7
139
	 */
140
	do_action( 'give_donation_form_login_fields' );
141
142
	give_die();
143
}
144
145
add_action( 'wp_ajax_nopriv_give_checkout_login', 'give_load_checkout_login_fields' );
146
147
/**
148
 * Load Checkout Fields
149
 *
150
 * @since  1.3.6
151
 *
152
 * @return void
153
 */
154
function give_load_checkout_fields() {
155
	$form_id = isset( $_POST['form_id'] ) ? $_POST['form_id'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
156
157
	ob_start();
158
159
	/**
160
	 * Fire to render registration/login form.
161
	 *
162
	 * @since 1.7
163
	 */
164
	do_action( 'give_donation_form_register_login_fields', $form_id );
165
166
	$fields = ob_get_clean();
167
168
	wp_send_json( array(
169
		'fields' => wp_json_encode( $fields ),
170
		'submit' => wp_json_encode( give_get_donation_form_submit_button( $form_id ) ),
171
	) );
172
}
173
174
add_action( 'wp_ajax_nopriv_give_cancel_login', 'give_load_checkout_fields' );
175
add_action( 'wp_ajax_nopriv_give_checkout_register', 'give_load_checkout_fields' );
176
177
/**
178
 * Get Form Title via AJAX (used only in WordPress Admin)
179
 *
180
 * @since  1.0
181
 *
182
 * @return void
183
 */
184
function give_ajax_get_form_title() {
185
	if ( isset( $_POST['form_id'] ) ) {
186
		$title = get_the_title( $_POST['form_id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
187
		if ( $title ) {
188
			echo $title;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$title'
Loading history...
189
		} else {
190
			echo 'fail';
191
		}
192
	}
193
	give_die();
194
}
195
196
add_action( 'wp_ajax_give_get_form_title', 'give_ajax_get_form_title' );
197
add_action( 'wp_ajax_nopriv_give_get_form_title', 'give_ajax_get_form_title' );
198
199
/**
200
 * Retrieve a states drop down
201
 *
202
 * @since  1.0
203
 *
204
 * @return void
205
 */
206
function give_ajax_get_states_field() {
207
	$states_found   = false;
208
	$show_field     = true;
209
	$states_require = true;
210
	// Get the Country code from the $_POST.
211
	$country = sanitize_text_field( $_POST['country'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
212
213
	// Get the field name from the $_POST.
214
	$field_name = sanitize_text_field( $_POST['field_name'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
215
216
	$label        = __( 'State', 'give' );
217
	$states_label = give_get_states_label();
218
219
	$default_state = '';
220
	if ( $country === give_get_country() ) {
221
		$default_state = give_get_state();
222
	}
223
224
	// Check if $country code exists in the array key for states label.
225
	if ( array_key_exists( $country, $states_label ) ) {
226
		$label = $states_label[ $country ];
227
	}
228
229
	if ( empty( $country ) ) {
230
		$country = give_get_country();
231
	}
232
233
	$states = give_get_states( $country );
234
	if ( ! empty( $states ) ) {
235
		$args         = array(
236
			'name'             => $field_name,
237
			'id'               => $field_name,
238
			'class'            => $field_name . '  give-select',
239
			'options'          => $states,
240
			'show_option_all'  => false,
241
			'show_option_none' => false,
242
			'placeholder'      => $label,
243
			'selected'         => $default_state,
244
		);
245
		$data         = Give()->html->select( $args );
246
		$states_found = true;
247
	} else {
248
		$data = 'nostates';
249
250
		// Get the country list that does not have any states init.
251
		$no_states_country = give_no_states_country_list();
252
253
		// Check if $country code exists in the array key.
254
		if ( array_key_exists( $country, $no_states_country ) ) {
255
			$show_field = false;
256
		}
257
258
		// Get the country list that does not require states.
259
		$states_not_required_country_list = give_states_not_required_country_list();
260
261
		// Check if $country code exists in the array key.
262
		if ( array_key_exists( $country, $states_not_required_country_list ) ) {
263
			$states_require = false;
264
		}
265
	}
266
	$response = array(
267
		'success'        => true,
268
		'states_found'   => $states_found,
269
		'states_label'   => $label,
270
		'show_field'     => $show_field,
271
		'states_require' => $states_require,
272
		'data'           => $data,
273
		'default_state'  => $default_state,
274
	);
275
	wp_send_json( $response );
276
}
277
278
add_action( 'wp_ajax_give_get_states', 'give_ajax_get_states_field' );
279
add_action( 'wp_ajax_nopriv_give_get_states', 'give_ajax_get_states_field' );
280
281
/**
282
 * Retrieve donation forms via AJAX for chosen dropdown search field.
283
 *
284
 * @since  1.0
285
 *
286
 * @return void
287
 */
288
function give_ajax_form_search() {
289
	$results = array();
290
	$search  = esc_sql( sanitize_text_field( $_POST['s'] ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
291
292
	$args = array(
293
		'post_type' => 'give_forms',
294
		's'         => $search,
295
	);
296
297
	/**
298
	 * Filter to modify Ajax form search args
299
	 *
300
	 * @since 2.1
301
	 *
302
	 * @param array $args Query argument for WP_query
303
	 *
304
	 * @return array $args Query argument for WP_query
305
	 */
306
	$args = (array) apply_filters( 'give_ajax_form_search_args', $args );
307
308
	// get all the donation form.
309
	$query = new WP_Query( $args );
310
	if ( $query->have_posts() ) {
311
		while ( $query->have_posts() ) {
312
			$query->the_post();
313
			global $post;
314
315
			$results[] = array(
316
				'id'   => $post->ID,
317
				'name' => $post->post_title,
318
			);
319
		}
320
		wp_reset_postdata();
321
	}
322
323
	/**
324
	 * Filter to modify Ajax form search result
325
	 *
326
	 * @since 2.1
327
	 *
328
	 * @param array $results Contain the Donation Form id
329
	 *
330
	 * @return array $results Contain the Donation Form id
331
	 */
332
	$results = (array) apply_filters( 'give_ajax_form_search_responce', $results );
333
334
	wp_send_json( $results );
335
}
336
337
add_action( 'wp_ajax_give_form_search', 'give_ajax_form_search' );
338
add_action( 'wp_ajax_nopriv_give_form_search', 'give_ajax_form_search' );
339
340
/**
341
 * Search the donors database via Ajax
342
 *
343
 * @since  1.0
344
 *
345
 * @return void
346
 */
347
function give_ajax_donor_search() {
348
	global $wpdb;
349
350
	$search  = esc_sql( sanitize_text_field( $_POST['s'] ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
351
	$results = array();
352
	if ( ! current_user_can( 'view_give_reports' ) ) {
353
		$donors = array();
354
	} else {
355
		$donors = $wpdb->get_results( "SELECT id,name,email FROM $wpdb->donors WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%' LIMIT 50" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
356
	}
357
358
	if ( $donors ) {
359
		foreach ( $donors as $donor ) {
360
361
			$results[] = array(
362
				'id'   => $donor->id,
363
				'name' => $donor->name . ' (' . $donor->email . ')',
364
			);
365
		}
366
	}
367
368
	wp_send_json( $results );
369
}
370
371
add_action( 'wp_ajax_give_donor_search', 'give_ajax_donor_search' );
372
373
374
/**
375
 * Searches for users via ajax and returns a list of results
376
 *
377
 * @since  1.0
378
 *
379
 * @return void
380
 */
381
function give_ajax_search_users() {
382
	$results = array();
383
384
	if ( current_user_can( 'manage_give_settings' ) ) {
385
386
		$search = esc_sql( sanitize_text_field( $_POST['s'] ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
387
388
		$get_users_args = array(
389
			'number' => 9999,
390
			'search' => $search . '*',
391
		);
392
393
		$get_users_args = apply_filters( 'give_search_users_args', $get_users_args );
394
395
		$found_users = apply_filters( 'give_ajax_found_users', get_users( $get_users_args ), $search );
396
		$results     = array();
397
398
		if ( $found_users ) {
399
400
			foreach ( $found_users as $user ) {
401
402
				$results[] = array(
403
					'id'   => $user->ID,
404
					'name' => esc_html( $user->user_login . ' (' . $user->user_email . ')' ),
405
				);
406
			}
407
		}
408
	}// End if().
409
410
	wp_send_json( $results );
411
412
}
413
414
add_action( 'wp_ajax_give_user_search', 'give_ajax_search_users' );
415
416
417
/**
418
 * Queries page by title and returns page ID and title in JSON format.
419
 *
420
 * Note: this function in for internal use.
421
 *
422
 * @since 2.1
423
 *
424
 * @return string
425
 */
426
function give_ajax_pages_search() {
427
	$data = array();
428
	$args = array(
429
		'post_type' => 'page',
430
		's'         => give_clean( $_POST['s'] ),
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
431
	);
432
433
	$query = new WP_Query( $args );
434
435
	// Query posts by title.
436
	if ( $query->have_posts() ) {
437
		while ( $query->have_posts() ) {
438
			$query->the_post();
439
440
			$data[] = array(
441
				'id'   => get_the_ID(),
442
				'name' => get_the_title(),
443
			);
444
		}
445
	}
446
447
	wp_send_json( $data );
448
}
449
450
add_action( 'wp_ajax_give_pages_search', 'give_ajax_pages_search' );
451
452
/**
453
 * Retrieve Categories via AJAX for chosen dropdown search field.
454
 *
455
 * @since  2.1
456
 *
457
 * @return void
458
 */
459 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...
460
	$results = array();
461
462
	/**
463
	 * Filter to modify Ajax tags search args
464
	 *
465
	 * @since 2.1
466
	 *
467
	 * @param array $args argument for get_terms
468
	 *
469
	 * @return array $args argument for get_terms
470
	 */
471
	$args = (array) apply_filters( 'give_forms_categories_dropdown_args', array(
472
		'number'     => 30,
473
		'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
474
	) );
475
476
	$categories = get_terms( 'give_forms_category', $args );
477
478
	foreach ( $categories as $category ) {
479
		$results[] = array(
480
			'id'   => $category->term_id,
481
			'name' => $category->name,
482
		);
483
	}
484
485
	/**
486
	 * Filter to modify Ajax tags search result
487
	 *
488
	 * @since 2.1
489
	 *
490
	 * @param array $results Contain the categories id and name
491
	 *
492
	 * @return array $results Contain the categories id and name
493
	 */
494
	$results = (array) apply_filters( 'give_forms_categories_dropdown_responce', $results );
495
496
	wp_send_json( $results );
497
}
498
499
add_action( 'wp_ajax_give_categories_search', 'give_ajax_categories_search' );
500
501
/**
502
 * Retrieve Tags via AJAX for chosen dropdown search field.
503
 *
504
 * @since  2.1
505
 *
506
 * @return void
507
 */
508 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...
509
	$results = array();
510
511
	/**
512
	 * Filter to modify Ajax tags search args
513
	 *
514
	 * @since 2.1
515
	 *
516
	 * @param array $args argument for get_terms
517
	 *
518
	 * @return array $args argument for get_terms
519
	 */
520
	$args = (array) apply_filters( 'give_forms_tags_dropdown_args', array(
521
		'number'     => 30,
522
		'name__like' => esc_sql( sanitize_text_field( $_POST['s'] ) )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
523
	) );
524
525
	$categories = get_terms( 'give_forms_tag', $args );
526
527
	foreach ( $categories as $category ) {
528
		$results[] = array(
529
			'id'   => $category->term_id,
530
			'name' => $category->name,
531
		);
532
	}
533
534
	/**
535
	 * Filter to modify Ajax tags search result
536
	 *
537
	 * @since 2.1
538
	 *
539
	 * @param array $results Contain the tags id and name
540
	 *
541
	 * @return array $results Contain the tags id and name
542
	 */
543
	$results = (array) apply_filters( 'give_forms_tags_dropdown_responce', $results );
544
545
	wp_send_json( $results );
546
}
547
548
add_action( 'wp_ajax_give_tags_search', 'give_ajax_tags_search' );
549
550
/**
551
 * Check for Price Variations (Multi-level donation forms)
552
 *
553
 * @since  1.5
554
 *
555
 * @return void
556
 */
557
function give_check_for_form_price_variations() {
558
559
	if ( ! current_user_can( 'edit_give_forms', get_current_user_id() ) ) {
560
		die( '-1' );
561
	}
562
563
	$form_id = intval( $_POST['form_id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_POST
Loading history...
564
	$form    = get_post( $form_id );
565
566
	if ( 'give_forms' !== $form->post_type ) {
567
		die( '-2' );
568
	}
569
570
	if ( give_has_variable_prices( $form_id ) ) {
571
		$variable_prices = give_get_variable_prices( $form_id );
572
573
		if ( $variable_prices ) {
574
			$ajax_response = '<select class="give_price_options_select give-select give-select" name="give_price_option">';
575
576
			if ( isset( $_POST['all_prices'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
577
				$ajax_response .= '<option value="all">' . esc_html__( 'All Levels', 'give' ) . '</option>';
578
			}
579
580
			foreach ( $variable_prices as $key => $price ) {
581
582
				$level_text = ! empty( $price['_give_text'] ) ? esc_html( $price['_give_text'] ) : give_currency_filter( give_format_amount( $price['_give_amount'], array( 'sanitize' => false ) ) );
583
584
				$ajax_response .= '<option value="' . esc_attr( $price['_give_id']['level_id'] ) . '">' . $level_text . '</option>';
585
			}
586
			$ajax_response .= '</select>';
587
			echo $ajax_response;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$ajax_response'
Loading history...
588
		}
589
	}
590
591
	give_die();
592
}
593
594
add_action( 'wp_ajax_give_check_for_form_price_variations', 'give_check_for_form_price_variations' );
595
596
597
/**
598
 * Check for Variation Prices HTML  (Multi-level donation forms)
599
 *
600
 * @since  1.6
601
 *
602
 * @return void
603
 */
604
function give_check_for_form_price_variations_html() {
605
	if ( ! current_user_can( 'edit_give_payments', get_current_user_id() ) ) {
606
		wp_die();
607
	}
608
609
	$form_id    = ! empty( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
610
	$payment_id = ! empty( $_POST['payment_id'] ) ? intval( $_POST['payment_id'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
611
	if ( empty( $form_id ) || empty( $payment_id ) ) {
612
		wp_die();
613
	}
614
615
	$form = get_post( $form_id );
616
	if ( ! empty( $form->post_type ) && 'give_forms' !== $form->post_type ) {
617
		wp_die();
618
	}
619
620
	if ( ! give_has_variable_prices( $form_id ) || ! $form_id ) {
0 ignored issues
show
Security Bug introduced by
It seems like $form_id defined by !empty($_POST['form_id']...OST['form_id']) : false on line 609 can also be of type false; however, give_has_variable_prices() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Bug Best Practice introduced by
The expression $form_id of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
621
		esc_html_e( 'n/a', 'give' );
622
	} else {
623
		$prices_atts = array();
624 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...
625
			foreach ( $variable_prices as $variable_price ) {
626
				$prices_atts[ $variable_price['_give_id']['level_id'] ] = give_format_amount( $variable_price['_give_amount'], array( 'sanitize' => false ) );
627
			}
628
		}
629
630
		// Variable price dropdown options.
631
		$variable_price_dropdown_option = array(
632
			'id'               => $form_id,
633
			'name'             => 'give-variable-price',
634
			'chosen'           => true,
635
			'show_option_all'  => '',
636
			'show_option_none' => '',
637
			'select_atts'      => 'data-prices=' . esc_attr( json_encode( $prices_atts ) ),
638
		);
639
640
		if ( $payment_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment_id of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
641
			// Payment object.
642
			$payment = new Give_Payment( $payment_id );
643
644
			// Payment meta.
645
			$payment_meta                               = $payment->get_meta();
646
			$variable_price_dropdown_option['selected'] = $payment_meta['price_id'];
647
		}
648
649
		// Render variable prices select tag html.
650
		give_get_form_variable_price_dropdown( $variable_price_dropdown_option, true );
651
	}
652
653
	give_die();
654
}
655
656
add_action( 'wp_ajax_give_check_for_form_price_variations_html', 'give_check_for_form_price_variations_html' );
657
658
/**
659
 * Send Confirmation Email For Complete Donation History Access.
660
 *
661
 * @since 1.8.17
662
 *
663
 * @return bool
664
 */
665
function give_confirm_email_for_donation_access() {
666
667
	// Verify Security using Nonce.
668
	if ( ! check_ajax_referer( 'give_ajax_nonce', 'nonce' ) ) {
669
		return false;
670
	}
671
672
	// Bail Out, if email is empty.
673
	if ( empty( $_POST['email'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
674
		return false;
675
	}
676
677
	$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...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
678
	if ( Give()->email_access->can_send_email( $donor->id ) ) {
679
		$return     = array();
680
		$email_sent = Give()->email_access->send_email( $donor->id, $donor->email );
681
682
		if ( ! $email_sent ) {
683
			$return['status']  = 'error';
684
			$return['message'] = Give()->notices->print_frontend_notice(
685
				__( 'Unable to send email. Please try again.', 'give' ),
686
				false,
687
				'error'
688
			);
689
		}
690
691
		$return['status']  = 'success';
692
		$return['message'] = Give()->notices->print_frontend_notice(
693
			__( 'Please check your email and click on the link to access your complete donation history.', 'give' ),
694
			false,
695
			'success'
696
		);
697
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
698
699
	} else {
700
		$value             = Give()->email_access->verify_throttle / 60;
701
		$return['status']  = 'error';
702
		$return['message'] = Give()->notices->print_frontend_notice(
703
			sprintf(
704
				__( 'Too many access email requests detected. Please wait %s before requesting a new donation history access link.', 'give' ),
705
				sprintf( _n( '%s minute', '%s minutes', $value, 'give' ), $value )
706
			),
707
			false,
708
			'error'
709
		);
710
	}
711
712
	echo json_encode( $return );
713
	give_die();
714
}
715
716
add_action( 'wp_ajax_nopriv_give_confirm_email_for_donations_access', 'give_confirm_email_for_donation_access' );