aucor / polylang-translate-existing-media

Bulk translate and replace all existing media in content, featured image and meta fields when you enable translations in media with Polylang.
GNU General Public License v2.0
28 stars 4 forks source link

Use Polylang's synchronisation settings when creating translated media #1

Open bostrom opened 6 years ago

bostrom commented 6 years ago

Great plugin, thank you!

One useful improvement could be to let Polylang synchronise the metadata for the translated media, according to the Synchronisation settings in Polylang's settings.

For example, we have a custom taxonomy media_category on media items. We've enabled the "Taxonomy" synchronisation setting, so when we manually create a translation for a media item, the new media item will belong to the translated taxonomy term.

When bulk translating the items, however, the translated media items don't belong to any media_category, forcing us to manually set the categories for each translated item.

Can you think of any way to run the Polylang synchronisation in the translation process? We're happy to make a PR if you can point us in the right direction.

Cheers!

TeemuSuoranta commented 6 years ago

Good point! I have only ever used this for simple images that have not had any taxonomies or extra metadata. For synchronization this plugin doesn't really need to do anything special as long as it copies things initially, I think. Polylang should pick up the sync task from there. Then again, I can't remember using synchronization with attachments before in Polylang.

Copying metadata should be pretty simple as attachments are pretty much like any other post type. In this plugin's code there is a function that translates the attachment starting at https://github.com/aucor/polylang-translate-existing-media/blob/master/polylang-translate-existing-media.php#L596

There is hardcoded handling for two attachments' metadata fields but nothing else. You can manually add your field there or try to build a one-size-fits-all kind of solution. Polylang has the filter pll_copy_post_metas https://polylang.pro/doc/filter-reference/ that might work or not. You can do this with ACF, too (https://github.com/aucor/polylang-translate-existing-media/issues/2).

For taxonomies I am not sure of best way to do it. The place is same as before. Polylang must have some internal functions or filters to use but you can also do this in code by yourself (it needs more work, though).

I can look into these but it might take some weeks as I'm currently busy with other stuff. The plugin itself is meant to be ran once and deleted so I haven't really made huge efforts for it. The same issues might affect also https://github.com/aucor/polylang-copy-content and if you have Polylang Pro you might find some gems from it's source.

Let me know if you find solutions and I can look into this but it will take some time.

bostrom commented 6 years ago

Thanks for the pointers. It's not a generic solution, but we managed to copy over our plugin metadata, custom acf fields and taxonomies by adding

    add_post_meta($tr_id, '_wp_attachment_metadata', get_post_meta($post_id, '_wp_attachment_metadata', true));
    add_post_meta($tr_id, '_wp_attached_file', get_post_meta($post_id, '_wp_attached_file', true));
+   // copy over some custom metadata
+   add_post_meta($tr_id, 'windows_azure_storage_info', get_post_meta($post_id, 'windows_azure_storage_info', true));
+ 
+   // set translation's taxonomy term to the same as the original
+   $post_terms = wp_get_object_terms( $post_id, 'media_category' );
+   $tr_terms = array();
+   foreach($post_terms as $obj => $term ) {
+       array_push($tr_terms, $term->term_id);
+   }
+   wp_set_object_terms( $tr_id, $tr_terms, 'media_category' );
+
+   // copy custom acf field
+   $short_title = get_field('short_title', $post_id, false);
+   if ($short_title) {
+     update_field('short_title', $short_title, $tr_id);
+   }

I won't make a PR out of this since it's specific to our needs, but I thought I'd share it for reference.