Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Feature request: Copy post meta / manage separate meta translation #117

Open
JoryHogeveen opened this issue Jun 12, 2015 · 12 comments
Open
Labels
Milestone

Comments

@JoryHogeveen
Copy link

It would be awesome if one could manage the translations of post-metadata.
Optimal situation would be that one can select which metadata to sync and which to translate.

Is such a feature possible in the near future?
This could be troublesome with plugins like WooCommerce or Event Manager but I hope it can be done.
For me, this is a big reason to use this plugin.

@bueltge
Copy link
Member

bueltge commented Jun 12, 2015

Thanks for your requirements. The plugin can handle post meta data. But it is not possible out of the box. It is required, that you include the meta data.

For WooCommerce: we have an additional plugin, that can sync all Woo data about the network, with the API from MLP. The plugin is currently in a beta status, but you can test it. If you like, send me f.bueltgeATinpsydeDOTcom an Mail and I will send you information and the plugin.

As example to sync post meta data see this gist. https://gist.github.com/toscho/6393c79aacba0983a96a

Best regards and thanks for feedback.

@JoryHogeveen
Copy link
Author

Hello Frank,

Thanks for the info! I've started a addon to automate metafield syncing but I have some questions.
Is there a hook/constant that contains all the linked sites and posts so I can use them in the save process?

Thanks!

@tfrommen
Copy link
Member

Hi there,

I don't know exactly which way you want to do this, so what do you want/need here?

For a given blog with $blog_id, getting an array with the IDs of all connected blogs is possible like so:

switch_to_blog( $blog_id );
$connected_blog_ids = array_keys( mlp_get_available_languages_titles() );
restore_current_blog();

For a given post with $blog_id and post_id, getting an array with all interlinked posts in other blogs is possible like so:

$mlp_language_api = apply_filters( 'mlp_language_api', NULL );
$interlinked_posts = is_a( $mlp_language_api, 'Mlp_Language_Api_Interface' )
    ? $mlp_language_api->get_related_content_ids( $blog_id, $post_id )
    : array();

Anything else?

@JoryHogeveen
Copy link
Author

I'm not sure but I think I need the last one, only it doesn't work for mee.

This is in short what I'm trying to do with a separate plugin:

class Multilingualpress_Custom_Post_Meta_Sync {
  public function setup() {
    /* get linked post in combination with site id's */
  }
}
add_action( 'mlp_and_wp_loaded', array( new Multilingualpress_Custom_Post_Meta_Sync(), 'setup' ) );

I need these in order to store the values correctly across the network.

@JoryHogeveen
Copy link
Author

Update:

class Multilingualpress_Custom_Post_Meta_Sync {
  public function setup() {
    add_filter('mlp_pre_save_post_meta', array ( $this, 'register_meta_fields' ), 10, 2);
  }

  public function register_meta_fields( array $post_meta ) {
    $blog_id = get_current_blog_id();
    $post_id = intval($_POST['post_ID']);
    $type = $_POST['post_type'];//get_post_type($post_id);

    $mlp_language_api = apply_filters( 'mlp_language_api', NULL );
    $interlinked_posts = is_a( $mlp_language_api, 'Mlp_Language_Api_Interface' )
        ? $mlp_language_api->get_related_content_ids( $blog_id, $post_id, $type )
        : array();
    /* $interlinked_posts is always an empty array */
  }
}
add_action( 'mlp_and_wp_loaded', array( new Multilingualpress_Custom_Post_Meta_Sync(), 'setup' ) );

$interlinked_posts is always an empty array... can't figure out why.

@tfrommen
Copy link
Member

Hi there,

I copied your code to my theme's functions.php file - and everything works as expected.

I have a network with two blogs, each having one post. The blogs have a language, are related to each other, and the posts are translations of each other.
$interlinked_posts is then an array with two elements (two because the source post itself is included).

How does your setup look like? Are there any translations for the post in question?

@JoryHogeveen
Copy link
Author

I've got it working now, don't exacly know why but the first time it didn't work so I started looking into the MLP source code and saw the post_type ($type) value, thats where I went wrong since it required the "post" post_type.

I've made a quick first try for a plugin.. its a v0.1-beta1 (at most) but if you guy's could take a look that would be great!
https://github.com/JoryHogeveen/multilingual-press-custom-post-meta

Single post_meta values are working now, you can enable them in the network settings of MLP.

I'm still struggling with post_meta with multiple values so that won't work right now.
Also, it probably lacks security and clean-coding on multiple parts, any advice and/or help would be great.
I think this could be an awesome feature for MLP in the long run though!

@JoryHogeveen
Copy link
Author

ADD: Also I've found this function which does the same aparently:
mlp_get_linked_elements( $post_id, $type, $blog_id )

@bueltge
Copy link
Member

bueltge commented Jun 18, 2015

@JoryHogeveen We have a plugin that sync all data from WooCommerce. I can send this to you, that you have an example and a plugin for tests. If you will test the plugin, then send me a mail - f.bueltgeATinpsydeDOTcom

@JoryHogeveen
Copy link
Author

I've send you an email! I would like to see/test this addon.

But the reason I've started the above was not for WooCommerce only but more as an allround post-meta sync addon.
For example: When you create extra fields with Pods or ACF they won't get synced. I'm trying to enable that function on a per-metakey basis.

@kraftner
Copy link
Contributor

I've just started to investigate syncing of post meta and I was hitting a wall with the deletion of post meta. Let me explain what I mean.

Simply copying over post meta more or less boils down to this. (Leaving out all checks for empty data and alike for the sake of simplicity in this example)

add_filter( 'mlp_pre_save_post_meta', function( $post_meta, $save_context ){
    $post_meta['_yourprefix_text'] = $_POST['_yourprefix_text'];
    return $post_meta;
}, 10, 2 );

The problem is that when you delete post meta on one side it stays on the other languages. This also seems not to be addressed by the solutions from @toscho or @JoryHogeveen. I still haven't thought this through and deletion might not always make sense, but it definitely is relevant in some situations.

So I had a look if I can hook somewhere to delete post meta but I couldn't find anything. The relevant line seems to be this:

https://github.com/inpsyde/multilingual-press/blob/master/inc/post-translator/Mlp_Translatable_Post_Data.php#L319

Maybe one could add a check there if the value is null/empty/false and delete the metadata in such a case. I've seen this approach with CMB2:
https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2_Field.php#L466-L470

Once again this is just a braindump to see if you have any better ideas or there already is a working approach for this with the current codebase.

@Dinamiko Dinamiko added this to the v2.12.0 milestone Aug 27, 2018
@Dinamiko
Copy link
Contributor

Related to #125

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants