Yoast_Form::select()publicYoast 2.0

Create a Select Box.

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

Хуков нет.

Возвращает

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

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

$Yoast_Form = new Yoast_Form();
$Yoast_Form->select( $variable, $label, $select_options, $styled, $show_label, $attr, $help );
$variable(строка) (обязательный)
The variable within the option to create the select for.
$label(строка) (обязательный)
The label to show for the variable.
$select_options(массив) (обязательный)
The select options to choose from.
$styled(строка)
The select style. Use 'styled' to get a styled select.
По умолчанию: 'unstyled'
$show_label(true|false)
Whether or not to show the label, if not, it will be applied as an aria-label.
По умолчанию: true
$attr(массив)
Extra attributes to add to the select.
По умолчанию: []
$help(строка)
Inline Help HTML that will be printed after the label.
По умолчанию: ''

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

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

Код Yoast_Form::select() Yoast 22.3

public function select( $variable, $label, array $select_options, $styled = 'unstyled', $show_label = true, $attr = [], $help = '' ) {
	if ( empty( $select_options ) ) {
		return;
	}

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

	if ( $show_label ) {
		$this->label(
			$label,
			[
				'for'   => $variable,
				'class' => 'select',
			]
		);
		echo $help; // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: The help contains HTML.
	}

	$select_name       = esc_attr( $this->option_name ) . '[' . esc_attr( $variable ) . ']';
	$active_option     = $this->get_field_value( $variable, '' );
	$wrapper_start_tag = '';
	$wrapper_end_tag   = '';

	$select = new Yoast_Input_Select( $variable, $select_name, $select_options, $active_option );
	$select->add_attribute( 'class', 'select' );

	if ( $this->is_control_disabled( $variable )
		|| ( isset( $attr['disabled'] ) && $attr['disabled'] ) ) {
		$select->add_attribute( 'disabled', 'disabled' );
	}

	if ( ! $show_label ) {
		$select->add_attribute( 'aria-label', $label );
	}

	if ( $styled === 'styled' ) {
		$wrapper_start_tag = '<span class="yoast-styled-select">';
		$wrapper_end_tag   = '</span>';
	}

	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
	echo $wrapper_start_tag;
	$select->output_html();
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped before.
	echo $wrapper_end_tag;
	echo '<br class="clear"/>';
}