public function user_form($form_id, $post_id = null, $preview = false, $force_form_count = false, $specific_post_id = null) { $bypass_form = apply_filters('cred_bypass_process_form_' . $form_id, false, $form_id, $post_id, $preview); $bypass_form = apply_filters('cred_bypass_process_form', $bypass_form, $form_id, $post_id, $preview); require_once 'FormData.php'; $this->_formData = new FormData($form_id, CRED_USER_FORMS_CUSTOM_POST_NAME, $preview); $form =& $this->_formData; $formHelper = $this->_formHelper; // reference to the form submission method global ${'_' . StaticClass::METHOD}; $method =& ${'_' . StaticClass::METHOD}; // if some error happened, display a message instead $parse = $this->parseUserInputs($form_id, $post_id, $preview, $force_form_count, $specific_post_id); if ($formHelper->isError($parse)) { return $formHelper->getError($parse); } $zebraForm = $this->_zebraForm; $zebraForm->extra_parameters = $this->_formData->getExtra(); $form_id = $form->getForm()->ID; $_fields = $form->getFields(); $form_type = $_fields['form_settings']->form['type']; $user_role = $_fields['form_settings']->form['user_role']; if (empty($user_role)) { $user_role = array('subscriber'); } $prg_id = $this->out_['prg_id']; $form_count = $this->out_['count']; //$post_type=$form->fields['form_settings']->post['post_type']; $post_type = $this->_postType; //Removed this because we made it in parseInputs //Fixing: https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/190656261/comments#295538767 //$post_id=$this->_post_ID; // define global $post from $post_id global $post; if (is_int($post_id) && $post_id > 0) { if (!isset($post->ID) || isset($post->ID) && $post->ID != $post_id) { $post = get_post($post_id); } } //TODO: get user ??? // show display message from previous submit of same create form (P-R-G pattern) if (!$zebraForm->preview && isset($_GET['_success_message']) && $_GET['_success_message'] == $prg_id && 'message' == $_fields['form_settings']->form['action']) { $zebraForm->is_submit_success = true; return $formHelper->displayMessage($form); } else { $zebraForm->is_submit_success = $this->isSubmitted(); } $this->CRED_User_build(); // no message to display if not submitted $message = false; // add notification message from previous submit of same create form (P-R-G pattern) /* if (($n_data=$formHelper->readCookie('_cred_cred_notifications'.$prg_id))) { $formHelper->clearCookie('_cred_cred_notifications'.$prg_id); if (isset($n_data['sent'])) { foreach ((array)$n_data['sent'] as $ii) $zebraForm->add_form_message('notification_'.$ii, $formHelper->getLocalisedMessage('notification_was_sent')); } if (isset($n_data['failed'])) { foreach ((array)$n_data['failed'] as $ii) $zebraForm->add_form_message('notification_'.$ii, $formHelper->getLocalisedMessage('notification_failed')); } } */ $thisform = array('id' => $form_id, 'post_type' => $post_type, 'form_type' => $form_type); //Check dates foreach ($_POST as $name => &$value) { if ($name == StaticClass::NONCE) { continue; } if (is_array($value) && isset($value['datepicker'])) { if (!function_exists('adodb_date')) { require_once WPTOOLSET_FORMS_ABSPATH . '/lib/adodb-time.inc.php'; } $date_format = get_option('date_format'); $date = $value['datepicker']; $value['datetime'] = adodb_date("Y-m-d", $date); $value['hour'] = isset($value['hour']) ? $value['hour'] : "00"; $value['minute'] = isset($value['hour']) ? $value['minute'] : "00"; $value['timestamp'] = strtotime($value['datetime'] . " " . $value['hour'] . ":" . $value['minute'] . ":00"); } } //https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/196177636/comments#309966145 //Centralized the mime types $mime_types = wp_get_mime_types(); StaticClass::$_allowed_mime_types = array_merge($mime_types, array('xml' => 'text/xml')); StaticClass::$_allowed_mime_types = apply_filters('upload_mimes', StaticClass::$_allowed_mime_types); /** * sanitize input data */ if (!array_key_exists('post_fields', $this->out_['fields'])) { $this->out_['fields']['post_fields'] = array(); } //fixed Server side error messages should appear next to the field with the problem //https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/186243370/comments //https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/196177636/comments $formHelper->checkFilePost($zebraForm, $this->out_['fields']['post_fields']); if (isset($this->out_['fields']['post_fields']) && isset($this->out_['form_fields_info'])) { $formHelper->checkFilesType($this->out_['fields']['post_fields'], $this->out_['form_fields_info'], $zebraForm, $error_files); } //########################################################################################## //if (!$bypass_form && $_zebraForm->validate($post_id, $_zebraForm->form_properties['fields'])) if (!$bypass_form && $this->validate($error_files)) { if (!$zebraForm->preview) { // save post data $bypass_save_form_data = apply_filters('cred_bypass_save_data_' . $form_id, false, $form_id, $post_id, $thisform); $bypass_save_form_data = apply_filters('cred_bypass_save_data', $bypass_save_form_data, $form_id, $post_id, $thisform); if (!$bypass_save_form_data) { $model = CRED_Loader::get('MODEL/UserForms'); $attachedData = $model->getAttachedData($post_id); $user_id = $this->CRED_user_save($user_role, $post_id); } if (is_wp_error($user_id)) { $zebraForm->add_field_message($user_id->get_error_message(), 'User Name'); } else { if (is_int($user_id) && $user_id > 0) { // set global $post $post = get_post($post_id); // send notifications //list($n_sent, $n_failed)=$this->notify($post_id); // enable notifications and notification events if any $this->notify($user_id, $attachedData); unset($attachedData); // save results for later messages if PRG //$formHelper->setCookie('_cred_cred_notifications'.$prg_id, array('sent'=>$n_sent, 'failed'=>$n_failed)); // do custom action here // user can redirect, display messages, overwrite page etc.. $bypass_credaction = apply_filters('cred_bypass_credaction_' . $form_id, false, $form_id, $post_id, $thisform); $bypass_credaction = apply_filters('cred_bypass_credaction', $bypass_credaction, $form_id, $post_id, $thisform); //Emerson:->https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/185572863/comments /* Add cred_submit_complete_form_ hook in CRED 1.3 */ $form_slug = $form->getForm()->post_name; do_action('cred_submit_complete_form_' . $form_slug, $post_id, $thisform); do_action('cred_submit_complete_' . $form_id, $post_id, $thisform); do_action('cred_submit_complete', $post_id, $thisform); // no redirect url $url = false; // do success action if ($bypass_credaction) { $credaction = 'form'; } else { $credaction = $_fields['form_settings']->form['action']; } // do default or custom actions switch ($credaction) { case 'post': //$url = get_edit_user_link($user_id); // $formHelper->getLocalisedPermalink($post_id, $_fields['form_settings']->post['post_type']); //get_permalink($post_id); $url = get_author_posts_url($user_id); break; case 'page': $url = !empty($_fields['form_settings']->form['action_page']) ? $formHelper->getLocalisedPermalink($_fields['form_settings']->form['action_page'], 'page') : false; break; case 'message': case 'form': // custom 3rd-party action // custom 3rd-party action default: if ('form' != $credaction && 'message' != $credaction) { // add hooks here, to do custom action when custom cred action has been selected do_action('cred_custom_success_action_' . $form_id, $credaction, $post_id, $thisform); do_action('cred_custom_success_action', $credaction, $post_id, $thisform); } // if previous did not do anything, default to display form if ('form' != $credaction && 'message' != $credaction) { $credaction = 'form'; } // no redirect url $url = false; // PRG (POST-REDIRECT-GET) pattern, // to avoid resubmit on browser refresh issue, and also keep defaults on new form !! :) if ('message' == $credaction) { $url = $formHelper->currentURI(array('_tt' => time(), '_success_message' => $prg_id, '_target' => $post_id)); } else { $url = $formHelper->currentURI(array('_tt' => time(), '_success' => $prg_id)); } $url = $url . '#cred_form_' . $prg_id; // do PRG, redirect now $formHelper->redirect($url, array("HTTP/1.1 303 See Other")); exit; // just in case break; } // do redirect action here if (false !== $url) { if ('form' != $credaction && 'message' != $credaction) { $url = apply_filters('cred_success_redirect_form_' . $form_slug, $url, $post_id, $thisform); $url = apply_filters('cred_success_redirect_' . $form_id, $url, $post_id, $thisform); $url = apply_filters('cred_success_redirect', $url, $post_id, $thisform); } if (false !== $url) { $redirect_delay = $_fields['form_settings']->form['redirect_delay']; if ($redirect_delay <= 0) { $formHelper->redirect($url); } else { $formHelper->redirectDelayed($url, $redirect_delay); } } } $saved_message = $formHelper->getLocalisedMessage('post_saved'); $saved_message = apply_filters('cred_data_saved_message_' . $form_id, $saved_message, $form_id, $post_id, $preview); $saved_message = apply_filters('cred_data_saved_message', $saved_message, $form_id, $post_id, $preview); // add success message //$zebraForm->add_form_message('data-saved', $formHelper->getLocalisedMessage('post_saved')); $zebraForm->add_success_message($saved_message); } else { if (isset($_FILES) && count($_FILES) > 0) { // TODO check if this wp_list_pluck works with repetitive files... maybe in_array( array(1), $errors_on_files ) does the trick... $errors_on_files = $food_names = wp_list_pluck($_FILES, 'error'); if (in_array(1, $errors_on_files) || in_array(2, $errors_on_files)) { //$zebraForm->add_form_message('data-saved', $formHelper->getLocalisedMessage('no_data_submitted')); $zebraForm->add_field_message($formHelper->getLocalisedMessage('no_data_submitted')); } else { // else just show the form again, another error happening here //$zebraForm->add_form_message('data-saved', $formHelper->getLocalisedMessage('post_not_saved')); //$zebraForm->add_field_message($formHelper->getLocalisedMessage('post_not_saved')); // $form_name = $formHelper->createFormID($form_id, $form_count); // $field_messages = $zebraForm->field_messages[$form_name]; // if ( count($field_messages) == 1){ // $not_saved_message=$formHelper->getLocalisedMessage('post_not_saved_singular'); // }else{ // $not_saved_message=$formHelper->getLocalisedMessage('post_not_saved_plural'); // } // $error_list = '<ul>'; // foreach ($field_messages as $id_field=>$text) { // $error_list .= '<li>'. $text .'</li>'; // } // $error_list .= '</ul>'; // $not_saved_message = str_replace( array('%PROBLEMS_UL_LIST','%NN'), array($error_list, count($field_messages)), $not_saved_message); // $zebraForm->add_field_message($not_saved_message); } } else { // else just show the form again //$zebraForm->add_form_message('data-saved', $formHelper->getLocalisedMessage('post_not_saved')); $zebraForm->add_field_message($formHelper->getLocalisedMessage('post_not_saved')); } } } } else { //$zebraForm->add_form_message('preview-form',__('Preview Form submitted','wp-cred')); $zebraForm->add_field_message(__('Preview Form submitted', 'wp-cred')); } } else { if ($this->isSubmitted()) { $form_name = $formHelper->createFormID($form_id, $form_count); $top_messages = isset($zebraForm->top_messages[$form_name]) ? $zebraForm->top_messages[$form_name] : array(); if (empty($method)) { $not_saved_message = $formHelper->getLocalisedMessage('no_data_submitted'); } else { //$not_saved_message=$formHelper->getLocalisedMessage('post_not_saved'); // Replaced to new custom error message by Gen if (count($top_messages) == 1) { $tmpmsg = str_replace("<br />%PROBLEMS_UL_LIST", "", $formHelper->getLocalisedMessage('post_not_saved_singular')); $not_saved_message = $tmpmsg . "<br />%PROBLEMS_UL_LIST"; } else { $tmpmsg = str_replace("<br />%PROBLEMS_UL_LIST", "", $formHelper->getLocalisedMessage('post_not_saved_plural')); $not_saved_message = $tmpmsg . "<br />%PROBLEMS_UL_LIST"; } $error_list = '<ul>'; foreach ($top_messages as $id_field => $text) { $error_list .= '<li>' . $text . '</li>'; } $error_list .= '</ul>'; $not_saved_message = str_replace(array('%PROBLEMS_UL_LIST', '%NN'), array($error_list, count($top_messages)), $not_saved_message); } $not_saved_message = apply_filters('cred_data_not_saved_message_' . $form_id, $not_saved_message, $form_id, $post_id, $preview); $not_saved_message = apply_filters('cred_data_not_saved_message', $not_saved_message, $form_id, $post_id, $preview); //$zebraForm->add_form_message('data-saved', $not_saved_message); $zebraForm->add_field_message($not_saved_message); // if ( !empty( $zebraForm->form_errors ) ) { // foreach( $zebraForm->form_errors as $error_element_id => $error_message ) { // $zebraForm->add_form_message('data-saved', $error_message ); // } // } } else { if (isset($_GET['_success']) && $_GET['_success'] == $prg_id) { // add success message from previous submit of same any form (P-R-G pattern) $saved_message = $formHelper->getLocalisedMessage('post_saved'); $saved_message = apply_filters('cred_data_saved_message_' . $form_id, $saved_message, $form_id, $post_id, $preview); $saved_message = apply_filters('cred_data_saved_message', $saved_message, $form_id, $post_id, $preview); //$zebraForm->add_form_message('data-saved', $saved_message); $zebraForm->add_success_message($saved_message); } } } // $msgs = ""; // $msg_block = "data-saved"; // if (isset($zebraForm->form_messages[$msg_block])&&count($zebraForm->form_messages[$msg_block])>0) { // foreach ($zebraForm->form_messages[$msg_block] as $text) { // $msgs .= "<label class=\"wpt-form-error\">$text</label><div style='clear:both;'></div>"; // } // } $msgs = $zebraForm->getFieldsSuccessMessages(); $msgs .= $zebraForm->getFieldsErrorMessages(); $js = $zebraForm->getFieldsErrorMessagesJs(); if (false !== $message) { $output = $message; } else { $output = $this->CRED_render($msgs, $js); } return $output; }
public static function cred__add_custom_mime_types($mimes) { return StaticClass::$_allowed_mime_types = array_merge($mimes, StaticClass::$_allowed_mime_types); }
/** * Check if a file has a expected filetype * @param type $filetype * @param type $expected_filetypes * @return type */ private function is_correct_filetype($filename, $filetype, $expected_filetypes) { $filetypes = array(); $filetypes['audio'] = array('mp3|m4a|m4b' => 'audio/mpeg', 'ra|ram' => 'audio/x-realaudio', 'wav' => 'audio/wav', 'ogg|oga' => 'audio/ogg', 'mid|midi' => 'audio/midi', 'wma' => 'audio/x-ms-wma', 'wax' => 'audio/x-ms-wax', 'mka' => 'audio/x-matroska'); $filetypes['audio'] = apply_filters('audio_upload_mimes', $filetypes['audio']); $filetypes['video'] = array('asf|asx' => 'video/x-ms-asf', 'wmv' => 'video/x-ms-wmv', 'wmx' => 'video/x-ms-wmx', 'wm' => 'video/x-ms-wm', 'avi' => 'video/avi', 'divx' => 'video/divx', 'flv' => 'video/x-flv', 'mov|qt' => 'video/quicktime', 'mpeg|mpg|mpe' => 'video/mpeg', 'mp4|m4v' => 'video/mp4', 'ogv' => 'video/ogg', 'webm' => 'video/webm', 'mkv' => 'video/x-matroska', '3gp|3gpp' => 'video/3gpp', '3g2|3gp2' => 'video/3gpp2'); $filetypes['video'] = apply_filters('video_upload_mimes', $filetypes['video']); $filetypes['image'] = array('jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'tif|tiff' => 'image/tiff', 'ico' => 'image/x-icon'); $filetypes['image'] = apply_filters('image_upload_mimes', $filetypes['image']); $filetypes['file'] = array(); $filetypes['file'] = StaticClass::$_allowed_mime_types; $filetypes['file'] = apply_filters('file_upload_mimes', $filetypes['file']); StaticClass::$_allowed_mime_types = $filetypes['file']; add_filter('upload_mimes', array('StaticClass', 'cred__add_custom_mime_types')); $ret = wp_check_filetype($filename, StaticClass::$_allowed_mime_types); return !empty($ret['ext']); // $arr_filetypes_types = array_values($filetypes[$expected_filetypes]); // $arr_filetypes_exts = array_keys($filetypes[$expected_filetypes]); // // $arr = @explode("/", $filetype); // $isok = (isset($arr) && count($arr) >= 2) ? (in_array($arr[1], $arr_filetypes_exts) || in_array($filetype, $arr_filetypes_types)) : false; // return $isok; }