Exemple #1
0
 /**
  * Add or edit a single pod item
  *
  * $params['pod'] string The Pod name (pod or pod_id is required)
  * $params['pod_id'] string The Pod ID (pod or pod_id is required)
  * $params['id'] int The item ID
  * $params['data'] array (optional) Associative array of field names + values
  * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
  * $params['track_changed_fields'] bool Set to true to enable tracking of saved fields via PodsAPI::get_changed_fields()
  *
  * @param array|object $params An associative array of parameters
  *
  * @return int The item ID
  *
  * @since 1.7.9
  */
 public function save_pod_item($params)
 {
     global $wpdb;
     $params = (object) pods_str_replace('@wp_', '{prefix}', $params);
     $tableless_field_types = PodsForm::tableless_field_types();
     $repeatable_field_types = PodsForm::repeatable_field_types();
     $simple_tableless_objects = PodsForm::simple_tableless_objects();
     // @deprecated 2.0
     if (isset($params->datatype)) {
         pods_deprecated('$params->pod instead of $params->datatype', '2.0');
         $params->pod = $params->datatype;
         unset($params->datatype);
         if (isset($params->pod_id)) {
             pods_deprecated('$params->id instead of $params->pod_id', '2.0');
             $params->id = $params->pod_id;
             unset($params->pod_id);
         }
         if (isset($params->data) && !empty($params->data) && is_array($params->data)) {
             $check = current($params->data);
             if (is_array($check)) {
                 pods_deprecated('PodsAPI::save_pod_items', '2.0');
                 return $this->save_pod_items($params, $params->data);
             }
         }
     }
     // @deprecated 2.0
     if (isset($params->tbl_row_id)) {
         pods_deprecated('$params->id instead of $params->tbl_row_id', '2.0');
         $params->id = $params->tbl_row_id;
         unset($params->tbl_row_id);
     }
     // @deprecated 2.0
     if (isset($params->columns)) {
         pods_deprecated('$params->data instead of $params->columns', '2.0');
         $params->data = $params->columns;
         unset($params->columns);
     }
     if (!isset($params->pod)) {
         $params->pod = false;
     }
     if (isset($params->pod_id)) {
         $params->pod_id = pods_absint($params->pod_id);
     } else {
         $params->pod_id = 0;
     }
     if (isset($params->id)) {
         $params->id = pods_absint($params->id);
     } else {
         $params->id = 0;
     }
     if (!isset($params->from)) {
         $params->from = 'save';
     }
     if (!isset($params->location)) {
         $params->location = null;
     }
     if (!isset($params->track_changed_fields)) {
         $params->track_changed_fields = false;
     }
     /**
      * Override $params['track_changed_fields']
      *
      * Use for globally setting field change tracking.
      *
      * @param bool
      *
      * @since 2.3.19
      */
     $track_changed_fields = apply_filters('pods_api_save_pod_item_track_changed_fields_' . $params->pod, (bool) $params->track_changed_fields, $params);
     $changed_fields = array();
     if (!isset($params->clear_slug_cache)) {
         $params->clear_slug_cache = true;
     }
     // Support for bulk edit
     if (isset($params->id) && !empty($params->id) && is_array($params->id)) {
         $ids = array();
         $new_params = $params;
         foreach ($params->id as $id) {
             $new_params->id = $id;
             $ids[] = $this->save_pod_item($new_params);
         }
         return $ids;
     }
     // Allow Helpers to know what's going on, are we adding or saving?
     $is_new_item = false;
     if (empty($params->id)) {
         $is_new_item = true;
     }
     if (isset($params->is_new_item)) {
         $is_new_item = (bool) $params->is_new_item;
     }
     // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
     $bypass_helpers = false;
     if (isset($params->bypass_helpers) && false !== $params->bypass_helpers) {
         $bypass_helpers = true;
     }
     // Allow Custom Fields not defined by Pods to be saved
     $allow_custom_fields = false;
     if (isset($params->allow_custom_fields) && false !== $params->allow_custom_fields) {
         $allow_custom_fields = true;
     }
     // Get array of Pods
     $pod = $this->load_pod(array('id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true));
     if (false === $pod) {
         return pods_error(__('Pod not found', 'pods'), $this);
     }
     $params->pod = $pod['name'];
     $params->pod_id = $pod['id'];
     if ('settings' == $pod['type']) {
         $params->id = $pod['id'];
     }
     $fields = $pod['fields'];
     $object_fields = (array) pods_var_raw('object_fields', $pod, array(), null, true);
     $fields_active = array();
     $custom_data = array();
     // Find the active fields (loop through $params->data to retain order)
     if (!empty($params->data) && is_array($params->data)) {
         $custom_fields = array();
         foreach ($params->data as $field => $value) {
             if (isset($object_fields[$field])) {
                 $object_fields[$field]['value'] = $value;
                 $fields_active[] = $field;
             } elseif (isset($fields[$field])) {
                 if ('save' == $params->from || true === PodsForm::permission($fields[$field]['type'], $field, $fields[$field], $fields, $pod, $params->id, $params)) {
                     $fields[$field]['value'] = $value;
                     $fields_active[] = $field;
                 } elseif (!pods_has_permissions($fields[$field]['options']) && pods_var('hidden', $fields[$field]['options'], false)) {
                     $fields[$field]['value'] = $value;
                     $fields_active[] = $field;
                 }
             } else {
                 $found = false;
                 foreach ($object_fields as $object_field => $object_field_opt) {
                     if (in_array($field, $object_field_opt['alias'])) {
                         $object_fields[$object_field]['value'] = $value;
                         $fields_active[] = $object_field;
                         $found = true;
                         break;
                     }
                 }
                 if ($allow_custom_fields && !$found) {
                     $custom_fields[] = $field;
                 }
             }
         }
         if ($allow_custom_fields && !empty($custom_fields)) {
             foreach ($custom_fields as $field) {
                 $custom_data[$field] = $params->data[$field];
             }
         }
         if ($pod['type'] === 'taxonomy' && isset($params->data) && !empty($params->data)) {
             $term_data = $params->data;
         }
         unset($params->data);
     }
     if (empty($params->id) && !in_array('created', $fields_active) && isset($fields['created']) && in_array($fields['created']['type'], array('date', 'datetime'))) {
         $fields['created']['value'] = current_time('mysql');
         $fields_active[] = 'created';
     }
     if (!in_array('modified', $fields_active) && isset($fields['modified']) && in_array($fields['modified']['type'], array('date', 'datetime'))) {
         $fields['modified']['value'] = current_time('mysql');
         $fields_active[] = 'modified';
     }
     if (in_array($pod['type'], array('pod', 'table')) && empty($params->id) && !empty($pod['pod_field_index']) && in_array($pod['pod_field_index'], $fields_active) && !in_array($pod['pod_field_slug'], $fields_active) && isset($fields[$pod['pod_field_slug']])) {
         $fields[$pod['pod_field_slug']]['value'] = '';
         // this will get picked up by slug pre_save method
         $fields_active[] = $pod['pod_field_slug'];
     }
     // Handle hidden fields
     if (empty($params->id)) {
         foreach ($fields as $field => $field_data) {
             if (in_array($field, $fields_active)) {
                 continue;
             }
             if (in_array($params->from, array('save', 'process_form')) || true === PodsForm::permission($fields[$field]['type'], $field, $fields[$field], $fields, $pod, $params->id, $params)) {
                 $value = PodsForm::default_value(pods_var_raw($field, 'post'), $field_data['type'], $field, pods_var_raw('options', $field_data, $field_data, null, true), $pod, $params->id);
                 if (null !== $value && '' !== $value && false !== $value) {
                     $fields[$field]['value'] = $value;
                     $fields_active[] = $field;
                 }
             }
         }
         // Set default field values for object fields
         if (!empty($object_fields)) {
             foreach ($object_fields as $field => $field_data) {
                 if (in_array($field, $fields_active)) {
                     continue;
                 } elseif (!isset($field_data['default']) || strlen($field_data['default']) < 1) {
                     continue;
                 }
                 $value = PodsForm::default_value(pods_var_raw($field, 'post'), $field_data['type'], $field, pods_var_raw('options', $field_data, $field_data, null, true), $pod, $params->id);
                 if (null !== $value && '' !== $value && false !== $value) {
                     $object_fields[$field]['value'] = $value;
                     $fields_active[] = $field;
                 }
             }
         }
         // Set default field values for Pod fields
         foreach ($fields as $field => $field_data) {
             if (in_array($field, $fields_active)) {
                 continue;
             } elseif (!isset($field_data['default']) || strlen($field_data['default']) < 1) {
                 continue;
             }
             $value = PodsForm::default_value(pods_var_raw($field, 'post'), $field_data['type'], $field, pods_var_raw('options', $field_data, $field_data, null, true), $pod, $params->id);
             if (null !== $value && '' !== $value && false !== $value) {
                 $fields[$field]['value'] = $value;
                 $fields_active[] = $field;
             }
         }
     }
     $columns =& $fields;
     // @deprecated 2.0
     $active_columns =& $fields_active;
     // @deprecated 2.0
     $params->tbl_row_id =& $params->id;
     // @deprecated 2.0
     $pre_save_helpers = $post_save_helpers = array();
     $pieces = array('fields', 'params', 'pod', 'fields_active', 'object_fields', 'custom_fields', 'custom_data', 'track_changed_fields', 'changed_fields');
     if (false === $bypass_helpers) {
         // Plugin hooks
         $hooked = $this->do_hook('pre_save_pod_item', compact($pieces), $is_new_item, $params->id);
         if (is_array($hooked) && !empty($hooked)) {
             extract($hooked);
         }
         $hooked = $this->do_hook("pre_save_pod_item_{$params->pod}", compact($pieces), $is_new_item, $params->id);
         if (is_array($hooked) && !empty($hooked)) {
             extract($hooked);
         }
         if ($is_new_item) {
             $hooked = $this->do_hook('pre_create_pod_item', compact($pieces));
             if (is_array($hooked) && !empty($hooked)) {
                 extract($hooked);
             }
             $hooked = $this->do_hook("pre_create_pod_item_{$params->pod}", compact($pieces));
             if (is_array($hooked) && !empty($hooked)) {
                 extract($hooked);
             }
         } else {
             $hooked = $this->do_hook('pre_edit_pod_item', compact($pieces), $params->id);
             if (is_array($hooked) && !empty($hooked)) {
                 extract($hooked);
             }
             $hooked = $this->do_hook("pre_edit_pod_item_{$params->pod}", compact($pieces), $params->id);
             if (is_array($hooked) && !empty($hooked)) {
                 extract($hooked);
             }
         }
         // Call any pre-save helpers (if not bypassed)
         if (!defined('PODS_DISABLE_EVAL') || !PODS_DISABLE_EVAL) {
             if (!empty($pod['options']) && is_array($pod['options'])) {
                 $helpers = array('pre_save_helpers', 'post_save_helpers');
                 foreach ($helpers as $helper) {
                     if (isset($pod['options'][$helper]) && !empty($pod['options'][$helper])) {
                         ${$helper} = explode(',', $pod['options'][$helper]);
                     }
                 }
             }
             if (!empty($pre_save_helpers)) {
                 pods_deprecated(sprintf(__('Pre-save helpers are deprecated, use the action pods_pre_save_pod_item_%s instead', 'pods'), $params->pod), '2.0');
                 foreach ($pre_save_helpers as $helper) {
                     $helper = $this->load_helper(array('name' => $helper));
                     if (false !== $helper) {
                         eval('?>' . $helper['code']);
                     }
                 }
             }
         }
     }
     if ($track_changed_fields) {
         $changed_fields = $this->get_changed_fields(compact($pieces));
     }
     $table_data = $table_formats = $update_values = $rel_fields = $rel_field_ids = array();
     $object_type = $pod['type'];
     $object_ID = 'ID';
     if ('comment' == $object_type) {
         $object_ID = 'comment_ID';
     }
     $object_data = $object_meta = $post_term_data = array();
     if ('settings' == $object_type) {
         $object_data['option_id'] = $pod['name'];
     } elseif (!empty($params->id)) {
         $object_data[$object_ID] = $params->id;
     }
     $fields_active = array_unique($fields_active);
     // Loop through each active field, validating and preparing the table data
     foreach ($fields_active as $field) {
         if (isset($object_fields[$field])) {
             $field_data = $object_fields[$field];
         } elseif (isset($fields[$field])) {
             $field_data = $fields[$field];
         } else {
             continue;
         }
         $value = $field_data['value'];
         $type = $field_data['type'];
         $options = pods_var('options', $field_data, array());
         // WPML AJAX compatibility
         if (is_admin() && isset($_GET['page']) && false !== strpos($_GET['page'], '/menu/languages.php') && isset($_POST['icl_ajx_action']) && isset($_POST['_icl_nonce']) && wp_verify_nonce($_POST['_icl_nonce'], $_POST['icl_ajx_action'] . '_nonce')) {
             $options['unique'] = $fields[$field]['options']['unique'] = $options['required'] = $fields[$field]['options']['required'] = 0;
         } else {
             // Validate value
             $validate = $this->handle_field_validation($value, $field, $object_fields, $fields, $pod, $params);
             if (false === $validate) {
                 $validate = sprintf(__('There was an issue validating the field %s', 'pods'), $field_data['label']);
             } elseif (true !== $validate) {
                 $validate = (array) $validate;
             }
             if (!is_bool($validate) && !empty($validate)) {
                 return pods_error($validate, $this);
             }
         }
         $value = PodsForm::pre_save($field_data['type'], $value, $params->id, $field, array_merge($field_data, $options), array_merge($fields, $object_fields), $pod, $params);
         $field_data['value'] = $value;
         if (isset($object_fields[$field])) {
             if ('taxonomy' == $object_fields[$field]['type']) {
                 $post_term_data[$field] = $value;
             } else {
                 $object_data[$field] = $value;
             }
         } else {
             $simple = 'pick' == $type && in_array(pods_var('pick_object', $field_data), $simple_tableless_objects);
             $simple = (bool) $this->do_hook('tableless_custom', $simple, $field_data, $field, $fields, $pod, $params);
             // Handle Simple Relationships
             if ($simple) {
                 if (!is_array($value)) {
                     $value = explode(',', $value);
                 }
                 $pick_limit = (int) pods_var_raw('pick_limit', $options, 0);
                 if ('single' == pods_var_raw('pick_format_type', $options)) {
                     $pick_limit = 1;
                 }
                 if ('custom-simple' == pods_var('pick_object', $field_data)) {
                     $custom = pods_var_raw('pick_custom', $options, '');
                     $custom = apply_filters('pods_form_ui_field_pick_custom_values', $custom, $field_data['name'], $value, array_merge($field_data, $options), $pod, $params->id);
                     if (empty($value) || empty($custom)) {
                         $value = '';
                     } elseif (!empty($custom)) {
                         if (!is_array($custom)) {
                             $custom = explode("\n", $custom);
                             $custom_values = array();
                             foreach ($custom as $c => $cv) {
                                 if (0 < strlen($cv)) {
                                     $custom_label = explode('|', $cv);
                                     if (!isset($custom_label[1])) {
                                         $custom_label[1] = $custom_label[0];
                                     }
                                     $custom_label[0] = trim((string) $custom_label[0]);
                                     $custom_label[1] = trim((string) $custom_label[1]);
                                     $custom_values[$custom_label[0]] = $custom_label[1];
                                 }
                             }
                         } else {
                             $custom_values = $custom;
                         }
                         $values = array();
                         foreach ($value as $k => $v) {
                             $v = pods_unsanitize($v);
                             if (isset($custom_values[$v])) {
                                 $values[$k] = $v;
                             }
                         }
                         $value = $values;
                     }
                 }
                 if (0 < $pick_limit && !empty($value)) {
                     $value = array_slice($value, 0, $pick_limit);
                 }
                 // Don't save an empty array, just make it an empty string
                 if (empty($value)) {
                     $value = '';
                 } elseif (is_array($value)) {
                     // If there's just one item, don't save as an array, save the string
                     if (1 == $pick_limit || 1 == count($value)) {
                         $value = implode('', $value);
                     } elseif ('table' == pods_var('storage', $pod)) {
                         $value = version_compare(PHP_VERSION, '5.4.0', '>=') ? json_encode($value, JSON_UNESCAPED_UNICODE) : json_encode($value);
                     }
                 }
             }
             // Prepare all table / meta data
             if (!in_array($type, $tableless_field_types) || $simple) {
                 if (in_array($type, $repeatable_field_types) && 1 == pods_var($type . '_repeatable', $field_data, 0)) {
                     // Don't save an empty array, just make it an empty string
                     if (empty($value)) {
                         $value = '';
                     } elseif (is_array($value)) {
                         // If there's just one item, don't save as an array, save the string
                         if (1 == count($value)) {
                             $value = implode('', $value);
                         } elseif ('table' == pods_var('storage', $pod)) {
                             $value = version_compare(PHP_VERSION, '5.4.0', '>=') ? json_encode($value, JSON_UNESCAPED_UNICODE) : json_encode($value);
                         }
                     }
                 }
                 $table_data[$field] = str_replace(array('{prefix}', '@wp_'), array('{/prefix/}', '{prefix}'), $value);
                 // Fix for pods_query
                 $table_formats[] = PodsForm::prepare($type, $options);
                 $object_meta[$field] = $value;
             } else {
                 // Convert values from a comma-separated string into an array
                 if (!is_array($value)) {
                     $value = explode(',', $value);
                 }
                 $rel_fields[$type][$field] = $value;
                 $rel_field_ids[] = $field_data['id'];
             }
         }
     }
     if ('post_type' == $pod['type']) {
         $post_type = $pod['name'];
         if (!empty($pod['object'])) {
             $post_type = $pod['object'];
         }
         $object_data['post_type'] = $post_type;
     }
     if (('meta' == $pod['storage'] || 'settings' == $pod['type']) && !in_array($pod['type'], array('taxonomy', 'pod', 'table', ''))) {
         if ($allow_custom_fields && !empty($custom_data)) {
             $object_meta = array_merge($custom_data, $object_meta);
         }
         $fields_to_send = array_flip(array_keys($object_meta));
         foreach ($fields_to_send as $field => $field_data) {
             if (isset($object_fields[$field])) {
                 $field_data = $object_fields[$field];
             } elseif (isset($fields[$field])) {
                 $field_data = $fields[$field];
             } else {
                 unset($fields_to_send[$field]);
             }
             $fields_to_send[$field] = $field_data;
         }
         $params->id = $this->save_wp_object($object_type, $object_data, $object_meta, false, true, $fields_to_send);
         if (!empty($params->id) && 'settings' == $object_type) {
             $params->id = $pod['id'];
         }
     } else {
         if (!in_array($pod['type'], array('taxonomy', 'pod', 'table', ''))) {
             $params->id = $this->save_wp_object($object_type, $object_data, array(), false, true);
         } elseif ('taxonomy' == $pod['type']) {
             $term = pods_v($object_fields['name']['name'], $object_data, '', null, true);
             if (!isset($term_data)) {
                 $term_data = array();
             }
             if (empty($params->id) || !empty($term_data)) {
                 $taxonomy = $pod['name'];
                 if (!empty($pod['object'])) {
                     $taxonomy = $pod['object'];
                 }
                 $params->id = $this->save_term($params->id, $term, $taxonomy, $term_data, true);
             }
         }
         if ('table' == $pod['storage']) {
             // Every row should have an id set here, otherwise Pods with nothing
             // but relationship fields won't get properly ID'd
             if (empty($params->id)) {
                 $params->id = 0;
             }
             $table_data = array('id' => $params->id) + $table_data;
             array_unshift($table_formats, '%d');
             if (!empty($table_data)) {
                 $sql = pods_data()->insert_on_duplicate("@wp_pods_{$params->pod}", $table_data, $table_formats);
                 $id = pods_query($sql, 'Cannot add/save table row');
                 if (empty($params->id)) {
                     $params->id = $id;
                 }
             }
         }
     }
     $params->id = (int) $params->id;
     // Save terms for taxonomies associated to a post type
     if (0 < $params->id && 'post_type' == $pod['type'] && !empty($post_term_data)) {
         foreach ($post_term_data as $post_taxonomy => $post_terms) {
             $post_terms = (array) $post_terms;
             foreach ($post_terms as $k => $v) {
                 if (!preg_match('/[^0-9]/', $v)) {
                     $v = (int) $v;
                 }
                 $post_terms[$k] = $v;
             }
             wp_set_object_terms($params->id, $post_terms, $post_taxonomy);
         }
     }
     $no_conflict = pods_no_conflict_check($pod['type']);
     if (!$no_conflict) {
         pods_no_conflict_on($pod['type']);
     }
     // Save relationship / file data
     if (!empty($rel_fields)) {
         foreach ($rel_fields as $type => $data) {
             // Only handle tableless fields
             if (!in_array($type, $tableless_field_types)) {
                 continue;
             }
             foreach ($data as $field => $values) {
                 $pick_val = pods_var('pick_val', $fields[$field]);
                 if ('table' == pods_var('pick_object', $fields[$field])) {
                     $pick_val = pods_var('pick_table', $fields[$field]['options'], $pick_val, null, true);
                 }
                 if ('__current__' == $pick_val) {
                     if (is_object($pod)) {
                         $pick_val = $pod->pod;
                     } elseif (is_array($pod)) {
                         $pick_val = $pod['name'];
                     } elseif (0 < strlen($pod)) {
                         $pick_val = $pod;
                     }
                 }
                 $fields[$field]['options']['table_info'] = pods_api()->get_table_info(pods_var('pick_object', $fields[$field]), $pick_val, null, null, $fields[$field]['options']);
                 if (isset($fields[$field]['options']['table_info']['pod']) && !empty($fields[$field]['options']['table_info']['pod']) && isset($fields[$field]['options']['table_info']['pod']['name'])) {
                     $search_data = pods($fields[$field]['options']['table_info']['pod']['name']);
                     $data_mode = 'pods';
                 } else {
                     $search_data = pods_data();
                     $search_data->table($fields[$field]['options']['table_info']);
                     $data_mode = 'data';
                 }
                 $find_rel_params = array('select' => "`t`.`{$search_data->field_id}`", 'where' => "`t`.`{$search_data->field_slug}` = %s OR `t`.`{$search_data->field_index}` = %s", 'limit' => 1, 'pagination' => false, 'search' => false);
                 if (empty($search_data->field_slug) && !empty($search_data->field_index)) {
                     $find_rel_params['where'] = "`t`.`{$search_data->field_index}` = %s";
                 } elseif (empty($search_data->field_slug) && empty($search_data->field_index)) {
                     $find_rel_params = false;
                 }
                 $related_limit = (int) pods_var_raw($type . '_limit', $fields[$field]['options'], 0);
                 if ('single' == pods_var_raw($type . '_format_type', $fields[$field]['options'])) {
                     $related_limit = 1;
                 }
                 // Enforce integers / unique values for IDs
                 $value_ids = array();
                 $is_file_field = in_array($type, PodsForm::file_field_types());
                 $is_taggable = in_array($type, PodsForm::tableless_field_types()) && 1 == pods_v($type . '_taggable', $fields[$field]['options']);
                 // @todo Handle simple relationships eventually
                 foreach ($values as $v) {
                     if (!empty($v)) {
                         if (!is_array($v)) {
                             if (!preg_match('/[^0-9]/', $v)) {
                                 $v = (int) $v;
                             } elseif ($is_file_field) {
                                 // Get ID from GUID
                                 $v = pods_image_id_from_field($v);
                                 // If file not found, add it
                                 if (empty($v)) {
                                     $v = pods_attachment_import($v);
                                 }
                             } else {
                                 $v_data = false;
                                 if (false !== $find_rel_params) {
                                     $rel_params = $find_rel_params;
                                     $rel_params['where'] = $wpdb->prepare($rel_params['where'], array($v, $v));
                                     $search_data->select($rel_params);
                                     $v_data = $search_data->fetch($v);
                                 }
                                 if (!empty($v_data) && isset($v_data[$search_data->field_id])) {
                                     $v = (int) $v_data[$search_data->field_id];
                                 } elseif ($is_taggable && 'pods' == $data_mode) {
                                     $tag_data = array($search_data->field_index => $v);
                                     if ('post_type' == $search_data->pod_data['type']) {
                                         $tag_data['post_status'] = 'publish';
                                     }
                                     /**
                                      * Filter for changing tag before adding new item.
                                      *
                                      * @param array $tag_data Fields for creating new item.
                                      * @param int $v Field ID of tag.
                                      * @param obj $search_data Search object for tag.
                                      * @param string $field Table info for field.
                                      * @param array	$pieces Field array.
                                      *
                                      * @since 2.3.19
                                      */
                                     $tag_data = apply_filters('pods_api_save_pod_item_taggable_data', $tag_data, $v, $search_data, $field, compact($pieces));
                                     // Save $v to a new item on related object
                                     $v = $search_data->add($tag_data);
                                     // @todo Support non-Pods for tagging
                                 }
                             }
                         } elseif ($is_file_field && isset($v['id'])) {
                             $v = (int) $v['id'];
                         } else {
                             continue;
                         }
                         if (!empty($v) && !in_array($v, $value_ids)) {
                             $value_ids[] = $v;
                         }
                     }
                 }
                 $value_ids = array_unique(array_filter($value_ids));
                 // Limit values
                 if (0 < $related_limit && !empty($value_ids)) {
                     $value_ids = array_slice($value_ids, 0, $related_limit);
                 }
                 // Get current values
                 if ('pick' == $type && isset(PodsField_Pick::$related_data[$fields[$field]['id']]) && isset(PodsField_Pick::$related_data[$fields[$field]['id']]['current_ids'])) {
                     $related_ids = PodsField_Pick::$related_data[$fields[$field]['id']]['current_ids'];
                 } else {
                     $related_ids = $this->lookup_related_items($fields[$field]['id'], $pod['id'], $params->id, $fields[$field], $pod);
                 }
                 // Get ids to remove
                 $remove_ids = array_diff($related_ids, $value_ids);
                 // Delete relationships
                 if (!empty($remove_ids)) {
                     $this->delete_relationships($params->id, $remove_ids, $pod, $fields[$field]);
                 }
                 // Save relationships
                 if (!empty($value_ids)) {
                     $this->save_relationships($params->id, $value_ids, $pod, $fields[$field]);
                 }
                 // Run save function for field type (where needed)
                 PodsForm::save($type, $values, $params->id, $field, array_merge($fields[$field], $fields[$field]['options']), array_merge($fields, $object_fields), $pod, $params);
             }
             // Unset data no longer needed
             if ('pick' == $type) {
                 foreach ($data as $field => $values) {
                     if (isset(PodsField_Pick::$related_data[$fields[$field]['id']])) {
                         unset(PodsField_Pick::$related_data[PodsField_Pick::$related_data[$fields[$field]['id']]['related_field']['id']]);
                         unset(PodsField_Pick::$related_data[$fields[$field]['id']]);
                     }
                 }
             }
         }
     }
     if (!$no_conflict) {
         pods_no_conflict_off($pod['type']);
     }
     if (false === $bypass_helpers) {
         $pieces = compact($pieces);
         // Plugin hooks
         $this->do_hook('post_save_pod_item', $pieces, $is_new_item, $params->id);
         $this->do_hook("post_save_pod_item_{$params->pod}", $pieces, $is_new_item, $params->id);
         if ($is_new_item) {
             $this->do_hook('post_create_pod_item', $pieces, $params->id);
             $this->do_hook("post_create_pod_item_{$params->pod}", $pieces, $params->id);
         } else {
             $this->do_hook('post_edit_pod_item', $pieces, $params->id);
             $this->do_hook("post_edit_pod_item_{$params->pod}", $pieces, $params->id);
         }
         // Call any post-save helpers (if not bypassed)
         if (!defined('PODS_DISABLE_EVAL') || !PODS_DISABLE_EVAL) {
             if (!empty($post_save_helpers)) {
                 pods_deprecated(sprintf(__('Post-save helpers are deprecated, use the action pods_post_save_pod_item_%s instead', 'pods'), $params->pod), '2.0');
                 foreach ($post_save_helpers as $helper) {
                     $helper = $this->load_helper(array('name' => $helper));
                     if (false !== $helper && (!defined('PODS_DISABLE_EVAL') || !PODS_DISABLE_EVAL)) {
                         eval('?>' . $helper['code']);
                     }
                 }
             }
         }
     }
     // Clear cache
     pods_cache_clear($params->id, 'pods_items_' . $pod['name']);
     if ($params->clear_slug_cache && !empty($pod['field_slug'])) {
         $slug = pods($pod['name'], $params->id)->field($pod['field_slug']);
         if (0 < strlen($slug)) {
             pods_cache_clear($slug, 'pods_items_' . $pod['name']);
         }
     }
     // Clear WP meta cache
     if (in_array($pod['type'], array('post_type', 'taxonomy', 'user', 'comment'))) {
         $meta_type = $pod['type'];
         if ('post_type' == $meta_type) {
             $meta_type = 'post';
         }
         wp_cache_delete($params->id, $meta_type . '_meta');
         wp_cache_delete($params->id, 'pods_' . $meta_type . '_meta');
     }
     // Success! Return the id
     return $params->id;
 }
function set_post_content($post_id, $entry, $form)
{
    $attachments = $entry[7];
    $attachment_array = array();
    $attachments = json_decode($attachments);
    $fix_version = $entry[6];
    if (count($attachments) > 0) {
        foreach ($attachments as $attachment) {
            $attachment_id = pods_attachment_import($attachment, $post_id, false);
            $attachment_array[] = $attachment_id;
        }
    }
    $fix_version = explode(',', $fix_version);
    $status = wp_set_object_terms($post_id, $fix_version, 'fixversion', true);
    $data = array('project' => $entry[2], 'issue_type' => $entry[3], 'priority' => $entry[4], 'description' => $entry[5], 'fix_version' => $status, 'attachments' => $attachment_array, 'assignee' => $entry[8], 'cc_users' => $entry[9], 'estimated_time' => $entry[10], 'associated_tasks' => $entry[11], 'description_of_associated_tasks' => $entry[12], 'add_line_item_to_invoice' => $entry[13], 'add_line_item_to_estimate' => $entry[14], 'rate' => $entry[15], 'quantity' => $entry[16], 'percent_adjustment' => $entry[17]);
    if (isset($entry[13]) && !empty($entry[13])) {
        $rate = $entry[15];
        $description = "<span id='task-{$post_id}' class='" . sanitize_title($entry[4]) . " " . sanitize_title($entry[3]) . "'><strong class='title'>" . $entry[1] . "</strong><br/>" . $entry[5] . "</span>";
        $quantity = $entry[16];
        $percent_adjustment = floatval($entry[17]);
        $new_rate = (double) convert_estimated_time_to_minutes($entry[10]) / 60 * $rate;
        $total = (double) round($new_rate * $quantity - $new_rate * $quantity * ($percent_adjustment / 100), 2);
        $invoices = explode(',', $entry[13]);
        foreach ($invoices as $invoice) {
            $SI_Invoice = SI_Invoice::get_instance($invoice);
            $line_items = $SI_Invoice->get_line_items();
            $line_items[] = array('rate' => $new_rate, 'qty' => $quantity, 'desc' => $description, 'type' => 'task', 'total' => $total, 'tax' => $percent_adjustment);
            $SI_Invoice->set_line_items($line_items);
            $SI_Invoice->set_calculated_total();
        }
    }
    if (isset($entry[14]) && !empty($entry[14])) {
        $rate = $entry[15];
        $description = "<span id='task-{$post_id}' class='" . sanitize_title($entry[4]) . " " . sanitize_title($entry[3]) . "'><strong class='title'>" . $entry[1] . "</strong><br/>" . $entry[5] . "</span>";
        $quantity = $entry[16];
        $percent_adjustment = floatval($entry[17]);
        $new_rate = (double) convert_estimated_time_to_minutes($entry[10]) / 60 * $rate;
        $total = (double) round($new_rate * $quantity - $new_rate * $quantity * ($percent_adjustment / 100), 2);
        $estimates = explode(',', $entry[14]);
        foreach ($estimates as $estimate) {
            $SI_Estimate = SI_Estimate::get_instance($estimate);
            $line_items = $SI_Estimate->get_line_items();
            $line_items[] = array('rate' => $new_rate, 'qty' => $quantity, 'desc' => $description, 'type' => 'task', 'total' => $total, 'tax' => $percent_adjustment);
            $SI_Estimate->set_line_items($line_items);
            $SI_Estimate->set_calculated_total();
        }
    }
    $pod = pods('mg_task', $post_id);
    apply_filters('pods_api_pre_save_pod_item', $pod->save($data));
    if (isset($entry[18]) && check_for_value($entry[18]) === true) {
        wp_update_post(array('ID' => $post_id, 'post_parent' => $entry[18]));
    }
}