/** * If a form has list fields, add the columns to the field picker * * @since 1.17 * * @param array $fields Associative array of fields, with keys as field type * @param array $form GF Form array * @param bool $include_parent_field Whether to include the parent field when getting a field with inputs * * @return array $fields with list field columns added, if exist. Unmodified if form has no list fields. */ function add_form_fields($fields = array(), $form = array(), $include_parent_field = true) { $list_fields = GFAPI::get_fields_by_type($form, 'list'); // Add the list columns foreach ($list_fields as $list_field) { if (empty($list_field->enableColumns)) { continue; } $list_columns = array(); foreach ((array) $list_field->choices as $key => $input) { $input_id = sprintf('%d.%d', $list_field->id, $key); // {field_id}.{column_key} $list_columns[$input_id] = array('label' => rgar($input, 'text'), 'customLabel' => '', 'parent' => $list_field, 'type' => rgar($list_field, 'type'), 'adminLabel' => rgar($list_field, 'adminLabel'), 'adminOnly' => rgar($list_field, 'adminOnly')); } // If there are columns, add them under the parent field if (!empty($list_columns)) { $index = array_search($list_field->id, array_keys($fields)) + 1; /** * Merge the $list_columns into the $fields array at $index * @see https://stackoverflow.com/a/1783125 */ $fields = array_slice($fields, 0, $index, true) + $list_columns + array_slice($fields, $index, null, true); } unset($list_columns, $index, $input_id); } return $fields; }
/** * We need to remove the $value used by Gravity Forms so it instead checks for the $_POST field values * * In ~line 541, this code would be used if we didn't override in this method: * * `if (RG_CURRENT_VIEW == "entry" && $value){` * * We don't want that code (with the download/delete icons). So unsetting the $_POST here forces using the "sign again" code instead. * * @see GFSignature::signature_input * * @param array $form GF form array * @param int $entry_id Entry ID being edited */ function after_edit_entry($form, $entry_id) { $signature_fields = GFAPI::get_fields_by_type($form, 'signature'); foreach ($signature_fields as $field) { unset($_POST["input_{$field->id}"]); } }
/** * Update the post categories based on all post category fields * * @since 1.17 * * @param array $form Gravity Forms form array * @param int $entry_id Numeric ID of the entry that was updated * * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. false if there are no post category fields or connected post. */ public function set_post_categories($form = array(), $entry_id = 0) { $entry = GFAPI::get_entry($entry_id); $post_id = rgar($entry, 'post_id'); if (empty($post_id)) { return false; } $return = false; $post_category_fields = GFAPI::get_fields_by_type($form, 'post_category'); if ($post_category_fields) { $updated_categories = array(); foreach ($post_category_fields as $field) { // Get the value of the field, including $_POSTed value $field_cats = RGFormsModel::get_field_value($field); $field_cats = is_array($field_cats) ? array_values($field_cats) : (array) $field_cats; $field_cats = gv_map_deep($field_cats, 'intval'); $updated_categories = array_merge($updated_categories, array_values($field_cats)); } // Remove `0` values from intval() $updated_categories = array_filter($updated_categories); /** * @filter `gravityview/edit_entry/post_categories/append` Should post categories be added to or replaced? * @since 1.17 * @param bool $append If `true`, don't delete existing categories, just add on. If `false`, replace the categories with the submitted categories. Default: `false` */ $append = apply_filters('gravityview/edit_entry/post_categories/append', false); $return = wp_set_post_categories($post_id, $updated_categories, $append); } return $return; }
/** * If a form has quiz fields, add the fields to the field picker * * @since 1.17 * * @param array $fields Associative array of fields, with keys as field type * @param array $form GF Form array * @param bool $include_parent_field Whether to include the parent field when getting a field with inputs * * @return array $fields with quiz fields added, if exist. Unmodified if form has no quiz fields. */ function add_form_fields($fields = array(), $form = array(), $include_parent_field = true) { $quiz_fields = GFAPI::get_fields_by_type($form, 'quiz'); if (!empty($quiz_fields)) { $fields['gquiz_score'] = array('label' => __('Quiz Score Total', 'gravityview'), 'type' => 'quiz_score', 'desc' => __('Displays the number of correct Quiz answers the user submitted.', 'gravityview')); $fields['gquiz_percent'] = array('label' => __('Quiz Percentage Grade', 'gravityview'), 'type' => 'quiz_percent', 'desc' => __('Displays the percentage of correct Quiz answers the user submitted.', 'gravityview')); $fields['gquiz_grade'] = array('label' => __('Quiz Letter Grade', 'gravityview'), 'type' => 'quiz_grade', 'desc' => __('Displays the Grade the user achieved based on Letter Grading configured in the Quiz Settings.', 'gravityview')); $fields['gquiz_is_pass'] = array('label' => __('Quiz Pass/Fail', 'gravityview'), 'type' => 'quiz_is_pass', 'desc' => __('Displays either Passed or Failed based on the Pass/Fail settings configured in the Quiz Settings.', 'gravityview')); } return $fields; }
public function upgrade($previous_version) { if (version_compare($previous_version, '1.0.5') == -1) { $forms = GFAPI::get_forms(true); foreach ($forms as $form) { $entries = GFAPI::get_entries($form['id']); $fields = GFAPI::get_fields_by_type($form, 'repeater'); foreach ($entries as $entry) { foreach ($fields as $field) { if (array_key_exists($field['id'], $entry)) { $dataArray = GFFormsModel::unserialize($entry[$field['id']]); $dataUpdated = false; if (!is_array($dataArray)) { continue; } foreach ($dataArray as $repeaterChildId => $repeaterChild) { foreach ($repeaterChild as $repeatedFieldId => $repeatedField) { if (!is_array($repeatedField)) { if ($repeatedField !== '[gfRepeater-section]') { $dataUpdated = true; $dataArray[$repeaterChildId][$repeatedFieldId] = array($repeatedField); } } elseif (reset($repeatedField) == '[gfRepeater-section]') { $dataUpdated = true; $dataArray[$repeaterChildId][$repeatedFieldId] = reset($repeatedField); } } } if ($dataUpdated) { GFAPI::update_entry_field($entry['id'], $field['id'], maybe_serialize($dataArray)); } } } } } } }
public function contacts_tab($contact_id) { if (false === empty($contact_id)) { $search_criteria['status'] = 'active'; $search_criteria['field_filters'][] = array('type' => 'meta', 'key' => 'gcontacts_contact_id', 'value' => $contact_id); $form_ids = array(); $forms = GFFormsModel::get_forms(true); foreach ($forms as $form) { $form_meta = GFFormsModel::get_form_meta($form->id); $poll_fields = GFAPI::get_fields_by_type($form_meta, array('poll')); if (!empty($poll_fields)) { $form_ids[] = $form->id; } } if (empty($form_ids)) { return; } $entries = GFAPI::get_entries($form_ids, $search_criteria); if (empty($entries)) { esc_html_e('This contact has not submitted any poll entries yet.', 'gravityformspolls'); } else { ?> <h3><span><?php esc_html_e('Poll Entries', 'gravityformspolls'); ?> </span></h3> <div> <table id="gcontacts-entry-list" class="widefat"> <tr class="gcontacts-entries-header"> <td> <?php esc_html_e('Entry Id', 'gravityformspolls'); ?> </td> <td> <?php esc_html_e('Date', 'gravityformspolls'); ?> </td> <td> <?php esc_html_e('Form', 'gravityformspolls'); ?> </td> </tr> <?php foreach ($entries as $entry) { $form_id = $entry['form_id']; $form = GFFormsModel::get_form_meta($form_id); $form_title = rgar($form, 'title'); $entry_id = $entry['id']; $entry_date = GFCommon::format_date(rgar($entry, 'date_created'), false); $entry_url = admin_url("admin.php?page=gf_entries&view=entry&id={$form_id}&lid={$entry_id}"); ?> <tr> <td> <a href="<?php echo $entry_url; ?> "><?php echo $entry_id; ?> </a> </td> <td> <?php echo $entry_date; ?> </td> <td> <?php echo $form_title; ?> </td> </tr> <?php } ?> </table> </div> <?php } } }
/** * Returns the field ID of the first field of the desired type. * * @access public * @param string $field_type * @param int $subfield_id (default: null) * @param int $form_id (default: null) * @return string */ public function get_first_field_by_type($field_type, $subfield_id = null, $form_id = null, $return_first_only = true) { /* Get the current form ID. */ if (rgblank($form_id)) { $form_id = rgget('id'); } /* Get the form. */ $form = GFAPI::get_form($form_id); /* Get the request field type for the form. */ $fields = GFAPI::get_fields_by_type($form, array($field_type)); if (count($fields) == 0 || count($fields) > 1 && $return_first_only) { return null; } else { if (rgblank($subfield_id)) { return $fields[0]->id; } else { return $fields[0]->id . '.' . $subfield_id; } } }
protected function get_payment_choices($form) { $fields = GFAPI::get_fields_by_type($form, array('product')); $choices = array(array('label' => esc_html__('Select a product field', 'gravityforms'), 'value' => '')); foreach ($fields as $field) { $field_id = $field['id']; $field_label = RGFormsModel::get_label($field); $choices[] = array('value' => $field_id, 'label' => $field_label); } return $choices; }
private function _field_condition_matches($field_types, $form) { if (!is_array($field_types)) { $field_types = array($field_types); } $fields = GFAPI::get_fields_by_type($form, $field_types); if (count($fields) > 0) { return true; } return false; }
public static function delete_passwords($entry, $form) { $password_fields = GFAPI::get_fields_by_type($form, array('password')); foreach ($password_fields as $password_field) { GFAPI::update_entry_field($entry['id'], $password_field['id'], ''); } }
public static function has_multifile_fileupload_field($form) { $fileupload_fields = GFAPI::get_fields_by_type($form, array('fileupload', 'post_custom_field')); if (is_array($fileupload_fields)) { foreach ($fileupload_fields as $field) { if ($field->multipleFiles) { return true; } } } return false; }
/** * Remove the survey score from the entry meta conditional logic filters on the notifications/confirmations pages. * * @param array $filters The array of filters. * @param array $form The current form object. * @param string $id The ID of the notification/confirmation being edited. * * @return mixed */ public function conditional_logic_filters($filters, $form, $id) { $survey_fields = GFAPI::get_fields_by_type($form, array('survey')); if (empty($survey_fields)) { return $filters; } if (false === $this->scoring_enabled($form)) { unset($filters['gsurvey_score']); } return $filters; }
private static function get_field_row_count($form, $exported_field_ids, $entry_count) { $list_fields = GFAPI::get_fields_by_type($form, array('list'), true); //only getting fields that have been exported $field_ids = ''; foreach ($list_fields as $field) { if (in_array($field->id, $exported_field_ids) && $field->enableColumns) { $field_ids .= $field->id . ','; } } if (empty($field_ids)) { return array(); } $field_ids = substr($field_ids, 0, strlen($field_ids) - 1); $page_size = 200; $offset = 0; $row_counts = array(); global $wpdb; $go_to_next_page = true; while ($go_to_next_page) { $sql = "SELECT d.field_number as field_id, ifnull(l.value, d.value) as value\n FROM {$wpdb->prefix}rg_lead_detail d\n LEFT OUTER JOIN {$wpdb->prefix}rg_lead_detail_long l ON d.id = l.lead_detail_id\n WHERE d.form_id={$form['id']} AND cast(d.field_number as decimal) IN ({$field_ids})\n LIMIT {$offset}, {$page_size}"; $results = $wpdb->get_results($sql, ARRAY_A); foreach ($results as $result) { $list = unserialize($result['value']); $current_row_count = isset($row_counts[$result['field_id']]) ? intval($row_counts[$result['field_id']]) : 0; if (is_array($list) && count($list) > $current_row_count) { $row_counts[$result['field_id']] = count($list); } } $offset += $page_size; $go_to_next_page = count($results) == $page_size; } return $row_counts; }
private static function has_price_field($form) { $price_fields = GFAPI::get_fields_by_type($form, array('product', 'donation')); $has_price_field = !empty($price_fields); return gf_apply_filters('gform_has_price_field', $form['id'], $has_price_field, $form); }
/** * Output the Quiz results in a postbox div. * * @param array $form The current form. * @param array $entry The current entry. */ public function entry_results($form, $entry) { $fields = GFAPI::get_fields_by_type($form, array('quiz')); $count_quiz_fields = count($fields); if ($count_quiz_fields == 0) { return; } $grading = $this->get_form_setting($form, 'grading'); $score = rgar($entry, 'gquiz_score'); $percent = rgar($entry, 'gquiz_percent'); $is_pass = rgar($entry, 'gquiz_is_pass'); $grade = rgar($entry, 'gquiz_grade'); $max_score = $this->get_max_score($form); ?> <div id="gquiz-entry-detail-score-info-container" class="postbox"> <h3 style="cursor: default;"><?php esc_html_e('Quiz Results', 'gravityformsquiz'); ?> </h3> <div id="gquiz-entry-detail-score-info"> <?php echo $this->get_results_panel_markup($form, $entry); ?> </div> </div> <?php }
/** * Used by the gform_delete_entries hook to delete any signatures for the entries currently being deleted. * * @param int $form_id The ID of the form for which the entries are being deleted. */ public function delete_entries($form_id) { $form = RGFormsModel::get_form_meta($form_id); $signature_fields = GFAPI::get_fields_by_type($form, 'signature'); if (!empty($signature_fields)) { global $wpdb; $lead_details_table_name = GFFormsModel::get_lead_details_table_name(); foreach ($signature_fields as $field) { $input_id_min = (double) $field->id - 0.0001; $input_id_max = (double) $field->id + 0.0001; $filenames = $wpdb->get_col($wpdb->prepare("SELECT value FROM {$lead_details_table_name} WHERE form_id=%d AND field_number BETWEEN %s AND %s", $form_id, $input_id_min, $input_id_max)); if (is_array($filenames)) { foreach ($filenames as $filename) { $this->delete_signature_file($filename); } } } } }
public static function delete_password($entry, $form) { $password_fields = GFAPI::get_fields_by_type($form, array('password')); if (is_array($password_fields)) { foreach ($password_fields as $password_field) { $entry[$password_field->id] = ''; } } GFAPI::update_entry($entry); return $entry; }
/** * Check whether a form has product fields * * @since 1.16 * * @param array $form Gravity Forms form array * * @return bool|GF_Field[] */ public static function has_product_field($form = array()) { $product_fields = apply_filters('gform_product_field_types', array('option', 'quantity', 'product', 'total', 'shipping', 'calculation', 'price')); $fields = GFAPI::get_fields_by_type($form, $product_fields); return empty($fields) ? false : $fields; }
/** * Get form file fields for feed field settings. * * @access public * @param string $module (default: 'contact') * @return array $fields */ public function get_file_fields_for_feed_setting($module = 'contact') { /* Setup choices array. */ $choices = array(); /* Get the form. */ $form = GFAPI::get_form(rgget('id')); /* Get file fields for the form. */ $file_fields = GFAPI::get_fields_by_type($form, array('fileupload'), true); if (!empty($file_fields)) { foreach ($file_fields as $field) { $choices[] = array('name' => $module . 'Attachments[' . $field->id . ']', 'label' => $field->label, 'default_value' => 0); } } return $choices; }
private static function has_price_field($form) { $price_fields = GFAPI::get_fields_by_type($form, array('product', 'donation')); return !empty($price_fields); }