コード例 #1
0
ファイル: general.php プロジェクト: pods-framework/pods
/**
 * Standardize queries and error reporting. It replaces @wp_ with $wpdb->prefix.
 *
 * @see PodsData::query
 *
 * @param string $sql SQL Query
 * @param string $error (optional) The failure message
 * @param string $results_error (optional) Throw an error if a records are found
 * @param string $no_results_error (optional) Throw an error if no records are found
 *
 * @return array|bool|mixed|null|void
 * @since 2.0
 */
function pods_query($sql, $error = 'Database Error', $results_error = null, $no_results_error = null)
{
    $podsdata = pods_data();
    $sql = apply_filters('pods_query_sql', $sql, $error, $results_error, $no_results_error);
    $sql = $podsdata->get_sql($sql);
    if (is_array($error)) {
        if (!is_array($sql)) {
            $sql = array($sql, $error);
        }
        $error = 'Database Error';
    }
    if (1 == pods_v('pods_debug_sql_all', 'get', 0) && is_user_logged_in() && pods_is_admin(array('pods'))) {
        $debug_sql = $sql;
        echo '<textarea cols="100" rows="24">';
        if (is_array($debug_sql)) {
            $debug_sql = print_r($debug_sql, true);
        }
        echo esc_textarea($debug_sql);
        echo '</textarea>';
    }
    return $podsdata->query($sql, $error, $results_error, $no_results_error);
}
コード例 #2
0
ファイル: PodsAPI.php プロジェクト: satokora/IT354Project
 /**
  * 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;
 }
コード例 #3
0
 /**
  * Generate UI for Data Management
  *
  * @param mixed $options Object, Array, or String containing Pod or Options to be used
  * @param bool $deprecated Set to true to support old options array from Pods UI plugin
  *
  * @return \PodsUI
  *
  * @license http://www.gnu.org/licenses/gpl-2.0.html
  * @since 2.0
  */
 public function __construct($options, $deprecated = false)
 {
     $this->_nonce = pods_v('_wpnonce', 'request');
     $object = null;
     if (is_object($options)) {
         $object = $options;
         $options = array();
         if (isset($object->ui)) {
             $options = (array) $object->ui;
             unset($object->ui);
         }
         if (is_object($object) && ('Pods' == get_class($object) || 'Pod' == get_class($object))) {
             $this->pod =& $object;
         }
     }
     if (!is_array($options)) {
         // @todo need to come back to this and allow for multi-dimensional strings
         // like: option=value&option2=value2&option3=key[val],key2[val2]&option4=this,that,another
         if (false !== strpos($options, '=') || false !== strpos($options, '&')) {
             parse_str($options, $options);
         } else {
             $options = array('pod' => $options);
         }
     }
     if (!is_object($object) && isset($options['pod'])) {
         if (is_object($options['pod'])) {
             $this->pod = $options['pod'];
         } elseif (isset($options['id'])) {
             $this->pod = pods($options['pod'], $options['id']);
         } else {
             $this->pod = pods($options['pod']);
         }
         unset($options['pod']);
     } elseif (is_object($object)) {
         $this->pod = $object;
     }
     if (false !== $deprecated || is_object($this->pod) && 'Pod' == get_class($this->pod)) {
         $options = $this->setup_deprecated($options);
     }
     if (is_object($this->pod) && 'Pod' == get_class($this->pod) && is_object($this->pod->_data)) {
         $this->pods_data =& $this->pod->_data;
     } elseif (is_object($this->pod) && 'Pods' == get_class($this->pod) && is_object($this->pod->data)) {
         $this->pods_data =& $this->pod->data;
     } elseif (is_object($this->pod)) {
         $this->pods_data = pods_data($this->pod->pod);
     } elseif (!is_object($this->pod)) {
         $this->pods_data = pods_data($this->pod);
     }
     $options = $this->do_hook('pre_init', $options);
     $this->setup($options);
     if (is_object($this->pods_data) && is_object($this->pod) && 0 < $this->id) {
         if ($this->id != $this->pods_data->id) {
             $this->row = $this->pods_data->fetch($this->id);
         } else {
             $this->row = $this->pods_data->row;
         }
     }
     if ((!is_object($this->pod) || 'Pods' != get_class($this->pod)) && false === $this->sql['table'] && false === $this->data) {
         echo $this->error(__('<strong>Error:</strong> Pods UI needs a Pods object or a Table definition to run from, see the User Guide for more information.', 'pods'));
         return false;
     }
     $this->go();
 }
