Skip to content

Instantly share code, notes, and snippets.

@themefoundation
Last active October 12, 2015 09:07
Show Gist options
  • Save themefoundation/4003654 to your computer and use it in GitHub Desktop.
Save themefoundation/4003654 to your computer and use it in GitHub Desktop.
Input Class Demo
<?php
class THTK_Form_Input {
// Sets default form element properties
public $defaults = array(
'id' => '', // Unique ID for this option
'class' => '', // Optional CSS classes for input
'title' => '', // The content to display as the input title/label
'value' => '', // The option value
'desc' => '', // Descriptive text
'size' => 'default', // The size of the input (small, default, large; default: default)
'align' => 'left', // The alignment of the input (left, right; default: left)
'before' => '', // Custom content to place before the input
'after' => '' // Custom content to place after the input
);
/**
* Displays a label for an HTML form element
*
* @since 1.0
* @param string $label The text to display in the label.
* @param string $id ID of the form element to which this label belongs.
* @return string The HTML label element.
*/
public function get_label( $label, $id = '' ) {
if ( $label ) {
return '<label for="' . $id . '">' . $label . '</label>';
}
}
/**
* Displays a form element description
*
* Outputs an HTML form element description. Used if more explanation of a
* form element is needed.
*
* @since 1.0
* @param string $description Text to display in the description.
* @return string The description formatted as a paragraph.
*/
function get_description( $description ) {
return ' <p class="description">' . $description . '</p>';
} // End form_description()
}
class THTK_Text_Input extends THTK_Form_Input{
/**
* Displays a text input
*
* @since 1.0
* @param array $args Details used to create the text input.
* @return string The HTML text input element.
*/
public function get_text_input( $args = '' ) {
// Creates new array by merging the provided arguments array with the defaults array.
$element_details = wp_parse_args( $args, $this->defaults );
// Extracts the element details array into individual variables.
extract( $element_details );
// Return our output
return '<input type="text" value="' . esc_attr( $value ) . '" name="' . $id . '" id="' . $id . '" class="' . esc_attr( 'option-field-' . esc_attr( $size ) . ' ' . $class ) . '" />';
}
/**
* Displays a text input formatted for Startbox
*
* @since 1.0
* @param array $args Details used to create the text input.
* @return string The HTML text input element with formatting for StartBox.
*/
public function get_text_input_sb( $args = '' ) {
// Creates new array by merging the provided arguments array with the defaults array.
$element_details = wp_parse_args( $args, $this->defaults );
// Extracts the element details array into individual variables.
extract( $element_details );
// Creates a variable that will hold the output string.
$output = '';
// Generates the output string
$output .= '<p class="' . esc_attr( $args['id'] ) . '">';
$output .= $this->get_label( $title, $id );
$output .= '<span class="' .esc_attr( $align ) . '">';
$output .= $before;
$output .= $this->get_text_input( $element_details );
$output .= $after;
$output .= '</span>';
$output .= $this->get_description( $desc );
$output .= '</p>'."\n";
// Return our output
return $output;
}
/**
* Displays a text input formatted for use with custom metaboxes
*
* @since 1.0
* @param array $args Details used to create the text input.
* @return string The HTML text input element with formatting for metaboxes.
*/
public function get_metabox_text_input( $args = '' ) {
// Creates new array by merging the provided arguments array with the defaults array.
$element_details = wp_parse_args( $args, $this->defaults );
// Extracts the element details array into individual variables.
extract( $element_details );
// Creates a variable that will hold the output string.
$output = '';
// Generates the output string
$output .= '<tr class="' . esc_attr( $id ) . '"><th>';
$output .= $this->get_label( $title, $id );
$output .= '</th><td>';
$output .= '<span class="' .esc_attr( $align ) . '">';
$output .= $before;
$output .= $this->get_text_input( $element_details );
$output .= $after;
$output .= '</span>';
$output .= $this->get_description( $desc );
$output .= '</td></tr>'."\n";
// Returns the output string
return $output;
}
}
class THTK_Checkbox_Input extends THTK_Form_Input{
/**
* Displays a checkbox input
*
* @since 1.0
* @param string $name Name property for the checkbox.
* @param string $label Text string for the checkbox label.
* @param string $value Value used to determine the checkbox default state.
*/
public function get_checkbox_input( $args = '' ) {
// Creates new array by merging the provided arguments array with the defaults array.
$element_details = wp_parse_args( $args, $this->defaults );
// Extracts the element details array into individual variables.
extract( $element_details );
// Creates a variable to hold the output string.
$output = '';
// Generates the output string
$output .= '<label for="' . $id . '">';
$output .= '<input type="checkbox" id="' . $id . '" name="' . $id . '" value="' . $label . '"';
if ( $value ) {
$output .= ' checked';
}
$output .= ' /> ' . $label . '</label><br />';
return $output;
} // End get_checkbox_input()
/**
* Displays a checkbox input formatted for use with custom metaboxes
*
* @since 1.0
* @param array $args Details used to create the checkbox input.
* @return string The HTML checkbox input element with formatting for metaboxes.
*/
public function get_metabox_checkbox_input( $args = '' ) {
// Creates new array by merging the provided arguments array with the defaults array.
$element_details = wp_parse_args( $args, $this->defaults );
// Extracts the element details array into individual variables.
extract( $element_details );
// Creates a variable to hold the output string.
$output = '';
$output .= '<tr class="' . esc_attr( $id ) . '">';
$output .= '<th>' . $title . '</th>';
$output .= '<td>';
$output .= '<span class="' .esc_attr( $align ) . '">';
$output .= $before;
$output .= $this->get_checkbox_input( $element_details );
$output .= $after;
$output .= '</span>';
$output .= $this->get_description( $desc );
$output .= '</td>';
$output .= '</tr>'."\n";
// Returns the output string
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment