function update_profile()
 {
     check_ajax_referer('wpuf_form_add');
     @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
     $form_id = isset($_POST['form_id']) ? intval($_POST['form_id']) : 0;
     $form_vars = $this->get_input_fields($form_id);
     $form_settings = wpuf_get_form_settings($form_id);
     list($user_vars, $taxonomy_vars, $meta_vars) = $form_vars;
     $user_id = get_current_user_id();
     $userdata = array('ID' => $user_id);
     if ($this->search($user_vars, 'name', 'first_name')) {
         $userdata['first_name'] = $_POST['first_name'];
     }
     if ($this->search($user_vars, 'name', 'last_name')) {
         $userdata['last_name'] = $_POST['last_name'];
     }
     if ($this->search($user_vars, 'name', 'nickname')) {
         $userdata['nickname'] = $_POST['nickname'];
     }
     if ($this->search($user_vars, 'name', 'user_url')) {
         $userdata['user_url'] = $_POST['user_url'];
     }
     if ($this->search($user_vars, 'name', 'user_email')) {
         $userdata['user_email'] = $_POST['user_email'];
     }
     if ($this->search($user_vars, 'name', 'description')) {
         $userdata['description'] = $_POST['description'];
     }
     // check if password filled out
     // verify password
     if ($pass_element = $this->search($user_vars, 'name', 'password')) {
         $pass_element = current($pass_element);
         $password = $_POST['pass1'];
         $password_repeat = $_POST['pass2'];
         // check only if it's filled
         if ($pass_length = strlen($password)) {
             // min length check
             if ($pass_length < intval($pass_element['min_length'])) {
                 $this->send_error(sprintf(__('Password must be %s character long', 'wpuf'), $pass_element['min_length']));
             }
             // repeat password check
             if ($password != $password_repeat) {
                 $this->send_error(__('Password didn\'t match', 'wpuf'));
             }
             // seems like he want to change the password
             $userdata['user_pass'] = $password;
         }
     }
     $userdata = apply_filters('wpuf_update_profile_vars', $userdata, $form_id, $form_settings);
     $user_id = wp_update_user($userdata);
     if ($user_id) {
         // update meta fields
         $this->update_user_meta($meta_vars, $user_id);
         do_action('wpuf_update_profile', $user_id, $form_id, $form_settings);
     }
     //redirect URL
     $show_message = false;
     if ($form_settings['redirect_to'] == 'page') {
         $redirect_to = get_permalink($form_settings['page_id']);
     } elseif ($form_settings['redirect_to'] == 'url') {
         $redirect_to = $form_settings['url'];
     } elseif ($form_settings['redirect_to'] == 'same') {
         $redirect_to = get_permalink($_POST['page_id']);
         $redirect_to = add_query_arg(array('msg' => 'profile_update'), $redirect_to);
     }
     // send the response
     $response = array('success' => true, 'redirect_to' => $redirect_to, 'show_message' => $show_message, 'message' => $form_settings['update_message']);
     $response = apply_filters('wpuf_update_profile_resp', $response, $user_id, $form_id, $form_settings);
     wpuf_clear_buffer();
     echo json_encode($response);
     exit;
 }
 function draft_post()
 {
     check_ajax_referer('wpuf_form_add');
     @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
     $form_id = isset($_POST['form_id']) ? intval($_POST['form_id']) : 0;
     $form_vars = $this->get_input_fields($form_id);
     $form_settings = wpuf_get_form_settings($form_id);
     list($post_vars, $taxonomy_vars, $meta_vars) = $form_vars;
     // echo json_encode( $_POST );
     // print_r( $post_vars );
     // print_r( $taxonomy_vars );
     // print_r( $meta_vars );
     $postarr = array('post_type' => $form_settings['post_type'], 'post_status' => 'draft', 'post_author' => get_current_user_id(), 'post_title' => isset($_POST['post_title']) ? trim($_POST['post_title']) : '', 'post_content' => isset($_POST['post_content']) ? trim($_POST['post_content']) : '', 'post_excerpt' => isset($_POST['post_excerpt']) ? trim($_POST['post_excerpt']) : '');
     if (isset($_POST['category'])) {
         $category = $_POST['category'];
         $postarr['post_category'] = is_array($category) ? $category : array($category);
     }
     if (isset($_POST['tags'])) {
         $postarr['tags_input'] = explode(',', $_POST['tags']);
     }
     // if post_id is passed, we update the post
     if (isset($_POST['post_id'])) {
         $is_update = true;
         $postarr['ID'] = $_POST['post_id'];
         $postarr['comment_status'] = 'open';
     }
     $post_id = wp_insert_post($postarr);
     if ($post_id) {
         self::update_post_meta($meta_vars, $post_id);
         // set the post form_id for later usage
         update_post_meta($post_id, self::$config_id, $form_id);
         // save post formats if have any
         if (isset($form_settings['post_format']) && $form_settings['post_format'] != '0') {
             if (post_type_supports($form_settings['post_type'], 'post-formats')) {
                 set_post_format($post_id, $form_settings['post_format']);
             }
         }
         // save any custom taxonomies
         foreach ($taxonomy_vars as $taxonomy) {
             if (isset($_POST[$taxonomy['name']])) {
                 if (is_object_in_taxonomy($form_settings['post_type'], $taxonomy['name'])) {
                     $tax = $_POST[$taxonomy['name']];
                     // if it's not an array, make it one
                     if (!is_array($tax)) {
                         $tax = array($tax);
                     }
                     wp_set_post_terms($post_id, $_POST[$taxonomy['name']], $taxonomy['name']);
                 }
             }
         }
     }
     wpuf_clear_buffer();
     echo json_encode(array('post_id' => $post_id, 'action' => $_POST['action'], 'date' => current_time('mysql'), 'post_author' => get_current_user_id(), 'comment_status' => get_option('default_comment_status'), 'url' => add_query_arg('preview', 'true', get_permalink($post_id))));
     exit;
 }