Skip to content

Commit

Permalink
Fix language.name and language.slug in previews
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Oct 1, 2020
1 parent 3af0d56 commit c521f30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,16 @@ function add_post_type_fields(\WP_Post_Type $post_type_object)
'code' => null,
];

$post_id = $post->ID;

// The language of the preview post is not set at all so we
// must get the language using the original post id
if ($post->isPreview) {
$parent = wp_get_post_parent_id($post->ID);
$slug = pll_get_post_language($parent, 'slug');
} else {
$slug = pll_get_post_language($post->ID, 'slug');
$post_id = wp_get_post_parent_id($post->ID);
}

$slug = pll_get_post_language($post_id, 'slug');

if (!$slug) {
return null;
}
Expand All @@ -130,14 +133,14 @@ function add_post_type_fields(\WP_Post_Type $post_type_object)

if (isset($fields['name'])) {
$language['name'] = pll_get_post_language(
$post->ID,
$post_id,
'name'
);
}

if (isset($fields['locale'])) {
$language['locale'] = pll_get_post_language(
$post->ID,
$post_id,
'locale'
);
}
Expand Down Expand Up @@ -202,10 +205,7 @@ function add_post_type_fields(\WP_Post_Type $post_type_object)
$translations = pll_get_post_translations($post->ID);
}

foreach (
$translations
as $lang => $post_id
) {
foreach ($translations as $lang => $post_id) {
$translation = \WP_Post::get_instance($post_id);

if (!$translation) {
Expand Down
7 changes: 7 additions & 0 deletions tests/wpunit/PostPreviewQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function testCanHaveLanguageField()
title
language {
code
name
slug
}
}
}
Expand All @@ -58,6 +60,11 @@ public function testCanHaveLanguageField()
$result = do_graphql_request($query);
$this->assertArrayNotHasKey('errors', $result, print_r($result, true));
$this->assertEquals($result['data']['post']['language']['code'], 'FI');
$this->assertEquals(
$result['data']['post']['language']['name'],
'Suomi'
);
$this->assertEquals($result['data']['post']['language']['slug'], 'fi');
}

public function testCanFetchTranslatedVersions()
Expand Down

0 comments on commit c521f30

Please sign in to comment.