コード例 #4
0
 /**
  * Get data from relationship objects
  *
  * @param array $object_params Object data parameters
  *
  * @return array|bool Object data
  */
 public function get_object_data($object_params = null)
 {
     global $wpdb, $polylang, $sitepress, $icl_adjust_id_url_filter_off;
     $current_language = false;
     // WPML support
     if (is_object($sitepress) && !$icl_adjust_id_url_filter_off) {
         $current_language = pods_sanitize(ICL_LANGUAGE_CODE);
     } elseif (function_exists('pll_current_language')) {
         $current_language = pll_current_language('slug');
     }
     $object_params = array_merge(array('name' => '', 'value' => '', 'options' => array(), 'pod' => '', 'id' => '', 'context' => '', 'data_params' => array('query' => ''), 'page' => 1, 'limit' => 0), $object_params);
     $name = $object_params['name'];
     $value = $object_params['value'];
     $options = $object_params['options'] = (array) $object_params['options'];
     $pod = $object_params['pod'];
     $id = $object_params['id'];
     $context = $object_params['context'];
     $data_params = $object_params['data_params'] = (array) $object_params['data_params'];
     $page = min(1, (int) $object_params['page']);
     $limit = (int) $object_params['limit'];
     if (isset($options['options'])) {
         $options = array_merge($options, $options['options']);
         unset($options['options']);
     }
     $data = apply_filters('pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params);
     $items = array();
     if (!isset($options[self::$type . '_object'])) {
         $data = pods_var_raw('data', $options, array(), null, true);
     }
     $simple = false;
     if (null === $data) {
         $data = array();
         if ('custom-simple' == $options[self::$type . '_object']) {
             $custom = pods_var_raw(self::$type . '_custom', $options, '');
             $custom = apply_filters('pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params);
             if (!empty($custom)) {
                 if (!is_array($custom)) {
                     $data = array();
                     $custom = explode("\n", trim($custom));
                     foreach ($custom as $custom_value) {
                         $custom_label = explode('|', $custom_value);
                         if (empty($custom_label)) {
                             continue;
                         }
                         if (1 == count($custom_label)) {
                             $custom_label = $custom_value;
                         } else {
                             $custom_value = $custom_label[0];
                             $custom_label = $custom_label[1];
                         }
                         $custom_value = trim((string) $custom_value);
                         $custom_label = trim((string) $custom_label);
                         $data[$custom_value] = $custom_label;
                     }
                 } else {
                     $data = $custom;
                 }
                 $simple = true;
             }
         } elseif (isset(self::$related_objects[$options[self::$type . '_object']]) && isset(self::$related_objects[$options[self::$type . '_object']]['data']) && !empty(self::$related_objects[$options[self::$type . '_object']]['data'])) {
             $data = self::$related_objects[$options[self::$type . '_object']]['data'];
             $simple = true;
         } elseif (isset(self::$related_objects[$options[self::$type . '_object']]) && isset(self::$related_objects[$options[self::$type . '_object']]['data_callback']) && is_callable(self::$related_objects[$options[self::$type . '_object']]['data_callback'])) {
             $data = call_user_func_array(self::$related_objects[$options[self::$type . '_object']]['data_callback'], array($name, $value, $options, $pod, $id));
             $simple = true;
             // Cache data from callback
             if (!empty($data)) {
                 self::$related_objects[$options[self::$type . '_object']]['data'] = $data;
             }
         } elseif ('simple_value' != $context) {
             $pick_val = pods_var(self::$type . '_val', $options);
             if ('table' == pods_var(self::$type . '_object', $options)) {
                 $pick_val = pods_var(self::$type . '_table', $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;
                 }
             }
             $options['table_info'] = pods_api()->get_table_info(pods_var(self::$type . '_object', $options), $pick_val, null, null, $options);
             $search_data = pods_data();
             $search_data->table($options['table_info']);
             if (isset($options['table_info']['pod']) && !empty($options['table_info']['pod']) && isset($options['table_info']['pod']['name'])) {
                 $search_data->pod = $options['table_info']['pod']['name'];
                 $search_data->fields = $options['table_info']['pod']['fields'];
             }
             $params = array('select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`", 'table' => $search_data->table, 'where' => pods_var_raw(self::$type . '_where', $options, (array) $options['table_info']['where_default'], null, true), 'orderby' => pods_var_raw(self::$type . '_orderby', $options, null, null, true), 'groupby' => pods_var_raw(self::$type . '_groupby', $options, null, null, true), 'pagination' => false, 'search' => false);
             if (in_array($options[self::$type . '_object'], array('site', 'network'))) {
                 $params['select'] .= ', `t`.`path`';
             }
             if (!empty($params['where']) && (array) $options['table_info']['where_default'] != $params['where']) {
                 $params['where'] = pods_evaluate_tags($params['where'], true);
             }
             if (empty($params['where']) || !is_array($params['where']) && strlen(trim($params['where'])) < 1) {
                 $params['where'] = array();
             } elseif (!is_array($params['where'])) {
                 $params['where'] = (array) $params['where'];
             }
             if ('value_to_label' == $context) {
                 $params['where'][] = "`t`.`{$search_data->field_id}` = " . number_format($value, 0, '', '');
             }
             /* not needed yet
                             if ( !empty( $params[ 'orderby' ] ) )
                                 $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
             
                             if ( !empty( $params[ 'groupby' ] ) )
                                 $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
             $display = trim(pods_var(self::$type . '_display', $options), ' {@}');
             if (0 < strlen($display)) {
                 if (isset($options['table_info']['pod']) && !empty($options['table_info']['pod'])) {
                     if (isset($options['table_info']['pod']['object_fields']) && isset($options['table_info']['pod']['object_fields'][$display])) {
                         $search_data->field_index = $display;
                         $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                     } elseif (isset($options['table_info']['pod']['fields'][$display])) {
                         $search_data->field_index = $display;
                         if ('table' == $options['table_info']['pod']['storage'] && !in_array($options['table_info']['pod']['type'], array('pod', 'table'))) {
                             $params['select'] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
                         } elseif ('meta' == $options['table_info']['pod']['storage']) {
                             $params['select'] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}";
                         } else {
                             $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                         }
                     }
                 } elseif (isset($options['table_info']['object_fields']) && isset($options['table_info']['object_fields'][$display])) {
                     $search_data->field_index = $display;
                     $params['select'] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
                 }
             }
             $autocomplete = false;
             if ('single' == pods_var(self::$type . '_format_type', $options, 'single') && 'autocomplete' == pods_var(self::$type . '_format_single', $options, 'dropdown')) {
                 $autocomplete = true;
             } elseif ('multi' == pods_var(self::$type . '_format_type', $options, 'single') && 'autocomplete' == pods_var(self::$type . '_format_multi', $options, 'checkbox')) {
                 $autocomplete = true;
             }
             $hierarchy = false;
             if ('data' == $context && !$autocomplete) {
                 if ('single' == pods_var(self::$type . '_format_type', $options, 'single') && in_array(pods_var(self::$type . '_format_single', $options, 'dropdown'), array('dropdown', 'radio'))) {
                     $hierarchy = true;
                 } elseif ('multi' == pods_var(self::$type . '_format_type', $options, 'single') && in_array(pods_var(self::$type . '_format_multi', $options, 'checkbox'), array('multiselect', 'checkbox'))) {
                     $hierarchy = true;
                 }
             }
             if ($hierarchy && $options['table_info']['object_hierarchical'] && !empty($options['table_info']['field_parent'])) {
                 $params['select'] .= ', ' . $options['table_info']['field_parent_select'];
             }
             if ($autocomplete) {
                 if (0 == $limit) {
                     $limit = 30;
                 }
                 $params['limit'] = apply_filters('pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params);
                 if (is_array($value) && $params['limit'] < count($value)) {
                     $params['limit'] = count($value);
                 }
                 $params['page'] = $page;
                 if ('admin_ajax_relationship' == $context) {
                     $lookup_where = array($search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'");
                     // @todo Hook into WPML for each table
                     if ($wpdb->users == $search_data->table) {
                         $lookup_where['display_name'] = "`t`.`display_name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['user_login'] = "******" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['user_email'] = "`t`.`user_email` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     } elseif ($wpdb->posts == $search_data->table) {
                         $lookup_where['post_title'] = "`t`.`post_title` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['post_name'] = "`t`.`post_name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['post_content'] = "`t`.`post_content` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['post_excerpt'] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     } elseif ($wpdb->terms == $search_data->table) {
                         $lookup_where['name'] = "`t`.`name` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['slug'] = "`t`.`slug` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     } elseif ($wpdb->comments == $search_data->table) {
                         $lookup_where['comment_content'] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['comment_author'] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                         $lookup_where['comment_author_email'] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like($data_params['query']) . "%'";
                     }
                     $lookup_where = apply_filters('pods_form_ui_field_pick_autocomplete_lookup', $lookup_where, $data_params['query'], $name, $value, $options, $pod, $id, $object_params, $search_data);
                     if (!empty($lookup_where)) {
                         $params['where'][] = implode(' OR ', $lookup_where);
                     }
                     $orderby = array();
                     $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like($data_params['query']) . "%' ) DESC";
                     $pick_orderby = pods_var_raw(self::$type . '_orderby', $options, null, null, true);
                     if (0 < strlen($pick_orderby)) {
                         $orderby[] = $pick_orderby;
                     }
                     $orderby[] = "`t`.`{$search_data->field_index}`";
                     $orderby[] = "`t`.`{$search_data->field_id}`";
                     $params['orderby'] = $orderby;
                 }
             } elseif (0 < $limit) {
                 $params['limit'] = $limit;
                 $params['page'] = $page;
             }
             $extra = '';
             if ($wpdb->posts == $search_data->table) {
                 $extra = ', `t`.`post_type`';
             } elseif ($wpdb->terms == $search_data->table) {
                 $extra = ', `tt`.`taxonomy`';
             } elseif ($wpdb->comments == $search_data->table) {
                 $extra = ', `t`.`comment_type`';
             }
             $params['select'] .= $extra;
             if ('user' == pods_var(self::$type . '_object', $options)) {
                 $roles = pods_var(self::$type . '_user_role', $options);
                 if (!empty($roles)) {
                     $where = array();
                     foreach ((array) $roles as $role) {
                         if (empty($role) || pods_clean_name($role) != $role && sanitize_title($role) != $role) {
                             continue;
                         }
                         $where[] = $wpdb->base_prefix . (is_multisite() && !is_main_site() ? get_current_blog_id() . '_' : '') . 'capabilities.meta_value LIKE "%\\"' . pods_sanitize_like($role) . '\\"%"';
                     }
                     if (!empty($where)) {
                         $params['where'][] = implode(' OR ', $where);
                     }
                 }
             }
             $results = $search_data->select($params);
             if ($autocomplete && $params['limit'] < $search_data->total_found()) {
                 if (!empty($value)) {
                     $ids = $value;
                     if (is_array($ids) && isset($ids[0]) && is_array($ids[0])) {
                         $ids = wp_list_pluck($ids, $search_data->field_id);
                     }
                     if (is_array($ids)) {
                         $ids = implode(', ', $ids);
                     }
                     if (is_array($params['where'])) {
                         $params['where'] = implode(' AND ', $params['where']);
                     }
                     if (!empty($params['where'])) {
                         $params['where'] .= ' AND ';
                     }
                     $params['where'] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
                     $results = $search_data->select($params);
                 }
             } else {
                 $autocomplete = false;
             }
             if ('data' == $context) {
                 self::$field_data = array('field' => $name, 'id' => $options['id'], 'autocomplete' => $autocomplete);
             }
             if ($hierarchy && !$autocomplete && !empty($results) && $options['table_info']['object_hierarchical'] && !empty($options['table_info']['field_parent'])) {
                 $args = array('id' => $options['table_info']['field_id'], 'index' => $options['table_info']['field_index'], 'parent' => $options['table_info']['field_parent']);
                 $results = pods_hierarchical_select($results, $args);
             }
             $ids = array();
             if (!empty($results)) {
                 $display_filter = pods_var('display_filter', pods_var_raw('options', pods_var_raw($search_data->field_index, $search_data->pod_data['object_fields'])));
                 foreach ($results as $result) {
                     $result = get_object_vars($result);
                     if (!isset($result[$search_data->field_id]) || !isset($result[$search_data->field_index])) {
                         continue;
                     }
                     $result[$search_data->field_index] = trim($result[$search_data->field_index]);
                     $object = $object_type = '';
                     if ($wpdb->posts == $search_data->table && isset($result['post_type'])) {
                         $object = $result['post_type'];
                         $object_type = 'post_type';
                     } elseif ($wpdb->terms == $search_data->table && isset($result['taxonomy'])) {
                         $object = $result['taxonomy'];
                         $object_type = 'taxonomy';
                     }
                     // WPML integration for Post Types and Taxonomies
                     if (is_object($sitepress) && in_array($object_type, array('post_type', 'taxonomy'))) {
                         $translated = false;
                         if ('post_type' == $object_type && $sitepress->is_translated_post_type($object)) {
                             $translated = true;
                         } elseif ('taxonomy' == $object_type && $sitepress->is_translated_taxonomy($object)) {
                             $translated = true;
                         }
                         if ($translated) {
                             $object_id = icl_object_id($result[$search_data->field_id], $object, false, $current_language);
                             if (0 < $object_id && !in_array($object_id, $ids)) {
                                 $text = $result[$search_data->field_index];
                                 if ($result[$search_data->field_id] != $object_id) {
                                     if ($wpdb->posts == $search_data->table) {
                                         $text = trim(get_the_title($object_id));
                                     } elseif ($wpdb->terms == $search_data->table) {
                                         $text = trim(get_term($object_id, $object)->name);
                                     }
                                 }
                                 $result[$search_data->field_id] = $object_id;
                                 $result[$search_data->field_index] = $text;
                             } else {
                                 continue;
                             }
                         }
                     } elseif (is_object($polylang) && in_array($object_type, array('post_type', 'taxonomy')) && method_exists($polylang, 'get_translation')) {
                         $translated = false;
                         if ('post_type' == $object_type && pll_is_translated_post_type($object)) {
                             $translated = true;
                         } elseif ('taxonomy' == $object_type && pll_is_translated_taxonomy($object)) {
                             $translated = true;
                         }
                         if ($translated) {
                             $object_id = $polylang->get_translation($object, $result[$search_data->field_id], $current_language);
                             if (0 < $object_id && !in_array($object_id, $ids)) {
                                 $text = $result[$search_data->field_index];
                                 if ($result[$search_data->field_id] != $object_id) {
                                     if ($wpdb->posts == $search_data->table) {
                                         $text = trim(get_the_title($object_id));
                                     } elseif ($wpdb->terms == $search_data->table) {
                                         $text = trim(get_term($object_id, $object)->name);
                                     }
                                 }
                                 $result[$search_data->field_id] = $object_id;
                                 $result[$search_data->field_index] = $text;
                             } else {
                                 continue;
                             }
                         }
                     }
                     if (0 < strlen($display_filter)) {
                         $display_filter_args = pods_var('display_filter_args', pods_var_raw('options', pods_var_raw($search_data->field_index, $search_data->pod_data['object_fields'])));
                         $args = array($display_filter, $result[$search_data->field_index]);
                         if (!empty($display_filter_args)) {
                             foreach ((array) $display_filter_args as $display_filter_arg) {
                                 if (isset($result[$display_filter_arg])) {
                                     $args[] = $result[$display_filter_arg];
                                 }
                             }
                         }
                         $result[$search_data->field_index] = call_user_func_array('apply_filters', $args);
                     }
                     if (in_array($options[self::$type . '_object'], array('site', 'network'))) {
                         $result[$search_data->field_index] = $result[$search_data->field_index] . $result['path'];
                     } elseif (strlen($result[$search_data->field_index]) < 1) {
                         $result[$search_data->field_index] = '(No Title)';
                     }
                     if ('admin_ajax_relationship' == $context) {
                         $items[] = array('id' => $result[$search_data->field_id], 'text' => $result[$search_data->field_index], 'image' => '');
                     } else {
                         $data[$result[$search_data->field_id]] = $result[$search_data->field_index];
                     }
                     $ids[] = $result[$search_data->field_id];
                 }
             }
         }
         if ($simple && 'admin_ajax_relationship' == $context) {
             $found_data = array();
             foreach ($data as $k => $v) {
                 if (false !== stripos($v, $data_params['query']) || false !== stripos($k, $data_params['query'])) {
                     $found_data[$k] = $v;
                 }
             }
             $data = $found_data;
         }
     }
     if ('admin_ajax_relationship' == $context) {
         if (empty($items) && !empty($data)) {
             foreach ($data as $k => $v) {
                 $items[] = array('id' => $k, 'text' => $v, 'image' => '');
             }
         }
         return $items;
     }
     return $data;
 }
コード例 #5
0
ファイル: PodsData.php プロジェクト: Ingenex/redesign
 /**
  * Fetch a new row for the current pod_data
  *
  * @param int $row Row number to fetch
  * @param bool $explicit_set Whether to set explicitly (use false when in loop)
  *
  * @return mixed
  *
  * @since 2.0
  */
 public function fetch($row = null, $explicit_set = true)
 {
     global $wpdb;
     if (null === $row) {
         $explicit_set = false;
     }
     $id = $row;
     $tableless_field_types = PodsForm::tableless_field_types();
     if (null === $row) {
         $this->row_number++;
         $this->row = false;
         if (isset($this->data[$this->row_number])) {
             $this->row = get_object_vars($this->data[$this->row_number]);
             $current_row_id = false;
             if (in_array($this->pod_data['type'], array('post_type', 'media'))) {
                 $current_row_id = pods_var_raw('ID', $this->row);
             } elseif ('taxonomy' == $this->pod_data['type']) {
                 $current_row_id = pods_var_raw('term_id', $this->row);
             } elseif ('user' == $this->pod_data['type']) {
                 $current_row_id = pods_var_raw('ID', $this->row);
             } elseif ('comment' == $this->pod_data['type']) {
                 $current_row_id = pods_var_raw('comment_ID', $this->row);
             } elseif ('settings' == $this->pod_data['type']) {
                 $current_row_id = $this->pod_data['id'];
             }
             if (0 < $current_row_id) {
                 $row = $current_row_id;
             }
         }
     }
     if (null !== $row || 'settings' == $this->pod_data['type']) {
         if ($explicit_set) {
             $this->row_number = -1;
         }
         $mode = 'id';
         $id = pods_absint($row);
         if (!is_numeric($row) || 0 === strpos($row, '0') || $row != preg_replace('/[^0-9]/', '', $row)) {
             $mode = 'slug';
             $id = $row;
         }
         $row = false;
         // @todo Figure out why taking out this in_array() causes cached data issues in User edit screen
         if (!empty($this->pod) && in_array($this->pod_data['type'], array('pod', 'table'))) {
             $row = pods_cache_get($id, 'pods_items_' . $this->pod);
         }
         $current_row_id = false;
         $get_table_data = false;
         $old_row = $this->row;
         if (false !== $row && is_array($row)) {
             $this->row = $row;
         } elseif (in_array($this->pod_data['type'], array('post_type', 'media'))) {
             if ('post_type' == $this->pod_data['type']) {
                 if (empty($this->pod_data['object'])) {
                     $post_type = $this->pod_data['name'];
                 } else {
                     $post_type = $this->pod_data['object'];
                 }
             } else {
                 $post_type = 'attachment';
             }
             if ('id' == $mode) {
                 $this->row = get_post($id, ARRAY_A);
                 if (is_array($this->row) && $this->row['post_type'] != $post_type) {
                     $this->row = false;
                 }
             } else {
                 $args = array('post_type' => $post_type, 'name' => $id, 'numberposts' => 5);
                 $find = get_posts($args);
                 if (!empty($find)) {
                     $this->row = get_object_vars($find[0]);
                 }
             }
             if (is_wp_error($this->row) || empty($this->row)) {
                 $this->row = false;
             } else {
                 $current_row_id = $this->row['ID'];
             }
             $get_table_data = true;
         } elseif ('taxonomy' == $this->pod_data['type']) {
             $taxonomy = $this->pod_data['object'];
             if (empty($taxonomy)) {
                 $taxonomy = $this->pod_data['name'];
             }
             // Taxonomies are registered during init, so they aren't available before then
             if (!did_action('init')) {
                 // hackaround :(
                 if ('id' == $mode) {
                     $term_where = 't.term_id = %d';
                 } else {
                     $term_where = 't.slug = %s';
                 }
                 $filter = 'raw';
                 $term = $id;
                 if ('id' != $mode || !($_term = wp_cache_get($term, $taxonomy))) {
                     $_term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$term_where} LIMIT 1", $taxonomy, $term));
                     if ($_term) {
                         wp_cache_add($term, $_term, $taxonomy);
                     }
                 }
                 $_term = apply_filters('get_term', $_term, $taxonomy);
                 $_term = apply_filters("get_{$taxonomy}", $_term, $taxonomy);
                 $_term = sanitize_term($_term, $taxonomy, $filter);
                 if (is_object($_term)) {
                     $this->row = get_object_vars($_term);
                 }
             } elseif ('id' == $mode) {
                 $this->row = get_term($id, $taxonomy, ARRAY_A);
             } else {
                 $this->row = get_term_by('slug', $id, $taxonomy, ARRAY_A);
             }
             if (is_wp_error($this->row) || empty($this->row)) {
                 $this->row = false;
             } else {
                 $current_row_id = $this->row['term_id'];
             }
             $get_table_data = true;
         } elseif ('user' == $this->pod_data['type']) {
             if ('id' == $mode) {
                 $this->row = get_userdata($id);
             } else {
                 $this->row = get_user_by('slug', $id);
             }
             if (is_wp_error($this->row) || empty($this->row)) {
                 $this->row = false;
             } else {
                 // Get other vars
                 $roles = $this->row->roles;
                 $caps = $this->row->caps;
                 $allcaps = $this->row->allcaps;
                 $this->row = get_object_vars($this->row->data);
                 // Set other vars
                 $this->row['roles'] = $roles;
                 $this->row['caps'] = $caps;
                 $this->row['allcaps'] = $allcaps;
                 unset($this->row['user_pass']);
                 $current_row_id = $this->row['ID'];
             }
             $get_table_data = true;
         } elseif ('comment' == $this->pod_data['type']) {
             $this->row = get_comment($id, ARRAY_A);
             // No slug handling here
             if (is_wp_error($this->row) || empty($this->row)) {
                 $this->row = false;
             } else {
                 $current_row_id = $this->row['comment_ID'];
             }
             $get_table_data = true;
         } elseif ('settings' == $this->pod_data['type']) {
             $this->row = array();
             if (empty($this->fields)) {
                 $this->row = false;
             } else {
                 foreach ($this->fields as $field) {
                     if (!in_array($field['type'], $tableless_field_types)) {
                         $this->row[$field['name']] = get_option($this->pod_data['name'] . '_' . $field['name'], null);
                     }
                 }
                 // Force ID
                 $this->id = $this->pod_data['id'];
                 $this->row['option_id'] = $this->id;
             }
         } else {
             $params = array('table' => $this->table, 'where' => "`t`.`{$this->field_id}` = " . (int) $id, 'orderby' => "`t`.`{$this->field_id}` DESC", 'page' => 1, 'limit' => 1, 'search' => false);
             if ('slug' == $mode && !empty($this->field_slug)) {
                 $id = pods_sanitize($id);
                 $params['where'] = "`t`.`{$this->field_slug}` = '{$id}'";
             }
             $this->row = pods_data()->select($params);
             if (empty($this->row)) {
                 $this->row = false;
             } else {
                 $current_row = (array) $this->row;
                 $this->row = get_object_vars((object) @current($current_row));
             }
         }
         if (!$explicit_set && is_array($this->row) && !empty($this->row) && !empty($old_row)) {
             $this->row = array_merge($old_row, $this->row);
         }
         if ('table' == $this->pod_data['storage'] && false !== $get_table_data && is_numeric($current_row_id)) {
             $params = array('table' => $wpdb->prefix . "pods_", 'where' => "`t`.`id` = {$current_row_id}", 'orderby' => "`t`.`id` DESC", 'page' => 1, 'limit' => 1, 'search' => false, 'strict' => true);
             if (empty($this->pod_data['object'])) {
                 $params['table'] .= $this->pod_data['name'];
             } else {
                 $params['table'] .= $this->pod_data['object'];
             }
             $row = pods_data()->select($params);
             if (!empty($row)) {
                 $current_row = (array) $row;
                 $row = get_object_vars((object) @current($current_row));
                 if (is_array($this->row) && !empty($this->row)) {
                     $this->row = array_merge($row, $this->row);
                 } else {
                     $this->row = $row;
                 }
             }
         }
         // @todo Figure out why taking out this in_array() causes cached data issues in User edit screen
         if (!empty($this->pod) && in_array($this->pod_data['type'], array('pod', 'table'))) {
             pods_cache_set($id, $this->row, 'pods_items_' . $this->pod, 0);
         }
     }
     $this->row = $this->do_hook('fetch', $this->row, $id, $this->row_number);
     return $this->row;
 }
コード例 #6
0
ファイル: Pods.php プロジェクト: erkmen/wpstartersetup
 /**
  * Return the value for a field.
  *
  * If you are getting a field for output in a theme, most of the time you will want to use display() instead.
  *
  * This function will return arrays for relationship and file fields.
  *
  * @param string|array $name The field name, or an associative array of parameters
  * @param boolean $single (optional) For tableless fields, to return the whole array or the just the first item, or an associative array of parameters
  * @param boolean $raw (optional) Whether to return the raw value, or to run through the field type's display method, or an associative array of parameters
  *
  * @return mixed|null Value returned depends on the field type, null if the field doesn't exist, false if no value returned for tableless fields
  * @since 2.0
  * @link http://pods.io/docs/field/
  */
 public function field($name, $single = null, $raw = false)
 {
     global $sitepress;
     $defaults = array('name' => $name, 'orderby' => null, 'single' => $single, 'params' => null, 'in_form' => false, 'raw' => $raw, 'raw_display' => false, 'display' => false, 'get_meta' => false, 'output' => null, 'deprecated' => false, 'args' => array());
     if (is_array($name) || is_object($name)) {
         $defaults['name'] = null;
         $params = (object) array_merge($defaults, (array) $name);
     } elseif (is_array($single) || is_object($single)) {
         $defaults['single'] = null;
         $params = (object) array_merge($defaults, (array) $single);
     } elseif (is_array($raw) || is_object($raw)) {
         $defaults['raw'] = false;
         $params = (object) array_merge($defaults, (array) $raw);
     } else {
         $params = (object) $defaults;
     }
     if ($params->in_form) {
         $params->output = 'ids';
     } elseif (null === $params->output) {
         $params->output = $this->do_hook('field_related_output_type', 'arrays', $this->row, $params);
     }
     if (in_array($params->output, array('id', 'name', 'object', 'array', 'pod'))) {
         $params->output .= 's';
     }
     // Support old $orderby variable
     if (null !== $params->single && is_string($params->single) && empty($params->orderby)) {
         pods_deprecated('Pods::field', '2.0', 'Use $params[ \'orderby\' ] instead');
         $params->orderby = $params->single;
         $params->single = false;
     }
     if (null !== $params->single) {
         $params->single = (bool) $params->single;
     }
     if (is_array($params->name) || strlen(trim($params->name)) < 1) {
         return null;
     }
     $params->name = trim($params->name);
     $params->full_name = $params->name;
     $value = null;
     if (isset($this->row_override[$params->name])) {
         $value = $this->row_override[$params->name];
     }
     if (false === $this->row()) {
         if (false !== $this->data()) {
             $this->fetch();
         } else {
             return $value;
         }
     }
     if ($this->data->field_id == $params->name) {
         if (isset($this->row[$params->name])) {
             return $this->row[$params->name];
         } elseif (null !== $value) {
             return $value;
         }
         return 0;
     }
     $tableless_field_types = PodsForm::tableless_field_types();
     $simple_tableless_objects = PodsForm::field_method('pick', 'simple_objects');
     $params->traverse = array();
     if (in_array($params->name, array('_link', 'detail_url')) || in_array($params->name, array('permalink', 'the_permalink')) && in_array($this->pod_data['type'], array('post_type', 'media'))) {
         if (0 < strlen($this->detail_page)) {
             $value = get_home_url() . '/' . $this->do_magic_tags($this->detail_page);
         } elseif (in_array($this->pod_data['type'], array('post_type', 'media'))) {
             $value = get_permalink($this->id());
         } elseif ('taxonomy' == $this->pod_data['type']) {
             $value = get_term_link($this->id(), $this->pod_data['name']);
         } elseif ('user' == $this->pod_data['type']) {
             $value = get_author_posts_url($this->id());
         } elseif ('comment' == $this->pod_data['type']) {
             $value = get_comment_link($this->id());
         }
     }
     $field_data = false;
     $field_type = false;
     $first_field = explode('.', $params->name);
     $first_field = $first_field[0];
     if (isset($this->fields[$first_field])) {
         $field_data = $this->fields[$first_field];
         $field_type = 'field';
     } elseif (isset($this->pod_data['object_fields']) && !empty($this->pod_data['object_fields'])) {
         if (isset($this->pod_data['object_fields'][$first_field])) {
             $field_data = $this->pod_data['object_fields'][$first_field];
             $field_type = 'object_field';
         } else {
             foreach ($this->pod_data['object_fields'] as $object_field => $object_field_opt) {
                 if (in_array($first_field, $object_field_opt['alias'])) {
                     if ($first_field == $params->name) {
                         $params->name = $object_field;
                     }
                     $first_field = $object_field;
                     $field_data = $object_field_opt;
                     $field_type = 'object_field';
                     break;
                 }
             }
         }
     }
     // Simple fields have no other output options
     if ('pick' == $field_data['type'] && in_array($field_data['pick_object'], $simple_tableless_objects)) {
         $params->output = 'arrays';
     }
     if (empty($value) && in_array($field_data['type'], $tableless_field_types)) {
         $params->raw = true;
         $value = false;
         if ('arrays' != $params->output && isset($this->row['_' . $params->output . '_' . $params->name])) {
             $value = $this->row['_' . $params->output . '_' . $params->name];
         } elseif ('arrays' == $params->output && isset($this->row[$params->name])) {
             $value = $this->row[$params->name];
         }
         if (false !== $value && !is_array($value) && 'pick' == $field_data['type'] && in_array($field_data['pick_object'], $simple_tableless_objects)) {
             $value = PodsForm::field_method('pick', 'simple_value', $params->name, $value, $field_data, $this->pod_data, $this->id(), true);
         }
     }
     if (empty($value) && isset($this->row[$params->name]) && (!in_array($field_data['type'], $tableless_field_types) || 'arrays' == $params->output)) {
         if (empty($field_data) || in_array($field_data['type'], array('boolean', 'number', 'currency'))) {
             $params->raw = true;
         }
         $value = $this->row[$params->name];
     } elseif (empty($value)) {
         $object_field_found = false;
         if ('object_field' == $field_type) {
             $object_field_found = true;
             if (isset($this->row[$first_field])) {
                 $value = $this->row[$first_field];
             } elseif (in_array($field_data['type'], $tableless_field_types)) {
                 $this->fields[$first_field] = $field_data;
                 $object_field_found = false;
             } else {
                 return null;
             }
         }
         if ('post_type' == $this->pod_data['type'] && !isset($this->fields[$params->name])) {
             if (!isset($this->fields['post_thumbnail']) && ('post_thumbnail' == $params->name || 0 === strpos($params->name, 'post_thumbnail.'))) {
                 $size = 'thumbnail';
                 if (0 === strpos($params->name, 'post_thumbnail.')) {
                     $field_names = explode('.', $params->name);
                     if (isset($field_names[1])) {
                         $size = $field_names[1];
                     }
                 }
                 // Pods will auto-get the thumbnail ID if this isn't an attachment
                 $value = pods_image($this->id(), $size, 0, null, true);
                 $object_field_found = true;
             } elseif (!isset($this->fields['post_thumbnail_url']) && ('post_thumbnail_url' == $params->name || 0 === strpos($params->name, 'post_thumbnail_url.'))) {
                 $size = 'thumbnail';
                 if (0 === strpos($params->name, 'post_thumbnail_url.')) {
                     $field_names = explode('.', $params->name);
                     if (isset($field_names[1])) {
                         $size = $field_names[1];
                     }
                 }
                 // Pods will auto-get the thumbnail ID if this isn't an attachment
                 $value = pods_image_url($this->id(), $size, 0, true);
                 $object_field_found = true;
             } elseif (0 === strpos($params->name, 'image_attachment.')) {
                 $size = 'thumbnail';
                 $image_id = 0;
                 $field_names = explode('.', $params->name);
                 if (isset($field_names[1])) {
                     $image_id = $field_names[1];
                 }
                 if (isset($field_names[2])) {
                     $size = $field_names[2];
                 }
                 if (!empty($image_id)) {
                     $value = pods_image($image_id, $size, 0, null, true);
                     if (!empty($value)) {
                         $object_field_found = true;
                     }
                 }
             } elseif (0 === strpos($params->name, 'image_attachment_url.')) {
                 $size = 'thumbnail';
                 $image_id = 0;
                 $field_names = explode('.', $params->name);
                 if (isset($field_names[1])) {
                     $image_id = $field_names[1];
                 }
                 if (isset($field_names[2])) {
                     $size = $field_names[2];
                 }
                 if (!empty($image_id)) {
                     $value = pods_image_url($image_id, $size, 0, true);
                     if (!empty($value)) {
                         $object_field_found = true;
                     }
                 }
             }
         } elseif ('user' == $this->pod_data['type'] && !isset($this->fields[$params->name])) {
             if (!isset($this->fields['avatar']) && ('avatar' == $params->name || 0 === strpos($params->name, 'avatar.'))) {
                 $size = null;
                 if (0 === strpos($params->name, 'avatar.')) {
                     $field_names = explode('.', $params->name);
                     if (isset($field_names[1])) {
                         $size = (int) $field_names[1];
                     }
                 }
                 if (!empty($size)) {
                     $value = get_avatar($this->id(), $size);
                 } else {
                     $value = get_avatar($this->id());
                 }
                 $object_field_found = true;
             }
         } elseif (0 === strpos($params->name, 'image_attachment.')) {
             $size = 'thumbnail';
             $image_id = 0;
             $field_names = explode('.', $params->name);
             if (isset($field_names[1])) {
                 $image_id = $field_names[1];
             }
             if (isset($field_names[2])) {
                 $size = $field_names[2];
             }
             if (!empty($image_id)) {
                 $value = pods_image($image_id, $size, 0, null, true);
                 if (!empty($value)) {
                     $object_field_found = true;
                 }
             }
         } elseif (0 === strpos($params->name, 'image_attachment_url.')) {
             $size = 'thumbnail';
             $image_id = 0;
             $field_names = explode('.', $params->name);
             if (isset($field_names[1])) {
                 $image_id = $field_names[1];
             }
             if (isset($field_names[2])) {
                 $size = $field_names[2];
             }
             if (!empty($image_id)) {
                 $value = pods_image_url($image_id, $size, 0, true);
                 if (!empty($value)) {
                     $object_field_found = true;
                 }
             }
         }
         if (false === $object_field_found) {
             $params->traverse = array($params->name);
             if (false !== strpos($params->name, '.')) {
                 $params->traverse = explode('.', $params->name);
                 $params->name = $params->traverse[0];
             }
             if (isset($this->fields[$params->name]) && isset($this->fields[$params->name]['type'])) {
                 $v = $this->do_hook('field_' . $this->fields[$params->name]['type'], null, $this->fields[$params->name], $this->row, $params);
                 if (null !== $v) {
                     return $v;
                 }
             }
             $simple = false;
             $simple_data = array();
             if (isset($this->fields[$params->name])) {
                 if ('meta' == $this->pod_data['storage']) {
                     if (!in_array($this->fields[$params->name]['type'], $tableless_field_types)) {
                         $simple = true;
                     }
                 }
                 if (in_array($this->fields[$params->name]['type'], $tableless_field_types)) {
                     $params->raw = true;
                     if ('pick' == $this->fields[$params->name]['type'] && in_array($this->fields[$params->name]['pick_object'], $simple_tableless_objects)) {
                         $simple = true;
                         $params->single = true;
                     }
                 } elseif (in_array($this->fields[$params->name]['type'], array('boolean', 'number', 'currency'))) {
                     $params->raw = true;
                 }
             }
             if (!isset($this->fields[$params->name]) || !in_array($this->fields[$params->name]['type'], $tableless_field_types) || $simple) {
                 if (null === $params->single) {
                     if (isset($this->fields[$params->name]) && !in_array($this->fields[$params->name]['type'], $tableless_field_types)) {
                         $params->single = true;
                     } else {
                         $params->single = false;
                     }
                 }
                 $no_conflict = pods_no_conflict_check($this->pod_data['type']);
                 if (!$no_conflict) {
                     pods_no_conflict_on($this->pod_data['type']);
                 }
                 if (in_array($this->pod_data['type'], array('post_type', 'media'))) {
                     $id = $this->id();
                     // Support for WPML 'duplicated' translation handling
                     if (is_object($sitepress) && $sitepress->is_translated_post_type($this->pod_data['name'])) {
                         $master_post_id = (int) get_post_meta($id, '_icl_lang_duplicate_of', true);
                         if (0 < $master_post_id) {
                             $id = $master_post_id;
                         }
                     }
                     $value = get_post_meta($id, $params->name, $params->single);
                     $single_multi = pods_var($this->fields[$params->name]['type'] . '_format_type', $this->fields[$params->name]['options'], 'single');
                     if ($simple && !is_array($value) && 'single' != $single_multi) {
                         $value = get_post_meta($id, $params->name);
                     }
                 } elseif (in_array($this->pod_data['type'], array('user', 'comment'))) {
                     $value = get_metadata($this->pod_data['type'], $this->id(), $params->name, $params->single);
                     $single_multi = pods_var($this->fields[$params->name]['type'] . '_format_type', $this->fields[$params->name]['options'], 'single');
                     if ($simple && !is_array($value) && 'single' != $single_multi) {
                         $value = get_metadata($this->pod_data['type'], $this->id(), $params->name);
                     }
                 } elseif ('settings' == $this->pod_data['type']) {
                     $value = get_option($this->pod_data['name'] . '_' . $params->name, null);
                 }
                 // Handle Simple Relationships
                 if ($simple) {
                     if (null === $params->single) {
                         $params->single = false;
                     }
                     $value = PodsForm::field_method('pick', 'simple_value', $params->name, $value, $this->fields[$params->name], $this->pod_data, $this->id(), true);
                 }
                 if (!$no_conflict) {
                     pods_no_conflict_off($this->pod_data['type']);
                 }
             } else {
                 // Dot-traversal
                 $pod = $this->pod;
                 $ids = array($this->id());
                 $all_fields = array();
                 $lookup = $params->traverse;
                 if (!empty($lookup)) {
                     unset($lookup[0]);
                     foreach ($this->fields as $field) {
                         if (!in_array($field['type'], $tableless_field_types) || in_array($field['name'], $lookup)) {
                             continue;
                         }
                         $lookup[] = $field['name'];
                     }
                 }
                 // Get fields matching traversal names
                 if (!empty($lookup)) {
                     $fields = $this->api->load_fields(array('name' => $lookup, 'type' => $tableless_field_types, 'object_fields' => true));
                     if (!empty($fields)) {
                         foreach ($fields as $field) {
                             if (!empty($field)) {
                                 if (!isset($all_fields[$field['pod']])) {
                                     $all_fields[$field['pod']] = array();
                                 }
                                 $all_fields[$field['pod']][$field['name']] = $field;
                             }
                         }
                     }
                     if (isset($this->pod_data['object_fields']) && !empty($this->pod_data['object_fields'])) {
                         foreach ($this->pod_data['object_fields'] as $object_field => $object_field_opt) {
                             if (in_array($object_field_opt['type'], $tableless_field_types)) {
                                 $all_fields[$this->pod][$object_field] = $object_field_opt;
                             }
                         }
                     }
                 }
                 $last_type = $last_object = $last_pick_val = '';
                 $last_options = array();
                 $single_multi = pods_var($this->fields[$params->name]['type'] . '_format_type', $this->fields[$params->name]['options'], 'single');
                 if ('multi' == $single_multi) {
                     $limit = (int) pods_var($this->fields[$params->name]['type'] . '_limit', $this->fields[$params->name]['options'], 0);
                 } else {
                     $limit = 1;
                 }
                 $last_limit = 0;
                 // Loop through each traversal level
                 foreach ($params->traverse as $key => $field) {
                     $last_loop = false;
                     if (count($params->traverse) <= $key + 1) {
                         $last_loop = true;
                     }
                     $field_exists = isset($all_fields[$pod][$field]);
                     $simple = false;
                     $last_options = array();
                     if ($field_exists && 'pick' == $all_fields[$pod][$field]['type'] && in_array($all_fields[$pod][$field]['pick_object'], $simple_tableless_objects)) {
                         $simple = true;
                         $last_options = $all_fields[$pod][$field];
                     }
                     // Tableless handler
                     if ($field_exists && (!in_array($all_fields[$pod][$field]['type'], array('pick', 'taxonomy')) || !$simple)) {
                         $type = $all_fields[$pod][$field]['type'];
                         $pick_object = $all_fields[$pod][$field]['pick_object'];
                         $pick_val = $all_fields[$pod][$field]['pick_val'];
                         if ('table' == $pick_object) {
                             $pick_val = pods_var('pick_table', $all_fields[$pod][$field]['options'], $pick_val, null, true);
                         } elseif ('__current__' == $pick_val) {
                             $pick_val = $pod;
                         }
                         $last_limit = 0;
                         if (in_array($type, $tableless_field_types)) {
                             $single_multi = pods_var("{$type}_format_type", $all_fields[$pod][$field]['options'], 'single');
                             if ('multi' == $single_multi) {
                                 $last_limit = (int) pods_var("{$type}_limit", $all_fields[$pod][$field]['options'], 0);
                             } else {
                                 $last_limit = 1;
                             }
                         }
                         $last_type = $type;
                         $last_object = $pick_object;
                         $last_pick_val = $pick_val;
                         $last_options = $all_fields[$pod][$field];
                         // Temporary hack until there's some better handling here
                         $last_limit = $last_limit * count($ids);
                         // Get related IDs
                         if (!isset($all_fields[$pod][$field]['pod_id'])) {
                             $all_fields[$pod][$field]['pod_id'] = 0;
                         }
                         if (isset($all_fields[$pod][$field]['id'])) {
                             $ids = $this->api->lookup_related_items($all_fields[$pod][$field]['id'], $all_fields[$pod][$field]['pod_id'], $ids, $all_fields[$pod][$field]);
                         }
                         // No items found
                         if (empty($ids)) {
                             return false;
                         } elseif (0 < $last_limit) {
                             $ids = array_slice($ids, 0, $last_limit);
                         }
                         // Get $pod if related to a Pod
                         if (!empty($pick_object) && !empty($pick_val)) {
                             if ('pod' == $pick_object) {
                                 $pod = $pick_val;
                             } else {
                                 $check = $this->api->get_table_info($pick_object, $pick_val);
                                 if (!empty($check) && !empty($check['pod'])) {
                                     $pod = $check['pod']['name'];
                                 }
                             }
                         }
                     } else {
                         // Invalid field
                         if (0 == $key) {
                             return false;
                         }
                         $last_loop = true;
                     }
                     if ($last_loop) {
                         $object_type = $last_object;
                         $object = $last_pick_val;
                         if (in_array($last_type, PodsForm::file_field_types())) {
                             $object_type = 'media';
                             $object = 'attachment';
                         }
                         $data = array();
                         $table = $this->api->get_table_info($object_type, $object, null, null, $last_options);
                         $join = $where = array();
                         if (!empty($table['join'])) {
                             $join = (array) $table['join'];
                         }
                         if (!empty($table['where']) || !empty($ids)) {
                             foreach ($ids as $id) {
                                 $where[$id] = '`t`.`' . $table['field_id'] . '` = ' . (int) $id;
                             }
                             if (!empty($where)) {
                                 $where = array(implode(' OR ', $where));
                             }
                             if (!empty($table['where'])) {
                                 $where = array_merge($where, array_values((array) $table['where']));
                             }
                         }
                         /**
                          * @var $related_obj Pods
                          */
                         $related_obj = false;
                         if ('pod' == $object_type) {
                             $related_obj = pods($object, null, false);
                         } elseif (isset($table['pod']) && !empty($table['pod'])) {
                             $related_obj = pods($table['pod']['name'], null, false);
                         }
                         if (!empty($table['table']) || !empty($related_obj)) {
                             $sql = array('select' => '*, `t`.`' . $table['field_id'] . '` AS `pod_item_id`', 'table' => $table['table'], 'join' => $join, 'where' => $where, 'orderby' => $params->orderby, 'pagination' => false, 'search' => false, 'limit' => -1);
                             // Output types
                             if (in_array($params->output, array('ids', 'objects', 'pods'))) {
                                 $sql['select'] = '`t`.`' . $table['field_id'] . '` AS `pod_item_id`';
                             } elseif ('names' == $params->output && !empty($table['field_index'])) {
                                 $sql['select'] = '`t`.`' . $table['field_index'] . '` AS `pod_item_index`, `t`.`' . $table['field_id'] . '` AS `pod_item_id`';
                             }
                             if (is_array($params->params) && !empty($params->params)) {
                                 $where = $sql['where'];
                                 $sql = array_merge($sql, $params->params);
                                 if (isset($params->params['where'])) {
                                     $sql['where'] = array_merge((array) $where, (array) $params->params['where']);
                                 }
                             }
                             if (empty($related_obj)) {
                                 if (!is_object($this->alt_data)) {
                                     $this->alt_data = pods_data(null, 0, true, true);
                                 }
                                 $item_data = $this->alt_data->select($sql);
                             } else {
                                 $item_data = $related_obj->find($sql)->data();
                             }
                             $items = array();
                             if (!empty($item_data)) {
                                 foreach ($item_data as $item) {
                                     if (is_array($item)) {
                                         $item = (object) $item;
                                     }
                                     if (empty($item->pod_item_id)) {
                                         continue;
                                     }
                                     // Bypass pass field
                                     if (isset($item->user_pass)) {
                                         unset($item->user_pass);
                                     }
                                     // Get Item ID
                                     $item_id = $item->pod_item_id;
                                     // Cleanup
                                     unset($item->pod_item_id);
                                     // Output types
                                     if ('ids' == $params->output) {
                                         $item = (int) $item_id;
                                     } elseif ('names' == $params->output && !empty($table['field_index'])) {
                                         $item = $item->pod_item_index;
                                     } elseif ('objects' == $params->output) {
                                         if (in_array($object_type, array('post_type', 'media'))) {
                                             $item = get_post($item_id);
                                         } elseif ('taxonomy' == $object_type) {
                                             $item = get_term($item_id, $object);
                                         } elseif ('user' == $object_type) {
                                             $item = get_userdata($item_id);
                                             if (!empty($item)) {
                                                 // Get other vars
                                                 $roles = $item->roles;
                                                 $caps = $item->caps;
                                                 $allcaps = $item->allcaps;
                                                 $item = $item->data;
                                                 // Set other vars
                                                 $item->roles = $roles;
                                                 $item->caps = $caps;
                                                 $item->allcaps = $allcaps;
                                                 unset($item->user_pass);
                                             }
                                         } elseif ('comment' == $object_type) {
                                             $item = get_comment($item_id);
                                         } else {
                                             $item = (object) $item;
                                         }
                                     } elseif ('pods' == $params->output) {
                                         $item = pods($object, (int) $item_id);
                                     } else {
                                         // arrays
                                         $item = get_object_vars((object) $item);
                                     }
                                     // Pass item data into $data
                                     $items[$item_id] = $item;
                                 }
                                 // Cleanup
                                 unset($item_data);
                                 // Return all of the data in the order expected
                                 if (empty($params->orderby)) {
                                     foreach ($ids as $id) {
                                         if (isset($items[$id])) {
                                             $data[$id] = $items[$id];
                                         }
                                     }
                                 }
                             }
                         }
                         if (in_array($last_type, $tableless_field_types) || in_array($last_type, array('boolean', 'number', 'currency'))) {
                             $params->raw = true;
                         }
                         if (empty($data)) {
                             $value = false;
                         } else {
                             $object_type = $table['type'];
                             if (in_array($table['type'], array('post_type', 'attachment', 'media'))) {
                                 $object_type = 'post';
                             }
                             $no_conflict = true;
                             if (in_array($object_type, array('post', 'user', 'comment', 'settings'))) {
                                 $no_conflict = pods_no_conflict_check($object_type);
                                 if (!$no_conflict) {
                                     pods_no_conflict_on($object_type);
                                 }
                             }
                             // Return entire array
                             if (false !== $field_exists && (in_array($last_type, $tableless_field_types) && !$simple)) {
                                 $value = $data;
                             } else {
                                 $value = array();
                                 foreach ($data as $item_id => $item) {
                                     // $field is 123x123, needs to be _src.123x123
                                     $full_field = implode('.', array_splice($params->traverse, $key));
                                     if ((false !== strpos($full_field, '_src') || 'guid' == $field) && (in_array($table['type'], array('attachment', 'media')) || in_array($last_type, PodsForm::file_field_types())) || (in_array($field, array('_link', 'detail_url')) || in_array($field, array('permalink', 'the_permalink')) && in_array($last_type, PodsForm::file_field_types()))) {
                                         $size = 'full';
                                         if (false !== strpos($full_field, '_src.') && 5 < strlen($full_field)) {
                                             $size = substr($full_field, 5);
                                         } elseif (false !== strpos($full_field, '_src_relative.') && 14 < strlen($full_field)) {
                                             $size = substr($full_field, 14);
                                         } elseif (false !== strpos($full_field, '_src_schemeless.') && 16 < strlen($full_field)) {
                                             $size = substr($full_field, 16);
                                         }
                                         $value_url = pods_image_url($item_id, $size);
                                         if (false !== strpos($full_field, '_src_relative') && !empty($value_url)) {
                                             $value_url_parsed = parse_url($value_url);
                                             $value_url = $value_url_parsed['path'];
                                         } elseif (false !== strpos($full_field, '_src_schemeless') && !empty($value_url)) {
                                             $value_url = str_replace(array('http://', 'https://'), '//', $value_url);
                                         }
                                         if (!empty($value_url)) {
                                             $value[] = $value_url;
                                         }
                                         $params->raw_display = true;
                                     } elseif (false !== strpos($full_field, '_img') && (in_array($table['type'], array('attachment', 'media')) || in_array($last_type, PodsForm::file_field_types()))) {
                                         $size = 'full';
                                         if (false !== strpos($full_field, '_img.') && 5 < strlen($full_field)) {
                                             $size = substr($full_field, 5);
                                         }
                                         $value[] = pods_image($item_id, $size);
                                         $params->raw_display = true;
                                     } elseif (in_array($field, array('_link', 'detail_url')) || in_array($field, array('permalink', 'the_permalink')) && 'post' == $object_type) {
                                         if ('pod' == $object_type) {
                                             if (is_object($related_obj)) {
                                                 $related_obj->fetch($item_id);
                                                 $value[] = $related_obj->field('detail_url');
                                             } else {
                                                 $value[] = '';
                                             }
                                         } elseif ('post' == $object_type) {
                                             $value[] = get_permalink($item_id);
                                         } elseif ('taxonomy' == $object_type) {
                                             $value[] = get_term_link($item_id, $object);
                                         } elseif ('user' == $object_type) {
                                             $value[] = get_author_posts_url($item_id);
                                         } elseif ('comment' == $object_type) {
                                             $value[] = get_comment_link($item_id);
                                         } else {
                                             $value[] = '';
                                         }
                                         $params->raw_display = true;
                                     } elseif (is_array($item) && isset($item[$field])) {
                                         if ($table['field_id'] == $field) {
                                             $value[] = (int) $item[$field];
                                         } else {
                                             $value[] = $item[$field];
                                         }
                                     } elseif (is_object($item) && isset($item->{$field})) {
                                         if ($table['field_id'] == $field) {
                                             $value[] = (int) $item->{$field};
                                         } else {
                                             $value[] = $item->{$field};
                                         }
                                     } elseif ('post' == $object_type) {
                                         // Support for WPML 'duplicated' translation handling
                                         if (is_object($sitepress) && $sitepress->is_translated_post_type($object)) {
                                             $master_post_id = (int) get_post_meta($item_id, '_icl_lang_duplicate_of', true);
                                             if (0 < $master_post_id) {
                                                 $item_id = $master_post_id;
                                             }
                                         }
                                         $value[] = get_post_meta($item_id, $field, true);
                                     } elseif (in_array($object_type, array('post', 'user', 'comment'))) {
                                         $value[] = get_metadata($object_type, $item_id, $field, true);
                                     } elseif ('settings' == $object_type) {
                                         $value[] = get_option($object . '_' . $field);
                                     }
                                 }
                             }
                             if (in_array($object_type, array('post', 'user', 'comment', 'settings')) && !$no_conflict) {
                                 pods_no_conflict_off($object_type);
                             }
                             // Handle Simple Relationships
                             if ($simple) {
                                 if (null === $params->single) {
                                     $params->single = false;
                                 }
                                 $value = PodsForm::field_method('pick', 'simple_value', $field, $value, $last_options, $all_fields[$pod], 0, true);
                             } elseif (false === $params->in_form && !empty($value)) {
                                 $value = array_values($value);
                             }
                             // Return a single column value
                             if (false === $params->in_form && 1 == $limit && !empty($value) && is_array($value) && 1 == count($value)) {
                                 $value = current($value);
                             }
                         }
                         break;
                     }
                 }
             }
         }
     }
     if (!empty($params->traverse) && 1 < count($params->traverse)) {
         $field_names = implode('.', $params->traverse);
         $this->row[$field_names] = $value;
     } elseif ('arrays' != $params->output && in_array($field_data['type'], $tableless_field_types)) {
         $this->row['_' . $params->output . '_' . $params->full_name] = $value;
     } elseif ('arrays' == $params->output || !in_array($field_data['type'], $tableless_field_types)) {
         $this->row[$params->full_name] = $value;
     }
     if ($params->single && is_array($value) && 1 == count($value)) {
         $value = current($value);
     }
     // @todo Expand this into traversed fields too
     if (!empty($field_data) && ($params->display || !$params->raw) && !$params->in_form && !$params->raw_display) {
         if ($params->display || ($params->get_meta || $params->deprecated) && !in_array($field_data['type'], $tableless_field_types)) {
             $field_data['options'] = pods_var_raw('options', $field_data, array(), null, true);
             $post_temp = false;
             if ('post_type' == pods_var('type', $this->pod_data) && 0 < $this->id() && (!isset($GLOBALS['post']) || empty($GLOBALS['post']))) {
                 global $post_ID, $post;
                 $post_temp = true;
                 $old_post = $GLOBALS['post'];
                 $old_ID = $GLOBALS['post_ID'];
                 $post = get_post($this->id());
                 $post_ID = $this->id();
             }
             $filter = pods_var_raw('display_filter', $field_data['options']);
             if (0 < strlen($filter)) {
                 $args = array($filter, $value);
                 $filter_args = pods_var_raw('display_filter_args', $field_data['options']);
                 if (!empty($filter_args)) {
                     $args = array_merge($args, compact($filter_args));
                 }
                 $value = call_user_func_array('apply_filters', $args);
             } elseif (1 == pods_var('display_process', $field_data['options'], 1)) {
                 $value = PodsForm::display($field_data['type'], $value, $params->name, array_merge($field_data, $field_data['options']), $this->pod_data, $this->id());
             }
             if ($post_temp) {
                 $post = $old_post;
                 $post_ID = $old_ID;
             }
         } else {
             $value = PodsForm::value($field_data['type'], $value, $params->name, array_merge($field_data, $field_data['options']), $this->pod_data, $this->id());
         }
     }
     $value = $this->do_hook('field', $value, $this->row, $params);
     return $value;
 }
コード例 #7
0
ファイル: PodsData.php プロジェクト: centaurustech/chipin
 /**
  * Fetch a new row for the current pod_data
  *
  * @param int $row Row number to fetch
  * @param bool $explicit_set Whether to set explicitly (use false when in loop)
  *
  * @return mixed
  *
  * @since 2.0
  */
 public function fetch($row = null, $explicit_set = true)
 {
     global $wpdb;
     if (null === $row) {
         $explicit_set = false;
     }
     $id = $row;
     $tableless_field_types = PodsForm::tableless_field_types();
     if (null === $row) {
         $this->row_number++;
         $this->row = false;
         if (isset($this->data[$this->row_number])) {
             $this->row = get_object_vars($this->data[$this->row_number]);
             $current_row_id = false;
             if (in_array($this->pod_data['type'], array('post_type', 'media'))) {
                 $current_row_id = pods_var_raw('ID', $this->row);
             } elseif ('taxonomy' == $this->pod_data['type']) {
                 $current_row_id = pods_var_raw('term_id', $this->row);
             } elseif ('user' == $this->pod_data['type']) {
                 $current_row_id = pods_var_raw('ID', $this->row);
             } elseif ('comment' == $this->pod_data['type']) {
                 $current_row_id = pods_var_raw('comment_ID', $this->row);
             } elseif ('settings' == $this->pod_data['type']) {
                 $current_row_id = $this->pod_data['id'];
             }
             if (0 < $current_row_id) {
                 $row = $current_row_id;
             }
         }
     }
     if (null !== $row || 'settings' == $this->pod_data['type']) {
         if ($explicit_set) {
             $this->row_number = -1;
         }
         $mode = 'id';
         $id = pods_absint($row);
         if (!is_numeric($row) || 0 === strpos($row, '0') || $row != preg_replace('/[^0-9]/', '', $row)) {
             $mode = 'slug';
             $id = $row;
         }
         $row = false;
         if (!empty($this->pod)) {
             $row = pods_cache_get($id, 'pods_items_' . $this->pod);
         }
         $current_row_id = false;
         $get_table_data = false;
         if (false !== $row && is_array($row)) {
             $this->row = $row;
         } elseif (in_array($this->pod_data['type'], array('post_type', 'media'))) {
             if ('post_type' == $this->pod_data['type']) {
                 $post_type = $this->pod_data['object'];
                 if (empty($post_type)) {
                     $post_type = $this->pod_data['name'];
                 }
             } else {
                 $post_type = 'attachment';
             }
             if ('id' == $mode) {
                 $this->row = get_post($id, ARRAY_A);
                 if (is_array($this->row) && $this->row['post_type'] != $post_type) {
                     $this->row = false;
                 }
             } else {
                 $args = array('post_type' => $post_type, 'name' => $id, 'numberposts' => 5);
                 $find = get_posts($args);
                 if (!empty($find)) {
                     $this->row = get_object_vars($find[0]);
                 }
             }
             if (empty($this->row)) {
                 $this->row = false;
             }
             $current_row_id = $this->row['ID'];
             $get_table_data = true;
         } elseif ('taxonomy' == $this->pod_data['type']) {
             $taxonomy = $this->pod_data['object'];
             if (empty($taxonomy)) {
                 $taxonomy = $this->pod_data['name'];
             }
             if ('id' == $mode) {
                 $this->row = get_term($id, $taxonomy, ARRAY_A);
             } else {
                 $this->row = get_term_by('slug', $id, $taxonomy, ARRAY_A);
             }
             if (empty($this->row)) {
                 $this->row = false;
             }
             $current_row_id = $this->row['term_id'];
             $get_table_data = true;
         } elseif ('user' == $this->pod_data['type']) {
             if ('id' == $mode) {
                 $this->row = get_userdata($id);
             } else {
                 $this->row = get_user_by('slug', $id);
             }
             if (empty($this->row)) {
                 $this->row = false;
             } else {
                 $this->row = get_object_vars($this->row);
             }
             $current_row_id = $this->row['ID'];
             $get_table_data = true;
         } elseif ('comment' == $this->pod_data['type']) {
             $this->row = get_comment($id, ARRAY_A);
             // No slug handling here
             if (empty($this->row)) {
                 $this->row = false;
             }
             $current_row_id = $this->row['comment_ID'];
             $get_table_data = true;
         } elseif ('settings' == $this->pod_data['type']) {
             $this->row = array();
             if (empty($this->fields)) {
                 $this->row = false;
             } else {
                 foreach ($this->fields as $field) {
                     if (!in_array($field['type'], $tableless_field_types)) {
                         $this->row[$field['name']] = get_option($this->pod_data['name'] . '_' . $field['name'], null);
                     }
                 }
                 // Force ID
                 $this->id = $this->pod_data['id'];
                 $this->row['option_id'] = $this->id;
             }
         } else {
             $params = array('table' => $this->table, 'where' => "`t`.`{$this->field_id}` = " . (int) $id, 'orderby' => "`t`.`{$this->field_id}` DESC", 'page' => 1, 'limit' => 1, 'search' => false);
             if ('slug' == $mode && !empty($this->field_slug)) {
                 $id = esc_sql($id);
                 $params['where'] = "`t`.`{$this->field_slug}` = '{$id}'";
             }
             $this->row = pods_data()->select($params);
             if (empty($this->row)) {
                 $this->row = false;
             } else {
                 $current_row = (array) $this->row;
                 $this->row = get_object_vars((object) @current($current_row));
             }
         }
         if ('table' == $this->pod_data['storage'] && false !== $get_table_data && is_numeric($current_row_id)) {
             $params = array('table' => $wpdb->prefix . "pods_", 'where' => "`t`.`id` = {$current_row_id}", 'orderby' => "`t`.`id` DESC", 'page' => 1, 'limit' => 1, 'search' => false, 'strict' => true);
             if (empty($this->pod_data['object'])) {
                 $params['table'] .= $this->pod_data['name'];
             } else {
                 $params['table'] .= $this->pod_data['object'];
             }
             $row = pods_data()->select($params);
             if (!empty($row)) {
                 $current_row = (array) $row;
                 $row = get_object_vars((object) @current($current_row));
                 if (is_array($this->row) && !empty($this->row)) {
                     $this->row = array_merge($row, $this->row);
                 } else {
                     $this->row = $row;
                 }
             }
         }
         if (!empty($this->pod)) {
             pods_cache_set($id, $this->row, 'pods_items_' . $this->pod, 0);
         }
     }
     $this->row = $this->do_hook('fetch', $this->row, $id, $this->row_number);
     return $this->row;
 }