Esempio n. 1
0
/**
 * Retrieve a list of comments.
 *
 * The comment list can be for the blog as a whole or for an individual post.
 *
 * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt',
 * 'order', 'number', 'offset', and 'post_id'.
 *
 * @since 2.7.0
 * @uses $wpdb
 *
 * @param mixed $args Optional. Array or string of options to override defaults.
 * @return array List of comments.
 */
function get_comments($args = '')
{
    global $wpdb;
    $defaults = array('author_email' => '', 'ID' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_ID' => '', 'post_id' => 0, 'status' => '', 'type' => '', 'user_id' => '', 'search' => '', 'count' => false);
    $args = wp_parse_args($args, $defaults);
    extract($args, 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 = wp_cache_get('last_changed', 'comment');
    if (!$last_changed) {
        $last_changed = time();
        wp_cache_set('last_changed', $last_changed, 'comment');
    }
    $cache_key = "get_comments:{$key}:{$last_changed}";
    if ($cache = wp_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) {
            $limit = 'LIMIT ' . $offset . ',' . $number;
        } else {
            $limit = 'LIMIT ' . $number;
        }
    } else {
        $limit = '';
    }
    $post_where = "WHERE {$approved}";
    if (!empty($post_id)) {
        $post_where .= $wpdb->prepare(' AND comment_post_ID = %d', $post_id);
    }
    if ('' !== $author_email) {
        $post_where .= $wpdb->prepare('AND comment_author_email = %s', $author_email);
    }
    if ('' !== $karma) {
        $post_where .= $wpdb->prepare('AND comment_karma = %d', $karma);
    }
    if ('comment' == $type) {
        $post_where .= " AND comment_type = ''";
    } elseif (!empty($type)) {
        $post_where .= $wpdb->prepare(' AND comment_type = %s', $type);
    }
    if ('' !== $parent) {
        $post_where .= $wpdb->prepare(' AND comment_parent = %d', $parent);
    }
    if ('' !== $user_id) {
        $post_where .= $wpdb->prepare(' AND user_id = %d', $user_id);
    }
    if ('' !== $search) {
        $post_where .= _wp_search_sql($search, array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
    }
    if ($count) {
        return $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} {$post_where} ORDER BY {$orderby} {$order} {$limit}");
    }
    $comments = $wpdb->get_results("SELECT * FROM {$wpdb->comments} {$post_where} ORDER BY {$orderby} {$order} {$limit}");
    wp_cache_add($cache_key, $comments, 'comment');
    return $comments;
}
Esempio n. 2
0
 /**
  * Prepare the query variables
  *
  * @since 3.1.0
  * @access private
  */
 function prepare_query()
 {
     global $wpdb;
     $qv =& $this->query_vars;
     $this->query_from = " FROM {$wpdb->users}";
     $this->query_where = " WHERE 1=1";
     // sorting
     if (in_array($qv['orderby'], array('email', 'url', 'registered'))) {
         $orderby = 'user_' . $qv['orderby'];
     } elseif ('name' == $qv['orderby']) {
         $orderby = 'display_name';
     } elseif ('post_count' == $qv['orderby']) {
         $where = get_posts_by_author_sql('post');
         $this->query_from .= " LEFT OUTER JOIN (\n\t\t\t\tSELECT post_author, COUNT(*) as post_count\n\t\t\t\tFROM wp_posts\n\t\t\t\t{$where}\n\t\t\t\tGROUP BY post_author\n\t\t\t) p ON (wp_users.ID = p.post_author)\n\t\t\t";
         $orderby = 'post_count';
     } else {
         $orderby = 'user_login';
     }
     $qv['order'] = strtoupper($qv['order']);
     if ('ASC' == $qv['order']) {
         $order = 'ASC';
     } else {
         $order = 'DESC';
     }
     $this->query_orderby = " ORDER BY {$orderby} {$order}";
     // limit
     if ($qv['number']) {
         if ($qv['offset']) {
             $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $qv['offset'], $qv['offset'] + $qv['number']);
         } else {
             $this->query_limit = $wpdb->prepare(" LIMIT %d", $qv['number']);
         }
     }
     $search = trim($qv['search']);
     if ($search) {
         $this->query_where .= _wp_search_sql($search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name'));
     }
     $role = trim($qv['role']);
     if ($role) {
         $this->query_from .= " INNER JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb->usermeta}.user_id";
         $this->query_where .= $wpdb->prepare(" AND {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}capabilities' AND {$wpdb->usermeta}.meta_value LIKE %s", '%' . $role . '%');
     } elseif (is_multisite()) {
         $level_key = $wpdb->prefix . 'capabilities';
         // wpmu site admins don't have user_levels
         $this->query_from .= ", {$wpdb->usermeta}";
         $this->query_where .= " AND {$wpdb->users}.ID = {$wpdb->usermeta}.user_id AND meta_key = '{$level_key}'";
     }
     $meta_key = trim($qv['meta_key']);
     $meta_value = trim($qv['meta_value']);
     if ($meta_key) {
         if (empty($meta_value)) {
             $subquery = $wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key);
         } else {
             $subquery = $wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value);
         }
         $this->query_where .= " AND {$wpdb->users}.ID IN ({$subquery})";
     }
     if (!empty($qv['include'])) {
         $ids = implode(',', wp_parse_id_list($qv['include']));
         $this->query_where .= " AND {$wpdb->users}.ID IN ({$ids})";
     } elseif (!empty($qv['exclude'])) {
         $ids = implode(',', wp_parse_id_list($qv['exclude']));
         $this->query_where .= " AND {$wpdb->users}.ID NOT IN ({$ids})";
     }
     do_action_ref_array('pre_user_query', array(&$this));
 }