$value ) {
$name = (string) preg_replace( '/[^a-zA-Z0-9_:.-]/', '', (string) $name );
if ( '' === $name || false === $value || null === $value || is_array( $value ) ) {
continue;
}
if ( true === $value || '' === $value ) {
$out .= ' ' . $name;
continue;
}
$out .= ' ' . $name . '="' . ( in_array( $name, array( 'href', 'src', 'formaction' ), true ) ? esc_url( (string) $value ) : esc_attr( (string) $value ) ) . '"';
}
return $out;
}
public static function row( $label, $control, $help = '', array $opts = array() ) {
$field = array(
'id' => isset( $opts['id'] ) ? (string) $opts['id'] : '',
'label' => (string) $label,
'control' => (string) $control,
'help' => (string) $help,
'required' => ! empty( $opts['required'] ),
'group' => ! empty( $opts['group'] ),
'class' => isset( $opts['class'] ) ? (string) $opts['class'] : '',
);
if ( self::lib() ) {
return \Rooster\Admin\Ui::field_row( $field );
}
$id = $field['id'];
$group = $field['group'] || '' === $id;
return '
'
. '
' . ( $group ? '' : '' : '' ) . '
'
. '
' . $field['control'] . '
'
. ( '' !== $field['help'] ? '
' . esc_html( $field['help'] ) . '
' : '' )
. '
';
}
private static function control_attrs( $name, array $opts ) {
$attrs = array(
'id' => isset( $opts['id'] ) ? (string) $opts['id'] : $name,
'name' => $name,
);
foreach ( array( 'placeholder', 'autocomplete', 'min', 'max', 'step', 'maxlength', 'pattern', 'inputmode', 'rows', 'aria-label' ) as $key ) {
if ( isset( $opts[ $key ] ) && '' !== (string) $opts[ $key ] ) {
$attrs[ $key ] = (string) $opts[ $key ];
}
}
foreach ( array( 'required', 'disabled', 'readonly' ) as $flag ) {
if ( ! empty( $opts[ $flag ] ) ) {
$attrs[ $flag ] = true;
}
}
if ( ! empty( $opts['help'] ) ) {
$attrs['aria-describedby'] = $attrs['id'] . '-help';
}
if ( isset( $opts['attrs'] ) && is_array( $opts['attrs'] ) ) {
$attrs += $opts['attrs'];
}
return $attrs;
}
public static function input( $name, $value = '', array $opts = array() ) {
if ( self::lib() ) {
return \Rooster\Admin\Ui::input( $name, (string) $value, $opts );
}
$type = in_array( isset( $opts['type'] ) ? $opts['type'] : '', array( 'text', 'email', 'url', 'number', 'search', 'password', 'date', 'time', 'tel' ), true ) ? (string) $opts['type'] : 'text';
$attrs = array(
'type' => $type,
'class' => 'rooster-admin-input' . ( ! empty( $opts['short'] ) ? ' rooster-admin-input--short' : ( ! empty( $opts['medium'] ) ? ' rooster-admin-input--medium' : '' ) ) . ( ! empty( $opts['code'] ) ? ' is-code' : '' ),
'value' => (string) $value,
) + self::control_attrs( $name, $opts );
return '';
}
public static function textarea( $name, $value = '', array $opts = array() ) {
if ( self::lib() ) {
return \Rooster\Admin\Ui::textarea( $name, (string) $value, $opts );
}
$attrs = array(
'class' => 'rooster-admin-textarea' . ( ! empty( $opts['code'] ) ? ' is-code' : '' ),
'rows' => isset( $opts['rows'] ) ? (int) $opts['rows'] : 4,
) + self::control_attrs( $name, $opts );
return '';
}
public static function select( $name, array $options, $value = '', array $opts = array() ) {
if ( self::lib() ) {
return \Rooster\Admin\Ui::select( $name, $options, (string) $value, $opts );
}
$html = '';
}
public static function checks( $name, array $options, array $values = array(), array $opts = array() ) {
if ( self::lib() ) {
return \Rooster\Admin\Ui::checks( $name, $options, $values, $opts );
}
$radio = isset( $opts['type'] ) && 'radio' === $opts['type'];
$html = '';
foreach ( $options as $value => $label ) {
$desc = isset( $opts['descriptions'][ $value ] ) ? (string) $opts['descriptions'][ $value ] : '';
$html .= '';
}
return $html . '
';
}
public static function check( $name, $label, $on = false, array $opts = array() ) {
$attrs = array(
'type' => 'checkbox',
'name' => $name,
'value' => isset( $opts['value'] ) ? (string) $opts['value'] : '1',
'id' => isset( $opts['id'] ) ? (string) $opts['id'] : $name,
) + ( isset( $opts['attrs'] ) && is_array( $opts['attrs'] ) ? $opts['attrs'] : array() );
if ( $on ) {
$attrs['checked'] = true;
}
return '';
}
public static function button( $label, array $opts = array() ) {
if ( self::lib() ) {
return \Rooster\Admin\Ui::button( (string) $label, $opts );
}
$variant = isset( $opts['variant'] ) ? (string) $opts['variant'] : 'outline';
$class = 'rooster-admin-button' . ( in_array( $variant, array( 'primary', 'secondary', 'ghost', 'destructive', 'mono' ), true ) ? ' rooster-admin-button--' . $variant : '' )
. ( isset( $opts['size'] ) && in_array( $opts['size'], array( 'sm', 'lg' ), true ) ? ' rooster-admin-button--' . $opts['size'] : '' )
. ( ! empty( $opts['class'] ) ? ' ' . (string) $opts['class'] : '' );
$attrs = array( 'class' => $class ) + ( isset( $opts['attrs'] ) && is_array( $opts['attrs'] ) ? $opts['attrs'] : array() );
if ( ! empty( $opts['href'] ) ) {
$attrs += array( 'href' => (string) $opts['href'] );
return '' . esc_html( (string) $label ) . '';
}
$attrs += array( 'type' => isset( $opts['type'] ) && 'submit' === $opts['type'] ? 'submit' : 'button' );
if ( ! empty( $opts['disabled'] ) ) {
$attrs['disabled'] = true;
}
return '';
}
public static function fold( $title, $body, array $opts = array() ) {
if ( self::lib() && method_exists( '\Rooster\Admin\Ui', 'fold' ) ) {
return \Rooster\Admin\Ui::fold( (string) $title, (string) $body, $opts );
}
$attrs = array(
'class' => trim( 'rooster-admin-fold ' . ( isset( $opts['class'] ) ? (string) $opts['class'] : '' ) ),
'id' => ! empty( $opts['id'] ) ? (string) $opts['id'] : null,
'open' => ! empty( $opts['open'] ),
) + ( isset( $opts['attrs'] ) && is_array( $opts['attrs'] ) ? $opts['attrs'] : array() );
return '' . esc_html( (string) $title ) . ''
. ( isset( $opts['meta'] ) ? (string) $opts['meta'] : '' ) . '
' . (string) $body . ' ';
}
public static function note( $text ) {
return '' . esc_html( (string) $text ) . '
';
}
}