Beispiel #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;
 }
    /**
     * @return mixed
     */
    public function go()
    {
        $this->do_hook('go');
        $_GET = pods_unsanitize($_GET);
        // fix wp sanitization
        $_POST = pods_unsanitize($_POST);
        // fix wp sanitization
        if (false !== $this->css) {
            ?>
        <link type="text/css" rel="stylesheet" href="<?php 
            echo esc_url($this->css);
            ?>
" />
        <?php 
        }
        if (false !== $this->wpcss) {
            $stylesheets = array('global', 'wp-admin', $this->wpcss);
            foreach ($stylesheets as $style) {
                if (!wp_style_is($style, 'queue') && !wp_style_is($style, 'to_do') && !wp_style_is($style, 'done')) {
                    wp_enqueue_style($style);
                }
            }
        }
        $this->ui_page = array($this->action);
        if ('add' == $this->action && !in_array($this->action, $this->actions_disabled)) {
            $this->ui_page[] = 'form';
            if ('create' == $this->do && $this->save && !in_array($this->do, $this->actions_disabled) && !empty($_POST)) {
                $this->ui_page[] = $this->do;
                $this->save(true);
                $this->manage();
            } else {
                $this->add();
            }
        } elseif ('edit' == $this->action && !in_array($this->action, $this->actions_disabled) || 'duplicate' == $this->action && !in_array($this->action, $this->actions_disabled)) {
            $this->ui_page[] = 'form';
            if ('save' == $this->do && $this->save && !empty($_POST)) {
                $this->save();
            }
            $this->edit('duplicate' == $this->action && !in_array($this->action, $this->actions_disabled) ? true : false);
        } elseif ('delete' == $this->action && !in_array($this->action, $this->actions_disabled) && false !== wp_verify_nonce($this->_nonce, 'pods-ui-action-delete')) {
            $this->delete($this->id);
            $this->manage();
        } elseif ('reorder' == $this->action && !in_array($this->action, $this->actions_disabled) && false !== $this->reorder['on']) {
            if ('save' == $this->do) {
                $this->ui_page[] = $this->do;
                $this->reorder();
            }
            $this->manage(true);
        } elseif ('save' == $this->do && $this->save && !in_array($this->do, $this->actions_disabled) && !empty($_POST)) {
            $this->ui_page[] = $this->do;
            $this->save();
            $this->manage();
        } elseif ('create' == $this->do && $this->save && !in_array($this->do, $this->actions_disabled) && !empty($_POST)) {
            $this->ui_page[] = $this->do;
            $this->save(true);
            $this->manage();
        } elseif ('view' == $this->action && !in_array($this->action, $this->actions_disabled)) {
            $this->view();
        } else {
            if (isset($this->actions_custom[$this->action])) {
                $more_args = false;
                if (is_array($this->actions_custom[$this->action]) && isset($this->actions_custom[$this->action]['more_args'])) {
                    $more_args = $this->actions_custom[$this->action]['more_args'];
                }
                $row = $this->row;
                if (empty($row)) {
                    $row = $this->get_row();
                }
                if ($this->restricted($this->action, $row) || $more_args && !empty($more_args['nonce']) && false === wp_verify_nonce($this->_nonce, 'pods-ui-action-' . $this->action)) {
                    return $this->error(sprintf(__('<strong>Error:</strong> You do not have access to this %s.', 'pods'), $this->item));
                } elseif ($more_args && false !== $this->callback_action(true, $this->action, $this->id, $row)) {
                    return null;
                } elseif (false !== $this->callback_action(true, $this->action, $this->id)) {
                    return null;
                }
            }
            if (!in_array('manage', $this->actions_disabled)) {
                // handle session / user persistent settings for show_per_page, orderby, search, and filters
                $methods = array('session', 'user');
                // @todo fix this to set ($this) AND save (setting)
                foreach ($methods as $method) {
                    foreach ($this->{$method} as $setting) {
                        if ('show_per_page' == $setting) {
                            $value = $this->limit;
                        } elseif ('orderby' == $setting) {
                            if (empty($this->orderby)) {
                                $value = '';
                            } elseif (isset($this->orderby['default'])) {
                                $value = $this->orderby['default'] . ' ' . (false === strpos($this->orderby['default'], ' ') ? $this->orderby_dir : '');
                            } else {
                                $value = '';
                            }
                        } else {
                            $value = $this->{$setting};
                        }
                        pods_v_set($value, $setting, $method);
                    }
                }
                $this->manage();
            }
        }
    }
