Yoast_Form::radio()publicYoast 2.0

Create a Radio input field.

Метод класса: Yoast_Form{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$Yoast_Form = new Yoast_Form();
$Yoast_Form->radio( $variable, $values, $legend, $legend_attr, $attr );
$variable(строка) (обязательный)
The variable within the option to create the radio button for.
$values(массив) (обязательный)
The radio options to choose from.
$legend(строка)
The legend to show for the field set, if any.
По умолчанию: ''
$legend_attr(массив)
The attributes for the legend, if any.
По умолчанию: []
$attr(массив)
Extra attributes to add to the radio button.
По умолчанию: []

Список изменений

С версии 2.0 Введена.

Код Yoast_Form::radio() Yoast 22.3

public function radio( $variable, $values, $legend = '', $legend_attr = [], $attr = [] ) {
	if ( ! is_array( $values ) || $values === [] ) {
		return;
	}
	$val = $this->get_field_value( $variable, false );

	$var_esc = esc_attr( $variable );

	$defaults = [
		'disabled' => false,
	];
	$attr     = wp_parse_args( $attr, $defaults );

	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
	echo '<fieldset class="yoast-form-fieldset wpseo_radio_block" id="' . $var_esc . '">';

	if ( is_string( $legend ) && $legend !== '' ) {

		$legend_defaults = [
			'id'    => '',
			'class' => 'radiogroup',
		];

		$legend_attr = wp_parse_args( $legend_attr, $legend_defaults );

		$this->legend( $legend, $legend_attr );
	}

	foreach ( $values as $key => $value ) {
		$label      = $value;
		$aria_label = '';

		if ( is_array( $value ) ) {
			$label      = ( $value['label'] ?? '' );
			$aria_label = ( $value['aria_label'] ?? '' );
		}

		$key_esc = esc_attr( $key );

		$disabled_attribute = $this->get_disabled_attribute( $variable, $attr );

		// phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded and all other output is properly escaped.
		echo '<input type="radio" class="radio" id="' . $var_esc . '-' . $key_esc . '" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" value="' . $key_esc . '" ' . checked( $val, $key_esc, false ) . $disabled_attribute . ' />';
		$this->label(
			$label,
			[
				'for'        => $var_esc . '-' . $key_esc,
				'class'      => 'radio',
				'aria_label' => $aria_label,
			]
		);
	}
	echo '</fieldset>';
}