| 1 |
<?php |
| 2 |
|
| 3 |
class DeepL_Metabox { |
| 4 |
protected $metabox_config = array(); |
| 5 |
|
| 6 |
function __construct() { |
| 7 |
// adding the box |
| 8 |
add_action( 'add_meta_boxes', array( &$this, 'add_meta_box' ) ); |
| 9 |
|
| 10 |
// adding the javascript footer |
| 11 |
//add_action( 'admin_footer', array( &$this, 'deepl_admin_footer_javascript' ) ); |
| 12 |
|
| 13 |
// adding the ajax hook |
| 14 |
add_action( 'wp_ajax_deepl_translate', array( &$this, 'action_deepl_translate' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
function action_deepl_translate() { |
| 18 |
if( ! wp_verify_nonce( $_POST['nonce'], 'permission_to_translate' ) ){ |
| 19 |
wp_send_json_error( "Unauthorized" ); |
| 20 |
} |
| 21 |
|
| 22 |
$post_id = $_POST['post_id']; |
| 23 |
$strings = $_POST['to_translate']; |
| 24 |
foreach( $strings as $key => $string ) { |
| 25 |
$strings[$key] = stripslashes( $string ); |
| 26 |
} |
| 27 |
$source_lang = $_POST['source_lang']; |
| 28 |
$target_lang = $_POST['target_lang']; |
| 29 |
|
| 30 |
//plouf( $strings, " strings zehruhr euh" ); plouf( $_POST, " POST " ); echo " from $source_lang to $target_lang for id $post_id"; |
| 31 |
//die( 'ok' ); |
| 32 |
|
| 33 |
$response = deepl_translate( $source_lang, $target_lang, $strings, $post_id ); |
| 34 |
|
| 35 |
/*foreach( $translations as $key => $array ) { |
| 36 |
$responses[$key] = $array; |
| 37 |
}*/ |
| 38 |
wp_send_json_success( $response ); |
| 39 |
} |
| 40 |
|
| 41 |
function deepl_admin_footer_javascript() { |
| 42 |
?> |
| 43 |
<script type="text/javascript" > |
| 44 |
jQuery( document ).ready( function() { |
| 45 |
var is_gutenberg = jQuery( '.wp-block' ).length; |
| 46 |
|
| 47 |
if( is_gutenberg > 0 ) { |
| 48 |
//var warning = jQuery( '#deepl_metabox .hidden_warning' ).html(); |
| 49 |
//jQuery( '#deepl_metabox div.inside' ).html( warning ); |
| 50 |
} |
| 51 |
} ); |
| 52 |
jQuery( "#deepl_translate" ).on( "click", function() { |
| 53 |
var is_gutenberg = jQuery( '.wp-block' ).length; |
| 54 |
|
| 55 |
jQuery( '#deepl_spinner' ).css( 'visibility', 'visible' ); |
| 56 |
|
| 57 |
var should_we_replace = jQuery( 'input[name="deepl_replace"]:checked' ).val(); |
| 58 |
var target_lang = jQuery( '#deepl_target_lang' ).val(); |
| 59 |
var text_bits = {}; |
| 60 |
|
| 61 |
if( is_gutenberg ) { |
| 62 |
text_bits['post_title'] = jQuery( '.editor-post-title textarea' ).val(); |
| 63 |
text_bits['post_excerpt'] = jQuery( '.editor-post-excerpt textarea' ).val(); |
| 64 |
/*jQuery( '.editor-block-list__layout p.wp-block-paragraph' ).each( function( index ) { |
| 65 |
text_bits['post_content_' + index] = jQuery( this ).html(); |
| 66 |
} );*/ |
| 67 |
|
| 68 |
jQuery( '.edit-post-visual-editor .editor-rich-text p.wp-block-paragraph' ).each( function() { |
| 69 |
text_bits['post_content_' + index] = jQuery( this ).html(); |
| 70 |
} ); |
| 71 |
jQuery( '.edit-post-visual-editor .editor-rich-text :header' ).each( function() { |
| 72 |
text_bits['post_conttit_' + index] = jQuery( this ).html(); |
| 73 |
} ); |
| 74 |
} |
| 75 |
else { |
| 76 |
text_bits['post_title'] = jQuery( 'input[name="post_title"]' ).val(); |
| 77 |
text_bits['post_excerpt'] = jQuery( 'textarea[name="excerpt"]' ).val(); |
| 78 |
text_bits['post_content'] = jQuery( '#content_ifr' ).contents().find( '#tinymce' ).html(); |
| 79 |
} |
| 80 |
|
| 81 |
JSON.stringify( text_bits ); |
| 82 |
|
| 83 |
var data = { |
| 84 |
action: 'deepl_translate', |
| 85 |
post_id: jQuery( 'input[name="post_ID"]' ).val(), |
| 86 |
to_translate: text_bits, |
| 87 |
source_lang: jQuery( '#deepl_source_lang' ).val(), |
| 88 |
target_lang: target_lang, |
| 89 |
nonce: jQuery( '#deepl_nonce' ).val(), |
| 90 |
}; |
| 91 |
|
| 92 |
console.log( "AJAX parameters call" ); |
| 93 |
console.dir( data ); |
| 94 |
// stop |
| 95 |
//jQuery( '#deepl_spinner' ).css( 'visibility', 'hidden' ); return false; |
| 96 |
|
| 97 |
jQuery.post( ajaxurl, data, function( responses ) { |
| 98 |
console.log( "Responses from API" ); |
| 99 |
console.dir( responses ); |
| 100 |
$.each( responses.data, function( index, value ) { |
| 101 |
//console.log( "index = " + index ); console.log( " text = " + value + " VERSUS " + value.replace( /\\( . )/mg, "$1" ) ); |
| 102 |
if( index == 'post_title' ) { |
| 103 |
if( should_we_replace == 'replace' ) { |
| 104 |
var new_value = value.replace( /\\( . )/mg, "$1" ) |
| 105 |
} |
| 106 |
else { |
| 107 |
var new_value = text_bits['post_title'] + '<lang="' + target_lang + '">' + value + '</lang>'; |
| 108 |
} |
| 109 |
if( is_gutenberg ) { |
| 110 |
jQuery( '.editor-post-title textarea' ).val( new_value ); |
| 111 |
} |
| 112 |
else { |
| 113 |
jQuery( 'input[name="post_title"]' ).val( new_value ); |
| 114 |
} |
| 115 |
} |
| 116 |
else if( index == 'post_excerpt' ) { |
| 117 |
if( should_we_replace == 'replace' ) { |
| 118 |
var new_value = value.replace( /\\( . )/mg, "$1" ) |
| 119 |
} |
| 120 |
else { |
| 121 |
var new_value = text_bits['post_excerpt'] + '<lang="' + target_lang + '">' + value + '</lang>'; |
| 122 |
} |
| 123 |
if( is_gutenberg ) { |
| 124 |
jQuery( '.editor-post-excerpt textarea' ).val( new_value ); |
| 125 |
} |
| 126 |
else { |
| 127 |
jQuery( 'textarea[name="excerpt"]' ).val( new_value ); |
| 128 |
} |
| 129 |
} |
| 130 |
else if( index == 'post_content' && !is_gutenberg ) { |
| 131 |
if( should_we_replace == 'replace' ) { |
| 132 |
jQuery( '#content_ifr' ).contents().find( '#tinymce' ).html( value.replace( /\\( . )/mg, "$1" ) ); |
| 133 |
} |
| 134 |
else { |
| 135 |
jQuery( '#content_ifr' ).contents().find( '#tinymce' ).html( |
| 136 |
jQuery( '#content_ifr' ).contents().find( '#tinymce' ).html() |
| 137 |
+ '<lang="' + target_lang + '">' + value + '</lang>' |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
else if( is_gutenberg ) { |
| 142 |
var content_index = index.substr( 13 ); |
| 143 |
//console.log( "content index = " + content_index ); |
| 144 |
|
| 145 |
var content_type = ( index.substr( 0, 12 ) == 'post_content' ) ? 'post_content' : 'post_content_titles'; |
| 146 |
if( content_type == 'post_content' ) { |
| 147 |
var blocks = jQuery( '.edit-post-visual-editor .editor-rich-text p.wp-block-paragraph' ); |
| 148 |
} |
| 149 |
else if( content_type === 'post_content_titles' ) { |
| 150 |
var blocks = jQuery( '.edit-post-visual-editor .editor-rich-text :header' ); |
| 151 |
} |
| 152 |
|
| 153 |
var block = blocks[index]; |
| 154 |
if( should_we_replace == 'replace' ) { |
| 155 |
jQuery( block ).html( value.replace( /\\( . )/mg, "$1" ) ) |
| 156 |
} |
| 157 |
else { |
| 158 |
jQuery( block ).html( jQuery( this ).html() + value.replace( /\\( . )/mg, "$1" ) ); |
| 159 |
} |
| 160 |
|
| 161 |
/* |
| 162 |
jQuery( blocks ).each( function( index ) { |
| 163 |
if( index == content_index ) { |
| 164 |
if( should_we_replace == 'replace' ) { |
| 165 |
jQuery( this ).html( value.replace( /\\( . )/mg, "$1" ) ) |
| 166 |
} |
| 167 |
else { |
| 168 |
jQuery( this ).html( jQuery( this ).html() + value.replace( /\\( . )/mg, "$1" ) ); |
| 169 |
} |
| 170 |
} |
| 171 |
} ); */ |
| 172 |
} |
| 173 |
else { |
| 174 |
console.log( "No action for " + index ); |
| 175 |
} |
| 176 |
jQuery( '#deepl_spinner' ).css( 'visibility', 'hidden' ); |
| 177 |
} ); |
| 178 |
} ); |
| 179 |
} ); |
| 180 |
</script> <?php |
| 181 |
} |
| 182 |
|
| 183 |
public function add_meta_box() { |
| 184 |
$post_types = DeepLConfiguration::getMetaBoxPostTypes(); |
| 185 |
|
| 186 |
add_meta_box( |
| 187 |
'deepl_metabox', |
| 188 |
__( 'DeepL translation', 'wpdeepl' ), |
| 189 |
array( &$this, 'output' ), |
| 190 |
$post_types, |
| 191 |
'side', |
| 192 |
'high' |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
public function output() { |
| 197 |
$html = ''; |
| 198 |
$html = ' |
| 199 |
<form id="deepl_admin_translation" name="deepl_admin_translation" method="POST">'; |
| 200 |
$html .= deepl_language_selector( 'source', 'deepl_source_lang', false ); |
| 201 |
$html .= '<br />' . __( 'Translating to', 'wpdeepl' ) . '<br /> '; |
| 202 |
$html .= deepl_language_selector( 'target', 'deepl_target_lang', get_option( 'deepl_default_locale' ) ); |
| 203 |
$html .= ' |
| 204 |
<span id="deepl_spinner" class="spinner"></span> |
| 205 |
'; |
| 206 |
|
| 207 |
$html .= wp_nonce_field( 'permission_to_translate', 'deepl_nonce', true, false ); |
| 208 |
$html .= ' |
| 209 |
<input id="deepl_translate" name="deepl_translate" type="button" class="button button-primary button-large" value="' . __( 'Translate' , 'wpdeepl' ) . '"></span>'; |
| 210 |
|
| 211 |
$default_behaviour = DeepLConfiguration::getMetaBoxDefaultBehaviour(); |
| 212 |
$default_metabox_behaviours = DeepLConfiguration::DefaultsMetaboxBehaviours(); |
| 213 |
|
| 214 |
$html .= ' |
| 215 |
<hr />'; |
| 216 |
foreach( $default_metabox_behaviours as $value => $label ) { |
| 217 |
$html.= ' |
| 218 |
<span style="display: block;"> |
| 219 |
<input type="radio" name="deepl_replace" value="'. $value .'"'; |
| 220 |
|
| 221 |
if( $value == $default_behaviour ) $html .= ' checked="checked"'; |
| 222 |
$html .= '> |
| 223 |
<label for="deepl_replace">' . $label . '</label> |
| 224 |
</span>'; |
| 225 |
} |
| 226 |
|
| 227 |
$html .= ' |
| 228 |
</form> |
| 229 |
<div class="hidden_warning" style="display: none;">' . __( 'Gutenberg is not compatible with WPDeepL yet. Please use Classic Editor', 'wpdeepl' ); |
| 230 |
|
| 231 |
echo $html; |
| 232 |
} |
| 233 |
} |