Beispiel #3
0
    /**
     * @return mixed
     */
    public function go()
    {
        $this->do_hook('go');
        $_GET = pods_unsanitize($_GET);
        // fix wp sanitization
        $_POST = pods_unsanitize($_POST);
        // fix wp sanitization
        if (false !== $this->css) {
            ?>
        <link type="text/css" rel="stylesheet" href="<?php 
            echo $this->css;
            ?>
" />
        <?php 
        }
        if (false !== $this->wpcss) {
            $stylesheets = array('global', 'wp-admin', $this->wpcss);
            foreach ($stylesheets as $style) {
                if (!wp_style_is($style, 'queue') && !wp_style_is($style, 'to_do') && !wp_style_is($style, 'done')) {
                    wp_enqueue_style($style);
                }
            }
        }
        $this->ui_page = array($this->action);
        if ('add' == $this->action && !in_array($this->action, $this->actions_disabled)) {
            $this->ui_page[] = 'form';
            if ('create' == $this->do && $this->save && !in_array($this->do, $this->actions_disabled) && !empty($_POST)) {
                $this->ui_page[] = $this->do;
                $this->save(true);
                $this->manage();
            } else {
                $this->add();
            }
        } elseif ('edit' == $this->action && !in_array($this->action, $this->actions_disabled) || 'duplicate' == $this->action && !in_array($this->action, $this->actions_disabled)) {
            $this->ui_page[] = 'form';
            if ('save' == $this->do && $this->save && !empty($_POST)) {
                $this->save();
            }
            $this->edit('duplicate' == $this->action && !in_array($this->action, $this->actions_disabled) ? 1 : 0);
        } elseif ('delete' == $this->action && !in_array($this->action, $this->actions_disabled)) {
            $this->delete($this->id);
            $this->manage();
        } elseif ('reorder' == $this->action && !in_array($this->action, $this->actions_disabled) && false !== $this->reorder['on']) {
            if ('save' == $this->do) {
                $this->ui_page[] = $this->do;
                $this->reorder();
            }
            $this->manage(true);
        } elseif ('save' == $this->do && $this->save && !in_array($this->do, $this->actions_disabled) && !empty($_POST)) {
            $this->ui_page[] = $this->do;
            $this->save();
            $this->manage();
        } elseif ('create' == $this->do && $this->save && !in_array($this->do, $this->actions_disabled) && !empty($_POST)) {
            $this->ui_page[] = $this->do;
            $this->save(true);
            $this->manage();
        } elseif ('view' == $this->action && !in_array($this->action, $this->actions_disabled)) {
            $this->view();
        } elseif (isset($this->actions_custom[$this->action]) && is_callable($this->actions_custom[$this->action])) {
            return call_user_func_array($this->actions_custom[$this->action], array(&$this, $this->id));
        } elseif (isset($this->actions_custom[$this->action]) && (is_array($this->actions_custom[$this->action]) && isset($this->actions_custom[$this->action]['callback']) && is_callable($this->actions_custom[$this->action]['callback']))) {
            return call_user_func_array($this->actions_custom[$this->action]['callback'], array(&$this, $this->id));
        } elseif (!in_array('manage', $this->actions_disabled)) {
            $this->manage();
        }
        // handle session / user persistent settings for show_per_page, orderby, search, and filters
        $methods = array('session', 'user');
        foreach ($methods as $method) {
            foreach ($this->{$method} as $setting) {
                if ('show_per_page' == $setting) {
                    $value = $this->limit;
                } elseif ('orderby' == $setting) {
                    if (empty($this->orderby)) {
                        $value = '';
                    } elseif (isset($this->orderby['default'])) {
                        // save this if we have a default index set
                        $value = $this->orderby['default'] . ' ' . (false === strpos($this->orderby['default'], ' ') ? $this->orderby_dir : '');
                    } else {
                        $value = '';
                    }
                } else {
                    $value = $this->{$setting};
                }
                pods_var_set($value, $setting, $method);
            }
        }
    }
/**
 * Create a new URL off of the current one, with updated parameters
 *
 * @param array $array Parameters to be set (empty will remove it)
 * @param array $allowed Parameters to keep (if empty, all are kept)
 * @param array $excluded Parameters to always remove
 * @param string $url URL to base update off of
 *
 * @return mixed
 *
 * @since 2.3.10
 *
 * @see add_query_arg
 */
function pods_query_arg($array = null, $allowed = null, $excluded = null, $url = null)
{
    $array = (array) $array;
    $allowed = (array) $allowed;
    $excluded = (array) $excluded;
    if (!isset($_GET)) {
        $query_args = array();
    } else {
        $query_args = pods_unsanitize($_GET);
    }
    foreach ($query_args as $key => $val) {
        if (is_array($val) && empty($val)) {
            $query_args[$key] = false;
        } elseif (!is_array($val) && strlen($val) < 1) {
            $query_args[$key] = false;
        } elseif (!empty($allowed)) {
            $allow_it = false;
            foreach ($allowed as $allow) {
                if ($allow == $key) {
                    $allow_it = true;
                } elseif (false !== strpos($allow, '*') && 0 === strpos($key, trim($allow, '*'))) {
                    $allow_it = true;
                }
            }
            if (!$allow_it) {
                $query_args[$key] = false;
            }
        }
    }
    if (!empty($excluded)) {
        foreach ($excluded as $exclusion) {
            if (isset($query_args[$exclusion]) && !in_array($exclusion, $allowed)) {
                $query_args[$exclusion] = false;
            }
        }
    }
    if (!empty($array)) {
        foreach ($array as $key => $val) {
            if (null !== $val || false === strpos($key, '*')) {
                if (is_array($val) && !empty($val)) {
                    $query_args[$key] = $val;
                } elseif (!is_array($val) && 0 < strlen($val)) {
                    $query_args[$key] = $val;
                } elseif (isset($query_args[$key])) {
                    $query_args[$key] = false;
                }
            } else {
                $key = str_replace('*', '', $key);
                foreach ($query_args as $k => $v) {
                    if (false !== strpos($k, $key)) {
                        $query_args[$k] = false;
                    }
                }
            }
        }
    }
    if (null === $url) {
        $url = add_query_arg($query_args);
    } else {
        $url = add_query_arg($query_args, $url);
    }
    return $url;
}
Beispiel #5
0
/**
 * Create a new URL off of the current one, with updated parameters
 *
 * @param array $array Parameters to be set (empty will remove it)
 * @param array $allowed Parameters to keep (if empty, all are kept)
 * @param array $excluded Parameters to always remove
 * @param string $url URL to base update off of
 *
 * @return mixed
 *
 * @since 2.0
 */
function pods_var_update($array = null, $allowed = null, $excluded = null, $url = null)
{
    $array = (array) $array;
    $allowed = (array) $allowed;
    $excluded = (array) $excluded;
    if (empty($url)) {
        $url = $_SERVER['REQUEST_URI'];
    }
    if (!isset($_GET)) {
        $get = array();
    } else {
        $get = $_GET;
    }
    $get = pods_unsanitize($get);
    foreach ($get as $key => $val) {
        if (is_array($val) && empty($val)) {
            unset($get[$key]);
        } elseif (!is_array($val) && strlen($val) < 1) {
            unset($get[$key]);
        } elseif (!empty($allowed)) {
            $allow_it = false;
            foreach ($allowed as $allow) {
                if ($allow == $key) {
                    $allow_it = true;
                } elseif (false !== strpos($allow, '*') && 0 === strpos($key, trim($allow, '*'))) {
                    $allow_it = true;
                }
            }
            if (!$allow_it) {
                unset($get[$key]);
            }
        }
    }
    if (!empty($excluded)) {
        foreach ($excluded as $exclusion) {
            if (isset($get[$exclusion]) && !in_array($exclusion, $allowed)) {
                unset($get[$exclusion]);
            }
        }
    }
    if (!empty($array)) {
        foreach ($array as $key => $val) {
            if (null !== $val || false === strpos($key, '*')) {
                if (is_array($val) && !empty($val)) {
                    $get[$key] = $val;
                } elseif (!is_array($val) && 0 < strlen($val)) {
                    $get[$key] = $val;
                } elseif (isset($get[$key])) {
                    unset($get[$key]);
                }
            } else {
                $key = str_replace('*', '', $key);
                foreach ($get as $k => $v) {
                    if (false !== strpos($k, $key)) {
                        unset($get[$k]);
                    }
                }
            }
        }
    }
    $url = current(explode('#', current(explode('?', $url))));
    if (!empty($get)) {
        $url = $url . '?' . http_build_query($get);
    }
    return $url;
}