| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Text Field Class |
| 5 |
*/ |
| 6 |
class WeForms_Form_Field_Radio extends WeForms_Form_Field_Checkbox { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
$this->name = __( 'Radio', 'weforms' ); |
| 10 |
$this->input_type = 'radio_field'; |
| 11 |
$this->icon = 'dot-circle-o'; |
| 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 |
$use_theme_css = isset( $form_settings['use_theme_css'] ) ? $form_settings['use_theme_css'] : 'wpuf-style'; |
| 24 |
$selected = isset( $field_settings['selected'] ) ? $field_settings['selected'] : ''; ?> |
| 25 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 26 |
<?php $this->print_label( $field_settings, $form_id ); ?> |
| 27 |
|
| 28 |
<?php do_action( 'weforms_radio_field_after_label', $field_settings ); ?> |
| 29 |
<div class="wpuf-fields" data-required="<?php echo esc_attr( $field_settings['required'] ) ?>" data-type="radio" data-style="<?php echo esc_attr( $use_theme_css ); ?>"> |
| 30 |
|
| 31 |
<?php |
| 32 |
if ( $field_settings['options'] && count( $field_settings['options'] ) > 0 ) { |
| 33 |
foreach ( $field_settings['options'] as $value => $option ) { |
| 34 |
?> |
| 35 |
|
| 36 |
<label <?php echo $field_settings['inline'] == 'yes' ? 'class="wpuf-radio-inline"' : 'class="wpuf-radio-block"'; ?>> |
| 37 |
<input |
| 38 |
name="<?php echo esc_attr($field_settings['name']); ?>" |
| 39 |
class="<?php echo 'wpuf_'. esc_attr( $field_settings['name'] ). '_'. esc_attr( $form_id ); ?>" |
| 40 |
type="radio" |
| 41 |
value="<?php echo esc_attr( $value ); ?>"<?php checked( $selected, $value ); ?> |
| 42 |
/> |
| 43 |
<?php echo $option; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 44 |
</label> |
| 45 |
<?php |
| 46 |
} |
| 47 |
} ?> |
| 48 |
|
| 49 |
<?php $this->help_text( $field_settings ); ?> |
| 50 |
|
| 51 |
</div> |
| 52 |
</li> |
| 53 |
<?php |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get field options setting |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function get_options_settings() { |
| 62 |
$default_options = $this->get_default_option_settings( true, [ 'width' ] ); |
| 63 |
$dropdown_options = [ |
| 64 |
$this->get_default_option_dropdown_settings(), |
| 65 |
[ |
| 66 |
'name' => 'inline', |
| 67 |
'title' => __( 'Show in inline list', 'weforms' ), |
| 68 |
'type' => 'radio', |
| 69 |
'options' => [ |
| 70 |
'yes' => __( 'Yes', 'weforms' ), |
| 71 |
'no' => __( 'No', 'weforms' ), |
| 72 |
], |
| 73 |
'default' => 'no', |
| 74 |
'inline' => true, |
| 75 |
'section' => 'advanced', |
| 76 |
'priority' => 23, |
| 77 |
'help_text' => __( 'Show this option in an inline list', 'weforms' ), |
| 78 |
], |
| 79 |
]; |
| 80 |
|
| 81 |
$dropdown_options = apply_filters( 'weforms_radio_field_option_settings', $dropdown_options ); |
| 82 |
|
| 83 |
return array_merge( $default_options, $dropdown_options ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the field props |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function get_field_props() { |
| 92 |
$defaults = $this->default_attributes(); |
| 93 |
$props = [ |
| 94 |
'selected' => '', |
| 95 |
'inline' => 'no', |
| 96 |
'options' => [ 'Option' => __( 'Option', 'weforms' ) ], |
| 97 |
]; |
| 98 |
|
| 99 |
$props = apply_filters( 'weforms_radio_field_props', $props ); |
| 100 |
|
| 101 |
return array_merge( $defaults, $props ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Prepare entry |
| 106 |
* |
| 107 |
* @param $field |
| 108 |
* |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
public function prepare_entry( $field, $args = [] ) { |
| 112 |
if( empty( $_POST[ '_wpnonce' ] ) ) { |
| 113 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 117 |
wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 118 |
} |
| 119 |
|
| 120 |
$args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 121 |
$val = $args[$field['name']]; |
| 122 |
|
| 123 |
return isset( $field['options'][$val] ) ? $field['options'][$val] : $val; |
| 124 |
} |
| 125 |
} |
| 126 |
|