クラス WP_User_Query::prepare_query()
2014/12/17
WP_User_Query::prepare_query()
定義ファイル :/blog/wp-includes/user.php 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | 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' => '' ) ); } 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"; // 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 ( SELECT post_author, COUNT(*) as post_count FROM $wpdb->posts $where GROUP BY post_author ) p ON ({$wpdb->users}.ID = p.post_author) "; $orderby = 'post_count'; } elseif ( 'ID' == $qv['orderby'] || 'id' == $qv['orderby'] ) { $orderby = 'ID'; } elseif ( 'meta_value' == $qv['orderby'] ) { $orderby = "$wpdb->usermeta.meta_value"; } 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'); } $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 } $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( $qv['meta_query'] ) || ! in_array( $cap_meta_query, $qv['meta_query'], true ) ) { $qv['meta_query'][] = $cap_meta_query; } } $meta_query = new WP_Meta_Query(); $meta_query->parse_query_vars( $qv ); 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( $qv['include'] ) ) { $ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); $this->query_where .= " AND $wpdb->users.ID IN ($ids)"; } elseif ( ! empty( $qv['exclude'] ) ) { $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)"; } do_action_ref_array( 'pre_user_query', array( &$this ) ); } |