| 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" multiple>'; |
| 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 |
if ( !is_array( $value ) && strpos( $value, ', ' ) ) |
| 31 |
$value = explode( ', ', $value ); |
| 32 |
|
| 33 |
|
| 34 |
$i = 0; |
| 35 |
foreach( $details['options'] as $option ){ |
| 36 |
$optionOutput = Wordpress_Creation_Kit_PB::wck_generate_select_option($option, $details['value'], $i, $value); |
| 37 |
$options .= apply_filters("wck_select_{$meta}_{$field_name}_option_{$i}", $optionOutput, $i); |
| 38 |
|
| 39 |
$i++; |
| 40 |
|
| 41 |
if( is_array( $value ) ) { |
| 42 |
if( strpos( $option, '%' ) !== false ) { |
| 43 |
$option_parts = explode( '%', $option ); |
| 44 |
|
| 45 |
if( !empty( $option_parts ) && empty( $option_parts[0] ) && count( $option_parts ) == 3 ) { |
| 46 |
$slug = $option_parts[2]; |
| 47 |
} |
| 48 |
|
| 49 |
if( in_array( $slug, $value ) ) |
| 50 |
$new_nonexisting_value = false; |
| 51 |
} |
| 52 |
} |
| 53 |
elseif( !empty( $value ) && !is_array( $value ) && ( $option === $value || strpos( $option, '%'.$value ) !== false ) ) |
| 54 |
$new_nonexisting_value = false; |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
//display the custom value that was inserted with select 2 that was not present in options |
| 59 |
if( $new_nonexisting_value ) |
| 60 |
$options .= '<option value="'. esc_attr( $value ) .'" '. selected( $value, $value, false ) .' >'. esc_html( $value ) .'</option>'; |
| 61 |
} |
| 62 |
|
| 63 |
$element .= apply_filters( "wck_select_{$meta}_{$field_name}_options", $options ); |
| 64 |
$element .= '</select>'; |
| 65 |
$element .= '<script type="text/javascript"> |
| 66 |
if( typeof jQuery.fn.select2 === "undefined" ) { |
| 67 |
jQuery( function() { |
| 68 |
jQuery( ".mb-select-2").select2({ tags: true, placeholder: "Select or type in an option" }); |
| 69 |
}); |
| 70 |
} |
| 71 |
else jQuery( ".mb-select-2").select2({ tags: true, placeholder: "Select or type in an option" }); |
| 72 |
</script>'; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|