Esempio n. 1
0
/**
 * Retrieve Bookmark data
 *
 * @since 2.1.0
 * @uses $nxtdb Database Object
 *
 * @param mixed $bookmark
 * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
 * @param string $filter Optional, default is 'raw'.
 * @return array|object Type returned depends on $output value.
 */
function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw')
{
    global $nxtdb;
    if (empty($bookmark)) {
        if (isset($GLOBALS['link'])) {
            $_bookmark =& $GLOBALS['link'];
        } else {
            $_bookmark = null;
        }
    } elseif (is_object($bookmark)) {
        nxt_cache_add($bookmark->link_id, $bookmark, 'bookmark');
        $_bookmark = $bookmark;
    } else {
        if (isset($GLOBALS['link']) && $GLOBALS['link']->link_id == $bookmark) {
            $_bookmark =& $GLOBALS['link'];
        } elseif (!($_bookmark = nxt_cache_get($bookmark, 'bookmark'))) {
            $_bookmark = $nxtdb->get_row($nxtdb->prepare("SELECT * FROM {$nxtdb->links} WHERE link_id = %d LIMIT 1", $bookmark));
            $_bookmark->link_category = array_unique(nxt_get_object_terms($_bookmark->link_id, 'link_category', array('fields' => 'ids')));
            nxt_cache_add($_bookmark->link_id, $_bookmark, 'bookmark');
        }
    }
    $_bookmark = sanitize_bookmark($_bookmark, $filter);
    if ($output == OBJECT) {
        return $_bookmark;
    } elseif ($output == ARRAY_A) {
        return get_object_vars($_bookmark);
    } elseif ($output == ARRAY_N) {
        return array_values(get_object_vars($_bookmark));
    } else {
        return $_bookmark;
    }
}
Esempio n. 2
0
function get_topic($id, $cache = true)
{
    global $bbdb;
    if (!is_numeric($id)) {
        list($slug, $sql) = bb_get_sql_from_slug('topic', $id);
        $id = nxt_cache_get($slug, 'bb_topic_slug');
    }
    // not else
    if (is_numeric($id)) {
        $id = (int) $id;
        $sql = "topic_id = {$id}";
    }
    if (0 === $id || !$sql) {
        return false;
    }
    // &= not =&
    $cache &= 'AND topic_status = 0' == ($where = apply_filters('get_topic_where', 'AND topic_status = 0'));
    if (($cache || !$where) && is_numeric($id) && false !== ($topic = nxt_cache_get($id, 'bb_topic'))) {
        return $topic;
    }
    // $where is NOT bbdb:prepared
    $topic = $bbdb->get_row("SELECT * FROM {$bbdb->topics} WHERE {$sql} {$where}");
    $topic = bb_append_meta($topic, 'topic');
    if ($cache) {
        nxt_cache_set($topic->topic_id, $topic, 'bb_topic');
        nxt_cache_add($topic->topic_slug, $topic->topic_id, 'bb_topic_slug');
    }
    return $topic;
}
Esempio n. 3
0
/**
 * Retrieves all category IDs.
 *
 * @since 2.0.0
 * @link http://codex.nxtclass.org/Function_Reference/get_all_category_ids
 *
 * @return object List of all of the category IDs.
 */
function get_all_category_ids()
{
    if (!($cat_ids = nxt_cache_get('all_category_ids', 'category'))) {
        $cat_ids = get_terms('category', array('fields' => 'ids', 'get' => 'all'));
        nxt_cache_add('all_category_ids', $cat_ids, 'category');
    }
    return $cat_ids;
}
Esempio n. 4
0
 /**
  * Updates Terms to Taxonomy in cache.
  *
  * @package NXTClass
  * @subpackage Taxonomy
  * @since 2.3.0
  *
  * @param array $terms List of Term objects to change
  * @param string $taxonomy Optional. Update Term to this taxonomy in cache
  */
 function update_term_cache($terms, $taxonomy = '')
 {
     foreach ((array) $terms as $term) {
         $term_taxonomy = $taxonomy;
         if (empty($term_taxonomy)) {
             $term_taxonomy = $term->taxonomy;
         }
         nxt_cache_add($term->term_id, $term, $term_taxonomy);
         nxt_cache_add($term->term_taxonomy_id, $term->term_id, "{$term_taxonomy}:tt_id");
     }
 }
 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;
 }
Esempio n. 6
0
/**
 * Adds an objects meta data to the object
 *
 * This is the only function that should add to user / topic - NOT bbdb::prepared
 *
 * @internal
 */
function bb_append_meta($object, $type)
{
    global $bbdb;
    switch ($type) {
        case 'user':
            global $nxt_users_object;
            return $nxt_users_object->append_meta($object);
            break;
        case 'forum':
            $object_id_column = 'forum_id';
            $object_type = 'bb_forum';
            $slug = 'forum_slug';
            break;
        case 'topic':
            $object_id_column = 'topic_id';
            $object_type = 'bb_topic';
            $slug = 'topic_slug';
            break;
        case 'post':
            $object_id_column = 'post_id';
            $object_type = 'bb_post';
            $slug = false;
            break;
    }
    if (is_array($object) && count($object)) {
        $trans = array();
        foreach (array_keys($object) as $i) {
            $trans[$object[$i]->{$object_id_column}] =& $object[$i];
        }
        $ids = array_map('intval', array_keys($trans));
        $query_ids = array();
        $cached_objects = array();
        $position = 0;
        foreach ($ids as $_id) {
            if (false !== ($_cached_object = nxt_cache_get($_id, $object_type))) {
                $cached_objects[$position] = $_cached_object;
            } else {
                $query_ids[$position] = $_id;
            }
            $position++;
        }
        if (!count($query_ids)) {
            return $cached_objects;
        }
        $_query_ids = $query_ids;
        sort($_query_ids);
        $_query_ids = join(',', $_query_ids);
        if ($metas = $bbdb->get_results("SELECT `object_id`, `meta_key`, `meta_value` FROM `{$bbdb->meta}` WHERE `object_type` = '{$object_type}' AND `object_id` IN ({$_query_ids}) /* bb_append_meta */")) {
            usort($metas, '_bb_append_meta_sort');
            foreach ($metas as $meta) {
                $trans[$meta->object_id]->{$meta->meta_key} = maybe_unserialize($meta->meta_value);
                if (strpos($meta->meta_key, $bbdb->prefix) === 0) {
                    $trans[$meta->object_id]->{substr($meta->meta_key, strlen($bbdb->prefix))} = maybe_unserialize($meta->meta_value);
                }
            }
        }
        foreach ($query_ids as $position => $i) {
            $cached_objects[$position] = $trans[$i];
            nxt_cache_add($i, $trans[$i], $object_type);
            if ($slug) {
                nxt_cache_add($trans[$i]->{$slug}, $i, 'bb_' . $slug);
            }
        }
        if (!count($cached_objects)) {
            return $object;
        }
        ksort($cached_objects);
        return $cached_objects;
    } elseif ($object) {
        if (false !== ($cached_object = nxt_cache_get($object->{$object_id_column}, $object_type))) {
            return $cached_object;
        }
        if ($metas = $bbdb->get_results($bbdb->prepare("SELECT `object_id`, `meta_key`, `meta_value` FROM `{$bbdb->meta}` WHERE `object_type` = '{$object_type}' AND `object_id` = %d /* bb_append_meta */", $object->{$object_id_column}))) {
            usort($metas, '_bb_append_meta_sort');
            foreach ($metas as $meta) {
                $object->{$meta->meta_key} = maybe_unserialize($meta->meta_value);
                if (strpos($meta->meta_key, $bbdb->prefix) === 0) {
                    $object->{substr($meta->meta_key, strlen($bbdb->prefix))} = $object->{$meta->meta_key};
                }
            }
        }
        if ($object->{$object_id_column}) {
            nxt_cache_add($object->{$object_id_column}, $object, $object_type);
            if ($slug) {
                nxt_cache_add($object->{$slug}, $object->{$object_id_column}, 'bb_' . $slug);
            }
        }
        return $object;
    }
}
Esempio n. 7
0
/**
 * Updates posts in cache.
 *
 * @usedby update_page_cache() Aliased by this function.
 *
 * @package NXTClass
 * @subpackage Cache
 * @since 1.5.1
 *
 * @param array $posts Array of post objects
 */
function update_post_cache(&$posts)
{
    if (!$posts) {
        return;
    }
    foreach ($posts as $post) {
        nxt_cache_add($post->ID, $post, 'posts');
    }
}
Esempio n. 8
0
function bb_cache_posts($query, $post_id_query = false)
{
    global $bbdb;
    $_query = '';
    $_query_post_ids = array();
    $_query_posts = array();
    $_cached_posts = array();
    $ordered_post_ids = array();
    if ($post_id_query && is_string($query)) {
        // The query is a SQL query to retrieve post_ids only
        $key = md5($query);
        if (false === ($post_ids = nxt_cache_get($key, 'bb_cache_posts_post_ids'))) {
            if (!($post_ids = (array) $bbdb->get_col($query, 0))) {
                return array();
            }
            nxt_cache_add($key, $post_ids, 'bb_cache_posts_post_ids');
        }
        $query = $post_ids;
    }
    if (is_array($query)) {
        $get_order_from_query = false;
        foreach ($query as $_post_id) {
            $ordered_post_ids[] = $_post_id;
            if (false === ($_post = nxt_cache_get($_post_id, 'bb_post'))) {
                $_query_post_ids[] = $_post_id;
            } else {
                $_cached_posts[$_post->post_id] = $_post;
            }
        }
        if (count($_query_post_ids)) {
            // Escape the ids for the SQL query
            $_query_post_ids = $bbdb->escape_deep($_query_post_ids);
            // Sort the ids so the MySQL will more consistently cache the query
            sort($_query_post_ids);
            $_query = "SELECT * FROM {$bbdb->posts} WHERE post_id IN ('" . join("','", $_query_post_ids) . "')";
        }
    } else {
        // The query is a full SQL query which needs to be executed
        $get_order_from_query = true;
        $_query = $query;
    }
    if ($_query_posts = (array) $bbdb->get_results($_query)) {
        $_appendable_posts = array();
        foreach ($_query_posts as $_query_post) {
            if ($get_order_from_query) {
                $ordered_post_ids[] = $_query_post->post_id;
            }
            if (false === ($_post = nxt_cache_get($_query_post->post_id, 'bb_post'))) {
                $_appendable_posts[] = $_query_post;
            } else {
                $_cached_posts[$_query_post->post_id] = $_post;
            }
        }
        if (count($_appendable_posts)) {
            $_query_posts = bb_append_meta($_appendable_posts, 'post');
            foreach ($_query_posts as $_query_post) {
                nxt_cache_add($_query_post->post_id, $_query_post, 'bb_post');
            }
        } else {
            $_query_posts = array();
        }
    } else {
        $_query_posts = array();
    }
    foreach (array_merge($_cached_posts, $_query_posts) as $_post) {
        $keyed_posts[$_post->post_id] = $_post;
    }
    $the_posts = array();
    foreach ($ordered_post_ids as $ordered_post_id) {
        $the_posts[] = $keyed_posts[$ordered_post_id];
    }
    return $the_posts;
}
Esempio n. 9
0
/**
 * Update the metadata cache for the specified objects.
 *
 * @since 2.9.0
 * @uses $nxtdb NXTClass database object for queries.
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
 * @param int|array $object_ids array or comma delimited list of object IDs to update cache for
 * @return mixed Metadata cache for the specified objects, or false on failure.
 */
function update_meta_cache($meta_type, $object_ids)
{
    if (empty($meta_type) || empty($object_ids)) {
        return false;
    }
    if (!($table = _get_meta_table($meta_type))) {
        return false;
    }
    $column = esc_sql($meta_type . '_id');
    global $nxtdb;
    if (!is_array($object_ids)) {
        $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
        $object_ids = explode(',', $object_ids);
    }
    $object_ids = array_map('intval', $object_ids);
    $cache_key = $meta_type . '_meta';
    $ids = array();
    $cache = array();
    foreach ($object_ids as $id) {
        $cached_object = nxt_cache_get($id, $cache_key);
        if (false === $cached_object) {
            $ids[] = $id;
        } else {
            $cache[$id] = $cached_object;
        }
    }
    if (empty($ids)) {
        return $cache;
    }
    // Get meta info
    $id_list = join(',', $ids);
    $meta_list = $nxtdb->get_results($nxtdb->prepare("SELECT {$column}, meta_key, meta_value FROM {$table} WHERE {$column} IN ({$id_list})", $meta_type), ARRAY_A);
    if (!empty($meta_list)) {
        foreach ($meta_list as $metarow) {
            $mpid = intval($metarow[$column]);
            $mkey = $metarow['meta_key'];
            $mval = $metarow['meta_value'];
            // Force subkeys to be array type:
            if (!isset($cache[$mpid]) || !is_array($cache[$mpid])) {
                $cache[$mpid] = array();
            }
            if (!isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey])) {
                $cache[$mpid][$mkey] = array();
            }
            // Add a value to the current pid/key:
            $cache[$mpid][$mkey][] = $mval;
        }
    }
    foreach ($ids as $id) {
        if (!isset($cache[$id])) {
            $cache[$id] = array();
        }
        nxt_cache_add($id, $cache[$id], $cache_key);
    }
    return $cache;
}
Esempio n. 10
0
/**
 * Loads and caches all autoloaded options, if available or all options.
 *
 * @since 2.2.0
 * @package NXTClass
 * @subpackage Option
 *
 * @return array List of all options.
 */
function nxt_load_alloptions()
{
    global $nxtdb;
    if (!defined('nxt_INSTALLING') || !is_multisite()) {
        $alloptions = nxt_cache_get('alloptions', 'options');
    } else {
        $alloptions = false;
    }
    if (!$alloptions) {
        $suppress = $nxtdb->suppress_errors();
        if (!($alloptions_db = $nxtdb->get_results("SELECT option_name, option_value FROM {$nxtdb->options} WHERE autoload = 'yes'"))) {
            $alloptions_db = $nxtdb->get_results("SELECT option_name, option_value FROM {$nxtdb->options}");
        }
        $nxtdb->suppress_errors($suppress);
        $alloptions = array();
        foreach ((array) $alloptions_db as $o) {
            $alloptions[$o->option_name] = $o->option_value;
        }
        if (!defined('nxt_INSTALLING') || !is_multisite()) {
            nxt_cache_add('alloptions', $alloptions, 'options');
        }
    }
    return $alloptions;
}
Esempio n. 11
0
/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int $id Post ID. Is not optional.
 * @param string $taxonomy Taxonomy name.
 * @return array|bool False on failure. Array of term objects on success.
 */
function get_the_terms($id = 0, $taxonomy)
{
    global $post;
    $id = (int) $id;
    if (!$id) {
        if (!$post->ID) {
            return false;
        } else {
            $id = (int) $post->ID;
        }
    }
    $terms = get_object_term_cache($id, $taxonomy);
    if (false === $terms) {
        $terms = nxt_get_object_terms($id, $taxonomy);
        nxt_cache_add($id, $terms, $taxonomy . '_relationships');
    }
    $terms = apply_filters('get_the_terms', $terms, $id, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}
Esempio n. 12
0
/**
 * Update all user caches
 *
 * @since 3.0.0
 *
 * @param object $user User object to be cached
 */
function update_user_caches($user)
{
    nxt_cache_add($user->ID, $user, 'users');
    nxt_cache_add($user->user_login, $user->ID, 'userlogins');
    nxt_cache_add($user->user_email, $user->ID, 'useremail');
    nxt_cache_add($user->user_nicename, $user->ID, 'userslugs');
}
Esempio n. 13
0
/**
 * Get the permalink for a post on another blog.
 *
 * @since MU 1.0
 *
 * @param int $_blog_id ID of the source blog.
 * @param int $post_id ID of the desired post.
 * @return string The post's permalink
 */
function get_blog_permalink($_blog_id, $post_id)
{
    $key = "{$_blog_id}-{$post_id}-blog_permalink";
    $link = nxt_cache_get($key, 'site-options');
    if ($link == false) {
        switch_to_blog($_blog_id);
        $link = get_permalink($post_id);
        restore_current_blog();
        nxt_cache_add($key, $link, 'site-options', 360);
    }
    return $link;
}
Esempio n. 14
0
function bb_get_forum($id)
{
    global $bbdb;
    if (!is_numeric($id)) {
        list($slug, $sql) = bb_get_sql_from_slug('forum', $id);
        $id = nxt_cache_get($slug, 'bb_forum_slug');
    }
    // not else
    if (is_numeric($id)) {
        $sql = $bbdb->prepare("forum_id = %d", $id);
    }
    if (0 === $id || empty($sql)) {
        return false;
    }
    // $where is NOT bbdb:prepared
    if ($where = apply_filters('get_forum_where', '')) {
        $forum = $bbdb->get_row("SELECT * FROM {$bbdb->forums} WHERE {$sql} {$where}");
        return bb_append_meta($forum, 'forum');
    }
    if (is_numeric($id) && false !== ($forum = nxt_cache_get($id, 'bb_forum'))) {
        return $forum;
    }
    $forum = $bbdb->get_row("SELECT * FROM {$bbdb->forums} WHERE {$sql}");
    $forum = bb_append_meta($forum, 'forum');
    nxt_cache_set($forum->forum_id, $forum, 'bb_forum');
    nxt_cache_add($forum->forum_slug, $forum->forum_id, 'bb_forum_slug');
    return $forum;
}
Esempio n. 15
0
 /**
  * Execute the query
  *
  * @since 3.1.0
  *
  * @param string|array $query_vars
  * @return int|array
  */
 function query($query_vars)
 {
     global $nxtdb;
     $defaults = array('author_email' => '', 'ID' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_ID' => '', 'post_id' => 0, 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => '', 'type' => '', 'user_id' => '', 'search' => '', 'count' => false);
     $this->query_vars = nxt_parse_args($query_vars, $defaults);
     do_action_ref_array('pre_get_comments', array(&$this));
     extract($this->query_vars, EXTR_SKIP);
     // $args can be whatever, only use the args defined in defaults to compute the key
     $key = md5(serialize(compact(array_keys($defaults))));
     $last_changed = nxt_cache_get('last_changed', 'comment');
     if (!$last_changed) {
         $last_changed = time();
         nxt_cache_set('last_changed', $last_changed, 'comment');
     }
     $cache_key = "get_comments:{$key}:{$last_changed}";
     if ($cache = nxt_cache_get($cache_key, 'comment')) {
         return $cache;
     }
     $post_id = absint($post_id);
     if ('hold' == $status) {
         $approved = "comment_approved = '0'";
     } elseif ('approve' == $status) {
         $approved = "comment_approved = '1'";
     } elseif ('spam' == $status) {
         $approved = "comment_approved = 'spam'";
     } elseif ('trash' == $status) {
         $approved = "comment_approved = 'trash'";
     } else {
         $approved = "( comment_approved = '0' OR comment_approved = '1' )";
     }
     $order = 'ASC' == strtoupper($order) ? 'ASC' : 'DESC';
     if (!empty($orderby)) {
         $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\\s]/', $orderby);
         $ordersby = array_intersect($ordersby, array('comment_agent', 'comment_approved', 'comment_author', 'comment_author_email', 'comment_author_IP', 'comment_author_url', 'comment_content', 'comment_date', 'comment_date_gmt', 'comment_ID', 'comment_karma', 'comment_parent', 'comment_post_ID', 'comment_type', 'user_id'));
         $orderby = empty($ordersby) ? 'comment_date_gmt' : implode(', ', $ordersby);
     } else {
         $orderby = 'comment_date_gmt';
     }
     $number = absint($number);
     $offset = absint($offset);
     if (!empty($number)) {
         if ($offset) {
             $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
     } else {
         $limits = '';
     }
     if ($count) {
         $fields = 'COUNT(*)';
     } else {
         $fields = '*';
     }
     $join = '';
     $where = $approved;
     if (!empty($post_id)) {
         $where .= $nxtdb->prepare(' AND comment_post_ID = %d', $post_id);
     }
     if ('' !== $author_email) {
         $where .= $nxtdb->prepare(' AND comment_author_email = %s', $author_email);
     }
     if ('' !== $karma) {
         $where .= $nxtdb->prepare(' AND comment_karma = %d', $karma);
     }
     if ('comment' == $type) {
         $where .= " AND comment_type = ''";
     } elseif ('pings' == $type) {
         $where .= ' AND comment_type IN ("pingback", "trackback")';
     } elseif (!empty($type)) {
         $where .= $nxtdb->prepare(' AND comment_type = %s', $type);
     }
     if ('' !== $parent) {
         $where .= $nxtdb->prepare(' AND comment_parent = %d', $parent);
     }
     if ('' !== $user_id) {
         $where .= $nxtdb->prepare(' AND user_id = %d', $user_id);
     }
     if ('' !== $search) {
         $where .= $this->get_search_sql($search, array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
     }
     $post_fields = array_filter(compact(array('post_author', 'post_name', 'post_parent', 'post_status', 'post_type')));
     if (!empty($post_fields)) {
         $join = "JOIN {$nxtdb->posts} ON {$nxtdb->posts}.ID = {$nxtdb->comments}.comment_post_ID";
         foreach ($post_fields as $field_name => $field_value) {
             $where .= $nxtdb->prepare(" AND {$nxtdb->posts}.{$field_name} = %s", $field_value);
         }
     }
     $pieces = array('fields', 'join', 'where', 'orderby', 'order', 'limits');
     $clauses = apply_filters_ref_array('comments_clauses', array(compact($pieces), &$this));
     foreach ($pieces as $piece) {
         ${$piece} = isset($clauses[$piece]) ? $clauses[$piece] : '';
     }
     $query = "SELECT {$fields} FROM {$nxtdb->comments} {$join} WHERE {$where} ORDER BY {$orderby} {$order} {$limits}";
     if ($count) {
         return $nxtdb->get_var($query);
     }
     $comments = $nxtdb->get_results($query);
     $comments = apply_filters_ref_array('the_comments', array($comments, &$this));
     nxt_cache_add($cache_key, $comments, 'comment');
     return $comments;
 }