name_prefix = $newfix; } /** * return the html for any field based on given params * Prepopulate the value in parameter $current_value. If no current_value, then $field['value'] will prepopulate. */ public function get_field($field, $current_value = '', $enabled = true){ if(gettype($current_value) == 'string'){ $current_value = htmlentities($current_value); } /**** Fallback defaults */ if(!isset($field['type'])){ $field['type'] = 'text'; //--strict, and practical } if(!isset($field['name'])){ $field['name'] = false; //--strict } if(!isset($field['value'])){ $field['value'] = ''; //--strict } if(!isset($field['default'])){ $field['default'] = ''; //--strict } if(!isset($field['label'])){ $field['label'] = ''; //--strict } if(!isset($field['tooltip'])){ $field['tooltip'] = ''; //--strict } if(!isset($field['placeholder'])){ $field['placeholder'] = ''; //--strict } if(!isset($field['options'])){ $field['options'] = array(); //--strict } /**** Prepare auto postid for some fields */ global $post; if($post && $post->ID){ $post_id = $post->ID; }else{ $post_id = ''; } /**** Prepare some shortened parameters. These will most likely be used later */ $prefix = $this->name_prefix; $type = $field['type']; $label = $field['label']; $tooltip = $field['tooltip']; $placeholder= $field['placeholder']; $default = $field['default']; $value = $field['value']; //--strict $options = $field['options']; //--strict $checked = ' checked="checked" '; $selected = ' selected="selected" '; $current_active = false; //--strict $image_manipulators = ''; //--strict //TODO: TYPE MAP /**** Verify the "name" parameter */ if(!$field['name']){ if($type == 'custom' && !$field['name']){ //If type="custom", then we can just create a fake name. Type custom does not save any values (atleast not automatically!) $field['name'] = 'fakename-'.mt_rand(1,100000000); }else{ return 'Error: Field missing parameter "name" for a field of type "'.$type.'"
'; } } //Name is OK $name = $field['name']; $prefixed_name = $prefix.$name; $id = $prefixed_name; /**** Some possible preparations for the previously saved value (if any) */ if($current_value === ''){ /* NO current value */ //Default value as fallback if($default){ $current_value = $default; } } /**** Prepare attributes */ $attr_id = ' id="'.$id.'"'; //for functioning labels. Also, practical $attr_type = ' type="'.$type.'"'; $attr_name = ' name="'.$prefixed_name.'"'; $attr_value = ' value="'.$current_value.'"'; /**** Optional and tagtype specifics */ //Labels! Connected to the input id $element_label = $label ? '' : ''; //Tooltips. Basic hoverable $tooltip = $tooltip ? '
'.$tooltip.'
' : ''; //Placeholders are great for example values. $attr_placeholder = $placeholder ? ' placeholder="'.$placeholder.'"' : ''; $editor_args = array(); /**** ENABLER! */ if(isset($field['enabler']) && $field['enabler']){ $enabler_label = is_string($field['enabler']) ? $field['enabler'] : 'Enable:'; $enabler_name = $this->name_prefix . $field['name'].'-enabled'; $enabler = '
'; }else{ $enabler = ''; //No enabler means always active! } /* Field specific style! */ $element_styles = ''; if(isset($field['style'])){ $field_style = $field['style']; $element_styles = ''; } /**** Time to actually do something useful! */ switch($type){ case 'image': $type = 'hidden';//Image field should save it's value like a hidden $attr_type = ' type="'.$type.'"'; //Reset attr type $image_manipulators = $this->image_field($field, $current_value);//Image selection buttons //NO break - use same as hidden below case 'text': case 'password': case 'hidden': $element = $element_label; $element .= ''.$image_manipulators; break; case 'radio': $element = ''; //For radios, we iterate options $current_found = false; $count = 1; foreach($options as $k=>$v){ $current_option = ($v==$current_value) ? $checked : ''; $attr_value = ' value="'.$v.'"'; $radio_id = 'radio-'.$name.'-'.$count; $element .= '
'; $element .= ''; $element .= ''; $element .= '
'; if($current_option) $current_found = true; $count++; } if(!$current_found && $default){ //No "current" radio found. Attempt to select the default value $element = str_replace(' value="'.$default.'"', $checked.' value="'.$default.'"', $element); } break; case 'checkbox': $element = $element_label; $element .= 'chosen_includes(); $select2 = ' class="chosen"'; case 'select': case 'dropdown': $element = $element_label; $element .= ''; //The select-element is populated by the given options-parameter, which should be an array foreach($options as $k=>$v){ $attr_value = ' value="'.htmlentities($v).'"'; $current_option = ($v==$current_value) ? $selected : ''; $element .= ''.$k.''; } $element .= ''; break; case 'textarea': $element = $element_label; $element .= ''.$current_value.''; break; case 'separater': case 'separator': $element = '
'; break; case 'color': case 'colour': case 'colorpicker': case 'colourpicker': //Actually, use type text. This allows us to write the hex-color $attr_type = ' type="text"'; $element = $element_label; $element .= ''; break; /* ACTUALLY -- lets rethink this .. */ case 'font-selector': $elem_start = ''; break; /* EDITOR - wp-style */ case 'editor-teeny': $editor_args['teeny'] = true; $editor_args['media_buttons'] = false; $editor_args['editor_height'] = 120; case 'editor': $editor_args['wpautop'] = false; $editor_args['textarea_name'] = $prefixed_name; ob_start(); if($label){ echo '

'.$label.'

'; } wp_editor( html_entity_decode($current_value), $prefixed_name.'-editorfield', $editor_args); $element = ob_get_clean(); break; /* This field does not autosave anything - And can be used for anything */ case 'custom': //Custom field shows whatever is in it's value-parameter. Post id can be included $element = $element_label; $element .= str_replace('__POSTID__', $post_id, $value); break; /* This field is merely for layout */ case 'pagebreak': $current_page = isset($_GET['hash']) && $_GET['hash'] === $field['name'] ? ' current-fieldset' : ''; //Custom field shows whatever is in it's value-parameter. Post id can be included $element = '
'.$label.'
'; return $element; break; default: $element = 'ERROR: Uknown field type "'.$type.'"'; break; } //Now we are ready to complete this mission and return safely $complete_element = ''. $enabler. $element. $tooltip. $element_styles. '
'; return $complete_element; } public function include_colorpicker(){ //check if already in place if(wp_script_is('wp-color-picker')){ return; } //Make color picker become reality wp_enqueue_style('wp-color-picker'); wp_enqueue_script('wp-color-picker'); add_action('admin_footer', function(){?> included_chosens){return;} $this->included_chosens = true; add_filter('admin_footer', function(){ echo ''."\n"; echo ''."\n"; echo ''; }); } /* Add the javascript to show some image selection stuff * Will only include the script once, even if called multiple times. */ protected function image_field_script(){ if($this->image_script_included) return; wp_enqueue_media(); ob_start()?> image_script_included = true; return ob_get_clean(); } /* Entry function for displaying image selector in wordpress */ protected function image_field($field, $current_value=''){ ob_start()?>
'; }?>
image_field_script();//only included once even if called multiple times! return $ret; } } ?>