Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions classes/elements/WP_Form_Element_Checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
class WP_Form_Element_Checkboxes extends WP_Form_Element_Multiple {
protected $type = 'checkboxes';
protected $default_view = 'WP_Form_View_Checkboxes';

public function get_selected() {
if ( !empty($this->value) ) {
return $this->value;
}
return $this->default_value;
}
}
12 changes: 10 additions & 2 deletions classes/views/WP_Form_View_Checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ protected function checkboxes( WP_Form_Element_Checkboxes $element ) {
throw new LogicException(__('Cannot render checkbox group without a name', 'wp-forms'));
}
$options = $element->get_options();
$selected = (array) $element->get_selected();
$output = '';
foreach ( $options as $key => $label ) {
$output .= $this->checkbox( $key, $label, $attributes );
$checked = false;
if ( in_array( $key, $selected ) ) {
$checked = true;
}
$output .= $this->checkbox( $key, $label, $attributes, $checked );
}
return $output;
}

protected function checkbox( $key, $label, $attributes ) {
protected function checkbox( $key, $label, $attributes, $checked ) {
$checkbox = WP_Form_Element::create('checkbox')
->set_name($attributes['name'].'[]')
->set_label($label)
Expand All @@ -32,6 +37,9 @@ protected function checkbox( $key, $label, $attributes ) {
foreach ( $attributes as $att => $value ) {
$checkbox->set_attribute($att, $value);
}
if ( $checked ) {
$checkbox->set_attribute('checked', 'checked');
}
return $checkbox->render();
}
}