Tribe__Field::__construct( string $id, array $field, null|mixed $value = null )
Class constructor
Contents
Parameters
- $id
-
(string) (Required) The field id.
- $field
-
(array) (Required) The field settings.
- $value
-
(null|mixed) (Optional) The value passed when saving the field.
Default value: null
Return
(void)
Source
File: src/Tribe/Field.php
public function __construct( $id, $field, $value = null ) {
// setup the defaults
$this->defaults = array(
'type' => 'html',
'name' => $id,
'fieldset_attributes' => array(),
'attributes' => array(),
'class' => null,
'label' => null,
'label_attributes' => null,
'placeholder' => null,
'tooltip' => null,
'size' => 'medium',
'html' => null,
'error' => false,
'value' => $value,
'options' => null,
'conditional' => true,
'display_callback' => null,
'if_empty' => null,
'can_be_empty' => false,
'clear_after' => true,
'tooltip_first' => false,
);
// a list of valid field types, to prevent screwy behavior
$this->valid_field_types = array(
'heading',
'html',
'text',
'textarea',
'wysiwyg',
'radio',
'checkbox_bool',
'checkbox_list',
'dropdown',
'dropdown',
'dropdown_select2', // Deprecated use `dropdown`
'dropdown_chosen', // Deprecated use `dropdown`
'license_key',
'wrapped_html',
'email',
);
$this->valid_field_types = apply_filters( 'tribe_valid_field_types', $this->valid_field_types );
// parse args with defaults and extract them
$args = wp_parse_args( $field, $this->defaults );
// sanitize the values just to be safe
$id = esc_attr( $id );
$type = esc_attr( $args['type'] );
$name = esc_attr( $args['name'] );
$placeholder = esc_attr( $args['placeholder'] );
$class = $this->sanitize_class_attribute( $args['class'] );
$label = wp_kses(
$args['label'], array(
'a' => array( 'href' => array(), 'title' => array() ),
'br' => array(),
'em' => array(),
'strong' => array(),
'b' => array(),
'i' => array(),
'u' => array(),
'img' => array(
'title' => array(),
'src' => array(),
'alt' => array(),
),
'span' => array( 'class' => array() ),
)
);
$label_attributes = $args['label_attributes'];
$tooltip = wp_kses(
$args['tooltip'], array(
'a' => array( 'href' => array(), 'title' => array(), 'target' => array() ),
'br' => array(),
'em' => array(),
'strong' => array(),
'b' => array(),
'i' => array(),
'u' => array(),
'img' => array(
'title' => array(),
'src' => array(),
'alt' => array(),
),
'code' => array( 'span' => array() ),
'span' => array(),
)
);
$fieldset_attributes = array();
if ( is_array( $args['fieldset_attributes'] ) ) {
foreach ( $args['fieldset_attributes'] as $key => $val ) {
$fieldset_attributes[ $key ] = esc_attr( $val );
}
}
$attributes = array();
if ( is_array( $args['attributes'] ) ) {
foreach ( $args['attributes'] as $key => $val ) {
$attributes[ $key ] = esc_attr( $val );
}
}
if ( is_array( $args['options'] ) ) {
$options = array();
foreach ( $args['options'] as $key => $val ) {
$options[ $key ] = $val;
}
} else {
$options = $args['options'];
}
$size = esc_attr( $args['size'] );
$html = $args['html'];
$error = (bool) $args['error'];
$value = is_array( $value ) ? array_map( 'esc_attr', $value ) : esc_attr( $value );
$conditional = $args['conditional'];
$display_callback = $args['display_callback'];
$if_empty = is_string( $args['if_empty'] ) ? trim( $args['if_empty'] ) : $args['if_empty'];
$can_be_empty = (bool) $args['can_be_empty'];
$clear_after = (bool) $args['clear_after'];
$tooltip_first = (bool) $args['tooltip_first'];
// set the ID
$this->id = apply_filters( 'tribe_field_id', $id );
// set each instance variable and filter
foreach ( array_keys( $this->defaults ) as $key ) {
$this->{$key} = apply_filters( 'tribe_field_' . $key, $$key, $this->id );
}
// epicness
$this->do_field();
}