Skip to content

Instantly share code, notes, and snippets.

@tnottu
Created November 28, 2016 13:06
Show Gist options
  • Save tnottu/a28a5037487a7c7fec68c968e91f5568 to your computer and use it in GitHub Desktop.
Save tnottu/a28a5037487a7c7fec68c968e91f5568 to your computer and use it in GitHub Desktop.
WP Polylang Bulk Translate
<?php
/*
Plugin Name: Polylang Bulk Translate
Plugin URI:
Version: 0.1.0
Author: Tyomaa Oy
Author URI: https://github.com/tnottu
Description: Translate multiple posts with bulk actions
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: polylang-bulk-translate
*/
class PolylangBulkTranslate {
/**
* Constructor
*/
public function __construct() {
// Check that Polylang is active
global $polylang;
if (isset($polylang)) {
add_action( 'current_screen', array( $this, 'register_bulk_actions' ) );
}
}
/**
* Register "Translate to: $lang" -actions
*/
function register_bulk_actions() {
$is_any_language_active = !empty( pll_current_language() );
$currentScreen = get_current_screen();
$is_post_list = $currentScreen->base === 'edit';
$is_taxonomy_list = $currentScreen->base === 'edit-tags';
$post_type = $currentScreen->post_type;
$taxonomy = $currentScreen->taxonomy;
$type = $is_post_list ? 'post' : 'term';
// TODO: Add support for taxonomies
$is_translatable = $is_post_list && pll_is_translated_post_type( $post_type );
$should_init = $is_any_language_active && $is_translatable;
if (!$should_init) {
return;
}
$bulk_actions = new Seravo_Custom_Bulk_Action( array( 'post_type' => $post_type ) );
// Register action for each language, except current
foreach ( pll_languages_list() as $language ) {
if ($language === pll_current_language()) {
continue;
}
$bulk_actions->register_bulk_action( array(
'menu_text' => 'Translate to: ' . strtoupper( $language) ,
'admin_notice' => ( $type === 'post' ) ? '%s posts translated.' : '%s terms translated.',
'callback' => function( $ids ) use ( $language, $type ) {
foreach ( $ids as $id ) {
if ( $type === 'post' ) {
$this->translate_post( $id, $language );
} else {
// TODO: Add support for taxonomies
}
}
return true;
}
) );
}
$bulk_actions->init();
}
/**
* Translate a post
*
* Creates a copy of the post, with all possible content from the original and sets
* the new copy as a translation.
*
* @param int $post_id ID of the original post
* @param string $new_lang New language slug
*
*/
function translate_post( $post_id, $new_lang ) {
$from_post = get_post( $post_id );
$has_translation = pll_get_post( $post_id, $new_lang );
if ($has_translation) return;
$new_post = clone $from_post; // Copy the post
/*
* Prepare post
*/
$new_post->ID = null;
$new_post->post_status = 'draft';
$new_post_id = wp_insert_post( $new_post ); // Creates a new post thanks to ID being null
/*
* Set languate & translation relation
*/
pll_set_post_language( $new_post_id, $new_lang );
pll_save_post_translations( array(
pll_get_post_language( $from_post->ID ) => $from_post->ID,
$new_lang => $new_post_id
) );
/*
* Copy relevant extra data
*/
PLL()->sync->copy_taxonomies( $from_post->ID, $new_post_id, $new_lang );
PLL()->sync->copy_post_metas( $from_post->ID, $new_post_id, $new_lang );
wp_update_post( $new_post_id );
}
}
add_action('plugins_loaded', create_function('', 'global $polylang_bulk_translate; $polylang_bulk_translate = new PolylangBulkTranslate();'));
@amityweb
Copy link

Found a serious issue with this.... Using this plugin worked for the new language, but it lost all connections between all the other languages we have already translated.

Background: we used WPML previously, then used a WPML to Polylang migration plugin which worked fine. All pages were there in Polylang, all the languages connected (so you see the Edit icon). When we then created a new language (and still the connections existed) then went and did a Bulk Translate to the new language, it worked for that language, but then all other languages lost the connections. The pages exist in all languages, but there is no link between them (so it shows the + symbol now, not the edit symbol).

So this is quite a serious issue for us. Dont know if its do with the above script, or the way the languages are migrated from WPML to Polylang. But it all worked OK before using this script. I did it twice in my dev site, happened both times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment