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 [53941]). 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 #12821. Built from https://develop.svn.wordpress.org/trunk@55569 git-svn-id: http://core.svn.wordpress.org/trunk@55081 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
90b5db421c
commit
7dde8c4440
|
@ -5886,9 +5886,8 @@ function get_page_uri( $page = 0 ) {
|
|||
/**
|
||||
* Retrieves an array of pages (or hierarchical post type items).
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @since 6.3.0 Use WP_Query internally.
|
||||
*
|
||||
* @param array|string $args {
|
||||
* Optional. Array or string of arguments to retrieve pages.
|
||||
|
@ -5928,8 +5927,6 @@ function get_page_uri( $page = 0 ) {
|
|||
* supported by the post type.
|
||||
*/
|
||||
function get_pages( $args = array() ) {
|
||||
global $wpdb;
|
||||
|
||||
$defaults = array(
|
||||
'child_of' => 0,
|
||||
'sort_order' => 'ASC',
|
||||
|
@ -5978,50 +5975,35 @@ function get_pages( $args = array() ) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// $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' );
|
||||
$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,
|
||||
);
|
||||
|
||||
$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 = '';
|
||||
if ( ! empty( $parsed_args['include'] ) ) {
|
||||
$child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
|
||||
$parent = -1;
|
||||
$exclude = '';
|
||||
$meta_key = '';
|
||||
$meta_value = '';
|
||||
unset( $query_args['post__not_in'], $query_args['meta_key'], $query_args['meta_value'] );
|
||||
$hierarchical = false;
|
||||
$incpages = wp_parse_id_list( $parsed_args['include'] );
|
||||
if ( ! empty( $incpages ) ) {
|
||||
$inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')';
|
||||
}
|
||||
$query_args['post__in'] = wp_parse_id_list( $parsed_args['include'] );
|
||||
}
|
||||
|
||||
$exclusions = '';
|
||||
if ( ! empty( $exclude ) ) {
|
||||
$expages = wp_parse_id_list( $exclude );
|
||||
if ( ! empty( $expages ) ) {
|
||||
$exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')';
|
||||
}
|
||||
}
|
||||
|
||||
$author_query = '';
|
||||
if ( ! empty( $parsed_args['authors'] ) ) {
|
||||
$post_authors = wp_parse_list( $parsed_args['authors'] );
|
||||
|
||||
if ( ! empty( $post_authors ) ) {
|
||||
$query_args['author__in'] = array();
|
||||
foreach ( $post_authors as $post_author ) {
|
||||
// Do we have an author id or an author login?
|
||||
if ( 0 == (int) $post_author ) {
|
||||
|
@ -6034,136 +6016,37 @@ function get_pages( $args = array() ) {
|
|||
}
|
||||
$post_author = $post_author->ID;
|
||||
}
|
||||
|
||||
if ( '' === $author_query ) {
|
||||
$author_query = $wpdb->prepare( ' post_author = %d ', $post_author );
|
||||
} else {
|
||||
$author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author );
|
||||
$query_args['author__in'][] = $post_author;
|
||||
}
|
||||
}
|
||||
if ( '' !== $author_query ) {
|
||||
$author_query = " AND ($author_query)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_array( $parent ) ) {
|
||||
$post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
|
||||
$post_parent__in = array_map( 'absint', (array) $parent );
|
||||
if ( ! empty( $post_parent__in ) ) {
|
||||
$where .= " AND post_parent IN ($post_parent__in)";
|
||||
$query_args['post_parent__in'] = $post_parent__in;
|
||||
}
|
||||
} elseif ( $parent >= 0 ) {
|
||||
$where .= $wpdb->prepare( ' AND post_parent = %d ', $parent );
|
||||
$query_args['post_parent'] = $parent;
|
||||
}
|
||||
|
||||
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'] );
|
||||
$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'] );
|
||||
}
|
||||
|
||||
$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;
|
||||
$order = $parsed_args['sort_order'];
|
||||
if ( $order ) {
|
||||
$query_args['order'] = $order;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
$sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";
|
||||
|
||||
$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;
|
||||
|
||||
if ( ! empty( $number ) ) {
|
||||
$query .= ' LIMIT ' . $offset . ',' . $number;
|
||||
$query_args['posts_per_page'] = $number;
|
||||
}
|
||||
|
||||
$pages = $wpdb->get_results( $query );
|
||||
|
||||
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 );
|
||||
$query = new WP_Query( $query_args );
|
||||
$pages = $query->get_posts();
|
||||
|
||||
if ( $child_of || $hierarchical ) {
|
||||
$pages = get_page_children( $child_of, $pages );
|
||||
|
@ -6186,16 +6069,6 @@ function get_pages( $args = array() ) {
|
|||
}
|
||||
}
|
||||
|
||||
$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 );
|
||||
|
||||
/**
|
||||
* Filters the retrieved list of pages.
|
||||
*
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.3-alpha-55568';
|
||||
$wp_version = '6.3-alpha-55569';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue