private static function default_if_empty($field, $value) { if (!GFCommon::is_empty_array($value)) { return $value; } if (IS_ADMIN) { $value = rgget("defaultValue", $field); } else { $value = rgar($field, "defaultValue"); if (!is_array($value)) { $value = GFCommon::replace_variables_prepopulate(rgget("defaultValue", $field)); } } return $value; }
public static function prepare_date($date_format, $value) { $format = empty($date_format) ? 'mdy' : $date_format; $date_info = GFCommon::parse_date($value, $format); if (!empty($date_info) && !GFCommon::is_empty_array($date_info)) { $value = sprintf('%s-%02d-%02d', $date_info['year'], $date_info['month'], $date_info['day']); } else { $value = ''; } return $value; }
/** * Prepare the value before saving it to the lead. * * @param mixed $form * @param mixed $field * @param mixed $value * @param mixed $input_name * @param mixed $lead_id the current lead ID, used for fields that are processed after other fields have been saved (ie Total, Calculations) * @param mixed $lead passed by the RGFormsModel::create_lead() method, lead ID is not available for leads created by this function */ public static function prepare_value($form, $field, $value, $input_name, $lead_id, $lead = array()) { $form_id = $form["id"]; $input_type = self::get_input_type($field); switch ($input_type) { case "total": $lead = empty($lead) ? RGFormsModel::get_lead($lead_id) : $lead; $value = GFCommon::get_order_total($form, $lead); break; case "calculation": // ignore submitted value and recalculate price in backend list(, , $input_id) = rgexplode("_", $input_name, 3); if ($input_id == 2) { require_once GFCommon::get_base_path() . '/currency.php'; $currency = new RGCurrency(GFCommon::get_currency()); $lead = empty($lead) ? RGFormsModel::get_lead($lead_id) : $lead; $value = $currency->to_money(GFCommon::calculate($field, $form, $lead)); } break; case "phone": if ($field["phoneFormat"] == "standard" && preg_match('/^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$/', $value, $matches)) { $value = sprintf("(%s)%s-%s", $matches[1], $matches[2], $matches[3]); } break; case "time": if (!is_array($value) && !empty($value)) { preg_match('/^(\\d*):(\\d*) ?(.*)$/', $value, $matches); $value = array(); $value[0] = $matches[1]; $value[1] = $matches[2]; $value[2] = rgar($matches, 3); } $hour = empty($value[0]) ? "0" : strip_tags($value[0]); $minute = empty($value[1]) ? "0" : strip_tags($value[1]); $ampm = strip_tags(rgar($value, 2)); if (!empty($ampm)) { $ampm = " {$ampm}"; } if (!(empty($hour) && empty($minute))) { $value = sprintf("%02d:%02d%s", $hour, $minute, $ampm); } else { $value = ""; } break; case "date": $value = self::prepare_date($field["dateFormat"], $value); break; case "post_image": $url = self::get_fileupload_value($form_id, $input_name); $image_title = isset($_POST["{$input_name}_1"]) ? strip_tags($_POST["{$input_name}_1"]) : ""; $image_caption = isset($_POST["{$input_name}_4"]) ? strip_tags($_POST["{$input_name}_4"]) : ""; $image_description = isset($_POST["{$input_name}_7"]) ? strip_tags($_POST["{$input_name}_7"]) : ""; $value = !empty($url) ? $url . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : ""; break; case "fileupload": $value = self::get_fileupload_value($form_id, $input_name); break; case "number": $lead = empty($lead) ? RGFormsModel::get_lead($lead_id) : $lead; $value = GFCommon::has_field_calculation($field) ? GFCommon::round_number(GFCommon::calculate($field, $form, $lead), rgar($field, "calculationRounding")) : GFCommon::clean_number($value, rgar($field, "numberFormat")); //return the value as a string when it is zero and a calc so that the "==" comparison done when checking if the field has changed isn't treated as false if (GFCommon::has_field_calculation($field) && $value == 0) { $value = "0"; } break; case "website": if ($value == "http://") { $value = ""; } break; case "list": if (GFCommon::is_empty_array($value)) { $value = ""; } else { $value = self::create_list_array($field, $value); $value = serialize($value); } break; case "radio": if (rgar($field, 'enableOtherChoice') && $value == 'gf_other_choice') { $value = rgpost("input_{$field['id']}_other"); } break; case "multiselect": $value = empty($value) ? "" : implode(",", $value); break; case "creditcard": //saving last 4 digits of credit card list($input_token, $field_id_token, $input_id) = rgexplode("_", $input_name, 3); if ($input_id == "1") { $value = str_replace(" ", "", $value); $card_number_length = strlen($value); $value = substr($value, -4, 4); $value = str_pad($value, $card_number_length, "X", STR_PAD_LEFT); } else { if ($input_id == "4") { $card_number = rgpost("input_{$field_id_token}_1"); $card_type = GFCommon::get_card_type($card_number); $value = $card_type ? $card_type["name"] : ""; } else { $value = ""; } } break; default: //allow HTML for certain field types $allow_html = in_array($field["type"], array("post_custom_field", "post_title", "post_content", "post_excerpt", "post_tags")) || in_array($input_type, array("checkbox", "radio")) ? true : false; $allowable_tags = apply_filters("gform_allowable_tags_{$form_id}", apply_filters("gform_allowable_tags", $allow_html, $field, $form_id), $field, $form_id); if ($allowable_tags !== true) { $value = strip_tags($value, $allowable_tags); } break; } // special format for Post Category fields if ($field['type'] == 'post_category') { $full_values = array(); if (!is_array($value)) { $value = explode(',', $value); } foreach ($value as $cat_id) { $cat = get_term($cat_id, 'category'); $full_values[] = !is_wp_error($cat) && is_object($cat) ? $cat->name . ":" . $cat_id : ""; } $value = implode(',', $full_values); } //do not save price fields with blank price if (rgar($field, "enablePrice")) { $ary = explode("|", $value); $label = count($ary) > 0 ? $ary[0] : ""; $price = count($ary) > 1 ? $ary[1] : ""; $is_empty = strlen(trim($price)) <= 0; if ($is_empty) { $value = ""; } } return $value; }
/** * Returns the field default value if the field does not already have a value. * * @param array|string $value The field value. * * @return array|string */ public function get_value_default_if_empty($value) { if (!GFCommon::is_empty_array($value)) { return $value; } return $this->get_value_default(); }
/** * Helper to determine if the empty field should be displayed when the lead detail grid is processed. * * @param array $form The Form object for the current Entry. * @param string|array $value The field value. * * @return bool */ public function hide_empty_likert_field($form, $value) { $mode = empty($_POST['screen_mode']) ? 'view' : $_POST['screen_mode']; $allow_display_empty_fields = $mode == 'view'; return GFCommon::is_empty_array($value) && !GFEntryDetail::maybe_display_empty_fields($allow_display_empty_fields, $form, false); }
public function get_value_save_entry($value, $form, $input_name, $lead_id, $lead) { if ($this->adminOnly && $this->allowsPrepopulate) { $value = json_decode($value); } if (GFCommon::is_empty_array($value)) { $value = ''; } else { $value = $this->create_list_array($value); $value = serialize($value); } return $value; }
public static function process_feed($feed, $entry, $form) { $body = self::get_body($entry, $form); $headers = array(); if (GFCommon::is_empty_array($body)) { $headers['X-Hook-Test'] = 'true'; } $json_body = json_encode($body); if (empty($body)) { self::log_debug('There is no field data to send to Zapier.'); return false; } self::log_debug('Posting to url: ' . $feed['url'] . ' data: ' . print_r($body, true)); $form_data = array('sslverify' => false, 'ssl' => true, 'body' => $json_body, 'headers' => $headers); $response = wp_remote_post($feed['url'], $form_data); if (is_wp_error($response)) { self::log_error('The following error occurred: ' . print_r($response, true)); return false; } else { self::log_debug('Successful response from Zap: ' . print_r($response, true)); if (!empty($entry)) { self::log_debug('Marking entry #' . $entry['id'] . ' as fulfilled.'); gform_update_meta($entry['id'], self::$slug . '_is_fulfilled', true); } return true; } }
/** * Prepare the value before saving it to the lead. * * @param mixed $form * @param mixed $field * @param mixed $value * @param mixed $input_name * @param mixed $lead_id the current lead ID, used for fields that are processed after other fields have been saved (ie Total, Calculations) * @param mixed $lead passed by the RGFormsModel::create_lead() method, lead ID is not available for leads created by this function */ public static function prepare_value($form, $field, $value, $input_name, $lead_id, $lead = array()) { $form_id = $form["id"]; $input_type = self::get_input_type($field); switch ($input_type) { case "total": $lead = empty($lead) ? RGFormsModel::get_lead($lead_id) : $lead; $value = GFCommon::get_order_total($form, $lead); break; case "calculation": // ignore submitted value and recalculate price in backend list(, , $input_id) = rgexplode("_", $input_name, 3); if ($input_id == 2) { require_once GFCommon::get_base_path() . '/currency.php'; $currency = new RGCurrency(GFCommon::get_currency()); $lead = empty($lead) ? RGFormsModel::get_lead($lead_id) : $lead; $value = $currency->to_money(GFCommon::calculate($field, $form, $lead)); } break; case "phone": if ($field["phoneFormat"] == "standard" && preg_match('/^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$/', $value, $matches)) { $value = sprintf("(%s)%s-%s", $matches[1], $matches[2], $matches[3]); } break; case "time": if (!is_array($value) && !empty($value)) { preg_match('/^(\\d*):(\\d*) ?(.*)$/', $value, $matches); $value = array(); $value[0] = $matches[1]; $value[1] = $matches[2]; $value[2] = rgar($matches, 3); } $hour = empty($value[0]) ? "0" : strip_tags($value[0]); $minute = empty($value[1]) ? "0" : strip_tags($value[1]); $ampm = strip_tags(rgar($value, 2)); if (!empty($ampm)) { $ampm = " {$ampm}"; } if (!(empty($hour) && empty($minute))) { $value = sprintf("%02d:%02d%s", $hour, $minute, $ampm); } else { $value = ""; } break; case "date": $value = self::prepare_date(rgar($field, 'dateFormat'), $value); break; case "post_image": $url = self::get_fileupload_value($form_id, $input_name); $image_title = isset($_POST["{$input_name}_1"]) ? strip_tags($_POST["{$input_name}_1"]) : ""; $image_caption = isset($_POST["{$input_name}_4"]) ? strip_tags($_POST["{$input_name}_4"]) : ""; $image_description = isset($_POST["{$input_name}_7"]) ? strip_tags($_POST["{$input_name}_7"]) : ""; $value = !empty($url) ? $url . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : ""; break; case "fileupload": if (rgar($field, "multipleFiles")) { global $_gf_uploaded_files; if (isset($_gf_uploaded_files[$input_name])) { $value = $_gf_uploaded_files[$input_name]; } else { if (isset(GFFormsModel::$uploaded_files[$form_id][$input_name])) { $uploaded_temp_files = GFFormsModel::$uploaded_files[$form_id][$input_name]; $uploaded_files = array(); foreach ($uploaded_temp_files as $i => $file_info) { $temp_filepath = self::get_upload_path($form_id) . '/tmp/' . $file_info['temp_filename']; if ($file_info && file_exists($temp_filepath)) { $uploaded_files[$i] = self::move_temp_file($form_id, $file_info); } } if (!empty($value)) { // merge with existing files (admin edit entry) $value = json_decode($value, true); $value = array_merge($value, $uploaded_files); $value = json_encode($value); } else { $value = json_encode($uploaded_files); } } else { $value = ''; } $_gf_uploaded_files[$input_name] = $value; } } else { $value = self::get_fileupload_value($form_id, $input_name); } break; case "number": $value = GFCommon::maybe_add_leading_zero($value); $is_hidden = RGFormsModel::is_field_hidden($form, $field, array()); $lead = empty($lead) ? RGFormsModel::get_lead($lead_id) : $lead; $value = GFCommon::has_field_calculation($field) ? GFCommon::round_number(GFCommon::calculate($field, $form, $lead), rgar($field, "calculationRounding")) : GFCommon::clean_number($value, rgar($field, "numberFormat")); //return the value as a string when it is zero and a calc so that the "==" comparison done when checking if the field has changed isn't treated as false if (GFCommon::has_field_calculation($field) && $value == 0) { $value = "0"; } break; case "website": if ($value == "http://") { $value = ""; } break; case "list": if (rgar($field, "adminOnly") && rgar($field, "allowsPrepopulate")) { $value = json_decode($value); } if (GFCommon::is_empty_array($value)) { $value = ""; } else { foreach ($value as &$val) { $val = self::sanitize_entry_value($field, $val, $input_type, $form_id); } $value = self::create_list_array($field, $value); $value = serialize($value); } break; case "radio": if (rgar($field, 'enableOtherChoice') && $value == 'gf_other_choice') { $value = rgpost("input_{$field['id']}_other"); } $value = self::sanitize_entry_value($field, $value, $input_type, $form_id); break; case "multiselect": $value = empty($value) ? "" : is_array($value) ? implode(",", $value) : $value; $value = self::sanitize_entry_value($field, $value, $input_type, $form_id); break; case "creditcard": //saving last 4 digits of credit card list($input_token, $field_id_token, $input_id) = rgexplode("_", $input_name, 3); if ($input_id == "1") { $value = str_replace(" ", "", $value); $card_number_length = strlen($value); $value = substr($value, -4, 4); $value = str_pad($value, $card_number_length, "X", STR_PAD_LEFT); } else { if ($input_id == '4') { $value = rgpost("input_{$field_id_token}_4"); if (!$value) { $card_number = rgpost("input_{$field_id_token}_1"); $card_type = GFCommon::get_card_type($card_number); $value = $card_type ? $card_type['name'] : ''; } } else { $value = ""; } } break; case 'password': $encrypt_password = apply_filters('gform_encrypt_password', false, $field, $form); if ($encrypt_password) { $value = GFCommon::encrypt($value); self::set_encrypted_fields($lead_id, $field['id']); } break; default: // only filter HTML on non-array based values if (!is_array($value)) { $value = self::sanitize_entry_value($field, $value, $input_type, $form_id); } break; } // special format for Post Category fields if ($field['type'] == 'post_category') { $full_values = array(); if (!is_array($value)) { $value = explode(',', $value); } foreach ($value as $cat_id) { $cat = get_term($cat_id, 'category'); $full_values[] = !is_wp_error($cat) && is_object($cat) ? $cat->name . ":" . $cat_id : ""; } $value = implode(',', $full_values); } //do not save price fields with blank price if (rgar($field, "enablePrice")) { $ary = explode("|", $value); $label = count($ary) > 0 ? $ary[0] : ""; $price = count($ary) > 1 ? $ary[1] : ""; $is_empty = strlen(trim($price)) <= 0; if ($is_empty) { $value = ""; } } return $value; }
private static function prepare_value($form, $field, $value, $input_name, $lead_id) { $form_id = $form["id"]; $input_type = self::get_input_type($field); switch ($input_type) { case "total": $lead = RGFormsModel::get_lead($lead_id); $value = GFCommon::get_order_total($form, $lead); break; case "post_category": $cat = get_category($value); $value = !empty($cat) ? $cat->name . ":" . $value : ""; break; case "phone": if ($field["phoneFormat"] == "standard" && preg_match('/^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$/', $value, $matches)) { $value = sprintf("(%s)%s-%s", $matches[1], $matches[2], $matches[3]); } break; case "time": if (!is_array($value) && !empty($value)) { preg_match('/^(\\d*):(\\d*) ?(.*)$/', $value, $matches); $value = array(); $value[0] = $matches[1]; $value[1] = $matches[2]; $value[2] = rgar($matches, 3); } $hour = empty($value[0]) ? "0" : strip_tags($value[0]); $minute = empty($value[1]) ? "0" : strip_tags($value[1]); $ampm = strip_tags(rgar($value, 2)); if (!empty($ampm)) { $ampm = " {$ampm}"; } if (!(empty($hour) && empty($minute))) { $value = sprintf("%02d:%02d%s", $hour, $minute, $ampm); } else { $value = ""; } break; case "date": $format = empty($field["dateFormat"]) ? "mdy" : $field["dateFormat"]; $date_info = GFCommon::parse_date($value, $format); if (!empty($date_info)) { $value = sprintf("%d-%02d-%02d", $date_info["year"], $date_info["month"], $date_info["day"]); } else { $value = ""; } break; case "post_image": $url = self::get_fileupload_value($form_id, $input_name); $image_title = isset($_POST["{$input_name}_1"]) ? strip_tags($_POST["{$input_name}_1"]) : ""; $image_caption = isset($_POST["{$input_name}_4"]) ? strip_tags($_POST["{$input_name}_4"]) : ""; $image_description = isset($_POST["{$input_name}_7"]) ? strip_tags($_POST["{$input_name}_7"]) : ""; $value = !empty($url) ? $url . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : ""; break; case "fileupload": $value = self::get_fileupload_value($form_id, $input_name); break; case "number": $value = GFCommon::clean_number($value, rgar($field, "numberFormat")); break; case "website": if ($value == "http://") { $value = ""; } break; case "list": if (GFCommon::is_empty_array($value)) { $value = ""; } else { $value = self::create_list_array($field, $value); $value = serialize($value); } break; case "radio": if (rgar($field, 'enableOtherChoice') && $value == 'gf_other_choice') { $value = rgpost("input_{$field['id']}_other"); } break; case "multiselect": $value = empty($value) ? "" : implode(",", $value); break; case "creditcard": //saving last 4 digits of credit card list($input_token, $field_id_token, $input_id) = rgexplode("_", $input_name, 3); if ($input_id == "1") { $value = str_replace(" ", "", $value); $card_number_length = strlen($value); $value = substr($value, -4, 4); $value = str_pad($value, $card_number_length, "X", STR_PAD_LEFT); } else { if ($input_id == "4") { $card_number = rgpost("input_{$field_id_token}_1"); $card_type = GFCommon::get_card_type($card_number); $value = $card_type ? $card_type["name"] : ""; } else { $value = ""; } } break; default: //allow HTML for certain field types $allow_html = in_array($field["type"], array("post_custom_field", "post_title", "post_content", "post_excerpt", "post_tags")) || in_array($input_type, array("checkbox", "radio")) ? true : false; $allowable_tags = apply_filters("gform_allowable_tags_{$form_id}", apply_filters("gform_allowable_tags", $allow_html, $field, $form_id), $field, $form_id); if ($allowable_tags !== true) { $value = strip_tags($value, $allowable_tags); } break; } //do not save price fields with blank price if (rgar($field, "enablePrice")) { $ary = explode("|", $value); $label = count($ary) > 0 ? $ary[0] : ""; $price = count($ary) > 1 ? $ary[1] : ""; $is_empty = strlen(trim($price)) <= 0; if ($is_empty) { $value = ""; } } return $value; }
public static function send_form_data_to_zapier($entry = null, $form) { //if there is an entry, then this is a form submission, get data out of entry to POST to Zapier //otherwise this is a dummy setup to give Zapier the field data, get the form fields and POST to Zapier with empty data if (empty($form) && empty($entry)) { self::log_debug("No form or entry was provided to send data to Zapier."); return false; } $body = self::get_body($entry, $form); $headers = array(); if (GFCommon::is_empty_array($body)) { $headers["X-Hook-Test"] = 'true'; } $json_body = json_encode($body); if (empty($body)) { self::log_debug("There is no field data to send to Zapier."); return false; } //get zaps for form $form_id = $form["id"]; $zaps = GFZapierData::get_feed_by_form($form_id, true); if (empty($zaps)) { self::log_debug("There are no zaps configured for form id {$form_id}"); return false; } $is_entry = !empty($entry); $is_entry ? self::log_debug("Gathering entry data to send submission.") : self::log_debug("Gathering field data to send dummy submission."); $retval = true; foreach ($zaps as $zap) { //checking to see if a condition was specified, and if so, met, otherwise don't send to zapier //only check this when there is an entry, simple form updates should go to zapier regardless of conditions existing if (!$is_entry || $is_entry && self::conditions_met($form, $zap)) { if ($is_entry) { self::log_debug("No condition specified or a condition was specified and met, sending to Zapier"); } $form_data = array("sslverify" => false, "ssl" => true, "body" => $json_body, "headers" => $headers); self::log_debug("Posting to url: " . $zap["url"] . " data: " . print_r($body, true)); $response = wp_remote_post($zap["url"], $form_data); if (is_wp_error($response)) { self::log_error("The following error occurred: " . print_r($response, true)); $retval = false; } else { self::log_debug("Successful response from Zap: " . print_r($response, true)); } } else { self::log_debug("A condition was specified and not met, not sending to Zapier"); $retval = false; } } return $retval; }
public static function send_form_data_to_zapier($entry = null, $form) { //if there is an entry, then this is a form submission, get data out of entry to POST to Zapier //otherwise this is a dummy setup to give Zapier the field data, get the form fields and POST to Zapier with empty data if (empty($form) && empty($entry)) { self::log_debug('No form or entry was provided to send data to Zapier.'); return false; } //get zaps for form $form_id = $form['id']; $zaps = GFZapierData::get_feed_by_form($form_id, true); if (empty($zaps)) { self::log_debug("There are no zaps configured for form id {$form_id}"); return false; } //see if there is a paypal feed and zapier is set to be delayed until payment is received if (class_exists('GFPayPal')) { $paypal_feeds = self::get_paypal_feeds($form['id']); //loop through paypal feeds to get active one for this form submission, needed to see if add-on processing should be delayed foreach ($paypal_feeds as $paypal_feed) { if ($paypal_feed['is_active'] && self::is_feed_condition_met($paypal_feed, $form, $entry)) { $active_paypal_feed = $paypal_feed; break; } } $is_fulfilled = rgar($entry, 'is_fulfilled'); if (!empty($active_paypal_feed) && self::is_delayed($active_paypal_feed) && self::has_paypal_payment($active_paypal_feed, $form, $entry) && !$is_fulfilled) { self::log_debug('Zapier Feed processing is delayed pending payment, not processing feed for entry #' . $entry['id']); return false; } } //do not send spam entries to zapier if (!empty($entry) && $entry['status'] == 'spam') { self::log_debug('The entry is marked as spam, NOT sending to Zapier.'); return false; } $body = self::get_body($entry, $form); $headers = array(); if (GFCommon::is_empty_array($body)) { $headers['X-Hook-Test'] = 'true'; } $json_body = json_encode($body); if (empty($body)) { self::log_debug('There is no field data to send to Zapier.'); return false; } $is_entry = !empty($entry); $is_entry ? self::log_debug('Gathering entry data to send submission.') : self::log_debug('Gathering field data to send dummy submission.'); $retval = true; foreach ($zaps as $zap) { //checking to see if a condition was specified, and if so, met, otherwise don't send to zapier //only check this when there is an entry, simple form updates should go to zapier regardless of conditions existing if (!$is_entry || $is_entry && self::conditions_met($form, $zap, $entry)) { if ($is_entry) { self::log_debug('No condition specified or a condition was specified and met, sending to Zapier'); } $form_data = array('sslverify' => false, 'ssl' => true, 'body' => $json_body, 'headers' => $headers); self::log_debug('Posting to url: ' . $zap['url'] . ' data: ' . print_r($body, true)); $response = wp_remote_post($zap['url'], $form_data); if (is_wp_error($response)) { self::log_error('The following error occurred: ' . print_r($response, true)); $retval = false; } else { self::log_debug('Successful response from Zap: ' . print_r($response, true)); } } else { self::log_debug('A condition was specified and not met, not sending to Zapier'); $retval = false; } } return $retval; }