From f6d22628572acb82e7787eb93af7c7420592eda6 Mon Sep 17 00:00:00 2001 From: Ville Huumo Date: Tue, 29 Sep 2020 12:39:19 +0300 Subject: [PATCH 1/6] Add support for previews --- src/PostObject.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/PostObject.php b/src/PostObject.php index d234a72..ed2f177 100644 --- a/src/PostObject.php +++ b/src/PostObject.php @@ -113,7 +113,12 @@ function add_post_type_fields(\WP_Post_Type $post_type_object) 'code' => null, ]; - $slug = pll_get_post_language($post->ID, 'slug'); + 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'); + } if (!$slug) { return null; @@ -190,8 +195,15 @@ function add_post_type_fields(\WP_Post_Type $post_type_object) 'resolve' => function (\WPGraphQL\Model\Post $post) { $posts = []; + 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 ( - pll_get_post_translations($post->ID) + $translations as $lang => $post_id ) { $translation = \WP_Post::get_instance($post_id); From bebf17a1c4a253498e84bd00584db7a6ee4aa810 Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Wed, 30 Sep 2020 18:39:50 +0300 Subject: [PATCH 2/6] Add preview tests --- tests/wpunit/PostPreviewQueryTest.php | 119 ++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 tests/wpunit/PostPreviewQueryTest.php diff --git a/tests/wpunit/PostPreviewQueryTest.php b/tests/wpunit/PostPreviewQueryTest.php new file mode 100644 index 0000000..5c4049d --- /dev/null +++ b/tests/wpunit/PostPreviewQueryTest.php @@ -0,0 +1,119 @@ +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'); + } + + 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']); + } +} From 659d1011c704fb19f61c6c6c24f80c7c0eca9a3d Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Wed, 30 Sep 2020 18:45:36 +0300 Subject: [PATCH 3/6] Fix 'translations' when fetching preview --- src/PostObject.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/PostObject.php b/src/PostObject.php index ed2f177..a14f6af 100644 --- a/src/PostObject.php +++ b/src/PostObject.php @@ -220,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); } From c6978580d80f75ae8ce0d9762269a9dc5c6a0021 Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Wed, 30 Sep 2020 18:50:28 +0300 Subject: [PATCH 4/6] Fix test file name --- tests/wpunit/PostPreviewQueryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wpunit/PostPreviewQueryTest.php b/tests/wpunit/PostPreviewQueryTest.php index 5c4049d..3c139d8 100644 --- a/tests/wpunit/PostPreviewQueryTest.php +++ b/tests/wpunit/PostPreviewQueryTest.php @@ -3,7 +3,7 @@ // XXX: Can we autoload this somehow? require_once __DIR__ . '/PolylangUnitTestCase.php'; -class PostObjectQueryTest extends PolylangUnitTestCase +class PostPreviewQueryTest extends PolylangUnitTestCase { static function wpSetUpBeforeClass() { From 3af0d56f5c2f093315b94dd4fdf8e119b747804b Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Thu, 1 Oct 2020 10:45:32 +0300 Subject: [PATCH 5/6] Fix preview language.code test --- tests/wpunit/PostPreviewQueryTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/wpunit/PostPreviewQueryTest.php b/tests/wpunit/PostPreviewQueryTest.php index 3c139d8..ac16b94 100644 --- a/tests/wpunit/PostPreviewQueryTest.php +++ b/tests/wpunit/PostPreviewQueryTest.php @@ -39,7 +39,9 @@ public function testCanHaveLanguageField() 'post_status' => 'inherit', 'post_parent' => $post_id, ]); - pll_set_post_language($preview_id, 'en'); + // 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 { @@ -55,7 +57,7 @@ public function testCanHaveLanguageField() 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'); + $this->assertEquals($result['data']['post']['language']['code'], 'FI'); } public function testCanFetchTranslatedVersions() From c521f30fb2d852987ed28f6ccbf1fc76d6a0b741 Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Thu, 1 Oct 2020 10:51:00 +0300 Subject: [PATCH 6/6] Fix language.name and language.slug in previews --- src/PostObject.php | 20 ++++++++++---------- tests/wpunit/PostPreviewQueryTest.php | 7 +++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/PostObject.php b/src/PostObject.php index a14f6af..b3345b2 100644 --- a/src/PostObject.php +++ b/src/PostObject.php @@ -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; } @@ -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' ); } @@ -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) { diff --git a/tests/wpunit/PostPreviewQueryTest.php b/tests/wpunit/PostPreviewQueryTest.php index ac16b94..716e4fb 100644 --- a/tests/wpunit/PostPreviewQueryTest.php +++ b/tests/wpunit/PostPreviewQueryTest.php @@ -49,6 +49,8 @@ public function testCanHaveLanguageField() title language { code + name + slug } } } @@ -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()