Test Failed
Push — backup/issues/1944 ( 2dea4f...018a60 )
by Ravinder
04:15
created

Give_HTML_Elements::forms_dropdown()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 69
Code Lines 47

Duplication

Lines 4
Ratio 5.8 %

Importance

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