/plugins/gravityforms/form_display.php
PHP | 2273 lines | 2022 code | 195 blank | 56 comment | 205 complexity | a191c52941c7534290aaeb132dcaa2d6 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- class GFFormDisplay{
- public static $submission = array();
- private static $init_scripts = array();
- const ON_PAGE_RENDER = 1;
- const ON_CONDITIONAL_LOGIC = 2;
- public static function process_form($form_id){
- //reading form metadata
- $form = RGFormsModel::get_form_meta($form_id);
- $form = RGFormsModel::add_default_properties($form);
- $lead = array();
- $field_values = RGForms::post("gform_field_values");
- $confirmation_message = "";
- $source_page_number = self::get_source_page($form_id);
- $page_number = $source_page_number;
- $target_page = self::get_target_page($form, $page_number, $field_values);
- //Loading files that have been uploaded to temp folder
- $files = GFCommon::json_decode(stripslashes(RGForms::post("gform_uploaded_files")));
- if(!is_array($files))
- $files = array();
- RGFormsModel::$uploaded_files[$form["id"]] = $files;
- $is_valid = true;
- //don't validate when going to previous page
- if(empty($target_page) || $target_page >= $page_number){
- $is_valid = self::validate($form, $field_values, $page_number);
- }
- //Upload files to temp folder when going to the next page or when submitting the form and it failed validation
- if( $target_page >= $page_number || ($target_page == 0 && !$is_valid) ){
- //Uploading files to temporary folder
- $files = self::upload_files($form, $files);
- RGFormsModel::$uploaded_files[$form["id"]] = $files;
- }
- // Load target page if it did not fail validation or if going to the previous page
- if($is_valid){
- $page_number = $target_page;
- }
- $confirmation = "";
- if($is_valid && $page_number == 0){
- $ajax = isset($_POST["gform_ajax"]);
- //adds honeypot field if configured
- if(rgar($form,"enableHoneypot"))
- $form["fields"][] = self::get_honeypot_field($form);
- $failed_honeypot = rgar($form,"enableHoneypot") && !self::validate_honeypot($form);
- if($failed_honeypot){
- //display confirmation but doesn't process the form when honeypot fails
- $confirmation = self::handle_confirmation($form, $lead, $ajax);
- $is_valid = false;
- }
- else{
- //pre submission action
- do_action("gform_pre_submission", $form);
- do_action("gform_pre_submission_{$form["id"]}", $form);
- //pre submission filter
- $form = apply_filters("gform_pre_submission_filter_{$form["id"]}", apply_filters("gform_pre_submission_filter", $form));
- //handle submission
- $confirmation = self::handle_submission($form, $lead, $ajax);
- //after submission hook
- do_action("gform_after_submission", $lead, $form);
- do_action("gform_after_submission_{$form["id"]}", $lead, $form);
- }
- if(is_array($confirmation) && isset($confirmation["redirect"])){
- header("Location: {$confirmation["redirect"]}");
- do_action("gform_post_submission", $lead, $form);
- do_action("gform_post_submission_{$form["id"]}", $lead, $form);
- exit;
- }
- }
- if(!isset(self::$submission[$form_id]))
- self::$submission[$form_id] = array();
- self::set_submission_if_null($form_id, "is_valid", $is_valid);
- self::set_submission_if_null($form_id, "form", $form);
- self::set_submission_if_null($form_id, "lead", $lead);
- self::set_submission_if_null($form_id, "confirmation_message", $confirmation);
- self::set_submission_if_null($form_id, "page_number", $page_number);
- self::set_submission_if_null($form_id, "source_page_number", $source_page_number);
- }
- private static function set_submission_if_null($form_id, $key, $val){
- if(!isset(self::$submission[$form_id][$key]))
- self::$submission[$form_id][$key] = $val;
- }
- private static function upload_files($form, $files){
- //Creating temp folder if it does not exist
- $target_path = RGFormsModel::get_upload_path($form["id"]) . "/tmp/";
- wp_mkdir_p($target_path);
- foreach($form["fields"] as $field){
- $input_name = "input_{$field["id"]}";
- //skip fields that are not file upload fields or that don't have a file to be uploaded or that have failed validation
- $input_type = RGFormsModel::get_input_type($field);
- if(!in_array($input_type, array("fileupload", "post_image")) || $field["failed_validation"] || empty($_FILES[$input_name]["name"])){
- continue;
- }
- $file_info = RGFormsModel::get_temp_filename($form["id"], $input_name);
- if($file_info && move_uploaded_file($_FILES[$input_name]['tmp_name'], $target_path . $file_info["temp_filename"])){
- $files[$input_name] = $file_info["uploaded_filename"];
- }
- }
- return $files;
- }
- public static function get_state($form, $field_values){
- $product_fields = array();
- foreach($form["fields"] as $field){
- if(GFCommon::is_product_field($field["type"]) || $field["type"] == "donation"){
- $value = RGFormsModel::get_field_value($field, $field_values, false);
- $value = self::default_if_empty($field, $value);
- switch($field["inputType"]){
- case "calculation" :
- case "singleproduct" :
- case "hiddenproduct" :
- $price = !is_array($value) || empty($value[$field["id"] . ".2"]) ? $field["basePrice"] : $value[$field["id"] . ".2"];
- if(empty($price))
- $price = 0;
- $price = GFCommon::to_number($price);
- $product_name = !is_array($value) || empty($value[$field["id"] . ".1"]) ? $field["label"] : $value[$field["id"] . ".1"];
- $product_fields[$field["id"]. ".1"] = wp_hash($product_name);
- $product_fields[$field["id"]. ".2"] = wp_hash($price);
- break;
- case "singleshipping" :
- $price = !empty($value) ? $value : $field["basePrice"];
- $price = GFCommon::to_number($price);
- $product_fields[$field["id"]] = wp_hash($price);
- break;
- case "radio" :
- case "select" :
- $product_fields[$field["id"]] = array();
- foreach($field["choices"] as $choice){
- $field_value = !empty($choice["value"]) || rgar($field,"enableChoiceValue") ? $choice["value"] : $choice["text"];
- if($field["enablePrice"])
- $field_value .= "|" . GFCommon::to_number(rgar($choice,"price"));
- $product_fields[$field["id"]][] = wp_hash($field_value);
- }
- break;
- case "checkbox" :
- $index = 1;
- foreach($field["choices"] as $choice){
- $field_value = !empty($choice["value"]) || $field["enableChoiceValue"] ? $choice["value"] : $choice["text"];
- if($field["enablePrice"])
- $field_value .= "|" . GFCommon::to_number($choice["price"]);
- if($index % 10 == 0) //hack to skip numbers ending in 0. so that 5.1 doesn't conflict with 5.10
- $index++;
- $product_fields[$field["id"] . "." . $index++] = wp_hash($field_value);
- }
- break;
- }
- }
- }
- $hash = serialize($product_fields);
- $checksum = wp_hash(crc32($hash));
- return base64_encode(serialize(array($hash, $checksum)));
- }
- private static function has_pages($form){
- return GFCommon::has_pages($form);
- }
- private static function has_character_counter($form){
- if(!is_array($form["fields"]))
- return false;
- foreach($form["fields"] as $field){
- if(rgar($field, "maxLength") && !rgar($field, "inputMask"))
- return true;
- }
- return false;
- }
- private static function has_enhanced_dropdown($form){
- if(!is_array($form["fields"]))
- return false;
- foreach($form["fields"] as $field){
- if(in_array(RGFormsModel::get_input_type($field), array("select", "multiselect")) && rgar($field, "enableEnhancedUI"))
- return true;
- }
- return false;
- }
- private static function has_password_strength($form){
- if(!is_array($form["fields"]))
- return false;
- foreach($form["fields"] as $field){
- if($field["type"] == "password" && RGForms::get("passwordStrengthEnabled", $field))
- return true;
- }
- return false;
- }
- private static function has_other_choice($form){
- if(!is_array($form["fields"]))
- return false;
- foreach($form["fields"] as $field){
- if($field["type"] == "radio" && rgget("enableOtherChoice", $field))
- return true;
- }
- return false;
- }
- public static function get_target_page($form, $current_page, $field_values){
- $page_number = RGForms::post("gform_target_page_number_{$form["id"]}");
- $page_number = !is_numeric($page_number) ? 1 : $page_number;
- $direction = $page_number >= $current_page ? 1 : -1;
- //Finding next page that is not hidden by conditional logic
- while(RGFormsModel::is_page_hidden($form, $page_number, $field_values)){
- $page_number += $direction;
- }
- //If all following pages are hidden, submit the form
- if($page_number > self::get_max_page_number($form))
- $page_number = 0;
- return $page_number;
- }
- public static function get_source_page($form_id){
- $page_number = RGForms::post("gform_source_page_number_{$form_id}");
- return !is_numeric($page_number) ? 1 : $page_number;
- }
- public static function set_current_page($form_id, $page_number){
- self::$submission[$form_id]["page_number"] = $page_number;
- }
- public static function get_current_page($form_id){
- $page_number = isset(self::$submission[$form_id]) ? self::$submission[$form_id]["page_number"] : 1;
- return $page_number;
- }
- private static function is_page_active($form_id, $page_number){
- return intval(self::get_current_page($form_id)) == intval($page_number);
- }
- private static function get_limit_period_dates($period){
- if(empty($period))
- return array("start_date" => null, "end_date" => null);
- switch($period){
- case "day" :
- return array(
- "start_date" => gmdate("Y-m-d"),
- "end_date" => gmdate("Y-m-d"));
- break;
- case "week" :
- return array(
- "start_date" => gmdate("Y-m-d", strtotime("last Monday")),
- "end_date" => gmdate("Y-m-d", strtotime("next Sunday")));
- break;
- case "month" :
- $month_start = gmdate("Y-m-1");
- return array(
- "start_date" => $month_start,
- "end_date" => gmdate("Y-m-d", strtotime("{$month_start} +1 month - 1 hour")));
- break;
- case "year" :
- return array(
- "start_date" => gmdate("Y-1-1"),
- "end_date" => gmdate("Y-12-31"));
- break;
- }
- }
- public static function get_form($form_id, $display_title=true, $display_description=true, $force_display=false, $field_values=null, $ajax=false, $tabindex = 1){
- //looking up form id by form name
- if(!is_numeric($form_id))
- $form_id = RGFormsModel::get_form_id($form_id);
- //reading form metadata
- $form = RGFormsModel::get_form_meta($form_id, true);
- $form = RGFormsModel::add_default_properties($form);
- //disable ajax if form has a reCAPTCHA field (not supported).
- if($ajax && self::has_recaptcha_field($form))
- $ajax = false;
- $is_postback = false;
- $is_valid = true;
- $confirmation_message = "";
- $page_number = 1;
- //If form was submitted, read variables set during form submission procedure
- $submission_info = isset(self::$submission[$form_id]) ? self::$submission[$form_id] : false;
- if($submission_info){
- $is_postback = true;
- $is_valid = rgar($submission_info, "is_valid") || rgar($submission_info, "is_confirmation");
- $form = $submission_info["form"];
- $lead = $submission_info["lead"];
- $confirmation_message = rgget("confirmation_message", $submission_info);
- if($is_valid && !RGForms::get("is_confirmation", $submission_info)){
- if($submission_info["page_number"] == 0){
- //post submission hook
- do_action("gform_post_submission", $lead, $form);
- do_action("gform_post_submission_{$form["id"]}", $lead, $form);
- }
- else{
- //change page hook
- do_action("gform_post_paging", $form, $submission_info["source_page_number"], $submission_info["page_number"]);
- do_action("gform_post_paging_{$form["id"]}", $form, $submission_info["source_page_number"], $submission_info["page_number"]);
- }
- }
- }
- else if(!current_user_can("administrator")){
- RGFormsModel::insert_form_view($form_id, $_SERVER['REMOTE_ADDR']);
- }
- if(rgar($form,"enableHoneypot"))
- $form["fields"][] = self::get_honeypot_field($form);
- //Fired right before the form rendering process. Allow users to manipulate the form object before it gets displayed in the front end
- $form = apply_filters("gform_pre_render_$form_id", apply_filters("gform_pre_render", $form, $ajax), $ajax);
- if($form == null)
- return "<p>" . __("Oops! We could not locate your form.", "gravityforms") . "</p>";
- $has_pages = self::has_pages($form);
- //calling tab index filter
- GFCommon::$tab_index = apply_filters("gform_tabindex_{$form_id}", apply_filters("gform_tabindex", $tabindex, $form), $form);
- //Don't display inactive forms
- if(!$force_display && !$is_postback) {
- $form_info = RGFormsModel::get_form($form_id);
- if(!$form_info->is_active)
- return "";
- // If form requires login, check if user is logged in
- if(rgar($form, "requireLogin")) {
- if(!is_user_logged_in())
- return empty($form["requireLoginMessage"]) ? "<p>" . __("Sorry. You must be logged in to view this form.", "gravityforms"). "</p>" : "<p>" . GFCommon::gform_do_shortcode($form["requireLoginMessage"]) . "</p>";
- }
- }
- // show the form regardless of the following validations when force display is set to true
- if(!$force_display) {
- $form_schedule_validation = self::validate_form_schedule($form);
- // if form schedule validation fails AND this is not a postback, display the validation error
- // if form schedule validation fails AND this is a postback, make sure is not a valid submission (enables display of confirmation message)
- if( ($form_schedule_validation && !$is_postback) || ($form_schedule_validation && $is_postback && !$is_valid) )
- return $form_schedule_validation;
- $entry_limit_validation = self::validate_entry_limit($form);
- // refer to form schedule condition notes above
- if( ($entry_limit_validation && !$is_postback) || ($entry_limit_validation && $is_postback && !$is_valid) )
- return $entry_limit_validation;
- }
- $form_string = "";
- //When called via a template, this will enqueue the proper scripts
- //When called via a shortcode, this will be ignored (too late to enqueue), but the scripts will be enqueued via the enqueue_scripts event
- self::enqueue_form_scripts($form, $ajax);
- if(empty($confirmation_message)){
- $wrapper_css_class = GFCommon::get_browser_class() . " gform_wrapper";
- if(!$is_valid)
- $wrapper_css_class .=" gform_validation_error";
- //Hidding entire form if conditional logic is on to prevent "hidden" fields from blinking. Form will be set to visible in the conditional_logic.php after the rules have been applied.
- $style = self::has_conditional_logic($form) ? "style='display:none'" : "";
- $custom_wrapper_css_class = !empty($form["cssClass"]) ? " {$form["cssClass"]}_wrapper": "";
- $form_string .= "
- <div class='{$wrapper_css_class}{$custom_wrapper_css_class}' id='gform_wrapper_$form_id' " . $style . ">";
- $action = add_query_arg(array());
- $default_anchor = $has_pages || $ajax ? true : false;
- $use_anchor = apply_filters("gform_confirmation_anchor_{$form["id"]}", apply_filters("gform_confirmation_anchor", $default_anchor));
- if($use_anchor !== false){
- $form_string .="<a id='gf_$form_id' name='gf_$form_id' class='gform_anchor' ></a>";
- $action .= "#gf_$form_id";
- }
- $target = $ajax ? "target='gform_ajax_frame_{$form_id}'" : "";
- $form_css_class = !empty($form["cssClass"]) ? "class='{$form["cssClass"]}'": "";
- $action = esc_attr($action);
- $form_string .= apply_filters("gform_form_tag_{$form_id}", apply_filters("gform_form_tag", "<form method='post' enctype='multipart/form-data' {$target} id='gform_{$form_id}' {$form_css_class} action='{$action}'>", $form), $form);
- if($display_title || $display_description){
- $form_string .= "
- <div class='gform_heading'>";
- if($display_title){
- $form_string .= "
- <h3 class='gform_title'>" . $form['title'] . "</h3>";
- }
- if($display_description){
- $form_string .= "
- <span class='gform_description'>" . rgar($form,'description') ."</span>";
- }
- $form_string .= "
- </div>";
- }
- $current_page = self::get_current_page($form_id);
- if($has_pages && !IS_ADMIN){
- $page_count = self::get_max_page_number($form);
- if($form["pagination"]["type"] == "percentage"){
- $form_string .= self::get_progress_bar($form, $form_id,$confirmation_message);
- }
- else if($form["pagination"]["type"] == "steps"){
- $form_string .="
- <div id='gf_page_steps_{$form_id}' class='gf_page_steps'>";
- for($i=0, $count = sizeof($form["pagination"]["pages"]); $i<$count; $i++){
- $step_number = $i+1;
- $active_class = $step_number == $current_page ? " gf_step_active" : "";
- $first_class = $i==0 ? " gf_step_first" : "";
- $last_class = $i+1 == $count ? " gf_step_last" : "";
- $complete_class = $step_number < $current_page ? " gf_step_completed" : "";
- $previous_class = $step_number + 1 == $current_page ? " gf_step_previous" : "";
- $next_class = $step_number - 1 == $current_page ? " gf_step_next" : "";
- $pending_class = $step_number > $current_page ? " gf_step_pending" : "";
- $classes = "gf_step" . $active_class . $first_class . $last_class . $complete_class . $previous_class . $next_class . $pending_class;
- $classes = GFCommon::trim_all($classes);
- $form_string .="
- <div id='gf_step_{$form_id}_{$step_number}' class='{$classes}'><span class='gf_step_number'>{$step_number}</span> {$form["pagination"]["pages"][$i]}</div>";
- }
- $form_string .="
- <div class='gf_step_clear'></div>
- </div>";
- }
- }
- if($is_postback && !$is_valid){
- $validation_message = "<div class='validation_error'>" . __("There was a problem with your submission.", "gravityforms") . " " . __("Errors have been highlighted below.", "gravityforms") . "</div>";
- $form_string .= apply_filters("gform_validation_message_{$form["id"]}", apply_filters("gform_validation_message", $validation_message, $form), $form);
- }
- $form_string .= "
- <div class='gform_body'>";
- //add first page if this form has any page fields
- if($has_pages){
- $style = self::is_page_active($form_id, 1) ? "" : "style='display:none;'";
- $class = !empty($form["firstPageCssClass"]) ? " {$form["firstPageCssClass"]}" : "";
- $form_string .= "<div id='gform_page_{$form_id}_1' class='gform_page{$class}' {$style}>
- <div class='gform_page_fields'>";
- }
- $description_class = rgar($form,"descriptionPlacement") == "above" ? "description_above" : "description_below";
- $form_string .= "
- <ul id='gform_fields_$form_id' class='gform_fields {$form['labelPlacement']} {$description_class}'>";
- if(is_array($form['fields']))
- {
- foreach($form['fields'] as $field){
- $field["conditionalLogicFields"] = self::get_conditional_logic_fields($form, $field["id"]);
- $form_string .= self::get_field($field, RGFormsModel::get_field_value($field, $field_values), false, $form, $field_values);
- }
- }
- $form_string .= "
- </ul>";
- if($has_pages){
- $previous_button = self::get_form_button($form["id"], "gform_previous_button_{$form["id"]}", $form["lastPageButton"], __("Previous", "gravityforms"), "button gform_previous_button", __("Previous Page", "gravityforms"), self::get_current_page($form_id) -1);
- $form_string .= "</div>" . self::gform_footer($form, "gform_page_footer " . $form['labelPlacement'], $ajax, $field_values, $previous_button, $display_title, $display_description, $is_postback) . "
- </div>"; //closes gform_page
- }
- $form_string .= "</div>"; //closes gform_body
- //suppress form footer for multi-page forms (footer will be included on the last page
- if(!$has_pages)
- $form_string .= self::gform_footer($form, "gform_footer " . $form['labelPlacement'], $ajax, $field_values, "", $display_title, $display_description, $is_postback);
- $form_string .= "
- </form>
- </div>";
- if($ajax && $is_postback){
- global $wp_scripts;
- $form_string = "<!DOCTYPE html><html><head>" .
- "<meta charset='UTF-8' /></head><body class='GF_AJAX_POSTBACK'>" . $form_string . "</body></html>";
- }
- if($ajax && !$is_postback){
- $spinner_url = apply_filters("gform_ajax_spinner_url_{$form_id}", apply_filters("gform_ajax_spinner_url", GFCommon::get_base_url() . "/images/spinner.gif", $form), $form);
- $scroll_position = array('default' => '', 'confirmation' => '');
- if($use_anchor !== false) {
- $scroll_position['default'] = is_numeric($use_anchor) ? "jQuery(document).scrollTop(" . intval($use_anchor) . ");" : "jQuery(document).scrollTop(jQuery('#gform_wrapper_{$form_id}').offset().top);";
- $scroll_position['confirmation'] = is_numeric($use_anchor) ? "jQuery(document).scrollTop(" . intval($use_anchor) . ");" : "jQuery(document).scrollTop(jQuery('#gforms_confirmation_message').offset().top);";
- }
- $form_string .="
- <iframe style='display:none;width:0px; height:0px;' src='about:blank' name='gform_ajax_frame_{$form_id}' id='gform_ajax_frame_{$form_id}'></iframe>
- <script type='text/javascript'>" . apply_filters("gform_cdata_open", "") . "" .
- "function gformInitSpinner_{$form_id}(){" .
- "jQuery('#gform_{$form_id}').submit(function(){" .
- "jQuery('#gform_submit_button_{$form_id}').attr('disabled', true).after('<' + 'img id=\"gform_ajax_spinner_{$form_id}\" class=\"gform_ajax_spinner\" src=\"{$spinner_url}\" alt=\"\" />');" .
- "jQuery('#gform_wrapper_{$form_id} .gform_previous_button').attr('disabled', true); " .
- "jQuery('#gform_wrapper_{$form_id} .gform_next_button').attr('disabled', true).after('<' + 'img id=\"gform_ajax_spinner_{$form_id}\" class=\"gform_ajax_spinner\" src=\"{$spinner_url}\" alt=\"\" />');" .
- "});" .
- "}" .
- "jQuery(document).ready(function($){" .
- "gformInitSpinner_{$form_id}();" .
- "jQuery('#gform_ajax_frame_{$form_id}').load( function(){" .
- "var contents = jQuery(this).contents().find('*').html();" .
- "var is_postback = contents.indexOf('GF_AJAX_POSTBACK') >= 0;" .
- "if(!is_postback){return;}" .
- "var form_content = jQuery(this).contents().find('#gform_wrapper_{$form_id}');" .
- "var is_redirect = contents.indexOf('gformRedirect(){') >= 0;".
- "jQuery('#gform_submit_button_{$form_id}').removeAttr('disabled');" .
- "if(form_content.length > 0){" .
- "jQuery('#gform_wrapper_{$form_id}').html(form_content.html());" .
- "{$scroll_position['default']}" .
- "if(window['gformInitDatepicker']) {gformInitDatepicker();}" .
- "if(window['gformInitPriceFields']) {gformInitPriceFields();}" .
- "var current_page = jQuery('#gform_source_page_number_{$form_id}').val();".
- "gformInitSpinner_{$form_id}();" .
- "jQuery(document).trigger('gform_page_loaded', [{$form_id}, current_page]);" .
- "}" .
- "else if(!is_redirect){" .
- "var confirmation_content = jQuery(this).contents().find('#gforms_confirmation_message').html();" .
- "if(!confirmation_content){".
- "confirmation_content = contents;".
- "}" .
- "setTimeout(function(){" .
- "jQuery('#gform_wrapper_{$form_id}').replaceWith('<' + 'div id=\'gforms_confirmation_message\' class=\'gform_confirmation_message_{$form_id}\'' + '>' + confirmation_content + '<' + '/div' + '>');" .
- "{$scroll_position['confirmation']}" .
- "jQuery(document).trigger('gform_confirmation_loaded', [{$form_id}]);" .
- "}, 50);" .
- "}" .
- "else{" .
- "jQuery('#gform_{$form_id}').append(contents);" .
- "if(window['gformRedirect']) gformRedirect();" .
- "}" .
- "jQuery(document).trigger('gform_post_render', [{$form_id}, current_page]);" .
- "});" .
- "});" . apply_filters("gform_cdata_close", "") . "</script>";
- }
- $is_first_load = !$is_postback;
- if(!$ajax || $is_first_load) {
- $form_string .= self::get_form_init_scripts($form);
- $form_string .= "<script type='text/javascript'>" . apply_filters("gform_cdata_open", "") . " jQuery(document).ready(function(){jQuery(document).trigger('gform_post_render', [{$form_id}, {$current_page}])}); " . apply_filters("gform_cdata_close", "") . "</script>";
- }
- return apply_filters('gform_get_form_filter',$form_string);
- }
- else{
- $progress_confirmation = "";
- //check admin setting for whether the progress bar should start at zero
- $start_at_zero = rgars($form, "pagination/display_progressbar_on_confirmation");
- //check for filter
- $start_at_zero = apply_filters("gform_progressbar_start_at_zero", $start_at_zero, $form);
- //show progress bar on confirmation
- if($start_at_zero && $has_pages && !IS_ADMIN && ($form["confirmation"]["type"] == "message" && $form["pagination"]["type"] == "percentage") && $form["pagination"]["display_progressbar_on_confirmation"])
- {
- $progress_confirmation = self::get_progress_bar($form, $form_id,$confirmation_message);
- if($ajax)
- {
- $progress_confirmation = "<!DOCTYPE html><html><head><meta charset='UTF-8' /></head><body class='GF_AJAX_POSTBACK'>" . $progress_confirmation . $confirmation_message . "</body></html>";
- }
- else
- {
- $progress_confirmation = $progress_confirmation . $confirmation_message;
- }
- }
- else
- {
- //return regular confirmation message
- if($ajax)
- {
- $progress_confirmation = "<!DOCTYPE html><html><head><meta charset='UTF-8' /></head><body class='GF_AJAX_POSTBACK'>" . $confirmation_message . "</body></html>";
- }
- else
- {
- $progress_confirmation = $confirmation_message;
- }
- }
- return $progress_confirmation;
- }
- }
- public static function add_init_script($form_id, $script_name, $location, $script){
- $key = $script_name . "_" . $location;
- if(!isset(self::$init_scripts[$form_id]))
- self::$init_scripts[$form_id] = array();
- //add script if it hasn't been added before
- if(!array_key_exists($key, self::$init_scripts[$form_id]))
- self::$init_scripts[$form_id][$key] = array("location" => $location, "script" => $script);
- }
- private static function get_form_button($form_id, $button_input_id, $button, $default_text, $class, $alt, $target_page_number){
- $tabindex = GFCommon::get_tabindex();
- $input_type='submit';
- $onclick="";
- if(!empty($target_page_number)){
- $onclick = "onclick='jQuery(\"#gform_target_page_number_{$form_id}\").val(\"{$target_page_number}\"); jQuery(\"#gform_{$form_id}\").trigger(\"submit\",[true]); '";
- $input_type='button';
- }
- if($button["type"] == "text" || empty($button["imageUrl"])){
- $button_text = !empty($button["text"]) ? $button["text"] : $default_text;
- $button_input = "<input type='{$input_type}' id='{$button_input_id}' class='{$class}' value='" . esc_attr($button_text) . "' {$tabindex} {$onclick}/>";
- }
- else{
- $imageUrl = $button["imageUrl"];
- $button_input= "<input type='image' src='{$imageUrl}' id='{$button_input_id}' class='gform_image_button' alt='{$alt}' {$tabindex} {$onclick}/>";
- }
- return $button_input;
- }
- private static function gform_footer($form, $class, $ajax, $field_values, $previous_button, $display_title, $display_description){
- $form_id = $form["id"];
- $footer = "
- <div class='" . $class ."'>";
- $button_input = self::get_form_button($form["id"], "gform_submit_button_{$form["id"]}", $form["button"], __("Submit", "gravityforms"), "button gform_button", __("Submit", "gravityforms"), 0);
- $button_input = apply_filters("gform_submit_button", $button_input, $form);
- $button_input = apply_filters("gform_submit_button_{$form_id}", $button_input, $form);
- $footer .= $previous_button . " " . $button_input;
- if($ajax){
- $footer .= "<input type='hidden' name='gform_ajax' value='" . esc_attr("form_id={$form_id}&title={$display_title}&description={$display_description}") . "' />";
- }
- $current_page = self::get_current_page($form_id);
- $next_page = $current_page + 1;
- $next_page = $next_page > self::get_max_page_number($form) ? 0 : $next_page;
- $field_values_str = is_array($field_values) ? http_build_query($field_values) : "";
- $files_input = "";
- if(!empty(RGFormsModel::$uploaded_files[$form_id])){
- $files = GFCommon::json_encode(RGFormsModel::$uploaded_files[$form_id]);
- $files_input = "<input type='hidden' name='gform_uploaded_files' id='gform_uploaded_files_{$form_id}' value='" . str_replace("'", "'", $files) . "' />";
- }
- $footer .="
- <input type='hidden' class='gform_hidden' name='is_submit_{$form_id}' value='1' />
- <input type='hidden' class='gform_hidden' name='gform_submit' value='{$form_id}' />
- <input type='hidden' class='gform_hidden' name='gform_unique_id' value='" . esc_attr(RGFormsModel::get_form_unique_id($form_id)) . "' />
- <input type='hidden' class='gform_hidden' name='state_{$form_id}' value='" . self::get_state($form, $field_values) . "' />
- <input type='hidden' class='gform_hidden' name='gform_target_page_number_{$form_id}' id='gform_target_page_number_{$form_id}' value='" . $next_page . "' />
- <input type='hidden' class='gform_hidden' name='gform_source_page_number_{$form_id}' id='gform_source_page_number_{$form_id}' value='" . $current_page . "' />
- <input type='hidden' name='gform_field_values' value='{$field_values_str}' />
- {$files_input}
- </div>";
- return $footer;
- }
- private static function get_max_page_number($form){
- $page_number = 0;
- foreach($form["fields"] as $field){
- if($field["type"] == "page"){
- $page_number++;
- }
- }
- return $page_number == 0 ? 0 : $page_number + 1;
- }
- private static function get_honeypot_field($form){
- $max_id = self::get_max_field_id($form);
- $labels = self::get_honeypot_labels();
- return array("type" => "honeypot", "label" => $labels[rand(0, 3)], "id" => $max_id + 1, "cssClass" => "gform_validation_container", "description" => "This field is for validation purposes and should be left unchanged.");
- }
- private static function get_max_field_id($form){
- $max = 0;
- foreach($form["fields"] as $field){
- if(floatval($field["id"]) > $max)
- $max = floatval($field["id"]);
- }
- return $max;
- }
- private static function get_honeypot_labels(){
- return array("Name", "Email", "Phone", "Comments");
- }
- public static function is_empty($field, $form_id=0){
- switch(RGFormsModel::get_input_type($field)){
- case "post_image" :
- case "fileupload" :
- $input_name = "input_" . $field["id"];
- $file_info = RGFormsModel::get_temp_filename($form_id, $input_name);
- return !$file_info && empty($_FILES[$input_name]['name']);
- case "list" :
- $value = rgpost("input_" . $field["id"]);
- if(is_array($value)){
- //empty if all inputs are empty (for inputs with the same name)
- foreach($value as $input){
- if(strlen(trim($input)) > 0 )
- return false;
- }
- }
- return true;
- }
- if(is_array($field["inputs"]))
- {
- foreach($field["inputs"] as $input){
- $value = rgpost("input_" . str_replace('.', '_', $input["id"]));
- if(is_array($value) && !empty($value)){
- return false;
- }
- if(!is_array($value) && strlen(trim($value)) > 0)
- return false;
- }
- return true;
- }
- else{
- $value = rgpost("input_" . $field["id"]);
- if(is_array($value)){
- //empty if any of the inputs are empty (for inputs with the same name)
- foreach($value as $input){
- if(strlen(trim($input)) <= 0 )
- return true;
- }
- return false;
- }
- else if($field["enablePrice"]){
- list($label, $price) = explode("|", $value);
- $is_empty = (strlen(trim($price)) <= 0);
- return $is_empty;
- }
- else{
- $is_empty = (strlen(trim($value)) <= 0) || ($field["type"] == "post_category" && $value < 0);
- return $is_empty;
- }
- }
- }
- private static function clean_extensions($extensions){
- $count = sizeof($extensions);
- for($i=0; $i<$count; $i++){
- $extensions[$i] = str_replace(".", "",str_replace(" ", "", $extensions[$i]));
- }
- return $extensions;
- }
- private static function validate_range($field, $value){
- if( !GFCommon::is_numeric($value, rgar($field, "numberFormat")) )
- return false;
- $number = GFCommon::clean_number($value, rgar($field, "numberFormat"));
- if( (is_numeric($field["rangeMin"]) && $number < $field["rangeMin"]) ||
- (is_numeric($field["rangeMax"]) && $number > $field["rangeMax"])
- )
- return false;
- else
- return true;
- }
- private static function validate_honeypot($form){
- $honeypot_id = self::get_max_field_id($form);
- return rgempty("input_{$honeypot_id}");
- }
- public static function handle_submission($form, &$lead, $ajax=false){
- //creating entry in DB
- RGFormsModel::save_lead($form, $lead);
- //reading entry that was just saved
- $lead = RGFormsModel::get_lead($lead["id"]);
- do_action('gform_entry_created', $lead, $form);
- //if Akismet plugin is installed, run lead through Akismet and mark it as Spam when appropriate
- $is_spam = false;
- if(GFCommon::akismet_enabled($form['id']) && GFCommon::is_akismet_spam($form, $lead)){
- $is_spam = true;
- }
- if(!$is_spam){
- GFCommon::create_post($form, $lead);
- //send auto-responder and notification emails
- self::send_emails($form, $lead);
- }
- else{
- //marking entry as spam
- RGFormsModel::update_lead_property($lead["id"], "status", "spam", false, true);
- $lead["status"] = "spam";
- }
- //display confirmation message or redirect to confirmation page
- return self::handle_confirmation($form, $lead, $ajax);
- }
- public static function handle_confirmation($form, $lead, $ajax=false){
- if($form["confirmation"]["type"] == "message"){
- $default_anchor = self::has_pages($form) ? 1 : 0;
- $anchor = apply_filters("gform_confirmation_anchor_{$form["id"]}", apply_filters("gform_confirmation_anchor", $default_anchor)) ? "<a id='gf_{$form["id"]}' name='gf_{$form["id"]}' class='gform_anchor' ></a>" : "";
- $nl2br = rgar($form["confirmation"],"disableAutoformat") ? false : true;
- $confirmation = empty($form["confirmation"]["message"]) ? "{$anchor} " : "{$anchor}<div id='gforms_confirmation_message' class='gform_confirmation_message_{$form["id"]}'>" . GFCommon::replace_variables($form["confirmation"]["message"], $form, $lead, false, true, $nl2br) . "</div>";
- }
- else{
- if(!empty($form["confirmation"]["pageId"])){
- $url = get_permalink($form["confirmation"]["pageId"]);
- }
- else{
- $url = GFCommon::replace_variables(trim($form["confirmation"]["url"]), $form, $lead, false, true);
- $url_info = parse_url($url);
- $query_string = $url_info["query"];
- $dynamic_query = GFCommon::replace_variables(trim($form["confirmation"]["queryString"]), $form, $lead, true);
- $query_string .= empty($url_info["query"]) || empty($dynamic_query) ? $dynamic_query : "&" . $dynamic_query;
- if(!empty($url_info["fragment"]))
- $query_string .= "#" . $url_info["fragment"];
- $url = $url_info["scheme"] . "://" . $url_info["host"] . $url_info["path"] . "?" . $query_string;
- }
- if(headers_sent() || $ajax){
- //Perform client side redirect for AJAX forms, of if headers have already been sent
- $confirmation = self::get_js_redirect_confirmation($url, $ajax);
- }
- else{
- $confirmation = array("redirect" => $url);
- }
- }
- $confirmation = apply_filters("gform_confirmation_{$form["id"]}", apply_filters("gform_confirmation", $confirmation, $form, $lead, $ajax), $form, $lead, $ajax);
- if(!is_array($confirmation)){
- $confirmation = GFCommon::gform_do_shortcode($confirmation); //enabling shortcodes
- }
- else if(headers_sent() || $ajax){
- //Perform client side redirect for AJAX forms, of if headers have already been sent
- $confirmation = self::get_js_redirect_confirmation($confirmation["redirect"], $ajax); //redirecting via client side
- }
- return $confirmation;
- }
- private static function get_js_redirect_confirmation($url, $ajax){
- $confirmation = "<script type=\"text/javascript\">" . apply_filters("gform_cdata_open", "") . " function gformRedirect(){document.location.href='$url';}";
- if(!$ajax)
- $confirmation .="gformRedirect();";
- $confirmation .= apply_filters("gform_cdata_close", "") . "</script>";
- return $confirmation;
- }
- public static function send_emails($form, $lead){
- $disable_user_notification = apply_filters("gform_disable_user_notification_{$form["id"]}", apply_filters("gform_disable_user_notification", false, $form, $lead), $form, $lead);
- if(!$disable_user_notification){
- GFCommon::send_user_notification($form, $lead);
- }
- $disable_admin_notification = apply_filters("gform_disable_admin_notification_{$form["id"]}", apply_filters("gform_disable_admin_notification", false, $form, $lead), $form, $lead);
- if(!$disable_admin_notification){
- GFCommon::send_admin_notification($form, $lead);
- }
- }
- public static function checkdate($month, $day, $year){
- if(empty($month) || !is_numeric($month) || empty($day) || !is_numeric($day) || empty($year) || !is_numeric($year) || strlen($year) != 4)
- return false;
- return checkdate($month, $day, $year);
- }
- public static function validate(&$form, $field_values, $page_number=0){
- // validate form schedule
- if(self::validate_form_schedule($form))
- return false;
- // validate entry limit
- if(self::validate_entry_limit($form))
- return false;
- foreach($form["fields"] as &$field){
- //If a page number is specified, only validates fields that are on current page
- if($page_number > 0 && $field["pageNumber"] != $page_number)
- continue;
- //ignore validation if field is hidden or admin only
- if(RGFormsModel::is_field_hidden($form, $field, $field_values) || $field["adminOnly"])
- continue;
- $value = RGFormsModel::get_field_value($field);
- //display error message if field is marked as required and the submitted value is empty
- if($field["isRequired"] && self::is_empty($field, $form["id"])){
- $field["failed_validation"] = true;
- $field["validation_message"] = empty($field["errorMessage"]) ? __("This field is required.", "gravityforms") : $field["errorMessage"];
- }
- //display error if field does not allow duplicates and the submitted value already exists
- else if($field["noDuplicates"] && RGFormsModel::is_duplicate($form["id"], $field, $value)){
- $field["failed_validation"] = true;
- $input_type = RGFormsModel::get_input_type($field);
- switch($input_type){
- case "date" :
- $default_message = __("This date has already been taken. Please select a new date.", "gravityforms");
- break;
- default:
- $default_message = is_array($value) ? __("This field requires an unique entry and the values you entered have been already been used.", "gravityforms") :
- sprintf(__("This field requires an unique entry and '%s' has already been used", "gravityforms"), $value);
- break;
- }
- $field["validation_message"] = apply_filters("gform_duplicate_message_{$form["id"]}", apply_filters("gform_duplicate_message", $default_message, $form), $form);
- }
- else{
- if(self::failed_state_validation($form["id"], $field, $value)){
- $field["failed_validation"] = true;
- $field["validation_message"] = in_array($field["inputType"], array("singleproduct", "singleshipping", "hiddenproduct")) ? __("Please enter a valid value.", "gravityforms") : __("Invalid selection. Please select one of the available choices.", "gravityforms");
- }
- else{
- switch(RGFormsModel::get_input_type($field)){
- case "password" :
- $password = $_POST["input_" . $field["id"]];
- $confirm = $_POST["input_" . $field["id"] . "_2"];
- if($password != $confirm){
- $field["failed_validation"] = true;
- $field["validation_message"] = __("Your passwords do not match.", "gravityforms");
- }
- else if(rgar($field,"passwordStrengthEnabled") && !rgempty("minPasswordStrength",$field) && !empty($password)){
- $strength = $_POST["input_" . $field["id"] . "_strength"];
- $levels = array("short" => 1, "bad" => 2, "good" => 3, "strong" => 4);
- if($levels[$strength] < $levels[$field["minPasswordStrength"]]){
- $field["failed_validation"] = true;
- $field["validation_message"] = empty($field["errorMessage"]) ? __("Your password does not meet the required strength. <br/>Hint: To make it stronger, use upper and lower case letters, numbers and symbols like ! \" ? $ % ^ & ).", "gravityforms") : $field["errorMessage"];
- }
- }
- break;
- case "name" :
- if($field["isRequired"] && $field["nameFormat"] != "simple")
- {
- $first = $_POST["input_" . $field["id"] . "_3"];
- $last = $_POST["input_" . $field["id"] . "_6"];
- if(empty($first) || empty($last)){
- $field["failed_validation"] = true;
- $field["validation_message"] = empty($field["errorMessage"]) ? __("This field is required. Please enter the first and last name.", "gravityforms") : $field["errorMessage"];
- }
- }
- break;
- case "address" :
- if($field["isRequired"])
- {
- $street = $_POST["input_" . $field["id"] . "_1"];
- $city = $_POST["input_" . $field["id"] . "_3"];
- $state = $_POST["input_" . $field["id"] . "_4"];
- $zip = $_POST["input_" . $field["id"] . "_5"];
- $country = $_POST["input_" . $field["id"] . "_6"];
- if(empty($street) || empty($city) || empty($zip) || (empty($state) && !$field["hideS…
Large files files are truncated, but you can click here to view the full file