Changeset 28471
- Timestamp:
- 05/17/2014 05:11:23 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/post.php
r28470 r28471 4158 4158 global $wpdb; 4159 4159 4160 $pages = false;4161 4162 4160 $defaults = array( 4163 4161 'child_of' => 0, 'sort_order' => 'ASC', … … 4171 4169 4172 4170 $r = wp_parse_args( $args, $defaults ); 4173 extract( $r, EXTR_SKIP ); 4174 $number = (int) $number; 4175 $offset = (int) $offset; 4171 4172 $number = (int) $r['number']; 4173 $offset = (int) $r['offset']; 4174 $child_of = (int) $r['child_of']; 4175 $hierarchical = $r['hierarchical']; 4176 $exclude = $r['exclude']; 4177 $meta_key = $r['meta_key']; 4178 $meta_value = $r['meta_value']; 4179 $parent = $r['parent']; 4180 $post_status = $r['post_status']; 4176 4181 4177 4182 // Make sure the post type is hierarchical 4178 4183 $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); 4179 if ( !in_array( $post_type, $hierarchical_post_types ) ) 4180 return $pages; 4181 4182 if ( $parent > 0 && ! $child_of ) 4184 if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) { 4185 return false; 4186 } 4187 4188 if ( $parent > 0 && ! $child_of ) { 4183 4189 $hierarchical = false; 4190 } 4184 4191 4185 4192 // Make sure we have a valid post status 4186 if ( ! is_array( $post_status ) )4193 if ( ! is_array( $post_status ) ) { 4187 4194 $post_status = explode( ',', $post_status ); 4188 if ( array_diff( $post_status, get_post_stati() ) ) 4189 return $pages; 4195 } 4196 if ( array_diff( $post_status, get_post_stati() ) ) { 4197 return false; 4198 } 4190 4199 4191 4200 // $args can be whatever, only use the args defined in defaults to compute the key 4192 $key = md5( serialize( compact(array_keys($defaults)) ) );4201 $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) ); 4193 4202 $last_changed = wp_cache_get( 'last_changed', 'posts' ); 4194 4203 if ( ! $last_changed ) { … … 4207 4216 4208 4217 $inclusions = ''; 4209 if ( ! empty( $ include) ) {4218 if ( ! empty( $r['include'] ) ) { 4210 4219 $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include 4211 4220 $parent = -1; … … 4214 4223 $meta_value = ''; 4215 4224 $hierarchical = false; 4216 $incpages = wp_parse_id_list( $ include);4217 if ( ! empty( $incpages ) ) 4225 $incpages = wp_parse_id_list( $r['include'] ); 4226 if ( ! empty( $incpages ) ) { 4218 4227 $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; 4228 } 4219 4229 } 4220 4230 … … 4222 4232 if ( ! empty( $exclude ) ) { 4223 4233 $expages = wp_parse_id_list( $exclude ); 4224 if ( ! empty( $expages ) ) 4234 if ( ! empty( $expages ) ) { 4225 4235 $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; 4236 } 4226 4237 } 4227 4238 4228 4239 $author_query = ''; 4229 if ( !empty($authors)) {4230 $post_authors = preg_split( '/[\s,]+/',$authors);4240 if ( ! empty( $r['authors'] ) ) { 4241 $post_authors = preg_split( '/[\s,]+/', $r['authors'] ); 4231 4242 4232 4243 if ( ! empty( $post_authors ) ) { … … 4235 4246 if ( 0 == intval($post_author) ) { 4236 4247 $post_author = get_user_by('login', $post_author); 4237 if ( empty( $post_author) )4248 if ( empty( $post_author ) ) { 4238 4249 continue; 4239 if ( empty($post_author->ID) ) 4250 } 4251 if ( empty( $post_author->ID ) ) { 4240 4252 continue; 4253 } 4241 4254 $post_author = $post_author->ID; 4242 4255 } 4243 4256 4244 if ( '' == $author_query ) 4257 if ( '' == $author_query ) { 4245 4258 $author_query = $wpdb->prepare(' post_author = %d ', $post_author); 4246 else4259 } else { 4247 4260 $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author); 4261 } 4248 4262 } 4249 if ( '' != $author_query ) 4263 if ( '' != $author_query ) { 4250 4264 $author_query = " AND ($author_query)"; 4265 } 4251 4266 } 4252 4267 } … … 4260 4275 $meta_key = wp_unslash($meta_key); 4261 4276 $meta_value = wp_unslash($meta_value); 4262 if ( '' !== $meta_key ) 4277 if ( '' !== $meta_key ) { 4263 4278 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key); 4264 if ( '' !== $meta_value ) 4279 } 4280 if ( '' !== $meta_value ) { 4265 4281 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value); 4282 } 4266 4283 4267 4284 } … … 4269 4286 if ( is_array( $parent ) ) { 4270 4287 $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); 4271 if ( ! empty( $post_parent__in ) ) 4288 if ( ! empty( $post_parent__in ) ) { 4272 4289 $where .= " AND post_parent IN ($post_parent__in)"; 4290 } 4273 4291 } elseif ( $parent >= 0 ) { 4274 4292 $where .= $wpdb->prepare(' AND post_parent = %d ', $parent); … … 4276 4294 4277 4295 if ( 1 == count( $post_status ) ) { 4278 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $ post_type, array_shift( $post_status ) );4296 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $r['post_type'], array_shift( $post_status ) ); 4279 4297 } else { 4280 4298 $post_status = implode( "', '", $post_status ); 4281 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $ post_type);4299 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] ); 4282 4300 } 4283 4301 4284 4302 $orderby_array = array(); 4285 $allowed_keys = array('author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified', 4286 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent', 4287 'ID', 'rand', 'comment_count'); 4288 foreach ( explode( ',', $sort_column ) as $orderby ) { 4303 $allowed_keys = array( 'author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified', 4304 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent', 4305 'ID', 'rand', 'comment_count' ); 4306 4307 foreach ( explode( ',', $r['sort_column'] ) as $orderby ) { 4289 4308 $orderby = trim( $orderby ); 4290 if ( ! in_array( $orderby, $allowed_keys ) )4309 if ( ! in_array( $orderby, $allowed_keys ) ) { 4291 4310 continue; 4311 } 4292 4312 4293 4313 switch ( $orderby ) { … … 4304 4324 break; 4305 4325 default: 4306 if ( 0 === strpos( $orderby, 'post_' ) ) 4326 if ( 0 === strpos( $orderby, 'post_' ) ) { 4307 4327 $orderby = "$wpdb->posts." . $orderby; 4308 else4328 } else { 4309 4329 $orderby = "$wpdb->posts.post_" . $orderby; 4330 } 4310 4331 } 4311 4332 … … 4315 4336 $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; 4316 4337 4317 $sort_order = strtoupper( $ sort_order);4318 if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) )4338 $sort_order = strtoupper( $r['sort_order'] ); 4339 if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) { 4319 4340 $sort_order = 'ASC'; 4341 } 4320 4342 4321 4343 $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; … … 4323 4345 $query .= " ORDER BY " . $sort_column . " " . $sort_order ; 4324 4346 4325 if ( ! empty($number) )4347 if ( ! empty( $number ) ) { 4326 4348 $query .= ' LIMIT ' . $offset . ',' . $number; 4349 } 4327 4350 4328 4351 $pages = $wpdb->get_results($query); … … 4343 4366 update_post_cache( $pages ); 4344 4367 4345 if ( $child_of || $hierarchical ) 4368 if ( $child_of || $hierarchical ) { 4346 4369 $pages = get_page_children($child_of, $pages); 4347 4348 if ( ! empty( $exclude_tree ) ) { 4349 $exclude = wp_parse_id_list( $exclude_tree ); 4370 } 4371 4372 if ( ! empty( $r['exclude_tree'] ) ) { 4373 $exclude = wp_parse_id_list( $r['exclude_tree'] ); 4350 4374 foreach( $exclude as $id ) { 4351 4375 $children = get_page_children( $id, $pages ); … … 4364 4388 4365 4389 $page_structure = array(); 4366 foreach ( $pages as $page ) 4390 foreach ( $pages as $page ) { 4367 4391 $page_structure[] = $page->ID; 4392 } 4368 4393 4369 4394 wp_cache_set( $cache_key, $page_structure, 'posts' );
Note: See TracChangeset
for help on using the changeset viewer.