'',
'method' => 'POST',
'action' => '',
'is_ajaxified' => false,
'class' => 'ap-form',
'submit_button' => __('Submit', 'ap'),
);
// Merge defaults args
$this->args = wp_parse_args( $args, $defaults );
// set the name of the form
$this->name = $this->args['name'];
global $ap_errors;
$this->errors = $ap_errors;
$this->add_default_in_field();
$this->order_fields();
}
private function add_default_in_field()
{
if(!isset($this->args['fields']))
return;
foreach($this->args['fields'] as $k => $field){
if(!isset($field['order']))
$this->args['fields'][$k]['order'] = 10;
if(!isset($field['show_desc_tip']))
$this->args['fields'][$k]['show_desc_tip'] = true;
}
}
/**
* Order fields
* @return void
* @since 2.0.1
*/
private function order_fields()
{
if(!isset($this->args['fields']))
return;
usort($this->args['fields'], function($a, $b) {
return $a['order'] - $b['order'];
});
}
/**
* Build the form
* @return void
* @since 2.0.1
*/
public function build()
{
$this->form_head();
$this->form_fields();
$this->hidden_fields();
$this->form_footer();
}
/**
* FORM element
* @return void
* @since 2.0.1
*/
private function form_head()
{
$attr = '';
if($this->args['is_ajaxified'])
$attr .= ' data-action="ap_ajax_form"';
if(!empty($this->args['class']))
$attr .= ' class="'.$this->args['class'].'"';
ob_start();
/**
* ACTION: ap_form_before_[form_name]
* action for hooking before form
* @since 2.0.1
*/
do_action('ap_form_before_'. $this->name);
$this->output .= ob_get_clean();
$this->output .= '
';
}
private function nonce()
{
$nonce_name = isset($this->args['nonce_name']) ? $this->args['nonce_name'] : $this->name;
$this->output .= wp_nonce_field( $nonce_name, '__nonce', true, false) ;
}
/**
* Form hidden fields
* @return void
* @since 2.0.1
*/
private function hidden_fields()
{
if($this->args['is_ajaxified'])
$this->output .= ' ';
$this->output .= ' ';
$this->nonce();
}
/**
* form field label
* @return void
* @since 2.0.1
*/
private function label()
{
if($this->field['label'] && !$this->field['show_desc_tip']){
$this->output .= ''. @$this->field['label'].' ';
}elseif($this->field['label']){
$this->output .= ''. @$this->field['label'];
$this->desc();
$this->output .= ' ';
}
}
/**
* Output placeholder attribute of current field
* @return string
* @since 2.0.1
*/
private function placeholder(){
return !empty($this->field['placeholder']) ? ' placeholder="'.$this->field['placeholder'].'"' : '';
}
/**
* Output description of a form fields
* @return void
* @since 2.0.1
*/
private function desc(){
if(!$this->field['show_desc_tip'])
$this->output .= (!empty($this->field['desc']) ? ''.$this->field['desc'].'
' : '');
else
$this->output .= (!empty($this->field['desc']) ? '? ' : '');
}
/**
* Output text fields
* @param array $field
* @return void
* @since 2.0
*/
private function text_field($field = array())
{
if(isset($field['label']))
$this->label();
$placeholder = $this->placeholder();
$autocomplete = isset($field['autocomplete']) ? ' autocomplete="off"' : '';
$this->output .= ' ';
$this->error_messages();
}
/**
* Checkbox field
* @param array $field
* @return void
* @since 2.0.1
*/
private function checkbox_field($field = array())
{
if(isset($field['label']))
$this->label();
if(!empty($field['desc']))
$this->output .= '';
$this->output .= ' ';
$this->desc();
$this->error_messages();
if(!empty($field['desc']))
$this->output .= '
';
}
/**
* output select field options
* @param array $field
* @return void
* @since 2.0.1
*/
private function select_options($field = array())
{
foreach($field['options'] as $k => $opt )
$this->output .= ''.$opt.' ';
}
/**
* Select fields
* @param array $field
* @return void
* @since 2.0.1
*/
private function select_field($field = array())
{
if(isset($field['label']))
$this->label();
$this->output .= '';
$this->output .= ' ';
$this->select_options($field);
$this->output .= ' ';
$this->error_messages();
}
/**
* output select field options
* @param array $field
* @return void
* @since 2.0.1
*/
private function taxonomy_select_options($field = array())
{
$taxonomies = get_terms( $field['taxonomy'], 'orderby=count&hide_empty=0&hierarchical=0' );
if($taxonomies){
foreach($taxonomies as $tax )
$this->output .= 'term_id, $field['value'], false).'>'.$tax->name.' ';
}
}
/**
* Taxonomy select field
* @param array $field
* @return void
* @since 2.0.1
*/
private function taxonomy_select_field($field = array())
{
if(isset($field['label']))
$this->label();
$this->output .= '';
$this->output .= ' ';
$this->taxonomy_select_options($field);
$this->output .= ' ';
$this->error_messages();
}
/**
* textarea fields
* @param array $field
* @return void
* @since 2.0
*/
private function textarea_field($field = array())
{
if(isset($field['label']))
$this->label();
$placeholder = $this->placeholder();
$this->output .= '';
$this->error_messages();
}
/**
* Create wp_editor field
* @param array $field
* @return void
* @since 2.0.1
*/
private function editor_field($field = array())
{
if(isset($field['label']))
$this->label();
/**
* FILTER: ap_pre_editor_settings
* Can be used to mody wp_editor settings
* @var array
* @since 2.0.1
*/
$field['settings']['tinymce'] = array(
'content_css' => ap_get_theme_url('css/editor.css')
);
$settings = apply_filters('ap_pre_editor_settings', $field['settings'] );
// Turn on the output buffer
ob_start();
echo '';
wp_editor( $field['value'], $field['name'], $field['settings'] );
echo '
';
$this->output .= ob_get_clean();
$this->error_messages();
}
/**
* For creating hidden input fields
* @param array $field
* @return void
* @since 2.0.1
*/
private function hidden_field($field = array()){
$this->output .= ' ';
}
private function custom_field($field = array()){
$this->output .= $field['html'];
}
/**
* Check if current field have any error
* @return boolean
* @since 2.0.1
*/
private function have_error(){
if(isset($this->errors[$this->field['name']]))
return true;
return false;
}
private function error_messages(){
if(isset($this->errors[$this->field['name']])){
$this->output .= '';
}
}
/**
* Out put all form fields based on on their type
* @return void
* @since 2.0
*/
private function form_fields()
{
/**
* FILTER: ap_pre_form_fields
* Provide filter to add or override form fields before output.
* @var array
* @since 2.0.1
*/
$this->args['fields'] = apply_filters('ap_pre_form_fields', $this->args['fields'] );
foreach($this->args['fields'] as $field){
$this->field = $field;
$error_class = $this->have_error() ? ' ap-have-error' : '';
switch ($field['type']) {
case 'text':
$this->output .= '';
$this->text_field($field);
$this->output .= '
';
break;
case 'checkbox':
$this->output .= '';
$this->checkbox_field($field);
$this->output .= '
';
break;
case 'select':
$this->output .= '';
$this->select_field($field);
$this->output .= '
';
break;
case 'taxonomy_select':
$this->output .= '';
$this->taxonomy_select_field($field);
$this->output .= '
';
break;
case 'textarea':
$this->output .= '';
$this->textarea_field($field);
$this->output .= '
';
break;
case 'editor':
$this->output .= '';
$this->editor_field($field);
$this->output .= '
';
break;
case 'hidden':
$this->hidden_field($field);
break;
case 'custom':
$this->custom_field($field);
break;
default:
/**
* FILTER: ap_form_fields_[type]
* filter for custom form field type
*/
$this->output .= apply_filters( 'ap_form_fields_'.$field['type'], $field);
break;
}
}
}
/**
* Output form
* @return string
* @since 2.0.1
*/
public function get_form()
{
if(empty($this->args['fields']))
return __('No fields found', 'ap');
$this->build();
return $this->output;
}
}