Exemplo n.º 1
0
 private function parseInputs($form_id, &$post_id = null, $preview = false, $force_form_count = false, $specific_post_id = null)
 {
     global $post, $_post_to_create, $_current_post;
     // reference to the form submission method
     global ${'_' . StaticClass::METHOD};
     $method =& ${'_' . StaticClass::METHOD};
     // get post inputs
     if (isset($post_id) && !empty($post_id) && null !== $post_id && false !== $post_id && !$preview) {
         $post_id = intval($post_id);
     } elseif (isset($post->ID) && !$preview) {
         $post_id = $post->ID;
     } else {
         $post_id = false;
     }
     $formHelper = $this->_formHelper;
     // get recaptcha settings
     StaticClass::$_staticGlobal['RECAPTCHA'] = $formHelper->getRecaptchaSettings(StaticClass::$_staticGlobal['RECAPTCHA']);
     $form = $this->_formData;
     $this->_post_ID = $post_id;
     // load form data
     //$this->_formData=$formHelper->loadForm($form_id, $preview);
     if ($formHelper->isError($form)) {
         return $form;
     }
     $form_id = $form->getForm()->ID;
     $_fields = $form->getFields();
     $form_type = $_fields['form_settings']->form['type'];
     $post_type = $_fields['form_settings']->post['post_type'];
     // if this is an edit form and no post id given
     if (('edit' == $form_type && false === $post_id && !$preview || isset($_GET['action']) && $_GET['action'] == 'edit_translation' && 'translation' == $form_type) && false === $post_id && !$preview) {
         return $formHelper->error(__('No post specified', 'wp-cred'));
     }
     // if this is a new form or preview
     if ('new' == $form_type || $preview || isset($_GET['action']) && $_GET['action'] == 'create_translation' && 'translation' == $form_type || $preview) {
         //if (isset($post_id)&&!empty($post_id)) $_post_to_create = $post_id;
         // always get new dummy id, to avoid the issue of editing the post on browser back button
         //$post_id=get_default_post_to_edit( $post_type, true )->ID;
         if (!isset($_post_to_create) || empty($_post_to_create)) {
             //Fix
             global $wpdb;
             $querystr = $wpdb->prepare("SELECT {$wpdb->posts}.ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_status = 'auto-draft' AND {$wpdb->posts}.post_type = %s ORDER by ID desc Limit 1", $post_type);
             $_myposts = $wpdb->get_results($querystr, OBJECT);
             //Fix https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/192489607/comments
             if (!empty($_myposts)) {
                 $mypost = get_post($_myposts[0]->ID);
                 $mypost->post_title = 'Auto Draft';
                 $mypost->post_content = '';
             } else {
                 //$post_id
                 //Fixed https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/192489607/comments
                 $mypost = get_default_post_to_edit($post_type, true);
                 //################################################################################################
             }
             $_post_to_create = $mypost->ID;
             $this->_post_ID = $_post_to_create;
             //Fix https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/193427659/comments#303699314
             //If post_id is not null and is not auto-draft means that the user used
             //back arrow of the browser
             //So i set post_id with a new one
             if ($post_id != null) {
                 $mycheckpost = get_post($post_id);
                 if ($mycheckpost->post_status != 'auto-draft') {
                     $post_id = $_post_to_create;
                 }
             }
             //#########################################################################
         } else {
             $this->_post_ID = $_post_to_create;
         }
     }
     // get existing post data if edit form and post given
     if (('edit' == $form_type && !$preview || isset($_GET['action']) && $_GET['action'] == 'edit_translation' && 'translation' == $form_type) && !$preview) {
         if ($_current_post == 0) {
             $_current_post = $post_id;
         }
         $is_current_edit = false;
         if (is_null($specific_post_id) && $_current_post > 0) {
             $is_current_edit = true;
             $post_id = $_current_post;
         }
         $this->_post_ID = $post_id;
         //$this->_postData=$formHelper->getPostData($post_id);
         require_once "PostData.php";
         $postdata = new PostData();
         $this->_postData = $postdata->getPostData($post_id);
         if ($is_current_edit) {
             $post_type = $this->_postData->post->post_type;
         }
         if ($formHelper->isError($this->_postData)) {
             return $this->_postData;
         }
         if (!$is_current_edit && $this->_postData->post->post_type != $post_type) {
             return $formHelper->error(__('Form type and post type do not match', 'wp-cred'));
         }
     } else {
         //Fixed https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/186563662/comments
         //conditional not working after failed submition
         //$this->_post_ID=$post_id;
         //removed creates side effect to other stuff !
     }
     // check if user has access to this form
     if (!$preview && !$formHelper->checkFormAccess($form_type, $form_id, $this->_postData)) {
         return $formHelper->error();
     }
     // Deprecated
     // set allowed file types
     StaticClass::$_staticGlobal['MIMES'] = $formHelper->getAllowedMimeTypes();
     // get custom post fields
     $fields_settings = $formHelper->getFieldSettings($post_type);
     // instantiate Zebra Form
     if (false !== $force_form_count) {
         $form_count = $force_form_count;
     } else {
         $form_count = StaticClass::$_globalStatic['COUNT'];
     }
     // strip any unneeded parsms from current uri
     $actionUri = $formHelper->currentURI(array('_tt' => time()), array('_success', '_success_message'));
     $prg_form_id = $formHelper->createPrgID($form_id, $form_count);
     $my_form_id = $formHelper->createFormID($form_id, $form_count);
     $this->_zebraForm = new CredForm($my_form_id, $form_type, $post_id, $actionUri, $preview);
     $this->_zebraForm->setLanguage(StaticClass::$_staticGlobal['LOCALES']);
     if ($formHelper->isError($this->_zebraForm)) {
         return $this->_zebraForm;
     }
     // all fine here
     $this->_postType = $post_type;
     $this->_content = $form->getForm()->post_content;
     $this->out_['fields'] = $fields_settings;
     $this->out_['count'] = $form_count;
     $this->out_['prg_id'] = $prg_form_id;
     return true;
 }