Skip to content

Instantly share code, notes, and snippets.

@olenkadark
Created May 16, 2019 08:17
Show Gist options
  • Save olenkadark/898683e2b37c974871928f77b2b48da9 to your computer and use it in GitHub Desktop.
Save olenkadark/898683e2b37c974871928f77b2b48da9 to your computer and use it in GitHub Desktop.
<?php
/**
* Form Functions
*
* @author Elena ZH
* @category Core
* @package Functions
* @version 1.1.5
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Output form fileds.
*
* @param array $field
*/
function u_wp_form_field( $field ) {
switch ($field['type']){
case 'hidden' :
u_wp_hidden_input( $field );
break;
case 'textarea' :
u_wp_textarea_input( $field );
break;
case 'checkbox' :
if( isset($field['options'] )){
u_wp_checkbox( $field );
}else{
u_wp_checkbox_list( $field );
}
break;
case 'select' :
u_wp_select( $field );
break;
case 'radio' :
u_wp_radio( $field );
break;
default:
u_wp_text_input( $field );
break;
}
}
/**
* Output a text input box.
*
* @param array $field
*/
function u_wp_text_input( $field ) {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'short';
$field['style'] = isset( $field['style'] ) ? $field['style'] : '';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['type'] = isset( $field['type'] ) ? $field['type'] : 'text';
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
foreach ( $field['custom_attributes'] as $attribute => $value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}
echo '<div class="form-group ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">';
if( !empty($field['label'])){
echo '<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
echo u_help_tip( $field['description'] );
}
}
$required = !empty($field['required']) ? 'required' : '';
echo '<input type="' . esc_attr( $field['type'] ) . '" class="' . esc_attr( $field['class'] ) . ' form-control" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" ' . implode( ' ', $custom_attributes ) . ' ' . $required . ' /> ';
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
echo '<small class="description">' . wp_kses_post( $field['description'] ) . '</small>';
}
echo '</div>';
}
/**
* Output a hidden input box.
*
* @param array $field
*/
function u_wp_hidden_input( $field ) {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
$field['class'] = isset( $field['class'] ) ? $field['class'] : '';
echo '<input type="hidden" class="' . esc_attr( $field['class'] ) . '" name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" /> ';
}
/**
* Output a textarea input box.
*
* @param array $field
*/
function u_wp_textarea_input( $field ) {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'short';
$field['style'] = isset( $field['style'] ) ? $field['style'] : '';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['rows'] = isset( $field['rows'] ) ? $field['rows'] : 2;
$field['cols'] = isset( $field['cols'] ) ? $field['cols'] : 20;
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
foreach ( $field['custom_attributes'] as $attribute => $value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}
echo '<div class="form-group ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">';
if( !empty($field['label'])){
echo '<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
echo u_help_tip( $field['description'] );
}
}
$required = !empty($field['required']) ? 'required' : '';
echo '<textarea class="form-control ' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" rows="' . esc_attr( $field['rows'] ) . '" cols="' . esc_attr( $field['cols'] ) . '" ' . implode( ' ', $custom_attributes ) . ' ' . $required . '>' . esc_textarea( $field['value'] ) . '</textarea> ';
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
echo '<small class="description">' . wp_kses_post( $field['description'] ) . '</small>';
}
echo '</div>';
}
/**
* Output a checkbox input box.
*
* @param array $field
*/
function u_wp_checkbox( $field ) {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'checkbox';
$field['style'] = isset( $field['style'] ) ? $field['style'] : '';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
$field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'yes';
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
foreach ( $field['custom_attributes'] as $attribute => $value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}
$required = !empty($field['required']) ? 'required' : '';
echo '<div class="form-check ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">';
echo '<input type="checkbox" class="form-check-input ' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['cbvalue'] ) . '" ' . checked( $field['value'], $field['cbvalue'], false ) . ' ' . implode( ' ', $custom_attributes ) . ' ' . $required . '/> ';
if( !empty($field['label'])){
echo '<label class="form-check-label" for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
echo u_help_tip( $field['description'] );
}
}
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
echo '<small class="description">' . wp_kses_post( $field['description'] ) . '</small>';
}
echo '</div>';
}
/**
* Output a select input box.
*
* @param array $field Data about the field to render.
*/
function u_wp_select( $field ) {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field = wp_parse_args(
$field, array(
'class' => 'select short',
'style' => '',
'wrapper_class' => '',
'value' => get_post_meta( $thepostid, $field['id'], true ),
'name' => $field['id'],
'desc_tip' => false,
'custom_attributes' => array(),
)
);
$wrapper_attributes = array(
'class' => $field['wrapper_class'] . " form-group {$field['id']}_field",
);
$label_attributes = array(
'for' => $field['id'],
);
$field_attributes = (array) $field['custom_attributes'];
$field_attributes['style'] = $field['style'];
$field_attributes['id'] = $field['id'];
$field_attributes['name'] = $field['name'];
$field_attributes['class'] = $field['class'] . " form-control";
$tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : '';
$description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : '';
$required = !empty($field['required']) ? 'required' : '';
?>
<div <?php echo u_implode_html_attributes( $wrapper_attributes ); ?>>
<?php if( !empty($field['label'])){ ?>
<label <?php echo u_implode_html_attributes( $label_attributes ); ?>><?php echo wp_kses_post( $field['label'] ); ?></label>
<?php if ( $tooltip ) : ?>
<?php echo u_help_tip( $tooltip ); ?>
<?php endif; ?>
<?php } ?>
<select <?php echo u_implode_html_attributes( $field_attributes ); ?> <?php echo $required; ?>>
<?php
foreach ( $field['options'] as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '"' . u_selected( $key, $field['value'] ) . '>' . esc_html( $value ) . '</option>';
}
?>
</select>
<?php if ( $description ) : ?>
<small class="description"><?php echo wp_kses_post( $description ); ?></small>
<?php endif; ?>
</div>
<?php
}
/**
* Output a checkboxes input box.
*
* @param array $field
*/
function u_wp_checkbox_list( $field ) {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
$field['style'] = isset( $field['style'] ) ? $field['style'] : '';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
echo '<div class="form-group ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">';
if( !empty($field['label'])){
echo '<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
echo u_help_tip( $field['description'] );
}
}
$required = !empty($field['required']) ? 'required' : '';
foreach ( $field['options'] as $key => $value ) {
echo '<div class="form-check"><label class="form-check-label">';
echo '<input name="' . esc_attr( $field['name'] ) . '"
value="' . esc_attr( $key ) . '"
type="checkbox"
class="form-check-input ' . esc_attr( $field['class'] ) . '"
style="' . esc_attr( $field['style'] ) . '"
' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '
' . $required . '
/> '. esc_html( $value ) . '</label>';
echo '</div>';
$required = '';
}
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
echo '<small class="description">' . wp_kses_post( $field['description'] ) . '</small>';
}
echo '</div>';
}
/**
* Output a radio input box.
*
* @param array $field
*/
function u_wp_radio( $field ) {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
$field['style'] = isset( $field['style'] ) ? $field['style'] : '';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
echo '<div class="form-group ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">';
if( !empty($field['label'])){
echo '<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
echo u_help_tip( $field['description'] );
}
}
$required = !empty($field['required']) ? 'required' : '';
foreach ( $field['options'] as $key => $value ) {
echo '<div class="form-check"><label class="form-check-label">';
echo '<input name="' . esc_attr( $field['name'] ) . '"
value="' . esc_attr( $key ) . '"
type="radio"
class="form-check-input ' . esc_attr( $field['class'] ) . '"
style="' . esc_attr( $field['style'] ) . '"
' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '
' . $required . '
/> '. esc_html( $value ) . '</label>';
echo '</div>';
$required = '';
}
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
echo '<small class="description">' . wp_kses_post( $field['description'] ) . '</small>';
}
echo '</div>';
}
/**
* Functions to group Terms-Years by decade
*/
function u_group_year_terms( $taxonomy ){
if( empty( $taxonomy ) ){
return;
}
$tab_args = array(
'taxonomy' => $taxonomy,
'hide_empty' => true,
'orderby' => 'name',
'order' => 'ASC'
);
$tab_terms = get_terms( $tab_args );
$grouped_terms = array();
if( !empty( $tab_terms ) ){
foreach( $tab_terms as $term ){
$grouped_terms[ substr( $term->name, 0, 3 ) . "0's" ][] = $term;
}
}
return $grouped_terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment