Created
November 6, 2012 19:54
-
-
Save alexmansfield/4027076 to your computer and use it in GitHub Desktop.
WordPress Form Classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class THTK_Form_Input { | |
// Sets default form element properties | |
public $defaults = array( | |
'id' => '', // Unique element ID. | |
'class' => '', // Optional. CSS class names. | |
'title' => '', // Text to display as the input title/label. | |
'value' => '', // Optional. The value of the input field. | |
'desc' => '', // Optional. Description of form element. | |
'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. | |
); | |
// Sets the variable that will hold the specific form element properties | |
public $particulars = ''; | |
/** | |
* Sets the public $particulars variable by merging the arguments array with the defaults array. | |
* | |
* @since 1.0 | |
* @param array $args The array of arguments to use when creating the form element. | |
*/ | |
public function __construct( $args ) { | |
$this->particulars = wp_parse_args( $args, $this->defaults ); | |
} | |
/** | |
* Displays the label for a 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>'; | |
} | |
} // End get_label() | |
/** | |
* Generates a form element description | |
* | |
* @since 1.0 | |
* @param string $desc Text to display in the description. | |
* @return string The description string formatted as a paragraph. | |
*/ | |
function get_description( $desc ) { | |
if ( $desc ) { | |
return ' <p class="description">' . $desc . '</p>'; | |
} | |
} // End get_description() | |
/** | |
* Generates a text input | |
* | |
* @since 1.0 | |
* @return string The HTML text input element. | |
*/ | |
public function get_text_input() { | |
// Extracts the element details array into individual variables. | |
extract( $this->particulars ); | |
// Returns the output string. | |
return '<input type="text" value="' . esc_attr( $value ) . '" name="' . $id . '" id="' . $id . '" class="' . esc_attr( 'option-field-' . esc_attr( $size ) . ' ' . $class ) . '" />'; | |
} // End get_text_input() | |
/** | |
* Generates a checkbox input | |
* | |
* @since 1.0 | |
* @return string The HTML checkbox input element. | |
*/ | |
public function get_checkbox_input() { | |
// Extracts the element details array into individual variables. | |
extract( $this->particulars ); | |
// 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="true"'; | |
if ( $value ) { | |
$output .= ' checked'; | |
} | |
$output .= ' /> '; | |
if( isset( $label ) ) { | |
$output .= $label; | |
} | |
$output .= '</label><br />'; | |
// Returns the output string. | |
return $output; | |
} // End get_checkbox_input() | |
/** | |
* Generates a set of radio buttons | |
* | |
* @since 1.0 | |
* @return string The HTML radio button input element. | |
*/ | |
public function get_radio_buttons() { | |
// Extracts the element details array into individual variables. | |
extract( $this->particulars ); | |
// Sets the $line_break variable if not already set. | |
if( !isset( $line_break ) ) { | |
$line_break = true; | |
} | |
// Creates a variable to hold the output string. | |
$output = ''; | |
// Generates the output string. | |
foreach ( $choices as $choice => $label ) { | |
$output .= '<label for="' . $choice . '">'; | |
$output .= '<input type="radio" id="' . $choice . '" name="' . $id . '" value="' . $choice . '"'; | |
if ( $value == $choice ) { | |
$output .= ' checked'; | |
} | |
$output .= ' /> ' . $label . '</label>'; | |
if ( $line_break ) { | |
$output .= '<br />'; | |
} | |
} | |
// Returns the output string. | |
return $output; | |
} // End get_radio_buttons() | |
/** | |
* Generates a select list | |
* | |
* @since 1.0 | |
* @return string The HTML select list input element. | |
*/ | |
public function get_select_list() { | |
// Extracts the element details array into individual variables. | |
extract( $this->particulars ); | |
// Creates a variable to hold the output string. | |
$output = ''; | |
// Generates the output string. | |
$output = '<select id="' . $id . '" name="' . $id . '">'; | |
foreach ( $choices as $choice => $label ) { | |
$selected = ( $choice == $value ) ? ' selected' : ''; | |
$output .= '<option value="' . $choice . '"' . $selected . '>' . $label . ' </option>'; | |
} // End foreach $choices | |
$output .= '</select>'; | |
// Returns the output string. | |
return $output; | |
} // End get_select_list() | |
/** | |
* Generates a textarea | |
* | |
* @since 1.0 | |
* @return string The HTML textarea input element. | |
*/ | |
public function get_textarea() { | |
// Extracts the element details array into individual variables. | |
extract( $this->particulars ); | |
// Creates a variable to hold the output string. | |
$output = ''; | |
// Generates the output string. | |
$output = '<textarea'; | |
$output .= ' id="' . $id . '"'; | |
$output .=' name="' . $id . '">'; | |
if ( $value ) { | |
$output .= $value; | |
} // End if | |
$output .= '</textarea>'; | |
// Returns the output string. | |
return $output; | |
} // End get_textarea() | |
} // End class THTK_Form_Input | |
class THTK_Form_Metabox extends THTK_Form_Input{ | |
/** | |
* Displays a form element with metabox formatting | |
* | |
* @since 1.0 | |
* @return string The fully formatted metabox option. | |
*/ | |
function get_metabox() { | |
// Extracts the element details array into individual variables. | |
extract( $this->particulars ); | |
// Creates a variable to 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; | |
// Calls form element display functions based on the type of input field. | |
switch ( $type ) { | |
case 'text': | |
$output .= $this->get_text_input(); | |
break; | |
case 'checkbox': | |
$output .= $this->get_checkbox_input(); | |
break; | |
case 'radio': | |
$output .= $this->get_radio_buttons(); | |
break; | |
case 'select': | |
$output .= $this->get_select_list(); | |
break; | |
case 'textarea': | |
$output .= $this->get_textarea(); | |
break; | |
} | |
$output .= $after; | |
$output .= '</span>'; | |
$output .= $this->get_description( $desc ); | |
$output .= '</td></tr>'."\n"; | |
// Returns the output string. | |
return $output; | |
} // End get_metabox() | |
} // End class THTK_Form_Metabox | |
class THTK_Form_Formatted extends THTK_Form_Input{ | |
/** | |
* Displays a form input with label and description fully formatted | |
* | |
* @since 1.0 | |
* @return string The HTML text input element with full formatting. | |
*/ | |
public function get_formatted() { | |
// Extracts the element details array into individual variables. | |
extract( $this->particulars ); | |
// Creates a variable to hold the output string. | |
$output = ''; | |
// Generates the output string. | |
$output .= '<p class="' . esc_attr( $id ) . '">'; | |
$output .= $this->get_label( $title, $id ); | |
$output .= '<span class="' .esc_attr( $align ) . '">'; | |
$output .= $before; | |
// Calls form element display functions based on the type of input field. | |
switch ( $type ) { | |
case 'text': | |
$output .= $this->get_text_input(); | |
break; | |
case 'checkbox': | |
$output .= $this->get_checkbox_input(); | |
break; | |
case 'radio': | |
$output .= $this->get_radio_buttons(); | |
break; | |
case 'select': | |
$output .= $this->get_select_list(); | |
break; | |
case 'textarea': | |
$output .= $this->get_textarea(); | |
break; | |
} | |
$output .= $after; | |
$output .= '</span>'; | |
$output .= $this->get_description( $desc ); | |
$output .= '</p>'."\n"; | |
// Returns the output string. | |
return $output; | |
} // End get_formatted() | |
} // End class THTK_Form_Formatted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment