<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[55569] trunk: Posts, Post Types: Use WP_Query internally in get_pages.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/55569">55569</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/55569","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-03-21 12:47:20 +0000 (Tue, 21 Mar 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Posts, Post Types: Use WP_Query internally in get_pages. 

 Convert `get_pages` to use `WP_Query` internally. Using WP_Query means that a lot of code has been removed however existing parameters supported by get_pages are transformed in to query arguments. The custom caching solution found in the old version of this function is replaced with the caching found in WP_Query (added in <a href="https://core.trac.wordpress.org/changeset/53941">[53941]</a>). This change adds consistency to the codebase, as improvements and changes to `WP_Query` will filter down to the `get_pages` function. 

Props mikeschinkel, spacedmonkey, nacin, scribu, filosofo, jane, garyc40, markoheijnen, grandslambert, kevinB, wlindley, dbernar1, atimmer, mdawaffe, helen, benjibee, johnbillion, peterwilsoncc, costdev, flixos90, joemcgill.
Fixes <a href="https://core.trac.wordpress.org/ticket/12821">#12821</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestspostgetPagesphp">trunk/tests/phpunit/tests/post/getPages.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2023-03-21 12:41:26 UTC (rev 55568)
+++ trunk/src/wp-includes/post.php      2023-03-21 12:47:20 UTC (rev 55569)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5886,9 +5886,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Retrieves an array of pages (or hierarchical post type items).
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global wpdb $wpdb WordPress database abstraction object.
- *
</del><span class="cx" style="display: block; padding: 0 10px">  * @since 1.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.3.0 Use WP_Query internally.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array|string $args {
</span><span class="cx" style="display: block; padding: 0 10px">  *     Optional. Array or string of arguments to retrieve pages.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5928,8 +5927,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                         supported by the post type.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_pages( $args = array() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        global $wpdb;
-
</del><span class="cx" style="display: block; padding: 0 10px">         $defaults = array(
</span><span class="cx" style="display: block; padding: 0 10px">                'child_of'     => 0,
</span><span class="cx" style="display: block; padding: 0 10px">                'sort_order'   => 'ASC',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5978,50 +5975,35 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // $args can be whatever, only use the args defined in defaults to compute the key.
-       $key          = md5( serialize( wp_array_slice_assoc( $parsed_args, array_keys( $defaults ) ) ) );
-       $last_changed = wp_cache_get_last_changed( 'posts' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query_args = array(
+               'orderby'                => 'post_title',
+               'order'                  => 'ASC',
+               'post__not_in'           => wp_parse_id_list( $exclude ),
+               'meta_key'               => $meta_key,
+               'meta_value'             => $meta_value,
+               'posts_per_page'         => -1,
+               'offset'                 => $offset,
+               'post_type'              => $parsed_args['post_type'],
+               'post_status'            => $post_status,
+               'update_post_term_cache' => false,
+               'update_post_meta_cache' => false,
+               'ignore_sticky_posts'    => true,
+               'no_found_rows'          => true,
+       );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $cache_key = "get_pages:$key:$last_changed";
-       $cache     = wp_cache_get( $cache_key, 'post-queries' );
-       if ( false !== $cache ) {
-               _prime_post_caches( $cache, false, false );
-
-               // Convert to WP_Post instances.
-               $pages = array_map( 'get_post', $cache );
-               /** This filter is documented in wp-includes/post.php */
-               $pages = apply_filters( 'get_pages', $pages, $parsed_args );
-
-               return $pages;
-       }
-
-       $inclusions = '';
</del><span class="cx" style="display: block; padding: 0 10px">         if ( ! empty( $parsed_args['include'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $child_of     = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
-               $parent       = -1;
-               $exclude      = '';
-               $meta_key     = '';
-               $meta_value   = '';
-               $hierarchical = false;
-               $incpages     = wp_parse_id_list( $parsed_args['include'] );
-               if ( ! empty( $incpages ) ) {
-                       $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')';
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
+               $parent   = -1;
+               unset( $query_args['post__not_in'], $query_args['meta_key'], $query_args['meta_value'] );
+               $hierarchical           = false;
+               $query_args['post__in'] = wp_parse_id_list( $parsed_args['include'] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $exclusions = '';
-       if ( ! empty( $exclude ) ) {
-               $expages = wp_parse_id_list( $exclude );
-               if ( ! empty( $expages ) ) {
-                       $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')';
-               }
-       }
-
-       $author_query = '';
</del><span class="cx" style="display: block; padding: 0 10px">         if ( ! empty( $parsed_args['authors'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $post_authors = wp_parse_list( $parsed_args['authors'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $post_authors ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $query_args['author__in'] = array();
</ins><span class="cx" style="display: block; padding: 0 10px">                         foreach ( $post_authors as $post_author ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                // Do we have an author id or an author login?
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( 0 == (int) $post_author ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6034,137 +6016,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px">                                        $post_author = $post_author->ID;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                               if ( '' === $author_query ) {
-                                       $author_query = $wpdb->prepare( ' post_author = %d ', $post_author );
-                               } else {
-                                       $author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author );
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $query_args['author__in'][] = $post_author;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( '' !== $author_query ) {
-                               $author_query = " AND ($author_query)";
-                       }
</del><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $join  = '';
-       $where = "$exclusions $inclusions ";
-       if ( '' !== $meta_key || '' !== $meta_value ) {
-               $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
-
-               // meta_key and meta_value might be slashed.
-               $meta_key   = wp_unslash( $meta_key );
-               $meta_value = wp_unslash( $meta_value );
-               if ( '' !== $meta_key ) {
-                       $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key );
-               }
-               if ( '' !== $meta_value ) {
-                       $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value );
-               }
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         if ( is_array( $parent ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $post_parent__in = array_map( 'absint', (array) $parent );
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! empty( $post_parent__in ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where .= " AND post_parent IN ($post_parent__in)";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $query_args['post_parent__in'] = $post_parent__in;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        } elseif ( $parent >= 0 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $where .= $wpdb->prepare( ' AND post_parent = %d ', $parent );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $query_args['post_parent'] = $parent;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( 1 === count( $post_status ) ) {
-               $where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $parsed_args['post_type'], reset( $post_status ) );
-       } else {
-               $post_status     = implode( "', '", $post_status );
-               $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $parsed_args['post_type'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $orderby = wp_parse_list( $parsed_args['sort_column'] );
+       $orderby = array_map( 'trim', $orderby );
+       if ( $orderby ) {
+               $query_args['orderby'] = array_fill_keys( $orderby, $parsed_args['sort_order'] );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $orderby_array = array();
-       $allowed_keys  = array(
-               'author',
-               'post_author',
-               'date',
-               'post_date',
-               'title',
-               'post_title',
-               'name',
-               'post_name',
-               'modified',
-               'post_modified',
-               'modified_gmt',
-               'post_modified_gmt',
-               'menu_order',
-               'parent',
-               'post_parent',
-               'ID',
-               'rand',
-               'comment_count',
-       );
-
-       foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) {
-               $orderby = trim( $orderby );
-               if ( ! in_array( $orderby, $allowed_keys, true ) ) {
-                       continue;
-               }
-
-               switch ( $orderby ) {
-                       case 'menu_order':
-                               break;
-                       case 'ID':
-                               $orderby = "$wpdb->posts.ID";
-                               break;
-                       case 'rand':
-                               $orderby = 'RAND()';
-                               break;
-                       case 'comment_count':
-                               $orderby = "$wpdb->posts.comment_count";
-                               break;
-                       default:
-                               if ( 0 === strpos( $orderby, 'post_' ) ) {
-                                       $orderby = "$wpdb->posts." . $orderby;
-                               } else {
-                                       $orderby = "$wpdb->posts.post_" . $orderby;
-                               }
-               }
-
-               $orderby_array[] = $orderby;
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $order = $parsed_args['sort_order'];
+       if ( $order ) {
+               $query_args['order'] = $order;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $sort_order = strtoupper( $parsed_args['sort_order'] );
-       if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) {
-               $sort_order = 'ASC';
-       }
-
-       $query  = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where ";
-       $query .= $author_query;
-       $query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order;
-
</del><span class="cx" style="display: block; padding: 0 10px">         if ( ! empty( $number ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $query .= ' LIMIT ' . $offset . ',' . $number;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $query_args['posts_per_page'] = $number;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $pages = $wpdb->get_results( $query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query = new WP_Query( $query_args );
+       $pages = $query->get_posts();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( empty( $pages ) ) {
-               wp_cache_set( $cache_key, array(), 'post-queries' );
-
-               /** This filter is documented in wp-includes/post.php */
-               $pages = apply_filters( 'get_pages', array(), $parsed_args );
-
-               return $pages;
-       }
-
-       // Sanitize before caching so it'll only get done once.
-       $num_pages = count( $pages );
-       for ( $i = 0; $i < $num_pages; $i++ ) {
-               $pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' );
-       }
-
-       // Update cache.
-       update_post_cache( $pages );
-
</del><span class="cx" style="display: block; padding: 0 10px">         if ( $child_of || $hierarchical ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $pages = get_page_children( $child_of, $pages );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6186,16 +6069,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $page_structure = array();
-       foreach ( $pages as $page ) {
-               $page_structure[] = $page->ID;
-       }
-
-       wp_cache_set( $cache_key, $page_structure, 'post-queries' );
-
-       // Convert to WP_Post instances.
-       $pages = array_map( 'get_post', $pages );
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filters the retrieved list of pages.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="trunktestsphpunittestspostgetPagesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/getPages.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/getPages.php       2023-03-21 12:41:26 UTC (rev 55568)
+++ trunk/tests/phpunit/tests/post/getPages.php 2023-03-21 12:47:20 UTC (rev 55569)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -66,7 +66,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Force last_changed to increment.
</span><span class="cx" style="display: block; padding: 0 10px">                clean_post_cache( $pages[0]->ID );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         get_post( $pages[0]->ID );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $num_queries = $wpdb->num_queries;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // last_changed bumped so num_queries should increment.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -173,6 +173,60 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_include_ignore_meta_key() {
+               $posts = self::factory()->post->create_many(
+                       2,
+                       array(
+                               'post_type' => 'page',
+                       )
+               );
+
+               $pages = get_pages(
+                       array(
+                               'include'    => $posts,
+                               'meta_key'   => 'foo',
+                               'meta_value' => 'bar',
+                       )
+               );
+
+               $page_ids = wp_list_pluck( $pages, 'ID' );
+               $this->assertSameSets( $posts, $page_ids );
+       }
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_include_ignore_exclude() {
+               $includes = self::factory()->post->create_many(
+                       2,
+                       array(
+                               'post_type' => 'page',
+                       )
+               );
+
+               $excludes = self::factory()->post->create_many(
+                       2,
+                       array(
+                               'post_type' => 'page',
+                       )
+               );
+
+               $pages = get_pages(
+                       array(
+                               'include' => $includes,
+                               'exclude' => $excludes,
+                       )
+               );
+
+               $page_ids = wp_list_pluck( $pages, 'ID' );
+               $this->assertSameSets( $includes, $page_ids );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 40669
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_cache_should_be_invalidated_by_delete_post_meta() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -719,4 +773,253 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $num_queries, $wpdb->num_queries );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_get_pages_post_type() {
+               register_post_type( 'wptests_pt', array( 'hierarchical' => true ) );
+               $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
+               $pages = get_pages(
+                       array(
+                               'post_type' => 'wptests_pt',
+                       )
+               );
+               $this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) );
+       }
+
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_get_pages_author() {
+               $author_1 = self::factory()->user->create(
+                       array(
+                               'user_login' => 'author1',
+                               'role'       => 'author',
+                       )
+               );
+               $posts    = self::factory()->post->create_many(
+                       2,
+                       array(
+                               'post_type'   => 'page',
+                               'post_author' => $author_1,
+                       )
+               );
+               $pages    = get_pages(
+                       array(
+                               'authors' => $author_1,
+                       )
+               );
+
+               $this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) );
+       }
+
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_get_pages_post_status() {
+               register_post_status(
+                       'foo',
+                       array(
+                               'public' => true,
+                       )
+               );
+
+               $posts = self::factory()->post->create_many(
+                       2,
+                       array(
+                               'post_type'   => 'page',
+                               'post_status' => 'foo',
+                       )
+               );
+               $pages = get_pages(
+                       array(
+                               'post_status' => 'foo',
+                       )
+               );
+
+               $this->assertSameSets( $posts, wp_list_pluck( $pages, 'ID' ) );
+       }
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_get_pages_offset() {
+               $posts = self::factory()->post->create_many( 4, array( 'post_type' => 'page' ) );
+               $pages = get_pages(
+                       array(
+                               'offset' => 2,
+                               'number' => 2,
+                       )
+               );
+
+               $this->assertSameSets( array( $posts[2], $posts[3] ), wp_list_pluck( $pages, 'ID' ) );
+       }
+
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_get_pages_authors() {
+               $author_1 = self::factory()->user->create(
+                       array(
+                               'user_login' => 'author1',
+                               'role'       => 'author',
+                       )
+               );
+               $post_1   = self::factory()->post->create(
+                       array(
+                               'post_title'  => 'Page 1',
+                               'post_type'   => 'page',
+                               'post_author' => $author_1,
+                               'post_date'   => '2007-01-01 00:00:00',
+                       )
+               );
+
+               $author_2 = self::factory()->user->create(
+                       array(
+                               'user_login' => 'author2',
+                               'role'       => 'author',
+                       )
+               );
+               $post_2   = self::factory()->post->create(
+                       array(
+                               'post_title'  => 'Page 2',
+                               'post_type'   => 'page',
+                               'post_author' => $author_2,
+                               'post_date'   => '2007-01-01 00:00:00',
+                       )
+               );
+               $pages    = get_pages(
+                       array(
+                               'authors' => "{$author_1}, {$author_2}",
+                       )
+               );
+
+               $this->assertSameSets( array( $post_1, $post_2 ), wp_list_pluck( $pages, 'ID' ) );
+       }
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_get_pages_authors_names() {
+               $author_1 = self::factory()->user->create(
+                       array(
+                               'user_login' => 'author1',
+                               'role'       => 'author',
+                       )
+               );
+               $post_1   = self::factory()->post->create(
+                       array(
+                               'post_title'  => 'Page 1',
+                               'post_type'   => 'page',
+                               'post_author' => $author_1,
+                               'post_date'   => '2007-01-01 00:00:00',
+                       )
+               );
+
+               $author_2 = self::factory()->user->create(
+                       array(
+                               'user_login' => 'author2',
+                               'role'       => 'author',
+                       )
+               );
+               $post_2   = self::factory()->post->create(
+                       array(
+                               'post_title'  => 'Page 2',
+                               'post_type'   => 'page',
+                               'post_author' => $author_2,
+                               'post_date'   => '2007-01-01 00:00:00',
+                       )
+               );
+               $pages    = get_pages(
+                       array(
+                               'authors' => 'author1, author2',
+                       )
+               );
+
+               $this->assertSameSets( array( $post_1, $post_2 ), wp_list_pluck( $pages, 'ID' ) );
+       }
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_orderby() {
+               global $wpdb;
+               // 'rand' is a valid value.
+               get_pages( array( 'sort_column' => 'rand' ) );
+               $this->assertStringContainsString( 'ORDER BY RAND()', $wpdb->last_query, 'Check order is random' );
+
+               // This isn't allowed.
+               get_pages( array( 'sort_order' => 'rand' ) );
+               $this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present' );
+               $this->assertStringNotContainsString( 'RAND()', $wpdb->last_query, 'Check order is random is not present' );
+               $this->assertStringContainsString( 'DESC', $wpdb->last_query, 'Check DESC is random is not present' );
+
+               // 'none' is a valid value.
+               get_pages( array( 'sort_column' => 'none' ) );
+               $this->assertStringNotContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is not present' );
+               $this->assertStringNotContainsString( 'DESC', $wpdb->last_query, 'Check DESC is not present' );
+               $this->assertStringNotContainsString( 'ASC', $wpdb->last_query, 'Check ASC is not present' );
+
+               // False is a valid value.
+               get_pages( array( 'sort_column' => false ) );
+               $this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present if sort_column equal false is passed.' );
+
+               // Empty array() is a valid value.
+               get_pages( array( 'sort_column' => array() ) );
+               $this->assertStringContainsString( 'ORDER BY', $wpdb->last_query, 'Check orderby is present  if sort_column equals an empty array is passed.' );
+       }
+
+       /**
+        * @ticket 12821
+        * @covers ::get_pages
+        */
+       public function test_order() {
+               global $wpdb;
+
+               get_pages(
+                       array(
+                               'sort_column' => 'post_type',
+                       )
+               );
+               $this->assertStringContainsString(
+                       "ORDER BY $wpdb->posts.post_type ASC",
+                       $wpdb->last_query,
+                       'Check order is post type'
+               );
+
+               get_pages(
+                       array(
+                               'sort_column' => 'title',
+                               'sort_order'  => 'foo',
+                       )
+               );
+               $this->assertStringContainsString(
+                       "ORDER BY $wpdb->posts.post_title DESC",
+                       $wpdb->last_query,
+                       'Check order is default'
+               );
+
+               get_pages(
+                       array(
+                               'sort_order'  => 'asc',
+                               'sort_column' => 'date',
+                       )
+               );
+               $this->assertStringContainsString(
+                       "ORDER BY $wpdb->posts.post_date ASC",
+                       $wpdb->last_query,
+                       'Check order is post date'
+               );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>