From 28426c041c0f49e6b0f52f51a26e19ea3949c927 Mon Sep 17 00:00:00 2001
From: Scott Taylor <wonderboymusic@git.wordpress.org>
Date: Tue, 15 Sep 2015 18:53:12 +0000
Subject: [PATCH] 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
---
 src/wp-includes/class-wp.php       |  6 ++++--
 src/wp-includes/post-functions.php |  7 +++++--
 tests/phpunit/includes/utils.php   |  2 +-
 tests/phpunit/tests/rewrite.php    | 15 +++++++++++++++
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php
index 50d3638bdf..84111bca9f 100644
--- a/src/wp-includes/class-wp.php
+++ b/src/wp-includes/class-wp.php
@@ -261,9 +261,11 @@ class WP {
 		 */
 		$this->public_query_vars = apply_filters( 'query_vars', $this->public_query_vars );
 
-		foreach ( get_post_types( array(), 'objects' ) as $post_type => $t )
-			if ( $t->query_var )
+		foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) {
+			if ( is_post_type_viewable( $t ) && $t->query_var ) {
 				$post_type_query_vars[$t->query_var] = $post_type;
+			}
+		}
 
 		foreach ( $this->public_query_vars as $wpvar ) {
 			if ( isset( $this->extra_query_vars[$wpvar] ) )
diff --git a/src/wp-includes/post-functions.php b/src/wp-includes/post-functions.php
index 6e1d0e3742..2d9f90960c 100644
--- a/src/wp-includes/post-functions.php
+++ b/src/wp-includes/post-functions.php
@@ -1072,12 +1072,15 @@ function register_post_type( $post_type, $args = array() ) {
 		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 )
 			$args->query_var = $post_type;
 		else
 			$args->query_var = sanitize_title_with_dashes( $args->query_var );
-		$wp->add_query_var( $args->query_var );
+
+		if ( $wp && is_post_type_viewable( $args ) ) {
+			$wp->add_query_var( $args->query_var );
+		}
 	}
 
 	if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php
index 094b83b12e..a7aec495f9 100644
--- a/tests/phpunit/includes/utils.php
+++ b/tests/phpunit/includes/utils.php
@@ -362,7 +362,7 @@ function _cleanup_query_vars() {
 	}
 
 	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 );
 	}
 }
diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php
index cba783740d..d31a6d36b8 100644
--- a/tests/phpunit/tests/rewrite.php
+++ b/tests/phpunit/tests/rewrite.php
@@ -144,6 +144,21 @@ class Tests_Rewrite extends WP_UnitTestCase {
 		$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() {
 		update_option( 'home', home_url('/example/') );