Query: Revert [55169].

Revert [55169] and mark #56689 as wontfix. `get_page_by_path` can not use `WP_Query`, as is results in fatel errors for those using `get_page_by_path` in the `pre_get_posts` action or `posts_pre_query` filter. This sadly means that `WP_Query` can never be used in `get_page_by_path`.

Props spacedmonkey, iandunn, mukesh27, joemcgill, adamsilverstein, otto42. 
See #56689.
Built from https://develop.svn.wordpress.org/trunk@55215


git-svn-id: http://core.svn.wordpress.org/trunk@54748 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-02-03 16:17:21 +00:00
parent e3e4d51e11
commit 203ba0cc28
2 changed files with 40 additions and 24 deletions

View File

@ -5675,6 +5675,8 @@ function get_page( $page, $output = OBJECT, $filter = 'raw' ) {
* *
* @since 2.1.0 * @since 2.1.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $page_path Page path. * @param string $page_path Page path.
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
* correspond to a WP_Post object, an associative array, or a numeric array, * correspond to a WP_Post object, an associative array, or a numeric array,
@ -5683,11 +5685,30 @@ function get_page( $page, $output = OBJECT, $filter = 'raw' ) {
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
*/ */
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
$page_path = rawurlencode( urldecode( $page_path ) ); global $wpdb;
$page_path = str_replace( '%2F', '/', $page_path );
$page_path = str_replace( '%20', ' ', $page_path ); $last_changed = wp_cache_get_last_changed( 'posts' );
$parts = explode( '/', trim( $page_path, '/' ) );
$parts = array_map( 'sanitize_title_for_query', $parts ); $hash = md5( $page_path . serialize( $post_type ) );
$cache_key = "get_page_by_path:$hash:$last_changed";
$cached = wp_cache_get( $cache_key, 'posts' );
if ( false !== $cached ) {
// Special case: '0' is a bad `$page_path`.
if ( '0' === $cached || 0 === $cached ) {
return;
} else {
return get_post( $cached, $output );
}
}
$page_path = rawurlencode( urldecode( $page_path ) );
$page_path = str_replace( '%2F', '/', $page_path );
$page_path = str_replace( '%20', ' ', $page_path );
$parts = explode( '/', trim( $page_path, '/' ) );
$parts = array_map( 'sanitize_title_for_query', $parts );
$escaped_parts = esc_sql( $parts );
$in_string = "'" . implode( "','", $escaped_parts ) . "'";
if ( is_array( $post_type ) ) { if ( is_array( $post_type ) ) {
$post_types = $post_type; $post_types = $post_type;
@ -5695,29 +5716,21 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
$post_types = array( $post_type, 'attachment' ); $post_types = array( $post_type, 'attachment' );
} }
$args = array( $post_types = esc_sql( $post_types );
'post_name__in' => $parts, $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
'post_type' => $post_types, $sql = "
'post_status' => 'all', SELECT ID, post_name, post_parent, post_type
'posts_per_page' => -1, FROM $wpdb->posts
'update_post_term_cache' => false, WHERE post_name IN ($in_string)
'update_post_meta_cache' => false, AND post_type IN ($post_type_in_string)
'no_found_rows' => true, ";
'orderby' => 'none',
);
$query = new WP_Query( $args ); $pages = $wpdb->get_results( $sql, OBJECT_K );
$posts = $query->get_posts();
$pages = array();
foreach ( $posts as $post ) {
$pages[ $post->ID ] = $post;
}
$revparts = array_reverse( $parts ); $revparts = array_reverse( $parts );
$foundid = 0; $foundid = 0;
foreach ( $pages as $page ) { foreach ( (array) $pages as $page ) {
if ( $page->post_name == $revparts[0] ) { if ( $page->post_name == $revparts[0] ) {
$count = 0; $count = 0;
$p = $page; $p = $page;
@ -5744,6 +5757,9 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
} }
} }
// We cache misses as well as hits.
wp_cache_set( $cache_key, $foundid, 'posts' );
if ( $foundid ) { if ( $foundid ) {
return get_post( $foundid, $output ); return get_post( $foundid, $output );
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.2-alpha-55214'; $wp_version = '6.2-alpha-55215';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.