Created
March 6, 2016 17:04
-
-
Save Dimasmagadan/cfc5623433ea37558578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Plugin Name: qTranslate-X Yandex Auto Translator Addon | |
Description: Over 900 lines of code? Rly? | |
Version: 1.0 | |
Author: [email protected] | |
*/ | |
/** | |
* undocumented function | |
* | |
* @return void | |
* @author dimasmagadan | |
**/ | |
function os_translate_text($from, $to, $text = false ){ | |
if($text){ | |
// TODO options page | |
$key = 'trnsl.1.1.20160306T130030Z.0bc73ad9793e29e9.a3d991d1e577c5ad0fea2765159b6a4046ff4906'; | |
$lang_pairs = array(); | |
$lang_pairs_url = 'https://translate.yandex.net/api/v1.5/tr.json/getLangs?key='.$key; | |
$response = wp_remote_get( $lang_pairs_url ); | |
if( !is_wp_error( $response) ) { | |
$lang_pairs_raw = json_decode( wp_remote_retrieve_body( $response ), true ); | |
if(isset($lang_pairs_raw['dirs'])){ | |
$lang_pairs = $lang_pairs_raw['dirs']; | |
} | |
} | |
if( in_array( $from.'-'.$to, $lang_pairs) ){ | |
$translate_url = 'https://translate.yandex.net/api/v1.5/tr.json/translate'; | |
$response = wp_remote_post( $translate_url, array( | |
'body' => array( | |
'key' => $key, | |
'text' => $text, | |
'lang' => $from.'-'.$to, | |
'format' => 'html', | |
) | |
)); | |
if( !is_wp_error( $response ) ){ | |
$translation_raw = json_decode( wp_remote_retrieve_body( $response ), true ); | |
if( isset($translation_raw['code']) && $translation_raw['code']==200){ | |
// TODO implode array? | |
return $translation_raw['text'][0]; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* undocumented function | |
* | |
* @return void | |
* @author dimasmagadan | |
**/ | |
function os_cron_translation_task(){ | |
} | |
/** | |
* undocumented function | |
* | |
* @return void | |
* @author dimasmagadan | |
**/ | |
function os_get_post_to_translate(){ | |
// TODO add option for main language. | |
$main_language = 'ru'; | |
/** | |
* The WordPress Query class. | |
* @link http://codex.wordpress.org/Function_Reference/WP_Query | |
* | |
*/ | |
$args = array( | |
// FIXME put you own post types | |
'post_type' => 'post', | |
'post_status' => array( | |
'publish', | |
'pending', | |
'draft', | |
'future', | |
'private', | |
), | |
'suppress_filters' => true, | |
'posts_per_page' => 1, | |
'meta_query' => array( | |
array( | |
'key' => '_ya_translated', | |
'compare' => 'NOT EXISTS' | |
), | |
), | |
); | |
$query = new WP_Query( $args ); | |
if( $query->have_posts() ){ | |
global $q_config; | |
$languages = $q_config['enabled_languages']; | |
while ($query->have_posts()) { | |
$query->the_post(); | |
add_post_meta( get_the_ID(), '_ya_translated', '0', true ); | |
// TODO will work only for 2 languages | |
foreach ($languages as $current_language) { | |
if( $main_language != $current_language ){ | |
$text_to_translate = qtranxf_use( $main_language, get_the_content(), false, true ); | |
$translation = os_translate_text( $main_language, $current_language, $text_to_translate ); | |
if($translation){ | |
update_post_meta(get_the_ID(), '_ya_translated', '1' ); | |
wp_update_post( array( | |
'ID' => get_the_ID(), | |
'post_content' => get_the_content().'[:'.$current_language.']'. $translation .'[:]' | |
) ); | |
} | |
} | |
} | |
} | |
} | |
wp_reset_query(); | |
exit; | |
} | |
// DEV | |
add_action( 'wp_ajax_text', 'os_get_post_to_translate' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment