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