Ejemplo n.º 1
0
 /**
  * Execute the query
  *
  * @since 3.1.0
  *
  * @param string|array $query_vars
  * @return int|array
  */
 function query($query_vars)
 {
     global $wpdb;
     $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, 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', 'date_query' => null);
     $groupby = '';
     $this->query_vars = wp_parse_args($query_vars, $defaults);
     // Parse meta query
     $this->meta_query = new WP_Meta_Query();
     $this->meta_query->parse_query_vars($this->query_vars);
     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 = wp_cache_get('last_changed', 'comment');
     if (!$last_changed) {
         $last_changed = microtime();
         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 (!empty($status) && 'all' != $status) {
         $approved = $wpdb->prepare("comment_approved = %s", $status);
     } 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);
         $allowed_keys = 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');
         if (!empty($this->query_vars['meta_key'])) {
             $allowed_keys[] = $this->query_vars['meta_key'];
             $allowed_keys[] = 'meta_value';
             $allowed_keys[] = 'meta_value_num';
         }
         $ordersby = array_intersect($ordersby, $allowed_keys);
         foreach ($ordersby as $key => $value) {
             if ($value == $this->query_vars['meta_key'] || $value == 'meta_value') {
                 $ordersby[$key] = "{$wpdb->commentmeta}.meta_value";
             } elseif ($value == 'meta_value_num') {
                 $ordersby[$key] = "{$wpdb->commentmeta}.meta_value+0";
             }
         }
         $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 .= $wpdb->prepare(' AND comment_post_ID = %d', $post_id);
     }
     if ('' !== $author_email) {
         $where .= $wpdb->prepare(' AND comment_author_email = %s', $author_email);
     }
     if ('' !== $karma) {
         $where .= $wpdb->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 .= $wpdb->prepare(' AND comment_type = %s', $type);
     }
     if ('' !== $parent) {
         $where .= $wpdb->prepare(' AND comment_parent = %d', $parent);
     }
     if ('' !== $user_id) {
         $where .= $wpdb->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 {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID";
         foreach ($post_fields as $field_name => $field_value) {
             $where .= $wpdb->prepare(" AND {$wpdb->posts}.{$field_name} = %s", $field_value);
         }
     }
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('comment', $wpdb->comments, 'comment_ID', $this);
         $join .= $clauses['join'];
         $where .= $clauses['where'];
         $groupby = "{$wpdb->comments}.comment_ID";
     }
     if (!empty($date_query) && is_array($date_query)) {
         $date_query_object = new WP_Date_Query($date_query, 'comment_date');
         $where .= $date_query_object->get_sql();
     }
     $pieces = array('fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby');
     $clauses = apply_filters_ref_array('comments_clauses', array(compact($pieces), &$this));
     foreach ($pieces as $piece) {
         ${$piece} = isset($clauses[$piece]) ? $clauses[$piece] : '';
     }
     if ($groupby) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     $query = "SELECT {$fields} FROM {$wpdb->comments} {$join} WHERE {$where} {$groupby} ORDER BY {$orderby} {$order} {$limits}";
     if ($count) {
         return $wpdb->get_var($query);
     }
     $comments = $wpdb->get_results($query);
     $comments = apply_filters_ref_array('the_comments', array($comments, &$this));
     wp_cache_add($cache_key, $comments, 'comment');
     return $comments;
 }
 /**
  * Used internally to get a list of comment IDs matching the query vars.
  *
  * @since 4.4.0
  * @access protected
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  */
 protected function get_comment_ids()
 {
     global $wpdb;
     // Assemble clauses related to 'comment_approved'.
     $approved_clauses = array();
     // 'status' accepts an array or a comma-separated string.
     $status_clauses = array();
     $statuses = $this->query_vars['status'];
     if (!is_array($statuses)) {
         $statuses = preg_split('/[\\s,]+/', $statuses);
     }
     // 'any' overrides other statuses.
     if (!in_array('any', $statuses)) {
         foreach ($statuses as $status) {
             switch ($status) {
                 case 'hold':
                     $status_clauses[] = "comment_approved = '0'";
                     break;
                 case 'approve':
                     $status_clauses[] = "comment_approved = '1'";
                     break;
                 case 'all':
                 case '':
                     $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
                     break;
                 default:
                     $status_clauses[] = $wpdb->prepare("comment_approved = %s", $status);
                     break;
             }
         }
         if (!empty($status_clauses)) {
             $approved_clauses[] = '( ' . implode(' OR ', $status_clauses) . ' )';
         }
     }
     // User IDs or emails whose unapproved comments are included, regardless of $status.
     if (!empty($this->query_vars['include_unapproved'])) {
         $include_unapproved = $this->query_vars['include_unapproved'];
         // Accepts arrays or comma-separated strings.
         if (!is_array($include_unapproved)) {
             $include_unapproved = preg_split('/[\\s,]+/', $include_unapproved);
         }
         $unapproved_ids = $unapproved_emails = array();
         foreach ($include_unapproved as $unapproved_identifier) {
             // Numeric values are assumed to be user ids.
             if (is_numeric($unapproved_identifier)) {
                 $approved_clauses[] = $wpdb->prepare("( user_id = %d AND comment_approved = '0' )", $unapproved_identifier);
                 // Otherwise we match against email addresses.
             } else {
                 $approved_clauses[] = $wpdb->prepare("( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier);
             }
         }
     }
     // Collapse comment_approved clauses into a single OR-separated clause.
     if (!empty($approved_clauses)) {
         if (1 === count($approved_clauses)) {
             $this->sql_clauses['where']['approved'] = $approved_clauses[0];
         } else {
             $this->sql_clauses['where']['approved'] = '( ' . implode(' OR ', $approved_clauses) . ' )';
         }
     }
     $order = 'ASC' == strtoupper($this->query_vars['order']) ? 'ASC' : 'DESC';
     // Disable ORDER BY with 'none', an empty array, or boolean false.
     if (in_array($this->query_vars['orderby'], array('none', array(), false), true)) {
         $orderby = '';
     } elseif (!empty($this->query_vars['orderby'])) {
         $ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']);
         $orderby_array = array();
         $found_orderby_comment_ID = false;
         foreach ($ordersby as $_key => $_value) {
             if (!$_value) {
                 continue;
             }
             if (is_int($_key)) {
                 $_orderby = $_value;
                 $_order = $order;
             } else {
                 $_orderby = $_key;
                 $_order = $_value;
             }
             if (!$found_orderby_comment_ID && in_array($_orderby, array('comment_ID', 'comment__in'))) {
                 $found_orderby_comment_ID = true;
             }
             $parsed = $this->parse_orderby($_orderby);
             if (!$parsed) {
                 continue;
             }
             if ('comment__in' === $_orderby) {
                 $orderby_array[] = $parsed;
                 continue;
             }
             $orderby_array[] = $parsed . ' ' . $this->parse_order($_order);
         }
         // If no valid clauses were found, order by comment_date_gmt.
         if (empty($orderby_array)) {
             $orderby_array[] = "{$wpdb->comments}.comment_date_gmt {$order}";
         }
         // To ensure determinate sorting, always include a comment_ID clause.
         if (!$found_orderby_comment_ID) {
             $comment_ID_order = '';
             // Inherit order from comment_date or comment_date_gmt, if available.
             foreach ($orderby_array as $orderby_clause) {
                 if (preg_match('/comment_date(?:_gmt)*\\ (ASC|DESC)/', $orderby_clause, $match)) {
                     $comment_ID_order = $match[1];
                     break;
                 }
             }
             // If no date-related order is available, use the date from the first available clause.
             if (!$comment_ID_order) {
                 foreach ($orderby_array as $orderby_clause) {
                     if (false !== strpos('ASC', $orderby_clause)) {
                         $comment_ID_order = 'ASC';
                     } else {
                         $comment_ID_order = 'DESC';
                     }
                     break;
                 }
             }
             // Default to DESC.
             if (!$comment_ID_order) {
                 $comment_ID_order = 'DESC';
             }
             $orderby_array[] = "{$wpdb->comments}.comment_ID {$comment_ID_order}";
         }
         $orderby = implode(', ', $orderby_array);
     } else {
         $orderby = "{$wpdb->comments}.comment_date_gmt {$order}";
     }
     $number = absint($this->query_vars['number']);
     $offset = absint($this->query_vars['offset']);
     if (!empty($number)) {
         if ($offset) {
             $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
     }
     if ($this->query_vars['count']) {
         $fields = 'COUNT(*)';
     } else {
         $fields = "{$wpdb->comments}.comment_ID";
     }
     $post_id = absint($this->query_vars['post_id']);
     if (!empty($post_id)) {
         $this->sql_clauses['where']['post_id'] = $wpdb->prepare('comment_post_ID = %d', $post_id);
     }
     // Parse comment IDs for an IN clause.
     if (!empty($this->query_vars['comment__in'])) {
         $this->sql_clauses['where']['comment__in'] = "{$wpdb->comments}.comment_ID IN ( " . implode(',', wp_parse_id_list($this->query_vars['comment__in'])) . ' )';
     }
     // Parse comment IDs for a NOT IN clause.
     if (!empty($this->query_vars['comment__not_in'])) {
         $this->sql_clauses['where']['comment__not_in'] = "{$wpdb->comments}.comment_ID NOT IN ( " . implode(',', wp_parse_id_list($this->query_vars['comment__not_in'])) . ' )';
     }
     // Parse comment parent IDs for an IN clause.
     if (!empty($this->query_vars['parent__in'])) {
         $this->sql_clauses['where']['parent__in'] = 'comment_parent IN ( ' . implode(',', wp_parse_id_list($this->query_vars['parent__in'])) . ' )';
     }
     // Parse comment parent IDs for a NOT IN clause.
     if (!empty($this->query_vars['parent__not_in'])) {
         $this->sql_clauses['where']['parent__not_in'] = 'comment_parent NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['parent__not_in'])) . ' )';
     }
     // Parse comment post IDs for an IN clause.
     if (!empty($this->query_vars['post__in'])) {
         $this->sql_clauses['where']['post__in'] = 'comment_post_ID IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__in'])) . ' )';
     }
     // Parse comment post IDs for a NOT IN clause.
     if (!empty($this->query_vars['post__not_in'])) {
         $this->sql_clauses['where']['post__not_in'] = 'comment_post_ID NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__not_in'])) . ' )';
     }
     if ('' !== $this->query_vars['author_email']) {
         $this->sql_clauses['where']['author_email'] = $wpdb->prepare('comment_author_email = %s', $this->query_vars['author_email']);
     }
     if ('' !== $this->query_vars['author_url']) {
         $this->sql_clauses['where']['author_url'] = $wpdb->prepare('comment_author_url = %s', $this->query_vars['author_url']);
     }
     if ('' !== $this->query_vars['karma']) {
         $this->sql_clauses['where']['karma'] = $wpdb->prepare('comment_karma = %d', $this->query_vars['karma']);
     }
     // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
     $raw_types = array('IN' => array_merge((array) $this->query_vars['type'], (array) $this->query_vars['type__in']), 'NOT IN' => (array) $this->query_vars['type__not_in']);
     $comment_types = array();
     foreach ($raw_types as $operator => $_raw_types) {
         $_raw_types = array_unique($_raw_types);
         foreach ($_raw_types as $type) {
             switch ($type) {
                 // An empty translates to 'all', for backward compatibility
                 case '':
                 case 'all':
                     break;
                 case 'comment':
                 case 'comments':
                     $comment_types[$operator][] = "''";
                     break;
                 case 'pings':
                     $comment_types[$operator][] = "'pingback'";
                     $comment_types[$operator][] = "'trackback'";
                     break;
                 default:
                     $comment_types[$operator][] = $wpdb->prepare('%s', $type);
                     break;
             }
         }
         if (!empty($comment_types[$operator])) {
             $types_sql = implode(', ', $comment_types[$operator]);
             $this->sql_clauses['where']['comment_type__' . strtolower(str_replace(' ', '_', $operator))] = "comment_type {$operator} ({$types_sql})";
         }
     }
     if ($this->query_vars['hierarchical'] && !$this->query_vars['parent']) {
         $this->query_vars['parent'] = 0;
     }
     if ('' !== $this->query_vars['parent']) {
         $this->sql_clauses['where']['parent'] = $wpdb->prepare('comment_parent = %d', $this->query_vars['parent']);
     }
     if (is_array($this->query_vars['user_id'])) {
         $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode(',', array_map('absint', $this->query_vars['user_id'])) . ')';
     } elseif ('' !== $this->query_vars['user_id']) {
         $this->sql_clauses['where']['user_id'] = $wpdb->prepare('user_id = %d', $this->query_vars['user_id']);
     }
     if ('' !== $this->query_vars['search']) {
         $search_sql = $this->get_search_sql($this->query_vars['search'], array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
         // Strip leading 'AND'.
         $this->sql_clauses['where']['search'] = preg_replace('/^\\s*AND\\s*/', '', $search_sql);
     }
     // If any post-related query vars are passed, join the posts table.
     $join_posts_table = false;
     $plucked = wp_array_slice_assoc($this->query_vars, array('post_author', 'post_name', 'post_parent', 'post_status', 'post_type'));
     $post_fields = array_filter($plucked);
     if (!empty($post_fields)) {
         $join_posts_table = true;
         foreach ($post_fields as $field_name => $field_value) {
             // $field_value may be an array.
             $esses = array_fill(0, count((array) $field_value), '%s');
             $this->sql_clauses['where']['post_fields'] = $wpdb->prepare(" {$wpdb->posts}.{$field_name} IN (" . implode(',', $esses) . ')', $field_value);
         }
     }
     // Comment author IDs for an IN clause.
     if (!empty($this->query_vars['author__in'])) {
         $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode(',', wp_parse_id_list($this->query_vars['author__in'])) . ' )';
     }
     // Comment author IDs for a NOT IN clause.
     if (!empty($this->query_vars['author__not_in'])) {
         $this->sql_clauses['where']['author__not_in'] = 'user_id NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['author__not_in'])) . ' )';
     }
     // Post author IDs for an IN clause.
     if (!empty($this->query_vars['post_author__in'])) {
         $join_posts_table = true;
         $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post_author__in'])) . ' )';
     }
     // Post author IDs for a NOT IN clause.
     if (!empty($this->query_vars['post_author__not_in'])) {
         $join_posts_table = true;
         $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post_author__not_in'])) . ' )';
     }
     $join = '';
     if ($join_posts_table) {
         $join .= "JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID";
     }
     if (!empty($this->meta_query_clauses)) {
         $join .= $this->meta_query_clauses['join'];
         // Strip leading 'AND'.
         $this->sql_clauses['where']['meta_query'] = preg_replace('/^\\s*AND\\s*/', '', $this->meta_query_clauses['where']);
         if (!$this->query_vars['count']) {
             $groupby = "{$wpdb->comments}.comment_ID";
         }
     }
     $date_query = $this->query_vars['date_query'];
     if (!empty($date_query) && is_array($date_query)) {
         $date_query_object = new WP_Date_Query($date_query, 'comment_date');
         $this->sql_clauses['where']['date_query'] = preg_replace('/^\\s*AND\\s*/', '', $date_query_object->get_sql());
     }
     $where = implode(' AND ', $this->sql_clauses['where']);
     $pieces = array('fields', 'join', 'where', 'orderby', 'limits', 'groupby');
     /**
      * Filter the comment query clauses.
      *
      * @since 3.1.0
      *
      * @param array            $pieces A compacted array of comment query clauses.
      * @param WP_Comment_Query &$this  Current instance of WP_Comment_Query, passed by reference.
      */
     $clauses = apply_filters_ref_array('comments_clauses', array(compact($pieces), &$this));
     $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
     $join = isset($clauses['join']) ? $clauses['join'] : '';
     $where = isset($clauses['where']) ? $clauses['where'] : '';
     $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
     $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
     $this->filtered_where_clause = $where;
     if ($where) {
         $where = 'WHERE ' . $where;
     }
     if ($groupby) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if ($orderby) {
         $orderby = "ORDER BY {$orderby}";
     }
     $found_rows = '';
     if (!$this->query_vars['no_found_rows']) {
         $found_rows = 'SQL_CALC_FOUND_ROWS';
     }
     $this->sql_clauses['select'] = "SELECT {$found_rows} {$fields}";
     $this->sql_clauses['from'] = "FROM {$wpdb->comments} {$join}";
     $this->sql_clauses['groupby'] = $groupby;
     $this->sql_clauses['orderby'] = $orderby;
     $this->sql_clauses['limits'] = $limits;
     $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
     if ($this->query_vars['count']) {
         return intval($wpdb->get_var($this->request));
     } else {
         $comment_ids = $wpdb->get_col($this->request);
         return array_map('intval', $comment_ids);
     }
 }
Ejemplo n.º 3
0
 /**
  * Prepare the query variables.
  *
  * @since 3.1.0
  * @since 4.2.0 Added 'meta_value_num' support for `$orderby` parameter. Added multi-dimensional array syntax
  *              for `$orderby` parameter.
  * @access public
  *
  * @param string|array $query {
  *     Optional. Array or string of Query parameters.
  *
  *     @type int          $blog_id         The site ID. Default is the global blog id.
  *     @type string       $role            Role name. Default empty.
  *     @type string       $meta_key        User meta key. Default empty.
  *     @type string       $meta_value      User meta value. Default empty.
  *     @type string       $meta_compare    Comparison operator to test the `$meta_value`. Accepts '=', '!=',
  *                                         '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN',
  *                                         'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP',
  *                                         or 'RLIKE'. Default '='.
  *     @type array        $include         An array of user IDs to include. Default empty array.
  *     @type array        $exclude         An array of user IDs to exclude. Default empty array.
  *     @type string       $search          Search keyword. Searches for possible string matches on columns.
  *                                         When `$search_columns` is left empty, it tries to determine which
  *                                         column to search in based on search string. Default empty.
  *     @type array        $search_columns  Array of column names to be searched. Accepts 'ID', 'login',
  *                                         'nicename', 'email', 'url'. Default empty array.
  *     @type string|array $orderby         Field(s) to sort the retrieved users by. May be a single value,
  *                                         an array of values, or a multi-dimensional array with fields as keys
  *                                         and orders ('ASC' or 'DESC') as values. Accepted values are'ID',
  *                                         'display_name' (or 'name'), 'user_login' (or 'login'),
  *                                         'user_nicename' (or 'nicename'), 'user_email' (or 'email'),
  *                                         'user_url' (or 'url'), 'user_registered' (or 'registered'),
  *                                         'post_count', 'meta_value', 'meta_value_num', the value of
  *                                         `$meta_key`, or an array key of `$meta_query`. To use 'meta_value'
  *                                         or 'meta_value_num', `$meta_key` must be also be defined.
  *                                         Default 'user_login'.
  *     @type string       $order           Designates ascending or descending order of users. Order values
  *                                         passed as part of an `$orderby` array take precedence over this
  *                                         parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
  *     @type int          $offset          Number of users to offset in retrieved results. Can be used in
  *                                         conjunction with pagination. Default 0.
  *     @type int          $number          Number of users to limit the query for. Can be used in conjunction
  *                                         with pagination. Value -1 (all) is not supported.
  *                                         Default empty (all users).
  *     @type bool         $count_total     Whether to count the total number of users found. If pagination is not
  *                                         needed, setting this to false can improve performance. Default true.
  *     @type string|array $fields          Which fields to return. Single or all fields (string), or array
  *                                         of fields. Accepts 'ID', 'display_name', 'login', 'nicename', 'email',
  *                                         'url', 'registered'. Use 'all' for all fields and 'all_with_meta' to
  *                                         include meta fields. Default 'all'.
  *     @type string       $who             Type of users to query. Accepts 'authors'. Default empty (all users).
  * }
  */
 public function prepare_query($query = array())
 {
     global $wpdb;
     if (empty($this->query_vars) || !empty($query)) {
         $this->query_limit = null;
         $this->query_vars = wp_parse_args($query, array('blog_id' => $GLOBALS['blog_id'], 'role' => '', 'meta_key' => '', 'meta_value' => '', 'meta_compare' => '', 'include' => array(), 'exclude' => array(), 'search' => '', 'search_columns' => array(), 'orderby' => 'login', 'order' => 'ASC', 'offset' => '', 'number' => '', 'count_total' => true, 'fields' => 'all', 'who' => ''));
     }
     /**
      * Fires before the WP_User_Query has been parsed.
      *
      * The passed WP_User_Query object contains the query variables, not
      * yet passed into SQL.
      *
      * @since 4.0.0
      *
      * @param WP_User_Query $this The current WP_User_Query instance,
      *                            passed by reference.
      */
     do_action('pre_get_users', $this);
     $qv =& $this->query_vars;
     if (is_array($qv['fields'])) {
         $qv['fields'] = array_unique($qv['fields']);
         $this->query_fields = array();
         foreach ($qv['fields'] as $field) {
             $field = 'ID' === $field ? 'ID' : sanitize_key($field);
             $this->query_fields[] = "{$wpdb->users}.{$field}";
         }
         $this->query_fields = implode(',', $this->query_fields);
     } elseif ('all' == $qv['fields']) {
         $this->query_fields = "{$wpdb->users}.*";
     } else {
         $this->query_fields = "{$wpdb->users}.ID";
     }
     $this->query_from = "FROM {$wpdb->users}";
     $this->query_where = "WHERE 1=1";
     // Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
     if (!empty($qv['include'])) {
         $include = wp_parse_id_list($qv['include']);
     } else {
         $include = false;
     }
     $blog_id = 0;
     if (isset($qv['blog_id'])) {
         $blog_id = absint($qv['blog_id']);
     }
     if (isset($qv['who']) && 'authors' == $qv['who'] && $blog_id) {
         $qv['meta_key'] = $wpdb->get_blog_prefix($blog_id) . 'user_level';
         $qv['meta_value'] = 0;
         $qv['meta_compare'] = '!=';
         $qv['blog_id'] = $blog_id = 0;
         // Prevent extra meta query
     }
     // Meta query.
     $this->meta_query = new WP_Meta_Query();
     $this->meta_query->parse_query_vars($qv);
     $role = '';
     if (isset($qv['role'])) {
         $role = trim($qv['role']);
     }
     if ($blog_id && ($role || is_multisite())) {
         $cap_meta_query = array();
         $cap_meta_query['key'] = $wpdb->get_blog_prefix($blog_id) . 'capabilities';
         if ($role) {
             $cap_meta_query['value'] = '"' . $role . '"';
             $cap_meta_query['compare'] = 'like';
         }
         if (empty($this->meta_query->queries)) {
             $this->meta_query->queries = array($cap_meta_query);
         } elseif (!in_array($cap_meta_query, $this->meta_query->queries, true)) {
             // Append the cap query to the original queries and reparse the query.
             $this->meta_query->queries = array('relation' => 'AND', array($this->meta_query->queries, $cap_meta_query));
         }
         $this->meta_query->parse_query_vars($this->meta_query->queries);
     }
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('user', $wpdb->users, 'ID', $this);
         $this->query_from .= $clauses['join'];
         $this->query_where .= $clauses['where'];
         if ('OR' == $this->meta_query->relation) {
             $this->query_fields = 'DISTINCT ' . $this->query_fields;
         }
     }
     // sorting
     $qv['order'] = isset($qv['order']) ? strtoupper($qv['order']) : '';
     $order = $this->parse_order($qv['order']);
     if (empty($qv['orderby'])) {
         // Default order is by 'user_login'.
         $ordersby = array('user_login' => $order);
     } else {
         if (is_array($qv['orderby'])) {
             $ordersby = $qv['orderby'];
         } else {
             // 'orderby' values may be a comma- or space-separated list.
             $ordersby = preg_split('/[,\\s]+/', $qv['orderby']);
         }
     }
     $orderby_array = array();
     foreach ($ordersby as $_key => $_value) {
         if (!$_value) {
             continue;
         }
         if (is_int($_key)) {
             // Integer key means this is a flat array of 'orderby' fields.
             $_orderby = $_value;
             $_order = $order;
         } else {
             // Non-integer key means this the key is the field and the value is ASC/DESC.
             $_orderby = $_key;
             $_order = $_value;
         }
         $parsed = $this->parse_orderby($_orderby);
         if (!$parsed) {
             continue;
         }
         $orderby_array[] = $parsed . ' ' . $this->parse_order($_order);
     }
     // If no valid clauses were found, order by user_login.
     if (empty($orderby_array)) {
         $orderby_array[] = "user_login {$order}";
     }
     $this->query_orderby = 'ORDER BY ' . implode(', ', $orderby_array);
     // limit
     if ($qv['number']) {
         if ($qv['offset']) {
             $this->query_limit = $wpdb->prepare("OFFSET %d ROWS FETCH NEXT %d ROWS ONLY", $qv['offset'], $qv['number']);
         } else {
             $this->query_limit = $wpdb->prepare("OFFSET 0 ROWS FETCH NEXT %d ROWS ONLY", $qv['number']);
         }
     }
     $search = '';
     if (isset($qv['search'])) {
         $search = trim($qv['search']);
     }
     if ($search) {
         $leading_wild = ltrim($search, '*') != $search;
         $trailing_wild = rtrim($search, '*') != $search;
         if ($leading_wild && $trailing_wild) {
             $wild = 'both';
         } elseif ($leading_wild) {
             $wild = 'leading';
         } elseif ($trailing_wild) {
             $wild = 'trailing';
         } else {
             $wild = false;
         }
         if ($wild) {
             $search = trim($search, '*');
         }
         $search_columns = array();
         if ($qv['search_columns']) {
             $search_columns = array_intersect($qv['search_columns'], array('ID', 'user_login', 'user_email', 'user_url', 'user_nicename'));
         }
         if (!$search_columns) {
             if (false !== strpos($search, '@')) {
                 $search_columns = array('user_email');
             } elseif (is_numeric($search)) {
                 $search_columns = array('user_login', 'ID');
             } elseif (preg_match('|^https?://|', $search) && !(is_multisite() && wp_is_large_network('users'))) {
                 $search_columns = array('user_url');
             } else {
                 $search_columns = array('user_login', 'user_nicename');
             }
         }
         /**
          * Filter the columns to search in a WP_User_Query search.
          *
          * The default columns depend on the search term, and include 'user_email',
          * 'user_login', 'ID', 'user_url', and 'user_nicename'.
          *
          * @since 3.6.0
          *
          * @param array         $search_columns Array of column names to be searched.
          * @param string        $search         Text being searched.
          * @param WP_User_Query $this           The current WP_User_Query instance.
          */
         $search_columns = apply_filters('user_search_columns', $search_columns, $search, $this);
         $this->query_where .= $this->get_search_sql($search, $search_columns, $wild);
     }
     if (!empty($include)) {
         // Sanitized earlier.
         $ids = implode(',', $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})";
     }
     // Date queries are allowed for the user_registered field.
     if (!empty($qv['date_query']) && is_array($qv['date_query'])) {
         $date_query = new WP_Date_Query($qv['date_query'], 'user_registered');
         $this->query_where .= $date_query->get_sql();
     }
     /**
      * Fires after the WP_User_Query has been parsed, and before
      * the query is executed.
      *
      * The passed WP_User_Query object contains SQL parts formed
      * from parsing the given query.
      *
      * @since 3.1.0
      *
      * @param WP_User_Query $this The current WP_User_Query instance,
      *                            passed by reference.
      */
     do_action_ref_array('pre_user_query', array(&$this));
 }
Ejemplo n.º 4
0
 public static function get($args = array(), $output = OBJECT)
 {
     global $wpdb;
     $query = wp_parse_args($args);
     $query_length = count($query);
     $i = 1;
     $table = XBooking_BOOKING_TABLE;
     $where = $query_length > 0 ? 'WHERE ' : '';
     foreach ($query as $column => $value) {
         if ($column == 'date_query') {
             $query_args = array(array('column' => 'date', 'before' => $value['before'], 'inclusive' => true), array('column' => 'date', 'after' => $value['after'], 'inclusive' => true));
             $date_query = new WP_Date_Query($query_args, 'date');
             $where .= $date_query->get_sql();
             // Reduce counter as get_sql produces 'AND' for us
             $i--;
         } else {
             $where .= "{$column} = '{$value}' ";
         }
         $i++;
     }
     $select = "SELECT * FROM {$table} {$where} ORDER BY date DESC";
     // If unique value queried only get single row
     if (isset($query['id'])) {
         $results = $wpdb->get_row($select, $output);
     } else {
         $results = $wpdb->get_results($select, $output);
     }
     return !empty($results) ? $results : false;
 }
 /**
  * Get Commissions
  *
  * @param array $q
  *
  * @return array
  * @author Andrea Grillo <*****@*****.**>
  * @since  1.0
  */
 public function get_commissions($q = array())
 {
     global $wpdb;
     $default_args = array('line_item_id' => 0, 'product_id' => 0, 'order_id' => 0, 'user_id' => 0, 'vendor_id' => 0, 'status' => 'unpaid', 'm' => false, 'date_query' => false, 's' => '', 'number' => '', 'offset' => '', 'paged' => '', 'orderby' => 'ID', 'order' => 'ASC', 'fields' => 'ids');
     foreach (array('order_id', 'vendor_id', 'status', 'paged', 'm', 's', 'orderby', 'order', 'product_id') as $key) {
         if (isset($_REQUEST[$key])) {
             $default_args[$key] = $_REQUEST[$key];
         }
     }
     $q = wp_parse_args($q, $default_args);
     // Fairly insane upper bound for search string lengths.
     if (!is_scalar($q['s']) || !empty($q['s']) && strlen($q['s']) > 1600) {
         $q['s'] = '';
     }
     // First let's clear some variables
     $where = '';
     $limits = '';
     $join = '';
     $groupby = '';
     $orderby = '';
     // query parts initializating
     $pieces = array('where', 'groupby', 'join', 'orderby', 'limits');
     // filter
     if (!empty($q['line_item_id'])) {
         $where .= $wpdb->prepare(" AND c.line_item_id = %d", $q['line_item_id']);
     }
     if (!empty($q['product_id'])) {
         $join .= " JOIN {$wpdb->prefix}woocommerce_order_items oi ON ( oi.order_item_id = c.line_item_id AND oi.order_id = c.order_id )";
         $join .= " JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim ON ( oim.order_item_id = oi.order_item_id )";
         $where .= $wpdb->prepare(" AND oim.meta_key = %s AND oim.meta_value = %s", '_product_id', $q['product_id']);
     }
     if (!empty($q['order_id'])) {
         $where .= $wpdb->prepare(" AND c.order_id = %d", $q['order_id']);
     }
     if (!empty($q['user_id'])) {
         $where .= $wpdb->prepare(" AND c.user_id = %d", $q['user_id']);
     }
     if (!empty($q['vendor_id'])) {
         $where .= $wpdb->prepare(" AND c.vendor_id = %d", $q['vendor_id']);
     }
     if (!empty($q['status']) && 'all' != $q['status']) {
         if (is_array($q['status'])) {
             $q['status'] = implode("', '", $q['status']);
         }
         $where .= sprintf(" AND c.status IN ( '%s' )", $q['status']);
     }
     // The "m" parameter is meant for months but accepts datetimes of varying specificity
     if ($q['m']) {
         $q['m'] = absint(preg_replace('|[^0-9]|', '', $q['m']));
         $join .= strpos($join, "{$wpdb->posts} o") === false ? " JOIN {$wpdb->posts} o ON o.ID = c.order_id" : '';
         $where .= " AND o.post_type = 'shop_order'";
         $where .= " AND YEAR(o.post_date)=" . substr($q['m'], 0, 4);
         if (strlen($q['m']) > 5) {
             $where .= " AND MONTH(o.post_date)=" . substr($q['m'], 4, 2);
         }
         if (strlen($q['m']) > 7) {
             $where .= " AND DAYOFMONTH(o.post_date)=" . substr($q['m'], 6, 2);
         }
         if (strlen($q['m']) > 9) {
             $where .= " AND HOUR(o.post_date)=" . substr($q['m'], 8, 2);
         }
         if (strlen($q['m']) > 11) {
             $where .= " AND MINUTE(o.post_date)=" . substr($q['m'], 10, 2);
         }
         if (strlen($q['m']) > 13) {
             $where .= " AND SECOND(o.post_date)=" . substr($q['m'], 12, 2);
         }
     }
     // Handle complex date queries
     if (!empty($q['date_query'])) {
         $join .= strpos($join, "{$wpdb->posts} o") === false ? " JOIN {$wpdb->posts} o ON o.ID = c.order_id" : '';
         $where .= " AND o.post_type = 'shop_order'";
         $date_query = new WP_Date_Query($q['date_query'], 'o.post_date');
         $where .= $date_query->get_sql();
     }
     // Search
     if ($q['s']) {
         // added slashes screw with quote grouping when done early, so done later
         $q['s'] = stripslashes($q['s']);
         // there are no line breaks in <input /> fields
         $q['s'] = str_replace(array("\r", "\n"), '', $q['s']);
         // order
         $join .= strpos($join, "{$wpdb->posts} o") === false ? " JOIN {$wpdb->posts} o ON o.ID = c.order_id" : '';
         // product
         $join .= strpos($join, 'woocommerce_order_items') === false ? " JOIN {$wpdb->prefix}woocommerce_order_items oi ON ( oi.order_item_id = c.line_item_id AND oi.order_id = c.order_id )" : '';
         $where .= " AND oi.order_item_type = 'line_item'";
         // user
         $join .= " JOIN {$wpdb->users} u ON u.ID = c.user_id";
         $join .= " JOIN {$wpdb->usermeta} um ON um.user_id = c.user_id";
         $join .= " JOIN {$wpdb->usermeta} um2 ON um2.user_id = c.user_id";
         $where .= " AND um.meta_key = 'first_name'";
         $where .= " AND um2.meta_key = 'last_name'";
         $s = array($wpdb->prepare("o.ID = %s", $q['s']), $wpdb->prepare("oi.order_item_name LIKE %s", "%{$q['s']}%"), $wpdb->prepare("u.user_login LIKE %s", "%{$q['s']}%"), $wpdb->prepare("u.user_nicename LIKE %s", "%{$q['s']}%"), $wpdb->prepare("u.user_email LIKE %s", "%{$q['s']}%"), $wpdb->prepare("um.meta_value LIKE %s", "%{$q['s']}%"), $wpdb->prepare("um2.meta_value LIKE %s", "%{$q['s']}%"));
         $where .= ' AND ( ' . implode(' OR ', $s) . ' )';
     }
     // Order
     if (!is_string($q['order']) || empty($q['order'])) {
         $q['order'] = 'DESC';
     }
     if ('ASC' === strtoupper($q['order'])) {
         $q['order'] = 'ASC';
     } else {
         $q['order'] = 'DESC';
     }
     // Order by.
     if (empty($q['orderby'])) {
         /*
          * Boolean false or empty array blanks out ORDER BY,
          * while leaving the value unset or otherwise empty sets the default.
          */
         if (isset($q['orderby']) && (is_array($q['orderby']) || false === $q['orderby'])) {
             $orderby = '';
         } else {
             $orderby = "c.ID " . $q['order'];
         }
     } elseif ('none' == $q['orderby']) {
         $orderby = '';
     } else {
         $orderby_array = array();
         if (is_array($q['orderby'])) {
             foreach ($q['orderby'] as $_orderby => $order) {
                 $orderby = addslashes_gpc(urldecode($_orderby));
                 if (!is_string($order) || empty($order)) {
                     $order = 'DESC';
                 }
                 if ('ASC' === strtoupper($order)) {
                     $order = 'ASC';
                 } else {
                     $order = 'DESC';
                 }
                 $orderby_array[] = $orderby . ' ' . $order;
             }
             $orderby = implode(', ', $orderby_array);
         } else {
             $q['orderby'] = urldecode($q['orderby']);
             $q['orderby'] = addslashes_gpc($q['orderby']);
             foreach (explode(' ', $q['orderby']) as $i => $orderby) {
                 $orderby_array[] = $orderby;
             }
             $orderby = implode(' ' . $q['order'] . ', ', $orderby_array);
             if (empty($orderby)) {
                 $orderby = "c.ID " . $q['order'];
             } elseif (!empty($q['order'])) {
                 $orderby .= " {$q['order']}";
             }
         }
     }
     // Paging
     if (!empty($q['paged']) && !empty($q['number'])) {
         $page = absint($q['paged']);
         if (!$page) {
             $page = 1;
         }
         if (empty($q['offset'])) {
             $pgstrt = absint(($page - 1) * $q['number']) . ', ';
         } else {
             // we're ignoring $page and using 'offset'
             $q['offset'] = absint($q['offset']);
             $pgstrt = $q['offset'] . ', ';
         }
         $limits = 'LIMIT ' . $pgstrt . $q['number'];
     }
     $clauses = compact($pieces);
     $where = isset($clauses['where']) ? $clauses['where'] : '';
     $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
     $join = isset($clauses['join']) ? $clauses['join'] : '';
     $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
     $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     if (!empty($groupby)) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if (!empty($orderby)) {
         $orderby = 'ORDER BY ' . $orderby;
     }
     $found_rows = '';
     if (!empty($limits)) {
         $found_rows = 'SQL_CALC_FOUND_ROWS';
     }
     $fields = 'c.ID';
     if ('count' != $q['fields'] && 'ids' != $q['fields']) {
         if (is_array($q['fields'])) {
             $fields = implode(',', $q['fields']);
         } else {
             $fields = $q['fields'];
         }
     }
     $res = $wpdb->get_col("SELECT {$found_rows} DISTINCT {$fields} FROM {$wpdb->commissions} c {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}");
     // return count
     if ('count' == $q['fields']) {
         return !empty($limits) ? $wpdb->get_var('SELECT FOUND_ROWS()') : count($res);
     }
     return $res;
 }
Ejemplo n.º 6
0
 /**
  * Execute the query
  *
  * @since 3.1.0
  * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
  *              'post_author__not_in', 'author__in', 'author__not_in', 'post__in',
  *              'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in'
  *              arguments to $query_vars.
  *
  * @param string|array $query_vars {
  *     Optional. Array or query string of comment query parameters.
  *
  *     @type string       $author_email        Comment author email address. Default empty.
  *     @type array        $author__in          Array of author IDs to include comments for. Default empty.
  *     @type array        $author__not_in      Array of author IDs to exclude comments for. Default empty.
  *     @type array        $comment__in         Array of comment IDs to include. Default empty.
  *     @type array        $comment__not_in     Array of comment IDs to exclude. Default empty.
  *     @type bool         $count               Whether to return a comment count (true) or array of comment
  *                                             objects (false). Default false.
  *     @type array        $date_query          Date query clauses to limit comments by. See {@see WP_Date_Query}.
  *                                             Default null.
  *     @type string       $fields              Comment fields to return. Accepts 'ids' for comment IDs only or
  *                                             empty for all fields. Default empty.
  *     @type int          $ID                  Currently unused.
  *     @type array        $include_unapproved  Array of IDs or email addresses of users whose unapproved comments
  *                                             will be returned by the query regardless of `$status`. Default empty.
  *     @type int          $karma               Karma score to retrieve matching comments for. Default empty.
  *     @type string       $meta_key            Include comments with a matching comment meta key. Default empty.
  *     @type string       $meta_value          Include comments with a matching comment meta value. Requires
  *                                             `$meta_key` to be set. Default empty.
  *     @type array        $meta_query          Meta query clauses to limit retrieved comments by.
  *                                             See {@see WP_Meta_Query}. Default empty.
  *     @type int          $number              Maximum number of comments to retrieve. Default null (no limit).
  *     @type int          $offset              Number of comments to offset the query. Used to build LIMIT clause.
  *                                             Default 0.
  *     @type string|array $orderby             Comment status or array of statuses. Accepts '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',
  *                                             'meta_value', 'meta_value_num', or value of $meta_key.
  *                                              Also accepts false, empty array, or 'none' to disable `ORDER BY`
  *                                             clause. Default: 'comment_date_gmt'.
  *     @type string       $order               How to order retrieved comments. Accepts 'ASC', 'DESC'.
  *                                             Default: 'DESC'.
  *     @type int          $parent              Parent ID of comment to retrieve children of. Default empty.
  *     @type array        $post_author__in     Array of author IDs to retrieve comments for. Default empty.
  *     @type array        $post_author__not_in Array of author IDs *not* to retrieve comments for. Default empty.
  *     @type int          $post_ID             Currently unused.
  *     @type int          $post_id             Limit results to those affiliated with a given post ID. Default 0.
  *     @type array        $post__in            Array of post IDs to include affiliated comments for. Default empty.
  *     @type array        $post__not_in        Array of post IDs to exclude affiliated comments for. Default empty.
  *     @type int          $post_author         Comment author ID to limit results by. Default empty.
  *     @type string       $post_status         Post status to retrieve affiliated comments for. Default empty.
  *     @type string       $post_type           Post type to retrieve affiliated comments for. Default empty.
  *     @type string       $post_name           Post name to retrieve affiliated comments for. Default empty.
  *     @type int          $post_parent         Post parent ID to retrieve affiliated comments for. Default empty.
  *     @type string       $search              Search term(s) to retrieve matching comments for. Default empty.
  *     @type string       $status              Comment status to limit results by. Accepts 'hold'
  *                                             (`comment_status=0`), 'approve' (`comment_status=1`), 'all', or a
  *                                             custom comment status. Default 'all'.
  *     @type string|array $type                Include comments of a given type, or array of types. Accepts
  *                                             'comment', 'pings' (includes 'pingback' and 'trackback'), or any
  *                                             custom type string. Default empty.
  *     @type array        $type__in            Include comments from a given array of comment types. Default empty.
  *     @type array        $type__not_in        Exclude comments from a given array of comment types. Default empty.
  *     @type int          $user_id             Include comments for a specific user ID. Default empty.
  * }
  * @return int|array Array of comments or number of found comments if `$count` is set to true.
  */
 public function query($query_vars)
 {
     global $wpdb;
     $defaults = array('author_email' => '', 'author__in' => '', 'author__not_in' => '', 'include_unapproved' => '', 'fields' => '', 'ID' => '', 'comment__in' => '', 'comment__not_in' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_author__in' => '', 'post_author__not_in' => '', 'post_ID' => '', 'post_id' => 0, 'post__in' => '', 'post__not_in' => '', 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => 'all', 'type' => '', 'type__in' => '', 'type__not_in' => '', 'user_id' => '', 'search' => '', 'count' => false, 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', 'date_query' => null);
     $groupby = '';
     $this->query_vars = wp_parse_args($query_vars, $defaults);
     // Parse meta query
     $this->meta_query = new WP_Meta_Query();
     $this->meta_query->parse_query_vars($this->query_vars);
     /**
      * Fires before comments are retrieved.
      *
      * @since 3.1.0
      *
      * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
      */
     do_action_ref_array('pre_get_comments', array(&$this));
     // $args can be whatever, only use the args defined in defaults to compute the key
     $key = md5(serialize(wp_array_slice_assoc($this->query_vars, array_keys($defaults))));
     $last_changed = wp_cache_get('last_changed', 'comment');
     if (!$last_changed) {
         $last_changed = microtime();
         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;
     }
     $where = array();
     // Assemble clauses related to 'comment_approved'.
     $approved_clauses = array();
     // 'status' accepts an array or a comma-separated string.
     $status_clauses = array();
     $statuses = $this->query_vars['status'];
     if (!is_array($statuses)) {
         $statuses = preg_split('/[\\s,]+/', $statuses);
     }
     // 'any' overrides other statuses.
     if (!in_array('any', $statuses)) {
         foreach ($statuses as $status) {
             switch ($status) {
                 case 'hold':
                     $status_clauses[] = "comment_approved = '0'";
                     break;
                 case 'approve':
                     $status_clauses[] = "comment_approved = '1'";
                     break;
                 case 'all':
                 case '':
                     $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
                     break;
                 default:
                     $status_clauses[] = $wpdb->prepare("comment_approved = %s", $status);
                     break;
             }
         }
         if (!empty($status_clauses)) {
             $approved_clauses[] = '( ' . implode(' OR ', $status_clauses) . ' )';
         }
     }
     // User IDs or emails whose unapproved comments are included, regardless of $status.
     if (!empty($this->query_vars['include_unapproved'])) {
         $include_unapproved = $this->query_vars['include_unapproved'];
         // Accepts arrays or comma-separated strings.
         if (!is_array($include_unapproved)) {
             $include_unapproved = preg_split('/[\\s,]+/', $include_unapproved);
         }
         $unapproved_ids = $unapproved_emails = array();
         foreach ($include_unapproved as $unapproved_identifier) {
             // Numeric values are assumed to be user ids.
             if (is_numeric($unapproved_identifier)) {
                 $approved_clauses[] = $wpdb->prepare("( user_id = %d AND comment_approved = '0' )", $unapproved_identifier);
                 // Otherwise we match against email addresses.
             } else {
                 $approved_clauses[] = $wpdb->prepare("( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier);
             }
         }
     }
     // Collapse comment_approved clauses into a single OR-separated clause.
     if (!empty($approved_clauses)) {
         if (1 === count($approved_clauses)) {
             $where[] = $approved_clauses[0];
         } else {
             $where[] = '( ' . implode(' OR ', $approved_clauses) . ' )';
         }
     }
     $order = 'ASC' == strtoupper($this->query_vars['order']) ? 'ASC' : 'DESC';
     // Disable ORDER BY with 'none', an empty array, or boolean false.
     if (in_array($this->query_vars['orderby'], array('none', array(), false), true)) {
         $orderby = '';
     } else {
         if (!empty($this->query_vars['orderby'])) {
             $ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']);
             $allowed_keys = 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');
             if (!empty($this->query_vars['meta_key'])) {
                 $allowed_keys[] = $this->query_vars['meta_key'];
                 $allowed_keys[] = 'meta_value';
                 $allowed_keys[] = 'meta_value_num';
             }
             $ordersby = array_intersect($ordersby, $allowed_keys);
             foreach ($ordersby as $key => $value) {
                 if ($value == $this->query_vars['meta_key'] || $value == 'meta_value') {
                     $ordersby[$key] = "{$wpdb->commentmeta}.meta_value";
                 } elseif ($value == 'meta_value_num') {
                     $ordersby[$key] = "{$wpdb->commentmeta}.meta_value+0";
                 }
             }
             $orderby = empty($ordersby) ? 'comment_date_gmt' : implode(', ', $ordersby);
         } else {
             $orderby = 'comment_date_gmt';
         }
     }
     $number = absint($this->query_vars['number']);
     $offset = absint($this->query_vars['offset']);
     if (!empty($number)) {
         if ($offset) {
             $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
     } else {
         $limits = '';
     }
     if ($this->query_vars['count']) {
         $fields = 'COUNT(*)';
     } else {
         switch (strtolower($this->query_vars['fields'])) {
             case 'ids':
                 $fields = "{$wpdb->comments}.comment_ID";
                 break;
             default:
                 $fields = "*";
                 break;
         }
     }
     $join = '';
     $post_id = absint($this->query_vars['post_id']);
     if (!empty($post_id)) {
         $where[] = $wpdb->prepare('comment_post_ID = %d', $post_id);
     }
     // Parse comment IDs for an IN clause.
     if (!empty($this->query_vars['comment__in'])) {
         $where[] = 'comment_ID IN ( ' . implode(',', wp_parse_id_list($this->query_vars['comment__in'])) . ' )';
     }
     // Parse comment IDs for a NOT IN clause.
     if (!empty($this->query_vars['comment__not_in'])) {
         $where[] = 'comment_ID NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['comment__not_in'])) . ' )';
     }
     // Parse comment post IDs for an IN clause.
     if (!empty($this->query_vars['post__in'])) {
         $where[] = 'comment_post_ID IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__in'])) . ' )';
     }
     // Parse comment post IDs for a NOT IN clause.
     if (!empty($this->query_vars['post__not_in'])) {
         $where[] = 'comment_post_ID NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__not_in'])) . ' )';
     }
     if ('' !== $this->query_vars['author_email']) {
         $where[] = $wpdb->prepare('comment_author_email = %s', $this->query_vars['author_email']);
     }
     if ('' !== $this->query_vars['karma']) {
         $where[] = $wpdb->prepare('comment_karma = %d', $this->query_vars['karma']);
     }
     // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
     $raw_types = array('IN' => array_merge((array) $this->query_vars['type'], (array) $this->query_vars['type__in']), 'NOT IN' => (array) $this->query_vars['type__not_in']);
     $comment_types = array();
     foreach ($raw_types as $operator => $_raw_types) {
         $_raw_types = array_unique($_raw_types);
         foreach ($_raw_types as $type) {
             switch ($type) {
                 // An empty translates to 'all', for backward compatibility
                 case '':
                 case 'all':
                     break;
                 case 'comment':
                 case 'comments':
                     $comment_types[$operator][] = "''";
                     break;
                 case 'pings':
                     $comment_types[$operator][] = "'pingback'";
                     $comment_types[$operator][] = "'trackback'";
                     break;
                 default:
                     $comment_types[$operator][] = $wpdb->prepare('%s', $type);
                     break;
             }
         }
         if (!empty($comment_types[$operator])) {
             $types_sql = implode(', ', $comment_types[$operator]);
             $where[] = "comment_type {$operator} ({$types_sql})";
         }
     }
     if ('' !== $this->query_vars['parent']) {
         $where[] = $wpdb->prepare('comment_parent = %d', $this->query_vars['parent']);
     }
     if (is_array($this->query_vars['user_id'])) {
         $where[] = 'user_id IN (' . implode(',', array_map('absint', $this->query_vars['user_id'])) . ')';
     } elseif ('' !== $this->query_vars['user_id']) {
         $where[] = $wpdb->prepare('user_id = %d', $this->query_vars['user_id']);
     }
     if ('' !== $this->query_vars['search']) {
         $search_sql = $this->get_search_sql($this->query_vars['search'], array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
         // Strip leading 'AND'.
         $where[] = preg_replace('/^\\s*AND\\s*/', '', $search_sql);
     }
     // If any post-related query vars are passed, join the posts table.
     $join_posts_table = false;
     $plucked = wp_array_slice_assoc($this->query_vars, array('post_author', 'post_name', 'post_parent', 'post_status', 'post_type'));
     $post_fields = array_filter($plucked);
     if (!empty($post_fields)) {
         $join_posts_table = true;
         foreach ($post_fields as $field_name => $field_value) {
             $where[] = $wpdb->prepare(" {$wpdb->posts}.{$field_name} = %s", $field_value);
         }
     }
     // Comment author IDs for an IN clause.
     if (!empty($this->query_vars['author__in'])) {
         $where[] = 'user_id IN ( ' . implode(',', wp_parse_id_list($this->query_vars['author__in'])) . ' )';
     }
     // Comment author IDs for a NOT IN clause.
     if (!empty($this->query_vars['author__not_in'])) {
         $where[] = 'user_id NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['author__not_in'])) . ' )';
     }
     // Post author IDs for an IN clause.
     if (!empty($this->query_vars['post_author__in'])) {
         $join_posts_table = true;
         $where[] = 'post_author IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post_author__in'])) . ' )';
     }
     // Post author IDs for a NOT IN clause.
     if (!empty($this->query_vars['post_author__not_in'])) {
         $join_posts_table = true;
         $where[] = 'post_author NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post_author__not_in'])) . ' )';
     }
     if ($join_posts_table) {
         $join = "JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID";
     }
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('comment', $wpdb->comments, 'comment_ID', $this);
         $join .= $clauses['join'];
         // Strip leading 'AND'.
         $where[] = preg_replace('/^\\s*AND\\s*/', '', $clauses['where']);
         if (!$this->query_vars['count']) {
             $groupby = "{$wpdb->comments}.comment_ID";
         }
     }
     $date_query = $this->query_vars['date_query'];
     if (!empty($date_query) && is_array($date_query)) {
         $date_query_object = new WP_Date_Query($date_query, 'comment_date');
         $where[] = preg_replace('/^\\s*AND\\s*/', '', $date_query_object->get_sql());
     }
     $where = implode(' AND ', $where);
     $pieces = array('fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby');
     /**
      * Filter the comment query clauses.
      *
      * @since 3.1.0
      *
      * @param array            $pieces A compacted array of comment query clauses.
      * @param WP_Comment_Query &$this  Current instance of WP_Comment_Query, passed by reference.
      */
     $clauses = apply_filters_ref_array('comments_clauses', array(compact($pieces), &$this));
     $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
     $join = isset($clauses['join']) ? $clauses['join'] : '';
     $where = isset($clauses['where']) ? $clauses['where'] : '';
     $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
     $order = isset($clauses['order']) ? $clauses['order'] : '';
     $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
     if ($where) {
         $where = 'WHERE ' . $where;
     }
     if ($groupby) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if ($orderby) {
         $orderby = "ORDER BY {$orderby} {$order}";
     }
     $this->request = "SELECT {$fields} FROM {$wpdb->comments} {$join} {$where} {$groupby} {$orderby} {$limits}";
     if ($this->query_vars['count']) {
         return $wpdb->get_var($this->request);
     }
     if ('ids' == $this->query_vars['fields']) {
         $this->comments = $wpdb->get_col($this->request);
         return array_map('intval', $this->comments);
     }
     $results = $wpdb->get_results($this->request);
     /**
      * Filter the comment query results.
      *
      * @since 3.1.0
      *
      * @param array            $results  An array of comments.
      * @param WP_Comment_Query &$this    Current instance of WP_Comment_Query, passed by reference.
      */
     $comments = apply_filters_ref_array('the_comments', array($results, &$this));
     wp_cache_add($cache_key, $comments, 'comment');
     return $comments;
 }
 /**
  * Get the IDs that a user is following.
  *
  * @since 1.0.0
  *
  * @param int $user_id The user ID.
  * @param string $follow_type The follow type.  Leave blank to query users.
  * @param array $query_args {
  *     Various query arguments
  *     @type array $date_query See {@link WP_Date_Query}.
  *     @type string $orderby The DB column to order results by. Default: 'id'.
  *     @type string $order The order. Either 'ASC' or 'DESC'. Default: 'DESC'.
  * }
  * @return array
  */
 public static function get_following($user_id = 0, $follow_type = '', $query_args = array())
 {
     global $wpdb;
     // SQL statement
     $sql = self::get_select_sql('leader_id');
     $sql .= self::get_where_sql(array('follower_id' => $user_id, 'follow_type' => $follow_type));
     // Setup date query
     if (!empty($query_args['date_query']) && class_exists('WP_Date_Query')) {
         add_filter('date_query_valid_columns', array(__CLASS__, 'register_date_column'));
         $date_query = new WP_Date_Query($query_args['date_query'], 'date_recorded');
         $sql .= $date_query->get_sql();
         remove_filter('date_query_valid_columns', array(__CLASS__, 'register_date_column'));
     }
     // Setup orderby query
     $orderby = array();
     if (!empty($query_args['orderby'])) {
         $orderby = $query_args['orderby'];
     }
     if (!empty($query_args['order'])) {
         $orderby = $query_args['order'];
     }
     $sql .= self::get_orderby_sql($orderby);
     // do the query
     $result = $wpdb->get_col($sql);
     // if query args is empty, cache the count while we're here
     if (empty($query_args)) {
         $type = !empty($follow_type) ? "{$follow_type}_" : "";
         wp_cache_set($user_id, $wpdb->num_rows, "bp_follow_following_{$type}count");
     }
     return $result;
 }
Ejemplo n.º 8
0
 /**
  * Retrieve the posts based on query variables.
  *
  * There are a few filters and actions that can be used to modify the post
  * database query.
  *
  * @since 1.5.0
  * @access public
  * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  *
  * @return array List of posts.
  */
 function &get_posts()
 {
     global $wpdb, $user_ID, $_wp_using_ext_object_cache;
     $this->parse_query();
     do_action_ref_array('network_pre_get_posts', array(&$this));
     // Shorthand.
     $q =& $this->query_vars;
     // Fill again in case pre_get_posts unset some vars.
     $q = $this->fill_query_vars($q);
     // Parse meta query
     $this->meta_query = new WP_Network_Meta_Query();
     $this->meta_query->parse_query_vars($q);
     // Set a flag if a pre_get_posts hook changed the query vars.
     $hash = md5(serialize($this->query_vars));
     if ($hash != $this->query_vars_hash) {
         $this->query_vars_changed = true;
         $this->query_vars_hash = $hash;
     }
     unset($hash);
     // First let's clear some variables
     $distinct = '';
     $whichauthor = '';
     $whichmimetype = '';
     $where = '';
     $limits = '';
     $join = '';
     $search = '';
     $groupby = '';
     $fields = '';
     $post_status_join = false;
     $page = 1;
     if (isset($q['caller_get_posts'])) {
         _deprecated_argument('WP_Query', '3.1', __('"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.'));
         if (!isset($q['ignore_sticky_posts'])) {
             $q['ignore_sticky_posts'] = $q['caller_get_posts'];
         }
     }
     if (!isset($q['ignore_sticky_posts'])) {
         $q['ignore_sticky_posts'] = false;
     }
     if (!isset($q['suppress_filters'])) {
         $q['suppress_filters'] = false;
     }
     if (!isset($q['cache_results'])) {
         if ($_wp_using_ext_object_cache) {
             $q['cache_results'] = false;
         } else {
             $q['cache_results'] = true;
         }
     }
     if (!isset($q['update_post_term_cache'])) {
         $q['update_post_term_cache'] = true;
     }
     if (!isset($q['update_post_meta_cache'])) {
         $q['update_post_meta_cache'] = true;
     }
     if (!isset($q['post_type'])) {
         if ($this->is_search) {
             $q['post_type'] = 'any';
         } else {
             $q['post_type'] = '';
         }
     }
     $post_type = $q['post_type'];
     if (!isset($q['posts_per_page']) || $q['posts_per_page'] == 0) {
         $q['posts_per_page'] = get_option('posts_per_page');
     }
     if (isset($q['showposts']) && $q['showposts']) {
         $q['showposts'] = (int) $q['showposts'];
         $q['posts_per_page'] = $q['showposts'];
     }
     if (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0 && ($this->is_archive || $this->is_search)) {
         $q['posts_per_page'] = $q['posts_per_archive_page'];
     }
     if (!isset($q['nopaging'])) {
         if ($q['posts_per_page'] == -1) {
             $q['nopaging'] = true;
         } else {
             $q['nopaging'] = false;
         }
     }
     if ($this->is_feed) {
         $q['posts_per_page'] = get_option('posts_per_rss');
         $q['nopaging'] = false;
     }
     $q['posts_per_page'] = (int) $q['posts_per_page'];
     if ($q['posts_per_page'] < -1) {
         $q['posts_per_page'] = abs($q['posts_per_page']);
     } else {
         if ($q['posts_per_page'] == 0) {
             $q['posts_per_page'] = 1;
         }
     }
     if (!isset($q['comments_per_page']) || $q['comments_per_page'] == 0) {
         $q['comments_per_page'] = get_option('comments_per_page');
     }
     if ($this->is_home && (empty($this->query) || $q['preview'] == 'true') && 'page' == get_option('show_on_front') && get_option('page_on_front')) {
         $this->is_page = true;
         $this->is_home = false;
         $q['page_id'] = get_option('page_on_front');
     }
     if (isset($q['page'])) {
         $q['page'] = trim($q['page'], '/');
         $q['page'] = absint($q['page']);
     }
     // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
     if (isset($q['no_found_rows'])) {
         $q['no_found_rows'] = (bool) $q['no_found_rows'];
     } else {
         $q['no_found_rows'] = false;
     }
     switch ($q['fields']) {
         case 'ids':
             $fields = "{$this->network_posts}.ID";
             break;
         case 'id=>parent':
             $fields = "{$this->network_posts}.ID, {$this->network_posts}.post_parent";
             break;
         default:
             $fields = "{$this->network_posts}.*";
     }
     // If a month is specified in the querystring, load that month
     if ($q['m']) {
         $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
         $where .= " AND YEAR({$this->network_posts}.post_date)=" . substr($q['m'], 0, 4);
         if (strlen($q['m']) > 5) {
             $where .= " AND MONTH({$this->network_posts}.post_date)=" . substr($q['m'], 4, 2);
         }
         if (strlen($q['m']) > 7) {
             $where .= " AND DAYOFMONTH({$this->network_posts}.post_date)=" . substr($q['m'], 6, 2);
         }
         if (strlen($q['m']) > 9) {
             $where .= " AND HOUR({$this->network_posts}.post_date)=" . substr($q['m'], 8, 2);
         }
         if (strlen($q['m']) > 11) {
             $where .= " AND MINUTE({$this->network_posts}.post_date)=" . substr($q['m'], 10, 2);
         }
         if (strlen($q['m']) > 13) {
             $where .= " AND SECOND({$this->network_posts}.post_date)=" . substr($q['m'], 12, 2);
         }
     }
     if ('' !== $q['hour']) {
         $where .= " AND HOUR({$this->network_posts}.post_date)='" . $q['hour'] . "'";
     }
     if ('' !== $q['minute']) {
         $where .= " AND MINUTE({$this->network_posts}.post_date)='" . $q['minute'] . "'";
     }
     if ('' !== $q['second']) {
         $where .= " AND SECOND({$this->network_posts}.post_date)='" . $q['second'] . "'";
     }
     if ($q['year']) {
         $where .= " AND YEAR({$this->network_posts}.post_date)='" . $q['year'] . "'";
     }
     if ($q['monthnum']) {
         $where .= " AND MONTH({$this->network_posts}.post_date)='" . $q['monthnum'] . "'";
     }
     if ($q['day']) {
         $where .= " AND DAYOFMONTH({$this->network_posts}.post_date)='" . $q['day'] . "'";
     }
     if (isset($q['date_query'])) {
         $date_query = new WP_Date_Query($q['date_query'], 'wp_network_posts.post_date');
         $where .= $date_query->get_sql();
     }
     // If we've got a post_type AND its not "any" post_type.
     if (!empty($q['post_type']) && 'any' != $q['post_type']) {
         foreach ((array) $q['post_type'] as $_post_type) {
             $ptype_obj = get_post_type_object($_post_type);
             if (!$ptype_obj || !$ptype_obj->query_var || empty($q[$ptype_obj->query_var])) {
                 continue;
             }
             if (!$ptype_obj->hierarchical || strpos($q[$ptype_obj->query_var], '/') === false) {
                 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
                 $q['name'] = $q[$ptype_obj->query_var];
             } else {
                 // Hierarchical post_types will operate through the
                 $q['pagename'] = $q[$ptype_obj->query_var];
                 $q['name'] = '';
             }
             // Only one request for a slug is possible, this is why name & pagename are overwritten above.
             break;
         }
         //end foreach
         unset($ptype_obj);
     }
     if ('' != $q['name']) {
         $q['name'] = sanitize_title_for_query($q['name']);
         $where .= " AND {$this->network_posts}.post_name = '" . $q['name'] . "'";
     } elseif ('' != $q['pagename']) {
         if (isset($this->queried_object_id)) {
             $reqpage = $this->queried_object_id;
         } else {
             if ('page' != $q['post_type']) {
                 foreach ((array) $q['post_type'] as $_post_type) {
                     $ptype_obj = get_post_type_object($_post_type);
                     if (!$ptype_obj || !$ptype_obj->hierarchical) {
                         continue;
                     }
                     $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
                     if ($reqpage) {
                         break;
                     }
                 }
                 unset($ptype_obj);
             } else {
                 $reqpage = get_page_by_path($q['pagename']);
             }
             if (!empty($reqpage)) {
                 $reqpage = $reqpage->ID;
             } else {
                 $reqpage = 0;
             }
         }
         $page_for_posts = get_option('page_for_posts');
         if ('page' != get_option('show_on_front') || empty($page_for_posts) || $reqpage != $page_for_posts) {
             $q['pagename'] = sanitize_title_for_query(wp_basename($q['pagename']));
             $q['name'] = $q['pagename'];
             $where .= " AND ({$wpdb->network_posts}.ID = '{$reqpage}')";
             $reqpage_obj = get_page($reqpage);
             if (is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type) {
                 $this->is_attachment = true;
                 $post_type = $q['post_type'] = 'attachment';
                 $this->is_page = true;
                 $q['attachment_id'] = $reqpage;
             }
         }
     } elseif ('' != $q['attachment']) {
         $q['attachment'] = sanitize_title_for_query(wp_basename($q['attachment']));
         $q['name'] = $q['attachment'];
         $where .= " AND {$this->network_posts}.post_name = '" . $q['attachment'] . "'";
     }
     if ($q['w']) {
         $where .= ' AND ' . _wp_mysql_week("`{$this->network_posts}`.`post_date`") . " = '" . $q['w'] . "'";
     }
     if (intval($q['comments_popup'])) {
         $q['p'] = absint($q['comments_popup']);
     }
     // If an attachment is requested by number, let it supersede any post number.
     if ($q['attachment_id']) {
         $q['p'] = absint($q['attachment_id']);
     }
     // If a post number is specified, load that post
     if ($q['p']) {
         $where .= " AND {$this->network_posts}.ID = " . $q['p'];
     } elseif ($q['post__in']) {
         $post__in = implode(',', array_map('absint', $q['post__in']));
         $where .= " AND {$this->network_posts}.ID IN ({$post__in})";
     } elseif ($q['post__not_in']) {
         $post__not_in = implode(',', array_map('absint', $q['post__not_in']));
         $where .= " AND {$this->network_posts}.ID NOT IN ({$post__not_in})";
     }
     if (is_numeric($q['post_parent'])) {
         $where .= $wpdb->prepare(" AND {$this->network_posts}.post_parent = %d ", $q['post_parent']);
     }
     if ($q['page_id']) {
         if ('page' != get_option('show_on_front') || $q['page_id'] != get_option('page_for_posts')) {
             $q['p'] = $q['page_id'];
             $where = " AND {$this->network_posts}.ID = " . $q['page_id'];
         }
     }
     // If a search pattern is specified, load the posts that match
     if (!empty($q['s'])) {
         // added slashes screw with quote grouping when done early, so done later
         $q['s'] = stripslashes($q['s']);
         $q['search_terms'] = array();
         if (!empty($q['sentence'])) {
             $q['search_terms'][] = $q['s'];
         } else {
             preg_match_all('/".*?("|$)|((?<=[\\r\\n\\t ",+])|^)[^\\r\\n\\t ",+]+/', $q['s'], $matches);
             foreach ($matches[0] as $match) {
                 $q['search_terms'][] = trim($match, "\"'\n\r ");
             }
         }
         $n = !empty($q['exact']) ? '' : '%';
         $searchand = '';
         foreach ((array) $q['search_terms'] as $term) {
             $term = esc_sql(like_escape($term));
             $search .= "{$searchand}(({$this->network_posts}.post_title LIKE '{$n}{$term}{$n}') OR ({$this->network_posts}.post_content LIKE '{$n}{$term}{$n}'))";
             $searchand = ' AND ';
         }
         if (!empty($search)) {
             $search = " AND ({$search}) ";
             if (!is_user_logged_in()) {
                 $search .= " AND ({$this->network_posts}.post_password = '') ";
             }
         }
     }
     // Allow plugins to contextually add/remove/modify the search section of the database query
     $search = apply_filters_ref_array('network_posts_search', array($search, &$this));
     // Taxonomies
     if (!$this->is_singular) {
         $this->parse_tax_query($q);
         $clauses = $this->tax_query->get_sql($this->network_posts, 'ID', 'BLOG_ID');
         $join .= $clauses['join'];
         $where .= $clauses['where'];
     }
     if ($this->is_tax) {
         if (empty($post_type)) {
             $post_type = 'any';
             $post_status_join = true;
         } elseif (in_array('attachment', (array) $post_type)) {
             $post_status_join = true;
         }
     }
     // Back-compat
     if (!empty($this->tax_query->queries)) {
         $tax_query_in_and = wp_list_filter($this->tax_query->queries, array('operator' => 'NOT IN'), 'NOT');
         if (!empty($tax_query_in_and)) {
             if (!isset($q['taxonomy'])) {
                 foreach ($tax_query_in_and as $a_tax_query) {
                     if (!in_array($a_tax_query['taxonomy'], array('category', 'post_tag'))) {
                         $q['taxonomy'] = $a_tax_query['taxonomy'];
                         if ('slug' == $a_tax_query['field']) {
                             $q['term'] = $a_tax_query['terms'][0];
                         } else {
                             $q['term_id'] = $a_tax_query['terms'][0];
                         }
                         break;
                     }
                 }
             }
             $cat_query = wp_list_filter($tax_query_in_and, array('taxonomy' => 'category'));
             if (!empty($cat_query)) {
                 $cat_query = reset($cat_query);
                 $the_cat = get_term_by($cat_query['field'], $cat_query['terms'][0], 'category');
                 if ($the_cat) {
                     $this->set('cat', $the_cat->term_id);
                     $this->set('category_name', $the_cat->slug);
                 }
                 unset($the_cat);
             }
             unset($cat_query);
             $tag_query = wp_list_filter($tax_query_in_and, array('taxonomy' => 'post_tag'));
             if (!empty($tag_query)) {
                 $tag_query = reset($tag_query);
                 $the_tag = get_term_by($tag_query['field'], $tag_query['terms'][0], 'post_tag');
                 if ($the_tag) {
                     $this->set('tag_id', $the_tag->term_id);
                 }
                 unset($the_tag);
             }
             unset($tag_query);
         }
     }
     if (!empty($this->tax_query->queries) || !empty($this->meta_query->queries)) {
         $groupby = "{$this->network_posts}.ID, {$this->network_posts}.BLOG_ID";
     }
     // Author/user stuff
     if (empty($q['author']) || $q['author'] == '0') {
         $whichauthor = '';
     } else {
         $q['author'] = (string) urldecode($q['author']);
         $q['author'] = addslashes_gpc($q['author']);
         if (strpos($q['author'], '-') !== false) {
             $eq = '!=';
             $andor = 'AND';
             $q['author'] = explode('-', $q['author']);
             $q['author'] = (string) absint($q['author'][1]);
         } else {
             $eq = '=';
             $andor = 'OR';
         }
         $author_array = preg_split('/[,\\s]+/', $q['author']);
         $_author_array = array();
         foreach ($author_array as $key => $_author) {
             $_author_array[] = "{$this->network_posts}.post_author " . $eq . ' ' . absint($_author);
         }
         $whichauthor .= ' AND (' . implode(" {$andor} ", $_author_array) . ')';
         unset($author_array, $_author_array);
     }
     // Author stuff for nice URLs
     if ('' != $q['author_name']) {
         if (strpos($q['author_name'], '/') !== false) {
             $q['author_name'] = explode('/', $q['author_name']);
             if ($q['author_name'][count($q['author_name']) - 1]) {
                 $q['author_name'] = $q['author_name'][count($q['author_name']) - 1];
                 // no trailing slash
             } else {
                 $q['author_name'] = $q['author_name'][count($q['author_name']) - 2];
                 // there was a trailing slash
             }
         }
         $q['author_name'] = sanitize_title_for_query($q['author_name']);
         $q['author'] = get_user_by('slug', $q['author_name']);
         if ($q['author']) {
             $q['author'] = $q['author']->ID;
         }
         $whichauthor .= " AND ({$this->network_posts}.post_author = " . absint($q['author']) . ')';
     }
     // MIME-Type stuff for attachment browsing
     if (isset($q['post_mime_type']) && '' != $q['post_mime_type']) {
         $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $this->network_posts);
     }
     $where .= $search . $whichauthor . $whichmimetype;
     if (empty($q['order']) || strtoupper($q['order']) != 'ASC' && strtoupper($q['order']) != 'DESC') {
         $q['order'] = 'DESC';
     }
     // blog id
     if (!empty($q['blog_id'])) {
         $where .= " AND ({$this->network_posts}.BLOG_ID = " . absint($q['blog_id']) . ')';
     }
     // Order by
     if (empty($q['orderby'])) {
         $orderby = "{$this->network_posts}.post_date " . $q['order'];
     } elseif ('none' == $q['orderby']) {
         $orderby = '';
     } else {
         // Used to filter values
         $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
         if (!empty($q['meta_key'])) {
             $allowed_keys[] = $q['meta_key'];
             $allowed_keys[] = 'meta_value';
             $allowed_keys[] = 'meta_value_num';
         }
         $q['orderby'] = urldecode($q['orderby']);
         $q['orderby'] = addslashes_gpc($q['orderby']);
         $orderby_array = array();
         foreach (explode(' ', $q['orderby']) as $i => $orderby) {
             // Only allow certain values for safety
             if (!in_array($orderby, $allowed_keys)) {
                 continue;
             }
             switch ($orderby) {
                 case 'menu_order':
                     $orderby = "{$this->network_posts}.menu_order";
                     break;
                 case 'ID':
                     $orderby = "{$this->network_posts}.ID";
                     break;
                 case 'rand':
                     $orderby = 'RAND()';
                     break;
                 case $q['meta_key']:
                 case 'meta_value':
                     $orderby = "{$this->network_postmeta}.meta_value";
                     break;
                 case 'meta_value_num':
                     $orderby = "{$this->network_postmeta}.meta_value+0";
                     break;
                 case 'comment_count':
                     $orderby = "{$this->network_posts}.comment_count";
                     break;
                 default:
                     $orderby = "{$this->network_posts}.post_" . $orderby;
             }
             $orderby_array[] = $orderby;
         }
         $orderby = implode(',', $orderby_array);
         if (empty($orderby)) {
             $orderby = "{$this->network_posts}.post_date " . $q['order'];
         } else {
             $orderby .= " {$q['order']}";
         }
     }
     if (is_array($post_type)) {
         $post_type_cap = 'multiple_post_type';
     } else {
         $post_type_object = get_post_type_object($post_type);
         if (empty($post_type_object)) {
             $post_type_cap = $post_type;
         }
     }
     if ('any' == $post_type) {
         $in_search_post_types = get_post_types(array('exclude_from_search' => false));
         if (!empty($in_search_post_types)) {
             $where .= " AND {$this->network_posts}.post_type IN ('" . join("', '", $in_search_post_types) . "')";
         }
     } elseif (!empty($post_type) && is_array($post_type)) {
         $where .= " AND {$this->network_posts}.post_type IN ('" . join("', '", $post_type) . "')";
     } elseif (!empty($post_type)) {
         $where .= " AND {$this->network_posts}.post_type = '{$post_type}'";
         $post_type_object = get_post_type_object($post_type);
     } elseif ($this->is_attachment) {
         $where .= " AND {$this->network_posts}.post_type = 'attachment'";
         $post_type_object = get_post_type_object('attachment');
     } elseif ($this->is_page) {
         $where .= " AND {$this->network_posts}.post_type = 'page'";
         $post_type_object = get_post_type_object('page');
     } else {
         $where .= " AND {$this->network_posts}.post_type = 'post'";
         $post_type_object = get_post_type_object('post');
     }
     if (!empty($post_type_object)) {
         $edit_cap = $post_type_object->cap->edit_post;
         $read_cap = $post_type_object->cap->read_post;
         $edit_others_cap = $post_type_object->cap->edit_others_posts;
         $read_private_cap = $post_type_object->cap->read_private_posts;
     } else {
         $edit_cap = 'edit_' . $post_type_cap;
         $read_cap = 'read_' . $post_type_cap;
         $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
         $read_private_cap = 'read_private_' . $post_type_cap . 's';
     }
     if (!empty($q['post_status'])) {
         $statuswheres = array();
         $q_status = $q['post_status'];
         if (!is_array($q_status)) {
             $q_status = explode(',', $q_status);
         }
         $r_status = array();
         $p_status = array();
         $e_status = array();
         if (in_array('any', $q_status)) {
             foreach (get_post_stati(array('exclude_from_search' => true)) as $status) {
                 $e_status[] = "{$this->network_posts}.post_status <> '{$status}'";
             }
         } else {
             foreach (get_post_stati() as $status) {
                 if (in_array($status, $q_status)) {
                     if ('private' == $status) {
                         $p_status[] = "{$this->network_posts}.post_status = '{$status}'";
                     } else {
                         $r_status[] = "{$this->network_posts}.post_status = '{$status}'";
                     }
                 }
             }
         }
         if (empty($q['perm']) || 'readable' != $q['perm']) {
             $r_status = array_merge($r_status, $p_status);
             unset($p_status);
         }
         if (!empty($e_status)) {
             $statuswheres[] = "(" . join(' AND ', $e_status) . ")";
         }
         if (!empty($r_status)) {
             if (!empty($q['perm']) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap)) {
                 $statuswheres[] = "({$this->network_posts}.post_author = {$user_ID} " . "AND (" . join(' OR ', $r_status) . "))";
             } else {
                 $statuswheres[] = "(" . join(' OR ', $r_status) . ")";
             }
         }
         if (!empty($p_status)) {
             if (!empty($q['perm']) && 'readable' == $q['perm'] && !current_user_can($read_private_cap)) {
                 $statuswheres[] = "({$this->network_posts}.post_author = {$user_ID} " . "AND (" . join(' OR ', $p_status) . "))";
             } else {
                 $statuswheres[] = "(" . join(' OR ', $p_status) . ")";
             }
         }
         if ($post_status_join) {
             $join .= " LEFT JOIN {$this->network_posts} AS p2 ON ({$this->network_posts}.post_parent = p2.ID) ";
             foreach ($statuswheres as $index => $statuswhere) {
                 $statuswheres[$index] = "({$statuswhere} OR ({$this->network_posts}.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
             }
         }
         foreach ($statuswheres as $statuswhere) {
             $where .= " AND {$statuswhere}";
         }
     } elseif (!$this->is_singular) {
         $where .= " AND ({$this->network_posts}.post_status = 'publish'";
         // Add public states.
         $public_states = get_post_stati(array('public' => true));
         foreach ((array) $public_states as $state) {
             if ('publish' == $state) {
                 // Publish is hard-coded above.
                 continue;
             }
             $where .= " OR {$this->network_posts}.post_status = '{$state}'";
         }
         if ($this->is_admin) {
             // Add protected states that should show in the admin all list.
             $admin_all_states = get_post_stati(array('protected' => true, 'show_in_admin_all_list' => true));
             foreach ((array) $admin_all_states as $state) {
                 $where .= " OR {$this->network_posts}.post_status = '{$state}'";
             }
         }
         if (is_user_logged_in()) {
             // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
             $private_states = get_post_stati(array('private' => true));
             foreach ((array) $private_states as $state) {
                 $where .= current_user_can($read_private_cap) ? " OR {$this->network_posts}.post_status = '{$state}'" : " OR {$this->network_posts}.post_author = {$user_ID} AND {$this->network_posts}.post_status = '{$state}'";
             }
         }
         $where .= ')';
     }
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('network_post', $this->network_posts, 'ID', $this);
         //print_r($clauses);
         $join .= $clauses['join'];
         $where .= $clauses['where'];
     }
     // Apply filters on where and join prior to paging so that any
     // manipulations to them are reflected in the paging by day queries.
     if (!$q['suppress_filters']) {
         $where = apply_filters_ref_array('network_posts_where', array($where, &$this));
         $join = apply_filters_ref_array('network_posts_join', array($join, &$this));
     }
     // Paging
     if (empty($q['nopaging']) && !$this->is_singular) {
         $page = absint($q['paged']);
         if (!$page) {
             $page = 1;
         }
         if (empty($q['offset'])) {
             $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
         } else {
             // we're ignoring $page and using 'offset'
             $q['offset'] = absint($q['offset']);
             $pgstrt = $q['offset'] . ', ';
         }
         $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
     }
     // Comments feeds
     /*
     if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
     	if ( $this->is_archive || $this->is_search ) {
     		$cjoin = "JOIN $this->posts ON ($this->comments.comment_post_ID = $this->posts.ID) $join ";
     		$cwhere = "WHERE comment_approved = '1' $where";
     		$cgroupby = "$wpdb->comments.comment_id";
     	} else { // Other non singular e.g. front
     		$cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
     		$cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
     		$cgroupby = '';
     	}
     
     	if ( !$q['suppress_filters'] ) {
     		$cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
     		$cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
     		$cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
     		$corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
     		$climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
     	}
     	$cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
     	$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
     
     	$this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
     	$this->comment_count = count($this->comments);
     
     	$post_ids = array();
     
     	foreach ( $this->comments as $comment )
     		$post_ids[] = (int) $comment->comment_post_ID;
     
     	$post_ids = join(',', $post_ids);
     	$join = '';
     	if ( $post_ids )
     		$where = "AND $wpdb->posts.ID IN ($post_ids) ";
     	else
     		$where = "AND 0";
     }
     */
     $pieces = array('where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits');
     // Apply post-paging filters on where and join. Only plugins that
     // manipulate paging queries should use these hooks.
     if (!$q['suppress_filters']) {
         $where = apply_filters_ref_array('network_posts_where_paged', array($where, &$this));
         $groupby = apply_filters_ref_array('network_posts_groupby', array($groupby, &$this));
         $join = apply_filters_ref_array('network_posts_join_paged', array($join, &$this));
         $orderby = apply_filters_ref_array('network_posts_orderby', array($orderby, &$this));
         $distinct = apply_filters_ref_array('network_posts_distinct', array($distinct, &$this));
         $limits = apply_filters_ref_array('network_post_limits', array($limits, &$this));
         $fields = apply_filters_ref_array('network_posts_fields', array($fields, &$this));
         // Filter all clauses at once, for convenience
         $clauses = (array) apply_filters_ref_array('network_posts_clauses', array(compact($pieces), &$this));
         foreach ($pieces as $piece) {
             ${$piece} = isset($clauses[$piece]) ? $clauses[$piece] : '';
         }
     }
     // Announce current selection parameters. For use by caching plugins.
     do_action('posts_selection', $where . $groupby . $orderby . $limits . $join);
     // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
     if (!$q['suppress_filters']) {
         $where = apply_filters_ref_array('network_posts_where_request', array($where, &$this));
         $groupby = apply_filters_ref_array('network_posts_groupby_request', array($groupby, &$this));
         $join = apply_filters_ref_array('network_posts_join_request', array($join, &$this));
         $orderby = apply_filters_ref_array('network_posts_orderby_request', array($orderby, &$this));
         $distinct = apply_filters_ref_array('network_posts_distinct_request', array($distinct, &$this));
         $fields = apply_filters_ref_array('network_posts_fields_request', array($fields, &$this));
         $limits = apply_filters_ref_array('network_post_limits_request', array($limits, &$this));
         // Filter all clauses at once, for convenience
         $clauses = (array) apply_filters_ref_array('network_posts_clauses_request', array(compact($pieces), &$this));
         foreach ($pieces as $piece) {
             ${$piece} = isset($clauses[$piece]) ? $clauses[$piece] : '';
         }
     }
     if (!empty($groupby)) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if (!empty($orderby)) {
         $orderby = 'ORDER BY ' . $orderby;
     }
     $found_rows = '';
     if (!$q['no_found_rows'] && !empty($limits)) {
         $found_rows = 'SQL_CALC_FOUND_ROWS';
     }
     $this->request = $old_request = "SELECT {$found_rows} {$distinct} {$fields} FROM {$this->network_posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}";
     //echo $this->request;
     if (!$q['suppress_filters']) {
         $this->request = apply_filters_ref_array('network_posts_request', array($this->request, &$this));
     }
     if ('ids' == $q['fields']) {
         $this->posts = $wpdb->get_col($this->request);
         return $this->posts;
     }
     if ('id=>parent' == $q['fields']) {
         $this->posts = $wpdb->get_results($this->request);
         $r = array();
         foreach ($this->posts as $post) {
             $r[$post->ID] = $post->post_parent;
         }
         return $r;
     }
     $split_the_query = $old_request == $this->request && "{$this->network_posts}.*" == $fields && !empty($limits) && $q['posts_per_page'] < 500;
     $split_the_query = apply_filters('network_split_the_query', $split_the_query, $this);
     if ($split_the_query) {
         // First get the IDs and then fill in the objects
         $this->request = "SELECT {$found_rows} {$distinct} {$this->network_posts}.BLOG_ID, {$this->network_posts}.ID FROM {$this->network_posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}";
         $this->request = apply_filters('network_posts_request_ids', $this->request, $this);
         $ids = $wpdb->get_results($this->request);
         if ($ids) {
             $this->set_found_posts($q, $limits);
             $this->posts = array();
             foreach ($ids as $id) {
                 $this->posts[] = network_get_post($id->BLOG_ID, $id->ID);
             }
         } else {
             $this->found_posts = $this->max_num_pages = 0;
             $this->posts = array();
         }
     } else {
         $this->posts = $wpdb->get_results($this->request);
         $this->set_found_posts($q, $limits);
     }
     // Raw results filter. Prior to status checks.
     if (!$q['suppress_filters']) {
         $this->posts = apply_filters_ref_array('network_posts_results', array($this->posts, &$this));
     }
     /*
     if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
     	$cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
     	$cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
     	$cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
     	$cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
     	$corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
     	$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
     	$climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
     	$comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
     	$this->comments = $wpdb->get_results($comments_request);
     	$this->comment_count = count($this->comments);
     }
     */
     if (!$q['suppress_filters']) {
         $this->posts = apply_filters_ref_array('network_the_posts', array($this->posts, &$this));
     }
     $this->post_count = count($this->posts);
     // Always sanitize
     foreach ($this->posts as $i => $post) {
         $this->posts[$i] = sanitize_post($post, 'raw');
     }
     if ($q['cache_results']) {
         update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
     }
     if ($this->post_count > 0) {
         $this->post = $this->posts[0];
     }
     return $this->posts;
 }
