'POST', 'action' => '', 'is_ajaxified' => false, 'class' => 'ap-form', 'multipart' => false, 'submit_button' => __( 'Submit', 'anspress-question-answer' ), ); // 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(); } /** * Add fields default values. */ private function add_default_in_field() { if ( ! isset( $this->args['fields'] ) ) { return; } if ( is_array( $this->args['fields'] ) ) { 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; } $this->args['fields'] = ap_sort_array_by_order( $this->args['fields'] ); } /** * Build the form * @return void * @since 2.0.1 */ public function build() { $this->form_head(); $this->form_fields(); if ( ! isset( $this->args['hide_footer'] ) || $this->args['hide_footer'] !== false ) { $this->hidden_fields(); $this->form_footer(); } } /** * FORM element * @return void * @since 2.0.1 */ private function form_head() { $attr = ''; if ( !empty( $this->args['attr'] ) ) { $attr .= $this->args['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(); // Add enctype if form is multipart. $multipart = $this->args['multipart'] ? ' enctype="multipart/form-data"' : ''; if ( ! isset( $this->args['hide_footer'] ) || $this->args['hide_footer'] !== false ) { $this->output .= '
'; } } /** * FORM footer * @return void * @since 2.0.1 */ private function form_footer() { ob_start(); /** * ACTION: ap_form_bottom_[form_name] * action for hooking captcha and extar fields * @since 2.0.1 */ do_action( 'ap_form_bottom_'. $this->name ); $this->output .= ob_get_clean(); $this->output .= ''; if ( @$this->args['show_cancel'] === true ) { $this->output .= ''; } $this->output .= '
'; } /** * Add nonce field */ 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() { $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 .= ''; } elseif ( $this->field['label'] ) { $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(), $type = 'text') { if ( isset( $field['label'] ) ) { $this->label(); } $placeholder = $this->placeholder(); $autocomplete = isset( $field['autocomplete'] ) ? ' autocomplete="off"' : ''; $this->output .= '
'; if ( ! isset( $field['repeatable'] ) || ! $field['repeatable'] ) { $this->output .= ''; if ( $type == 'password' ) { $this->output .= ''; } } else { if ( ! empty( $field['value'] ) && is_array( $field['value'] ) ) { $this->output .= '
'; foreach ( $field['value'] as $k => $rep_f ) { $this->output .= '
'; $this->output .= ''; $this->output .= '
'; } $this->output .= '
'; $this->output .= '
'; $this->output .= '
'; $this->output .= ''; $this->output .= '
'; $this->output .= ''; } } $this->error_messages(); if ( ! $this->field['show_desc_tip'] ) { $this->desc(); } $this->output .= '
'; } /** * Output text type="number" * @param array $field * @return void * @since 2.0.0-alpha2 */ private function number_field($field = array()) { if ( isset( $field['label'] ) ) { $this->label(); } $placeholder = $this->placeholder(); $autocomplete = isset( $field['autocomplete'] ) ? ' autocomplete="off"' : ''; $this->output .= '
'; $this->output .= ''; $this->error_messages(); if ( ! $this->field['show_desc_tip'] ) { $this->desc(); } $this->output .= '
'; } /** * Checkbox field * @param array $field * @return void * @since 2.0.1 */ private function checkbox_field($field = array()) { if ( isset( $field['label'] ) ) { $this->label(); } $this->output .= '
'; if ( ! empty( $field['desc'] ) ) { $this->output .= ''; } $this->error_messages(); $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 .= ''; } } /** * 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->error_messages(); if ( ! $this->field['show_desc_tip'] ) { $this->desc(); } $this->output .= '
'; } /** * 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 .= '
'; $taxonomies = wp_dropdown_categories( array( 'taxonomy' => $field['taxonomy'], 'orderby' => @$field['orderby'], 'hide_empty' => 0, 'hierarchical' => 1, 'selected' => @$field['value'], 'name' => @$field['name'], 'class' => 'ap-form-control', 'id' => @$field['name'], 'echo' => false ) ); $this->output .= $taxonomies; $this->error_messages(); if ( ! $this->field['show_desc_tip'] ) { $this->desc(); } $this->output .= '
'; } /** * Page select field * @param array $field * @return void * @since 2.0.0-alpha2 */ private function page_select_field($field = array()) { if ( isset( $field['label'] ) ) { $this->label(); } $this->output .= '
'; $this->output .= wp_dropdown_pages( array( 'show_option_none' => __( 'Select a page', 'anspress-question-answer' ), 'selected' => @$field['value'], 'name' => @$field['name'], 'post_type' => 'page', 'echo' => false ) ); $this->error_messages(); if ( ! $this->field['show_desc_tip'] ) { $this->desc(); } $this->output .= '
'; } /** * textarea fields * @param array $field * @return void * @since 2.0 */ private function textarea_field($field = array()) { if ( isset( $field['label'] ) ) { $this->label(); } $this->output .= '
'; $placeholder = $this->placeholder(); $this->output .= ''; $this->error_messages(); if ( ! $this->field['show_desc_tip'] ) { $this->desc(); } $this->output .= '
'; } /** * 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(); } if ( $field['settings']['tinymce'] !== false ) { $field['settings']['tinymce'] = array( 'content_css' => ap_get_theme_url( 'css/editor.css' ), 'wp_autoresize_on' => true, ); } /** * FILTER: ap_pre_editor_settings * Can be used to mody wp_editor settings * @var array * @since 2.0.1 */ $settings = apply_filters( 'ap_pre_editor_settings', $field['settings'] ); $this->output .= '
'; // Turn on the output buffer ob_start(); echo '
'; wp_editor( $field['value'], $field['name'], $settings ); echo '
'; $this->output .= ob_get_clean(); $this->error_messages(); if ( ! $this->field['show_desc_tip'] ) { $this->desc(); } $this->output .= '
'; } /** * 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->field['name'] ) && isset( $this->errors[$this->field['name']] ) ) { return true; } return false; } private function error_messages() { if ( isset( $this->errors[$this->field['name']] ) ) { $this->output .= '
'; foreach ( $this->errors[$this->field['name']] as $error ) { $this->output .= '

'. $error .'

'; } $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' : ''; if ( isset( $this->args['field_hidden'] ) && $this->args['field_hidden'] ) { if ( isset( $field['name'] ) && $field['type'] != 'hidden' && (@$field['visibility'] != 'me' || ( @$field['visibility'] == 'me' && $this->args['user_id'] == get_current_user_id())) ) { $nonce = wp_create_nonce( 'user_field_form_'.$field['name'].'_'.$this->args['user_id'] ); $this->output .= '
'; $this->output .= ''; if ( @$field['edit_disabled'] !== true && $this->args['user_id'] == get_current_user_id() ) { $this->output .= ''.__( 'Edit', 'anspress-question-answer' ).''; } $this->output .= '
'.@$field['value'].'
'; $this->output .= '
'; } } else { switch ( $field['type'] ) { case 'text': $this->output .= '
'; $this->text_field( $field, 'text' ); $this->output .= '
'; break; case 'password': $this->output .= '
'; $this->text_field( $field, 'password' ); $this->output .= '
'; break; case 'number': $this->output .= '
'; $this->number_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 'page_select': $this->output .= '
'; $this->page_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', 'anspress-question-answer' ); } $this->build(); return $this->output; } }