$this->sanitize_options( isset( $raw['options'] ) ? $raw['options'] : array() ),
'layout' => ( isset( $raw['layout'] ) && in_array( $raw['layout'], array( 'stacked', 'inline', 'cards' ), true ) ) ? $raw['layout'] : 'stacked',
);
}
/**
* Whether a choice is checked.
*
* @param mixed $current Current value.
* @param string $value Choice value.
* @return bool
*/
protected function is_checked( $current, $value ) {
return (string) $current === (string) $value;
}
/** {@inheritdoc} */
public function render_control( array $field, array $ctx ) {
$html = sprintf( '
', esc_attr( $field['layout'] ) );
foreach ( $field['options'] as $i => $option ) {
$id = $ctx['id'] . '-' . $i;
$attrs = array(
'type' => $this->choice_type(),
'id' => $id,
'name' => 'checkbox' === $this->choice_type() ? $ctx['name'] . '[]' : $ctx['name'],
'value' => $option['value'],
'class' => 'pf-choice__input',
'checked' => $this->is_checked( $ctx['value'], $option['value'] ),
'required' => 'radio' === $this->choice_type() && ! empty( $field['required'] ),
'aria-describedby' => 0 === $i ? $ctx['describedby'] : '',
);
$html .= sprintf(
'',
esc_attr( $id ),
$this->attrs( $attrs ),
esc_html( $option['label'] )
);
}
return $html . '
';
}
}