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