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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'
);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down
128 changes: 128 additions & 0 deletions tests/wpunit/PostPreviewQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

// XXX: Can we autoload this somehow?
require_once __DIR__ . '/PolylangUnitTestCase.php';

class PostPreviewQueryTest extends PolylangUnitTestCase
{
static function wpSetUpBeforeClass()
{
parent::wpSetUpBeforeClass();

self::set_default_language('en_US');
self::create_language('en_US');
self::create_language('fr_FR');
self::create_language('fi');
self::create_language('de_DE_formal');
self::create_language('es_ES');
}

public function setUp(): void
{
parent::setUp();
}

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,
]);
// 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']);
}
}