Hi,
I apologize for the very long entry but I'm a bit a loss with plugin creation.
I'm trying to code a plugin for internal use. It should offer a widget where the user can choose a source language, a target language and a text to translate; these info are then sent (in ajax) to a translation program hosted on a thrid-party server (hence the cURL part) and, finally, the translated text is displayed in the widget, below the text to translate.
I was able to do the form, cURL and ajax with two php files, and a mix of php, html and js so that it works but translating this into a plugin (and even worse, in a plugin with a widget) was a totaly different story and I'm not a developper...
I've tried to correctly implement Ajax in the WordPress way but I'm not sure if it is ok like that and I actually don't know how I could make the widget display my code ^^° I guess I should put something in the function widget ? (but then what ?)
Below, the php code :
<?php
/**
Plugin Name: Moduletrad
Plugin URI:
Description: Plugin to interface automatic translation
Version: 1.0
Author: Korvapuusti
Author URI:
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: traduction-ipra
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
define(plugin_dir_path(__FILE__));
define(plugin_dir_url(__FILE__));
// enqueue the js script
function plugintrad_enqueuescripts(){
wp_register_script ('ajax-script', plugin_dir_url(__FILE__) . '/js/ajax-script.js', array( 'jquery' ),'1',true);
wp_enqueue_script('ajax-script');
}
// passing the url of admin-ajx.php
wp_localize_script( 'ajax-script', 'ajax-script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action('wp_enqueue_scripts', plugintrad_enqueuescripts);
// form to send source language, target language and the text to translate
// text area to receive the translated text
function plugintrad_form(){
?>
<form method="post" action="" id="form">
<label for="source">Langue source</label>
<select id="source" name="source">
<option value="fr">Français</option>
<option value="en" selected="selected">Anglais</option>
<option value="ar">Arabe</option>
</select>
<label for="target">Langue cible</label>
<select id="target" name="target">
<option value="fr">Français</option>
<option value="en">Anglais</option>
<option value="ar">Arabe</option>
</select>
<p>
<label for="textatrad">Saisissez le texte à traduire</label>
<br />
<textarea name="textatrad" id="textatrad" rows="10" cols="50" maxlength ="255"></textarea>
</p>
</form>
<div>
<textarea name="traduction" id="rep" rows="10" cols="50"></textarea>
</div>
<?php
} ?>
<?php // function to handle the info obtained with the form (url-ification, sending to the server hosting the translation program, retrieving the translated text)
function moduletrad_ajax_handler (){
$params = array(
'q' => urlencode(htmlspecialchars($_POST["q"])),
'key' => "bla",
'target' => $_POST["target"],
'source' => $_POST["source"],
);
function httpPost($url,$params){
$postData = '';
//crée les paires nom-valeur séparées par &
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
$postData = rtrim($postData, '&'); //enlève le dernier & pour que la fin de l'url soit correcte
$proxy = "172.20.12.74";
$proxyport = "3128";
$link = $url .'?'. $postData;
//curl
$ch = curl_init(); // initialise la session curl
// réglage des options curl
curl_setopt($ch,CURLOPT_URL,$link); // url à récupérer
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); // return la réponse du serveur
/*curl_setopt($ch,CURLOPT_HEADER,false); // n'inclue pas l'en-tête dans la valeur de retour*/
curl_setopt($ch,CURLOPT_PROXY, $proxy);
curl_setopt($ch,CURLOPT_PROXYPORT, $proxyport);
curl_setopt($cURL,CURLOPT_HTTPHEADER,array (
"Content-type: application/json",
"Accept: application/json"
));
$output = curl_exec($ch);
curl_close($ch); // ferme la session curl
return trim($output);
}
$data = httpPost("http://adress.domain:0000/translate", $params);
$data = json_decode($data, true);
echo $data['data']['translations'][0]['translatedText'];
}
//creating Ajax call for WordPress
add_action('wp_ajax_nopriv_moduletrad_ajax_handler','moduletrad_ajax_handler');
add_action('wp_ajax_moduletrad_ajax_handler','moduletrad_ajax_handler');
// adding the widget
add_action('widgets_init','moduletrad_init');
function moduletrad_init(){
register_widget("moduletrad_widget");
}
class moduletrad_widget extends WP_widget{
function moduletrad_widget(){
$widget_ops = array(
'classname' => 'traduction',
'description' => 'Automatic translation of specific terms'
);
parent::__construct('widget-moduletrad','Widget de traduction', $widget_ops);
}
function widget($args,$instance){
extract($args);
echo $before_widget;
echo $before_title.$instance["titre"].$after_title;
echo $after_widget;
}
function update($new,$old){
return $new;
}
function form($instance){
?>
<p>
<label for="<?php echo $this->get_field_id("titre"); ?>">Titre :</label>
<input value="<?php echo $instance["titre"]; ?>" name="<?php echo $this->get_field_name("titre"); ?>" id="<?php echo $this->get_field_id("titre"); ?>" type="text"/>
</p>
<?php
}
} ?>
and the js (ajax-script.js):
$(document).ready(function($){
$("#rep").hide();
$("#textatrad").on('input', function(){
var textatrad = $("#textatrad").val();
var source = $("#source option:selected").val();
var target = $("#target option:selected").val();
if (source == target){
alert("la langue source et la langue cible doivent être différentes");
}
else if (textatrad == ""){
$("#rep").hide();
}
else {
$.post("ajaxtraitement2.php", {q: textatrad, source: source, target: target},
url : ajax-script.ajaxurl,
rep : {action: "ajaxscript"},
function (data){
$("#rep").show().empty().append(data);
}
)}
return false;
});
});
`
At this point, any advise would be most welcomed...