From 7918d5cd9830890ea3f1669706988e6f50e5c5f6 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 2 Oct 2013 19:41:19 +0000 Subject: [PATCH] Make `url_to_postid()` work for custom post type URLs. Use `get_post_types()` and `get_taxonomies()` instead of directly accessing globals. Adds unit test. Props faishal, for the globals fix. Fixes #19744. git-svn-id: https://develop.svn.wordpress.org/trunk@25659 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 4 ++-- src/wp-includes/query.php | 2 +- src/wp-includes/rewrite.php | 19 +++++++++++++++---- tests/phpunit/tests/rewrite.php | 12 ++++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index c3b9bf1d90..aaf4c32bfc 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -238,7 +238,7 @@ class WP { $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars); - foreach ( $GLOBALS['wp_post_types'] as $post_type => $t ) + foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) if ( $t->query_var ) $post_type_query_vars[$t->query_var] = $post_type; @@ -271,7 +271,7 @@ class WP { } // Convert urldecoded spaces back into + - foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) + foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) if ( $t->query_var && isset( $this->query_vars[$t->query_var] ) ) $this->query_vars[$t->query_var] = str_replace( ' ', '+', $this->query_vars[$t->query_var] ); diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 935d278569..409044372c 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -1735,7 +1735,7 @@ class WP_Query { ); } - foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) { + foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) { if ( 'post_tag' == $taxonomy ) continue; // Handled further down in the $q['tag'] block diff --git a/src/wp-includes/rewrite.php b/src/wp-includes/rewrite.php index 1c2b747e45..2b48cdf83c 100644 --- a/src/wp-includes/rewrite.php +++ b/src/wp-includes/rewrite.php @@ -338,6 +338,12 @@ function url_to_postid($url) { $url = trim($url, '/'); $request = $url; + $post_type_query_vars = array(); + + foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) { + if ( ! empty( $t->query_var ) ) + $post_type_query_vars[ $t->query_var ] = $post_type; + } // Look for matches. $request_match = $request; @@ -365,16 +371,21 @@ function url_to_postid($url) { // Filter out non-public query vars global $wp; - parse_str($query, $query_vars); + parse_str( $query, $query_vars ); $query = array(); foreach ( (array) $query_vars as $key => $value ) { - if ( in_array($key, $wp->public_query_vars) ) + if ( in_array( $key, $wp->public_query_vars ) ){ $query[$key] = $value; + if ( isset( $post_type_query_vars[$key] ) ) { + $query['post_type'] = $post_type_query_vars[$key]; + $query['name'] = $value; + } + } } // Do the query - $query = new WP_Query($query); - if ( !empty($query->posts) && $query->is_singular ) + $query = new WP_Query( $query ); + if ( ! empty( $query->posts ) && $query->is_singular ) return $query->post->ID; else return 0; diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php index 323076604e..a6c5417658 100644 --- a/tests/phpunit/tests/rewrite.php +++ b/tests/phpunit/tests/rewrite.php @@ -32,6 +32,18 @@ class Tests_Rewrite extends WP_UnitTestCase { $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); } + function test_url_to_postid_custom_post_type() { + delete_option( 'rewrite_rules' ); + + $post_type = rand_str( 12 ); + register_post_type( $post_type, array( 'public' => true ) ); + + $id = $this->factory->post->create( array( 'post_type' => $post_type ) ); + $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); + + _unregister_post_type( $post_type ); + } + function test_url_to_postid_hierarchical() { $parent_id = $this->factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) );