get_page_by_path() diff
4 removals
82 lines
5 additions
83 lines
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
global $wpdb;
$last_changed = wp_cache_get_last_changed( 'posts' );
$last_changed = wp_cache_get_last_changed( 'posts' );
$hash = md5( $page_path . serialize( $post_type ) );
$hash = md5( $page_path . serialize( $post_type ) );
$cache_key = "get_page_by_path:$hash:$last_changed";
$cache_key = "get_page_by_path:$hash:$last_changed";
$cached = wp_cache_get( $cache_key, 'posts' );
$cached = wp_cache_get( $cache_key, 'post-queries' );
if ( false !== $cached ) {
if ( false !== $cached ) {
// Special case: '0' is a bad `$page_path`.
// Special case: '0' is a bad `$page_path`.
if ( '0' === $cached || 0 === $cached ) {
if ( '0' === $cached || 0 === $cached ) {
return;
return;
} else {
} else {
return get_post( $cached, $output );
return get_post( $cached, $output );
}
}
}
}
$page_path = rawurlencode( urldecode( $page_path ) );
$page_path = rawurlencode( urldecode( $page_path ) );
$page_path = str_replace( '%2F', '/', $page_path );
$page_path = str_replace( '%2F', '/', $page_path );
$page_path = str_replace( '%20', ' ', $page_path );
$page_path = str_replace( '%20', ' ', $page_path );
$parts = explode( '/', trim( $page_path, '/' ) );
$parts = explode( '/', trim( $page_path, '/' ) );
$parts = array_map( 'sanitize_title_for_query', $parts );
$parts = array_map( 'sanitize_title_for_query', $parts );
$escaped_parts = esc_sql( $parts );
$escaped_parts = esc_sql( $parts );
$in_string = "'" . implode( "','", $escaped_parts ) . "'";
$in_string = "'" . implode( "','", $escaped_parts ) . "'";
if ( is_array( $post_type ) ) {
if ( is_array( $post_type ) ) {
$post_types = $post_type;
$post_types = $post_type;
} else {
} else {
$post_types = array( $post_type, 'attachment' );
$post_types = array( $post_type, 'attachment' );
}
}
$post_types = esc_sql( $post_types );
$post_types = esc_sql( $post_types );
$post_type_in_string = "'" . implode( "','", $post_types ) . "'";
$post_type_in_string = "'" . implode( "','", $post_types ) . "'";
$sql = "
$sql = "
SELECT ID, post_name, post_parent, post_type
SELECT ID, post_name, post_parent, post_type
FROM $wpdb->posts
FROM $wpdb->posts
WHERE post_name IN ($in_string)
WHERE post_name IN ($in_string)
AND post_type IN ($post_type_in_string)
AND post_type IN ($post_type_in_string)
";
";
$pages = $wpdb->get_results( $sql, OBJECT_K );
$pages = $wpdb->get_results( $sql, OBJECT_K );
$revparts = array_reverse( $parts );
$revparts = array_reverse( $parts );
$foundid = 0;
$foundid = 0;
foreach ( (array) $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;
/*
/*
* Loop through the given path parts from right to left,
* Loop through the given path parts from right to left,
* ensuring each matches the post ancestry.
* ensuring each matches the post ancestry.
*/
*/
while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
$count++;
++$count;
$parent = $pages[ $p->post_parent ];
$parent = $pages[ $p->post_parent ];
if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) {
if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) {
break;
break;
}
}
$p = $parent;
$p = $parent;
}
}
if ( 0 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) {
if ( 0 == $p->post_parent && count( $revparts ) === $count + 1 && $p->post_name == $revparts[ $count ] ) {
$foundid = $page->ID;
$foundid = $page->ID;
if ( $page->post_type == $post_type ) {
if ( $page->post_type == $post_type ) {
break;
break;
}
}
}
}
}
}
}
}
// We cache misses as well as hits.
// We cache misses as well as hits.
wp_cache_set( $cache_key, $foundid, 'posts' );
wp_cache_set( $cache_key, $foundid, 'post-queries' );
if ( $foundid ) {
if ( $foundid ) {
return get_post( $foundid, $output );
return get_post( $foundid, $output );
}
}
return null;
return null;
}
}