Test Failed
Pull Request — master (#1942)
by Devin
05:03
created

Give_HTML_Elements::ajax_user_search()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 68
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 43
nc 8
nop 1
dl 0
loc 68
ccs 0
cts 0
cp 0
crap 30
rs 8.5748
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * HTML elements
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_HTML_Elements
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Give_HTML_Elements Class
19
 *
20
 * A helper class for outputting common HTML elements, such as product drop downs.
21
 *
22
 * @since 1.0
23
 */
24
class Give_HTML_Elements {
25
26
	/**
27
	 * Donations Dropdown
28
	 *
29
	 * Renders an HTML Dropdown of all the donations.
30
	 *
31
	 * @since  1.0
32
	 * @access public
33
	 *
34
	 * @param  array $args Arguments for the dropdown.
35
	 *
36
	 * @return string       Donations dropdown.
37
	 */
38
	public function donations_dropdown( $args = array() ) {
39
40
		$defaults = array(
41
			'name'        => 'donations',
42
			'id'          => 'donations',
43
			'class'       => '',
44
			'multiple'    => false,
45
			'selected'    => 0,
46
			'chosen'      => false,
47
			'number'      => 30,
48
			'placeholder' => __( 'Select a donation', 'give' ),
49
		);
50
51
		$args = wp_parse_args( $args, $defaults );
52
53
		$payments = new Give_Payments_Query( array(
54
			'number' => $args['number'],
55
		) );
56
57
		$payments = $payments->get_payments();
58
59
		$options = array();
60
61
		// Provide nice human readable options.
62
		if ( $payments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
			$options[0] = $args['placeholder'];
64
			foreach ( $payments as $payment ) {
65
66
				$options[ absint( $payment->ID ) ] = esc_html( '#' . $payment->ID . ' - ' . $payment->email . ' - ' . $payment->form_title );
67
68
			}
69
		} else {
70
			$options[0] = __( 'No donations found.', 'give' );
71
		}
72
73
		$output = $this->select( array(
74
			'name'             => $args['name'],
75
			'selected'         => $args['selected'],
76
			'id'               => $args['id'],
77
			'class'            => $args['class'],
78
			'options'          => $options,
79
			'chosen'           => $args['chosen'],
80
			'multiple'         => $args['multiple'],
81
			'placeholder'      => $args['placeholder'],
82
			'select_atts'      => $args['select_atts'],
83
			'show_option_all'  => false,
84
			'show_option_none' => false,
85
		) );
86
87
		return $output;
88
	}
89
90
	/**
91
	 * Give Forms Dropdown
92
	 *
93
	 * Renders an HTML Dropdown of all the Give Forms.
94
	 *
95
	 * @since  1.0
96
	 * @access public
97
	 *
98
	 * @param  array $args Arguments for the dropdown.
99
	 *
100
	 * @return string      Give forms dropdown.
101
	 */
102
	public function forms_dropdown( $args = array() ) {
103
104
		$defaults = array(
105
			'name'        => 'forms',
106
			'id'          => 'forms',
107
			'class'       => '',
108
			'multiple'    => false,
109
			'selected'    => 0,
110
			'chosen'      => false,
111
			'number'      => 30,
112
			'placeholder' => esc_attr__( 'Select a Donation Form', 'give' ),
113
			'data'        => array(
114
				'search-type' => 'form',
115
			),
116
		);
117
118
		$args = wp_parse_args( $args, $defaults );
119
120
		$forms = get_posts( array(
121
			'post_type'      => 'give_forms',
122
			'orderby'        => 'title',
123
			'order'          => 'ASC',
124
			'posts_per_page' => $args['number'],
125
		) );
126
127
		$options = array();
128
129
		// Ensure the selected.
130
		if ( false !== $args['selected'] && $args['selected'] !== 0 ) {
0 ignored issues
show
introduced by
Found "!== 0". Use Yoda Condition checks, you must
Loading history...
131
			$options[ $args['selected'] ] = get_the_title( $args['selected'] );
132
		}
133
134
		if ( $forms ) {
135
			$options[0] = $args['placeholder'];
136 View Code Duplication
			foreach ( $forms as $form ) {
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...
137
				$form_title                     = empty( $form->post_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $form->ID ) : $form->post_title;
138
				$options[ absint( $form->ID ) ] = esc_html( $form_title );
139
			}
140
		} else {
141
			$options[0] = esc_html__( 'No forms found.', 'give' );
142
		}
143
144
		$output = $this->select( array(
145
			'name'             => $args['name'],
146
			'selected'         => $args['selected'],
147
			'id'               => $args['id'],
148
			'class'            => $args['class'],
149
			'options'          => $options,
150
			'chosen'           => $args['chosen'],
151
			'multiple'         => $args['multiple'],
152
			'placeholder'      => $args['placeholder'],
153
			'show_option_all'  => false,
154
			'show_option_none' => false,
155
			'data'             => $args['data'],
156
		) );
157
158
		return $output;
159
	}
160
161
	/**
162
	 * Donors Dropdown
163
	 *
164
	 * Renders an HTML Dropdown of all customers.
165
	 *
166
	 * @since  1.0
167
	 * @access public
168
	 *
169
	 * @param  array $args Arguments for the dropdown.
170
	 *
171
	 * @return string      Donors dropdown.
172
	 */
173
	public function donor_dropdown( $args = array() ) {
174
175
		$defaults = array(
176
			'name'        => 'donors',
177
			'id'          => 'donors',
178
			'class'       => '',
179
			'multiple'    => false,
180
			'selected'    => 0,
181
			'chosen'      => true,
182
			'placeholder' => esc_attr__( 'Select a Donor', 'give' ),
183
			'number'      => 30,
184
			'data'        => array(
185
				'search-type' => 'donor',
186
			),
187
		);
188
189
		$args = wp_parse_args( $args, $defaults );
190
191
		$donors = Give()->donors->get_donors( array(
192
			'number' => $args['number']
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
193
		) );
194
195
		$options = array();
196
197 View Code Duplication
		if ( $donors ) {
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...
198
			$options[0] = esc_html__( 'No donor attached', 'give' );
199
			foreach ( $donors as $donor ) {
200
				$options[ absint( $donor->id ) ] = esc_html( $donor->name . ' (' . $donor->email . ')' );
201
			}
202
		} else {
203
			$options[0] = esc_html__( 'No donors found.', 'give' );
204
		}
205
206
		if ( ! empty( $args['selected'] ) ) {
207
208
			// If a selected customer has been specified, we need to ensure it's in the initial list of customers displayed.
209
			if ( ! array_key_exists( $args['selected'], $options ) ) {
210
211
				$donor = new Give_Donor( $args['selected'] );
212
213
				if ( $donor ) {
214
215
					$options[ absint( $args['selected'] ) ] = esc_html( $donor->name . ' (' . $donor->email . ')' );
216
217
				}
218
			}
219
		}
220
221
		$output = $this->select( array(
222
			'name'             => $args['name'],
223
			'selected'         => $args['selected'],
224
			'id'               => $args['id'],
225
			'class'            => $args['class'] . ' give-customer-select',
226
			'options'          => $options,
227
			'multiple'         => $args['multiple'],
228
			'chosen'           => $args['chosen'],
229
			'show_option_all'  => false,
230
			'show_option_none' => false,
231
			'data'             => $args['data'],
232
		) );
233
234
		return $output;
235
	}
236
237
	/**
238
	 * Categories Dropdown
239
	 *
240
	 * Renders an HTML Dropdown of all the Categories.
241
	 *
242
	 * @since  1.0
243
	 * @access public
244
	 *
245
	 * @param  string $name     Name attribute of the dropdown. Default is 'give_forms_categories'.
246
	 * @param  int    $selected Category to select automatically. Default is 0.
247
	 * @param  array  $args     Select box options.
248
	 *
249
	 * @return string           Categories dropdown.
250
	 */
251 View Code Duplication
	public function category_dropdown( $name = 'give_forms_categories', $selected = 0, $args = array() ) {
0 ignored issues
show
Duplication introduced by
This method 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...
252
		$categories = get_terms( 'give_forms_category', apply_filters( 'give_forms_category_dropdown', array() ) );
253
		$options    = array();
254
255
		foreach ( $categories as $category ) {
256
			$options[ absint( $category->term_id ) ] = esc_html( $category->name );
257
		}
258
259
		$output = $this->select( wp_parse_args( $args, array(
260
			'name'             => $name,
261
			'selected'         => $selected,
262
			'options'          => $options,
263
			'show_option_all'  => esc_html__( 'All Categories', 'give' ),
264
			'show_option_none' => false,
265
		) ) );
266
267
		return $output;
268
	}
269
270
	/**
271
	 * Tags Dropdown
272
	 *
273
	 * Renders an HTML Dropdown of all the Tags.
274
	 *
275
	 * @since  1.8
276
	 * @access public
277
	 *
278
	 * @param  string $name     Name attribute of the dropdown. Default is 'give_forms_tags'.
279
	 * @param  int    $selected Tag to select automatically. Default is 0.
280
	 * @param  array  $args     Select box options.
281
	 *
282
	 * @return string           Tags dropdown.
283
	 */
284 View Code Duplication
	public function tags_dropdown( $name = 'give_forms_tags', $selected = 0, $args = array() ) {
0 ignored issues
show
Duplication introduced by
This method 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...
285
		$tags    = get_terms( 'give_forms_tag', apply_filters( 'give_forms_tag_dropdown', array() ) );
286
		$options = array();
287
288
		foreach ( $tags as $tag ) {
289
			$options[ absint( $tag->term_id ) ] = esc_html( $tag->name );
290
		}
291
292
		$output = $this->select( wp_parse_args( $args, array(
293
			'name'             => $name,
294
			'selected'         => $selected,
295
			'options'          => $options,
296
			'show_option_all'  => esc_html__( 'All Tags', 'give' ),
297
			'show_option_none' => false,
298
		) ) );
299
300
		return $output;
301
	}
302
303
	/**
304
	 * Years Dropdown
305
	 *
306
	 * Renders an HTML Dropdown of years.
307
	 *
308
	 * @since  1.0
309
	 * @access public
310
	 *
311
	 * @param  string $name         Name attribute of the dropdown. Default is 'year'.
312
	 * @param  int    $selected     Year to select automatically. Default is 0.
313
	 * @param  int    $years_before Number of years before the current year the dropdown should start with. Default is 5.
314
	 * @param  int    $years_after  Number of years after the current year the dropdown should finish at. Default is 0.
315
	 *
316
	 * @return string               Years dropdown.
317
	 */
318
	public function year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
319
		$current    = date( 'Y' );
320
		$start_year = $current - absint( $years_before );
321
		$end_year   = $current + absint( $years_after );
322
		$selected   = empty( $selected ) ? date( 'Y' ) : $selected;
323
		$options    = array();
324
325
		while ( $start_year <= $end_year ) {
326
			$options[ absint( $start_year ) ] = $start_year;
327
			$start_year ++;
328
		}
329
330
		$output = $this->select( array(
331
			'name'             => $name,
332
			'selected'         => $selected,
333
			'options'          => $options,
334
			'show_option_all'  => false,
335
			'show_option_none' => false,
336
		) );
337
338
		return $output;
339
	}
340
341
	/**
342
	 * Months Dropdown
343
	 *
344
	 * Renders an HTML Dropdown of months.
345
	 *
346
	 * @since  1.0
347
	 * @access public
348
	 *
349
	 * @param  string $name     Name attribute of the dropdown. Default is 'month'.
350
	 * @param  int    $selected Month to select automatically. Default is 0.
351
	 *
352
	 * @return string           Months dropdown.
353
	 */
354
	public function month_dropdown( $name = 'month', $selected = 0 ) {
355
		$month    = 1;
356
		$options  = array();
357
		$selected = empty( $selected ) ? date( 'n' ) : $selected;
358
359
		while ( $month <= 12 ) {
360
			$options[ absint( $month ) ] = give_month_num_to_name( $month );
361
			$month ++;
362
		}
363
364
		$output = $this->select( array(
365
			'name'             => $name,
366
			'selected'         => $selected,
367
			'options'          => $options,
368
			'show_option_all'  => false,
369
			'show_option_none' => false,
370
		) );
371
372
		return $output;
373
	}
374
375
	/**
376
	 * Dropdown
377
	 *
378
	 * Renders an HTML Dropdown.
379
	 *
380
	 * @since  1.0
381
	 * @access public
382
	 *
383
	 * @param  array $args Arguments for the dropdown.
384
	 *
385
	 * @return string      The dropdown.
386
	 */
387
	public function select( $args = array() ) {
388
		$defaults = array(
389
			'options'          => array(),
390
			'name'             => null,
391
			'class'            => '',
392
			'id'               => '',
393
			'selected'         => 0,
394
			'chosen'           => false,
395
			'placeholder'      => null,
396
			'multiple'         => false,
397
			'select_atts'      => false,
398
			'show_option_all'  => __( 'All', 'give' ),
399
			'show_option_none' => __( 'None', 'give' ),
400
			'data'             => array(),
401
			'readonly'         => false,
402
			'disabled'         => false,
403
		);
404
405
		$args = wp_parse_args( $args, $defaults );
406
407
		$data_elements = '';
408
		foreach ( $args['data'] as $key => $value ) {
409
			$data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
410
		}
411
412
		if ( $args['multiple'] ) {
413
			$multiple = ' MULTIPLE';
414
		} else {
415
			$multiple = '';
416
		}
417
418
		if ( $args['chosen'] ) {
419
			$args['class'] .= ' give-select-chosen';
420
		}
421
422
		if ( $args['placeholder'] ) {
423
			$placeholder = $args['placeholder'];
424
		} else {
425
			$placeholder = '';
426
		}
427
428
		$output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( sanitize_key( str_replace( '-', '_', $args['id'] ) ) ) . '" class="give-select ' . esc_attr( $args['class'] ) . '"' . $multiple . ' ' . $args['select_atts'] . ' data-placeholder="' . $placeholder . '"' . $data_elements . '>';
429
430 View Code Duplication
		if ( $args['show_option_all'] ) {
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...
431
			if ( $args['multiple'] ) {
432
				$selected = selected( true, in_array( 0, $args['selected'] ), false );
433
			} else {
434
				$selected = selected( $args['selected'], 0, false );
435
			}
436
			$output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
437
		}
438
439
		if ( ! empty( $args['options'] ) ) {
440
441 View Code Duplication
			if ( $args['show_option_none'] ) {
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...
442
				if ( $args['multiple'] ) {
443
					$selected = selected( true, in_array( - 1, $args['selected'] ), false );
444
				} else {
445
					$selected = selected( $args['selected'], - 1, false );
446
				}
447
				$output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
448
			}
449
450
			foreach ( $args['options'] as $key => $option ) {
451
452
				if ( $args['multiple'] && is_array( $args['selected'] ) ) {
453
					$selected = selected( true, in_array( $key, $args['selected'] ), false );
454
				} else {
455
					$selected = selected( $args['selected'], $key, false );
456
				}
457
458
				$output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
459
			}
460
		}
461
462
		$output .= '</select>';
463
464
		return $output;
465
	}
466
467
	/**
468
	 * Checkbox
469
	 *
470
	 * Renders an HTML Checkbox.
471
	 *
472
	 * @since  1.0
473
	 * @access public
474
	 *
475
	 * @param  array $args Arguments for the Checkbox.
476
	 *
477
	 * @return string      The checkbox.
478
	 */
479
	public function checkbox( $args = array() ) {
480
		$defaults = array(
481
			'name'    => null,
482
			'current' => null,
483
			'class'   => 'give-checkbox',
484
			'options' => array(
485
				'disabled' => false,
486
				'readonly' => false,
487
			),
488
		);
489
490
		$args = wp_parse_args( $args, $defaults );
491
492
		$options = '';
493
		if ( ! empty( $args['options']['disabled'] ) ) {
494
			$options .= ' disabled="disabled"';
495
		} elseif ( ! empty( $args['options']['readonly'] ) ) {
496
			$options .= ' readonly';
497
		}
498
499
		$output = '<input type="checkbox"' . $options . ' name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" class="' . $args['class'] . ' ' . esc_attr( $args['name'] ) . '" ' . checked( 1, $args['current'], false ) . ' />';
500
501
		return $output;
502
	}
503
504
	/**
505
	 * Text Field
506
	 *
507
	 * Renders an HTML Text field.
508
	 *
509
	 * @since  1.0
510
	 * @access public
511
	 *
512
	 * @param  array $args Arguments for the text field.
513
	 *
514
	 * @return string      The text field.
515
	 */
516
	public function text( $args = array() ) {
517
		// Backwards compatibility.
518
		if ( func_num_args() > 1 ) {
519
			$args = func_get_args();
520
521
			$name  = $args[0];
522
			$value = isset( $args[1] ) ? $args[1] : '';
523
			$label = isset( $args[2] ) ? $args[2] : '';
524
			$desc  = isset( $args[3] ) ? $args[3] : '';
525
		}
526
527
		$defaults = array(
528
			'name'         => isset( $name ) ? $name : 'text',
529
			'value'        => isset( $value ) ? $value : null,
530
			'label'        => isset( $label ) ? $label : null,
531
			'desc'         => isset( $desc ) ? $desc : null,
532
			'placeholder'  => '',
533
			'class'        => 'regular-text',
534
			'disabled'     => false,
535
			'autocomplete' => '',
536
			'data'         => false,
537
		);
538
539
		$args = wp_parse_args( $args, $defaults );
540
541
		$disabled = '';
542
		if ( $args['disabled'] ) {
543
			$disabled = ' disabled="disabled"';
544
		}
545
546
		$data = '';
547
		if ( ! empty( $args['data'] ) ) {
548
			foreach ( $args['data'] as $key => $value ) {
549
				$data .= 'data-' . $key . '="' . $value . '" ';
550
			}
551
		}
552
553
		$output = '<span id="give-' . sanitize_key( $args['name'] ) . '-wrap">';
554
555
		$output .= '<label class="give-label" for="give-' . sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
556
557 View Code Duplication
		if ( ! empty( $args['desc'] ) ) {
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...
558
			$output .= '<span class="give-description">' . esc_html( $args['desc'] ) . '</span>';
559
		}
560
561
		$output .= '<input type="text" name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" autocomplete="' . esc_attr( $args['autocomplete'] ) . '" value="' . esc_attr( $args['value'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" class="' . $args['class'] . '" ' . $data . '' . $disabled . '/>';
562
563
		$output .= '</span>';
564
565
		return $output;
566
	}
567
568
	/**
569
	 * Date Picker
570
	 *
571
	 * Renders a date picker field.
572
	 *
573
	 * @since  1.5
574
	 * @access public
575
	 *
576
	 * @param  array $args Arguments for the date picker.
577
	 *
578
	 * @return string      The date picker.
579
	 */
580
	public function date_field( $args = array() ) {
581
582
		if ( empty( $args['class'] ) ) {
583
			$args['class'] = 'give_datepicker';
584
		} elseif ( ! strpos( $args['class'], 'give_datepicker' ) ) {
585
			$args['class'] .= ' give_datepicker';
586
		}
587
588
		return $this->text( $args );
589
	}
590
591
	/**
592
	 * Textarea
593
	 *
594
	 * Renders an HTML textarea.
595
	 *
596
	 * @since  1.0
597
	 * @access public
598
	 *
599
	 * @param  array $args Arguments for the textarea.
600
	 *
601
	 * @return string      The textarea.
602
	 */
603
	public function textarea( $args = array() ) {
604
		$defaults = array(
605
			'name'     => 'textarea',
606
			'value'    => null,
607
			'label'    => null,
608
			'desc'     => null,
609
			'class'    => 'large-text',
610
			'disabled' => false,
611
		);
612
613
		$args = wp_parse_args( $args, $defaults );
614
615
		$disabled = '';
616
		if ( $args['disabled'] ) {
617
			$disabled = ' disabled="disabled"';
618
		}
619
620
		$output = '<span id="give-' . sanitize_key( $args['name'] ) . '-wrap">';
621
622
		$output .= '<label class="give-label" for="give-' . sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
623
624
		$output .= '<textarea name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" class="' . $args['class'] . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>';
625
626 View Code Duplication
		if ( ! empty( $args['desc'] ) ) {
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...
627
			$output .= '<span class="give-description">' . esc_html( $args['desc'] ) . '</span>';
628
		}
629
630
		$output .= '</span>';
631
632
		return $output;
633
	}
634
635
	/**
636
	 * User Search Field
637
	 *
638
	 * Renders an ajax user search field.
639
	 *
640
	 * @since  1.0
641
	 * @access public
642
	 *
643
	 * @param  array $args Arguments for the search field.
644
	 *
645
	 * @return string      The text field with ajax search.
646
	 */
647
	public function ajax_user_search( $args = array() ) {
648
649
		$defaults = array(
650
			'name'        => 'users',
651
			'id'          => 'users',
652
			'class'       => 'give-ajax-user-search',
653
			'multiple'    => false,
654
			'selected'    => 0,
655
			'chosen'      => true,
656
			'number'      => 30,
657
			'select_atts' => '',
658
			'placeholder' => __( 'Select a user', 'give' ),
659
			'data'        => array(
660
				'search-type' => 'user',
661
			),
662
		);
663
664
		$args = wp_parse_args( $args, $defaults );
665
666
		// Set initial args.
667
		$get_users_args = array(
668
			'number' => $args['number'],
669
		);
670
671
		// Ensure selected user is not included in initial query.
672
		// This is because sites with many users, it's not a guarantee the selected user will be returned.
673
		if ( ! empty( $args['selected'] ) ) {
674
			$get_users_args['exclude'] = $args['selected'];
675
		}
676
677
		// Initial users array.
678
		$users = apply_filters( 'give_ajax_user_search_initial_results', get_users( $get_users_args ), $args );
679
680
		// Now add the selected user to the $users array if the arg is present.
681
		if ( ! empty( $args['selected'] ) ) {
682
			$selected_user =  apply_filters( 'give_ajax_user_search_selected_results', get_users( "include={$args['selected']}" ), $args );;
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
683
			$users         = array_merge( $users, $selected_user );
684
		}
685
686
		$options = array();
687
688
		if ( $users ) {
689
			$options[0] = $args['placeholder'];
690
			foreach ( $users as $user ) {
691
				$options[ absint( $user->ID ) ] = esc_html( $user->user_login . ' (' . $user->user_email . ')' );
692
			}
693
		} else {
694
			$options[0] = __( 'No users found.', 'give' );
695
		}
696
697
		$output = $this->select( array(
698
			'name'             => $args['name'],
699
			'selected'         => $args['selected'],
700
			'id'               => $args['id'],
701
			'class'            => $args['class'],
702
			'options'          => $options,
703
			'chosen'           => $args['chosen'],
704
			'multiple'         => $args['multiple'],
705
			'placeholder'      => $args['placeholder'],
706
			'select_atts'      => $args['select_atts'],
707
			'show_option_all'  => false,
708
			'show_option_none' => false,
709
			'data'             => $args['data'],
710
		) );
711
712
		return $output;
713
714
	}
715
716
}
717