Ticket #12821: post.diff

File post.diff, 8.5 KB (added by dbernar1, 16 months ago)
Line 
1Index: post.php
2===================================================================
3--- post.php    (revision 19734)
4+++ post.php    (working copy)
5@@ -3314,7 +3314,7 @@
6  * Retrieve a list of pages.
7  *
8  * The defaults that can be overridden are the following: 'child_of',
9- * 'sort_order', 'sort_column', 'post_title', 'hierarchical', 'exclude',
10+ * 'sort_order', 'sort_column', 'hierarchical', 'exclude',
11  * 'include', 'meta_key', 'meta_value','authors', 'number', and 'offset'.
12  *
13  * @since 1.5.0
14@@ -3324,7 +3324,6 @@
15  * @return array List of pages matching defaults or $args
16  */
17 function &get_pages($args = '') {
18-       global $wpdb;
19 
20        $defaults = array(
21                'child_of' => 0, 'sort_order' => 'ASC',
22@@ -3337,177 +3336,94 @@
23        );
24 
25        $r = wp_parse_args( $args, $defaults );
26-       extract( $r, EXTR_SKIP );
27-       $number = (int) $number;
28-       $offset = (int) $offset;
29+  extract( $r, EXTR_SKIP );
30 
31+  //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
32+       if ( !empty($include) ) {
33+               $child_of = 0;
34+               $parent = -1;
35+               $exclude = '';
36+               $meta_key = '';
37+               $meta_value = '';
38+               $hierarchical = false;
39+       }
40
41        // Make sure the post type is hierarchical
42+  // TODO extract this to something like is_a_hierarchical_post_type( $post_type )
43        $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
44        if ( !in_array( $post_type, $hierarchical_post_types ) )
45                return false;
46 
47-       // Make sure we have a valid post status
48-       if ( !is_array( $post_status ) )
49-               $post_status = explode( ',', $post_status );
50-       if ( array_diff( $post_status, get_post_stati() ) )
51-               return false;
52+       $sort_order = strtoupper( $sort_order );
53+       if ( empty( $sort_order ) && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) )
54+               $sort_order = 'ASC';
55 
56+  // Check whether we have cached the results of the same query
57        $cache = array();
58        $key = md5( serialize( compact(array_keys($defaults)) ) );
59        if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
60-               if ( is_array($cache) && isset( $cache[ $key ] ) ) {
61-                       $pages = apply_filters('get_pages', $cache[ $key ], $r );
62+               if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
63+                       $pages = apply_filters( 'get_pages', $cache[ $key ], $r );
64                        return $pages;
65                }
66        }
67 
68-       if ( !is_array($cache) )
69+       if ( ! is_array( $cache ) )
70                $cache = array();
71 
72-       $inclusions = '';
73-       if ( !empty($include) ) {
74-               $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
75-               $parent = -1;
76-               $exclude = '';
77-               $meta_key = '';
78-               $meta_value = '';
79-               $hierarchical = false;
80-               $incpages = wp_parse_id_list( $include );
81-               if ( ! empty( $incpages ) ) {
82-                       foreach ( $incpages as $incpage ) {
83-                               if (empty($inclusions))
84-                                       $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
85-                               else
86-                                       $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage);
87-                       }
88-               }
89-       }
90-       if (!empty($inclusions))
91-               $inclusions .= ')';
92+  // TODO Rename $post_authors to $requested_authors
93+       if ( ! empty( $authors ) ) {
94+               $post_authors = preg_split( '/[\s,]+/',$authors );
95 
96-       $exclusions = '';
97-       if ( !empty($exclude) ) {
98-               $expages = wp_parse_id_list( $exclude );
99-               if ( ! empty( $expages ) ) {
100-                       foreach ( $expages as $expage ) {
101-                               if (empty($exclusions))
102-                                       $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
103-                               else
104-                                       $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage);
105-                       }
106-               }
107-       }
108-       if (!empty($exclusions))
109-               $exclusions .= ')';
110-
111-       $author_query = '';
112-       if (!empty($authors)) {
113-               $post_authors = preg_split('/[\s,]+/',$authors);
114-
115                if ( ! empty( $post_authors ) ) {
116                        foreach ( $post_authors as $post_author ) {
117                                //Do we have an author id or an author login?
118-                               if ( 0 == intval($post_author) ) {
119-                                       $post_author = get_user_by('login', $post_author);
120-                                       if ( empty($post_author) )
121+                               if ( 0 == intval( $post_author ) ) {
122+                                       $post_author = get_user_by( 'login', $post_author );
123+                                       if ( empty( $post_author ) || empty( $post_author->ID ) )
124                                                continue;
125-                                       if ( empty($post_author->ID) )
126-                                               continue;
127-                                       $post_author = $post_author->ID;
128-                               }
129-
130-                               if ( '' == $author_query )
131-                                       $author_query = $wpdb->prepare(' post_author = %d ', $post_author);
132-                               else
133-                                       $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
134-                       }
135-                       if ( '' != $author_query )
136-                               $author_query = " AND ($author_query)";
137+                                       $author_ids[] = $post_author->ID;
138+                               } else {
139+          $author_ids[] = $post_author;
140+        }
141+      }
142                }
143+    $r['author'] = join( ',', $author_ids );
144        }
145 
146-       $join = '';
147-       $where = "$exclusions $inclusions ";
148-       if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) {
149-               $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
150+  // get_pages() API supports both post_author and author for sorting, and WP_Query only supports the version without the post_ prefix
151+  // get_pages accepts comma-separated list of sort_columns, and WP_Query accepts a space-separated one
152+       // TODO get_pages had this additional sorting field. Figure out what to do about this one: $allowed_keys = array( 'modified_gmt', 'post_modified_gmt' );
153+  $sort_column = str_replace( array( 'post_', ',' ), array( '', ' ' ), $sort_column );
154 
155-               // meta_key and meta_value might be slashed
156-               $meta_key = stripslashes($meta_key);
157-               $meta_value = stripslashes($meta_value);
158-               if ( ! empty( $meta_key ) )
159-                       $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
160-               if ( ! empty( $meta_value ) )
161-                       $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);
162+       if ( ! empty( $r['number'] ) && empty( $r['posts_per_page'] ) )
163+               $r['posts_per_page'] = $r['number'];
164+       if ( ! empty( $include ) ) {
165+               $incposts = wp_parse_id_list( $include );
166+               $r['posts_per_page'] = count($incposts);  // only the number of posts included
167+               $r['post__in'] = $incposts;
168+       } elseif ( ! empty( $exclude ) )
169+               $r['post__not_in'] = wp_parse_id_list( $exclude );
170 
171-       }
172+  if ( ! empty( $sort_order ) )
173+    $r['order'] = $sort_order;
174 
175-       if ( $parent >= 0 )
176-               $where .= $wpdb->prepare(' AND post_parent = %d ', $parent);
177+  if ( ! empty( $sort_column ) )
178+    $r['orderby'] = $sort_column;
179+  if ( $parent >= 0 )
180+    $r['post_parent'] = $parent;
181 
182-       if ( 1 == count( $post_status ) ) {
183-               $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $post_type, array_shift( $post_status ) );
184-       } else {
185-               $post_status = implode( "', '", $post_status );
186-               $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $post_type );
187-       }
188+       $r['ignore_sticky_posts'] = true;
189+       $r['no_found_rows'] = true;
190 
191-       $orderby_array = array();
192-       $allowed_keys = array('author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified',
193-                                                 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent',
194-                                                 'ID', 'rand', 'comment_count');
195-       foreach ( explode( ',', $sort_column ) as $orderby ) {
196-               $orderby = trim( $orderby );
197-               if ( !in_array( $orderby, $allowed_keys ) )
198-                       continue;
199+  $get_pages = new WP_Query;
200+       $pages = $get_pages->query( $r );
201 
202-               switch ( $orderby ) {
203-                       case 'menu_order':
204-                               break;
205-                       case 'ID':
206-                               $orderby = "$wpdb->posts.ID";
207-                               break;
208-                       case 'rand':
209-                               $orderby = 'RAND()';
210-                               break;
211-                       case 'comment_count':
212-                               $orderby = "$wpdb->posts.comment_count";
213-                               break;
214-                       default:
215-                               if ( 0 === strpos( $orderby, 'post_' ) )
216-                                       $orderby = "$wpdb->posts." . $orderby;
217-                               else
218-                                       $orderby = "$wpdb->posts.post_" . $orderby;
219-               }
220-
221-               $orderby_array[] = $orderby;
222-
223-       }
224-       $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";
225-
226-       $sort_order = strtoupper( $sort_order );
227-       if ( '' !== $sort_order && !in_array( $sort_order, array( 'ASC', 'DESC' ) ) )
228-               $sort_order = 'ASC';
229-
230-       $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where ";
231-       $query .= $author_query;
232-       $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
233-
234-       if ( !empty($number) )
235-               $query .= ' LIMIT ' . $offset . ',' . $number;
236-
237-       $pages = $wpdb->get_results($query);
238-
239        if ( empty($pages) ) {
240                $pages = apply_filters('get_pages', array(), $r);
241                return $pages;
242        }
243 
244-       // Sanitize before caching so it'll only get done once
245-       $num_pages = count($pages);
246-       for ($i = 0; $i < $num_pages; $i++) {
247-               $pages[$i] = sanitize_post($pages[$i], 'raw');
248-       }
249-
250        // Update cache.
251        update_page_cache($pages);
252 
253@@ -3528,6 +3444,7 @@
254                }
255        }
256 
257+  // TODO rename $key to unique_id_for_a_given_set_of_params
258        $cache[ $key ] = $pages;
259        wp_cache_set( 'get_pages', $cache, 'posts' );
260