In WP::parse_request(), don't add query vars of non-viewable post types to WP::public_query_vars. In register_post_type(), don't add query vars of non-viewable post types to WP::public_query_vars.

In `_unregister_post_type()` (unit tests), don't add query vars of non-viewable post types to `WP::public_query_vars`.

Adds unit test.

Fixes #30018.


git-svn-id: https://develop.svn.wordpress.org/trunk@34215 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-09-15 18:53:12 +00:00
parent 6493d211d2
commit 28426c041c
4 changed files with 25 additions and 5 deletions

View File

@ -261,9 +261,11 @@ class WP {
*/ */
$this->public_query_vars = apply_filters( 'query_vars', $this->public_query_vars ); $this->public_query_vars = apply_filters( 'query_vars', $this->public_query_vars );
foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) {
if ( $t->query_var ) if ( is_post_type_viewable( $t ) && $t->query_var ) {
$post_type_query_vars[$t->query_var] = $post_type; $post_type_query_vars[$t->query_var] = $post_type;
}
}
foreach ( $this->public_query_vars as $wpvar ) { foreach ( $this->public_query_vars as $wpvar ) {
if ( isset( $this->extra_query_vars[$wpvar] ) ) if ( isset( $this->extra_query_vars[$wpvar] ) )

View File

@ -1072,13 +1072,16 @@ function register_post_type( $post_type, $args = array() ) {
add_post_type_support( $post_type, array( 'title', 'editor' ) ); add_post_type_support( $post_type, array( 'title', 'editor' ) );
} }
if ( false !== $args->query_var && ! empty( $wp ) ) { if ( false !== $args->query_var ) {
if ( true === $args->query_var ) if ( true === $args->query_var )
$args->query_var = $post_type; $args->query_var = $post_type;
else else
$args->query_var = sanitize_title_with_dashes( $args->query_var ); $args->query_var = sanitize_title_with_dashes( $args->query_var );
if ( $wp && is_post_type_viewable( $args ) ) {
$wp->add_query_var( $args->query_var ); $wp->add_query_var( $args->query_var );
} }
}
if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
if ( ! is_array( $args->rewrite ) ) if ( ! is_array( $args->rewrite ) )

View File

@ -362,7 +362,7 @@ function _cleanup_query_vars() {
} }
foreach ( get_post_types( array() , 'objects' ) as $t ) { foreach ( get_post_types( array() , 'objects' ) as $t ) {
if ( ! empty( $t->query_var ) ) if ( is_post_type_viewable( $t ) && ! empty( $t->query_var ) )
$GLOBALS['wp']->add_query_var( $t->query_var ); $GLOBALS['wp']->add_query_var( $t->query_var );
} }
} }

View File

@ -144,6 +144,21 @@ class Tests_Rewrite extends WP_UnitTestCase {
$this->assertEquals( array( 'page' => '', 'pagename' => 'match/page' ), $GLOBALS['wp']->query_vars ); $this->assertEquals( array( 'page' => '', 'pagename' => 'match/page' ), $GLOBALS['wp']->query_vars );
} }
/**
* @ticket 30018
*/
function test_parse_request_home_path_non_public_type() {
register_post_type( 'foo', array( 'public' => false ) );
$url = add_query_arg( 'foo', '1', home_url() );
$this->go_to( $url );
_unregister_post_type( 'foo' );
$this->assertEquals( array(), $GLOBALS['wp']->query_vars );
}
function test_url_to_postid_dupe_path() { function test_url_to_postid_dupe_path() {
update_option( 'home', home_url('/example/') ); update_option( 'home', home_url('/example/') );