Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

Give_HTML_Elements::checkbox()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 24
ccs 0
cts 9
cp 0
crap 12
rs 8.9713
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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