Completed
Push — master ( 232512...2d0d2e )
by Devin
19:13
created
includes/ajax-functions.php 1 patch
Spacing   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
  */
13 13
 
14 14
 // Exit if accessed directly
15
-if ( ! defined( 'ABSPATH' ) ) {
15
+if ( ! defined('ABSPATH')) {
16 16
 	exit;
17 17
 }
18 18
 
@@ -26,27 +26,27 @@  discard block
 block discarded – undo
26 26
 function give_test_ajax_works() {
27 27
 
28 28
 	// Check if the Airplane Mode plugin is installed
29
-	if ( class_exists( 'Airplane_Mode_Core' ) ) {
29
+	if (class_exists('Airplane_Mode_Core')) {
30 30
 
31 31
 		$airplane = Airplane_Mode_Core::getInstance();
32 32
 
33
-		if ( method_exists( $airplane, 'enabled' ) ) {
33
+		if (method_exists($airplane, 'enabled')) {
34 34
 
35
-			if ( $airplane->enabled() ) {
35
+			if ($airplane->enabled()) {
36 36
 				return true;
37 37
 			}
38 38
 
39 39
 		} else {
40 40
 
41
-			if ( $airplane->check_status() == 'on' ) {
41
+			if ($airplane->check_status() == 'on') {
42 42
 				return true;
43 43
 			}
44 44
 		}
45 45
 	}
46 46
 
47
-	add_filter( 'block_local_requests', '__return_false' );
47
+	add_filter('block_local_requests', '__return_false');
48 48
 
49
-	if ( get_transient( '_give_ajax_works' ) ) {
49
+	if (get_transient('_give_ajax_works')) {
50 50
 		return true;
51 51
 	}
52 52
 
@@ -58,35 +58,35 @@  discard block
 block discarded – undo
58 58
 		)
59 59
 	);
60 60
 
61
-	$ajax  = wp_remote_post( give_get_ajax_url(), $params );
61
+	$ajax  = wp_remote_post(give_get_ajax_url(), $params);
62 62
 	$works = true;
63 63
 
64
-	if ( is_wp_error( $ajax ) ) {
64
+	if (is_wp_error($ajax)) {
65 65
 
66 66
 		$works = false;
67 67
 
68 68
 	} else {
69 69
 
70
-		if ( empty( $ajax['response'] ) ) {
70
+		if (empty($ajax['response'])) {
71 71
 			$works = false;
72 72
 		}
73 73
 
74
-		if ( empty( $ajax['response']['code'] ) || 200 !== (int) $ajax['response']['code'] ) {
74
+		if (empty($ajax['response']['code']) || 200 !== (int) $ajax['response']['code']) {
75 75
 			$works = false;
76 76
 		}
77 77
 
78
-		if ( empty( $ajax['response']['message'] ) || 'OK' !== $ajax['response']['message'] ) {
78
+		if (empty($ajax['response']['message']) || 'OK' !== $ajax['response']['message']) {
79 79
 			$works = false;
80 80
 		}
81 81
 
82
-		if ( ! isset( $ajax['body'] ) || 0 !== (int) $ajax['body'] ) {
82
+		if ( ! isset($ajax['body']) || 0 !== (int) $ajax['body']) {
83 83
 			$works = false;
84 84
 		}
85 85
 
86 86
 	}
87 87
 
88
-	if ( $works ) {
89
-		set_transient( '_give_ajax_works', '1', DAY_IN_SECONDS );
88
+	if ($works) {
89
+		set_transient('_give_ajax_works', '1', DAY_IN_SECONDS);
90 90
 	}
91 91
 
92 92
 	return $works;
@@ -101,16 +101,16 @@  discard block
 block discarded – undo
101 101
  * @return string
102 102
  */
103 103
 function give_get_ajax_url() {
104
-	$scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin';
104
+	$scheme = defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN ? 'https' : 'admin';
105 105
 
106 106
 	$current_url = give_get_current_page_url();
107
-	$ajax_url    = admin_url( 'admin-ajax.php', $scheme );
107
+	$ajax_url    = admin_url('admin-ajax.php', $scheme);
108 108
 
109
-	if ( preg_match( '/^https/', $current_url ) && ! preg_match( '/^https/', $ajax_url ) ) {
110
-		$ajax_url = preg_replace( '/^http/', 'https', $ajax_url );
109
+	if (preg_match('/^https/', $current_url) && ! preg_match('/^https/', $ajax_url)) {
110
+		$ajax_url = preg_replace('/^http/', 'https', $ajax_url);
111 111
 	}
112 112
 
113
-	return apply_filters( 'give_ajax_url', $ajax_url );
113
+	return apply_filters('give_ajax_url', $ajax_url);
114 114
 }
115 115
 
116 116
 /**
@@ -121,11 +121,11 @@  discard block
 block discarded – undo
121 121
  * @return void
122 122
  */
123 123
 function give_load_checkout_login_fields() {
124
-	do_action( 'give_purchase_form_login_fields' );
124
+	do_action('give_purchase_form_login_fields');
125 125
 	give_die();
126 126
 }
127 127
 
128
-add_action( 'wp_ajax_nopriv_give_checkout_login', 'give_load_checkout_login_fields' );
128
+add_action('wp_ajax_nopriv_give_checkout_login', 'give_load_checkout_login_fields');
129 129
 
130 130
 /**
131 131
  * Load Checkout Fields
@@ -135,22 +135,22 @@  discard block
 block discarded – undo
135 135
  * @return void
136 136
  */
137 137
 function give_load_checkout_fields() {
138
-	$form_id = isset( $_POST['form_id'] ) ? $_POST['form_id'] : '';
138
+	$form_id = isset($_POST['form_id']) ? $_POST['form_id'] : '';
139 139
 
140 140
 	ob_start();
141 141
 
142
-	do_action( 'give_purchase_form_register_login_fields', $form_id );
142
+	do_action('give_purchase_form_register_login_fields', $form_id);
143 143
 
144 144
 	$fields = ob_get_clean();
145 145
 
146
-	wp_send_json( array(
147
-		'fields' => wp_json_encode( $fields ),
148
-		'submit' => wp_json_encode( give_checkout_button_purchase( $form_id ) ),
149
-	) );
146
+	wp_send_json(array(
147
+		'fields' => wp_json_encode($fields),
148
+		'submit' => wp_json_encode(give_checkout_button_purchase($form_id)),
149
+	));
150 150
 }
151 151
 
152
-add_action( 'wp_ajax_nopriv_give_cancel_login', 'give_load_checkout_fields' );
153
-add_action( 'wp_ajax_nopriv_give_checkout_register', 'give_load_checkout_fields' );
152
+add_action('wp_ajax_nopriv_give_cancel_login', 'give_load_checkout_fields');
153
+add_action('wp_ajax_nopriv_give_checkout_register', 'give_load_checkout_fields');
154 154
 
155 155
 /**
156 156
  * Get Form Title via AJAX (used only in WordPress Admin)
@@ -160,9 +160,9 @@  discard block
 block discarded – undo
160 160
  * @return void
161 161
  */
162 162
 function give_ajax_get_form_title() {
163
-	if ( isset( $_POST['form_id'] ) ) {
164
-		$title = get_the_title( $_POST['form_id'] );
165
-		if ( $title ) {
163
+	if (isset($_POST['form_id'])) {
164
+		$title = get_the_title($_POST['form_id']);
165
+		if ($title) {
166 166
 			echo $title;
167 167
 		} else {
168 168
 			echo 'fail';
@@ -171,8 +171,8 @@  discard block
 block discarded – undo
171 171
 	give_die();
172 172
 }
173 173
 
174
-add_action( 'wp_ajax_give_get_form_title', 'give_ajax_get_form_title' );
175
-add_action( 'wp_ajax_nopriv_give_get_form_title', 'give_ajax_get_form_title' );
174
+add_action('wp_ajax_give_get_form_title', 'give_ajax_get_form_title');
175
+add_action('wp_ajax_nopriv_give_get_form_title', 'give_ajax_get_form_title');
176 176
 
177 177
 /**
178 178
  * Retrieve a states drop down
@@ -183,23 +183,23 @@  discard block
 block discarded – undo
183 183
  */
184 184
 function give_ajax_get_states_field() {
185 185
 
186
-	if ( empty( $_POST['country'] ) ) {
186
+	if (empty($_POST['country'])) {
187 187
 		$_POST['country'] = give_get_country();
188 188
 	}
189
-	$states = give_get_states( $_POST['country'] );
189
+	$states = give_get_states($_POST['country']);
190 190
 
191
-	if ( ! empty( $states ) ) {
191
+	if ( ! empty($states)) {
192 192
 
193 193
 		$args = array(
194 194
 			'name'             => $_POST['field_name'],
195 195
 			'id'               => $_POST['field_name'],
196
-			'class'            => $_POST['field_name'] . '  give-select',
197
-			'options'          => give_get_states( $_POST['country'] ),
196
+			'class'            => $_POST['field_name'].'  give-select',
197
+			'options'          => give_get_states($_POST['country']),
198 198
 			'show_option_all'  => false,
199 199
 			'show_option_none' => false
200 200
 		);
201 201
 
202
-		$response = Give()->html->select( $args );
202
+		$response = Give()->html->select($args);
203 203
 
204 204
 	} else {
205 205
 
@@ -211,8 +211,8 @@  discard block
 block discarded – undo
211 211
 	give_die();
212 212
 }
213 213
 
214
-add_action( 'wp_ajax_give_get_states', 'give_ajax_get_states_field' );
215
-add_action( 'wp_ajax_nopriv_give_get_states', 'give_ajax_get_states_field' );
214
+add_action('wp_ajax_give_get_states', 'give_ajax_get_states_field');
215
+add_action('wp_ajax_nopriv_give_get_states', 'give_ajax_get_states_field');
216 216
 
217 217
 /**
218 218
  * Retrieve a states drop down
@@ -224,17 +224,17 @@  discard block
 block discarded – undo
224 224
 function give_ajax_form_search() {
225 225
 	global $wpdb;
226 226
 
227
-	$search  = esc_sql( sanitize_text_field( $_GET['s'] ) );
227
+	$search  = esc_sql(sanitize_text_field($_GET['s']));
228 228
 	$results = array();
229
-	if ( current_user_can( 'edit_give_forms' ) ) {
230
-		$items = $wpdb->get_results( "SELECT ID,post_title FROM $wpdb->posts WHERE `post_type` = 'give_forms' AND `post_title` LIKE '%$search%' LIMIT 50" );
229
+	if (current_user_can('edit_give_forms')) {
230
+		$items = $wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts WHERE `post_type` = 'give_forms' AND `post_title` LIKE '%$search%' LIMIT 50");
231 231
 	} else {
232
-		$items = $wpdb->get_results( "SELECT ID,post_title FROM $wpdb->posts WHERE `post_type` = 'give_forms' AND `post_status` = 'publish' AND `post_title` LIKE '%$search%' LIMIT 50" );
232
+		$items = $wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts WHERE `post_type` = 'give_forms' AND `post_status` = 'publish' AND `post_title` LIKE '%$search%' LIMIT 50");
233 233
 	}
234 234
 
235
-	if ( $items ) {
235
+	if ($items) {
236 236
 
237
-		foreach ( $items as $item ) {
237
+		foreach ($items as $item) {
238 238
 
239 239
 			$results[] = array(
240 240
 				'id'   => $item->ID,
@@ -246,18 +246,18 @@  discard block
 block discarded – undo
246 246
 
247 247
 		$items[] = array(
248 248
 			'id'   => 0,
249
-			'name' => esc_html__( 'No results found', 'give' )
249
+			'name' => esc_html__('No results found', 'give')
250 250
 		);
251 251
 
252 252
 	}
253 253
 
254
-	echo json_encode( $results );
254
+	echo json_encode($results);
255 255
 
256 256
 	give_die();
257 257
 }
258 258
 
259
-add_action( 'wp_ajax_give_form_search', 'give_ajax_form_search' );
260
-add_action( 'wp_ajax_nopriv_give_form_search', 'give_ajax_form_search' );
259
+add_action('wp_ajax_give_form_search', 'give_ajax_form_search');
260
+add_action('wp_ajax_nopriv_give_form_search', 'give_ajax_form_search');
261 261
 
262 262
 /**
263 263
  * Search the donors database via Ajax
@@ -269,21 +269,21 @@  discard block
 block discarded – undo
269 269
 function give_ajax_donor_search() {
270 270
 	global $wpdb;
271 271
 
272
-	$search  = esc_sql( sanitize_text_field( $_GET['s'] ) );
272
+	$search  = esc_sql(sanitize_text_field($_GET['s']));
273 273
 	$results = array();
274
-	if ( ! current_user_can( 'view_give_reports' ) ) {
274
+	if ( ! current_user_can('view_give_reports')) {
275 275
 		$donors = array();
276 276
 	} else {
277
-		$donors = $wpdb->get_results( "SELECT id,name,email FROM {$wpdb->prefix}give_donors WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%' LIMIT 50" );
277
+		$donors = $wpdb->get_results("SELECT id,name,email FROM {$wpdb->prefix}give_donors WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%' LIMIT 50");
278 278
 	}
279 279
 
280
-	if ( $donors ) {
280
+	if ($donors) {
281 281
 
282
-		foreach ( $donors as $donor ) {
282
+		foreach ($donors as $donor) {
283 283
 
284 284
 			$results[] = array(
285 285
 				'id'   => $donor->id,
286
-				'name' => $donor->name . '(' . $donor->email . ')'
286
+				'name' => $donor->name.'('.$donor->email.')'
287 287
 			);
288 288
 		}
289 289
 
@@ -291,17 +291,17 @@  discard block
 block discarded – undo
291 291
 
292 292
 		$donors[] = array(
293 293
 			'id'   => 0,
294
-			'name' => esc_html__( 'No results found', 'give' )
294
+			'name' => esc_html__('No results found', 'give')
295 295
 		);
296 296
 
297 297
 	}
298 298
 
299
-	echo json_encode( $results );
299
+	echo json_encode($results);
300 300
 
301 301
 	give_die();
302 302
 }
303 303
 
304
-add_action( 'wp_ajax_give_donor_search', 'give_ajax_donor_search' );
304
+add_action('wp_ajax_give_donor_search', 'give_ajax_donor_search');
305 305
 
306 306
 
307 307
 /**
@@ -313,42 +313,42 @@  discard block
 block discarded – undo
313 313
  */
314 314
 function give_ajax_search_users() {
315 315
 
316
-	if ( current_user_can( 'manage_give_settings' ) ) {
316
+	if (current_user_can('manage_give_settings')) {
317 317
 
318
-		$search_query = trim( $_POST['user_name'] );
319
-		$exclude      = trim( $_POST['exclude'] );
318
+		$search_query = trim($_POST['user_name']);
319
+		$exclude      = trim($_POST['exclude']);
320 320
 
321 321
 		$get_users_args = array(
322 322
 			'number' => 9999,
323
-			'search' => $search_query . '*'
323
+			'search' => $search_query.'*'
324 324
 		);
325 325
 
326
-		if ( ! empty( $exclude ) ) {
327
-			$exclude_array             = explode( ',', $exclude );
326
+		if ( ! empty($exclude)) {
327
+			$exclude_array             = explode(',', $exclude);
328 328
 			$get_users_args['exclude'] = $exclude_array;
329 329
 		}
330 330
 
331
-		$get_users_args = apply_filters( 'give_search_users_args', $get_users_args );
331
+		$get_users_args = apply_filters('give_search_users_args', $get_users_args);
332 332
 
333
-		$found_users = apply_filters( 'give_ajax_found_users', get_users( $get_users_args ), $search_query );
333
+		$found_users = apply_filters('give_ajax_found_users', get_users($get_users_args), $search_query);
334 334
 
335 335
 		$user_list = '<ul>';
336
-		if ( $found_users ) {
337
-			foreach ( $found_users as $user ) {
338
-				$user_list .= '<li><a href="#" data-userid="' . esc_attr( $user->ID ) . '" data-login="' . esc_attr( $user->user_login ) . '">' . esc_html( $user->user_login ) . '</a></li>';
336
+		if ($found_users) {
337
+			foreach ($found_users as $user) {
338
+				$user_list .= '<li><a href="#" data-userid="'.esc_attr($user->ID).'" data-login="'.esc_attr($user->user_login).'">'.esc_html($user->user_login).'</a></li>';
339 339
 			}
340 340
 		} else {
341
-			$user_list .= '<li>' . esc_html__( 'No users found', 'give' ) . '</li>';
341
+			$user_list .= '<li>'.esc_html__('No users found', 'give').'</li>';
342 342
 		}
343 343
 		$user_list .= '</ul>';
344 344
 
345
-		echo json_encode( array( 'results' => $user_list ) );
345
+		echo json_encode(array('results' => $user_list));
346 346
 
347 347
 	}
348 348
 	die();
349 349
 }
350 350
 
351
-add_action( 'wp_ajax_give_search_users', 'give_ajax_search_users' );
351
+add_action('wp_ajax_give_search_users', 'give_ajax_search_users');
352 352
 
353 353
 
354 354
 /**
@@ -360,32 +360,32 @@  discard block
 block discarded – undo
360 360
  */
361 361
 function give_check_for_form_price_variations() {
362 362
 
363
-	if ( ! current_user_can( 'edit_give_forms', get_current_user_id() ) ) {
364
-		die( '-1' );
363
+	if ( ! current_user_can('edit_give_forms', get_current_user_id())) {
364
+		die('-1');
365 365
 	}
366 366
 
367
-	$form_id = intval( $_POST['form_id'] );
368
-	$form    = get_post( $form_id );
367
+	$form_id = intval($_POST['form_id']);
368
+	$form    = get_post($form_id);
369 369
 
370
-	if ( 'give_forms' != $form->post_type ) {
371
-		die( '-2' );
370
+	if ('give_forms' != $form->post_type) {
371
+		die('-2');
372 372
 	}
373 373
 
374
-	if ( give_has_variable_prices( $form_id ) ) {
375
-		$variable_prices = give_get_variable_prices( $form_id );
374
+	if (give_has_variable_prices($form_id)) {
375
+		$variable_prices = give_get_variable_prices($form_id);
376 376
 
377
-		if ( $variable_prices ) {
377
+		if ($variable_prices) {
378 378
 			$ajax_response = '<select class="give_price_options_select give-select give-select" name="give_price_option">';
379 379
 
380
-			if ( isset( $_POST['all_prices'] ) ) {
381
-				$ajax_response .= '<option value="">' . esc_html__( 'All Levels', 'give' ) . '</option>';
380
+			if (isset($_POST['all_prices'])) {
381
+				$ajax_response .= '<option value="">'.esc_html__('All Levels', 'give').'</option>';
382 382
 			}
383 383
 
384
-			foreach ( $variable_prices as $key => $price ) {
384
+			foreach ($variable_prices as $key => $price) {
385 385
 
386
-				$level_text = ! empty( $price['_give_text'] ) ? esc_html( $price['_give_text'] ) : give_currency_filter( give_format_amount( $price['_give_amount'] ) );
386
+				$level_text = ! empty($price['_give_text']) ? esc_html($price['_give_text']) : give_currency_filter(give_format_amount($price['_give_amount']));
387 387
 				
388
-				$ajax_response .= '<option value="' . esc_attr( $price['_give_id']['level_id'] ) . '">' . $level_text . '</option>';
388
+				$ajax_response .= '<option value="'.esc_attr($price['_give_id']['level_id']).'">'.$level_text.'</option>';
389 389
 			}
390 390
 			$ajax_response .= '</select>';
391 391
 			echo $ajax_response;
@@ -396,7 +396,7 @@  discard block
 block discarded – undo
396 396
 	give_die();
397 397
 }
398 398
 
399
-add_action( 'wp_ajax_give_check_for_form_price_variations', 'give_check_for_form_price_variations' );
399
+add_action('wp_ajax_give_check_for_form_price_variations', 'give_check_for_form_price_variations');
400 400
 
401 401
 
402 402
 /**
@@ -407,30 +407,30 @@  discard block
 block discarded – undo
407 407
  * @return void
408 408
  */
409 409
 function give_check_for_form_price_variations_html() {
410
-	if ( ! current_user_can( 'edit_give_payments', get_current_user_id() ) ) {
410
+	if ( ! current_user_can('edit_give_payments', get_current_user_id())) {
411 411
 		wp_die();
412 412
 	}
413 413
 
414
-	$form_id = intval( $_POST['form_id'] );
415
-	$payment_id = intval( $_POST['payment_id'] );
416
-	$form    = get_post( $form_id );
414
+	$form_id = intval($_POST['form_id']);
415
+	$payment_id = intval($_POST['payment_id']);
416
+	$form    = get_post($form_id);
417 417
 
418
-	if ( 'give_forms' != $form->post_type ) {
418
+	if ('give_forms' != $form->post_type) {
419 419
 		wp_die();
420 420
 	}
421 421
 
422
-    if ( ! give_has_variable_prices( $form_id ) ) {
423
-        esc_html_e( 'n/a', 'give' );
422
+    if ( ! give_has_variable_prices($form_id)) {
423
+        esc_html_e('n/a', 'give');
424 424
     } else {
425 425
         // Payment object.
426
-        $payment = new Give_Payment( $payment_id );
426
+        $payment = new Give_Payment($payment_id);
427 427
 
428 428
         // Payment meta.
429 429
         $payment_meta = $payment->get_meta();
430 430
 
431 431
 
432 432
         // Variable price dropdown options.
433
-        $variable_price_dropdown_option =  array(
433
+        $variable_price_dropdown_option = array(
434 434
             'id'                => $form_id,
435 435
             'name'              => 'give-variable-price',
436 436
             'chosen'            => true,
@@ -439,10 +439,10 @@  discard block
 block discarded – undo
439 439
         );
440 440
 
441 441
         // Render variable prices select tag html.
442
-        give_get_form_variable_price_dropdown( $variable_price_dropdown_option, true );
442
+        give_get_form_variable_price_dropdown($variable_price_dropdown_option, true);
443 443
     }
444 444
 
445 445
     give_die();
446 446
 }
447 447
 
448
-add_action( 'wp_ajax_give_check_for_form_price_variations_html', 'give_check_for_form_price_variations_html' );
448
+add_action('wp_ajax_give_check_for_form_price_variations_html', 'give_check_for_form_price_variations_html');
Please login to merge, or discard this patch.