| 1 |
<?php |
| 2 |
/* @param string $meta Meta name. |
| 3 |
* @param array $details Contains the details for the field. |
| 4 |
* @param string $value Contains input value; |
| 5 |
* @param string $context Context where the function is used. Depending on it some actions are preformed.; |
| 6 |
* @return string $element input element html string. */ |
| 7 |
|
| 8 |
$id = esc_attr( Wordpress_Creation_Kit_PB::wck_generate_slug( $details['title'], $details ) ); |
| 9 |
if( !empty( $frontend_prefix ) ) |
| 10 |
$id = $frontend_prefix.$id; |
| 11 |
|
| 12 |
$element .= '<select name="'. $single_prefix . esc_attr( Wordpress_Creation_Kit_PB::wck_generate_slug( $details['title'], $details ) ) .'" id="'.$id.'"'; |
| 13 |
$element .= '" class="mb-select-2 mb-field" >'; |
| 14 |
|
| 15 |
if( !empty( $details['default-option'] ) && $details['default-option'] ) |
| 16 |
$element .= '<option value="">'. __('Select or type in an option', 'profile-builder') .'</option>'; |
| 17 |
|
| 18 |
$field_name = Wordpress_Creation_Kit_PB::wck_generate_slug( $details['title'], $details ); |
| 19 |
|
| 20 |
// we're passing this further to a function. Need to make sure it exists so we don't get a notice. |
| 21 |
if( empty( $details['value'] ) ){ |
| 22 |
$details['value'] = false; |
| 23 |
} |
| 24 |
|
| 25 |
$options = ''; |
| 26 |
if( !empty( $details['options'] ) ){ |
| 27 |
|
| 28 |
$new_nonexisting_value = true; |
| 29 |
|
| 30 |
$i = 0; |
| 31 |
foreach( $details['options'] as $option ){ |
| 32 |
$optionOutput = Wordpress_Creation_Kit_PB::wck_generate_select_option($option, $details['value'], $i, $value); |
| 33 |
$options .= apply_filters("wck_select_{$meta}_{$field_name}_option_{$i}", $optionOutput, $i); |
| 34 |
|
| 35 |
$i++; |
| 36 |
|
| 37 |
if( !empty( $value ) && ( $option === $value || strpos( $option, '%'.$value ) !== false ) )//it is not a custom value because it is present in the options |
| 38 |
$new_nonexisting_value = false; |
| 39 |
} |
| 40 |
|
| 41 |
//display the custom value that was inserted with select 2 that was not present in options |
| 42 |
if( $new_nonexisting_value ) |
| 43 |
$options .= '<option value="'. esc_attr( $value ) .'" '. selected( $value, $value, false ) .' >'. esc_html( $value ) .'</option>'; |
| 44 |
} |
| 45 |
|
| 46 |
$element .= apply_filters( "wck_select_{$meta}_{$field_name}_options", $options ); |
| 47 |
$element .= '</select>'; |
| 48 |
$element .= '<script type="text/javascript"> |
| 49 |
if(typeof jQuery.fn.select2 === "undefined"){jQuery( function() {jQuery( ".mb-select-2").select2({ tags: true, placeholder: "Select or type in an option" });});} |
| 50 |
else{jQuery( ".mb-select-2").select2({ tags: true, placeholder: "Select or type in an option" });} |
| 51 |
</script>'; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|