Make WordPress Core

Changeset 28471


Ignore:
Timestamp:
05/17/2014 05:11:23 PM (12 years ago)
Author:
wonderboymusic
Message:

Eliminate the use of extract() in get_pages().

See #22400.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r28470 r28471  
    41584158        global $wpdb;
    41594159
    4160         $pages = false;
    4161 
    41624160        $defaults = array(
    41634161                'child_of' => 0, 'sort_order' => 'ASC',
    … …  
    41714169
    41724170        $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'];
    41764181
    41774182        // Make sure the post type is hierarchical
    41784183        $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 ) {
    41834189                $hierarchical = false;
     4190        }
    41844191
    41854192        // Make sure we have a valid post status
    4186         if ( !is_array( $post_status ) )
     4193        if ( ! is_array( $post_status ) ) {
    41874194                $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        }
    41904199
    41914200        // $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 ) ) ) );
    41934202        $last_changed = wp_cache_get( 'last_changed', 'posts' );
    41944203        if ( ! $last_changed ) {
    … …  
    42074216
    42084217        $inclusions = '';
    4209         if ( ! empty( $include ) ) {
     4218        if ( ! empty( $r['include'] ) ) {
    42104219                $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
    42114220                $parent = -1;
    … …  
    42144223                $meta_value = '';
    42154224                $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 ) ) {
    42184227                        $inclusions = ' AND ID IN (' . implode( ',', $incpages ) .  ')';
     4228                }
    42194229        }
    42204230
    … …  
    42224232        if ( ! empty( $exclude ) ) {
    42234233                $expages = wp_parse_id_list( $exclude );
    4224                 if ( ! empty( $expages ) )
     4234                if ( ! empty( $expages ) ) {
    42254235                        $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) .  ')';
     4236                }
    42264237        }
    42274238
    42284239        $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'] );
    42314242
    42324243                if ( ! empty( $post_authors ) ) {
    … …  
    42354246                                if ( 0 == intval($post_author) ) {
    42364247                                        $post_author = get_user_by('login', $post_author);
    4237                                         if ( empty($post_author) )
     4248                                        if ( empty( $post_author ) ) {
    42384249                                                continue;
    4239                                         if ( empty($post_author->ID) )
     4250                                        }
     4251                                        if ( empty( $post_author->ID ) ) {
    42404252                                                continue;
     4253                                        }
    42414254                                        $post_author = $post_author->ID;
    42424255                                }
    42434256
    4244                                 if ( '' == $author_query )
     4257                                if ( '' == $author_query ) {
    42454258                                        $author_query = $wpdb->prepare(' post_author = %d ', $post_author);
    4246                                 else
     4259                                } else {
    42474260                                        $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
     4261                                }
    42484262                        }
    4249                         if ( '' != $author_query )
     4263                        if ( '' != $author_query ) {
    42504264                                $author_query = " AND ($author_query)";
     4265                        }
    42514266                }
    42524267        }
    … …  
    42604275                $meta_key = wp_unslash($meta_key);
    42614276                $meta_value = wp_unslash($meta_value);
    4262                 if ( '' !== $meta_key )
     4277                if ( '' !== $meta_key ) {
    42634278                        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
    4264                 if ( '' !== $meta_value )
     4279                }
     4280                if ( '' !== $meta_value ) {
    42654281                        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
     4282                }
    42664283
    42674284        }
    … …  
    42694286        if ( is_array( $parent ) ) {
    42704287                $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
    4271                 if ( ! empty( $post_parent__in ) )
     4288                if ( ! empty( $post_parent__in ) ) {
    42724289                        $where .= " AND post_parent IN ($post_parent__in)";
     4290                }
    42734291        } elseif ( $parent >= 0 ) {
    42744292                $where .= $wpdb->prepare(' AND post_parent = %d ', $parent);
    … …  
    42764294
    42774295        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 ) );
    42794297        } else {
    42804298                $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'] );
    42824300        }
    42834301
    42844302        $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 ) {
    42894308                $orderby = trim( $orderby );
    4290                 if ( !in_array( $orderby, $allowed_keys ) )
     4309                if ( ! in_array( $orderby, $allowed_keys ) ) {
    42914310                        continue;
     4311                }
    42924312
    42934313                switch ( $orderby ) {
    … …  
    43044324                                break;
    43054325                        default:
    4306                                 if ( 0 === strpos( $orderby, 'post_' ) )
     4326                                if ( 0 === strpos( $orderby, 'post_' ) ) {
    43074327                                        $orderby = "$wpdb->posts." . $orderby;
    4308                                 else
     4328                                } else {
    43094329                                        $orderby = "$wpdb->posts.post_" . $orderby;
     4330                                }
    43104331                }
    43114332
    … …  
    43154336        $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";
    43164337
    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' ) ) ) {
    43194340                $sort_order = 'ASC';
     4341        }
    43204342
    43214343        $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where ";
    … …  
    43234345        $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
    43244346
    4325         if ( !empty($number) )
     4347        if ( ! empty( $number ) ) {
    43264348                $query .= ' LIMIT ' . $offset . ',' . $number;
     4349        }
    43274350
    43284351        $pages = $wpdb->get_results($query);
    … …  
    43434366        update_post_cache( $pages );
    43444367
    4345         if ( $child_of || $hierarchical )
     4368        if ( $child_of || $hierarchical ) {
    43464369                $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'] );
    43504374                foreach( $exclude as $id ) {
    43514375                        $children = get_page_children( $id, $pages );
    … …  
    43644388
    43654389        $page_structure = array();
    4366         foreach ( $pages as $page )
     4390        foreach ( $pages as $page ) {
    43674391                $page_structure[] = $page->ID;
     4392        }
    43684393
    43694394        wp_cache_set( $cache_key, $page_structure, 'posts' );
Note: See TracChangeset for help on using the changeset viewer.