| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Text Field Class |
| 5 |
*/ |
| 6 |
class WeForms_Form_Field_Dropdown extends WeForms_Field_Contract { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
$this->name = __( 'Dropdown', 'weforms' ); |
| 10 |
$this->input_type = 'dropdown_field'; |
| 11 |
$this->icon = 'caret-square-o-down'; |
| 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 |
$name = $field_settings['name']; ?> |
| 25 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 26 |
<?php $this->print_label( $field_settings, $form_id ); ?> |
| 27 |
|
| 28 |
<?php do_action( 'weforms_dropdown_field_after_label', $field_settings ); ?> |
| 29 |
|
| 30 |
<div class="wpuf-fields"> |
| 31 |
<select |
| 32 |
class="<?php echo 'wpuf_'. esc_attr( $field_settings['name'] ) .'_'. esc_attr( $form_id ); ?>" |
| 33 |
id="<?php echo esc_attr( $field_settings['name'] ) . '_' . esc_attr( $form_id ); ?>" |
| 34 |
name="<?php echo esc_attr( $name ); ?>" |
| 35 |
data-required="<?php echo esc_attr( $field_settings['required'] ) ?>" |
| 36 |
data-type="select"> |
| 37 |
|
| 38 |
<?php if ( !empty( $field_settings['first'] ) ) { ?> |
| 39 |
<option value=""><?php echo esc_attr( $field_settings['first'] ); ?></option> |
| 40 |
<?php } ?> |
| 41 |
|
| 42 |
<?php |
| 43 |
if ( $field_settings['options'] && count( $field_settings['options'] ) > 0 ) { |
| 44 |
foreach ($field_settings['options'] as $value => $option) { |
| 45 |
$current_select = selected( $selected, $value, false ); |
| 46 |
?> |
| 47 |
<option value="<?php echo esc_attr( $value ); ?>"<?php echo esc_attr( $current_select ); ?>><?php echo |
| 48 |
esc_attr( $option ); ?></option> |
| 49 |
<?php |
| 50 |
} |
| 51 |
} ?> |
| 52 |
</select> |
| 53 |
<?php $this->help_text( $field_settings ); ?> |
| 54 |
</div> |
| 55 |
|
| 56 |
</li> |
| 57 |
<?php |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get field options setting |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function get_options_settings() { |
| 66 |
$default_options = $this->get_default_option_settings(); |
| 67 |
$dropdown_options = [ |
| 68 |
$this->get_default_option_dropdown_settings(), |
| 69 |
[ |
| 70 |
'name' => 'first', |
| 71 |
'title' => __( 'Select Text', 'weforms' ), |
| 72 |
'type' => 'text', |
| 73 |
'section' => 'basic', |
| 74 |
'priority' => 13, |
| 75 |
'help_text' => __( "First element of the select dropdown. Leave this empty if you don't want to show this field", 'weforms' ), |
| 76 |
], |
| 77 |
]; |
| 78 |
|
| 79 |
$dropdown_options = apply_filters( 'weforms_dropdown_field_option_settings', $dropdown_options ); |
| 80 |
|
| 81 |
return array_merge( $default_options, $dropdown_options ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the field props |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function get_field_props() { |
| 90 |
$defaults = $this->default_attributes(); |
| 91 |
$props = [ |
| 92 |
'selected' => '', |
| 93 |
'options' => [ 'Option' => __( 'Option', 'weforms' ) ], |
| 94 |
'first' => __( '— Select —', 'weforms' ), |
| 95 |
]; |
| 96 |
|
| 97 |
$props = apply_filters( 'weforms_dropdown_field_props', $props ); |
| 98 |
|
| 99 |
return array_merge( $defaults, $props ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Prepare entry |
| 104 |
* |
| 105 |
* @param $field |
| 106 |
* |
| 107 |
* @return mixed |
| 108 |
*/ |
| 109 |
public function prepare_entry( $field, $args = [] ) { |
| 110 |
if( empty( $_POST['_wpnonce'])) { |
| 111 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 112 |
} |
| 113 |
|
| 114 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 115 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 116 |
} |
| 117 |
|
| 118 |
$args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 119 |
$val = $args[$field['name']]; |
| 120 |
|
| 121 |
return isset( $field['options'][$val] ) ? $field['options'][$val] : $val; |
| 122 |
} |
| 123 |
} |
| 124 |
|