Ejemplo n.º 9
0
 /**
  * Get the IDs that a user is following.
  *
  * @since 1.0.0
  *
  * @param int $user_id The user ID.
  * @param string $follow_type The follow type.  Leave blank to query users.
  * @param array $query_args {
  *     Various query arguments
  *     @type array $date_query See {@link WP_Date_Query}.
  *     @type string $orderby The DB column to order results by. Default: 'id'.
  *     @type string $order The order. Either 'ASC' or 'DESC'. Default: 'DESC'.
  * }
  * @return array
  */
 public static function get_following($user_id = 0, $follow_type = '', $query_args = array())
 {
     global $wpdb;
     // SQL statement
     $sql = self::get_select_sql('leader_id');
     $sql .= self::get_where_sql(array('follower_id' => $user_id, 'follow_type' => $follow_type));
     // Setup date query
     if (!empty($query_args['date_query']) && class_exists('WP_Date_Query')) {
         add_filter('date_query_valid_columns', array(__CLASS__, 'register_date_column'));
         $date_query = new WP_Date_Query($query_args['date_query'], 'date_recorded');
         $sql .= $date_query->get_sql();
         remove_filter('date_query_valid_columns', array(__CLASS__, 'register_date_column'));
     }
     // Setup orderby query
     $orderby = array();
     if (!empty($query_args['orderby'])) {
         $orderby = $query_args['orderby'];
     }
     if (!empty($query_args['order'])) {
         $orderby = $query_args['order'];
     }
     $sql .= self::get_orderby_sql($orderby);
     // do the query
     return $wpdb->get_col($sql);
 }
Ejemplo n.º 10
0
 /**
  * Retrieve the posts based on query variables.
  *
  * There are a few filters and actions that can be used to modify the post
  * database query.
  *
  * @since 1.5.0
  * @access public
  * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  *
  * @return array List of posts.
  */
 function get_posts()
 {
     global $wpdb;
     $this->parse_query();
     do_action_ref_array('pre_get_posts', array(&$this));
     // Shorthand.
     $q =& $this->query_vars;
     // Fill again in case pre_get_posts unset some vars.
     $q = $this->fill_query_vars($q);
     // Parse meta query
     $this->meta_query = new WP_Meta_Query();
     $this->meta_query->parse_query_vars($q);
     // Set a flag if a pre_get_posts hook changed the query vars.
     $hash = md5(serialize($this->query_vars));
     if ($hash != $this->query_vars_hash) {
         $this->query_vars_changed = true;
         $this->query_vars_hash = $hash;
     }
     unset($hash);
     // First let's clear some variables
     $distinct = '';
     $whichauthor = '';
     $whichmimetype = '';
     $where = '';
     $limits = '';
     $join = '';
     $search = '';
     $groupby = '';
     $fields = '';
     $post_status_join = false;
     $page = 1;
     if (isset($q['caller_get_posts'])) {
         _deprecated_argument('WP_Query', '3.1', __('"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.'));
         if (!isset($q['ignore_sticky_posts'])) {
             $q['ignore_sticky_posts'] = $q['caller_get_posts'];
         }
     }
     if (!isset($q['ignore_sticky_posts'])) {
         $q['ignore_sticky_posts'] = false;
     }
     if (!isset($q['suppress_filters'])) {
         $q['suppress_filters'] = false;
     }
     if (!isset($q['cache_results'])) {
         if (wp_using_ext_object_cache()) {
             $q['cache_results'] = false;
         } else {
             $q['cache_results'] = true;
         }
     }
     if (!isset($q['update_post_term_cache'])) {
         $q['update_post_term_cache'] = true;
     }
     if (!isset($q['update_post_meta_cache'])) {
         $q['update_post_meta_cache'] = true;
     }
     if (!isset($q['post_type'])) {
         if ($this->is_search) {
             $q['post_type'] = 'any';
         } else {
             $q['post_type'] = '';
         }
     }
     $post_type = $q['post_type'];
     if (!isset($q['posts_per_page']) || $q['posts_per_page'] == 0) {
         $q['posts_per_page'] = get_option('posts_per_page');
     }
     if (isset($q['showposts']) && $q['showposts']) {
         $q['showposts'] = (int) $q['showposts'];
         $q['posts_per_page'] = $q['showposts'];
     }
     if (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0 && ($this->is_archive || $this->is_search)) {
         $q['posts_per_page'] = $q['posts_per_archive_page'];
     }
     if (!isset($q['nopaging'])) {
         if ($q['posts_per_page'] == -1) {
             $q['nopaging'] = true;
         } else {
             $q['nopaging'] = false;
         }
     }
     if ($this->is_feed) {
         $q['posts_per_page'] = get_option('posts_per_rss');
         $q['nopaging'] = false;
     }
     $q['posts_per_page'] = (int) $q['posts_per_page'];
     if ($q['posts_per_page'] < -1) {
         $q['posts_per_page'] = abs($q['posts_per_page']);
     } else {
         if ($q['posts_per_page'] == 0) {
             $q['posts_per_page'] = 1;
         }
     }
     if (!isset($q['comments_per_page']) || $q['comments_per_page'] == 0) {
         $q['comments_per_page'] = get_option('comments_per_page');
     }
     if ($this->is_home && (empty($this->query) || $q['preview'] == 'true') && 'page' == get_option('show_on_front') && get_option('page_on_front')) {
         $this->is_page = true;
         $this->is_home = false;
         $q['page_id'] = get_option('page_on_front');
     }
     if (isset($q['page'])) {
         $q['page'] = trim($q['page'], '/');
         $q['page'] = absint($q['page']);
     }
     // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
     if (isset($q['no_found_rows'])) {
         $q['no_found_rows'] = (bool) $q['no_found_rows'];
     } else {
         $q['no_found_rows'] = false;
     }
     switch ($q['fields']) {
         case 'ids':
             $fields = "{$wpdb->posts}.ID";
             break;
         case 'id=>parent':
             $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent";
             break;
         default:
             $fields = "{$wpdb->posts}.*";
     }
     if ('' !== $q['menu_order']) {
         $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order'];
     }
     // The "m" parameter is meant for months but accepts datetimes of varying specificity
     if ($q['m']) {
         $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr($q['m'], 0, 4);
         if (strlen($q['m']) > 5) {
             $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 4, 2);
         }
         if (strlen($q['m']) > 7) {
             $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 6, 2);
         }
         if (strlen($q['m']) > 9) {
             $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr($q['m'], 8, 2);
         }
         if (strlen($q['m']) > 11) {
             $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr($q['m'], 10, 2);
         }
         if (strlen($q['m']) > 13) {
             $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr($q['m'], 12, 2);
         }
     }
     // Handle the other individual date parameters
     $date_parameters = array();
     if ('' !== $q['hour']) {
         $date_parameters['hour'] = $q['hour'];
     }
     if ('' !== $q['minute']) {
         $date_parameters['minute'] = $q['minute'];
     }
     if ('' !== $q['second']) {
         $date_parameters['second'] = $q['second'];
     }
     if ($q['year']) {
         $date_parameters['year'] = $q['year'];
     }
     if ($q['monthnum']) {
         $date_parameters['monthnum'] = $q['monthnum'];
     }
     if ($q['w']) {
         $date_parameters['week'] = $q['w'];
     }
     if ($q['day']) {
         $date_parameters['day'] = $q['day'];
     }
     if ($date_parameters) {
         $date_query = new WP_Date_Query(array($date_parameters));
         $where .= $date_query->get_sql();
     }
     unset($date_parameters, $date_query);
     // Handle complex date queries
     if (!empty($q['date_query'])) {
         $this->date_query = new WP_Date_Query($q['date_query']);
         $where .= $this->date_query->get_sql();
     }
     // If we've got a post_type AND it's not "any" post_type.
     if (!empty($q['post_type']) && 'any' != $q['post_type']) {
         foreach ((array) $q['post_type'] as $_post_type) {
             $ptype_obj = get_post_type_object($_post_type);
             if (!$ptype_obj || !$ptype_obj->query_var || empty($q[$ptype_obj->query_var])) {
                 continue;
             }
             if (!$ptype_obj->hierarchical || strpos($q[$ptype_obj->query_var], '/') === false) {
                 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
                 $q['name'] = $q[$ptype_obj->query_var];
             } else {
                 // Hierarchical post_types will operate through the
                 $q['pagename'] = $q[$ptype_obj->query_var];
                 $q['name'] = '';
             }
             // Only one request for a slug is possible, this is why name & pagename are overwritten above.
             break;
         }
         //end foreach
         unset($ptype_obj);
     }
     if ('' != $q['name']) {
         $q['name'] = sanitize_title_for_query($q['name']);
         $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'";
     } elseif ('' != $q['pagename']) {
         if (isset($this->queried_object_id)) {
             $reqpage = $this->queried_object_id;
         } else {
             if ('page' != $q['post_type']) {
                 foreach ((array) $q['post_type'] as $_post_type) {
                     $ptype_obj = get_post_type_object($_post_type);
                     if (!$ptype_obj || !$ptype_obj->hierarchical) {
                         continue;
                     }
                     $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
                     if ($reqpage) {
                         break;
                     }
                 }
                 unset($ptype_obj);
             } else {
                 $reqpage = get_page_by_path($q['pagename']);
             }
             if (!empty($reqpage)) {
                 $reqpage = $reqpage->ID;
             } else {
                 $reqpage = 0;
             }
         }
         $page_for_posts = get_option('page_for_posts');
         if ('page' != get_option('show_on_front') || empty($page_for_posts) || $reqpage != $page_for_posts) {
             $q['pagename'] = sanitize_title_for_query(wp_basename($q['pagename']));
             $q['name'] = $q['pagename'];
             $where .= " AND ({$wpdb->posts}.ID = '{$reqpage}')";
             $reqpage_obj = get_post($reqpage);
             if (is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type) {
                 $this->is_attachment = true;
                 $post_type = $q['post_type'] = 'attachment';
                 $this->is_page = true;
                 $q['attachment_id'] = $reqpage;
             }
         }
     } elseif ('' != $q['attachment']) {
         $q['attachment'] = sanitize_title_for_query(wp_basename($q['attachment']));
         $q['name'] = $q['attachment'];
         $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'";
     }
     if (intval($q['comments_popup'])) {
         $q['p'] = absint($q['comments_popup']);
     }
     // If an attachment is requested by number, let it supersede any post number.
     if ($q['attachment_id']) {
         $q['p'] = absint($q['attachment_id']);
     }
     // If a post number is specified, load that post
     if ($q['p']) {
         $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
     } elseif ($q['post__in']) {
         $post__in = implode(',', array_map('absint', $q['post__in']));
         $where .= " AND {$wpdb->posts}.ID IN ({$post__in})";
     } elseif ($q['post__not_in']) {
         $post__not_in = implode(',', array_map('absint', $q['post__not_in']));
         $where .= " AND {$wpdb->posts}.ID NOT IN ({$post__not_in})";
     }
     if (is_numeric($q['post_parent'])) {
         $where .= $wpdb->prepare(" AND {$wpdb->posts}.post_parent = %d ", $q['post_parent']);
     } elseif ($q['post_parent__in']) {
         $post_parent__in = implode(',', array_map('absint', $q['post_parent__in']));
         $where .= " AND {$wpdb->posts}.post_parent IN ({$post_parent__in})";
     } elseif ($q['post_parent__not_in']) {
         $post_parent__not_in = implode(',', array_map('absint', $q['post_parent__not_in']));
         $where .= " AND {$wpdb->posts}.post_parent NOT IN ({$post_parent__not_in})";
     }
     if ($q['page_id']) {
         if ('page' != get_option('show_on_front') || $q['page_id'] != get_option('page_for_posts')) {
             $q['p'] = $q['page_id'];
             $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
         }
     }
     // If a search pattern is specified, load the posts that match.
     if (!empty($q['s'])) {
         $search = $this->parse_search($q);
     }
     /**
      * Filter the search SQL that is used in the WHERE clause of WP_Query.
      *
      * @since 3.0.0
      *
      * @param string   $search Search SQL for WHERE clause.
      * @param WP_Query $this   The current WP_Query object.
      */
     $search = apply_filters_ref_array('posts_search', array($search, &$this));
     // Taxonomies
     if (!$this->is_singular) {
         $this->parse_tax_query($q);
         $clauses = $this->tax_query->get_sql($wpdb->posts, 'ID');
         $join .= $clauses['join'];
         $where .= $clauses['where'];
     }
     if ($this->is_tax) {
         if (empty($post_type)) {
             // Do a fully inclusive search for currently registered post types of queried taxonomies
             $post_type = array();
             $taxonomies = wp_list_pluck($this->tax_query->queries, 'taxonomy');
             foreach (get_post_types(array('exclude_from_search' => false)) as $pt) {
                 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies($pt);
                 if (array_intersect($taxonomies, $object_taxonomies)) {
                     $post_type[] = $pt;
                 }
             }
             if (!$post_type) {
                 $post_type = 'any';
             } elseif (count($post_type) == 1) {
                 $post_type = $post_type[0];
             }
             $post_status_join = true;
         } elseif (in_array('attachment', (array) $post_type)) {
             $post_status_join = true;
         }
     }
     // Back-compat
     if (!empty($this->tax_query->queries)) {
         $tax_query_in_and = wp_list_filter($this->tax_query->queries, array('operator' => 'NOT IN'), 'NOT');
         if (!empty($tax_query_in_and)) {
             if (!isset($q['taxonomy'])) {
                 foreach ($tax_query_in_and as $a_tax_query) {
                     if (!in_array($a_tax_query['taxonomy'], array('category', 'post_tag'))) {
                         $q['taxonomy'] = $a_tax_query['taxonomy'];
                         if ('slug' == $a_tax_query['field']) {
                             $q['term'] = $a_tax_query['terms'][0];
                         } else {
                             $q['term_id'] = $a_tax_query['terms'][0];
                         }
                         break;
                     }
                 }
             }
             $cat_query = wp_list_filter($tax_query_in_and, array('taxonomy' => 'category'));
             if (!empty($cat_query)) {
                 $cat_query = reset($cat_query);
                 if (!empty($cat_query['terms'][0])) {
                     $the_cat = get_term_by($cat_query['field'], $cat_query['terms'][0], 'category');
                     if ($the_cat) {
                         $this->set('cat', $the_cat->term_id);
                         $this->set('category_name', $the_cat->slug);
                     }
                     unset($the_cat);
                 }
             }
             unset($cat_query);
             $tag_query = wp_list_filter($tax_query_in_and, array('taxonomy' => 'post_tag'));
             if (!empty($tag_query)) {
                 $tag_query = reset($tag_query);
                 if (!empty($tag_query['terms'][0])) {
                     $the_tag = get_term_by($tag_query['field'], $tag_query['terms'][0], 'post_tag');
                     if ($the_tag) {
                         $this->set('tag_id', $the_tag->term_id);
                     }
                     unset($the_tag);
                 }
             }
             unset($tag_query);
         }
     }
     if (!empty($this->tax_query->queries) || !empty($this->meta_query->queries)) {
         $groupby = "{$wpdb->posts}.ID";
     }
     // Author/user stuff
     if (!empty($q['author']) && $q['author'] != '0') {
         $q['author'] = addslashes_gpc('' . urldecode($q['author']));
         $authors = array_unique(array_map('intval', preg_split('/[,\\s]+/', $q['author'])));
         foreach ($authors as $author) {
             $key = $author > 0 ? 'author__in' : 'author__not_in';
             $q[$key][] = abs($author);
         }
         $q['author'] = implode(',', $authors);
     }
     if (!empty($q['author__not_in'])) {
         $author__not_in = implode(',', array_map('absint', array_unique((array) $q['author__not_in'])));
         $where .= " AND {$wpdb->posts}.post_author NOT IN ({$author__not_in}) ";
     } elseif (!empty($q['author__in'])) {
         $author__in = implode(',', array_map('absint', array_unique((array) $q['author__in'])));
         $where .= " AND {$wpdb->posts}.post_author IN ({$author__in}) ";
     }
     // Author stuff for nice URLs
     if ('' != $q['author_name']) {
         if (strpos($q['author_name'], '/') !== false) {
             $q['author_name'] = explode('/', $q['author_name']);
             if ($q['author_name'][count($q['author_name']) - 1]) {
                 $q['author_name'] = $q['author_name'][count($q['author_name']) - 1];
                 // no trailing slash
             } else {
                 $q['author_name'] = $q['author_name'][count($q['author_name']) - 2];
                 // there was a trailing slash
             }
         }
         $q['author_name'] = sanitize_title_for_query($q['author_name']);
         $q['author'] = get_user_by('slug', $q['author_name']);
         if ($q['author']) {
             $q['author'] = $q['author']->ID;
         }
         $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint($q['author']) . ')';
     }
     // MIME-Type stuff for attachment browsing
     if (isset($q['post_mime_type']) && '' != $q['post_mime_type']) {
         $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $wpdb->posts);
     }
     $where .= $search . $whichauthor . $whichmimetype;
     if (empty($q['order']) || strtoupper($q['order']) != 'ASC' && strtoupper($q['order']) != 'DESC') {
         $q['order'] = 'DESC';
     }
     // Order by
     if (empty($q['orderby'])) {
         $orderby = "{$wpdb->posts}.post_date " . $q['order'];
     } elseif ('none' == $q['orderby']) {
         $orderby = '';
     } elseif ($q['orderby'] == 'post__in' && !empty($post__in)) {
         $orderby = "FIELD( {$wpdb->posts}.ID, {$post__in} )";
     } elseif ($q['orderby'] == 'post_parent__in' && !empty($post_parent__in)) {
         $orderby = "FIELD( {$wpdb->posts}.post_parent, {$post_parent__in} )";
     } else {
         // Used to filter values
         $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
         if (!empty($q['meta_key'])) {
             $allowed_keys[] = $q['meta_key'];
             $allowed_keys[] = 'meta_value';
             $allowed_keys[] = 'meta_value_num';
         }
         $q['orderby'] = urldecode($q['orderby']);
         $q['orderby'] = addslashes_gpc($q['orderby']);
         $orderby_array = array();
         foreach (explode(' ', $q['orderby']) as $i => $orderby) {
             // Only allow certain values for safety
             if (!in_array($orderby, $allowed_keys)) {
                 continue;
             }
             switch ($orderby) {
                 case 'menu_order':
                     $orderby = "{$wpdb->posts}.menu_order";
                     break;
                 case 'ID':
                     $orderby = "{$wpdb->posts}.ID";
                     break;
                 case 'rand':
                     $orderby = 'RAND()';
                     break;
                 case $q['meta_key']:
                 case 'meta_value':
                     if (isset($q['meta_type'])) {
                         $meta_type = $this->meta_query->get_cast_for_type($q['meta_type']);
                         $orderby = "CAST({$wpdb->postmeta}.meta_value AS {$meta_type})";
                     } else {
                         $orderby = "{$wpdb->postmeta}.meta_value";
                     }
                     break;
                 case 'meta_value_num':
                     $orderby = "{$wpdb->postmeta}.meta_value+0";
                     break;
                 case 'comment_count':
                     $orderby = "{$wpdb->posts}.comment_count";
                     break;
                 default:
                     $orderby = "{$wpdb->posts}.post_" . $orderby;
             }
             $orderby_array[] = $orderby;
         }
         $orderby = implode(',', $orderby_array);
         if (empty($orderby)) {
             $orderby = "{$wpdb->posts}.post_date " . $q['order'];
         } else {
             $orderby .= " {$q['order']}";
         }
     }
     // Order search results by relevance only when another "orderby" is not specified in the query.
     if (!empty($q['s'])) {
         $search_orderby = '';
         if (!empty($q['search_orderby_title']) && (empty($q['orderby']) && !$this->is_feed) || isset($q['orderby']) && 'relevance' === $q['orderby']) {
             $search_orderby = $this->parse_search_order($q);
         }
         /**
          * Filter the ORDER BY used when ordering search results.
          *
          * @since 3.7.0
          *
          * @param string   $search_orderby The ORDER BY clause.
          * @param WP_Query $this           The current WP_Query instance.
          */
         $search_orderby = apply_filters('posts_search_orderby', $search_orderby, $this);
         if ($search_orderby) {
             $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
         }
     }
     if (is_array($post_type) && count($post_type) > 1) {
         $post_type_cap = 'multiple_post_type';
     } else {
         if (is_array($post_type)) {
             $post_type = reset($post_type);
         }
         $post_type_object = get_post_type_object($post_type);
         if (empty($post_type_object)) {
             $post_type_cap = $post_type;
         }
     }
     if ('any' == $post_type) {
         $in_search_post_types = get_post_types(array('exclude_from_search' => false));
         if (empty($in_search_post_types)) {
             $where .= ' AND 1=0 ';
         } else {
             $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", $in_search_post_types) . "')";
         }
     } elseif (!empty($post_type) && is_array($post_type)) {
         $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", $post_type) . "')";
     } elseif (!empty($post_type)) {
         $where .= " AND {$wpdb->posts}.post_type = '{$post_type}'";
         $post_type_object = get_post_type_object($post_type);
     } elseif ($this->is_attachment) {
         $where .= " AND {$wpdb->posts}.post_type = 'attachment'";
         $post_type_object = get_post_type_object('attachment');
     } elseif ($this->is_page) {
         $where .= " AND {$wpdb->posts}.post_type = 'page'";
         $post_type_object = get_post_type_object('page');
     } else {
         $where .= " AND {$wpdb->posts}.post_type = 'post'";
         $post_type_object = get_post_type_object('post');
     }
     $edit_cap = 'edit_post';
     $read_cap = 'read_post';
     if (!empty($post_type_object)) {
         $edit_others_cap = $post_type_object->cap->edit_others_posts;
         $read_private_cap = $post_type_object->cap->read_private_posts;
     } else {
         $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
         $read_private_cap = 'read_private_' . $post_type_cap . 's';
     }
     $user_id = get_current_user_id();
     if (!empty($q['post_status'])) {
         $statuswheres = array();
         $q_status = $q['post_status'];
         if (!is_array($q_status)) {
             $q_status = explode(',', $q_status);
         }
         $r_status = array();
         $p_status = array();
         $e_status = array();
         if (in_array('any', $q_status)) {
             foreach (get_post_stati(array('exclude_from_search' => true)) as $status) {
                 $e_status[] = "{$wpdb->posts}.post_status <> '{$status}'";
             }
         } else {
             foreach (get_post_stati() as $status) {
                 if (in_array($status, $q_status)) {
                     if ('private' == $status) {
                         $p_status[] = "{$wpdb->posts}.post_status = '{$status}'";
                     } else {
                         $r_status[] = "{$wpdb->posts}.post_status = '{$status}'";
                     }
                 }
             }
         }
         if (empty($q['perm']) || 'readable' != $q['perm']) {
             $r_status = array_merge($r_status, $p_status);
             unset($p_status);
         }
         if (!empty($e_status)) {
             $statuswheres[] = "(" . join(' AND ', $e_status) . ")";
         }
         if (!empty($r_status)) {
             if (!empty($q['perm']) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap)) {
                 $statuswheres[] = "({$wpdb->posts}.post_author = {$user_id} " . "AND (" . join(' OR ', $r_status) . "))";
             } else {
                 $statuswheres[] = "(" . join(' OR ', $r_status) . ")";
             }
         }
         if (!empty($p_status)) {
             if (!empty($q['perm']) && 'readable' == $q['perm'] && !current_user_can($read_private_cap)) {
                 $statuswheres[] = "({$wpdb->posts}.post_author = {$user_id} " . "AND (" . join(' OR ', $p_status) . "))";
             } else {
                 $statuswheres[] = "(" . join(' OR ', $p_status) . ")";
             }
         }
         if ($post_status_join) {
             $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) ";
             foreach ($statuswheres as $index => $statuswhere) {
                 $statuswheres[$index] = "({$statuswhere} OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
             }
         }
         foreach ($statuswheres as $statuswhere) {
             $where .= " AND {$statuswhere}";
         }
     } elseif (!$this->is_singular) {
         $where .= " AND ({$wpdb->posts}.post_status = 'publish'";
         // Add public states.
         $public_states = get_post_stati(array('public' => true));
         foreach ((array) $public_states as $state) {
             if ('publish' == $state) {
                 // Publish is hard-coded above.
                 continue;
             }
             $where .= " OR {$wpdb->posts}.post_status = '{$state}'";
         }
         if ($this->is_admin) {
             // Add protected states that should show in the admin all list.
             $admin_all_states = get_post_stati(array('protected' => true, 'show_in_admin_all_list' => true));
             foreach ((array) $admin_all_states as $state) {
                 $where .= " OR {$wpdb->posts}.post_status = '{$state}'";
             }
         }
         if (is_user_logged_in()) {
             // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
             $private_states = get_post_stati(array('private' => true));
             foreach ((array) $private_states as $state) {
                 $where .= current_user_can($read_private_cap) ? " OR {$wpdb->posts}.post_status = '{$state}'" : " OR {$wpdb->posts}.post_author = {$user_id} AND {$wpdb->posts}.post_status = '{$state}'";
             }
         }
         $where .= ')';
     }
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('post', $wpdb->posts, 'ID', $this);
         $join .= $clauses['join'];
         $where .= $clauses['where'];
     }
     // Apply filters on where and join prior to paging so that any
     // manipulations to them are reflected in the paging by day queries.
     if (!$q['suppress_filters']) {
         $where = apply_filters_ref_array('posts_where', array($where, &$this));
         $join = apply_filters_ref_array('posts_join', array($join, &$this));
     }
     // Paging
     if (empty($q['nopaging']) && !$this->is_singular) {
         $page = absint($q['paged']);
         if (!$page) {
             $page = 1;
         }
         if (empty($q['offset'])) {
             $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
         } else {
             // we're ignoring $page and using 'offset'
             $q['offset'] = absint($q['offset']);
             $pgstrt = $q['offset'] . ', ';
         }
         $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
     }
     // Comments feeds
     if ($this->is_comment_feed && ($this->is_archive || $this->is_search || !$this->is_singular)) {
         if ($this->is_archive || $this->is_search) {
             $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) {$join} ";
             $cwhere = "WHERE comment_approved = '1' {$where}";
             $cgroupby = "{$wpdb->comments}.comment_id";
         } else {
             // Other non singular e.g. front
             $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )";
             $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
             $cgroupby = '';
         }
         if (!$q['suppress_filters']) {
             $cjoin = apply_filters_ref_array('comment_feed_join', array($cjoin, &$this));
             $cwhere = apply_filters_ref_array('comment_feed_where', array($cwhere, &$this));
             $cgroupby = apply_filters_ref_array('comment_feed_groupby', array($cgroupby, &$this));
             $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this));
             $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this));
         }
         $cgroupby = !empty($cgroupby) ? 'GROUP BY ' . $cgroupby : '';
         $corderby = !empty($corderby) ? 'ORDER BY ' . $corderby : '';
         $this->comments = (array) $wpdb->get_results("SELECT {$distinct} {$wpdb->comments}.* FROM {$wpdb->comments} {$cjoin} {$cwhere} {$cgroupby} {$corderby} {$climits}");
         $this->comment_count = count($this->comments);
         $post_ids = array();
         foreach ($this->comments as $comment) {
             $post_ids[] = (int) $comment->comment_post_ID;
         }
         $post_ids = join(',', $post_ids);
         $join = '';
         if ($post_ids) {
             $where = "AND {$wpdb->posts}.ID IN ({$post_ids}) ";
         } else {
             $where = "AND 0";
         }
     }
     $pieces = array('where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits');
     // Apply post-paging filters on where and join. Only plugins that
     // manipulate paging queries should use these hooks.
     if (!$q['suppress_filters']) {
         $where = apply_filters_ref_array('posts_where_paged', array($where, &$this));
         $groupby = apply_filters_ref_array('posts_groupby', array($groupby, &$this));
         $join = apply_filters_ref_array('posts_join_paged', array($join, &$this));
         $orderby = apply_filters_ref_array('posts_orderby', array($orderby, &$this));
         $distinct = apply_filters_ref_array('posts_distinct', array($distinct, &$this));
         $limits = apply_filters_ref_array('post_limits', array($limits, &$this));
         $fields = apply_filters_ref_array('posts_fields', array($fields, &$this));
         // Filter all clauses at once, for convenience
         $clauses = (array) apply_filters_ref_array('posts_clauses', array(compact($pieces), &$this));
         foreach ($pieces as $piece) {
             ${$piece} = isset($clauses[$piece]) ? $clauses[$piece] : '';
         }
     }
     // Announce current selection parameters. For use by caching plugins.
     do_action('posts_selection', $where . $groupby . $orderby . $limits . $join);
     // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
     if (!$q['suppress_filters']) {
         $where = apply_filters_ref_array('posts_where_request', array($where, &$this));
         $groupby = apply_filters_ref_array('posts_groupby_request', array($groupby, &$this));
         $join = apply_filters_ref_array('posts_join_request', array($join, &$this));
         $orderby = apply_filters_ref_array('posts_orderby_request', array($orderby, &$this));
         $distinct = apply_filters_ref_array('posts_distinct_request', array($distinct, &$this));
         $fields = apply_filters_ref_array('posts_fields_request', array($fields, &$this));
         $limits = apply_filters_ref_array('post_limits_request', array($limits, &$this));
         // Filter all clauses at once, for convenience
         $clauses = (array) apply_filters_ref_array('posts_clauses_request', array(compact($pieces), &$this));
         foreach ($pieces as $piece) {
             ${$piece} = isset($clauses[$piece]) ? $clauses[$piece] : '';
         }
     }
     if (!empty($groupby)) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if (!empty($orderby)) {
         $orderby = 'ORDER BY ' . $orderby;
     }
     $found_rows = '';
     if (!$q['no_found_rows'] && !empty($limits)) {
         $found_rows = 'SQL_CALC_FOUND_ROWS';
     }
     $this->request = $old_request = "SELECT {$found_rows} {$distinct} {$fields} FROM {$wpdb->posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}";
     if (!$q['suppress_filters']) {
         $this->request = apply_filters_ref_array('posts_request', array($this->request, &$this));
     }
     if ('ids' == $q['fields']) {
         $this->posts = $wpdb->get_col($this->request);
         $this->post_count = count($this->posts);
         $this->set_found_posts($q, $limits);
         return $this->posts;
     }
     if ('id=>parent' == $q['fields']) {
         $this->posts = $wpdb->get_results($this->request);
         $this->post_count = count($this->posts);
         $this->set_found_posts($q, $limits);
         $r = array();
         foreach ($this->posts as $post) {
             $r[$post->ID] = $post->post_parent;
         }
         return $r;
     }
     $split_the_query = $old_request == $this->request && "{$wpdb->posts}.*" == $fields && !empty($limits) && $q['posts_per_page'] < 500;
     $split_the_query = apply_filters('split_the_query', $split_the_query, $this);
     if ($split_the_query) {
         // First get the IDs and then fill in the objects
         $this->request = "SELECT {$found_rows} {$distinct} {$wpdb->posts}.ID FROM {$wpdb->posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}";
         $this->request = apply_filters('posts_request_ids', $this->request, $this);
         $ids = $wpdb->get_col($this->request);
         if ($ids) {
             $this->posts = $ids;
             $this->set_found_posts($q, $limits);
             _prime_post_caches($ids, $q['update_post_term_cache'], $q['update_post_meta_cache']);
         } else {
             $this->posts = array();
         }
     } else {
         $this->posts = $wpdb->get_results($this->request);
         $this->set_found_posts($q, $limits);
     }
     // Convert to WP_Post objects
     if ($this->posts) {
         $this->posts = array_map('get_post', $this->posts);
     }
     // Raw results filter. Prior to status checks.
     if (!$q['suppress_filters']) {
         $this->posts = apply_filters_ref_array('posts_results', array($this->posts, &$this));
     }
     if (!empty($this->posts) && $this->is_comment_feed && $this->is_singular) {
         $cjoin = apply_filters_ref_array('comment_feed_join', array('', &$this));
         $cwhere = apply_filters_ref_array('comment_feed_where', array("WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this));
         $cgroupby = apply_filters_ref_array('comment_feed_groupby', array('', &$this));
         $cgroupby = !empty($cgroupby) ? 'GROUP BY ' . $cgroupby : '';
         $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this));
         $corderby = !empty($corderby) ? 'ORDER BY ' . $corderby : '';
         $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this));
         $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} {$cjoin} {$cwhere} {$cgroupby} {$corderby} {$climits}";
         $this->comments = $wpdb->get_results($comments_request);
         $this->comment_count = count($this->comments);
     }
     // Check post status to determine if post should be displayed.
     if (!empty($this->posts) && ($this->is_single || $this->is_page)) {
         $status = get_post_status($this->posts[0]);
         $post_status_obj = get_post_status_object($status);
         //$type = get_post_type($this->posts[0]);
         if (!$post_status_obj->public) {
             if (!is_user_logged_in()) {
                 // User must be logged in to view unpublished posts.
                 $this->posts = array();
             } else {
                 if ($post_status_obj->protected) {
                     // User must have edit permissions on the draft to preview.
                     if (!current_user_can($edit_cap, $this->posts[0]->ID)) {
                         $this->posts = array();
                     } else {
                         $this->is_preview = true;
                         if ('future' != $status) {
                             $this->posts[0]->post_date = current_time('mysql');
                         }
                     }
                 } elseif ($post_status_obj->private) {
                     if (!current_user_can($read_cap, $this->posts[0]->ID)) {
                         $this->posts = array();
                     }
                 } else {
                     $this->posts = array();
                 }
             }
         }
         if ($this->is_preview && $this->posts && current_user_can($edit_cap, $this->posts[0]->ID)) {
             $this->posts[0] = get_post(apply_filters_ref_array('the_preview', array($this->posts[0], &$this)));
         }
     }
     // Put sticky posts at the top of the posts array
     $sticky_posts = get_option('sticky_posts');
     if ($this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts']) {
         $num_posts = count($this->posts);
         $sticky_offset = 0;
         // Loop over posts and relocate stickies to the front.
         for ($i = 0; $i < $num_posts; $i++) {
             if (in_array($this->posts[$i]->ID, $sticky_posts)) {
                 $sticky_post = $this->posts[$i];
                 // Remove sticky from current position
                 array_splice($this->posts, $i, 1);
                 // Move to front, after other stickies
                 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
                 // Increment the sticky offset. The next sticky will be placed at this offset.
                 $sticky_offset++;
                 // Remove post from sticky posts array
                 $offset = array_search($sticky_post->ID, $sticky_posts);
                 unset($sticky_posts[$offset]);
             }
         }
         // If any posts have been excluded specifically, Ignore those that are sticky.
         if (!empty($sticky_posts) && !empty($q['post__not_in'])) {
             $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
         }
         // Fetch sticky posts that weren't in the query results
         if (!empty($sticky_posts)) {
             $stickies = get_posts(array('post__in' => $sticky_posts, 'post_type' => $post_type, 'post_status' => 'publish', 'nopaging' => true));
             foreach ($stickies as $sticky_post) {
                 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
                 $sticky_offset++;
             }
         }
     }
     if (!$q['suppress_filters']) {
         $this->posts = apply_filters_ref_array('the_posts', array($this->posts, &$this));
     }
     // Ensure that any posts added/modified via one of the filters above are
     // of the type WP_Post and are filtered.
     if ($this->posts) {
         $this->post_count = count($this->posts);
         $this->posts = array_map('get_post', $this->posts);
         if ($q['cache_results']) {
             update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
         }
         $this->post = reset($this->posts);
     } else {
         $this->post_count = 0;
         $this->posts = array();
     }
     return $this->posts;
 }
 /**
  * Inject the date_query SQL in SearchWP's WHERE
  *
  * @since 2.6
  *
  * @param $sql
  * @param $engine
  *
  * @return string
  */
 function date_where($sql, $engine)
 {
     if ($engine != $this->engine || empty($this->date_query) || !is_array($this->date_query)) {
         return $sql;
     }
     $date_query = new WP_Date_Query((array) $this->date_query);
     $dq_sql = $date_query->get_sql();
     return $sql . $dq_sql;
 }
Ejemplo n.º 12
0
 /**
  * Prepare the query variables.
  *
  * @since 3.1.0
  * @access public
  *
  * @param string|array $query {
  *     Optional. Array or string of Query parameters.
  *
  *     @type int          $blog_id         The site ID. Default is the global blog id.
  *     @type string       $role            Role name. Default empty.
  *     @type string       $meta_key        User meta key. Default empty.
  *     @type string       $meta_value      User meta value. Default empty.
  *     @type string       $meta_compare    Comparison operator to test the `$meta_value`. Accepts '=', '!=',
  *                                         '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN',
  *                                         'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP',
  *                                         or 'RLIKE'. Default '='.
  *     @type array        $include         An array of user IDs to include. Default empty array.
  *     @type array        $exclude         An array of user IDs to exclude. Default empty array.
  *     @type string       $search          Search keyword. Searches for possible string matches on columns.
  *                                         When `$search_columns` is left empty, it tries to determine which
  *                                         column to search in based on search string. Default empty.
  *     @type array        $search_columns  Array of column names to be searched. Accepts 'ID', 'login',
  *                                         'nicename', 'email', 'url'. Default empty array.
  *     @type string       $orderby         Field to sort the retrieved users by. Accepts 'ID', 'display_name',
  *                                         'login', 'nicename', 'email', 'url', 'registered', 'post_count', or
  *                                         'meta_value'. To use 'meta_value', `$meta_key` must be also be defined.
  *                                         Default 'user_login'.
  *     @type string       $order           Designates ascending or descending order of users. Accepts 'ASC',
  *                                         'DESC'. Default 'ASC'.
  *     @type int          $offset          Number of users to offset in retrieved results. Can be used in
  *                                         conjunction with pagination. Default 0.
  *     @type int          $number          Number of users to limit the query for. Can be used in conjunction
  *                                         with pagination. Value -1 (all) is not supported.
  *                                         Default empty (all users).
  *     @type bool         $count_total     Whether to count the total number of users found. If pagination is not
  *                                         needed, setting this to false can improve performance. Default true.
  *     @type string|array $fields          Which fields to return. Single or all fields (string), or array
  *                                         of fields. Accepts 'ID', 'display_name', 'login', 'nicename', 'email',
  *                                         'url', 'registered'. Use 'all' for all fields and 'all_with_meta' to
  *                                         include meta fields. Default 'all'.
  *     @type string       $who             Type of users to query. Accepts 'authors'. Default empty (all users).
  * }
  */
 public function prepare_query($query = array())
 {
     global $wpdb;
     if (empty($this->query_vars) || !empty($query)) {
         $this->query_limit = null;
         $this->query_vars = wp_parse_args($query, array('blog_id' => $GLOBALS['blog_id'], 'role' => '', 'meta_key' => '', 'meta_value' => '', 'meta_compare' => '', 'include' => array(), 'exclude' => array(), 'search' => '', 'search_columns' => array(), 'orderby' => 'login', 'order' => 'ASC', 'offset' => '', 'number' => '', 'count_total' => true, 'fields' => 'all', 'who' => ''));
     }
     /**
      * Fires before the WP_User_Query has been parsed.
      *
      * The passed WP_User_Query object contains the query variables, not
      * yet passed into SQL.
      *
      * @since 4.0.0
      *
      * @param WP_User_Query $this The current WP_User_Query instance,
      *                            passed by reference.
      */
     do_action('pre_get_users', $this);
     $qv =& $this->query_vars;
     if (is_array($qv['fields'])) {
         $qv['fields'] = array_unique($qv['fields']);
         $this->query_fields = array();
         foreach ($qv['fields'] as $field) {
             $field = 'ID' === $field ? 'ID' : sanitize_key($field);
             $this->query_fields[] = "{$wpdb->users}.{$field}";
         }
         $this->query_fields = implode(',', $this->query_fields);
     } elseif ('all' == $qv['fields']) {
         $this->query_fields = "{$wpdb->users}.*";
     } else {
         $this->query_fields = "{$wpdb->users}.ID";
     }
     if (isset($qv['count_total']) && $qv['count_total']) {
         $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
     }
     $this->query_from = "FROM {$wpdb->users}";
     $this->query_where = "WHERE 1=1";
     // Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
     if (!empty($qv['include'])) {
         $include = wp_parse_id_list($qv['include']);
     } else {
         $include = false;
     }
     // sorting
     if (isset($qv['orderby'])) {
         if (in_array($qv['orderby'], array('nicename', 'email', 'url', 'registered'))) {
             $orderby = 'user_' . $qv['orderby'];
         } elseif (in_array($qv['orderby'], array('user_nicename', 'user_email', 'user_url', 'user_registered'))) {
             $orderby = $qv['orderby'];
         } elseif ('name' == $qv['orderby'] || 'display_name' == $qv['orderby']) {
             $orderby = 'display_name';
         } elseif ('post_count' == $qv['orderby']) {
             // todo: avoid the JOIN
             $where = get_posts_by_author_sql('post');
             $this->query_from .= " LEFT OUTER JOIN (\n\t\t\t\t\tSELECT post_author, COUNT(*) as post_count\n\t\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t\t{$where}\n\t\t\t\t\tGROUP BY post_author\n\t\t\t\t) p ON ({$wpdb->users}.ID = p.post_author)\n\t\t\t\t";
             $orderby = 'post_count';
         } elseif ('ID' == $qv['orderby'] || 'id' == $qv['orderby']) {
             $orderby = 'ID';
         } elseif ('meta_value' == $qv['orderby']) {
             $orderby = "{$wpdb->usermeta}.meta_value";
         } else {
             if ('include' === $qv['orderby'] && !empty($include)) {
                 // Sanitized earlier.
                 $include_sql = implode(',', $include);
                 $orderby = "FIELD( {$wpdb->users}.ID, {$include_sql} )";
             } else {
                 $orderby = 'user_login';
             }
         }
     }
     if (empty($orderby)) {
         $orderby = 'user_login';
     }
     $qv['order'] = isset($qv['order']) ? strtoupper($qv['order']) : '';
     if ('ASC' == $qv['order']) {
         $order = 'ASC';
     } else {
         $order = 'DESC';
     }
     $this->query_orderby = "ORDER BY {$orderby} {$order}";
     // limit
     if (isset($qv['number']) && $qv['number']) {
         if ($qv['offset']) {
             $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
         } else {
             $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']);
         }
     }
     $search = '';
     if (isset($qv['search'])) {
         $search = trim($qv['search']);
     }
     if ($search) {
         $leading_wild = ltrim($search, '*') != $search;
         $trailing_wild = rtrim($search, '*') != $search;
         if ($leading_wild && $trailing_wild) {
             $wild = 'both';
         } elseif ($leading_wild) {
             $wild = 'leading';
         } elseif ($trailing_wild) {
             $wild = 'trailing';
         } else {
             $wild = false;
         }
         if ($wild) {
             $search = trim($search, '*');
         }
         $search_columns = array();
         if ($qv['search_columns']) {
             $search_columns = array_intersect($qv['search_columns'], array('ID', 'user_login', 'user_email', 'user_url', 'user_nicename'));
         }
         if (!$search_columns) {
             if (false !== strpos($search, '@')) {
                 $search_columns = array('user_email');
             } elseif (is_numeric($search)) {
                 $search_columns = array('user_login', 'ID');
             } elseif (preg_match('|^https?://|', $search) && !(is_multisite() && wp_is_large_network('users'))) {
                 $search_columns = array('user_url');
             } else {
                 $search_columns = array('user_login', 'user_nicename');
             }
         }
         /**
          * Filter the columns to search in a WP_User_Query search.
          *
          * The default columns depend on the search term, and include 'user_email',
          * 'user_login', 'ID', 'user_url', and 'user_nicename'.
          *
          * @since 3.6.0
          *
          * @param array         $search_columns Array of column names to be searched.
          * @param string        $search         Text being searched.
          * @param WP_User_Query $this           The current WP_User_Query instance.
          */
         $search_columns = apply_filters('user_search_columns', $search_columns, $search, $this);
         $this->query_where .= $this->get_search_sql($search, $search_columns, $wild);
     }
     $blog_id = 0;
     if (isset($qv['blog_id'])) {
         $blog_id = absint($qv['blog_id']);
     }
     if (isset($qv['who']) && 'authors' == $qv['who'] && $blog_id) {
         $qv['meta_key'] = $wpdb->get_blog_prefix($blog_id) . 'user_level';
         $qv['meta_value'] = 0;
         $qv['meta_compare'] = '!=';
         $qv['blog_id'] = $blog_id = 0;
         // Prevent extra meta query
     }
     $meta_query = new WP_Meta_Query();
     $meta_query->parse_query_vars($qv);
     $role = '';
     if (isset($qv['role'])) {
         $role = trim($qv['role']);
     }
     if ($blog_id && ($role || is_multisite())) {
         $cap_meta_query = array();
         $cap_meta_query['key'] = $wpdb->get_blog_prefix($blog_id) . 'capabilities';
         if ($role) {
             $cap_meta_query['value'] = '"' . $role . '"';
             $cap_meta_query['compare'] = 'like';
         }
         if (empty($meta_query->queries)) {
             $meta_query->queries = array($cap_meta_query);
         } elseif (!in_array($cap_meta_query, $meta_query->queries, true)) {
             // Append the cap query to the original queries and reparse the query.
             $meta_query->queries = array('relation' => 'AND', array($meta_query->queries, $cap_meta_query));
         }
         $meta_query->parse_query_vars($meta_query->queries);
     }
     if (!empty($meta_query->queries)) {
         $clauses = $meta_query->get_sql('user', $wpdb->users, 'ID', $this);
         $this->query_from .= $clauses['join'];
         $this->query_where .= $clauses['where'];
         if ('OR' == $meta_query->relation) {
             $this->query_fields = 'DISTINCT ' . $this->query_fields;
         }
     }
     if (!empty($include)) {
         // Sanitized earlier.
         $ids = implode(',', $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})";
     }
     // Date queries are allowed for the user_registered field.
     if (!empty($qv['date_query']) && is_array($qv['date_query'])) {
         $date_query = new WP_Date_Query($qv['date_query'], 'user_registered');
         $this->query_where .= $date_query->get_sql();
     }
     /**
      * Fires after the WP_User_Query has been parsed, and before
      * the query is executed.
      *
      * The passed WP_User_Query object contains SQL parts formed
      * from parsing the given query.
      *
      * @since 3.1.0
      *
      * @param WP_User_Query $this The current WP_User_Query instance,
      *                            passed by reference.
      */
     do_action_ref_array('pre_user_query', array(&$this));
 }
