Skip to content

Commit

Permalink
Merge pull request #2490 from jasonbahl/bugfix/#2486-page-by-uri-for-…
Browse files Browse the repository at this point in the history
…posts-page

fix: bug when querying page set as "Posts Page"
  • Loading branch information
jasonbahl committed Sep 6, 2022
2 parents e2509d6 + e4c4e28 commit b89a31f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/Data/NodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {

} else {

if ( isset( $this->wp->query_vars['nodeType'] ) && 'Page' === $this->wp->query_vars['nodeType'] ) {
if ( isset( $this->wp->query_vars['nodeType'] ) && 'ContentNode' === $this->wp->query_vars['nodeType'] ) {
return null;
}

Expand Down Expand Up @@ -424,6 +424,11 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {
}

if ( (int) get_option( 'page_for_posts', 0 ) === $post->ID ) {

if ( isset( $extra_query_vars['nodeType'] ) && 'ContentNode' === $extra_query_vars['nodeType'] ) {
return null;
}

return $this->context->get_loader( 'post_type' )->load_deferred( 'post' );
}

Expand All @@ -443,7 +448,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {
} elseif ( isset( $this->wp->query_vars['post_type'] ) ) {

// If the query is asking for a Page nodeType with the home uri, try and resolve it.
if ( '/' === $this->wp->query_vars['uri'] && ( isset( $this->wp->query_vars['nodeType'] ) && 'Page' === $this->wp->query_vars['nodeType'] ) ) {
if ( '/' === $this->wp->query_vars['uri'] && ( isset( $this->wp->query_vars['nodeType'] ) && 'ContentNode' === $this->wp->query_vars['nodeType'] ) ) {

// If the post type is not a page, but the uri is for the home page, we can return null now
if ( 'page' !== $this->wp->query_vars['post_type'] ) {
Expand All @@ -456,7 +461,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {
}

// If the query is asking for a Page nodeType with the uri, try and resolve it.
if ( isset( $this->wp->query_vars['nodeType'] ) && 'Page' === $this->wp->query_vars['nodeType'] && isset( $this->wp->query_vars['uri'] ) ) {
if ( isset( $this->wp->query_vars['nodeType'] ) && 'ContentNode' === $this->wp->query_vars['nodeType'] && isset( $this->wp->query_vars['uri'] ) ) {
$post_type = $this->wp->query_vars['post_type'];

$post = get_page_by_path( $this->wp->query_vars['uri'], 'OBJECT', $post_type ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_page_by_path_get_page_by_path
Expand Down
9 changes: 9 additions & 0 deletions src/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,15 @@ protected function init() {
return '/';
}

// if the page is set as the posts page
// the page node itself is not identifiable
// by URI. Instead, the uri would return the
// Post content type as that uri
// represents the blog archive instead of a page
if ( true === $this->isPostsPage ) {
return null;
}

return ! empty( $uri ) ? str_ireplace( home_url(), '', $uri ) : null;
},
'commentCount' => function () {
Expand Down
4 changes: 2 additions & 2 deletions src/Type/ObjectType/RootQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public static function register_post_object_fields() {
[
'post_type' => $post_type_object->name,
'archive' => false,
'nodeType' => 'Page',
'nodeType' => 'ContentNode',
]
);
case 'database_id':
Expand Down Expand Up @@ -801,7 +801,7 @@ function ( $post ) use ( $post_type_object ) {
[
'post_type' => $post_type_object->name,
'archive' => false,
'nodeType' => 'Page',
'nodeType' => 'ContentNode',
]
);
} elseif ( ! empty( $args['slug'] ) ) {
Expand Down
39 changes: 39 additions & 0 deletions tests/wpunit/PageByUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,43 @@ function testPageByUriWithCustomPermalinks() {
$this->assertSame( $this->page, $actual['data']['page']['databaseId'] );
$this->assertSame( str_ireplace( home_url(), '', get_permalink( $this->page ) ), $actual['data']['page']['uri'] );
}

public function testQueryPageForPostsByUriReturnsNull() {


$query = '
query GetPageByUri($id:ID!) {
page(id: $id, idType: URI) {
__typename
}
}
';

$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => '/' . get_post( $this->page )->post_name,
]
]);

$this->assertQuerySuccessful( $actual, [
$this->expectedField( 'page.__typename', 'Page' ),
]);

// set the page as the page_for_posts
update_option( 'page_for_posts', $this->page );

$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => '/' . get_post( $this->page )->post_name,
]
]);

$this->assertQuerySuccessful( $actual, [
$this->expectedField( 'page', self::IS_NULL ),
]);

}

}

0 comments on commit b89a31f

Please sign in to comment.