function bb_merge_users_process($from_user, $to_user, $return = false) { global $wpdb; // GF doesn't hook into delete_user so we need to change leads manually $wpdb->query('UPDATE ' . GFFormsModel::get_lead_table_name() . ' SET created_by = ' . $to_user . ' WHERE created_by = ' . $from_user); // But WP handles the rest of it for us :-) wp_delete_user($from_user, $to_user); if ($return) { return true; } echo '<div class="updated"><p>Merge Complete</p></div>' . "\n"; }
public static function build_sql($atts, $lid = false) { global $wpdb; /* * Build our database call */ $sql = 'SELECT id FROM ' . GFFormsModel::get_lead_table_name() . ' WHERE status = "active"'; if ($lid === false) { $sql .= ' AND form_id = ' . $atts['form_id']; } else { $sql .= ' AND id = ' . (int) $lid; } if (isset($atts['show']) && $atts['show'] == 'user' || $lid !== false) { $user_id = get_current_user_id(); $sql .= ' AND created_by = ' . $user_id; } if ($atts['payment'] == 1) { $sql .= ' AND (payment_status = "Approved" OR payment_status IS NULL)'; } return $wpdb->get_results($sql); }
public static function is_valid_table($table_name) { global $wpdb; $tables = array(GFFormsModel::get_form_table_name(), GFFormsModel::get_form_view_table_name(), GFFormsModel::get_meta_table_name(), GFFormsModel::get_lead_table_name(), GFFormsModel::get_lead_notes_table_name(), GFFormsModel::get_lead_details_table_name(), GFFormsModel::get_lead_details_long_table_name(), GFFormsModel::get_lead_meta_table_name(), GFFormsModel::get_incomplete_submissions_table_name(), "{$wpdb->prefix}gf_addon_feed", "{$wpdb->prefix}gf_addon_payment_transaction", "{$wpdb->prefix}gf_addon_payment_callback"); return in_array($table_name, $tables); }
public static function count_search_leads($form_id, $search_criteria = array()) { global $wpdb; $detail_table_name = GFFormsModel::get_lead_details_table_name(); $lead_table_name = GFFormsModel::get_lead_table_name(); $where = self::get_search_where($form_id, $search_criteria); $sql = "SELECT count(distinct l.id)\n FROM {$lead_table_name} l\n INNER JOIN {$detail_table_name} ld ON l.id = ld.lead_id\n {$where}\n "; return $wpdb->get_var($sql); }
/** * Deletes all rows in the lead table that don't have corresponding rows in the details table. * * @since 2.0.0 * @access public * @static * @global $wpdb */ public static function delete_orphaned_entries() { global $wpdb; GFCommon::log_debug(__METHOD__ . '(): Starting to delete orphaned entries'); $lead_table = GFFormsModel::get_lead_table_name(); $lead_details_table = GFFormsModel::get_lead_details_table_name(); $sql = "DELETE FROM {$lead_table} WHERE id NOT IN( SELECT lead_id FROM {$lead_details_table} )"; $result = $wpdb->query($sql); GFCommon::log_debug(__METHOD__ . '(): Delete result: ' . print_r($result, true)); }
public static function count_search_leads($form_id, $search_criteria) { global $wpdb; if (!is_numeric($form_id)) { return ""; } $detail_table_name = GFFormsModel::get_lead_details_table_name(); $lead_table_name = GFFormsModel::get_lead_table_name(); $where_arr = array(); $search_where = self::get_search_where($form_id, $search_criteria); if (!empty($search_where)) { $where_arr[] = $search_where; } $info_search_where = self::get_info_search_where($search_criteria); if (!empty($info_search_where)) { $where_arr[] = $info_search_where; } $form_id_where = $form_id > 0 ? $wpdb->prepare("l.form_id=%d", $form_id) : ""; if (!empty($form_id_where)) { $where_arr[] = $form_id_where; } $where = empty($where_arr) ? "" : "WHERE " . join($where_arr, " AND "); $sql = "SELECT count(distinct l.id)\n FROM {$lead_table_name} l\n INNER JOIN {$detail_table_name} ld ON l.id = ld.lead_id\n {$where}\n "; return $wpdb->get_var($sql); }
/** * 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'", mysql_real_escape_string($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"] : 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'", mysql_real_escape_string($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'", mysql_real_escape_string($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"]) ? mysql_real_escape_string($entry["created_by"]) : ""; if (empty($user_id)) { $user_id = $current_user && $current_user->ID ? $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) { if (in_array($field["type"], array("html", "page", "section"))) { continue; } if (isset($field["inputs"]) && is_array($field["inputs"])) { foreach ($field["inputs"] as $input) { $input_id = $input["id"]; if (isset($entry[(string) $input_id])) { $result = GFFormsModel::update_lead_field_value($form, $entry, $field, 0, $input_id, $entry[(string) $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]); } } } return $entry_id; }
/** * 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 * * @param array $entry * @param array $form */ do_action('gform_post_add_entry', $entry, $form); return $entry_id; }
/** * Updates a form id of an entry. * * @param int $entry_id The ID of the Entry object * @param int $form_id The Form ID of the Entry object * * @param mixed $value The value to which the field should be set * * @return bool Whether the entry property was updated successfully */ function update_entry_form_id($entry_id, $form_id) { global $wpdb; $lead_table = GFFormsModel::get_lead_table_name(); $lead_detail_table = GFFormsModel::get_lead_details_table_name(); $lead_meta_table = GFFormsModel::get_lead_meta_table_name(); $result = $wpdb->query($wpdb->prepare("UPDATE {$lead_table} SET form_id={$form_id} WHERE id=%d ", $entry_id)); $wpdb->query($wpdb->prepare("UPDATE {$lead_detail_table} SET form_id={$form_id} WHERE lead_id=%d ", $entry_id)); $wpdb->query($wpdb->prepare("UPDATE {$lead_meta_table} SET form_id={$form_id} WHERE lead_id=%d ", $entry_id)); return $result; }
function sort_by_field_count($form_id, $searching) { global $wpdb; $search_value = isset($searching['value']) ? $searching['value'] : ''; $lead_detail_table_name = GFFormsModel::get_lead_details_table_name(); $lead_table_name = GFFormsModel::get_lead_table_name(); $search_160 = "(field_number BETWEEN '159.9999' AND '160.9999' AND value like ( '%{$search_value}%' ))"; $search_158 = "(field_number BETWEEN '157.9999' AND '158.9999' AND value like ( '%{$search_value}%' ))"; $search_155 = "(field_number BETWEEN '154.9999' AND '155.9999' AND value like ( '%{$search_value}%' ))"; $search_166 = "(field_number BETWEEN '165.9999' AND '166.9999' AND value like ( '%{$search_value}%' ))"; $search_157 = "(field_number BETWEEN '156.9999' AND '157.9999' AND value like ( '%{$search_value}%' ))"; $search_159 = "(field_number BETWEEN '158.9999' AND '159.9999' AND value like ( '%{$search_value}%' ))"; $search_154 = "(field_number BETWEEN '153.9999' AND '154.9999' AND value like ( '%{$search_value}%' ))"; $search_109 = "(field_number BETWEEN '108.9999' AND '109.9999' AND value like ( '%{$search_value}%' ))"; $search_151 = "(field_number BETWEEN '150.9999' AND '151.9999' AND value like ( '%{$search_value}%' ))"; $search_16 = "(field_number BETWEEN '15.9999' AND '16.9999' AND value like ( '%{$search_value}%' ))"; $accepted_criteria = "(field_number BETWEEN '302.9999' AND '303.9999' AND value = 'Accepted' )"; $sql = "SELECT\n\tcount(distinct l.lead_id) as total_count\n\tfrom {$lead_detail_table_name} as l\n\tINNER JOIN \n\t\t\t\t (\n\t\t\t\t SELECT\n\t\t\t\t\t\tlead_id as id\n\t\t\t\t\t\tfrom {$lead_detail_table_name}\n\t\t\t\t\t\tWHERE {$accepted_criteria}\n\t\t\t\t\t\tAND form_id in ({$form_id}) \n\t\t\t\t\t\t) accepted on l.lead_id=accepted.id\n\tWHERE ( {$search_160} OR {$search_158} OR {$search_155} OR {$search_166} OR {$search_157} OR {$search_159} OR {$search_154} OR {$search_109} OR {$search_151} OR {$search_16})\n\tAND form_id in ({$form_id}) \n\t"; return $sql; }
function get_counts($args) { if (!empty($args['field_filters'])) { if (isset($args['form-id'])) { $form_ids = absint($args['form-id']); } else { $form_ids = $this->get_workflow_form_ids(); } $results = new stdClass(); $results->total = 0; $results->pending = 0; $results->complete = 0; $results->cancelled = 0; if (empty($form_ids)) { $this->items = array(); return $results; } $base_search_criteria = $this->get_search_criteria(); $pending_search_criteria = $base_search_criteria; $pending_search_criteria['field_filters'][] = array('key' => 'workflow_final_status', 'value' => 'pending'); $complete_search_criteria = $base_search_criteria; $complete_search_criteria['field_filters'][] = array('key' => 'workflow_final_status', 'operator' => 'not in', 'value' => array('pending', 'cancelled')); $cancelled_search_criteria = $base_search_criteria; $cancelled_search_criteria['field_filters'][] = array('key' => 'workflow_final_status', 'value' => 'cancelled'); $results->total = GFAPI::count_entries($form_ids, $base_search_criteria); $results->pending = GFAPI::count_entries($form_ids, $pending_search_criteria); $results->complete = GFAPI::count_entries($form_ids, $complete_search_criteria); $results->cancelled = GFAPI::count_entries($form_ids, $cancelled_search_criteria); return $results; } global $wpdb; if (!empty($args['form-id'])) { $form_clause = ' AND l.form_id=' . absint($args['form-id']); } else { $form_ids = $this->get_workflow_form_ids(); if (empty($form_ids)) { $results = new stdClass(); $results->pending = 0; $results->complete = 0; $results->cancelled = 0; return $results; } $form_clause = ' AND l.form_id IN(' . join(',', $form_ids) . ')'; } $start_clause = ''; if (!empty($args['start-date'])) { $start_clause = $wpdb->prepare(' AND l.date_created >= %s', $args['start-date']); } $end_clause = ''; if (!empty($args['end-date'])) { $end_clause = $wpdb->prepare(' AND l.date_created <= %s', $args['end-date']); } $user_id_clause = ''; if (!$this->display_all) { $user = wp_get_current_user(); $user_id_clause = $wpdb->prepare(' AND created_by=%d', $user->ID); } $lead_table = GFFormsModel::get_lead_table_name(); $meta_table = GFFormsModel::get_lead_meta_table_name(); $sql = "SELECT\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l WHERE l.status='active' {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as total,\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l INNER JOIN {$meta_table} m ON l.id = m.lead_id WHERE l.status='active' AND meta_key='workflow_final_status' AND meta_value='pending' {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as pending,\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l INNER JOIN {$meta_table} m ON l.id = m.lead_id WHERE l.status='active' AND meta_key='workflow_final_status' AND meta_value NOT IN('pending', 'cancelled') {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as complete,\n\t\t(SELECT count(distinct(l.id)) FROM {$lead_table} l INNER JOIN {$meta_table} m ON l.id = m.lead_id WHERE l.status='active' AND meta_key='workflow_final_status' AND meta_value='cancelled' {$form_clause} {$start_clause} {$end_clause} {$user_id_clause}) as cancelled\n\t\t"; $results = $wpdb->get_results($sql); return $results[0]; }
public function maybe_process_queued_entries() { $this->log_debug(__METHOD__ . '(): starting'); $form_ids = $this->get_workflow_form_ids(); if (empty($form_ids)) { return; } global $wpdb; $lead_table = GFFormsModel::get_lead_table_name(); $meta_table = GFFormsModel::get_lead_meta_table_name(); $sql = "\nSELECT l.id, l.form_id\nFROM {$lead_table} l\nINNER JOIN {$meta_table} m\nON l.id = m.lead_id\nAND l.status='active'\nAND m.meta_key LIKE 'workflow_step_status_%'\nAND m.meta_value='queued'"; $results = $wpdb->get_results($sql); if (empty($results) || is_wp_error($results)) { return; } $this->log_debug(__METHOD__ . '() Queued entries: ' . print_r($results, true)); foreach ($results as $result) { $form = GFAPI::get_form($result->form_id); $entry = GFAPI::get_entry($result->id); $step = $this->get_current_step($form, $entry); if ($step) { $complete = $step->start(); if ($complete) { $this->process_workflow($form, $entry['id']); } else { $this->log_debug(__METHOD__ . '() queued entry started step but step is not complete: ' . $entry['id']); } } else { $this->log_debug(__METHOD__ . '() queued entry not on a step: ' . $entry['id']); } } }
function sort_by_field_count($form_id, $searching) { global $wpdb; $search_value = isset($searching['value']) ? $searching['value'] : ''; $search_key = isset($searching['key']) ? $searching['key'] : ''; $searchfield_number_min = $search_key - 0.0001; $searchfield_number_max = $search_key + 0.9999; $lead_detail_table_name = GFFormsModel::get_lead_details_table_name(); $lead_table_name = GFFormsModel::get_lead_table_name(); $accepted_criteria = "(field_number BETWEEN '302.9999' AND '303.9999' AND value = 'Accepted' )"; $sql = "SELECT count( distinct accepted.id ) as total_count\n\t\t\t\t\t\tfrom {$lead_detail_table_name}\n\t\t\t\t\t\tINNER JOIN \n\t\t\t\t (\n\t\t\t\t SELECT\n\t\t\t\t\t\tlead_id as id\n\t\t\t\t\t\tfrom {$lead_detail_table_name}\n\t\t\t\t\t\tWHERE {$accepted_criteria}\n\t\t\t\t\t\tAND form_id in ({$form_id}) \n\t\t\t\t\t\t) accepted on {$lead_detail_table_name}.lead_id=accepted.id\n\t\t\t\t\t\tWHERE (field_number BETWEEN '{$searchfield_number_min}' AND '{$searchfield_number_max}' AND value IN ( '{$search_value}' ))\n\t\t\t\t\t\tAND form_id in ({$form_id})\n\t"; return $sql; }