function nb_gform_post_submission($entry)
{
// add response value to entry meta
foreach ($this->nb_infogeniuz_fields as $field_group) {
foreach ($field_group as $field_key => $field_name) {
gform_update_meta($entry['id'], $field_key, esc_attr($_POST[$field_key]));
}
}
}
/**
* Adds a single Entry object.
*
* Intended to be used for importing an entry object. The usual hooks that are triggered while saving entries are not fired here.
* Checks that the form id, field ids and entry meta exist and ignores legacy values (i.e. values for fields that no longer exist).
*
* @since 1.8
* @access public
* @static
*
* @param array $entry The Entry object
*
* @return mixed Either the new Entry ID or a WP_Error instance
*/
public static function add_entry($entry)
{
global $wpdb;
if (!is_array($entry)) {
return new WP_Error('invalid_entry_object', __('The entry object must be an array', 'gravityforms'));
}
// make sure the form id exists
$form_id = rgar($entry, 'form_id');
if (empty($form_id)) {
return new WP_Error('empty_form_id', __('The form id must be specified', 'gravityforms'));
}
if (false === self::form_id_exists($form_id)) {
return new WP_Error('invalid_form_id', __('The form for this entry does not exist', 'gravityforms'));
}
// use values in the entry object if present
$post_id = isset($entry['post_id']) ? intval($entry['post_id']) : 'NULL';
$date_created = isset($entry['date_created']) && $entry['date_created'] != '' ? sprintf("'%s'", esc_sql($entry['date_created'])) : 'utc_timestamp()';
$is_starred = isset($entry['is_starred']) ? $entry['is_starred'] : 0;
$is_read = isset($entry['is_read']) ? $entry['is_read'] : 0;
$ip = isset($entry['ip']) ? $entry['ip'] : GFFormsModel::get_ip();
$source_url = isset($entry['source_url']) ? $entry['source_url'] : esc_url_raw(GFFormsModel::get_current_page_url());
$user_agent = isset($entry['user_agent']) ? $entry['user_agent'] : 'API';
$currency = isset($entry['currency']) ? $entry['currency'] : GFCommon::get_currency();
$payment_status = isset($entry['payment_status']) ? sprintf("'%s'", esc_sql($entry['payment_status'])) : 'NULL';
$payment_date = strtotime(rgar($entry, 'payment_date')) ? sprintf("'%s'", gmdate('Y-m-d H:i:s', strtotime("{$entry['payment_date']}"))) : 'NULL';
$payment_amount = isset($entry['payment_amount']) ? (double) $entry['payment_amount'] : 'NULL';
$payment_method = isset($entry['payment_method']) ? $entry['payment_method'] : '';
$transaction_id = isset($entry['transaction_id']) ? sprintf("'%s'", esc_sql($entry['transaction_id'])) : 'NULL';
$is_fulfilled = isset($entry['is_fulfilled']) ? intval($entry['is_fulfilled']) : 'NULL';
$status = isset($entry['status']) ? $entry['status'] : 'active';
global $current_user;
$user_id = isset($entry['created_by']) ? absint($entry['created_by']) : '';
if (empty($user_id)) {
$user_id = $current_user && $current_user->ID ? absint($current_user->ID) : 'NULL';
}
$transaction_type = isset($entry['transaction_type']) ? intval($entry['transaction_type']) : 'NULL';
$lead_table = GFFormsModel::get_lead_table_name();
$result = $wpdb->query($wpdb->prepare("\n INSERT INTO {$lead_table}\n (form_id, post_id, date_created, is_starred, is_read, ip, source_url, user_agent, currency, payment_status, payment_date, payment_amount, transaction_id, is_fulfilled, created_by, transaction_type, status, payment_method)\n VALUES\n (%d, {$post_id}, {$date_created}, %d, %d, %s, %s, %s, %s, {$payment_status}, {$payment_date}, {$payment_amount}, {$transaction_id}, {$is_fulfilled}, {$user_id}, {$transaction_type}, %s, %s)\n ", $form_id, $is_starred, $is_read, $ip, $source_url, $user_agent, $currency, $status, $payment_method));
if (false === $result) {
return new WP_Error('insert_entry_properties_failed', __('There was a problem while inserting the entry properties', 'gravityforms'), $wpdb->last_error);
}
// reading newly created lead id
$entry_id = $wpdb->insert_id;
$entry['id'] = $entry_id;
// only save field values for fields that currently exist in the form
$form = GFFormsModel::get_form_meta($form_id);
foreach ($form['fields'] as $field) {
/* @var GF_Field $field */
if (in_array($field->type, array('html', 'page', 'section'))) {
continue;
}
$inputs = $field->get_entry_inputs();
if (is_array($inputs)) {
foreach ($inputs as $input) {
$input_id = (string) $input['id'];
if (isset($entry[$input_id])) {
$result = GFFormsModel::update_lead_field_value($form, $entry, $field, 0, $input_id, $entry[$input_id]);
if (false === $result) {
return new WP_Error('insert_input_value_failed', __('There was a problem while inserting one of the input values for the entry', 'gravityforms'), $wpdb->last_error);
}
}
}
} else {
$field_id = $field->id;
$field_value = isset($entry[(string) $field_id]) ? $entry[(string) $field_id] : '';
$result = GFFormsModel::update_lead_field_value($form, $entry, $field, 0, $field_id, $field_value);
if (false === $result) {
return new WP_Error('insert_field_values_failed', __('There was a problem while inserting the field values', 'gravityforms'), $wpdb->last_error);
}
}
}
// add save the entry meta values - only for the entry meta currently available for the form, ignore the rest
$entry_meta = GFFormsModel::get_entry_meta($form_id);
if (is_array($entry_meta)) {
foreach (array_keys($entry_meta) as $key) {
if (isset($entry[$key])) {
gform_update_meta($entry_id, $key, $entry[$key], $form['id']);
}
}
}
// Refresh the entry
$entry = GFAPI::get_entry($entry['id']);
/**
* Fires after the Entry is added using the API.
*
* @since 1.9.14.26
//.........这里部分代码省略.........
/**
* pluploader_trash_checkbox
*
* Called by 'gform_update_status' gravity forms action.
* Handles the meta data for an entry relating to the deletion
* of any plupload files attached the entry.
*
* @access public
* @author Ben Moody
*/
public function pluploader_trash_checkbox($lead_id, $property_value, $previous_value)
{
if (isset($property_value) && $property_value === 'trash') {
if (isset($_POST['prso_pluploader_delete_uploads']) && $_POST['prso_pluploader_delete_uploads'] === 'on') {
//Update delete file meta for this entry
gform_update_meta($lead_id, self::$delete_files_meta_key, 'checked');
} else {
//Update delete file meta for this entry
gform_delete_meta($lead_id, self::$delete_files_meta_key);
}
}
}
public function form_save_confirmation($confirmation, $form, $lead, $ajax)
{
if (!isset($form['enableFormState']) || !$form['enableFormState']) {
return $confirmation;
}
$user = wp_get_current_user();
if (!isset($_POST['gform_save_state_' . $form['id']])) {
if (!empty($form['enableFormStateOnSubmit']) && $form['enableFormStateOnSubmit']) {
/* still save, but do submit, thanks */
update_user_meta($user->ID, 'completed_form_' . $form['id'], $lead['id']);
update_user_meta($user->ID, 'has_pending_form_' . $form['id'], $lead['id']);
return $confirmation;
}
/* remove all saved data for this form and user */
delete_user_meta($user->ID, 'has_pending_form_' . $form['id']);
update_user_meta($user->ID, 'completed_form_' . $form['id'], $lead['id']);
return $confirmation;
}
if (!isset($_POST['gform_save_state_' . $form['id']])) {
return $confirmation;
}
/* this should never happend */
/* set pending to user id */
gform_update_meta($lead['id'], 'is_pending', $user->ID);
/* set latest pending */
update_user_meta($user->ID, 'has_pending_form_' . $form['id'], $lead['id']);
/* set lead to pending */
RGFormsModel::update_lead_property($lead['id'], 'status', 'pending', false, true);
GFAPI::update_entry_property($lead['id'], 'status', 'pending', false, true);
GFAPI::update_entry_property($lead['id'], 'orderStatus', 'incomplete', false, true);
$headers[] = "Content-type: text/html";
wp_mail('[email protected]', 'Lead Data that will be saved', print_r($lead, true), $headers);
do_action('gform_save_state', $form, $lead);
$confirmation = __('Your progress has been saved. You Lead Number for this progress is ' . $lead['id']);
return $confirmation;
}
public function paypal_fulfillment($entry, $config, $transaction_id, $amount)
{
self::log_debug("Checking PayPal fulfillment for transaction {$transaction_id}");
$is_fulfilled = gform_get_meta($entry['id'], "{$this->_slug}_is_fulfilled");
if (!$is_fulfilled) {
self::log_debug("Entry {$entry['id']} has not been fulfilled.");
$form = RGFormsModel::get_form_meta($entry['form_id']);
$this->maybe_process_feed($entry, $form, true);
// updating meta to indicate this entry has been fulfilled for the current add-on
self::log_debug("Marking entry {$entry['id']} as fulfilled");
gform_update_meta($entry['id'], "{$this->_slug}_is_fulfilled", true);
} else {
self::log_debug("Entry {$entry['id']} is already fulfilled.");
}
}
public function form_save_confirmation($confirmation, $form, $lead, $ajax)
{
if (!isset($form['enableFormState']) || !$form['enableFormState']) {
return $confirmation;
}
$user = wp_get_current_user();
if (!isset($_POST['gform_save_state_' . $form['id']])) {
if (!empty($form['enableFormStateOnSubmit']) && $form['enableFormStateOnSubmit']) {
/* still save, but do submit, thanks */
update_user_meta($user->ID, 'completed_form_' . $form['id'], $lead['id']);
update_user_meta($user->ID, 'has_pending_form_' . $form['id'], $lead['id']);
return $confirmation;
}
/* remove all saved data for this form and user */
delete_user_meta($user->ID, 'has_pending_form_' . $form['id']);
update_user_meta($user->ID, 'completed_form_' . $form['id'], $lead['id']);
return $confirmation;
}
if (!isset($_POST['gform_save_state_' . $form['id']])) {
return $confirmation;
}
/* this should never happend */
/* set pending to user id */
gform_update_meta($lead['id'], 'is_pending', $user->ID);
/* set latest pending */
update_user_meta($user->ID, 'has_pending_form_' . $form['id'], $lead['id']);
/* set lead to pending */
RGFormsModel::update_lead_property($lead['id'], 'status', 'pending', false, true);
do_action('gform_save_state', $form, $lead);
$confirmation = __('Your progress has been saved. You can return to this form anytime in the future to complete it.');
return $confirmation;
}
private static function create($entry, $form, $feed)
{
self::log_debug(__METHOD__ . ': Starting the create process...');
$api = self::get_api();
$token = self::getToken();
// There was no token. This is all wrong.
if (empty($token)) {
self::log_error(__METHOD__ . ': There was no OAuth token. It was likely revoked. Aborting.');
return false;
}
if (!isset($feed['is_active']) || $feed['is_active'] == 0) {
self::log_error(sprintf('%s: Feed `%s` is not active.', __METHOD__, $feed['meta']['contact_object_name']));
return false;
}
$merge_vars = self::process_merge_vars($entry, $form, $feed);
$merge_vars = apply_filters('gf_salesforce_create_data', $merge_vars, $form, $entry, $feed, $api);
// Make sure the charset is UTF-8 for Salesforce.
$merge_vars = array_map(array('GFSalesforce', '_convert_to_utf_8'), $merge_vars);
// Don't send merge_vars that are empty. It can cause problems with Salesforce strict typing. For example,
// if the form has a text field where a number should go, but that number isn't always required, when it's
// not supplied, we don't want to send <var></var> to Salesforce. It might choke because it expects a Double
// data type, not an empty string
$merge_vars = array_filter($merge_vars, array('GFSalesforce', '_remove_empty_fields'));
// We create the object to insert/upsert into Salesforce
$Account = new SObject();
// The fields to use are the merge vars
$Account->fields = $merge_vars;
// Set the type of object
$Account->type = $feed['meta']['contact_object_name'];
$foreign_key_label = self::primary_key_id($feed);
try {
if (!self::$instance instanceof GFSalesforce) {
self::$instance = self::Instance();
}
// If the primary field has been set, use that to upsert instead of create.
// @since 2.5.2, to avoid duplicates at Salesforce
if (!empty($feed['meta']['primary_field'])) {
self::log_debug(sprintf('%s: Upserting using primary field of `%s`', __METHOD__, $feed['meta']['primary_field']));
if (empty(self::$instance->result->id)) {
// old upsert
// https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_upsert.htm
self::log_debug(__METHOD__ . ': Upserting');
$result = $api->upsert($feed['meta']['primary_field'], array($Account));
} else {
self::log_debug(sprintf('%s: Creating with previous id %s', __METHOD__, self::$instance->result->id));
$Account->fields[$feed['meta']['primary_field']] = self::$instance->result->id;
$result = $api->create(array($Account));
}
} else {
self::log_debug(__METHOD__ . ': Creating, not upserting');
$result = $api->create(array($Account));
}
$api_exception = '';
self::log_debug(sprintf('%s: $Account object: %s', __METHOD__, print_r($Account, true)));
} catch (Exception $e) {
self::log_error(sprintf("%s:\n\nException While Exporting Entry\nThere was an error exporting Entry #%s for Form #%s. Here's the error:\n%s", __METHOD__, $entry['id'], $form['id'], $e->getMessage()));
$api_exception = "\r\n\t\t\t\tException Message: " . $e->getMessage() . "\nFaultstring: " . $e->faultstring . "\nFile: " . $e->getFile() . "\nLine: " . $e->getLine() . "\nArgs: " . serialize($merge_vars) . "\nTrace: " . serialize($e->getTrace());
gform_update_meta($entry['id'], 'salesforce_api_result', 'Error: ' . $e->getMessage());
}
if (isset($result) && count($result) == 1 && !empty($result[0])) {
self::$instance->result = $result = $result[0];
}
if (isset($result->success) && !empty($result->success)) {
$result_id = $result->id;
self::$foreign_keys[$foreign_key_label] = $result_id;
gform_update_meta($entry['id'], 'salesforce_id', $result_id);
gform_update_meta($entry['id'], 'salesforce_api_result', 'success');
$success_note = sprintf(__('Successfully added/updated to Salesforce (%s) with ID #%s. View entry at %s', 'gravity-forms-salesforce'), $Account->type, $result_id, self::getTokenParam('instance_url') . '/' . $result_id);
self::log_debug(__METHOD__ . ': ' . $success_note);
self::add_note($entry["id"], $success_note);
self::admin_screen_message(__('Entry added/updated in Salesforce.', 'gravity-forms-salesforce'), 'updated');
/**
* @since 3.1.2
*/
do_action('gravityforms_salesforce_object_added_updated', $Account, $feed, $result_id);
return $result_id;
} else {
if (isset($result->errors[0])) {
$errors = $result->errors[0];
}
if (isset($errors)) {
self::log_error(sprintf('%s: There was an error exporting Entry #%s for Form #%s. Salesforce responded with:', __METHOD__, $entry['id'], $form['id']) . "\n" . print_r($errors, true));
if ($email = self::is_notify_on_error()) {
$error_heading = __('Error adding to Salesforce', 'gravity-forms-salesforce');
// Create the email message to send
$message = sprintf(apply_filters('gravityforms_salesforce_notify_on_error_message', '<h3>' . $error_heading . '</h3>' . wpautop(__("There was an error when attempting to add %sEntry #%s from the form \"%s\"", 'gravity-forms-salesforce')), $errors, $entry, $form), '<a href="' . admin_url('admin.php?page=gf_entries&view=entry&id=' . $entry['form_id'] . '&lid=' . $entry['id']) . '">', $entry['id'] . '</a>', $form['title']);
// Send as HTML
$headers = "Content-type: text/html; charset=" . get_option('blog_charset') . "\r\n";
// Send email
$sent = wp_mail($email, $error_heading, $message, $headers);
if (!$sent) {
self::log_error(__METHOD__ . ': There was an error sending the error email. This really isn\'t your day, is it?');
}
}
self::add_note($entry["id"], sprintf(__('Errors when adding to Salesforce (%s): %s', 'gravity-forms-salesforce'), $Account->type, $errors->message . $api_exception));
}
self::admin_screen_message(__('Errors when adding to Salesforce. Entry not sent! Check the Entry Notes below for more details.', 'gravity-forms-salesforce'), 'error');
return false;
}
}
请发表评论