A better checked method ref #45, #49 - props @kloneets

This commit is contained in:
Jay Wood
2016-09-30 10:41:18 -04:00
parent 9c5f2e293c
commit 873cbd0001

View File

@ -26,27 +26,42 @@ class CWV2_Settings {
} }
private function checked_array( $haystack, $cur, $show = true ) {
if ( is_array( $haystack ) && in_array( $cur, $haystack ) ) {
$cur = $haystack = 1;
}
if ( is_array( $haystack ) ) { //if $haystack is still an array, take first value
$haystack = array_shift( $haystack );
}
return checked( $haystack, $cur, $show );
}
public function check( $args = array() ) { public function check( $args = array() ) {
$args = $this->get_default_args( $args ); $args = $this->get_default_args( $args );
$field_id = $args['id']; $field_id = $args['id'];
$description = $args['desc']; $description = $args['desc'];
$options = $args['options']; $options = $args['options'];
$default = empty( $args['default'] );
if ( ! $options || empty( $field_id ) ) { if ( ! $options || empty( $field_id ) ) {
return; return;
} }
$option_value = get_option( $field_id, $field_id, $default );
?><fieldset><?php ?><fieldset><?php
$offset = 0; $offset = 0;
foreach ( $options as $id => $label ) { foreach ( $options as $op_value => $label ) {
$cur_id = $id . '-' . $offset; $cur_id = $op_value . '-' . $offset;
$offset++; $offset++;
?> ?>
<label for="<?php echo $cur_id; ?>"> <label for="<?php echo $cur_id; ?>">
<input id="<?php echo $cur_id; ?>" type="checkbox" value="<?php echo $id; ?>" name="<?php echo $field_id; ?>[]" /><?php echo $label; ?> <input id="<?php echo $cur_id; ?>" type="checkbox" value="<?php echo $op_value; ?>" name="<?php echo $field_id; ?>[]" <?php $this->checked_array( $option_value, $op_value ); ?>/><?php echo $label; ?>
</label> </label>
<?php <?php
} }
?></fieldset><?php ?></fieldset><?php
} }