Wordpress 3.6
[autoinstalls/wordpress.git] / wp-includes / query.php
index 443d0bb742b7c0e2a614a5edfaa511a6d8df83a3..04286aa517cfc9fe5b180ae955811ed7503e202a 100644 (file)
@@ -2,8 +2,8 @@
 /**
  * WordPress Query API
  *
- * The query API attempts to get which part of WordPress to the user is on. It
- * also provides functionality to getting URL query information.
+ * The query API attempts to get which part of WordPress the user is on. It
+ * also provides functionality for getting URL query information.
  *
  * @link http://codex.wordpress.org/The_Loop More information on The Loop.
  *
@@ -1406,8 +1406,8 @@ class WP_Query {
                                $array[$key] = '';
                }
 
-               $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
-                       'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
+               $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
+                       'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in' );
 
                foreach ( $array_keys as $key ) {
                        if ( !isset($array[$key]) )
@@ -2079,7 +2079,7 @@ class WP_Query {
                if ( $q['day'] )
                        $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
 
-               // If we've got a post_type AND its not "any" post_type.
+               // 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);
@@ -2168,8 +2168,15 @@ class WP_Query {
                        $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
                }
 
-               if ( is_numeric($q['post_parent']) )
+               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') ) ) {
@@ -2230,6 +2237,8 @@ class WP_Query {
                                }
                                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) ) {
@@ -2256,25 +2265,30 @@ class WP_Query {
                                }
 
                                $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
-                               if ( !empty( $cat_query ) ) {
+                               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 );
+
+                                       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( $the_cat );
                                }
                                unset( $cat_query );
 
                                $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
-                               if ( !empty( $tag_query ) ) {
+                               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 );
+
+                                       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( $the_tag );
                                }
                                unset( $tag_query );
                        }
@@ -2343,6 +2357,8 @@ class WP_Query {
                        $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');
@@ -2394,9 +2410,11 @@ class WP_Query {
                                $orderby .= " {$q['order']}";
                }
 
-               if ( is_array( $post_type ) ) {
+               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;
@@ -2422,14 +2440,13 @@ class WP_Query {
                        $post_type_object = get_post_type_object ( 'post' );
                }
 
+               $edit_cap = 'edit_post';
+               $read_cap = 'read_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';
                }
@@ -2800,7 +2817,7 @@ class WP_Query {
        function set_found_posts( $q, $limits ) {
                global $wpdb;
 
-               // Bail if posts is an empty array. Continue if posts is an empty string
+               // Bail if posts is an empty array. Continue if posts is an empty string,
                // null, or false to accommodate caching plugins that fill posts later.
                if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
                        return;
@@ -3622,7 +3639,7 @@ function wp_old_slug_redirect() {
  * @uses do_action_ref_array() Calls 'the_post'
  * @return bool True when finished.
  */
-function setup_postdata($post) {
+function setup_postdata( $post ) {
        global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
 
        $id = (int) $post->ID;
@@ -3632,24 +3649,28 @@ function setup_postdata($post) {
        $currentday = mysql2date('d.m.y', $post->post_date, false);
        $currentmonth = mysql2date('m', $post->post_date, false);
        $numpages = 1;
+       $multipage = 0;
        $page = get_query_var('page');
-       if ( !$page )
+       if ( ! $page )
                $page = 1;
        if ( is_single() || is_page() || is_feed() )
                $more = 1;
        $content = $post->post_content;
-       if ( strpos( $content, '<!--nextpage-->' ) ) {
+       if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
                if ( $page > 1 )
                        $more = 1;
-               $multipage = 1;
-               $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
-               $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
-               $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
+               $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
+               $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
+               $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
+               // Ignore nextpage at the beginning of the content.
+               if ( 0 === strpos( $content, '<!--nextpage-->' ) )
+                       $content = substr( $content, 15 );
                $pages = explode('<!--nextpage-->', $content);
                $numpages = count($pages);
+               if ( $numpages > 1 )
+                       $multipage = 1;
        } else {
                $pages = array( $post->post_content );
-               $multipage = 0;
        }
 
        do_action_ref_array('the_post', array(&$post));