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