Give_HTML_Elements   F
last analyzed

Complexity

Total Complexity 66

Size/Duplication

Total Lines 720
Duplicated Lines 11.94 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 86
loc 720
ccs 0
cts 282
cp 0
rs 3
c 0
b 0
f 0
wmc 66
lcom 1
cbo 5

13 Methods

Rating   Name   Duplication   Size   Complexity  
A donations_dropdown() 10 51 3
B forms_dropdown() 10 75 7
B donor_dropdown() 0 64 6
A category_dropdown() 18 19 2
A tags_dropdown() 18 19 2
A year_dropdown() 0 22 3
A month_dropdown() 0 20 3
D select() 16 86 13
A checkbox() 0 24 3
F text() 3 51 13
A date_field() 0 10 3
A textarea() 3 31 3
B ajax_user_search() 8 68 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Give_HTML_Elements often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Give_HTML_Elements, and based on these observations, apply Extract Interface, too.

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