Ejemplo n.º 13
0
 /**
  * Execute the query
  *
  * @since 3.1.0
  * @since 4.1.0 Introduced 'comment__in', 'comment__not_in',
  *              'post__in', and 'post__not_in' to $query_vars.
  *
  * @param string|array $query_vars
  * @return int|array
  */
 public function query($query_vars)
 {
     global $wpdb;
     $defaults = array('author_email' => '', 'fields' => '', 'ID' => '', 'comment__in' => '', 'comment__not_in' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_ID' => '', 'post_id' => 0, 'post__in' => '', 'post__not_in' => '', 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => '', 'type' => '', 'user_id' => '', 'search' => '', 'count' => false, 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', 'date_query' => null);
     $groupby = '';
     $this->query_vars = wp_parse_args($query_vars, $defaults);
     // Parse meta query
     $this->meta_query = new WP_Meta_Query();
     $this->meta_query->parse_query_vars($this->query_vars);
     /**
      * Fires before comments are retrieved.
      *
      * @since 3.1.0
      *
      * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
      */
     do_action_ref_array('pre_get_comments', array(&$this));
     // $args can be whatever, only use the args defined in defaults to compute the key
     $key = md5(serialize(wp_array_slice_assoc($this->query_vars, array_keys($defaults))));
     $last_changed = wp_cache_get('last_changed', 'comment');
     if (!$last_changed) {
         $last_changed = microtime();
         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;
     }
     $status = $this->query_vars['status'];
     if ('hold' == $status) {
         $approved = "comment_approved = '0'";
     } elseif ('approve' == $status) {
         $approved = "comment_approved = '1'";
     } elseif (!empty($status) && 'all' != $status) {
         $approved = $wpdb->prepare("comment_approved = %s", $status);
     } else {
         $approved = "( comment_approved = '0' OR comment_approved = '1' )";
     }
     $order = 'ASC' == strtoupper($this->query_vars['order']) ? 'ASC' : 'DESC';
     if (!empty($this->query_vars['orderby'])) {
         $ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']);
         $allowed_keys = 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');
         if (!empty($this->query_vars['meta_key'])) {
             $allowed_keys[] = $this->query_vars['meta_key'];
             $allowed_keys[] = 'meta_value';
             $allowed_keys[] = 'meta_value_num';
         }
         $ordersby = array_intersect($ordersby, $allowed_keys);
         foreach ($ordersby as $key => $value) {
             if ($value == $this->query_vars['meta_key'] || $value == 'meta_value') {
                 $ordersby[$key] = "{$wpdb->commentmeta}.meta_value";
             } elseif ($value == 'meta_value_num') {
                 $ordersby[$key] = "{$wpdb->commentmeta}.meta_value+0";
             }
         }
         $orderby = empty($ordersby) ? 'comment_date_gmt' : implode(', ', $ordersby);
     } else {
         $orderby = 'comment_date_gmt';
     }
     $number = absint($this->query_vars['number']);
     $offset = absint($this->query_vars['offset']);
     if (!empty($number)) {
         if ($offset) {
             $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
     } else {
         $limits = '';
     }
     if ($this->query_vars['count']) {
         $fields = 'COUNT(*)';
     } else {
         switch (strtolower($this->query_vars['fields'])) {
             case 'ids':
                 $fields = "{$wpdb->comments}.comment_ID";
                 break;
             default:
                 $fields = "*";
                 break;
         }
     }
     $join = '';
     $where = $approved;
     $post_id = absint($this->query_vars['post_id']);
     if (!empty($post_id)) {
         $where .= $wpdb->prepare(' AND comment_post_ID = %d', $post_id);
     }
     // Parse comment IDs for an IN clause.
     if (!empty($this->query_vars['comment__in'])) {
         $where .= ' AND comment_ID IN ( ' . implode(',', wp_parse_id_list($this->query_vars['comment__in'])) . ' )';
     }
     // Parse comment IDs for a NOT IN clause.
     if (!empty($this->query_vars['comment__not_in'])) {
         $where .= ' AND comment_ID NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['comment__not_in'])) . ' )';
     }
     // Parse comment post IDs for an IN clause.
     if (!empty($this->query_vars['post__in'])) {
         $where .= ' AND comment_post_ID IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__in'])) . ' )';
     }
     // Parse comment post IDs for a NOT IN clause.
     if (!empty($this->query_vars['post__not_in'])) {
         $where .= ' AND comment_post_ID NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['post__not_in'])) . ' )';
     }
     if ('' !== $this->query_vars['author_email']) {
         $where .= $wpdb->prepare(' AND comment_author_email = %s', $this->query_vars['author_email']);
     }
     if ('' !== $this->query_vars['karma']) {
         $where .= $wpdb->prepare(' AND comment_karma = %d', $this->query_vars['karma']);
     }
     if ('comment' == $this->query_vars['type']) {
         $where .= " AND comment_type = ''";
     } elseif ('pings' == $this->query_vars['type']) {
         $where .= ' AND comment_type IN ("pingback", "trackback")';
     } elseif (!empty($this->query_vars['type'])) {
         $where .= $wpdb->prepare(' AND comment_type = %s', $this->query_vars['type']);
     }
     if ('' !== $this->query_vars['parent']) {
         $where .= $wpdb->prepare(' AND comment_parent = %d', $this->query_vars['parent']);
     }
     if (is_array($this->query_vars['user_id'])) {
         $where .= ' AND user_id IN (' . implode(',', array_map('absint', $this->query_vars['user_id'])) . ')';
     } elseif ('' !== $this->query_vars['user_id']) {
         $where .= $wpdb->prepare(' AND user_id = %d', $this->query_vars['user_id']);
     }
     if ('' !== $this->query_vars['search']) {
         $where .= $this->get_search_sql($this->query_vars['search'], array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
     }
     $plucked = wp_array_slice_assoc($this->query_vars, array('post_author', 'post_name', 'post_parent', 'post_status', 'post_type'));
     $post_fields = array_filter($plucked);
     if (!empty($post_fields)) {
         $join = "JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID";
         foreach ($post_fields as $field_name => $field_value) {
             $where .= $wpdb->prepare(" AND {$wpdb->posts}.{$field_name} = %s", $field_value);
         }
     }
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('comment', $wpdb->comments, 'comment_ID', $this);
         $join .= $clauses['join'];
         $where .= $clauses['where'];
         $groupby = "{$wpdb->comments}.comment_ID";
     }
     $date_query = $this->query_vars['date_query'];
     if (!empty($date_query) && is_array($date_query)) {
         $date_query_object = new WP_Date_Query($date_query, 'comment_date');
         $where .= $date_query_object->get_sql();
     }
     $pieces = array('fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby');
     /**
      * Filter the comment query clauses.
      *
      * @since 3.1.0
      *
      * @param array            $pieces A compacted array of comment query clauses.
      * @param WP_Comment_Query &$this  Current instance of WP_Comment_Query, passed by reference.
      */
     $clauses = apply_filters_ref_array('comments_clauses', array(compact($pieces), &$this));
     $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
     $join = isset($clauses['join']) ? $clauses['join'] : '';
     $where = isset($clauses['where']) ? $clauses['where'] : '';
     $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
     $order = isset($clauses['order']) ? $clauses['order'] : '';
     $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
     if ($groupby) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     $query = "SELECT {$fields} FROM {$wpdb->comments} {$join} WHERE {$where} {$groupby} ORDER BY {$orderby} {$order} {$limits}";
     if ($this->query_vars['count']) {
         return $wpdb->get_var($query);
     }
     if ('ids' == $this->query_vars['fields']) {
         $this->comments = $wpdb->get_col($query);
         return array_map('intval', $this->comments);
     }
     $results = $wpdb->get_results($query);
     /**
      * Filter the comment query results.
      *
      * @since 3.1.0
      *
      * @param array            $results  An array of comments.
      * @param WP_Comment_Query &$this    Current instance of WP_Comment_Query, passed by reference.
      */
     $comments = apply_filters_ref_array('the_comments', array($results, &$this));
     wp_cache_add($cache_key, $comments, 'comment');
     return $comments;
 }
Ejemplo n.º 14
0
 /**
  * Retrieve the posts based on query variables.
  *
  * There are a few filters and actions that can be used to modify the post
  * database query.
  *
  * @since 1.5.0
  * @access public
  *
  * @return array List of posts.
  */
 public function get_posts()
 {
     $this->parse_query();
     /**
      * Fires after the query variable object is created, but before the actual query is run.
      *
      * Note: If using conditional tags, use the method versions within the passed instance
      * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
      * like is_main_query() test against the global $wp_query instance, not the passed one.
      *
      * @since 2.0.0
      *
      * @param WP_Query &$this The WP_Query instance (passed by reference).
      */
     do_action_ref_array('pre_get_posts', array(&$this));
     // Shorthand.
     $q =& $this->query_vars;
     // Fill again in case pre_get_posts unset some vars.
     $q = $this->fill_query_vars($q);
     // Parse meta query
     $this->meta_query = new WP_Meta_Query();
     $this->meta_query->parse_query_vars($q);
     // Set a flag if a pre_get_posts hook changed the query vars.
     $hash = md5(serialize($this->query_vars));
     if ($hash != $this->query_vars_hash) {
         $this->query_vars_changed = true;
         $this->query_vars_hash = $hash;
     }
     unset($hash);
     // First let's clear some variables
     $distinct = '';
     $whichauthor = '';
     $whichmimetype = '';
     $where = '';
     $limits = '';
     $join = '';
     $search = '';
     $groupby = '';
     $post_status_join = false;
     $page = 1;
     if (isset($q['caller_get_posts'])) {
         _deprecated_argument('WP_Query', '3.1.0', __('"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.'));
         if (!isset($q['ignore_sticky_posts'])) {
             $q['ignore_sticky_posts'] = $q['caller_get_posts'];
         }
     }
     if (!isset($q['ignore_sticky_posts'])) {
         $q['ignore_sticky_posts'] = false;
     }
     if (!isset($q['suppress_filters'])) {
         $q['suppress_filters'] = false;
     }
     if (!isset($q['cache_results'])) {
         if (wp_using_ext_object_cache()) {
             $q['cache_results'] = false;
         } else {
             $q['cache_results'] = true;
         }
     }
     if (!isset($q['update_post_term_cache'])) {
         $q['update_post_term_cache'] = true;
     }
     if (!isset($q['lazy_load_term_meta'])) {
         $q['lazy_load_term_meta'] = $q['update_post_term_cache'];
     }
     if (!isset($q['update_post_meta_cache'])) {
         $q['update_post_meta_cache'] = true;
     }
     if (!isset($q['post_type'])) {
         if ($this->is_search) {
             $q['post_type'] = 'any';
         } else {
             $q['post_type'] = '';
         }
     }
     $post_type = $q['post_type'];
     if (empty($q['posts_per_page'])) {
         $q['posts_per_page'] = get_option('posts_per_page');
     }
     if (isset($q['showposts']) && $q['showposts']) {
         $q['showposts'] = (int) $q['showposts'];
         $q['posts_per_page'] = $q['showposts'];
     }
     if (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0 && ($this->is_archive || $this->is_search)) {
         $q['posts_per_page'] = $q['posts_per_archive_page'];
     }
     if (!isset($q['nopaging'])) {
         if ($q['posts_per_page'] == -1) {
             $q['nopaging'] = true;
         } else {
             $q['nopaging'] = false;
         }
     }
     if ($this->is_feed) {
         // This overrides posts_per_page.
         if (!empty($q['posts_per_rss'])) {
             $q['posts_per_page'] = $q['posts_per_rss'];
         } else {
             $q['posts_per_page'] = get_option('posts_per_rss');
         }
         $q['nopaging'] = false;
     }
     $q['posts_per_page'] = (int) $q['posts_per_page'];
     if ($q['posts_per_page'] < -1) {
         $q['posts_per_page'] = abs($q['posts_per_page']);
     } elseif ($q['posts_per_page'] == 0) {
         $q['posts_per_page'] = 1;
     }
     if (!isset($q['comments_per_page']) || $q['comments_per_page'] == 0) {
         $q['comments_per_page'] = get_option('comments_per_page');
     }
     if ($this->is_home && (empty($this->query) || $q['preview'] == 'true') && 'page' == get_option('show_on_front') && get_option('page_on_front')) {
         $this->is_page = true;
         $this->is_home = false;
         $q['page_id'] = get_option('page_on_front');
     }
     if (isset($q['page'])) {
         $q['page'] = trim($q['page'], '/');
         $q['page'] = absint($q['page']);
     }
     // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
     if (isset($q['no_found_rows'])) {
         $q['no_found_rows'] = (bool) $q['no_found_rows'];
     } else {
         $q['no_found_rows'] = false;
     }
     switch ($q['fields']) {
         case 'ids':
             $fields = "{$this->db->posts}.ID";
             break;
         case 'id=>parent':
             $fields = "{$this->db->posts}.ID, {$this->db->posts}.post_parent";
             break;
         default:
             $fields = "{$this->db->posts}.*";
     }
     if ('' !== $q['menu_order']) {
         $where .= " AND {$this->db->posts}.menu_order = " . $q['menu_order'];
     }
     // The "m" parameter is meant for months but accepts datetimes of varying specificity
     if ($q['m']) {
         $where .= " AND YEAR({$this->db->posts}.post_date)=" . substr($q['m'], 0, 4);
         if (strlen($q['m']) > 5) {
             $where .= " AND MONTH({$this->db->posts}.post_date)=" . substr($q['m'], 4, 2);
         }
         if (strlen($q['m']) > 7) {
             $where .= " AND DAYOFMONTH({$this->db->posts}.post_date)=" . substr($q['m'], 6, 2);
         }
         if (strlen($q['m']) > 9) {
             $where .= " AND HOUR({$this->db->posts}.post_date)=" . substr($q['m'], 8, 2);
         }
         if (strlen($q['m']) > 11) {
             $where .= " AND MINUTE({$this->db->posts}.post_date)=" . substr($q['m'], 10, 2);
         }
         if (strlen($q['m']) > 13) {
             $where .= " AND SECOND({$this->db->posts}.post_date)=" . substr($q['m'], 12, 2);
         }
     }
     // Handle the other individual date parameters
     $date_parameters = array();
     if ('' !== $q['hour']) {
         $date_parameters['hour'] = $q['hour'];
     }
     if ('' !== $q['minute']) {
         $date_parameters['minute'] = $q['minute'];
     }
     if ('' !== $q['second']) {
         $date_parameters['second'] = $q['second'];
     }
     if ($q['year']) {
         $date_parameters['year'] = $q['year'];
     }
     if ($q['monthnum']) {
         $date_parameters['monthnum'] = $q['monthnum'];
     }
     if ($q['w']) {
         $date_parameters['week'] = $q['w'];
     }
     if ($q['day']) {
         $date_parameters['day'] = $q['day'];
     }
     if ($date_parameters) {
         $date_query = new WP_Date_Query(array($date_parameters));
         $where .= $date_query->get_sql();
     }
     unset($date_parameters, $date_query);
     // Handle complex date queries
     if (!empty($q['date_query'])) {
         $this->date_query = new WP_Date_Query($q['date_query']);
         $where .= $this->date_query->get_sql();
     }
     // If we've got a post_type AND it's not "any" post_type.
     if (!empty($q['post_type']) && 'any' != $q['post_type']) {
         foreach ((array) $q['post_type'] as $_post_type) {
             $ptype_obj = get_post_type_object($_post_type);
             if (!$ptype_obj || !$ptype_obj->query_var || empty($q[$ptype_obj->query_var])) {
                 continue;
             }
             if (!$ptype_obj->hierarchical) {
                 // Non-hierarchical post types can directly use 'name'.
                 $q['name'] = $q[$ptype_obj->query_var];
             } else {
                 // Hierarchical post types will operate through 'pagename'.
                 $q['pagename'] = $q[$ptype_obj->query_var];
                 $q['name'] = '';
             }
             // Only one request for a slug is possible, this is why name & pagename are overwritten above.
             break;
         }
         //end foreach
         unset($ptype_obj);
     }
     if ('' !== $q['title']) {
         $where .= $this->db->prepare(" AND {$this->db->posts}.post_title = %s", stripslashes($q['title']));
     }
     // Parameters related to 'post_name'.
     if ('' != $q['name']) {
         $q['name'] = sanitize_title_for_query($q['name']);
         $where .= " AND {$this->db->posts}.post_name = '" . $q['name'] . "'";
     } elseif ('' != $q['pagename']) {
         if (isset($this->queried_object_id)) {
             $reqpage = $this->queried_object_id;
         } else {
             if ('page' != $q['post_type']) {
                 foreach ((array) $q['post_type'] as $_post_type) {
                     $ptype_obj = get_post_type_object($_post_type);
                     if (!$ptype_obj || !$ptype_obj->hierarchical) {
                         continue;
                     }
                     $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
                     if ($reqpage) {
                         break;
                     }
                 }
                 unset($ptype_obj);
             } else {
                 $reqpage = get_page_by_path($q['pagename']);
             }
             if (!empty($reqpage)) {
                 $reqpage = $reqpage->ID;
             } else {
                 $reqpage = 0;
             }
         }
         $page_for_posts = get_option('page_for_posts');
         if ('page' != get_option('show_on_front') || empty($page_for_posts) || $reqpage != $page_for_posts) {
             $q['pagename'] = sanitize_title_for_query(wp_basename($q['pagename']));
             $q['name'] = $q['pagename'];
             $where .= " AND ({$this->db->posts}.ID = '{$reqpage}')";
             $reqpage_obj = get_post($reqpage);
             if (is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type) {
                 $this->is_attachment = true;
                 $post_type = $q['post_type'] = 'attachment';
                 $this->is_page = true;
                 $q['attachment_id'] = $reqpage;
             }
         }
     } elseif ('' != $q['attachment']) {
         $q['attachment'] = sanitize_title_for_query(wp_basename($q['attachment']));
         $q['name'] = $q['attachment'];
         $where .= " AND {$this->db->posts}.post_name = '" . $q['attachment'] . "'";
     } elseif (is_array($q['post_name__in']) && !empty($q['post_name__in'])) {
         $q['post_name__in'] = array_map('sanitize_title_for_query', $q['post_name__in']);
         $post_name__in = "'" . implode("','", $q['post_name__in']) . "'";
         $where .= " AND {$this->db->posts}.post_name IN ({$post_name__in})";
     }
     // If an attachment is requested by number, let it supersede any post number.
     if ($q['attachment_id']) {
         $q['p'] = absint($q['attachment_id']);
     }
     // If a post number is specified, load that post
     if ($q['p']) {
         $where .= " AND {$this->db->posts}.ID = " . $q['p'];
     } elseif ($q['post__in']) {
         $post__in = implode(',', array_map('absint', $q['post__in']));
         $where .= " AND {$this->db->posts}.ID IN ({$post__in})";
     } elseif ($q['post__not_in']) {
         $post__not_in = implode(',', array_map('absint', $q['post__not_in']));
         $where .= " AND {$this->db->posts}.ID NOT IN ({$post__not_in})";
     }
     if (is_numeric($q['post_parent'])) {
         $where .= $this->db->prepare(" AND {$this->db->posts}.post_parent = %d ", $q['post_parent']);
     } elseif ($q['post_parent__in']) {
         $post_parent__in = implode(',', array_map('absint', $q['post_parent__in']));
         $where .= " AND {$this->db->posts}.post_parent IN ({$post_parent__in})";
     } elseif ($q['post_parent__not_in']) {
         $post_parent__not_in = implode(',', array_map('absint', $q['post_parent__not_in']));
         $where .= " AND {$this->db->posts}.post_parent NOT IN ({$post_parent__not_in})";
     }
     if ($q['page_id']) {
         if ('page' != get_option('show_on_front') || $q['page_id'] != get_option('page_for_posts')) {
             $q['p'] = $q['page_id'];
             $where = " AND {$this->db->posts}.ID = " . $q['page_id'];
         }
     }
     // If a search pattern is specified, load the posts that match.
     if (strlen($q['s'])) {
         $search = $this->parse_search($q);
     }
     if (!$q['suppress_filters']) {
         /**
          * Filters the search SQL that is used in the WHERE clause of WP_Query.
          *
          * @since 3.0.0
          *
          * @param string   $search Search SQL for WHERE clause.
          * @param WP_Query $this   The current WP_Query object.
          */
         $search = apply_filters_ref_array('posts_search', array($search, &$this));
     }
     // Taxonomies
     if (!$this->is_singular) {
         $this->parse_tax_query($q);
         $clauses = $this->tax_query->get_sql($this->db->posts, 'ID');
         $join .= $clauses['join'];
         $where .= $clauses['where'];
     }
     if ($this->is_tax) {
         if (empty($post_type)) {
             // Do a fully inclusive search for currently registered post types of queried taxonomies
             $post_type = array();
             $taxonomies = array_keys($this->tax_query->queried_terms);
             foreach (get_post_types(array('exclude_from_search' => false)) as $pt) {
                 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies($pt);
                 if (array_intersect($taxonomies, $object_taxonomies)) {
                     $post_type[] = $pt;
                 }
             }
             if (!$post_type) {
                 $post_type = 'any';
             } elseif (count($post_type) == 1) {
                 $post_type = $post_type[0];
             }
             $post_status_join = true;
         } elseif (in_array('attachment', (array) $post_type)) {
             $post_status_join = true;
         }
     }
     /*
      * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
      * 'category_name' vars are set for backward compatibility.
      */
     if (!empty($this->tax_query->queried_terms)) {
         /*
          * Set 'taxonomy', 'term', and 'term_id' to the
          * first taxonomy other than 'post_tag' or 'category'.
          */
         if (!isset($q['taxonomy'])) {
             foreach ($this->tax_query->queried_terms as $queried_taxonomy => $queried_items) {
                 if (empty($queried_items['terms'][0])) {
                     continue;
                 }
                 if (!in_array($queried_taxonomy, array('category', 'post_tag'))) {
                     $q['taxonomy'] = $queried_taxonomy;
                     if ('slug' === $queried_items['field']) {
                         $q['term'] = $queried_items['terms'][0];
                     } else {
                         $q['term_id'] = $queried_items['terms'][0];
                     }
                     // Take the first one we find.
                     break;
                 }
             }
         }
         // 'cat', 'category_name', 'tag_id'
         foreach ($this->tax_query->queried_terms as $queried_taxonomy => $queried_items) {
             if (empty($queried_items['terms'][0])) {
                 continue;
             }
             if ('category' === $queried_taxonomy) {
                 $the_cat = get_term_by($queried_items['field'], $queried_items['terms'][0], 'category');
                 if ($the_cat) {
                     $this->set('cat', $the_cat->term_id);
                     $this->set('category_name', $the_cat->slug);
                 }
                 unset($the_cat);
             }
             if ('post_tag' === $queried_taxonomy) {
                 $the_tag = get_term_by($queried_items['field'], $queried_items['terms'][0], 'post_tag');
                 if ($the_tag) {
                     $this->set('tag_id', $the_tag->term_id);
                 }
                 unset($the_tag);
             }
         }
     }
     if (!empty($this->tax_query->queries) || !empty($this->meta_query->queries)) {
         $groupby = "{$this->db->posts}.ID";
     }
     // Author/user stuff
     if (!empty($q['author']) && $q['author'] != '0') {
         $q['author'] = addslashes_gpc('' . urldecode($q['author']));
         $authors = array_unique(array_map('intval', preg_split('/[,\\s]+/', $q['author'])));
         foreach ($authors as $author) {
             $key = $author > 0 ? 'author__in' : 'author__not_in';
             $q[$key][] = abs($author);
         }
         $q['author'] = implode(',', $authors);
     }
     if (!empty($q['author__not_in'])) {
         $author__not_in = implode(',', array_map('absint', array_unique((array) $q['author__not_in'])));
         $where .= " AND {$this->db->posts}.post_author NOT IN ({$author__not_in}) ";
     } elseif (!empty($q['author__in'])) {
         $author__in = implode(',', array_map('absint', array_unique((array) $q['author__in'])));
         $where .= " AND {$this->db->posts}.post_author IN ({$author__in}) ";
     }
     // Author stuff for nice URLs
     if ('' != $q['author_name']) {
         if (strpos($q['author_name'], '/') !== false) {
             $q['author_name'] = explode('/', $q['author_name']);
             if ($q['author_name'][count($q['author_name']) - 1]) {
                 $q['author_name'] = $q['author_name'][count($q['author_name']) - 1];
                 // no trailing slash
             } else {
                 $q['author_name'] = $q['author_name'][count($q['author_name']) - 2];
                 // there was a trailing slash
             }
         }
         $q['author_name'] = sanitize_title_for_query($q['author_name']);
         $q['author'] = get_user_by('slug', $q['author_name']);
         if ($q['author']) {
             $q['author'] = $q['author']->ID;
         }
         $whichauthor .= " AND ({$this->db->posts}.post_author = " . absint($q['author']) . ')';
     }
     // MIME-Type stuff for attachment browsing
     if (isset($q['post_mime_type']) && '' != $q['post_mime_type']) {
         $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $this->db->posts);
     }
     $where .= $search . $whichauthor . $whichmimetype;
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('post', $this->db->posts, 'ID', $this);
         $join .= $clauses['join'];
         $where .= $clauses['where'];
     }
     $rand = isset($q['orderby']) && 'rand' === $q['orderby'];
     if (!isset($q['order'])) {
         $q['order'] = $rand ? '' : 'DESC';
     } else {
         $q['order'] = $rand ? '' : $this->parse_order($q['order']);
     }
     // Order by.
     if (empty($q['orderby'])) {
         /*
          * Boolean false or empty array blanks out ORDER BY,
          * while leaving the value unset or otherwise empty sets the default.
          */
         if (isset($q['orderby']) && (is_array($q['orderby']) || false === $q['orderby'])) {
             $orderby = '';
         } else {
             $orderby = "{$this->db->posts}.post_date " . $q['order'];
         }
     } elseif ('none' == $q['orderby']) {
         $orderby = '';
     } elseif ($q['orderby'] == 'post__in' && !empty($post__in)) {
         $orderby = "FIELD( {$this->db->posts}.ID, {$post__in} )";
     } elseif ($q['orderby'] == 'post_parent__in' && !empty($post_parent__in)) {
         $orderby = "FIELD( {$this->db->posts}.post_parent, {$post_parent__in} )";
     } elseif ($q['orderby'] == 'post_name__in' && !empty($post_name__in)) {
         $orderby = "FIELD( {$this->db->posts}.post_name, {$post_name__in} )";
     } else {
         $orderby_array = array();
         if (is_array($q['orderby'])) {
             foreach ($q['orderby'] as $_orderby => $order) {
                 $orderby = addslashes_gpc(urldecode($_orderby));
                 $parsed = $this->parse_orderby($orderby);
                 if (!$parsed) {
                     continue;
                 }
                 $orderby_array[] = $parsed . ' ' . $this->parse_order($order);
             }
             $orderby = implode(', ', $orderby_array);
         } else {
             $q['orderby'] = urldecode($q['orderby']);
             $q['orderby'] = addslashes_gpc($q['orderby']);
             foreach (explode(' ', $q['orderby']) as $i => $orderby) {
                 $parsed = $this->parse_orderby($orderby);
                 // Only allow certain values for safety.
                 if (!$parsed) {
                     continue;
                 }
                 $orderby_array[] = $parsed;
             }
             $orderby = implode(' ' . $q['order'] . ', ', $orderby_array);
             if (empty($orderby)) {
                 $orderby = "{$this->db->posts}.post_date " . $q['order'];
             } elseif (!empty($q['order'])) {
                 $orderby .= " {$q['order']}";
             }
         }
     }
     // Order search results by relevance only when another "orderby" is not specified in the query.
     if (!empty($q['s'])) {
         $search_orderby = '';
         if (!empty($q['search_orderby_title']) && (empty($q['orderby']) && !$this->is_feed) || isset($q['orderby']) && 'relevance' === $q['orderby']) {
             $search_orderby = $this->parse_search_order($q);
         }
         if (!$q['suppress_filters']) {
             /**
              * Filters the ORDER BY used when ordering search results.
              *
              * @since 3.7.0
              *
              * @param string   $search_orderby The ORDER BY clause.
              * @param WP_Query $this           The current WP_Query instance.
              */
             $search_orderby = apply_filters('posts_search_orderby', $search_orderby, $this);
         }
         if ($search_orderby) {
             $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
         }
     }
     if (is_array($post_type) && count($post_type) > 1) {
         $post_type_cap = 'multiple_post_type';
     } else {
         if (is_array($post_type)) {
             $post_type = reset($post_type);
         }
         $post_type_object = get_post_type_object($post_type);
         if (empty($post_type_object)) {
             $post_type_cap = $post_type;
         }
     }
     if (isset($q['post_password'])) {
         $where .= $this->db->prepare(" AND {$this->db->posts}.post_password = %s", $q['post_password']);
         if (empty($q['perm'])) {
             $q['perm'] = 'readable';
         }
     } elseif (isset($q['has_password'])) {
         $where .= sprintf(" AND {$this->db->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=');
     }
     if (!empty($q['comment_status'])) {
         $where .= $this->db->prepare(" AND {$this->db->posts}.comment_status = %s ", $q['comment_status']);
     }
     if (!empty($q['ping_status'])) {
         $where .= $this->db->prepare(" AND {$this->db->posts}.ping_status = %s ", $q['ping_status']);
     }
     if ('any' == $post_type) {
         $in_search_post_types = get_post_types(array('exclude_from_search' => false));
         if (empty($in_search_post_types)) {
             $where .= ' AND 1=0 ';
         } else {
             $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $in_search_post_types) . "')";
         }
     } elseif (!empty($post_type) && is_array($post_type)) {
         $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $post_type) . "')";
     } elseif (!empty($post_type)) {
         $where .= " AND {$this->db->posts}.post_type = '{$post_type}'";
         $post_type_object = get_post_type_object($post_type);
     } elseif ($this->is_attachment) {
         $where .= " AND {$this->db->posts}.post_type = 'attachment'";
         $post_type_object = get_post_type_object('attachment');
     } elseif ($this->is_page) {
         $where .= " AND {$this->db->posts}.post_type = 'page'";
         $post_type_object = get_post_type_object('page');
     } else {
         $where .= " AND {$this->db->posts}.post_type = 'post'";
         $post_type_object = get_post_type_object('post');
     }
     $edit_cap = 'edit_post';
     $read_cap = 'read_post';
     if (!empty($post_type_object)) {
         $edit_others_cap = $post_type_object->cap->edit_others_posts;
         $read_private_cap = $post_type_object->cap->read_private_posts;
     } else {
         $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
         $read_private_cap = 'read_private_' . $post_type_cap . 's';
     }
     $user_id = get_current_user_id();
     $q_status = array();
     if (!empty($q['post_status'])) {
         $statuswheres = array();
         $q_status = $q['post_status'];
         if (!is_array($q_status)) {
             $q_status = explode(',', $q_status);
         }
         $r_status = array();
         $p_status = array();
         $e_status = array();
         if (in_array('any', $q_status)) {
             foreach (get_post_stati(array('exclude_from_search' => true)) as $status) {
                 if (!in_array($status, $q_status)) {
                     $e_status[] = "{$this->db->posts}.post_status <> '{$status}'";
                 }
             }
         } else {
             foreach (get_post_stati() as $status) {
                 if (in_array($status, $q_status)) {
                     if ('private' == $status) {
                         $p_status[] = "{$this->db->posts}.post_status = '{$status}'";
                     } else {
                         $r_status[] = "{$this->db->posts}.post_status = '{$status}'";
                     }
                 }
             }
         }
         if (empty($q['perm']) || 'readable' != $q['perm']) {
             $r_status = array_merge($r_status, $p_status);
             unset($p_status);
         }
         if (!empty($e_status)) {
             $statuswheres[] = "(" . join(' AND ', $e_status) . ")";
         }
         if (!empty($r_status)) {
             if (!empty($q['perm']) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap)) {
                 $statuswheres[] = "({$this->db->posts}.post_author = {$user_id} " . "AND (" . join(' OR ', $r_status) . "))";
             } else {
                 $statuswheres[] = "(" . join(' OR ', $r_status) . ")";
             }
         }
         if (!empty($p_status)) {
             if (!empty($q['perm']) && 'readable' == $q['perm'] && !current_user_can($read_private_cap)) {
                 $statuswheres[] = "({$this->db->posts}.post_author = {$user_id} " . "AND (" . join(' OR ', $p_status) . "))";
             } else {
                 $statuswheres[] = "(" . join(' OR ', $p_status) . ")";
             }
         }
         if ($post_status_join) {
             $join .= " LEFT JOIN {$this->db->posts} AS p2 ON ({$this->db->posts}.post_parent = p2.ID) ";
             foreach ($statuswheres as $index => $statuswhere) {
                 $statuswheres[$index] = "({$statuswhere} OR ({$this->db->posts}.post_status = 'inherit' AND " . str_replace($this->db->posts, 'p2', $statuswhere) . "))";
             }
         }
         $where_status = implode(' OR ', $statuswheres);
         if (!empty($where_status)) {
             $where .= " AND ({$where_status})";
         }
     } elseif (!$this->is_singular) {
         $where .= " AND ({$this->db->posts}.post_status = 'publish'";
         // Add public states.
         $public_states = get_post_stati(array('public' => true));
         foreach ((array) $public_states as $state) {
             if ('publish' == $state) {
                 // Publish is hard-coded above.
                 continue;
             }
             $where .= " OR {$this->db->posts}.post_status = '{$state}'";
         }
         if ($this->is_admin) {
             // Add protected states that should show in the admin all list.
             $admin_all_states = get_post_stati(array('protected' => true, 'show_in_admin_all_list' => true));
             foreach ((array) $admin_all_states as $state) {
                 $where .= " OR {$this->db->posts}.post_status = '{$state}'";
             }
         }
         if (is_user_logged_in()) {
             // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
             $private_states = get_post_stati(array('private' => true));
             foreach ((array) $private_states as $state) {
                 $where .= current_user_can($read_private_cap) ? " OR {$this->db->posts}.post_status = '{$state}'" : " OR {$this->db->posts}.post_author = {$user_id} AND {$this->db->posts}.post_status = '{$state}'";
             }
         }
         $where .= ')';
     }
     /*
      * Apply filters on where and join prior to paging so that any
      * manipulations to them are reflected in the paging by day queries.
      */
     if (!$q['suppress_filters']) {
         /**
          * Filters the WHERE clause of the query.
          *
          * @since 1.5.0
          *
          * @param string   $where The WHERE clause of the query.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $where = apply_filters_ref_array('posts_where', array($where, &$this));
         /**
          * Filters the JOIN clause of the query.
          *
          * @since 1.5.0
          *
          * @param string   $where The JOIN clause of the query.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $join = apply_filters_ref_array('posts_join', array($join, &$this));
     }
     // Paging
     if (empty($q['nopaging']) && !$this->is_singular) {
         $page = absint($q['paged']);
         if (!$page) {
             $page = 1;
         }
         // If 'offset' is provided, it takes precedence over 'paged'.
         if (isset($q['offset']) && is_numeric($q['offset'])) {
             $q['offset'] = absint($q['offset']);
             $pgstrt = $q['offset'] . ', ';
         } else {
             $pgstrt = absint(($page - 1) * $q['posts_per_page']) . ', ';
         }
         $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
     }
     // Comments feeds
     if ($this->is_comment_feed && !$this->is_singular) {
         if ($this->is_archive || $this->is_search) {
             $cjoin = "JOIN {$this->db->posts} ON ({$this->db->comments}.comment_post_ID = {$this->db->posts}.ID) {$join} ";
             $cwhere = "WHERE comment_approved = '1' {$where}";
             $cgroupby = "{$this->db->comments}.comment_id";
         } else {
             // Other non singular e.g. front
             $cjoin = "JOIN {$this->db->posts} ON ( {$this->db->comments}.comment_post_ID = {$this->db->posts}.ID )";
             $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' && post_type = 'attachment' ) ) AND comment_approved = '1'";
             $cgroupby = '';
         }
         if (!$q['suppress_filters']) {
             /**
              * Filters the JOIN clause of the comments feed query before sending.
              *
              * @since 2.2.0
              *
              * @param string   $cjoin The JOIN clause of the query.
              * @param WP_Query &$this The WP_Query instance (passed by reference).
              */
             $cjoin = apply_filters_ref_array('comment_feed_join', array($cjoin, &$this));
             /**
              * Filters the WHERE clause of the comments feed query before sending.
              *
              * @since 2.2.0
              *
              * @param string   $cwhere The WHERE clause of the query.
              * @param WP_Query &$this  The WP_Query instance (passed by reference).
              */
             $cwhere = apply_filters_ref_array('comment_feed_where', array($cwhere, &$this));
             /**
              * Filters the GROUP BY clause of the comments feed query before sending.
              *
              * @since 2.2.0
              *
              * @param string   $cgroupby The GROUP BY clause of the query.
              * @param WP_Query &$this    The WP_Query instance (passed by reference).
              */
             $cgroupby = apply_filters_ref_array('comment_feed_groupby', array($cgroupby, &$this));
             /**
              * Filters the ORDER BY clause of the comments feed query before sending.
              *
              * @since 2.8.0
              *
              * @param string   $corderby The ORDER BY clause of the query.
              * @param WP_Query &$this    The WP_Query instance (passed by reference).
              */
             $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this));
             /**
              * Filters the LIMIT clause of the comments feed query before sending.
              *
              * @since 2.8.0
              *
              * @param string   $climits The JOIN clause of the query.
              * @param WP_Query &$this   The WP_Query instance (passed by reference).
              */
             $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this));
         }
         $cgroupby = !empty($cgroupby) ? 'GROUP BY ' . $cgroupby : '';
         $corderby = !empty($corderby) ? 'ORDER BY ' . $corderby : '';
         $comments = (array) $this->db->get_results("SELECT {$distinct} {$this->db->comments}.* FROM {$this->db->comments} {$cjoin} {$cwhere} {$cgroupby} {$corderby} {$climits}");
         // Convert to WP_Comment
         $this->comments = array_map('get_comment', $comments);
         $this->comment_count = count($this->comments);
         $post_ids = array();
         foreach ($this->comments as $comment) {
             $post_ids[] = (int) $comment->comment_post_ID;
         }
         $post_ids = join(',', $post_ids);
         $join = '';
         if ($post_ids) {
             $where = "AND {$this->db->posts}.ID IN ({$post_ids}) ";
         } else {
             $where = "AND 0";
         }
     }
     $pieces = array('where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits');
     /*
      * Apply post-paging filters on where and join. Only plugins that
      * manipulate paging queries should use these hooks.
      */
     if (!$q['suppress_filters']) {
         /**
          * Filters the WHERE clause of the query.
          *
          * Specifically for manipulating paging queries.
          *
          * @since 1.5.0
          *
          * @param string   $where The WHERE clause of the query.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $where = apply_filters_ref_array('posts_where_paged', array($where, &$this));
         /**
          * Filters the GROUP BY clause of the query.
          *
          * @since 2.0.0
          *
          * @param string   $groupby The GROUP BY clause of the query.
          * @param WP_Query &$this   The WP_Query instance (passed by reference).
          */
         $groupby = apply_filters_ref_array('posts_groupby', array($groupby, &$this));
         /**
          * Filters the JOIN clause of the query.
          *
          * Specifically for manipulating paging queries.
          *
          * @since 1.5.0
          *
          * @param string   $join  The JOIN clause of the query.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $join = apply_filters_ref_array('posts_join_paged', array($join, &$this));
         /**
          * Filters the ORDER BY clause of the query.
          *
          * @since 1.5.1
          *
          * @param string   $orderby The ORDER BY clause of the query.
          * @param WP_Query &$this   The WP_Query instance (passed by reference).
          */
         $orderby = apply_filters_ref_array('posts_orderby', array($orderby, &$this));
         /**
          * Filters the DISTINCT clause of the query.
          *
          * @since 2.1.0
          *
          * @param string   $distinct The DISTINCT clause of the query.
          * @param WP_Query &$this    The WP_Query instance (passed by reference).
          */
         $distinct = apply_filters_ref_array('posts_distinct', array($distinct, &$this));
         /**
          * Filters the LIMIT clause of the query.
          *
          * @since 2.1.0
          *
          * @param string   $limits The LIMIT clause of the query.
          * @param WP_Query &$this  The WP_Query instance (passed by reference).
          */
         $limits = apply_filters_ref_array('post_limits', array($limits, &$this));
         /**
          * Filters the SELECT clause of the query.
          *
          * @since 2.1.0
          *
          * @param string   $fields The SELECT clause of the query.
          * @param WP_Query &$this  The WP_Query instance (passed by reference).
          */
         $fields = apply_filters_ref_array('posts_fields', array($fields, &$this));
         /**
          * Filters all query clauses at once, for convenience.
          *
          * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
          * fields (SELECT), and LIMITS clauses.
          *
          * @since 3.1.0
          *
          * @param array    $clauses The list of clauses for the query.
          * @param WP_Query &$this   The WP_Query instance (passed by reference).
          */
         $clauses = (array) apply_filters_ref_array('posts_clauses', array(compact($pieces), &$this));
         $where = isset($clauses['where']) ? $clauses['where'] : '';
         $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
         $join = isset($clauses['join']) ? $clauses['join'] : '';
         $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
         $distinct = isset($clauses['distinct']) ? $clauses['distinct'] : '';
         $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
         $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     }
     /**
      * Fires to announce the query's current selection parameters.
      *
      * For use by caching plugins.
      *
      * @since 2.3.0
      *
      * @param string $selection The assembled selection query.
      */
     do_action('posts_selection', $where . $groupby . $orderby . $limits . $join);
     /*
      * Filters again for the benefit of caching plugins.
      * Regular plugins should use the hooks above.
      */
     if (!$q['suppress_filters']) {
         /**
          * Filters the WHERE clause of the query.
          *
          * For use by caching plugins.
          *
          * @since 2.5.0
          *
          * @param string   $where The WHERE clause of the query.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $where = apply_filters_ref_array('posts_where_request', array($where, &$this));
         /**
          * Filters the GROUP BY clause of the query.
          *
          * For use by caching plugins.
          *
          * @since 2.5.0
          *
          * @param string   $groupby The GROUP BY clause of the query.
          * @param WP_Query &$this   The WP_Query instance (passed by reference).
          */
         $groupby = apply_filters_ref_array('posts_groupby_request', array($groupby, &$this));
         /**
          * Filters the JOIN clause of the query.
          *
          * For use by caching plugins.
          *
          * @since 2.5.0
          *
          * @param string   $join  The JOIN clause of the query.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $join = apply_filters_ref_array('posts_join_request', array($join, &$this));
         /**
          * Filters the ORDER BY clause of the query.
          *
          * For use by caching plugins.
          *
          * @since 2.5.0
          *
          * @param string   $orderby The ORDER BY clause of the query.
          * @param WP_Query &$this   The WP_Query instance (passed by reference).
          */
         $orderby = apply_filters_ref_array('posts_orderby_request', array($orderby, &$this));
         /**
          * Filters the DISTINCT clause of the query.
          *
          * For use by caching plugins.
          *
          * @since 2.5.0
          *
          * @param string   $distinct The DISTINCT clause of the query.
          * @param WP_Query &$this    The WP_Query instance (passed by reference).
          */
         $distinct = apply_filters_ref_array('posts_distinct_request', array($distinct, &$this));
         /**
          * Filters the SELECT clause of the query.
          *
          * For use by caching plugins.
          *
          * @since 2.5.0
          *
          * @param string   $fields The SELECT clause of the query.
          * @param WP_Query &$this  The WP_Query instance (passed by reference).
          */
         $fields = apply_filters_ref_array('posts_fields_request', array($fields, &$this));
         /**
          * Filters the LIMIT clause of the query.
          *
          * For use by caching plugins.
          *
          * @since 2.5.0
          *
          * @param string   $limits The LIMIT clause of the query.
          * @param WP_Query &$this  The WP_Query instance (passed by reference).
          */
         $limits = apply_filters_ref_array('post_limits_request', array($limits, &$this));
         /**
          * Filters all query clauses at once, for convenience.
          *
          * For use by caching plugins.
          *
          * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
          * fields (SELECT), and LIMITS clauses.
          *
          * @since 3.1.0
          *
          * @param array    $pieces The pieces of the query.
          * @param WP_Query &$this  The WP_Query instance (passed by reference).
          */
         $clauses = (array) apply_filters_ref_array('posts_clauses_request', array(compact($pieces), &$this));
         $where = isset($clauses['where']) ? $clauses['where'] : '';
         $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
         $join = isset($clauses['join']) ? $clauses['join'] : '';
         $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
         $distinct = isset($clauses['distinct']) ? $clauses['distinct'] : '';
         $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
         $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     }
     if (!empty($groupby)) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if (!empty($orderby)) {
         $orderby = 'ORDER BY ' . $orderby;
     }
     $found_rows = '';
     if (!$q['no_found_rows'] && !empty($limits)) {
         $found_rows = 'SQL_CALC_FOUND_ROWS';
     }
     $this->request = $old_request = "SELECT {$found_rows} {$distinct} {$fields} FROM {$this->db->posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}";
     if (!$q['suppress_filters']) {
         /**
          * Filters the completed SQL query before sending.
          *
          * @since 2.0.0
          *
          * @param string   $request The complete SQL query.
          * @param WP_Query &$this   The WP_Query instance (passed by reference).
          */
         $this->request = apply_filters_ref_array('posts_request', array($this->request, &$this));
     }
     /**
      * Filters the posts array before the query takes place.
      *
      * Return a non-null value to bypass WordPress's default post queries.
      *
      * Filtering functions that require pagination information are encouraged to set
      * the `found_posts` and `max_num_pages` properties of the WP_Query object,
      * passed to the filter by reference. If WP_Query does not perform a database
      * query, it will not have enough information to generate these values itself.
      *
      * @since 4.6.0
      *
      * @param array|null $posts Return an array of post data to short-circuit WP's query,
      *                          or null to allow WP to run its normal queries.
      * @param WP_Query   $this  The WP_Query instance, passed by reference.
      */
     $this->posts = apply_filters_ref_array('posts_pre_query', array(null, &$this));
     if ('ids' == $q['fields']) {
         if (null === $this->posts) {
             $this->posts = $this->db->get_col($this->request);
         }
         $this->posts = array_map('intval', $this->posts);
         $this->post_count = count($this->posts);
         $this->set_found_posts($q, $limits);
         return $this->posts;
     }
     if ('id=>parent' == $q['fields']) {
         if (null === $this->posts) {
             $this->posts = $this->db->get_results($this->request);
         }
         $this->post_count = count($this->posts);
         $this->set_found_posts($q, $limits);
         $r = array();
         foreach ($this->posts as $key => $post) {
             $this->posts[$key]->ID = (int) $post->ID;
             $this->posts[$key]->post_parent = (int) $post->post_parent;
             $r[(int) $post->ID] = (int) $post->post_parent;
         }
         return $r;
     }
     if (null === $this->posts) {
         $split_the_query = $old_request == $this->request && "{$this->db->posts}.*" == $fields && !empty($limits) && $q['posts_per_page'] < 500;
         /**
          * Filters whether to split the query.
          *
          * Splitting the query will cause it to fetch just the IDs of the found posts
          * (and then individually fetch each post by ID), rather than fetching every
          * complete row at once. One massive result vs. many small results.
          *
          * @since 3.4.0
          *
          * @param bool     $split_the_query Whether or not to split the query.
          * @param WP_Query $this            The WP_Query instance.
          */
         $split_the_query = apply_filters('split_the_query', $split_the_query, $this);
         if ($split_the_query) {
             // First get the IDs and then fill in the objects
             $this->request = "SELECT {$found_rows} {$distinct} {$this->db->posts}.ID FROM {$this->db->posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}";
             /**
              * Filters the Post IDs SQL request before sending.
              *
              * @since 3.4.0
              *
              * @param string   $request The post ID request.
              * @param WP_Query $this    The WP_Query instance.
              */
             $this->request = apply_filters('posts_request_ids', $this->request, $this);
             $ids = $this->db->get_col($this->request);
             if ($ids) {
                 $this->posts = $ids;
                 $this->set_found_posts($q, $limits);
                 _prime_post_caches($ids, $q['update_post_term_cache'], $q['update_post_meta_cache']);
             } else {
                 $this->posts = array();
             }
         } else {
             $this->posts = $this->db->get_results($this->request);
             $this->set_found_posts($q, $limits);
         }
     }
     // Convert to WP_Post objects.
     if ($this->posts) {
         $this->posts = array_map('get_post', $this->posts);
     }
     if (!$q['suppress_filters']) {
         /**
          * Filters the raw post results array, prior to status checks.
          *
          * @since 2.3.0
          *
          * @param array    $posts The post results array.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $this->posts = apply_filters_ref_array('posts_results', array($this->posts, &$this));
     }
     if (!empty($this->posts) && $this->is_comment_feed && $this->is_singular) {
         /** This filter is documented in wp-includes/query.php */
         $cjoin = apply_filters_ref_array('comment_feed_join', array('', &$this));
         /** This filter is documented in wp-includes/query.php */
         $cwhere = apply_filters_ref_array('comment_feed_where', array("WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this));
         /** This filter is documented in wp-includes/query.php */
         $cgroupby = apply_filters_ref_array('comment_feed_groupby', array('', &$this));
         $cgroupby = !empty($cgroupby) ? 'GROUP BY ' . $cgroupby : '';
         /** This filter is documented in wp-includes/query.php */
         $corderby = apply_filters_ref_array('comment_feed_orderby', array('comment_date_gmt DESC', &$this));
         $corderby = !empty($corderby) ? 'ORDER BY ' . $corderby : '';
         /** This filter is documented in wp-includes/query.php */
         $climits = apply_filters_ref_array('comment_feed_limits', array('LIMIT ' . get_option('posts_per_rss'), &$this));
         $comments_request = "SELECT {$this->db->comments}.* FROM {$this->db->comments} {$cjoin} {$cwhere} {$cgroupby} {$corderby} {$climits}";
         $comments = $this->db->get_results($comments_request);
         // Convert to WP_Comment
         $this->comments = array_map('get_comment', $comments);
         $this->comment_count = count($this->comments);
     }
     // Check post status to determine if post should be displayed.
     if (!empty($this->posts) && ($this->is_single || $this->is_page)) {
         $status = get_post_status($this->posts[0]);
         if ('attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent) {
             $this->is_page = false;
             $this->is_single = true;
             $this->is_attachment = true;
         }
         $post_status_obj = get_post_status_object($status);
         // If the post_status was specifically requested, let it pass through.
         if (!$post_status_obj->public && !in_array($status, $q_status)) {
             if (!is_user_logged_in()) {
                 // User must be logged in to view unpublished posts.
                 $this->posts = array();
             } else {
                 if ($post_status_obj->protected) {
                     // User must have edit permissions on the draft to preview.
                     if (!current_user_can($edit_cap, $this->posts[0]->ID)) {
                         $this->posts = array();
                     } else {
                         $this->is_preview = true;
                         if ('future' != $status) {
                             $this->posts[0]->post_date = current_time('mysql');
                         }
                     }
                 } elseif ($post_status_obj->private) {
                     if (!current_user_can($read_cap, $this->posts[0]->ID)) {
                         $this->posts = array();
                     }
                 } else {
                     $this->posts = array();
                 }
             }
         }
         if ($this->is_preview && $this->posts && current_user_can($edit_cap, $this->posts[0]->ID)) {
             /**
              * Filters the single post for preview mode.
              *
              * @since 2.7.0
              *
              * @param WP_Post  $post_preview  The Post object.
              * @param WP_Query &$this         The WP_Query instance (passed by reference).
              */
             $this->posts[0] = get_post(apply_filters_ref_array('the_preview', array($this->posts[0], &$this)));
         }
     }
     // Put sticky posts at the top of the posts array
     $sticky_posts = get_option('sticky_posts');
     if ($this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts']) {
         $num_posts = count($this->posts);
         $sticky_offset = 0;
         // Loop over posts and relocate stickies to the front.
         for ($i = 0; $i < $num_posts; $i++) {
             if (in_array($this->posts[$i]->ID, $sticky_posts)) {
                 $sticky_post = $this->posts[$i];
                 // Remove sticky from current position
                 array_splice($this->posts, $i, 1);
                 // Move to front, after other stickies
                 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
                 // Increment the sticky offset. The next sticky will be placed at this offset.
                 $sticky_offset++;
                 // Remove post from sticky posts array
                 $offset = array_search($sticky_post->ID, $sticky_posts);
                 unset($sticky_posts[$offset]);
             }
         }
         // If any posts have been excluded specifically, Ignore those that are sticky.
         if (!empty($sticky_posts) && !empty($q['post__not_in'])) {
             $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
         }
         // Fetch sticky posts that weren't in the query results
         if (!empty($sticky_posts)) {
             $stickies = get_posts(array('post__in' => $sticky_posts, 'post_type' => $post_type, 'post_status' => 'publish', 'nopaging' => true));
             foreach ($stickies as $sticky_post) {
                 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
                 $sticky_offset++;
             }
         }
     }
     // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
     if (!empty($this->comments)) {
         wp_queue_comments_for_comment_meta_lazyload($this->comments);
     }
     if (!$q['suppress_filters']) {
         /**
          * Filters the array of retrieved posts after they've been fetched and
          * internally processed.
          *
          * @since 1.5.0
          *
          * @param array    $posts The array of retrieved posts.
          * @param WP_Query &$this The WP_Query instance (passed by reference).
          */
         $this->posts = apply_filters_ref_array('the_posts', array($this->posts, &$this));
     }
     // Ensure that any posts added/modified via one of the filters above are
     // of the type WP_Post and are filtered.
     if ($this->posts) {
         $this->post_count = count($this->posts);
         $this->posts = array_map('get_post', $this->posts);
         if ($q['cache_results']) {
             update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
         }
         $this->post = reset($this->posts);
     } else {
         $this->post_count = 0;
         $this->posts = array();
     }
     if ($q['lazy_load_term_meta']) {
         wp_queue_posts_for_term_meta_lazyload($this->posts);
     }
     return $this->posts;
 }
    public function prepare_items()
    {
        global $wpdb;
        $page = $this->get_pagenum();
        $offset = ($page - 1) * $this->per_page;
        $checkout_fields_sql = "\n\t\t\tSELECT id, unique_name FROM " . WPSC_TABLE_CHECKOUT_FORMS . " WHERE unique_name IN ('billingfirstname', 'billinglastname', 'billingemail') AND active='1' AND checkout_set='0'\n\t\t";
        $checkout_fields = $wpdb->get_results($checkout_fields_sql);
        $joins = array();
        $where = array('1 = 1');
        if (isset($_REQUEST['post'])) {
            $posts = array_map('absint', $_REQUEST['post']);
            $where[] = ' and (p.id IN (' . implode(', ', $posts) . '))';
        }
        $i = 1;
        $selects = array('p.id', 'p.totalprice AS amount', 'p.processed AS status', 'p.track_id', 'p.date');
        $selects[] = '
			(
				SELECT SUM(quantity) FROM ' . WPSC_TABLE_CART_CONTENTS . ' AS c
				WHERE c.purchaseid = p.id
			) AS item_count';
        $search_terms = empty($_REQUEST['s']) ? array() : explode(' ', $_REQUEST['s']);
        $search_sql = array();
        foreach ($checkout_fields as $field) {
            $table_as = 's' . $i;
            $select_as = str_replace('billing', '', $field->unique_name);
            $selects[] = $table_as . '.value AS ' . $select_as;
            $joins[] = $wpdb->prepare("LEFT OUTER JOIN " . WPSC_TABLE_SUBMITTED_FORM_DATA . " AS {$table_as} ON {$table_as}.log_id = p.id AND {$table_as}.form_id = %d", $field->id);
            // build search term queries for first name, last name, email
            foreach ($search_terms as $term) {
                if (version_compare($GLOBALS['wp_version'], '4.0', '>=')) {
                    $escaped_term = esc_sql(like_escape($term));
                } else {
                    $escaped_term = esc_sql($wpdb->esc_like($term));
                }
                if (!array_key_exists($term, $search_sql)) {
                    $search_sql[$term] = array();
                }
                $search_sql[$term][] = $table_as . ".value LIKE '%" . $escaped_term . "%'";
            }
            $i++;
        }
        // combine query phrases into a single query string
        foreach ($search_terms as $term) {
            $search_sql[$term][] = "p.track_id = '" . esc_sql($term) . "'";
            if (is_numeric($term)) {
                $search_sql[$term][] = 'p.id = ' . esc_sql($term);
            }
            $search_sql[$term] = '(' . implode(' OR ', $search_sql[$term]) . ')';
        }
        $search_sql = implode(' AND ', array_values($search_sql));
        if ($search_sql) {
            $where[] = " AND ({$search_sql})";
        }
        // filter by status
        if (!empty($_REQUEST['status']) && $_REQUEST['status'] != 'all') {
            $this->status = absint($_REQUEST['status']);
            $where[] = ' AND (processed = ' . $this->status . ')';
        }
        $this->where_no_filter = implode(' ', $where);
        // filter by month
        if (!empty($_REQUEST['m'])) {
            // so we can tell WP_Date_Query we're legit
            add_filter('date_query_valid_columns', array($this, 'set_date_column_to_date'));
            if (strlen($_REQUEST['m']) < 4) {
                $query_args = $this->assemble_predefined_periods_query($_REQUEST['m']);
            } else {
                $query_args = array('year' => (int) substr($_REQUEST['m'], 0, 4), 'monthnum' => (int) substr($_REQUEST['m'], -2));
            }
            $date_query = new WP_Date_Query($query_args, $column = '__date__');
            /* this is a subtle hack since the FROM_UNIXTIME doesn't survive WP_Date_Query
             * so we use __date__ as a proxy
             */
            $where[] = str_replace('__date__', 'FROM_UNIXTIME(p.date)', $date_query->get_sql());
        }
        $selects = apply_filters('wpsc_manage_purchase_logs_selects', implode(', ', $selects));
        $this->joins = apply_filters('wpsc_manage_purchase_logs_joins', implode(' ', $joins));
        $this->where = apply_filters('wpsc_manage_purchase_logs_where', implode(' ', $where));
        $limit = $this->per_page !== 0 ? "LIMIT {$offset}, {$this->per_page}" : '';
        $orderby = empty($_REQUEST['orderby']) ? 'p.id' : 'p.' . $_REQUEST['orderby'];
        $order = empty($_REQUEST['order']) ? 'DESC' : $_REQUEST['order'];
        $orderby = esc_sql(apply_filters('wpsc_manage_purchase_logs_orderby', $orderby));
        $order = esc_sql($order);
        $purchase_log_sql = apply_filters('wpsc_manage_purchase_logs_sql', "\n\t\t\tSELECT SQL_CALC_FOUND_ROWS {$selects}\n\t\t\tFROM " . WPSC_TABLE_PURCHASE_LOGS . " AS p\n\t\t\t{$this->joins}\n\t\t\tWHERE {$this->where}\n\t\t\tORDER BY {$orderby} {$order}\n\t\t\t{$limit}\n\t\t");
        $this->items = apply_filters('wpsc_manage_purchase_logs_items', $wpdb->get_results($purchase_log_sql));
        if ($this->per_page) {
            $total_items = $wpdb->get_var("SELECT FOUND_ROWS()");
            $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $this->per_page));
        }
        $total_where = apply_filters('wpsc_manage_purchase_logs_total_where', $this->where);
        if ($this->status == 'all') {
            $total_where .= ' AND p.processed IN (3, 4, 5) ';
        }
        $total_sql = "\n\t\t\tSELECT SUM(totalprice)\n\t\t\tFROM " . WPSC_TABLE_PURCHASE_LOGS . " AS p\n\t\t\t{$this->joins}\n\t\t\tWHERE {$total_where}\n\t\t";
        $this->total_amount = $wpdb->get_var($total_sql);
    }
Ejemplo n.º 16
0
 /**
  * Prepare the query variables.
  *
  * @since 3.1.0
  * @since 4.1.0 Added the ability to order by the `include` value.
  * @since 4.2.0 Added 'meta_value_num' support for `$orderby` parameter. Added multi-dimensional array syntax
  *              for `$orderby` parameter.
  * @since 4.3.0 Added 'has_published_posts' parameter.
  * @since 4.4.0 Added 'paged', 'role__in', and 'role__not_in' parameters. The 'role' parameter was updated to
  *              permit an array or comma-separated list of values. The 'number' parameter was updated to support
  *              querying for all users with using -1.
  *
  * @access public
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  * @global int  $blog_id
  *
  * @param string|array $query {
  *     Optional. Array or string of Query parameters.
  *
  *     @type int          $blog_id             The site ID. Default is the global blog id.
  *     @type string|array $role                An array or a comma-separated list of role names that users must match
  *                                             to be included in results. Note that this is an inclusive list: users
  *                                             must match *each* role. Default empty.
  *     @type array        $role__in            An array of role names. Matched users must have at least one of these
  *                                             roles. Default empty array.
  *     @type array        $role__not_in        An array of role names to exclude. Users matching one or more of these
  *                                             roles will not be included in results. Default empty array.
  *     @type string       $meta_key            User meta key. Default empty.
  *     @type string       $meta_value          User meta value. Default empty.
  *     @type string       $meta_compare        Comparison operator to test the `$meta_value`. Accepts '=', '!=',
  *                                             '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
  *                                             'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP',
  *                                             'NOT REGEXP', or 'RLIKE'. Default '='.
  *     @type array        $include             An array of user IDs to include. Default empty array.
  *     @type array        $exclude             An array of user IDs to exclude. Default empty array.
  *     @type string       $search              Search keyword. Searches for possible string matches on columns.
  *                                             When `$search_columns` is left empty, it tries to determine which
  *                                             column to search in based on search string. Default empty.
  *     @type array        $search_columns      Array of column names to be searched. Accepts 'ID', 'login',
  *                                             'nicename', 'email', 'url'. Default empty array.
  *     @type string|array $orderby             Field(s) to sort the retrieved users by. May be a single value,
  *                                             an array of values, or a multi-dimensional array with fields as
  *                                             keys and orders ('ASC' or 'DESC') as values. Accepted values are
  *                                             'ID', 'display_name' (or 'name'), 'include', 'user_login'
  *                                             (or 'login'), 'user_nicename' (or 'nicename'), 'user_email'
  *                                             (or 'email'), 'user_url' (or 'url'), 'user_registered'
  *                                             or 'registered'), 'post_count', 'meta_value', 'meta_value_num',
  *                                             the value of `$meta_key`, or an array key of `$meta_query`. To use
  *                                             'meta_value' or 'meta_value_num', `$meta_key` must be also be
  *                                             defined. Default 'user_login'.
  *     @type string       $order               Designates ascending or descending order of users. Order values
  *                                             passed as part of an `$orderby` array take precedence over this
  *                                             parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
  *     @type int          $offset              Number of users to offset in retrieved results. Can be used in
  *                                             conjunction with pagination. Default 0.
  *     @type int          $number              Number of users to limit the query for. Can be used in
  *                                             conjunction with pagination. Value -1 (all) is supported, but
  *                                             should be used with caution on larger sites.
  *                                             Default empty (all users).
  *     @type int          $paged               When used with number, defines the page of results to return.
  *                                             Default 1.
  *     @type bool         $count_total         Whether to count the total number of users found. If pagination
  *                                             is not needed, setting this to false can improve performance.
  *                                             Default true.
  *     @type string|array $fields              Which fields to return. Single or all fields (string), or array
  *                                             of fields. Accepts 'ID', 'display_name', 'user_login',
  *                                             'user_nicename', 'user_email', 'user_url', 'user_registered'.
  *                                             Use 'all' for all fields and 'all_with_meta' to include
  *                                             meta fields. Default 'all'.
  *     @type string       $who                 Type of users to query. Accepts 'authors'.
  *                                             Default empty (all users).
  *     @type bool|array   $has_published_posts Pass an array of post types to filter results to users who have
  *                                             published posts in those post types. `true` is an alias for all
  *                                             public post types.
  * }
  */
 public function prepare_query($query = array())
 {
     global $wpdb;
     if (empty($this->query_vars) || !empty($query)) {
         $this->query_limit = null;
         $this->query_vars = $this->fill_query_vars($query);
     }
     /**
      * Fires before the WP_User_Query has been parsed.
      *
      * The passed WP_User_Query object contains the query variables, not
      * yet passed into SQL.
      *
      * @since 4.0.0
      *
      * @param WP_User_Query $this The current WP_User_Query instance,
      *                            passed by reference.
      */
     do_action('pre_get_users', $this);
     // Ensure that query vars are filled after 'pre_get_users'.
     $qv =& $this->query_vars;
     $qv = $this->fill_query_vars($qv);
     if (is_array($qv['fields'])) {
         $qv['fields'] = array_unique($qv['fields']);
         $this->query_fields = array();
         foreach ($qv['fields'] as $field) {
             $field = 'ID' === $field ? 'ID' : sanitize_key($field);
             $this->query_fields[] = "{$wpdb->users}.{$field}";
         }
         $this->query_fields = implode(',', $this->query_fields);
     } elseif ('all' == $qv['fields']) {
         $this->query_fields = "{$wpdb->users}.*";
     } else {
         $this->query_fields = "{$wpdb->users}.ID";
     }
     if (isset($qv['count_total']) && $qv['count_total']) {
         $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
     }
     $this->query_from = "FROM {$wpdb->users}";
     $this->query_where = "WHERE 1=1";
     // Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
     if (!empty($qv['include'])) {
         $include = wp_parse_id_list($qv['include']);
     } else {
         $include = false;
     }
     $blog_id = 0;
     if (isset($qv['blog_id'])) {
         $blog_id = absint($qv['blog_id']);
     }
     if (isset($qv['who']) && 'authors' == $qv['who'] && $blog_id) {
         $qv['meta_key'] = $wpdb->get_blog_prefix($blog_id) . 'user_level';
         $qv['meta_value'] = 0;
         $qv['meta_compare'] = '!=';
         $qv['blog_id'] = $blog_id = 0;
         // Prevent extra meta query
     }
     if ($qv['has_published_posts'] && $blog_id) {
         if (true === $qv['has_published_posts']) {
             $post_types = get_post_types(array('public' => true));
         } else {
             $post_types = (array) $qv['has_published_posts'];
         }
         foreach ($post_types as &$post_type) {
             $post_type = $wpdb->prepare('%s', $post_type);
         }
         $posts_table = $wpdb->get_blog_prefix($blog_id) . 'posts';
         $this->query_where .= " AND {$wpdb->users}.ID IN ( SELECT DISTINCT {$posts_table}.post_author FROM {$posts_table} WHERE {$posts_table}.post_status = 'publish' AND {$posts_table}.post_type IN ( " . join(", ", $post_types) . " ) )";
     }
     // Meta query.
     $this->meta_query = new WP_Meta_Query();
     $this->meta_query->parse_query_vars($qv);
     $roles = array();
     if (isset($qv['role'])) {
         if (is_array($qv['role'])) {
             $roles = $qv['role'];
         } elseif (is_string($qv['role']) && !empty($qv['role'])) {
             $roles = array_map('trim', explode(',', $qv['role']));
         }
     }
     $role__in = array();
     if (isset($qv['role__in'])) {
         $role__in = (array) $qv['role__in'];
     }
     $role__not_in = array();
     if (isset($qv['role__not_in'])) {
         $role__not_in = (array) $qv['role__not_in'];
     }
     if ($blog_id && (!empty($roles) || !empty($role__in) || !empty($role__not_in) || is_multisite())) {
         $role_queries = array();
         $roles_clauses = array('relation' => 'AND');
         if (!empty($roles)) {
             foreach ($roles as $role) {
                 $roles_clauses[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'LIKE');
             }
             $role_queries[] = $roles_clauses;
         }
         $role__in_clauses = array('relation' => 'OR');
         if (!empty($role__in)) {
             foreach ($role__in as $role) {
                 $role__in_clauses[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'LIKE');
             }
             $role_queries[] = $role__in_clauses;
         }
         $role__not_in_clauses = array('relation' => 'AND');
         if (!empty($role__not_in)) {
             foreach ($role__not_in as $role) {
                 $role__not_in_clauses[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'NOT LIKE');
             }
             $role_queries[] = $role__not_in_clauses;
         }
         // If there are no specific roles named, make sure the user is a member of the site.
         if (empty($role_queries)) {
             $role_queries[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'compare' => 'EXISTS');
         }
         // Specify that role queries should be joined with AND.
         $role_queries['relation'] = 'AND';
         if (empty($this->meta_query->queries)) {
             $this->meta_query->queries = $role_queries;
         } else {
             // Append the cap query to the original queries and reparse the query.
             $this->meta_query->queries = array('relation' => 'AND', array($this->meta_query->queries, $role_queries));
         }
         $this->meta_query->parse_query_vars($this->meta_query->queries);
     }
     if (!empty($this->meta_query->queries)) {
         $clauses = $this->meta_query->get_sql('user', $wpdb->users, 'ID', $this);
         $this->query_from .= $clauses['join'];
         $this->query_where .= $clauses['where'];
         if ($this->meta_query->has_or_relation()) {
             $this->query_fields = 'DISTINCT ' . $this->query_fields;
         }
     }
     // sorting
     $qv['order'] = isset($qv['order']) ? strtoupper($qv['order']) : '';
     $order = $this->parse_order($qv['order']);
     if (empty($qv['orderby'])) {
         // Default order is by 'user_login'.
         $ordersby = array('user_login' => $order);
     } elseif (is_array($qv['orderby'])) {
         $ordersby = $qv['orderby'];
     } else {
         // 'orderby' values may be a comma- or space-separated list.
         $ordersby = preg_split('/[,\\s]+/', $qv['orderby']);
     }
     $orderby_array = array();
     foreach ($ordersby as $_key => $_value) {
         if (!$_value) {
             continue;
         }
         if (is_int($_key)) {
             // Integer key means this is a flat array of 'orderby' fields.
             $_orderby = $_value;
             $_order = $order;
         } else {
             // Non-integer key means this the key is the field and the value is ASC/DESC.
             $_orderby = $_key;
             $_order = $_value;
         }
         $parsed = $this->parse_orderby($_orderby);
         if (!$parsed) {
             continue;
         }
         $orderby_array[] = $parsed . ' ' . $this->parse_order($_order);
     }
     // If no valid clauses were found, order by user_login.
     if (empty($orderby_array)) {
         $orderby_array[] = "user_login {$order}";
     }
     $this->query_orderby = 'ORDER BY ' . implode(', ', $orderby_array);
     // limit
     if (isset($qv['number']) && $qv['number'] > 0) {
         if ($qv['offset']) {
             $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
         } else {
             $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['number'] * ($qv['paged'] - 1), $qv['number']);
         }
     }
     $search = '';
     if (isset($qv['search'])) {
         $search = trim($qv['search']);
     }
     if ($search) {
         $leading_wild = ltrim($search, '*') != $search;
         $trailing_wild = rtrim($search, '*') != $search;
         if ($leading_wild && $trailing_wild) {
             $wild = 'both';
         } elseif ($leading_wild) {
             $wild = 'leading';
         } elseif ($trailing_wild) {
             $wild = 'trailing';
         } else {
             $wild = false;
         }
         if ($wild) {
             $search = trim($search, '*');
         }
         $search_columns = array();
         if ($qv['search_columns']) {
             $search_columns = array_intersect($qv['search_columns'], array('ID', 'user_login', 'user_email', 'user_url', 'user_nicename'));
         }
         if (!$search_columns) {
             if (false !== strpos($search, '@')) {
                 $search_columns = array('user_email');
             } elseif (is_numeric($search)) {
                 $search_columns = array('user_login', 'ID');
             } elseif (preg_match('|^https?://|', $search) && !(is_multisite() && wp_is_large_network('users'))) {
                 $search_columns = array('user_url');
             } else {
                 $search_columns = array('user_login', 'user_url', 'user_email', 'user_nicename', 'display_name');
             }
         }
         /**
          * Filter the columns to search in a WP_User_Query search.
          *
          * The default columns depend on the search term, and include 'user_email',
          * 'user_login', 'ID', 'user_url', 'display_name', and 'user_nicename'.
          *
          * @since 3.6.0
          *
          * @param array         $search_columns Array of column names to be searched.
          * @param string        $search         Text being searched.
          * @param WP_User_Query $this           The current WP_User_Query instance.
          */
         $search_columns = apply_filters('user_search_columns', $search_columns, $search, $this);
         $this->query_where .= $this->get_search_sql($search, $search_columns, $wild);
     }
     if (!empty($include)) {
         // Sanitized earlier.
         $ids = implode(',', $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})";
     }
     // Date queries are allowed for the user_registered field.
     if (!empty($qv['date_query']) && is_array($qv['date_query'])) {
         $date_query = new WP_Date_Query($qv['date_query'], 'user_registered');
         $this->query_where .= $date_query->get_sql();
     }
     /**
      * Fires after the WP_User_Query has been parsed, and before
      * the query is executed.
      *
      * The passed WP_User_Query object contains SQL parts formed
      * from parsing the given query.
      *
      * @since 3.1.0
      *
      * @param WP_User_Query $this The current WP_User_Query instance,
      *                            passed by reference.
      */
     do_action_ref_array('pre_user_query', array(&$this));
 }
Ejemplo n.º 17
0
 public static function wp_get_archives($args = array())
 {
     global $wpdb, $wp_rewrite;
     $r = wp_parse_args($args, array('type' => 'monthly', 'post_type' => 'post', 'order' => 'DESC', 'date_query' => null));
     $r['post_type'] = sanitize_key($r['post_type']);
     if (!post_type_exists($r['post_type'])) {
         return array();
     }
     if ('' == $r['type']) {
         $r['type'] = 'monthly';
     }
     $order = strtoupper($r['order']);
     if ($order !== 'ASC') {
         $order = 'DESC';
     }
     $where = " WHERE {$wpdb->posts}.post_type = '" . $r['post_type'] . "' AND {$wpdb->posts}.post_status = 'publish'";
     if (!empty($r['date_query'])) {
         $date_query = new WP_Date_Query($r['date_query']);
         $where .= $date_query->get_sql();
     }
     $where = apply_filters('getarchives_where', $where, $args);
     $where = apply_filters('ry_getarchives_where', $where, $args);
     $join = '';
     $join = apply_filters('getarchives_join', $join, $args);
     $join = apply_filters('ry_getarchives_join', $join, $args);
     $last_changed = wp_cache_get('last_changed', 'posts');
     if (!$last_changed) {
         $last_changed = microtime();
         wp_cache_set('last_changed', $last_changed, 'posts');
     }
     $list = array();
     if ('monthly' == $r['type']) {
         $query = "SELECT YEAR({$wpdb->posts}.post_date) AS `year`, MONTH({$wpdb->posts}.post_date) AS `month` FROM {$wpdb->posts} {$join} {$where} GROUP BY YEAR({$wpdb->posts}.post_date), MONTH({$wpdb->posts}.post_date) ORDER BY {$wpdb->posts}.post_date {$order} {$limit}";
         $key = md5($query);
         $key = "RY_CP_wp_get_archives:{$key}:{$last_changed}";
         if (!($results = wp_cache_get($key, 'posts'))) {
             $results = $wpdb->get_results($query);
             wp_cache_set($key, $results, 'posts');
         }
         if ($results) {
             $permalink_type = $wp_rewrite->get_month_permastruct();
             $permalink_type = empty($permalink_type);
             foreach ((array) $results as $result) {
                 $url = get_month_link($result->year, $result->month);
                 if ($r['post_type'] != 'post') {
                     if ($permalink_type) {
                         $url .= '&post_type=' . $r['post_type'];
                     } else {
                         $url = str_replace('/date/', '/date/' . $r['post_type'] . '/', $url);
                     }
                 }
                 $list[] = array('year' => $result->year, 'month' => $result->month, 'day' => 0, 'url' => $url);
             }
         }
     } elseif ('yearly' == $r['type']) {
         $query = "SELECT YEAR({$wpdb->posts}.post_date) AS `year` FROM {$wpdb->posts} {$join} {$where} GROUP BY YEAR({$wpdb->posts}.post_date) ORDER BY {$wpdb->posts}.post_date {$order} {$limit}";
         $key = md5($query);
         $key = "RY_CP_wp_get_archives:{$key}:{$last_changed}";
         if (!($results = wp_cache_get($key, 'posts'))) {
             $results = $wpdb->get_results($query);
             wp_cache_set($key, $results, 'posts');
         }
         if ($results) {
             $permalink_type = $wp_rewrite->get_year_permastruct();
             $permalink_type = empty($permalink_type);
             foreach ((array) $results as $result) {
                 $url = get_year_link($result->year);
                 if ($r['post_type'] != 'post') {
                     if ($permalink_type) {
                         $url .= '&post_type=' . $r['post_type'];
                     } else {
                         $url = str_replace('/date/', '/date/' . $r['post_type'] . '/', $url);
                     }
                 }
                 $list[] = array('year' => $result->year, 'month' => 0, 'day' => 0, 'url' => $url);
             }
         }
     } elseif ('daily' == $r['type']) {
         $query = "SELECT YEAR({$wpdb->posts}.post_date) AS `year`, MONTH({$wpdb->posts}.post_date) AS `month`, DAYOFMONTH({$wpdb->posts}.post_date) AS `day` FROM {$wpdb->posts} {$join} {$where} GROUP BY YEAR({$wpdb->posts}.post_date), MONTH({$wpdb->posts}.post_date), DAYOFMONTH({$wpdb->posts}.post_date) ORDER BY {$wpdb->posts}.post_date {$order} {$limit}";
         $key = md5($query);
         $key = "RY_CP_wp_get_archives:{$key}:{$last_changed}";
         if (!($results = wp_cache_get($key, 'posts'))) {
             $results = $wpdb->get_results($query);
             wp_cache_set($key, $results, 'posts');
         }
         if ($results) {
             $archive_day_date_format = 'Y/m/d';
             if (!(bool) $r['over_date']) {
                 $archive_day_date_format = get_option('date_format');
             }
             $permalink_type = $wp_rewrite->get_day_permastruct();
             $permalink_type = empty($permalink_type);
             foreach ((array) $results as $result) {
                 $url = get_day_link($result->year, $result->month, $result->day);
                 if ($r['post_type'] != 'post') {
                     if ($permalink_type) {
                         $url .= '&post_type=' . $r['post_type'];
                     } else {
                         $url = str_replace('/date/', '/date/' . $r['post_type'] . '/', $url);
                     }
                 }
                 $list[] = array('year' => $result->year, 'month' => $result->month, 'day' => $result->day, 'url' => $url);
             }
         }
     }
     return $list;
 }