Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Field.php
841 lines
| 1 | <?php |
| 2 | |
| 3 | // Don't load directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | die( '-1' ); |
| 6 | } |
| 7 | |
| 8 | if ( ! class_exists( 'Tribe__Field' ) ) { |
| 9 | /** |
| 10 | * helper class that creates fields for use in Settings, MetaBoxes, Users, anywhere. |
| 11 | * Instantiate it whenever you need a field |
| 12 | * |
| 13 | */ |
| 14 | class Tribe__Field { |
| 15 | |
| 16 | /** |
| 17 | * the field's id |
| 18 | * @var string |
| 19 | */ |
| 20 | public $id; |
| 21 | |
| 22 | /** |
| 23 | * the field's name (also known as it's label) |
| 24 | * @var string |
| 25 | */ |
| 26 | public $name; |
| 27 | |
| 28 | /** |
| 29 | * the fieldset attributes |
| 30 | * @var array |
| 31 | */ |
| 32 | public $fieldset_attributes; |
| 33 | |
| 34 | /** |
| 35 | * the field attributes |
| 36 | * @var array |
| 37 | */ |
| 38 | public $attributes; |
| 39 | |
| 40 | /** |
| 41 | * the field's arguments |
| 42 | * @var array |
| 43 | */ |
| 44 | public $args; |
| 45 | |
| 46 | /** |
| 47 | * field defaults (static) |
| 48 | * @var array |
| 49 | */ |
| 50 | public $defaults; |
| 51 | |
| 52 | /** |
| 53 | * valid field types (static) |
| 54 | * @var array |
| 55 | */ |
| 56 | public $valid_field_types; |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Class constructor |
| 61 | * |
| 62 | * @param string $id the field id |
| 63 | * @param array $field the field settings |
| 64 | * @param null|mixed $value the field's current value |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public function __construct( $id, $field, $value = null ) { |
| 69 | |
| 70 | // setup the defaults |
| 71 | $this->defaults = [ |
| 72 | 'type' => 'html', |
| 73 | 'name' => $id, |
| 74 | 'fieldset_attributes' => [], |
| 75 | 'attributes' => [], |
| 76 | 'class' => null, |
| 77 | 'label' => null, |
| 78 | 'label_attributes' => null, |
| 79 | 'placeholder' => null, |
| 80 | 'tooltip' => null, |
| 81 | 'size' => 'medium', |
| 82 | 'html' => null, |
| 83 | 'error' => false, |
| 84 | 'value' => $value, |
| 85 | 'options' => null, |
| 86 | 'conditional' => true, |
| 87 | 'display_callback' => null, |
| 88 | 'if_empty' => null, |
| 89 | 'can_be_empty' => false, |
| 90 | 'clear_after' => true, |
| 91 | 'tooltip_first' => false, |
| 92 | 'allow_clear' => false, |
| 93 | ]; |
| 94 | |
| 95 | // a list of valid field types, to prevent screwy behavior |
| 96 | $this->valid_field_types = [ |
| 97 | 'heading', |
| 98 | 'html', |
| 99 | 'text', |
| 100 | 'textarea', |
| 101 | 'wysiwyg', |
| 102 | 'radio', |
| 103 | 'checkbox_bool', |
| 104 | 'checkbox_list', |
| 105 | 'dropdown', |
| 106 | 'dropdown', |
| 107 | 'dropdown_select2', // Deprecated use `dropdown` |
| 108 | 'dropdown_chosen', // Deprecated use `dropdown` |
| 109 | 'license_key', |
| 110 | 'number', |
| 111 | 'wrapped_html', |
| 112 | 'email', |
| 113 | ]; |
| 114 | |
| 115 | $this->valid_field_types = apply_filters( 'tribe_valid_field_types', $this->valid_field_types ); |
| 116 | |
| 117 | // parse args with defaults and extract them |
| 118 | $args = wp_parse_args( $field, $this->defaults ); |
| 119 | |
| 120 | // sanitize the values just to be safe |
| 121 | $id = esc_attr( $id ); |
| 122 | $type = esc_attr( $args['type'] ); |
| 123 | $name = esc_attr( $args['name'] ); |
| 124 | $placeholder = esc_attr( $args['placeholder'] ); |
| 125 | $class = $this->sanitize_class_attribute( $args['class'] ); |
| 126 | $label = wp_kses( |
| 127 | $args['label'], [ |
| 128 | 'a' => [ 'href' => [], 'title' => [] ], |
| 129 | 'br' => [], |
| 130 | 'em' => [], |
| 131 | 'strong' => [], |
| 132 | 'b' => [], |
| 133 | 'i' => [], |
| 134 | 'u' => [], |
| 135 | 'img' => [ |
| 136 | 'title' => [], |
| 137 | 'src' => [], |
| 138 | 'alt' => [], |
| 139 | ], |
| 140 | 'span' => [ 'class' => [] ], |
| 141 | ] |
| 142 | ); |
| 143 | $label_attributes = $args['label_attributes']; |
| 144 | $tooltip = wp_kses( |
| 145 | $args['tooltip'], [ |
| 146 | 'a' => [ 'href' => [], 'title' => [], 'target' => [] ], |
| 147 | 'br' => [], |
| 148 | 'em' => [], |
| 149 | 'strong' => [], |
| 150 | 'b' => [], |
| 151 | 'i' => [], |
| 152 | 'u' => [], |
| 153 | 'img' => [ |
| 154 | 'title' => [], |
| 155 | 'src' => [], |
| 156 | 'alt' => [], |
| 157 | ], |
| 158 | 'code' => [ 'span' => [] ], |
| 159 | 'span' => [], |
| 160 | ] |
| 161 | ); |
| 162 | $fieldset_attributes = []; |
| 163 | if ( is_array( $args['fieldset_attributes'] ) ) { |
| 164 | foreach ( $args['fieldset_attributes'] as $key => $val ) { |
| 165 | $fieldset_attributes[ $key ] = esc_attr( $val ); |
| 166 | } |
| 167 | } |
| 168 | $attributes = []; |
| 169 | if ( is_array( $args['attributes'] ) ) { |
| 170 | foreach ( $args['attributes'] as $key => $val ) { |
| 171 | $attributes[ $key ] = esc_attr( $val ); |
| 172 | } |
| 173 | } |
| 174 | if ( is_array( $args['options'] ) ) { |
| 175 | $options = []; |
| 176 | foreach ( $args['options'] as $key => $val ) { |
| 177 | $options[ $key ] = $val; |
| 178 | } |
| 179 | } else { |
| 180 | $options = $args['options']; |
| 181 | } |
| 182 | $size = esc_attr( $args['size'] ); |
| 183 | $html = $args['html']; |
| 184 | $error = (bool) $args['error']; |
| 185 | $value = is_array( $value ) ? array_map( 'esc_attr', $value ) : esc_attr( $value ); |
| 186 | $conditional = $args['conditional']; |
| 187 | $display_callback = $args['display_callback']; |
| 188 | $if_empty = is_string( $args['if_empty'] ) ? trim( $args['if_empty'] ) : $args['if_empty']; |
| 189 | $can_be_empty = (bool) $args['can_be_empty']; |
| 190 | $clear_after = (bool) $args['clear_after']; |
| 191 | $tooltip_first = (bool) $args['tooltip_first']; |
| 192 | $allow_clear = (bool) $args['allow_clear']; |
| 193 | |
| 194 | // set the ID |
| 195 | $this->id = apply_filters( 'tribe_field_id', $id ); |
| 196 | |
| 197 | // set each instance variable and filter |
| 198 | foreach ( array_keys( $this->defaults ) as $key ) { |
| 199 | $this->{$key} = apply_filters( 'tribe_field_' . $key, $$key, $this->id ); |
| 200 | } |
| 201 | |
| 202 | // epicness |
| 203 | $this->do_field(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Determines how to handle this field's creation |
| 208 | * either calls a callback function or runs this class' course of action |
| 209 | * logs an error if it fails |
| 210 | * |
| 211 | * @return void |
| 212 | */ |
| 213 | public function do_field() { |
| 214 | |
| 215 | if ( $this->conditional ) { |
| 216 | |
| 217 | if ( $this->display_callback && is_callable( $this->display_callback ) ) { |
| 218 | |
| 219 | // if there's a callback, run it |
| 220 | call_user_func( $this->display_callback ); |
| 221 | |
| 222 | } elseif ( in_array( $this->type, $this->valid_field_types ) ) { |
| 223 | |
| 224 | // the specified type exists, run the appropriate method |
| 225 | $field = call_user_func( [ $this, $this->type ] ); |
| 226 | |
| 227 | // filter the output |
| 228 | $field = apply_filters( 'tribe_field_output_' . $this->type, $field, $this->id, $this ); |
| 229 | echo apply_filters( 'tribe_field_output_' . $this->type . '_' . $this->id, $field, $this->id, $this ); |
| 230 | |
| 231 | } else { |
| 232 | |
| 233 | // fail, log the error |
| 234 | Tribe__Main::debug( esc_html__( 'Invalid field type specified', 'tribe-common' ), $this->type, 'notice' ); |
| 235 | |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * returns the field's start |
| 242 | * |
| 243 | * @return string the field start |
| 244 | */ |
| 245 | public function do_field_start() { |
| 246 | $return = '<fieldset id="tribe-field-' . $this->id . '"'; |
| 247 | $return .= ' class="tribe-field tribe-field-' . $this->type; |
| 248 | $return .= ( $this->error ) ? ' tribe-error' : ''; |
| 249 | $return .= ( $this->size ) ? ' tribe-size-' . $this->size : ''; |
| 250 | $return .= ( $this->class ) ? ' ' . $this->class . '"' : '"'; |
| 251 | $return .= ( $this->fieldset_attributes ) ? ' ' . $this->do_fieldset_attributes() : ''; |
| 252 | $return .= '>'; |
| 253 | |
| 254 | return apply_filters( 'tribe_field_start', $return, $this->id, $this->type, $this->error, $this->class, $this ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * returns the field's end |
| 259 | * |
| 260 | * @return string the field end |
| 261 | */ |
| 262 | public function do_field_end() { |
| 263 | $return = '</fieldset>'; |
| 264 | $return .= ( $this->clear_after ) ? '<div class="clear"></div>' : ''; |
| 265 | |
| 266 | return apply_filters( 'tribe_field_end', $return, $this->id, $this ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * returns the field's label |
| 271 | * |
| 272 | * @return string the field label |
| 273 | */ |
| 274 | public function do_field_label() { |
| 275 | $return = ''; |
| 276 | if ( $this->label ) { |
| 277 | if ( isset( $this->label_attributes ) ) { |
| 278 | $this->label_attributes['class'] = isset( $this->label_attributes['class'] ) ? |
| 279 | implode( ' ', array_merge( [ 'tribe-field-label' ], $this->label_attributes['class'] ) ) : |
| 280 | [ 'tribe-field-label' ]; |
| 281 | $this->label_attributes = $this->concat_attributes( $this->label_attributes ); |
| 282 | } |
| 283 | $return = sprintf( '<legend class="tribe-field-label" %s>%s</legend>', $this->label_attributes, $this->label ); |
| 284 | } |
| 285 | |
| 286 | return apply_filters( 'tribe_field_label', $return, $this->label, $this ); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * returns the field's div start |
| 291 | * |
| 292 | * @return string the field div start |
| 293 | */ |
| 294 | public function do_field_div_start() { |
| 295 | $return = '<div class="tribe-field-wrap">'; |
| 296 | |
| 297 | if ( true === $this->tooltip_first ) { |
| 298 | $return .= $this->do_tool_tip(); |
| 299 | // and empty it to avoid it from being printed again |
| 300 | $this->tooltip = ''; |
| 301 | } |
| 302 | |
| 303 | return apply_filters( 'tribe_field_div_start', $return, $this ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * returns the field's div end |
| 308 | * |
| 309 | * @return string the field div end |
| 310 | */ |
| 311 | public function do_field_div_end() { |
| 312 | $return = $this->do_tool_tip(); |
| 313 | $return .= '</div>'; |
| 314 | |
| 315 | return apply_filters( 'tribe_field_div_end', $return, $this ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * returns the field's tooltip/description |
| 320 | * |
| 321 | * @return string the field tooltip |
| 322 | */ |
| 323 | public function do_tool_tip() { |
| 324 | $return = ''; |
| 325 | if ( $this->tooltip ) { |
| 326 | $return = '<p class="tooltip description">' . $this->tooltip . '</p>'; |
| 327 | } |
| 328 | |
| 329 | return apply_filters( 'tribe_field_tooltip', $return, $this->tooltip, $this ); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * returns the screen reader label |
| 334 | * |
| 335 | * @return string the screen reader label |
| 336 | */ |
| 337 | public function do_screen_reader_label() { |
| 338 | $return = ''; |
| 339 | if ( $this->tooltip ) { |
| 340 | $return = '<label class="screen-reader-text">' . $this->tooltip . '</label>'; |
| 341 | } |
| 342 | |
| 343 | return apply_filters( 'tribe_field_screen_reader_label', $return, $this->tooltip, $this ); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * returns the field's value |
| 348 | * |
| 349 | * @return string the field value |
| 350 | */ |
| 351 | public function do_field_value() { |
| 352 | $return = ''; |
| 353 | if ( $this->value ) { |
| 354 | $return = ' value="' . $this->value . '"'; |
| 355 | } |
| 356 | |
| 357 | return apply_filters( 'tribe_field_value', $return, $this->value, $this ); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * returns the field's name |
| 362 | * |
| 363 | * @param bool $multi |
| 364 | * |
| 365 | * @return string the field name |
| 366 | */ |
| 367 | public function do_field_name( $multi = false ) { |
| 368 | $return = ''; |
| 369 | if ( $this->name ) { |
| 370 | if ( $multi ) { |
| 371 | $return = ' name="' . $this->name . '[]"'; |
| 372 | } else { |
| 373 | $return = ' name="' . $this->name . '"'; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return apply_filters( 'tribe_field_name', $return, $this->name, $this ); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * returns the field's placeholder |
| 382 | * |
| 383 | * @return string the field value |
| 384 | */ |
| 385 | public function do_field_placeholder() { |
| 386 | $return = ''; |
| 387 | if ( $this->placeholder ) { |
| 388 | $return = ' placeholder="' . $this->placeholder . '"'; |
| 389 | } |
| 390 | |
| 391 | return apply_filters( 'tribe_field_placeholder', $return, $this->placeholder, $this ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Return a string of attributes for the field |
| 396 | * |
| 397 | * @return string |
| 398 | **/ |
| 399 | public function do_field_attributes() { |
| 400 | $return = ''; |
| 401 | if ( ! empty( $this->attributes ) ) { |
| 402 | foreach ( $this->attributes as $key => $value ) { |
| 403 | $return .= ' ' . $key . '="' . $value . '"'; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | return apply_filters( 'tribe_field_attributes', $return, $this->name, $this ); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Return a string of attributes for the fieldset |
| 412 | * |
| 413 | * @return string |
| 414 | **/ |
| 415 | public function do_fieldset_attributes() { |
| 416 | $return = ''; |
| 417 | if ( ! empty( $this->fieldset_attributes ) ) { |
| 418 | foreach ( $this->fieldset_attributes as $key => $value ) { |
| 419 | $return .= ' ' . $key . '="' . $value . '"'; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return apply_filters( 'tribe_fieldset_attributes', $return, $this->name, $this ); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * generate a heading field |
| 428 | * |
| 429 | * @return string the field |
| 430 | */ |
| 431 | public function heading() { |
| 432 | $field = '<h3>' . $this->label . '</h3>'; |
| 433 | |
| 434 | return $field; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * generate an html field |
| 439 | * |
| 440 | * @return string the field |
| 441 | */ |
| 442 | public function html() { |
| 443 | $field = $this->do_field_label(); |
| 444 | $field .= $this->html; |
| 445 | |
| 446 | return $field; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * generate a simple text field |
| 451 | * |
| 452 | * @return string the field |
| 453 | */ |
| 454 | public function text() { |
| 455 | $field = $this->do_field_start(); |
| 456 | $field .= $this->do_field_label(); |
| 457 | $field .= $this->do_field_div_start(); |
| 458 | $field .= '<input'; |
| 459 | $field .= ' type="text"'; |
| 460 | $field .= $this->do_field_name(); |
| 461 | $field .= $this->do_field_value(); |
| 462 | $field .= $this->do_field_placeholder(); |
| 463 | $field .= $this->do_field_attributes(); |
| 464 | $field .= '/>'; |
| 465 | $field .= $this->do_screen_reader_label(); |
| 466 | $field .= $this->do_field_div_end(); |
| 467 | $field .= $this->do_field_end(); |
| 468 | |
| 469 | return $field; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * generate a textarea field |
| 474 | * |
| 475 | * @return string the field |
| 476 | */ |
| 477 | public function textarea() { |
| 478 | $field = $this->do_field_start(); |
| 479 | $field .= $this->do_field_label(); |
| 480 | $field .= $this->do_field_div_start(); |
| 481 | $field .= '<textarea'; |
| 482 | $field .= $this->do_field_name(); |
| 483 | $field .= $this->do_field_attributes(); |
| 484 | $field .= '>'; |
| 485 | $field .= esc_html( stripslashes( $this->value ) ); |
| 486 | $field .= '</textarea>'; |
| 487 | $field .= $this->do_screen_reader_label(); |
| 488 | $field .= $this->do_field_div_end(); |
| 489 | $field .= $this->do_field_end(); |
| 490 | |
| 491 | return $field; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * generate a wp_editor field |
| 496 | * |
| 497 | * @return string the field |
| 498 | */ |
| 499 | public function wysiwyg() { |
| 500 | $settings = [ |
| 501 | 'teeny' => true, |
| 502 | 'wpautop' => true, |
| 503 | ]; |
| 504 | ob_start(); |
| 505 | wp_editor( html_entity_decode( ( $this->value ) ), $this->name, $settings ); |
| 506 | $editor = ob_get_clean(); |
| 507 | $field = $this->do_field_start(); |
| 508 | $field .= $this->do_field_label(); |
| 509 | $field .= $this->do_field_div_start(); |
| 510 | $field .= $editor; |
| 511 | $field .= $this->do_screen_reader_label(); |
| 512 | $field .= $this->do_field_div_end(); |
| 513 | $field .= $this->do_field_end(); |
| 514 | |
| 515 | return $field; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * generate a radio button field |
| 520 | * |
| 521 | * @return string the field |
| 522 | */ |
| 523 | public function radio() { |
| 524 | $field = $this->do_field_start(); |
| 525 | $field .= $this->do_field_label(); |
| 526 | $field .= $this->do_field_div_start(); |
| 527 | if ( is_array( $this->options ) ) { |
| 528 | foreach ( $this->options as $option_id => $title ) { |
| 529 | $field_id = sprintf( |
| 530 | '%1$s-%2$s', |
| 531 | sanitize_html_class( trim( $this->id ) ), |
| 532 | sanitize_html_class( trim( $option_id ) ) |
| 533 | ); |
| 534 | |
| 535 | $field .= '<label title="' . esc_attr( strip_tags( $title ) ) . '">'; |
| 536 | $field .= '<input type="radio"'; |
| 537 | $field .= ' id="tribe-field-' . esc_attr( $field_id ) . '"'; |
| 538 | $field .= $this->do_field_name(); |
| 539 | $field .= ' value="' . esc_attr( $option_id ) . '" ' . checked( $this->value, $option_id, false ) . '/>'; |
| 540 | $field .= $title; |
| 541 | $field .= '</label>'; |
| 542 | } |
| 543 | } else { |
| 544 | $field .= '<span class="tribe-error">' . esc_html__( 'No radio options specified', 'tribe-common' ) . '</span>'; |
| 545 | } |
| 546 | $field .= $this->do_field_div_end(); |
| 547 | $field .= $this->do_field_end(); |
| 548 | |
| 549 | return $field; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * generate a checkbox_list field |
| 554 | * |
| 555 | * @return string the field |
| 556 | */ |
| 557 | public function checkbox_list() { |
| 558 | $field = $this->do_field_start(); |
| 559 | $field .= $this->do_field_label(); |
| 560 | $field .= $this->do_field_div_start(); |
| 561 | |
| 562 | if ( ! is_array( $this->value ) ) { |
| 563 | if ( ! empty( $this->value ) ) { |
| 564 | $this->value = [ $this->value ]; |
| 565 | } else { |
| 566 | $this->value = []; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if ( is_array( $this->options ) ) { |
| 571 | foreach ( $this->options as $option_id => $title ) { |
| 572 | $field .= '<label title="' . esc_attr( $title ) . '">'; |
| 573 | $field .= '<input type="checkbox"'; |
| 574 | $field .= $this->do_field_name( true ); |
| 575 | $field .= ' value="' . esc_attr( $option_id ) . '" ' . checked( in_array( $option_id, $this->value ), true, false ) . '/>'; |
| 576 | $field .= $title; |
| 577 | $field .= '</label>'; |
| 578 | } |
| 579 | } else { |
| 580 | $field .= '<span class="tribe-error">' . esc_html__( 'No checkbox options specified', 'tribe-common' ) . '</span>'; |
| 581 | } |
| 582 | $field .= $this->do_field_div_end(); |
| 583 | $field .= $this->do_field_end(); |
| 584 | |
| 585 | return $field; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * generate a boolean checkbox field |
| 590 | * |
| 591 | * @return string the field |
| 592 | */ |
| 593 | public function checkbox_bool() { |
| 594 | $field = $this->do_field_start(); |
| 595 | $field .= $this->do_field_label(); |
| 596 | $field .= $this->do_field_div_start(); |
| 597 | $field .= '<input type="checkbox"'; |
| 598 | $field .= $this->do_field_name(); |
| 599 | $field .= ' value="1" ' . checked( $this->value, true, false ); |
| 600 | $field .= $this->do_field_attributes(); |
| 601 | $field .= '/>'; |
| 602 | $field .= $this->do_screen_reader_label(); |
| 603 | $field .= $this->do_field_div_end(); |
| 604 | $field .= $this->do_field_end(); |
| 605 | |
| 606 | return $field; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * generate a dropdown field |
| 611 | * |
| 612 | * @return string the field |
| 613 | */ |
| 614 | public function dropdown() { |
| 615 | $field = $this->do_field_start(); |
| 616 | $field .= $this->do_field_label(); |
| 617 | $field .= $this->do_field_div_start(); |
| 618 | if ( is_array( $this->options ) && ! empty( $this->options ) ) { |
| 619 | $field .= '<select'; |
| 620 | $field .= $this->do_field_name(); |
| 621 | $field .= " id='{$this->id}-select'"; |
| 622 | $field .= " class='tribe-dropdown'"; |
| 623 | if ( empty( $this->allow_clear ) ) { |
| 624 | $field .= " data-prevent-clear='true'"; |
| 625 | } |
| 626 | $field .= $this->do_field_attributes(); |
| 627 | $field .= '>'; |
| 628 | foreach ( $this->options as $option_id => $title ) { |
| 629 | $field .= '<option value="' . esc_attr( $option_id ) . '"'; |
| 630 | if ( is_array( $this->value ) ) { |
| 631 | $field .= isset( $this->value[0] ) ? selected( $this->value[0], $option_id, false ) : ''; |
| 632 | } else { |
| 633 | $field .= selected( $this->value, $option_id, false ); |
| 634 | } |
| 635 | $field .= '>' . esc_html( $title ) . '</option>'; |
| 636 | } |
| 637 | $field .= '</select>'; |
| 638 | $field .= $this->do_screen_reader_label(); |
| 639 | } elseif ( $this->if_empty ) { |
| 640 | $field .= '<span class="empty-field">' . (string) $this->if_empty . '</span>'; |
| 641 | } else { |
| 642 | $field .= '<span class="tribe-error">' . esc_html__( 'No select options specified', 'tribe-common' ) . '</span>'; |
| 643 | } |
| 644 | $field .= $this->do_field_div_end(); |
| 645 | $field .= $this->do_field_end(); |
| 646 | |
| 647 | return $field; |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * generate a chosen dropdown field - the same as the |
| 652 | * regular dropdown but wrapped so it can have the |
| 653 | * right css class applied to it |
| 654 | * |
| 655 | * @deprecated |
| 656 | * |
| 657 | * @return string the field |
| 658 | */ |
| 659 | public function dropdown_chosen() { |
| 660 | $field = $this->dropdown(); |
| 661 | |
| 662 | return $field; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * generate a select2 dropdown field - the same as the |
| 667 | * regular dropdown but wrapped so it can have the |
| 668 | * right css class applied to it |
| 669 | * |
| 670 | * @deprecated |
| 671 | * |
| 672 | * @return string the field |
| 673 | */ |
| 674 | public function dropdown_select2() { |
| 675 | $field = $this->dropdown(); |
| 676 | |
| 677 | return $field; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * generate a license key field |
| 682 | * |
| 683 | * @return string the field |
| 684 | */ |
| 685 | public function license_key() { |
| 686 | $field = $this->do_field_start(); |
| 687 | $field .= $this->do_field_label(); |
| 688 | $field .= $this->do_field_div_start(); |
| 689 | $field .= '<input'; |
| 690 | $field .= ' type="text"'; |
| 691 | $field .= $this->do_field_name(); |
| 692 | $field .= $this->do_field_value(); |
| 693 | $field .= $this->do_field_attributes(); |
| 694 | $field .= '/>'; |
| 695 | $field .= '<p class="license-test-results"><img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" class="ajax-loading-license" alt="Loading" style="display: none"/>'; |
| 696 | $field .= '<span class="key-validity"></span>'; |
| 697 | $field .= $this->do_screen_reader_label(); |
| 698 | $field .= $this->do_field_div_end(); |
| 699 | $field .= $this->do_field_end(); |
| 700 | |
| 701 | return $field; |
| 702 | } |
| 703 | |
| 704 | /* deprecated camelCase methods */ |
| 705 | public function doField() { |
| 706 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field' ); |
| 707 | return $this->do_field(); |
| 708 | } |
| 709 | |
| 710 | public function doFieldStart() { |
| 711 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_start' ); |
| 712 | return $this->do_field_start(); |
| 713 | } |
| 714 | |
| 715 | public function doFieldEnd() { |
| 716 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_end' ); |
| 717 | return $this->do_field_end(); |
| 718 | } |
| 719 | |
| 720 | public function doFieldLabel() { |
| 721 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_label' ); |
| 722 | return $this->do_field_label(); |
| 723 | } |
| 724 | |
| 725 | public function doFieldDivStart() { |
| 726 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_div_start' ); |
| 727 | return $this->do_field_div_start(); |
| 728 | } |
| 729 | |
| 730 | public function doFieldDivEnd() { |
| 731 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_div_end' ); |
| 732 | return $this->do_field_div_end(); |
| 733 | } |
| 734 | |
| 735 | public function doToolTip() { |
| 736 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_tool_tip' ); |
| 737 | return $this->do_tool_tip(); |
| 738 | } |
| 739 | |
| 740 | public function doFieldValue() { |
| 741 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_value' ); |
| 742 | return $this->do_field_value(); |
| 743 | } |
| 744 | |
| 745 | public function doFieldName( $multi = false ) { |
| 746 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_name' ); |
| 747 | return $this->do_field_name( $multi ); |
| 748 | } |
| 749 | |
| 750 | public function doFieldAttributes() { |
| 751 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_field_attributes' ); |
| 752 | return $this->do_field_attributes(); |
| 753 | } |
| 754 | |
| 755 | public function doScreenReaderLabel() { |
| 756 | _deprecated_function( __METHOD__, '4.3', __CLASS__ . '::do_screen_reader_label' ); |
| 757 | return $this->do_screen_reader_label(); |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Generate a wrapped html field. |
| 762 | * |
| 763 | * This is useful to print some HTML that should be inline with the other fieldsets. |
| 764 | * |
| 765 | * @return string The field markup. |
| 766 | */ |
| 767 | public function wrapped_html() { |
| 768 | $field = $this->do_field_start(); |
| 769 | $field .= $this->do_field_label(); |
| 770 | $field .= $this->do_field_div_start(); |
| 771 | $field .= $this->html; |
| 772 | $field .= $this->do_field_div_start(); |
| 773 | $field .= $this->do_field_end(); |
| 774 | |
| 775 | return $field; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Concatenates an array of attributes to use in HTML tags. |
| 780 | * |
| 781 | * Example usage: |
| 782 | * |
| 783 | * $attrs = [ 'class' => ['one', 'two'], 'style' => 'color:red;' ]; |
| 784 | * printf ( '<p %s>%s</p>', tribe_concat_attributes( $attrs ), 'bar' ); |
| 785 | * |
| 786 | * // <p> class="one two" style="color:red;">bar</p> |
| 787 | * |
| 788 | * @param array $attributes An array of attributes in the format |
| 789 | * [<attribute1> => <value>, <attribute2> => <value>] |
| 790 | * where `value` can be a string or an array. |
| 791 | * |
| 792 | * @return string The concatenated attributes. |
| 793 | */ |
| 794 | protected function concat_attributes( array $attributes = [] ) { |
| 795 | if ( empty( $attributes ) ) { |
| 796 | return ''; |
| 797 | } |
| 798 | |
| 799 | $concat = []; |
| 800 | foreach ( $attributes as $attribute => $value ) { |
| 801 | if ( is_array( $value ) ) { |
| 802 | $value = implode( ' ', $value ); |
| 803 | } |
| 804 | $quote = false !== strpos( $value, '"' ) ? "'" : '"'; |
| 805 | $concat[] = esc_attr( $attribute ) . '=' . $quote . esc_attr( $value ) . $quote; |
| 806 | } |
| 807 | |
| 808 | return implode( ' ', $concat ); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Generate an email address field |
| 813 | * |
| 814 | * @since 4.7.4 |
| 815 | * |
| 816 | * @return string The field |
| 817 | */ |
| 818 | public function email() { |
| 819 | $this->value = trim( $this->value ); |
| 820 | return $this->text(); |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Sanitizes a space-separated or array of classes. |
| 825 | * |
| 826 | * @since 4.7.7 |
| 827 | * |
| 828 | * @param string|array $class A single class, a space-separated list of classes |
| 829 | * or an array of classes. |
| 830 | * |
| 831 | * @return string A space-separated list of classes. |
| 832 | */ |
| 833 | protected function sanitize_class_attribute( $class ) { |
| 834 | $classes = is_array( $class ) ? $class : explode( ' ', $class ); |
| 835 | $sanitized = array_map( 'sanitize_html_class', $classes ); |
| 836 | |
| 837 | return implode( ' ', $sanitized ); |
| 838 | } |
| 839 | } // end class |
| 840 | } // endif class_exists |
| 841 |