PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.2.1
GiveWP – Donation Plugin and Fundraising Platform v4.2.1
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / class-give-html-elements.php
give / includes / admin Last commit date
add-ons 4 years ago donors 2 years ago emails 3 years ago forms 1 year ago payments 1 year ago reports 1 year ago settings 1 year ago shortcodes 1 year ago tools 1 year ago upgrades 1 year ago views 1 year ago abstract-admin-settings-page.php 2 years ago admin-actions.php 1 year ago admin-filters.php 3 years ago admin-footer.php 2 years ago admin-pages.php 1 year ago class-addon-activation-banner.php 1 year ago class-admin-settings.php 1 year ago class-api-keys-table.php 4 years ago class-blank-slate.php 1 year ago class-give-admin.php 5 years ago class-give-html-elements.php 1 year ago class-i18n-module.php 4 years ago dashboard-widgets.php 3 years ago give-metabox-functions.php 3 years ago import-functions.php 2 years ago misc-functions.php 2 years ago plugins.php 3 years ago setting-page-functions.php 6 years ago
class-give-html-elements.php
924 lines
1 <?php
2 /**
3 * HTML elements
4 *
5 * @package Give
6 * @subpackage Classes/Give_HTML_Elements
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly.
13 use Give\Framework\Database\DB;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Give_HTML_Elements Class
21 *
22 * A helper class for outputting common HTML elements, such as product drop downs.
23 *
24 * @since 1.0
25 */
26 class Give_HTML_Elements {
27 /**
28 * Instance.
29 *
30 * @since 1.0
31 * @access private
32 * @var
33 */
34 private static $instance;
35
36 /**
37 * Singleton pattern.
38 *
39 * @since 1.0
40 * @access private
41 */
42 private function __construct() {
43 }
44
45
46 /**
47 * Get instance.
48 *
49 * @since 1.0
50 * @access public
51 * @return Give_HTML_Elements
52 */
53 public static function get_instance() {
54 if ( null === static::$instance ) {
55 self::$instance = new static();
56 }
57
58 return self::$instance;
59 }
60
61
62 /**
63 * Donations Dropdown
64 *
65 * Renders an HTML Dropdown of all the donations.
66 *
67 * @since 1.0
68 * @access public
69 *
70 * @param array $args Arguments for the dropdown.
71 *
72 * @return string Donations dropdown.
73 */
74 public function donations_dropdown( $args = array() ) {
75
76 $defaults = array(
77 'name' => 'donations',
78 'id' => 'donations',
79 'class' => '',
80 'multiple' => false,
81 'selected' => 0,
82 'chosen' => false,
83 'number' => 30,
84 'placeholder' => __( 'Select a donation', 'give' ),
85 );
86
87 $args = wp_parse_args( $args, $defaults );
88
89 $payments = new Give_Payments_Query(
90 array(
91 'number' => $args['number'],
92 )
93 );
94
95 $payments = $payments->get_payments();
96
97 $options = array();
98
99 // Provide nice human readable options.
100 if ( $payments ) {
101 $options[0] = $args['placeholder'];
102 foreach ( $payments as $payment ) {
103
104 $options[ absint( $payment->ID ) ] = esc_html( '#' . $payment->ID . ' - ' . $payment->email . ' - ' . $payment->form_title );
105
106 }
107 } else {
108 $options[0] = __( 'No donations found.', 'give' );
109 }
110
111 $output = $this->select(
112 array(
113 'name' => $args['name'],
114 'selected' => $args['selected'],
115 'id' => $args['id'],
116 'class' => $args['class'],
117 'options' => $options,
118 'chosen' => $args['chosen'],
119 'multiple' => $args['multiple'],
120 'placeholder' => $args['placeholder'],
121 'select_atts' => $args['select_atts'],
122 'show_option_all' => false,
123 'show_option_none' => false,
124 )
125 );
126
127 return $output;
128 }
129
130 /**
131 * Give Forms Dropdown
132 *
133 * Renders an HTML Dropdown of all the Give Forms.
134 *
135 * @since 1.0
136 * @access public
137 *
138 * @param array $args Arguments for the dropdown.
139 *
140 * @return string Give forms dropdown.
141 */
142 public function forms_dropdown( $args = array() ) {
143
144 $defaults = array(
145 'name' => 'forms',
146 'id' => 'forms',
147 'class' => '',
148 'multiple' => false,
149 'selected' => 0,
150 'chosen' => false,
151 'number' => 30,
152 'placeholder' => esc_attr__( 'All Forms', 'give' ),
153 'data' => array(
154 'search-type' => 'form',
155 ),
156 'query_args' => array(),
157 );
158
159 $args = wp_parse_args( $args, $defaults );
160
161 $form_args = wp_parse_args(
162 $args['query_args'],
163 array(
164 'post_type' => 'give_forms',
165 'orderby' => 'ID',
166 'order' => 'DESC',
167 'posts_per_page' => $args['number'],
168 )
169 );
170
171 /**
172 * Filter the forms dropdown.
173 *
174 * @since 2.3.0
175 *
176 * @param array $form_args Arguments for forms_dropdown query.
177 *
178 * @return array Arguments for forms_dropdown query.
179 */
180 $form_args = apply_filters( 'give_forms_dropdown_args', $form_args );
181
182 $cache_key = Give_Cache::get_key( 'give_forms', $form_args, false );
183
184 // Get forms from cache.
185 $forms = Give_Cache::get_db_query( $cache_key );
186
187 if ( is_null( $forms ) ) {
188 $forms = new WP_Query( $form_args );
189 $forms = $forms->posts;
190 Give_Cache::set_db_query( $cache_key, $forms );
191 }
192
193 $options = array();
194
195 // Ensure the selected.
196 if ( false !== $args['selected'] && $args['selected'] !== 0 ) {
197 $options[ $args['selected'] ] = get_the_title( $args['selected'] );
198 }
199
200 $options[0] = esc_html__( 'No forms found.', 'give' );
201 if ( ! empty( $forms ) ) {
202 $options[0] = $args['placeholder'];
203 foreach ( $forms as $form ) {
204 $form_title = empty( $form->post_title )
205 ? sprintf( __( 'Untitled (#%s)', 'give' ), $form->ID )
206 : $form->post_title;
207
208 $options[ absint( $form->ID ) ] = esc_html( $form_title );
209 }
210 }
211
212 $output = $this->select(
213 array(
214 'name' => $args['name'],
215 'selected' => $args['selected'],
216 'id' => $args['id'],
217 'class' => $args['class'],
218 'options' => $options,
219 'chosen' => $args['chosen'],
220 'multiple' => $args['multiple'],
221 'placeholder' => $args['placeholder'],
222 'show_option_all' => false,
223 'show_option_none' => false,
224 'data' => $args['data'],
225 )
226 );
227
228 return $output;
229 }
230
231 /**
232 * @since 4.1.0
233 */
234 public function campaigns_dropdown($args = [])
235 {
236 $defaults = [
237 'name' => 'campaigns',
238 'id' => 'campaigns',
239 'class' => '',
240 'multiple' => false,
241 'selected' => 0,
242 'chosen' => false,
243 'number' => 30,
244 'placeholder' => esc_attr__('All Campaigns', 'give'),
245 'data' => [
246 'search-type' => 'campaign',
247 ],
248 'query_args' => [],
249 ];
250
251 $args = wp_parse_args($args, $defaults);
252
253 $campaigns_args = wp_parse_args(
254 $args['query_args'],
255 [
256 'orderby' => 'id',
257 'order' => 'DESC',
258 'per_page' => $args['number'],
259 ]
260 );
261
262 /**
263 * Filter the campaigns dropdown.
264 *
265 * @since 4.1.0
266 *
267 * @param array $campaigns_args Arguments for campaigns_dropdown query.
268 *
269 * @return array Arguments for campaigns_dropdown query.
270 */
271 $campaigns_args = apply_filters('give_campaigns_dropdown_args', $campaigns_args);
272
273 $campaigns = DB::table('give_campaigns', 'campaigns')
274 ->select(
275 ['campaigns.id', 'id'],
276 ['campaigns.form_id', 'defaultFormId'],
277 ['campaign_title', 'title'],
278 ['GROUP_CONCAT(campaign_forms.form_id)', 'form_ids']
279 )
280 ->join(function ($builder) {
281 $builder
282 ->leftJoin("give_campaign_forms", "campaign_forms")
283 ->on("campaign_forms.campaign_id", "id");
284 })
285 ->groupBy("campaigns.id")
286 ->orderBy($campaigns_args['orderby'], $campaigns_args['order'])
287 ->limit($campaigns_args['per_page'])
288 ->getAll();
289
290 $options = [];
291
292 // Ensure the selected.
293 if (false !== $args['selected'] && $args['selected'] !== 0) {
294 $options[$args['selected']] = get_the_title($args['selected']);
295 }
296
297 $options[0] = esc_html__('No campaigns found.', 'give');
298 if ( ! empty($campaigns)) {
299 $options[0] = $args['placeholder'];
300 foreach ($campaigns as $campaign) {
301 $campaign_title = empty($campaign->title)
302 ? sprintf(__('Untitled (#%s)', 'give'), $campaign->id)
303 : $campaign->title;
304
305 $options[absint($campaign->id)] = esc_html($campaign_title);
306 }
307 }
308
309 $output = $this->select(
310 [
311 'name' => $args['name'],
312 'selected' => $args['selected'],
313 'id' => $args['id'],
314 'class' => $args['class'],
315 'options' => $options,
316 'chosen' => $args['chosen'],
317 'multiple' => $args['multiple'],
318 'placeholder' => $args['placeholder'],
319 'show_option_all' => false,
320 'show_option_none' => false,
321 'data' => array_merge(
322 $args['data'],
323 ['campaigns' => json_encode($campaigns)]
324 ),
325 ]
326 );
327
328 return $output;
329 }
330
331
332 /**
333 * Donors Dropdown
334 *
335 * Renders an HTML Dropdown of all donors.
336 *
337 * @since 1.0
338 * @access public
339 *
340 * @param array $args Arguments for the dropdown.
341 *
342 * @return string Donors dropdown.
343 */
344 public function donor_dropdown( $args = array() ) {
345
346 $defaults = array(
347 'name' => 'donors',
348 'id' => 'donors',
349 'class' => '',
350 'multiple' => false,
351 'selected' => 0,
352 'chosen' => true,
353 'placeholder' => esc_attr__( 'Select a Donor', 'give' ),
354 'number' => 30,
355 'data' => array(
356 'search-type' => 'donor',
357 ),
358 );
359
360 $args = wp_parse_args( $args, $defaults );
361
362 $donors = Give()->donors->get_donors(
363 array(
364 'number' => $args['number'],
365 )
366 );
367
368 $options = array();
369
370 if ( $donors ) {
371 $options[0] = esc_html__( 'No donor attached', 'give' );
372 foreach ( $donors as $donor ) {
373 $donor = give_get_name_with_title_prefixes( $donor );
374 $options[ absint( $donor->id ) ] = esc_html( $donor->name . ' (' . $donor->email . ')' );
375 }
376 } else {
377 $options[0] = esc_html__( 'No donors found.', 'give' );
378 }
379
380 if ( ! empty( $args['selected'] ) ) {
381
382 // If a selected customer has been specified, we need to ensure it's in the initial list of customers displayed.
383 if ( ! array_key_exists( $args['selected'], $options ) ) {
384
385 $donor = new Give_Donor( $args['selected'] );
386
387 if ( $donor ) {
388 $donor = give_get_name_with_title_prefixes( $donor );
389 $options[ absint( $args['selected'] ) ] = esc_html( $donor->name . ' (' . $donor->email . ')' );
390
391 }
392 }
393 }
394
395 $output = $this->select(
396 array(
397 'name' => $args['name'],
398 'selected' => $args['selected'],
399 'id' => $args['id'],
400 'class' => $args['class'] . ' give-customer-select',
401 'options' => $options,
402 'multiple' => $args['multiple'],
403 'chosen' => $args['chosen'],
404 'show_option_all' => false,
405 'show_option_none' => false,
406 'data' => $args['data'],
407 )
408 );
409
410 return $output;
411 }
412
413 /**
414 * Categories Dropdown
415 *
416 * Renders an HTML Dropdown of all the Categories.
417 *
418 * @since 1.0
419 * @access public
420 *
421 * @param string $name Name attribute of the dropdown. Default is 'give_forms_categories'.
422 * @param int $selected Category to select automatically. Default is 0.
423 * @param array $args Select box options.
424 *
425 * @return string Categories dropdown.
426 */
427 public function category_dropdown( $name = 'give_forms_categories', $selected = 0, $args = array() ) {
428 $categories = get_terms( 'give_forms_category', apply_filters( 'give_forms_category_dropdown', array() ) );
429
430 $options = array();
431
432 foreach ( $categories as $category ) {
433 $options[ absint( $category->term_id ) ] = esc_html( $category->name );
434 }
435
436 $output = $this->select(
437 wp_parse_args(
438 $args,
439 array(
440 'name' => $name,
441 'selected' => $selected,
442 'options' => $options,
443 'show_option_all' => esc_html__( 'All Categories', 'give' ),
444 'show_option_none' => false,
445 )
446 )
447 );
448
449 return $output;
450 }
451
452 /**
453 * Tags Dropdown
454 *
455 * Renders an HTML Dropdown of all the Tags.
456 *
457 * @since 1.8
458 * @access public
459 *
460 * @param string $name Name attribute of the dropdown. Default is 'give_forms_tags'.
461 * @param int $selected Tag to select automatically. Default is 0.
462 * @param array $args Select box options.
463 *
464 * @return string Tags dropdown.
465 */
466 public function tags_dropdown( $name = 'give_forms_tags', $selected = 0, $args = array() ) {
467 $tags = get_terms( 'give_forms_tag', apply_filters( 'give_forms_tag_dropdown', array() ) );
468
469 $options = array();
470
471 foreach ( $tags as $tag ) {
472 $options[ absint( $tag->term_id ) ] = esc_html( $tag->name );
473 }
474
475 $output = $this->select(
476 wp_parse_args(
477 $args,
478 array(
479 'name' => $name,
480 'selected' => $selected,
481 'options' => $options,
482 'show_option_all' => esc_html__( 'All Tags', 'give' ),
483 'show_option_none' => false,
484 )
485 )
486 );
487
488 return $output;
489 }
490
491 /**
492 * Years Dropdown
493 *
494 * Renders an HTML Dropdown of years.
495 *
496 * @since 1.0
497 * @access public
498 *
499 * @param string $name Name attribute of the dropdown. Default is 'year'.
500 * @param int $selected Year to select automatically. Default is 0.
501 * @param int $years_before Number of years before the current year the dropdown should start with. Default is 5.
502 * @param int $years_after Number of years after the current year the dropdown should finish at. Default is 0.
503 *
504 * @return string Years dropdown.
505 */
506 public function year_dropdown( $name = 'year', $selected = 0, $years_before = 5, $years_after = 0 ) {
507 $current = date( 'Y' );
508 $start_year = $current - absint( $years_before );
509 $end_year = $current + absint( $years_after );
510 $selected = empty( $selected ) ? date( 'Y' ) : $selected;
511 $options = array();
512
513 while ( $start_year <= $end_year ) {
514 $options[ absint( $start_year ) ] = $start_year;
515 $start_year ++;
516 }
517
518 $output = $this->select(
519 array(
520 'name' => $name,
521 'selected' => $selected,
522 'options' => $options,
523 'show_option_all' => false,
524 'show_option_none' => false,
525 )
526 );
527
528 return $output;
529 }
530
531 /**
532 * Months Dropdown
533 *
534 * Renders an HTML Dropdown of months.
535 *
536 * @since 1.0
537 * @access public
538 *
539 * @param string $name Name attribute of the dropdown. Default is 'month'.
540 * @param int $selected Month to select automatically. Default is 0.
541 *
542 * @return string Months dropdown.
543 */
544 public function month_dropdown( $name = 'month', $selected = 0 ) {
545 $month = 1;
546 $options = array();
547 $selected = empty( $selected ) ? date( 'n' ) : $selected;
548
549 while ( $month <= 12 ) {
550 $options[ absint( $month ) ] = give_month_num_to_name( $month );
551 $month ++;
552 }
553
554 $output = $this->select(
555 array(
556 'name' => $name,
557 'selected' => $selected,
558 'options' => $options,
559 'show_option_all' => false,
560 'show_option_none' => false,
561 )
562 );
563
564 return $output;
565 }
566
567 /**
568 * Dropdown
569 *
570 * Renders an HTML Dropdown.
571 *
572 * @since 1.0
573 * @access public
574 *
575 * @param array $args Arguments for the dropdown.
576 *
577 * @return string The dropdown.
578 */
579 public function select( $args = array() ) {
580 $defaults = array(
581 'options' => array(),
582 'name' => null,
583 'class' => '',
584 'id' => '',
585 'autocomplete' => 'no',
586 'selected' => 0,
587 'chosen' => false,
588 'placeholder' => null,
589 'multiple' => false,
590 'select_atts' => false,
591 'show_option_all' => __( 'All', 'give' ),
592 'show_option_none' => __( 'None', 'give' ),
593 'data' => array(),
594 'readonly' => false,
595 'disabled' => false,
596 );
597
598 $args = wp_parse_args( $args, $defaults );
599
600 $data_elements = '';
601 foreach ( $args['data'] as $key => $value ) {
602 $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
603 }
604
605 $multiple = '';
606 if ( $args['multiple'] ) {
607 $multiple = 'MULTIPLE';
608 }
609
610 if ( $args['chosen'] ) {
611 $args['class'] .= ' give-select-chosen';
612 }
613
614 $placeholder = '';
615 if ( $args['placeholder'] ) {
616 $placeholder = $args['placeholder'];
617 }
618
619 $output = sprintf(
620 '<select name="%1$s" id="%2$s" autocomplete="%8$s" class="give-select %3$s" %4$s %5$s placeholder="%6$s" data-placeholder="%6$s" %7$s>',
621 esc_attr( $args['name'] ),
622 esc_attr( sanitize_key( str_replace( '-', '_', $args['id'] ) ) ),
623 esc_attr( $args['class'] ),
624 $multiple,
625 $args['select_atts'],
626 $placeholder,
627 $data_elements,
628 $args['autocomplete']
629 );
630
631 if ( $args['show_option_all'] ) {
632 if ( $args['multiple'] ) {
633 $selected = selected( true, in_array( 0, $args['selected'] ), false );
634 } else {
635 $selected = selected( $args['selected'], 0, false );
636 }
637 $output .= '<option value="all"' . $selected . '>' . esc_html( $args['show_option_all'] ) . '</option>';
638 }
639
640 if ( ! empty( $args['options'] ) ) {
641
642 if ( $args['show_option_none'] ) {
643 if ( $args['multiple'] ) {
644 $selected = selected( true, in_array( - 1, $args['selected'] ), false );
645 } else {
646 $selected = selected( $args['selected'], - 1, false );
647 }
648 $output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
649 }
650
651 foreach ( $args['options'] as $key => $option ) {
652
653 if ( $args['multiple'] && is_array( $args['selected'] ) ) {
654 $selected = selected( true, in_array( $key, $args['selected'] ), false );
655 } else {
656 $selected = selected( $args['selected'], $key, false );
657 }
658
659 $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
660 }
661 }
662
663 $output .= '</select>';
664
665 return $output;
666 }
667
668 /**
669 * Checkbox
670 *
671 * Renders an HTML Checkbox.
672 *
673 * @since 1.0
674 * @access public
675 *
676 * @param array $args Arguments for the Checkbox.
677 *
678 * @return string The checkbox.
679 */
680 public function checkbox( $args = array() ) {
681 $defaults = array(
682 'name' => null,
683 'current' => null,
684 'class' => 'give-checkbox',
685 'options' => array(
686 'disabled' => false,
687 'readonly' => false,
688 ),
689 );
690
691 $args = wp_parse_args( $args, $defaults );
692
693 $options = '';
694 if ( ! empty( $args['options']['disabled'] ) ) {
695 $options .= ' disabled="disabled"';
696 } elseif ( ! empty( $args['options']['readonly'] ) ) {
697 $options .= ' readonly';
698 }
699
700 $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 ) . ' />';
701
702 return $output;
703 }
704
705 /**
706 * Text Field
707 *
708 * Renders an HTML Text field.
709 *
710 * @since 1.0
711 * @access public
712 *
713 * @param array $args Arguments for the text field.
714 *
715 * @return string The text field.
716 */
717 public function text( $args = array() ) {
718 // Backwards compatibility.
719 if ( func_num_args() > 1 ) {
720 $args = func_get_args();
721
722 $name = $args[0];
723 $value = isset( $args[1] ) ? $args[1] : '';
724 $label = isset( $args[2] ) ? $args[2] : '';
725 $desc = isset( $args[3] ) ? $args[3] : '';
726 }
727
728 $defaults = array(
729 'name' => isset( $name ) ? $name : 'text',
730 'value' => isset( $value ) ? $value : null,
731 'label' => isset( $label ) ? $label : null,
732 'desc' => isset( $desc ) ? $desc : null,
733 'placeholder' => '',
734 'class' => 'regular-text',
735 'disabled' => false,
736 'autocomplete' => '',
737 'data' => false,
738 );
739
740 $args = wp_parse_args( $args, $defaults );
741
742 $disabled = '';
743 if ( $args['disabled'] ) {
744 $disabled = ' disabled="disabled"';
745 }
746
747 $data = '';
748 if ( ! empty( $args['data'] ) ) {
749 foreach ( $args['data'] as $key => $value ) {
750 $data .= 'data-' . $key . '="' . $value . '" ';
751 }
752 }
753
754 $output = '<span id="give-' . sanitize_key( $args['name'] ) . '-wrap">';
755
756 // Don't output label when the label is empty.
757 if ( ! empty( $args['label'] ) ) {
758 $output .= '<label class="give-label" for="give-' . sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
759 }
760
761 if ( ! empty( $args['desc'] ) ) {
762 $output .= '<span class="give-description">' . esc_html( $args['desc'] ) . '</span>';
763 }
764
765 $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 . '/>';
766
767 $output .= '</span>';
768
769 return $output;
770 }
771
772 /**
773 * Date Picker
774 *
775 * Renders a date picker field.
776 *
777 * @since 1.5
778 * @access public
779 *
780 * @param array $args Arguments for the date picker.
781 *
782 * @return string The date picker.
783 */
784 public function date_field( $args = array() ) {
785
786 if ( empty( $args['class'] ) ) {
787 $args['class'] = 'give_datepicker';
788 } elseif ( ! strpos( $args['class'], 'give_datepicker' ) ) {
789 $args['class'] .= ' give_datepicker';
790 }
791
792 return $this->text( $args );
793 }
794
795 /**
796 * Textarea
797 *
798 * Renders an HTML textarea.
799 *
800 * @since 1.0
801 * @access public
802 *
803 * @param array $args Arguments for the textarea.
804 *
805 * @return string The textarea.
806 */
807 public function textarea( $args = array() ) {
808 $defaults = array(
809 'name' => 'textarea',
810 'value' => null,
811 'label' => null,
812 'desc' => null,
813 'class' => 'large-text',
814 'disabled' => false,
815 );
816
817 $args = wp_parse_args( $args, $defaults );
818
819 $disabled = '';
820 if ( $args['disabled'] ) {
821 $disabled = ' disabled="disabled"';
822 }
823
824 $output = '<span id="give-' . sanitize_key( $args['name'] ) . '-wrap">';
825
826 $output .= '<label class="give-label" for="give-' . sanitize_key( $args['name'] ) . '">' . esc_html( $args['label'] ) . '</label>';
827
828 $output .= '<textarea name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( $args['name'] ) . '" class="' . $args['class'] . '"' . $disabled . '>' . esc_attr( $args['value'] ) . '</textarea>';
829
830 if ( ! empty( $args['desc'] ) ) {
831 $output .= '<span class="give-description">' . esc_html( $args['desc'] ) . '</span>';
832 }
833
834 $output .= '</span>';
835
836 return $output;
837 }
838
839 /**
840 * User Search Field
841 *
842 * Renders an ajax user search field.
843 *
844 * @since 1.0
845 * @access public
846 *
847 * @param array $args Arguments for the search field.
848 *
849 * @return string The text field with ajax search.
850 */
851 public function ajax_user_search( $args = array() ) {
852
853 $defaults = array(
854 'name' => 'users',
855 'id' => 'users',
856 'class' => 'give-ajax-user-search',
857 'multiple' => false,
858 'selected' => 0,
859 'chosen' => true,
860 'number' => 30,
861 'select_atts' => '',
862 'placeholder' => __( 'Select a user', 'give' ),
863 'data' => array(
864 'search-type' => 'user',
865 ),
866 );
867
868 $args = wp_parse_args( $args, $defaults );
869
870 // Set initial args.
871 $get_users_args = array(
872 'number' => $args['number'],
873 );
874
875 // Ensure selected user is not included in initial query.
876 // This is because sites with many users, it's not a guarantee the selected user will be returned.
877 if ( ! empty( $args['selected'] ) ) {
878 $get_users_args['exclude'] = $args['selected'];
879 }
880
881 // Initial users array.
882 $users = apply_filters( 'give_ajax_user_search_initial_results', get_users( $get_users_args ), $args );
883
884 // Now add the selected user to the $users array if the arg is present.
885 if ( ! empty( $args['selected'] ) ) {
886 $selected_user = apply_filters( 'give_ajax_user_search_selected_results', get_users( "include={$args['selected']}" ), $args );
887
888 $users = array_merge( $users, $selected_user );
889 }
890
891 $options = array();
892
893 if ( $users ) {
894 $options[0] = $args['placeholder'];
895 foreach ( $users as $user ) {
896 $options[ absint( $user->ID ) ] = esc_html( $user->user_login . ' (' . $user->user_email . ')' );
897 }
898 } else {
899 $options[0] = __( 'No users found.', 'give' );
900 }
901
902 $output = $this->select(
903 array(
904 'name' => $args['name'],
905 'selected' => $args['selected'],
906 'id' => $args['id'],
907 'class' => $args['class'],
908 'options' => $options,
909 'chosen' => $args['chosen'],
910 'multiple' => $args['multiple'],
911 'placeholder' => $args['placeholder'],
912 'select_atts' => $args['select_atts'],
913 'show_option_all' => false,
914 'show_option_none' => false,
915 'data' => $args['data'],
916 )
917 );
918
919 return $output;
920
921 }
922
923 }
924