Skip to content

Instantly share code, notes, and snippets.

@19h47
Last active December 6, 2022 09:04
Show Gist options
  • Save 19h47/5bc7dc96204d7290ce781a54756e0a7b to your computer and use it in GitHub Desktop.
Save 19h47/5bc7dc96204d7290ce781a54756e0a7b to your computer and use it in GitHub Desktop.
WP Form Controls
<?php
/**
* Plugin Name: WP Form Controls
* Plugin URI: https://gist.github.com/19h47/5bc7dc96204d7290ce781a54756e0a7b
* Description: WP Form Controls is a set of function that helps and allows to generate form controls in WordPress.
* Version: 3.1.6
* Author: Jérémy Levron
* Author URI: https://19h47.fr
*
* @package 19h47/wp-form-controls
*/
if ( ! function_exists( 'wp_form_controls_input' ) ) {
/**
* Input
*
* @param array $args Arguments.
* @param bool $echo (Optional) Whether to echo or return the textarea. Default true for echo.
*
* @return void|string
*/
function wp_form_controls_input( array $args = array(), bool $echo = true ) {
$defaults = array(
'type' => 'text',
'id' => '',
'name' => '',
'class' => 'regular-text code',
'placeholder' => '',
'value' => '',
'style' => '',
'attributes' => array(),
'description' => '',
'legend' => '',
);
$args = wp_parse_args( $args, $defaults );
$control = '<input ';
$control .= _wp_form_controls_attributes( $args );
$control .= _wp_form_controls_custom_attributes( $args['attributes'] );
$control .= '/>';
$control .= _wp_form_controls_description( $args['description'] );
if ( $echo ) {
echo $control; // phpcs:ignore
} else {
return $control;
}
}
}
if ( ! function_exists( 'wp_form_controls_textarea' ) ) {
/**
* Textarea
*
* @param array $args Arguments.
* @param bool $echo (Optional) Whether to echo or return the textarea. Default true for echo.
*
* @see https://developer.wordpress.org/reference/functions/esc_textarea/
*
* @return void|string
*/
function wp_form_controls_textarea( array $args = array(), bool $echo = true ) {
$defaults = array(
'id' => '',
'name' => '',
'class' => 'regular-text code',
'placeholder' => '',
'rows' => 10,
'col' => 50,
'style' => '',
'disabled' => false,
'attributes' => array(),
'value' => '',
'legend' => '',
'description' => '',
'legend' => '',
);
$args = wp_parse_args( $args, $defaults );
$control = '<textarea ';
$control .= _wp_form_controls_attributes( $args, array( 'rows', 'col' ) );
$control .= disabled( $args['disabled'], true, false );
$control .= _wp_form_controls_custom_attributes( $args['attributes'] );
$control .= '>';
if ( $args['value'] ) {
$control .= esc_textarea( $args['value'] );
}
$control .= '</textarea>';
$control .= _wp_form_controls_description( $args['description'] );
$control = _wp_form_controls_fieldset( $args['legend'], $control );
if ( $echo ) {
echo $control; // phpcs:ignore
} else {
return $control;
}
}
}
if ( ! function_exists( 'wp_form_controls_checkbox' ) ) {
/**
* Checkbox
*
* @param array $args Arguments.
* @param bool $echo (Optional) Whether to echo or return the textarea. Default true for echo.
*
* @see https://developer.wordpress.org/reference/functions/esc_textarea/
*
* @return void|string
*/
function wp_form_controls_checkbox( array $args = array(), bool $echo = true ) {
$defaults = array(
'type' => 'checkbox',
'id' => '',
'name' => '',
'class' => 'regular-text code',
'value' => '',
'style' => '',
'disabled' => false,
'attributes' => array(),
'label' => '',
'description' => '',
'legend' => '',
);
$args = wp_parse_args( $args, $defaults );
$control = '<label for="' . $args['name'] . '">';
$control .= '<input ';
$control .= _wp_form_controls_attributes( $args );
$control .= disabled( $args['disabled'], true, false );
$control .= _wp_form_controls_custom_attributes( $args['attributes'] );
$control .= ' />';
$control .= $args['label'] . '</label>';
$control .= _wp_form_controls_description( $args['description'] );
$control = _wp_form_controls_fieldset( $args['legend'], $control );
if ( $echo ) {
echo $control; // phpcs:ignore
} else {
return $control;
}
}
}
if ( ! function_exists( '_wp_form_controls_attributes' ) ) {
/**
* Attributes
*
* @param array $args Arguments.
* @param array $attributes Attributes.
*
* @return string
*/
function _wp_form_controls_attributes( array $args = array(), array $attributes = array() ) : string {
$defaults = array(
'type',
'id',
'name',
'class',
'placeholder',
'value',
'style',
);
$attributes = wp_parse_args( $attributes, $defaults );
$attr = array();
foreach ( $attributes as $attribute ) {
$attr[] = isset( $args[ $attribute ] ) && '' !== $args[ $attribute ] ? esc_attr( $attribute ) . '="' . esc_attr( $args[ $attribute ] ) . '" ' : '';
}
return implode( ' ', $attr );
}
}
if ( ! function_exists( '_wp_form_controls_custom_attributes' ) ) {
/**
* Custom attributes
*
* @param array $attributes Attributes.
*
* @return string
*/
function _wp_form_controls_custom_attributes( array $attributes ) : string {
$custom_attributes = array();
if ( ! empty( $attributes ) && is_array( $attributes ) ) {
foreach ( $attributes as $key => $value ) {
$custom_attributes[] = esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
}
}
return implode( ' ', $custom_attributes );
}
}
if ( ! function_exists( '_wp_form_controls_description' ) ) {
/**
* Description
*
* @param string $description Description.
*
* @return string
*/
function _wp_form_controls_description( string $description ) : string {
return $description ? '<p class="description">' . wp_kses_post( esc_html( $description ) ) . '</p>' . "\n" : '';
}
}
if ( ! function_exists( '_wp_form_controls_fieldset' ) ) {
/**
* Fieldset
*
* @param string $title Title.
* @param string $control Control.
*
* @return string
*/
function _wp_form_controls_fieldset( string $title, string $control ) : string {
$legend = $title ? "<legend class=\"screen-reader-text\"><span>$title</span></legend>" : '';
return '<fieldset>' . $legend . $control . '</fieldset>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment