Yoast_Form::toggle_switch()publicYoast 3.1

Create a toggle switch input field using two radio buttons.

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

Хуков нет.

Возвращает

null. Ничего.

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

$Yoast_Form = new Yoast_Form();
$Yoast_Form->toggle_switch( $variable, $values, $label, $help, $attr );
$variable(строка) (обязательный)
The variable within the option to create the radio buttons for.
$values(массив) (обязательный)
Associative array of on/off keys and their values to be used as the label elements text for the radio buttons. Optionally, each value can be an array of visible label text and screen reader text.
$label(строка) (обязательный)
The visual label for the radio buttons group, used as the fieldset legend.
$help(строка)
Inline Help that will be printed out before the visible toggles text.
По умолчанию: ''
$attr(массив)
Extra attributes to add to the toggle switch.
По умолчанию: []

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

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

Код Yoast_Form::toggle_switch() Yoast 19.7.1

public function toggle_switch( $variable, $values, $label, $help = '', $attr = [] ) {
	if ( ! is_array( $values ) || $values === [] ) {
		return;
	}

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

	if ( isset( $attr['preserve_disabled_value'] ) && $attr['preserve_disabled_value'] ) {
		$this->hidden( $variable );
		$variable .= '_disabled';
	}

	$val = $this->get_field_value( $variable, false );
	if ( $val === true ) {
		$val = 'on';
	}
	if ( $val === false ) {
		$val = 'off';
	}

	$help_class = ! empty( $help ) ? ' switch-container__has-help' : '';

	$has_premium_upsell = ( isset( $attr['show_premium_upsell'] ) && $attr['show_premium_upsell'] && isset( $attr['premium_upsell_url'] ) && ! empty( $attr['premium_upsell_url'] ) );
	$upsell_class       = ( $has_premium_upsell ) ? ' premium-upsell' : '';

	$var_esc = esc_attr( $variable );

	printf( '<div class="%s">', esc_attr( 'switch-container' . $help_class . $upsell_class ) );
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
	echo '<fieldset id="', $var_esc, '" class="fieldset-switch-toggle"><legend>', $label, '</legend>', $help;

	// Show disabled note if attribute does not exists or does exist and is set to true.
	if ( ! isset( $attr['show_disabled_note'] ) || ( $attr['show_disabled_note'] === true ) ) {
		if ( isset( $attr['note_when_disabled'] ) ) {
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
			echo $this->get_disabled_note( $variable, $attr['note_when_disabled'] );
		}
		else {
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
			echo $this->get_disabled_note( $variable );
		}
	}

	echo '<div class="switch-toggle switch-candy switch-yoast-seo">';

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

		if ( is_array( $value ) ) {
			$screen_reader_text      = $value['screen_reader_text'];
			$screen_reader_text_html = '<span class="screen-reader-text"> ' . esc_html( $screen_reader_text ) . '</span>';
			$value                   = $value['text'];
		}

		$key_esc            = esc_attr( $key );
		$for                = $var_esc . '-' . $key_esc;
		$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" id="' . $for . '" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" value="' . $key_esc . '" ' . checked( $val, $key_esc, false ) . $disabled_attribute . ' />',
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
		'<label for="', $for, '">', esc_html( $value ), $screen_reader_text_html, '</label>';
	}

	$upsell_button = '';
	if ( $has_premium_upsell ) {
		$upsell_button = '<a class="yoast-button yoast-button--buy yoast-button--small" href=' . esc_url( $attr['premium_upsell_url'] ) . ' target="_blank">' . esc_html__( 'Unlock with Premium!', 'wordpress-seo' ) . '<span class="screen-reader-text">' . esc_html__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>' .
		'<span aria-hidden="true" class="yoast-button--buy__caret"></span></a>';
	}


	echo '<a></a></div></fieldset><div class="clear"></div>' . $upsell_button . '</div>' . PHP_EOL . PHP_EOL;
}