| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Text Field Class |
| 5 |
*/ |
| 6 |
class WeForms_Form_Field_MultiDropdown extends WeForms_Form_Field_Dropdown { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
$this->name = __( 'Multi Select', 'weforms' ); |
| 10 |
$this->input_type = 'multiple_select'; |
| 11 |
$this->icon = 'list-ul'; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Render the text field |
| 16 |
* |
| 17 |
* @param array $field_settings |
| 18 |
* @param int $form_id |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function render( $field_settings, $form_id ) { |
| 23 |
$selected = isset( $field_settings['selected'] ) ? $field_settings['selected'] : ''; |
| 24 |
$selected = is_array( $selected ) ? $selected : []; |
| 25 |
$name = $field_settings['name'] . '[]'; ?> |
| 26 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 27 |
<?php $this->print_label( $field_settings, $form_id ); ?> |
| 28 |
|
| 29 |
<?php do_action( 'weforms_multidropdown_field_after_label', $field_settings ); ?> |
| 30 |
|
| 31 |
<div class="wpuf-fields"> |
| 32 |
<select multiple="multiple" class="multiselect <?php echo 'wpuf_'. esc_attr( $field_settings['name'] ) .'_'. esc_attr( $form_id ); ?>" id="<?php echo esc_attr($field_settings['name']) . '_' . esc_attr($form_id); ?>" name="<?php echo esc_attr($name); ?>" mulitple="multiple" data-required="<?php echo esc_attr($field_settings['required']) ?>" data-type="multiselect"> |
| 33 |
|
| 34 |
<?php if ( !empty( $field_settings['first'] ) ) { ?> |
| 35 |
<option value=""><?php echo esc_attr($field_settings['first']); ?></option> |
| 36 |
<?php } ?> |
| 37 |
|
| 38 |
<?php |
| 39 |
if ( $field_settings['options'] && count( $field_settings['options'] ) > 0 ) { |
| 40 |
foreach ($field_settings['options'] as $value => $option) { |
| 41 |
$current_select = selected( in_array( $value, $selected ), true, false ); |
| 42 |
?> |
| 43 |
<option value="<?php echo esc_attr( $value ); ?>"<?php echo esc_attr( $current_select ); ?>><?php echo esc_html( $option ); ?></option> |
| 44 |
<?php |
| 45 |
} |
| 46 |
} ?> |
| 47 |
</select> |
| 48 |
<?php $this->help_text( $field_settings ); ?> |
| 49 |
</div> |
| 50 |
|
| 51 |
</li> |
| 52 |
<?php |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the field props |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function get_field_props() { |
| 61 |
$defaults = $this->default_attributes(); |
| 62 |
$props = [ |
| 63 |
'selected' => [], |
| 64 |
'options' => [ 'Option' => __( 'Option', 'weforms' ) ], |
| 65 |
'first' => __( '— Select —', 'weforms' ), |
| 66 |
]; |
| 67 |
|
| 68 |
$props = apply_filters( 'weforms_multidropdown_field_props', $props ); |
| 69 |
|
| 70 |
return array_merge( $defaults, $props ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Prepare entry |
| 75 |
* |
| 76 |
* @param $field |
| 77 |
* |
| 78 |
* @return mixed |
| 79 |
*/ |
| 80 |
public function prepare_entry( $field, $args = [] ) { |
| 81 |
if( empty( $_POST[ '_wpnonce' ] ) ) { |
| 82 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 86 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
$args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 90 |
$entry_value = ( is_array( $args[$field['name']] ) && $args[$field['name']] ) ? $args[$field['name']] : array(); |
| 91 |
|
| 92 |
if ( $entry_value ) { |
| 93 |
$new_val = []; |
| 94 |
|
| 95 |
foreach ( $entry_value as $option_key ) { |
| 96 |
$new_val[] = isset( $field['options'][$option_key] ) ? $field['options'][$option_key] : $option_key; |
| 97 |
} |
| 98 |
|
| 99 |
$entry_value = implode( WeForms::$field_separator, $new_val ); |
| 100 |
} else { |
| 101 |
$entry_value = ''; |
| 102 |
} |
| 103 |
|
| 104 |
return $entry_value; |
| 105 |
} |
| 106 |
} |
| 107 |
|