function get_user($user_id = 0, $args = null) { $defaults = array('output' => OBJECT, 'by' => false, 'from_cache' => true, 'append_meta' => true); $args = nxt_parse_args($args, $defaults); extract($args, EXTR_SKIP); if (!$user_id) { return false; } // Let's just deal with arrays $user_ids = (array) $user_id; if (!count($user_ids)) { return false; } // Validate passed ids $safe_user_ids = array(); foreach ($user_ids as $_user_id) { switch ($by) { case 'login': $safe_user_ids[] = $this->sanitize_user($_user_id, true); break; case 'email': if ($this->is_email($_user_id)) { $safe_user_ids[] = $_user_id; } break; case 'nicename': $safe_user_ids[] = $this->sanitize_nicename($_user_id); break; default: if (is_numeric($_user_id)) { $safe_user_ids[] = (int) $_user_id; } else { // If one $_user_id is non-numerical, treat all $user_ids as user_logins $safe_user_ids[] = $this->sanitize_user($_user_id, true); $by = 'login'; } break; } } // No soup for you! if (!count($safe_user_ids)) { return false; } // Name the cache storing non-existant ids and the SQL field to query by switch ($by) { case 'login': $non_existant_cache = 'userlogins'; $sql_field = 'user_login'; break; case 'email': $non_existant_cache = 'useremail'; $sql_field = 'user_email'; break; case 'nicename': $non_existant_cache = 'usernicename'; $sql_field = 'user_nicename'; break; default: $non_existant_cache = 'users'; $sql_field = 'ID'; break; } // Check if the numeric user IDs exist from caches $cached_users = array(); if ($from_cache) { $existant_user_ids = array(); $maybe_existant_user_ids = array(); switch ($by) { case 'login': case 'email': case 'nicename': foreach ($safe_user_ids as $_safe_user_id) { $ID = nxt_cache_get($_safe_user_id, $non_existant_cache); if (false === $ID) { $maybe_existant_user_ids[] = $_safe_user_id; } elseif (0 !== $ID) { $existant_user_ids[] = $ID; } } if (count($existant_user_ids)) { // We need to run again using numeric ids $args['by'] = false; $cached_users = $this->get_user($existant_user_ids, $args); } break; default: foreach ($safe_user_ids as $_safe_user_id) { $user = nxt_cache_get($_safe_user_id, 'users'); if (false === $user) { $maybe_existant_user_ids[] = $_safe_user_id; } elseif (0 !== $user) { $cached_users[] = $user; } } break; } // No maybes? Then it's definite. if (!count($maybe_existant_user_ids)) { if (!count($cached_users)) { // Nothing there sorry return false; } // Deal with the case where one record was requested but multiple records are returned if (!is_array($user_id) && $user_id) { if (1 < count($cached_users)) { if ('user_email' == $sql_field) { $err = __('Multiple email matches. Log in with your username.'); } else { $err = sprintf(__('Multiple %s matches'), $sql_field); } return new nxt_Error($sql_field, $err, $args + array('user_id' => $user_id, 'unique' => false)); } // If one item was requested, it expects a single user object back $cached_users = array_shift($cached_users); } backpress_convert_object($cached_users, $output); return $cached_users; } // If we get this far, there are some maybes so try and grab them } else { $maybe_existant_user_ids = $safe_user_ids; } // Escape the ids for the SQL query $maybe_existant_user_ids = $this->db->escape_deep($maybe_existant_user_ids); // Sort the ids so the MySQL will more consistently cache the query sort($maybe_existant_user_ids); // Get the users from the database $sql = "SELECT * FROM `{$this->db->users}` WHERE `{$sql_field}` in ('" . join("','", $maybe_existant_user_ids) . "');"; $db_users = $this->db->get_results($sql); // Merge in the cached users if available if (count($cached_users)) { // Create a convenient array of database fetched user ids $db_user_ids = array(); foreach ($db_users as $_db_user) { $db_user_ids[] = $_db_user->ID; } $users = array_merge($cached_users, $db_users); } else { $users = $db_users; } // Deal with the case where one record was requested but multiple records are returned if (!is_array($user_id) && $user_id) { if (1 < count($users)) { if ('user_email' == $sql_field) { $err = __('Multiple email matches. Log in with your username.'); } else { $err = sprintf(__('Multiple %s matches'), $sql_field); } return new nxt_Error($sql_field, $err, $args + array('user_id' => $user_id, 'unique' => false)); } } // Create a convenient array of final user ids $final_user_ids = array(); foreach ($users as $_user) { $final_user_ids[] = $_user->{$sql_field}; } foreach ($safe_user_ids as $_safe_user_id) { if (!in_array($_safe_user_id, $final_user_ids)) { nxt_cache_add($_safe_user_id, 0, $non_existant_cache); } } if (!count($users)) { return false; } // Add display names $final_users = array(); foreach ($users as $_user) { // Make sure there is a display_name set if (!$_user->display_name) { $_user->display_name = $_user->user_login; } $final_users[] = $_user; } // append_meta() does the user object, useremail, userlogins caching if ($append_meta) { if (count($cached_users)) { $db_final_users = array(); $cached_final_users = array(); foreach ($final_users as $final_user) { if (in_array($final_user->ID, $db_user_ids)) { $db_final_users[] = $final_user; } else { $cached_final_users[] = $final_user; } } $db_final_users = $this->append_meta($db_final_users); $final_users = array_merge($cached_final_users, $db_final_users); } else { $final_users = $this->append_meta($final_users); } } // If one item was requested, it expects a single user object back if (!is_array($user_id) && $user_id) { $final_users = array_shift($final_users); } backpress_convert_object($final_users, $output); return $final_users; }
/** * Get all Term data from database by Term field and data. * * Warning: $value is not escaped for 'name' $field. You must do it yourself, if * required. * * The default $field is 'id', therefore it is possible to also use null for * field, but not recommended that you do so. * * If $value does not exist, the return value will be false. If $taxonomy exists * and $field and $value combinations exist, the Term will be returned. * * @package NXTClass * @subpackage Taxonomy * @since 2.3.0 * * @uses $this->sanitize_term() Cleanses the term based on $filter context before returning. * @see $this->sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. * * @param string $field Either 'slug', 'name', 'id', or 'tt_id' * @param string|int $value Search for this term value * @param string $taxonomy Taxonomy Name * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N * @param string $filter Optional, default is raw or no NXTClass defined filter will applied. * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found. */ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { if (!$this->is_taxonomy($taxonomy)) { return false; } if ('slug' == $field) { $field = 't.slug'; $value = $this->sanitize_term_slug($value, $taxonomy); if (empty($value)) { return false; } } else { if ('name' == $field) { // Assume already escaped $field = 't.name'; } else { if ('tt_id' == $field) { $field = 'tt.term_taxonomy_id'; $value = (int) $value; if ($_term_id = nxt_cache_get($value, "{$taxonomy}:tt_id")) { return $this->get_term($_term_id, $taxonomy, $output, $filter); } } else { $field = 't.term_id'; $value = (int) $value; } } } $term = $this->db->get_row($this->db->prepare("SELECT t.*, tt.* FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$field} = %s LIMIT 1", $taxonomy, $value)); if (!$term) { return false; } nxt_cache_add($term->term_id, $term, $taxonomy); nxt_cache_add($term->term_taxonomy_id, $term->term_id, "{$taxonomy}:tt_id"); $term = $this->sanitize_term($term, $taxonomy, $filter); backpress_convert_object($term, $output); return $term; }
/** * Convert object to given output format. * * bbPress needs this to convert vars. * * @param object $object Object to convert. * @param string $output Type of object to return. OBJECT, ARRAY_A, or ARRAY_N. */ function backpress_convert_object(&$object, $output) { if (is_array($object)) { foreach (array_keys($object) as $key) { backpress_convert_object($object[$key], $output); } } else { switch ($output) { case OBJECT: break; case ARRAY_A: $object = get_object_vars($object); break; case ARRAY_N: $object = array_values(get_object_vars($object)); break; } } }