Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for previews #30

Merged
merged 7 commits into from
Oct 1, 2020
Merged

Add support for previews #30

merged 7 commits into from
Oct 1, 2020

Conversation

vilpersson
Copy link

Returns languages and translations when querying post previews.

@esamattis
Copy link
Member

Merged master here and added tests.

For some reason the testCanHaveLanguageField passed even without your changes.

public function testCanHaveLanguageField()
{
$post_id = $this->factory()->post->create([
'post_title' => 'Original post',
'post_content' => '',
'post_type' => 'post',
'post_status' => 'publish',
]);
pll_set_post_language($post_id, 'fi');
$preview_id = $this->factory()->post->create([
'post_title' => 'Preview post',
'post_content' => 'Preview Content',
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
]);
pll_set_post_language($preview_id, 'en');
$query = "
query Preview {
post(id: \"$post_id\", idType: DATABASE_ID, asPreview: true) {
title
language {
code
}
}
}
";
wp_set_current_user(1);
$result = do_graphql_request($query);
$this->assertArrayNotHasKey('errors', $result, print_r($result, true));
$this->assertEquals($result['data']['post']['language']['code'], 'EN');
}

Can you eleborate when it failed before?

@esamattis
Copy link
Member

Oh wait. Now it fails in the CI 🤔

@vilpersson
Copy link
Author

vilpersson commented Oct 1, 2020

Are you sure that Polylang is setting the language of the preview post?

After creating preview, the newly created revision does not seem get the term relationship to the language taxomony, and this query will return null

query PREVIEW_QUERY {
    page(id: 7844, idType: DATABASE_ID, asPreview: true) {
        title
        language {
            code
        }
    }
}

If you go and check the post id of the revision, and set the language via pll_set_post_language function, then the you'll get correct language.

@esamattis
Copy link
Member

esamattis commented Oct 1, 2020

It is explicitly set here in the test:

https://github.com/valu-digital/wp-graphql-polylang/pull/30/files#diff-c9b04d9613c130bcf51b35ed35bf1490R42

But I'll test this in a real WP env. Unit testing Polylang correctly is actually quite hard since it's initialization so tied to the normal WP boot up.

@esamattis
Copy link
Member

esamattis commented Oct 1, 2020

Ah. It seems that in a real WP instance the language of the preview is not set at all so we need to fallback to the language of the original post.

@esamattis esamattis merged commit 0b40083 into valu-digital:master Oct 1, 2020
@esamattis
Copy link
Member

Merged, thank you!

Made couple additional fixes to this:

Must remove the original parent from the translations list to avoid duplicate entries
659d101

Also needed to use the parent id for the name and locale fields too
c521f30

@esamattis
Copy link
Member

esamattis commented Oct 1, 2020

Released in v0.5.0
https://github.com/valu-digital/wp-graphql-polylang/releases/tag/v0.5.0

@vilpersson
Copy link
Author

@esamattis Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants