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

nodeByURI not returning correct node when translations have same slug #67

Open
kuuak opened this issue Jan 21, 2022 · 7 comments
Open

Comments

@kuuak
Copy link

kuuak commented Jan 21, 2022

Hi,

First: thanks for the plugin which is great! 👏

My setup:

  • WP 5.8.3
  • Polylang Pro 3.1.2 (the pro version gives the possibility to share the same slug between translations)
  • WPGraphql 1.6.12
  • WPGraphql Polylang 0.6.0
  • Active theme: TwentyTwentyOne 1.4.0
  • Three languages set: FR*, EN & DE (FR is the default)

When I have posts or pages sharing the same slug between translations, the RootQuery nodeByUri field does not return the correct node according to the localized URI.

As you can see the queries in the GraphQLi (screenshot below), the three queries return the same node which can be any of the translated node.

Capture d’écran 2022-01-21 à 17 49 38 Capture d’écran 2022-01-21 à 17 49 59

I could find a workaround to make it work, but it feels really dirty:

// Add lang arg to $extra_query_vars to nodeByURI rootquery
add_filter( 'graphql_RootQuery_fields',  function ($fields) {
	if ( isset($fields['nodeByUri']) ) {
		$fields['nodeByUri']['resolve'] = function ( $root, $args, AppContext $context ) {
			if ( empty( $args['uri'] ) ) return null;

			$extra_query_vars = [];
			$parsed_url = wp_parse_url( $args['uri'] );

			// If we match a language in the URI
			if ( preg_match("/^\/?(\w{2})\/.+/", $parsed_url['path'], $matches) ) {
				$extra_query_vars['lang'] = $matches[1];
				$extra_query_vars['is_graphql_nodebyuri'] = true;
			}

			return $context->node_resolver->resolve_uri( $args['uri'], $extra_query_vars );
		};
	}
	return $fields;
}, 20);

// rewrite SQL query to include lang for pages & posts
add_filter( 'query', function query_pagename_nodebyuri( $query ) {
	global $wp;

	if ( isset($wp->query_vars['is_graphql_nodebyuri']) && isset($wp->query_vars['lang']) ) {
		global $wpdb;
		if ( isset($wp->query_vars['pagename']) ) {
			$pagename_in = implode("','", explode('/', $wp->query_vars['pagename']));
			if ( preg_match("/post_name IN \('$pagename_in'\)/", $query) ) {
				$query = "
					SELECT p.ID, p.post_name, p.post_parent, p.post_type
					FROM {$wpdb->posts} AS p
						LEFT JOIN {$wpdb->term_relationships} AS tr ON p.ID=tr.object_id
						LEFT JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id=tt.term_taxonomy_id
						LEFT JOIN {$wpdb->terms} AS t ON tt.term_id=t.term_id
					WHERE
						p.post_name IN ('$pagename_in')
						AND p.post_type IN ('post','page','attachment')
						AND tt.taxonomy='language'
						AND t.slug='{$wp->query_vars['lang']}'
				";
			}
		}
		else if ( isset($wp->query_vars['name']) && preg_match("/post_name IN \('{$wp->query_vars['name']}'\)/", $query) ) {
			$query = "
				SELECT p.ID, p.post_name, p.post_parent, p.post_type
				FROM {$wpdb->posts} AS p
					LEFT JOIN {$wpdb->term_relationships} AS tr ON p.ID=tr.object_id
					LEFT JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id=tt.term_taxonomy_id
					LEFT JOIN {$wpdb->terms} AS t ON tt.term_id=t.term_id
				WHERE
					p.post_name IN ('{$wp->query_vars['name']}')
					AND post_type IN ('post','attachment')
					AND tt.taxonomy='language'
					AND t.slug='{$wp->query_vars['lang']}'
			";
		}
} );

However, this is really risky business as I'm changing the SQL query to the DB to handle the language. So I'm wondering if there is a better solution to fix this translation issues with nodeByUri ??

@smesterheide
Copy link

Thanks for the problem description. I am facing the same issue.

@smesterheide
Copy link

I am reposting the conversation I had with the Polylang team regading the issue:

Sébastien replied

Feb 14, 16:33
Hello,

Posting here or in our GH is the same, we do not support GraphQL.
As per our lead developer the idea developed on the issue you mentioned
is the right one.

Regards

Jan replied

Feb 14, 12:42
Hi Sebastian,

thanks for your message.

Do you mind if I post this message verbatim to Github?

Best regards,

Sébastien replied

Feb 14, 9:30
Hello,

Thank you for contacting us.

As you know, "WP Graphql Polylang" is 13rd party plugin we're not
involved in. We do not know how it works and won't be able to help
without digging in the code... and it's not in our current roadmap.
If "WP Graphql Polylang" developers need help to solve this, they can of
course contact us and our developers will try to help the best as they
can. We can also provide them Polylang pro license to correct this.

I suggest you to contact & check with them how to fix this.

Regards

Jan sent a message

Feb 13, 14:13
Hello, I am using Polylang in a decoupled scenario with GraphQL
connecting the frontend to WP. There is a 3rd party plugin for Polylang
you might be familiar with:
https://github.com/valu-digital/wp-graphql-polylang

Unfortunately I cannot use the Pro feature of slug sharing as described
in the ongoing issue:
#67

I am hoping you can contribute to the GraphQL extension and help fix the
issue.

Best regards
Jan

@evrenbal
Copy link

evrenbal commented May 4, 2022

The same bug applies to all queries with an URI even for tags. One may use different slugs work posts or taxonomies to overcome this issue, but it even affects tags! It would be great if we can find a solution!

@krishaamer
Copy link

+1

@predaytor
Copy link

+1 :(

@ehnsio
Copy link

ehnsio commented Nov 20, 2023

+1

1 similar comment
@FloLech
Copy link

FloLech commented Aug 14, 2024

+1

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

No branches or pull requests

7 participants