Skip to content

Instantly share code, notes, and snippets.

@nmedia82
Last active September 21, 2017 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmedia82/c4e5ffc36e61a9ecbe3dc0d0e87b29ed to your computer and use it in GitHub Desktop.
Save nmedia82/c4e5ffc36e61a9ecbe3dc0d0e87b29ed to your computer and use it in GitHub Desktop.
NM Input Class
<?php
/**
* Inputs rendering class
**/
// constants/configs
define( 'ECHOABLE', false );
class NM_Form {
/**
* the static object instace
*/
private static $ins = null;
private $echoable;
private $defaults;
function __construct() {
// should control echo or return
$this -> echoable = $this->get_property( 'echoable' );
// control defaul settings
$this -> defaults = $this->get_property( 'defaults' );
// Local filters
add_filter('nmform_attribute_value', array($this, 'adjust_attributes_values'), 10, 3);
}
public function Input( $args, $default_value = '' ) {
$type = $this -> get_attribute_value( 'type', $args);
switch( $type ) {
case 'text':
case 'date':
case 'datetime-local':
case 'email':
case 'number':
$input_html = $this -> Regular( $args, $default_value );
break;
case 'textarea':
$input_html = $this -> Textarea( $args, $default_value );
break;
case 'select':
$input_html = $this -> Select( $args, $default_value );
break;
case 'checkbox':
$input_html = $this -> Checkbox( $args, $default_value );
break;
case 'radio':
$input_html = $this -> Radio( $args, $default_value );
break;
}
if( $this -> echoable )
echo $input_html;
else
return $input_html;
}
/**
* Regular Input Field
* 1. Text
* 2. Date
* 3. Email
* 4. Number
**/
public function Regular( $args, $default_value = '' ) {
$type = $this -> get_attribute_value( 'type', $args);
$label = $this -> get_attribute_value('label', $args);
$classes = $this -> get_attribute_value('classes', $args);
$id = $this -> get_attribute_value('id', $args);
$name = $this -> get_attribute_value('name', $args);
$placeholder= $this -> get_attribute_value('placeholder', $args);
$input_wrapper_class = $this->get_default_setting_value('global', 'input_wrapper_class', $id);
$html = '<div class="'.$input_wrapper_class.'">';
if( $label ){
$html .= '<label class="'.$this->get_default_setting_value('global', 'label_class', $id).'" for="'.$id.'">';
$html .= sprintf(__("%s", "nmform"), $label) .'</label>';
}
$html .= '<input type="'.esc_attr($type).'" ';
$html .= 'id="'.esc_attr($id).'" ';
$html .= 'name="'.esc_attr($name).'" ';
$html .= 'class="'.esc_attr($classes).'" ';
$html .= 'placeholder="'.esc_attr($placeholder).'" ';
//Values
if( $default_value != '')
$html .= 'value="'.esc_attr($default_value).'" ';
$html .= '>';
$html .= '</div>'; //form-group
// filter: nmforms_input_htmls
return apply_filters("nmforms_input_html", $html, $args, $default_value);
}
/**
* Textarea field only
*
* filter: nmforms_input_htmls
* filter:
* */
function Textarea($args, $default_value = '') {
$label = $this -> get_attribute_value('label', $args);
$classes = $this -> get_attribute_value('classes', $args);
$id = $this -> get_attribute_value('id', $args);
$name = $this -> get_attribute_value('name', $args);
$placeholder= $this -> get_attribute_value('placeholder', $args);
// cols & rows
$cols = $this -> get_attribute_value( 'cols', $args );
$rows = $this -> get_attribute_value( 'rows', $args );
$input_wrapper_class = $this->get_default_setting_value('global', 'input_wrapper_class', $id);
$html = '<div class="'.$input_wrapper_class.'">';
if( $label ){
$html .= '<label class="'.$this->get_default_setting_value('global', 'label_class', $id).'" for="'.$id.'">';
$html .= sprintf(__("%s", "nmform"), $label) .'</label>';
}
$html .= '<textarea ';
$html .= 'id="'.esc_attr($id).'" ';
$html .= 'name="'.esc_attr($name).'" ';
$html .= 'class="'.esc_attr($classes).'" ';
$html .= 'placeholder="'.esc_attr($placeholder).'" ';
$html .= 'cols="'.esc_attr($cols).'" ';
$html .= 'rows="'.esc_attr($rows).'" ';
$html .= '>'; // Closing textarea
//Values
if( $default_value != '')
$html .= esc_html($default_value);
$html .= '</textarea>';
$html .= '</div>'; //form-group
// filter: nmforms_input_htmls
return apply_filters("nmforms_input_html", $html, $args, $default_value);
}
/**
* Select options
*
* $options: array($key => $value)
**/
public function Select( $args, $selected_value = '' ) {
$label = $this -> get_attribute_value('label', $args);
$classes = $this -> get_attribute_value('classes', $args);
$id = $this -> get_attribute_value('id', $args);
$name = $this -> get_attribute_value('name', $args);
$multiple = $this -> get_attribute_value('multiple', $args);
// Options
$options = $this -> get_attribute_value('options', $args);
if ( ! $options ) return;
$input_wrapper_class = $this->get_default_setting_value('global', 'input_wrapper_class', $id);
$html = '<div class="'.$input_wrapper_class.'">';
if( $label ){
$html .= '<label class="'.$this->get_default_setting_value('global', 'label_class', $id).'" for="'.$id.'">';
$html .= sprintf(__("%s", "nmform"), $label) .'</label>';
}
$html .= '<select ';
$html .= 'id="'.esc_attr($id).'" ';
$html .= 'name="'.esc_attr($name).'" ';
$html .= 'class="'.esc_attr($classes).'" ';
$html .= ($multiple) ? 'multiple' : '';
$html .= '>'; // Closing textarea
foreach($options as $key => $value) {
// for multiple selected
if( is_array($selected_value) ){
foreach($selected_value as $s){
$html .= '<option '.selected( $s, $key, false ).' value="'.$key.'">'.$value.'</option>';
}
} else {
$html .= '<option '.selected( $selected_value, $key, false ).' value="'.$key.'">'.$value.'</option>';
}
}
$html .= '</select>';
$html .= '</div>'; //form-group
// filter: nmforms_input_htmls
return apply_filters("nmforms_input_html", $html, $args, $selected_value);
}
// Checkbox
public function Checkbox( $args, $checked_value = '' ) {
$type = $this -> get_attribute_value( 'type', $args);
$label = $this -> get_attribute_value('label', $args);
$classes = $this -> get_attribute_value('classes', $args);
$id = $this -> get_attribute_value('id', $args);
$name = $this -> get_attribute_value('name', $args);
// Options
$options = $this -> get_attribute_value('options', $args);
// Checkbox label class
$check_wrapper_class = $this -> get_attribute_value('check_wrapper_class', $args);
$check_label_class = $this -> get_attribute_value('check_label_class', $args);
if ( ! $options ) return;
$input_wrapper_class = $this->get_default_setting_value('global', 'input_wrapper_class', $id);
$html = '<div class="'.$input_wrapper_class.'">';
if( $label ){
$html .= '<label class="'.$this->get_default_setting_value('global', 'label_class', $id).'" for="'.$id.'">';
$html .= sprintf(__("%s", "nmform"), $label) .'</label>';
}
$html .= '<label class="'.$this->get_default_setting_value('global', 'label_class', $id).'" for="'.$id.'">';
foreach($options as $key => $check_label) {
$checked_option = '';
if( ! empty($checked_value) ){
foreach($checked_value as $option){
$checked_option = checked( $option, $key, false );
}
}
$option_id = sanitize_key($label)."-".$key;
$html .= '<div class="'.esc_attr($check_wrapper_class).'">';
$html .= '<label class="'.esc_attr($check_label_class).'" for="'.esc_attr($option_id).'">';
$html .= '<input type="'.esc_attr($type).'" ';
$html .= 'id="'.esc_attr($option_id).'" ';
$html .= 'name="'.esc_attr($name).'[]" ';
$html .= 'class="'.esc_attr($classes).'" ';
$html .= 'value="'.esc_attr($key).'"';
$html .= $checked_option;
$html .= '>'; // Closing checkbox
$html .= esc_attr($check_label);
$html .= '</label>'; // closing form-check
$html .= '</div>'; // closing form-check
}
$html .= '</div>'; //form-group
// filter: nmforms_input_htmls
return apply_filters("nmforms_input_html", $html, $args, $checked_value);
}
// Radio
public function Radio( $args, $checked_value = '' ) {
$type = $this -> get_attribute_value( 'type', $args);
$label = $this -> get_attribute_value('label', $args);
$classes = $this -> get_attribute_value('classes', $args);
$id = $this -> get_attribute_value('id', $args);
$name = $this -> get_attribute_value('name', $args);
// Options
$options = $this -> get_attribute_value('options', $args);
// Radio label class
$radio_wrapper_class = $this -> get_attribute_value('radio_wrapper_class', $args);
$radio_label_class = $this -> get_attribute_value('radio_label_class', $args);
if ( ! $options ) return;
$input_wrapper_class = $this->get_default_setting_value('global', 'input_wrapper_class', $id);
$html = '<div class="'.$input_wrapper_class.'">';
if( $label ){
$html .= '<label class="'.$this->get_default_setting_value('global', 'label_class', $id).'" for="'.$id.'">';
$html .= sprintf(__("%s", "nmform"), $label) .'</label>';
}
$html .= '<label class="'.$this->get_default_setting_value('global', 'label_class', $id).'" for="'.$id.'">';
foreach($options as $key => $check_label) {
$checked_option = '';
if( ! empty($checked_value) ){
$checked_option = checked( $checked_value, $key, false );
}
$option_id = sanitize_key($label)."-".$key;
$html .= '<div class="'.esc_attr($radio_wrapper_class).'">';
$html .= '<label class="'.esc_attr($radio_label_class).'" for="'.esc_attr($option_id).'">';
$html .= '<input type="'.esc_attr($type).'" ';
$html .= 'id="'.esc_attr($option_id).'" ';
$html .= 'name="'.esc_attr($name).'" ';
$html .= 'class="'.esc_attr($classes).'" ';
$html .= 'value="'.esc_attr($key).'"';
$html .= $checked_option;
$html .= '>'; // Closing checkbox
$html .= esc_attr($check_label);
$html .= '</label>'; // closing form-check
$html .= '</div>'; // closing form-check
}
$html .= '</div>'; //form-group
// filter: nmforms_input_htmls
return apply_filters("nmforms_input_html", $html, $args, $checked_value);
}
/**
* this function return current or/else default attribute values
*
* filter: nmform_attribute_value
*
* */
private function get_attribute_value( $attr, $args ) {
$attr_value = '';
$type = isset($args['type']) ? $args['type'] : $this->get_default_setting_value('global', 'type');
if( $attr == 'type' ) return $type;
if( isset($args[$attr]) ){
$attr_value = $args[$attr];
} else {
$attr_value = $this->get_default_setting_value( $type, $attr );
}
return apply_filters('nmform_attribute_value', $attr_value, $attr, $args);
}
/**
* this function return default value
* defined in class/config
*
* @params: $setting_type
* @params: $key
* filter: default_setting_value
* */
function get_default_setting_value( $setting_type, $key, $field_id = '' ){
$defaults = $this -> get_property( 'defaults' );
$default_value = isset( $defaults[$setting_type][$key] ) ? $defaults[$setting_type][$key] : '';
return apply_filters('default_setting_value', $default_value, $setting_type, $key, $field_id);
}
/**
* function return class property values/settings
*
* filter: nmform_property-{$property}
* */
private function get_property( $property ) {
$value = '';
switch( $property ) {
case 'echoable':
$value = ECHOABLE;
break;
case 'defaults':
$value = array(
'global' => array('type' => 'text',
'input_wrapper_class'=>'form-group',
'label_class' => 'form-control-label',),
'text' => array('placeholder' => "Please enter"),
'date' => array(),
'email' => array(),
'number' => array(),
'textarea' => array('cols' => 6, 'rows' => 3),
'select' => array('multiple' => false),
'checkbox' => array('label_class' => 'form-control-label',
'check_wrapper_class' => 'form-check',
'check_label_class' => 'form-check-label',
'classes' => array('form-check-input')),
'radio' => array('label_class' => 'form-control-label',
'radio_wrapper_class' => 'form-check',
'radio_label_class' => 'form-check-label',
'classes' => array('form-check-input')),
);
break;
}
return apply_filters("nmform_property-{$property}", $value);
}
/**
* ====================== FILTERS =====================================
*
* */
public function adjust_attributes_values( $attr_value, $attr, $args ) {
switch( $attr ) {
// converting classes to string
case 'classes':
$attr_value = implode(" ", $attr_value);
break;
/**
* converting name to array for multiple:select
* */
case 'name':
$type = $this -> get_attribute_value( 'type', $args);
$multiple = $this -> get_attribute_value('multiple', $args);
if( $type == 'select' && $multiple ){
$attr_value .= '[]';
}
break;
}
return $attr_value;
}
/**
* ====================== ENDs FILTERS =====================================
*
* */
public static function get_instance()
{
// create a new object if it doesn't exist.
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
}
function NMForm(){
return NM_Form::get_instance();
}
// Regular: text, email, date, number, datetime, datetime-local
$args = array( 'id' => 'lcms_case_title',
'name' => 'lcms[case_title]',
'classes' => array('form-control'),
'label' => "Case Title",
);
echo NMForm() -> Input($args, $case_info['case_title']);
// Select example
$options = lcms_get_tax_array('case_group');
$selected = $case_info['taxonomy_case_group'];
$args = array( 'id' => 'case_group',
'type' => 'select',
'name' => 'lcms[case_group]',
'classes' => array('form-control'),
'label' => "Case Group",
'options' => $options,
);
echo NMForm() -> Input($args, $selected);
// Textarea example
$args = array( 'id' => 'case_detail',
'type' => 'textarea',
'name' => 'lcms[case_detail]',
'classes' => array('form-control'),
'label' => "Case Detail",
);
echo NMForm() -> Input($args, $case_info['case_detail']);
// Radio example
$options = array('local'=>'Local','cloud'=>'Cloud','none'=>'No Recording');
$checked = 'cloud';
$args = array( 'id' => 'zoom_auto_record',
'type' => 'radio',
'name' => 'zoom[option_auto_record_type]',
'classes' => array('form-control'),
'label' => 'Auto Recording Option',
'options' => $options,
);
echo NMForm() -> Input($args, $checked);
// Checkbox example
$options = lqo_array_get_zoom_meeting_options();
$checked = array("option_participants_video");
$args = array( 'id' => 'zoom_options',
'type' => 'checkbox',
'name' => 'zoom[options]',
'label' => 'Meeting Options',
'options' => $options,
);
echo NMForm() -> Input($args, $checked);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment