Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ErikBernskiold/9349e02cc850a2d2e69b4a6dd780c0f0 to your computer and use it in GitHub Desktop.
Save ErikBernskiold/9349e02cc850a2d2e69b4a6dd780c0f0 to your computer and use it in GitHub Desktop.
From MultiLingualPress support:
Thanks for contacting us. I'll help you to solve your problem.
First of all sorry about the api, actually we are working on them but we haven't realized the entire api interface, but things can be done with a bit of effort.
MLP use different classes that allow you to connect terms and post by passing Source/Remote site Ids and Source/Remote post/term Ids.
It is also possible to do that by making an ajax post request but the two solutions are actually slightly different.
Going to explain in deeper.
MLP use a container that allow you to get shared instances of object and to be able to perform some task you have to pick the instances from there because it's also resolve some dependencies
that otherwise you'll need to resolve manually.
The way to retrieve a shared instance from the container is done by calling `resolve` function and passing the FQ name of the class.
Es. `resolve(ContentRelations::class)`, internally MLP will resolve `ContentRelations` to `Inpsyde\MultilingualPress\Api\WpdbContentRelations`
Only shared instances can be resolved.
To create a relationship for posts or terms MLP use the following objects:
*Inpsyde\MultilingualPress\Framework\Api\ContentRelations*
This interface allow you to manage the relationships, it's basically a CRUD. It's implementation is `Inpsyde\MultilingualPress\Api\WpdbContentRelations`.
*Inpsyde\MultilingualPress\TranslationUi\Post\PostRelationSaveHelper*
and
*Inpsyde\MultilingualPress\TranslationUi\Term\TermRelationSaveHelper*
Those helper classes will help you on create the relationships of various parts of a post or term, like relate two posts, synchronize a thumbnail and or terms assigned to a post.
Them also help you to sync metadata.
*Inpsyde\MultilingualPress\TranslationUi\Post\RelationshipContext*
and
*Inpsyde\MultilingualPress\TranslationUi\Term\RelationshipContext*
The last element you need to have is a relationship context, this is basically a data class, containing Source Site Id, Source post Id, Remote Site Id, Remote post Id along with posts objects and some utility method
such as `hasRemotePost` that help you to know if the remote post exists or not.
The same for Term relationship context.
To create a programmatic relationship between two posts or terms without the need to sync thumbnail, terms o metadata is simple, here an example.
```
// Somewhere on top of the page
use Inpsyde\MultilingualPress\Framework\Api\ContentRelations;
use Inpsyde\MultilingualPress\TranslationUi\Post\PostRelationSaveHelper;
use Inpsyde\MultilingualPress\TranslationUi\Post\RelationshipContext;
$contentRelations = resolve(ContentRelations::class);
$relationShipHelper = new PostRelationSaveHelper($contentRelations);
$context = new RelationshipContext(
[
RelationshipContext::REMOTE_POST_ID => 1,
RelationshipContext::REMOTE_SITE_ID => 2,
RelationshipContext::SOURCE_POST_ID => 1,
RelationshipContext::SOURCE_SITE_ID => 1,
]
);
$related = $relationShipHelper->relatePosts($context);
```
And for make a relationship between terms
```
// Somewhere on top of the page
use Inpsyde\MultilingualPress\Framework\Api\ContentRelations;
use Inpsyde\MultilingualPress\TranslationUi\Term\TermRelationSaveHelper;
use Inpsyde\MultilingualPress\TranslationUi\Term\RelationshipContext;
$contentRelations = resolve(ContentRelations::class);
$relationShipHelper = new TermRelationSaveHelper($contentRelations);
$context = new RelationshipContext(
[
RelationshipContext::SOURCE_SITE_ID => 1,
RelationshipContext::SOURCE_TERM_ID => 34,
RelationshipContext::REMOTE_TERM_ID => 1,
RelationshipContext::REMOTE_SITE_ID => 2,
]
);
$related = $relationShipHelper->relateTerms($context);
```
Obviously the logic could be more complex, probably you want to check if the post exists, it's public or draft, isn't in the trash etc...
Based on that you can also create a relationship based on a RelationshipContext build from a POST request by using `\Inpsyde\MultilingualPress\TranslationUi\Term\Ajax\ContextBuilder`.
The class helps you on create a `RelationshipContext` for terms from an ajax request.
If you have any question please don't hesitate to contact us.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment