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

Support for accessing variations of a post in different languages through the REST API #5958

Closed
adrianosferreira opened this issue Apr 3, 2018 · 8 comments
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience REST API Interaction Related to REST API [Status] Needs More Info Follow-up required in order to be actionable. [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.

Comments

@adrianosferreira
Copy link

Issue Overview

On WPML, through a custom meta box in post edit page post.php?post=43&action=edit we let users add a translated version of the current post. This produces a new URL containing the lang argument, something like this:

post-new.php?lang=pt-br&post_type=page&trid=376&source_lang=en

Which allows us to save the language information in the database.

Recently we've added a new filter to hook into rest_url in order to convert the rest URL in the translated version, which basically appends the lang argument into the rest URL. That way we can later properly save the language information of the post.

However now trying the same thing with Gutenberg I've noticed some messed URL caused by our filter in rest_url:

http://wpml.local/wp-json/?lang=pt-brwp/v2/types/page&context=edit

This happens because Gutenberg simply appends into the already filtered rest_url the path that it needs, doing the request with this messed URL.

After a short discussion with @mcsf he said:

that could be a use-case for a hook in the mapping logic of withAPIData

Whatever you decide, we simply need to have a way to hook into Gutenberg background rest requests in order to properly add the lang argument and later save WPML language information.

@andreasciamanna
Copy link

We had similar issues in the past with the AJAX URL because third party plugins were using strings concatenation instead of proper URL manipulation functions.
Eventually, we had to give up and find a different approach, because we could not expect all these developers to fix their code.

I think that at least Gutenberg (and by extension, the WP-Core team) should avoid using simple strings concatenation when manipulating URLs.

Of course, provided that this is the case: I was trying to figure out where the URL gets changed by Gutenberg, but I got a bit lost.

Does the code rely on URL manipulation functions (PHP: wp_parse_url, add_query_arg, http_build_query, http_build_url etc. - JS: URL interface and URLSearchParams) or simple strings concatenation?

@danielbachhuber danielbachhuber added [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. [Feature] Extensibility The ability to extend blocks or the editing experience labels Apr 4, 2018
@danielbachhuber
Copy link
Member

Recently we've added a new filter to hook into rest_url in order to convert the rest URL in the translated version, which basically appends the lang argument into the rest URL. That way we can later properly save the language information of the post.

Another idea you could consider, albeit not necessarily a good one:

  • Register a wpml-json/ rewrite rule where you can pass the language as wpml-json/pt-br/wp/v2/posts.

The benefit of this approach: you'd be able to very cleanly introduce WPML functionality. The rewrite rule would introduce the language as a lang query var, and then could pass the request through to WP_REST_Server to have everything work as normal.

The downside of this approach: only one plugin gets to filter rest_url in this way.

@danielbachhuber danielbachhuber changed the title Be able to filter background rest requests Support for including custom query variables in REST requests Apr 4, 2018
@adrianosferreira
Copy link
Author

adrianosferreira commented Apr 4, 2018

Thanks, @danielbachhuber. This is the very first time I dig into Gutenberg code base, so take it easy hehe. I've just pushed a PR containing a tiny fix that may resolve this question: #5976

@danielbachhuber
Copy link
Member

@adrianosferreira Have you considered registering language as an attribute on the Post resource, and achieving your goal that way?

@adrianosferreira
Copy link
Author

@danielbachhuber how? Can you give me one example?

@danielbachhuber danielbachhuber added this to the Merge Proposal milestone Apr 4, 2018
@danielbachhuber danielbachhuber changed the title Support for including custom query variables in REST requests Support for accessing variations of a post in different languages through the REST API Apr 4, 2018
@danielbachhuber danielbachhuber added the REST API Interaction Related to REST API label Apr 4, 2018
@danielbachhuber
Copy link
Member

how? Can you give me one example?

I was referring to using register_rest_field() to register a field to the schema: https://developer.wordpress.org/reference/functions/register_rest_field/

However, in considering this further, the client would still need some way of querying for a specific post variation from the server (i.e. the query parameter you've mentioned before). I think we're back at square one.

Let's let this soak a bit and maybe a better solution will become more obvious with time.

@karmatosed karmatosed modified the milestones: Merge Proposal, Merge Proposal: Plugins Apr 12, 2018
@mtias mtias mentioned this issue Aug 1, 2018
16 tasks
@mtias mtias removed this from the Merge: Plugins milestone Oct 3, 2018
@danielbachhuber
Copy link
Member

@adrianosferreira Were you able to get to an acceptable implementation? If yes, can you share the details of how you ended up handling this? If no, can you describe where you're currently stuck?

@danielbachhuber danielbachhuber added the [Status] Needs More Info Follow-up required in order to be actionable. label Oct 23, 2018
@danielbachhuber
Copy link
Member

Closing for now. Feel free to follow-up with additional details if there's something you're stuck on.

https://github.com/polylang/polylang-gutenberg/issues/15 has a good amount of related detail on the topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience REST API Interaction Related to REST API [Status] Needs More Info Follow-up required in order to be actionable. [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.
Projects
None yet
Development

No branches or pull requests

5 participants