diff --git a/src/PostObject.php b/src/PostObject.php index d234a72..b3345b2 100644 --- a/src/PostObject.php +++ b/src/PostObject.php @@ -113,7 +113,15 @@ function add_post_type_fields(\WP_Post_Type $post_type_object) 'code' => null, ]; - $slug = pll_get_post_language($post->ID, 'slug'); + $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) { + $post_id = wp_get_post_parent_id($post->ID); + } + + $slug = pll_get_post_language($post_id, 'slug'); if (!$slug) { return null; @@ -125,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' ); } @@ -190,10 +198,14 @@ function add_post_type_fields(\WP_Post_Type $post_type_object) 'resolve' => function (\WPGraphQL\Model\Post $post) { $posts = []; - foreach ( - pll_get_post_translations($post->ID) - as $lang => $post_id - ) { + if ($post->isPreview) { + $parent = wp_get_post_parent_id($post->ID); + $translations = pll_get_post_translations($parent); + } else { + $translations = pll_get_post_translations($post->ID); + } + + foreach ($translations as $lang => $post_id) { $translation = \WP_Post::get_instance($post_id); if (!$translation) { @@ -208,6 +220,11 @@ function add_post_type_fields(\WP_Post_Type $post_type_object) continue; } + // If fetching preview do not add the original as a translation + if ($post->isPreview && $parent === $translation->ID) { + continue; + } + $posts[] = new \WPGraphQL\Model\Post($translation); } diff --git a/tests/wpunit/PostPreviewQueryTest.php b/tests/wpunit/PostPreviewQueryTest.php new file mode 100644 index 0000000..716e4fb --- /dev/null +++ b/tests/wpunit/PostPreviewQueryTest.php @@ -0,0 +1,128 @@ +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, + ]); + // Note: The language of the preview post is not set at all in a real + // wp instance so we won't be setting it here neighter. wpgql-polylang + // must read the language from the original. + + $query = " + query Preview { + post(id: \"$post_id\", idType: DATABASE_ID, asPreview: true) { + title + language { + code + name + slug + } + } + } + "; + + 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'], 'FI'); + $this->assertEquals( + $result['data']['post']['language']['name'], + 'Suomi' + ); + $this->assertEquals($result['data']['post']['language']['slug'], 'fi'); + } + + public function testCanFetchTranslatedVersions() + { + $fi_post_id = wp_insert_post([ + 'post_title' => 'Finnish post version', + 'post_content' => '', + 'post_type' => 'post', + 'post_status' => 'publish', + ]); + pll_set_post_language($fi_post_id, 'fi'); + + $en_post_id = wp_insert_post([ + 'post_title' => 'English post version', + 'post_content' => '', + 'post_type' => 'post', + 'post_status' => 'publish', + ]); + pll_set_post_language($en_post_id, 'en'); + + pll_save_post_translations([ + 'en' => $en_post_id, + 'fi' => $fi_post_id, + ]); + + $preview_id = $this->factory()->post->create([ + 'post_title' => 'Preview post', + 'post_content' => 'Preview Content', + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $en_post_id, + ]); + + $query = " + query Preview { + post(id: \"$en_post_id\", idType: DATABASE_ID, asPreview: true) { + title + translations { + title + } + } + } + "; + + wp_set_current_user(1); + $data = do_graphql_request($query); + $this->assertArrayNotHasKey('errors', $data, print_r($data, true)); + error_log(print_r($data, true)); + + $expected = [ + 'title' => 'Preview post', + 'translations' => [ + [ + 'title' => 'Finnish post version', + ], + ], + ]; + + $this->assertEquals($expected, $data['data']['post']); + } +}