Skip to content

Commit

Permalink
Merge pull request #2851 from jasonbahl/fix/#2847-not-resolving-unico…
Browse files Browse the repository at this point in the history
…de-slugs

fix: querying posts by slug or uri when the post name is non-ascii
  • Loading branch information
jasonbahl committed Jul 12, 2023
2 parents f711cc1 + fa8d9cb commit 85852e2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Data/NodeResolver.php
Expand Up @@ -74,9 +74,9 @@ public function validate_post( WP_Post $post ) {
return $post;
}

// if the uri doesn't have the post's name or ID in it, we must've found something we didn't expect
// if the uri doesn't have the post's urlencoded name or ID in it, we must've found something we didn't expect
// so we will return null
if ( false === strpos( $this->wp->query_vars['uri'], $post->post_name ) && false === strpos( $this->wp->query_vars['uri'], (string) $post->ID ) ) {
if ( false === strpos( $this->wp->query_vars['uri'], (string) $post->ID ) && false === strpos( $this->wp->query_vars['uri'], urldecode( sanitize_title( $post->post_name ) ) ) ) {
return null;
}

Expand Down Expand Up @@ -210,6 +210,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {
return $node;
}


// Resolve Post Objects.
if ( $queried_object instanceof WP_Post ) {
// If Page for Posts is set, we need to return the Page archive, not the page.
Expand All @@ -230,6 +231,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {

// Validate the post before returning it.
if ( ! $this->validate_post( $queried_object ) ) {

return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Model/Post.php
Expand Up @@ -527,7 +527,7 @@ protected function init() {
return ! empty( $this->data->ping_status ) ? $this->data->ping_status : null;
},
'slug' => function () {
return ! empty( $this->data->post_name ) ? $this->data->post_name : null;
return ! empty( $this->data->post_name ) ? urldecode( $this->data->post_name ) : null;
},
'template' => function () {

Expand Down Expand Up @@ -683,7 +683,7 @@ protected function init() {
$link = get_permalink( $this->data->ID );
}

return ! empty( $link ) ? $link : null;
return ! empty( $link ) ? urldecode( $link ) : null;
},
'uri' => function () {
$uri = $this->link;
Expand Down
100 changes: 100 additions & 0 deletions tests/wpunit/PostObjectQueriesTest.php
Expand Up @@ -2243,4 +2243,104 @@ public function testQueryNonPostsAsPostReturnsNull() {
$this->assertNull( $actual['data']['post'] );
}

// create a post using emoji
public function testQueryPostBySlugWithEmojiSlug() {

$raw_title = '🍌';

// the $encoded_slug will have a dash between words i.e. 'سلا-دنیا'
$encoded_slug = urldecode( sanitize_title( $raw_title ) );

$non_ascii_post = $this->factory()->post->create_and_get([
'post_title' => $raw_title,
'post_status' => 'publish',
'post_author' => $this->admin,
]);

codecept_debug( [
'$non_ascii_string' => $raw_title,
'$encoded_slug' => $encoded_slug,
'$post_name' => $non_ascii_post->post_name,
'post' => $non_ascii_post,
]);

$query = '
query getPostBySlug( $id: ID! ) {
post( id: $id idType: SLUG ) {
__typename
title
slug
databaseId
}
}
';

// query the post by (encoded) slug
$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => $encoded_slug, // سلام-دنیا //
]
]);

// assert that the response is what we expect
self::assertQuerySuccessful($actual, [
$this->expectedField( 'post.__typename', 'Post' ),
$this->expectedField( 'post.title', $raw_title ),
$this->expectedField( 'post.slug', $encoded_slug ),
$this->expectedField( 'post.databaseId', $non_ascii_post->ID ),
]);

}

// create a post using unicode
public function testQueryPostBySlugWithNonAsciiSlug() {

$raw_title = 'سلام دنیا';

// the $encoded_slug will have a dash between words i.e. 'سلا-دنیا'
$encoded_slug = urldecode( sanitize_title( $raw_title ) );

$non_ascii_post = $this->factory()->post->create_and_get([
'post_title' => $raw_title,
'post_status' => 'publish',
'post_author' => $this->admin,
]);

codecept_debug( [
'$non_ascii_string' => $raw_title,
'$encoded_slug' => $encoded_slug,
'$post_name' => $non_ascii_post->post_name,
'post' => $non_ascii_post,
]);

$query = '
query getPostBySlug( $id: ID! ) {
post( id: $id idType: SLUG ) {
__typename
title
slug
databaseId
}
}
';

// query the post by (encoded) slug
$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => $encoded_slug, // سلام-دنیا //
]
]);

// assert that the response is what we expect
self::assertQuerySuccessful($actual, [
$this->expectedField( 'post.__typename', 'Post' ),
$this->expectedField( 'post.title', $raw_title ),
$this->expectedField( 'post.slug', $encoded_slug ),
$this->expectedField( 'post.databaseId', $non_ascii_post->ID ),
]);

}

}

0 comments on commit 85852e2

Please sign in to comment.