| 1 |
<?php |
| 2 |
|
| 3 |
function deepl_get_translation_results_in_admin() { |
| 4 |
if ( isset( $_GET['translated'] ) ) { |
| 5 |
if ( filter_var( $_GET['translated'], FILTER_VALIDATE_INT ) == 1 ) { |
| 6 |
$deepl_result = 'success'; |
| 7 |
$message = __( 'This post has been translated.', 'wpdeepl' ); |
| 8 |
} |
| 9 |
else { |
| 10 |
$deepl_result = 'warning'; |
| 11 |
$message = __( 'The translation has failed. See logs for details', 'wpdeepl' ); |
| 12 |
} |
| 13 |
} |
| 14 |
else { |
| 15 |
$message = ''; |
| 16 |
$deepl_result = ''; |
| 17 |
} |
| 18 |
return compact('message', 'deepl_result'); |
| 19 |
} |
| 20 |
// notice for classic editor |
| 21 |
add_action( 'admin_notices', 'deepl_admin_notice_nogutenberg_deepl_translated' ); |
| 22 |
function deepl_admin_notice_nogutenberg_deepl_translated() { |
| 23 |
$results = deepl_get_translation_results_in_admin(); |
| 24 |
extract( $results ); |
| 25 |
if( !empty( $message ) ) { |
| 26 |
echo ' |
| 27 |
<div class="notice notice-'. $deepl_result. ' is-dismissible"> |
| 28 |
<p>' . $message . '</p> |
| 29 |
</div>'; |
| 30 |
|
| 31 |
} |
| 32 |
} |
| 33 |
// notice for gutenberg // obsolete since we reload after translation |
| 34 |
add_action('admin_footer', 'deepl_admin_notice_gutenberg_deepl_translated'); |
| 35 |
function deepl_admin_notice_gutenberg_deepl_translated() { |
| 36 |
$results = deepl_get_translation_results_in_admin(); |
| 37 |
extract( $results ); |
| 38 |
|
| 39 |
if ( $message && strlen( $message) && !empty( $deepl_result ) ) : |
| 40 |
?> |
| 41 |
<script type="text/javascript"> |
| 42 |
|
| 43 |
jQuery(document).ready(function() { |
| 44 |
if( typeof wp !== 'undefined' && typeof wp.data !== 'undefined' ) { |
| 45 |
var result_type = '<?php echo $deepl_result; ?>'; |
| 46 |
if( result_type == 'success' ) { |
| 47 |
wp.data.dispatch( 'core/notices').createSuccessNotice( '<?php echo addslashes( $message ); ?>', 'deepl-result'); |
| 48 |
} |
| 49 |
else { |
| 50 |
wp.data.dispatch( 'core/notices').createWarningNotice( '<?php echo addslashes( $message ); ?>', 'deepl-result'); |
| 51 |
} |
| 52 |
} |
| 53 |
else { |
| 54 |
// console.log('no gutenberg'); |
| 55 |
} |
| 56 |
}); |
| 57 |
</script> |
| 58 |
|
| 59 |
<?php |
| 60 |
endif; |
| 61 |
} |
| 62 |
//https://wpdeepl.zebench.net/wp-admin/post.php?post=134&action=deepl_translate_post_do&deepl_source_lang=auto&deepl_target_lang=en_GB&behaviour=replace&_deeplnonce=60a9264039 |
| 63 |
add_action('admin_init', 'deepl_maybe_translate_post' ); |
| 64 |
function deepl_maybe_translate_post() { |
| 65 |
$local_debug = true; |
| 66 |
$local_debug = false; |
| 67 |
if ( !isset( $_GET['deepl_action']) ) { |
| 68 |
if( $local_debug ) die('1'); |
| 69 |
return; |
| 70 |
} |
| 71 |
if ( !isset($_GET['deepl_action']) || html_entity_decode( $_GET['deepl_action'] ) != 'deepl_translate_post_do' ) { |
| 72 |
if( $local_debug ) die('2'); |
| 73 |
return; |
| 74 |
} |
| 75 |
if ( !isset( $_GET['_deeplnonce'] ) ) { |
| 76 |
if( $local_debug ) die('3'); |
| 77 |
return; |
| 78 |
} |
| 79 |
if ( !wp_verify_nonce( $_GET['_deeplnonce'], DeepLConfiguration::getNonceAction() ) ) { |
| 80 |
if( $local_debug ) die('4'); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
$args = array(); |
| 87 |
$args['ID'] = filter_var( $_GET['post'] , FILTER_VALIDATE_INT ); |
| 88 |
$args['source_lang'] = html_entity_decode( $_GET['deepl_source_lang'] ); |
| 89 |
$args['target_lang'] = html_entity_decode( $_GET['deepl_target_lang'] ); |
| 90 |
$args['behaviour'] = html_entity_decode( $_GET['behaviour'] ); |
| 91 |
$args['force_polylang'] = filter_var( $_GET['deepl_force_polylang'], FILTER_VALIDATE_BOOLEAN ); |
| 92 |
|
| 93 |
$translated = deepl_translate_post_link( $args ); |
| 94 |
//echo "translaed "; var_dump( $translated); |
| 95 |
|
| 96 |
$redirection = admin_url( '/post.php?post=' . $args['ID'] . '&action=edit&translated=' ); |
| 97 |
if ( $translated ) |
| 98 |
$redirection .= '1'; |
| 99 |
else |
| 100 |
$redirection .= '0'; |
| 101 |
if( $local_debug ) die('5 = ' . $redirection); |
| 102 |
|
| 103 |
//https://wpdeepl.zebench.net/wp-admin/post.php?post=130&action=edit&translated=1 |
| 104 |
// var_dump( $redirection); die('gloubi boulga'); |
| 105 |
|
| 106 |
|
| 107 |
// die("redirection $redirection " . __FUNCTION__ ); |
| 108 |
wp_redirect( $redirection ); |
| 109 |
exit(); |
| 110 |
} |
| 111 |
|
| 112 |
function deepl_already_exists_in_polylang( $post_id, $target_lang ) { |
| 113 |
// checking if Polylang equivalent already exists; |
| 114 |
$post_translations_terms = wp_get_object_terms( $post_id, 'post_translations' ); |
| 115 |
if( is_array( $post_translations_terms ) && count( $post_translations_terms ) ) { |
| 116 |
$post_translations = $post_translations_terms[0]; |
| 117 |
$pll_link_array = maybe_unserialize( $post_translations->description ); |
| 118 |
if( strlen( $target_lang ) == 2 ) { |
| 119 |
$pll_language = strtolower( $target_lang ); |
| 120 |
} |
| 121 |
else { |
| 122 |
$pll_language = strtolower( substr( $target_lang, 0, 2 ) ); |
| 123 |
} |
| 124 |
if( isset( $pll_link_array[$pll_language] ) ) { |
| 125 |
$post_id = $pll_link_array[$pll_language]; |
| 126 |
return $post_id; |
| 127 |
} |
| 128 |
} |
| 129 |
return false; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
function deepl_translate_post_link( $args ) { |
| 134 |
|
| 135 |
$defaults = array( |
| 136 |
'ID' => false, |
| 137 |
'source_lang' => false, |
| 138 |
'target_lang' => DeepLConfiguration::getDefaultTargetLanguage(), |
| 139 |
'behaviour' => DeepLConfiguration::getMetaBoxDefaultBehaviour(), |
| 140 |
'bulk' => false, |
| 141 |
'bulk_action' => false, |
| 142 |
'force_polylang' => false, |
| 143 |
'redirect' => true, |
| 144 |
); |
| 145 |
$args = wp_parse_args( $args, $defaults ); |
| 146 |
//plouf( $args );// die('oiezrjzeoijr'); |
| 147 |
extract( $args ); |
| 148 |
|
| 149 |
$WP_Post = get_post( $args['ID'] ); |
| 150 |
|
| 151 |
if( !$force_polylang && DeepLConfiguration::usingPolylang() ) { |
| 152 |
$translation_id = deepl_already_exists_in_polylang( $WP_Post->ID, $target_lang ); |
| 153 |
// var_dump( $translation_id ); die('okazmeaz4aze6848846'); |
| 154 |
|
| 155 |
if( $translation_id ) { |
| 156 |
$log = array ($WP_Post->ID, "already exists in $target_lang", $translation_id ); |
| 157 |
wpdeepl_log( $log, 'errors'); |
| 158 |
if( $redirect ) { |
| 159 |
$redirect = get_permalink( $translation_id ); |
| 160 |
die(" redirect $redirect " . __FUNCTION__ ); |
| 161 |
wp_redirect( $redirect ); |
| 162 |
exit(); |
| 163 |
|
| 164 |
} |
| 165 |
else { |
| 166 |
if( !$bulk_action ) |
| 167 |
return $translation_id; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$strings_to_translate = array(); |
| 173 |
|
| 174 |
|
| 175 |
foreach ( array( 'post_title', 'post_content', 'post_excerpt' ) as $key ) { |
| 176 |
$option_key = 'wpdeepl_t' . $key; |
| 177 |
if( WPDEEPL_DEBUG ) echo "\n option key $option_key = " . get_option( $option_key ); |
| 178 |
if( get_option( $option_key ) !== '' ) |
| 179 |
$strings_to_translate[$key] = $WP_Post->$key; |
| 180 |
} |
| 181 |
|
| 182 |
if( WPDEEPL_DEBUG ) plouf( $WP_Post, " WP POST" ); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
//plouf( $strings_to_translate); echo "\n excertp = "; var_dump( $WP_Post->excerpt ); echo "\n post excertp"; var_dump( $WP_Post->post_excerpt) ; die('okaze4az6e84e68a4e'); |
| 187 |
|
| 188 |
|
| 189 |
// shortcodes |
| 190 |
foreach( $strings_to_translate as $key => $string ) { |
| 191 |
preg_match_all( '#\[([a-z_0-9]+)]#m', $string, $matches ); |
| 192 |
if( $matches ) { |
| 193 |
//plouf( $matches, $key ); |
| 194 |
foreach( $matches[0] as $found ) { |
| 195 |
//echo "\n '$found' to '<x>$found</x>' "; |
| 196 |
$strings_to_translate[$key] = str_replace( $found, '<x>' . $found .'</x>', $strings_to_translate[$key] ); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
if( WPDEEPL_DEBUG ) plouf( $strings_to_translate, "avant filtre"); |
| 205 |
|
| 206 |
$strings_to_translate = apply_filters( |
| 207 |
'deepl_translate_post_link_strings', |
| 208 |
$strings_to_translate, |
| 209 |
$WP_Post, |
| 210 |
$target_lang, |
| 211 |
$source_lang, |
| 212 |
$bulk, |
| 213 |
$bulk_action |
| 214 |
); |
| 215 |
if( WPDEEPL_DEBUG ) plouf( $strings_to_translate, "apres filtre"); |
| 216 |
|
| 217 |
|
| 218 |
$no_translation = array(); |
| 219 |
if( isset( $strings_to_translate['_notranslation'] ) ) { |
| 220 |
$no_translation = $strings_to_translate['_notranslation']; |
| 221 |
unset( $strings_to_translate['_notranslation'] ); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
//plouf( $strings_to_translate); die('okzeùlrkzpeorrkpzo'); |
| 226 |
|
| 227 |
$response = deepl_translate( $source_lang, $target_lang, $strings_to_translate ); |
| 228 |
if( WPDEEPL_DEBUG ) { |
| 229 |
plouf( $response, " response de traduction");; |
| 230 |
} |
| 231 |
|
| 232 |
//plouf( $strings_to_translate," from $source_lang to $target_lang "); plouf( $response ); |
| 233 |
//die('zemozjpriook'); |
| 234 |
|
| 235 |
$log = array('ID' => $WP_Post->ID ); |
| 236 |
$log = array_merge( $log, json_decode( json_encode( $response ), true ) ); |
| 237 |
$return = false; |
| 238 |
|
| 239 |
$post_array = array(); |
| 240 |
if ( is_array( $response ) && $response['success'] ) { |
| 241 |
$post_array = array( |
| 242 |
'ID' => $WP_Post->ID |
| 243 |
); |
| 244 |
foreach ( $response['translations'] as $key => $translation ) { |
| 245 |
// shortcode |
| 246 |
$translation = preg_replace( '#<x>(.+?)<\/x>#', '\1', $translation ); |
| 247 |
$post_array[$key] = $translation; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
//plouf( $post_array ); die('oaze5az46ea6ze4a48eak'); |
| 252 |
$post_array = apply_filters('deepl_translate_post_link_translated_array', |
| 253 |
$post_array, |
| 254 |
$strings_to_translate, |
| 255 |
$response, |
| 256 |
$WP_Post, |
| 257 |
$no_translation, |
| 258 |
$bulk, |
| 259 |
$bulk_action |
| 260 |
); |
| 261 |
|
| 262 |
do_action('deepl_translate_before_post_update', |
| 263 |
$post_array, |
| 264 |
$strings_to_translate, |
| 265 |
$response, |
| 266 |
$WP_Post, |
| 267 |
$no_translation, |
| 268 |
$bulk, |
| 269 |
$bulk_action |
| 270 |
); |
| 271 |
|
| 272 |
|
| 273 |
if( isset( $post_array['post_content'] ) ) { |
| 274 |
$post_array['post_content'] = html_entity_decode( $post_array['post_content'] ); |
| 275 |
|
| 276 |
} |
| 277 |
// plouf( $response ); plouf( $post_array , " arz)ozoz"); plouf( wp_slash( $post_array ) ); die('azezeeeaok'); |
| 278 |
|
| 279 |
if( WPDEEPL_DEBUG ) { |
| 280 |
plouf( $post_array, " nouvelle post array ");; |
| 281 |
} |
| 282 |
|
| 283 |
if (count( $post_array ) > 1 ) { |
| 284 |
$return = wp_update_post( wp_slash( $post_array ) ); |
| 285 |
|
| 286 |
//$translated_post = get_post( $post_array['ID'] ); echo "\n translated = \n" . $translated_post->post_content;die('ozerkozerk'); |
| 287 |
//var_dump( $updated); |
| 288 |
|
| 289 |
do_action('deepl_translate_post_link_translation_success', $response, $WP_Post ); |
| 290 |
} else { |
| 291 |
$log[] = __('Nothing to update', 'wpdeepl' ); |
| 292 |
wpdeepl_log( $log, 'errors'); |
| 293 |
do_action('deepl_translate_post_link_translation_error', $response, $WP_Post ); |
| 294 |
} |
| 295 |
} else { |
| 296 |
$log[] = __('Translation error', 'wpdeepl' ); |
| 297 |
$log[] = json_encode( $response ); |
| 298 |
do_action('deepl_translate_post_link_translation_error', $response, $WP_Post ); |
| 299 |
wpdeepl_log( $log, 'errors'); |
| 300 |
} |
| 301 |
|
| 302 |
do_action('deepl_translate_after_post_update', |
| 303 |
$post_array, |
| 304 |
$strings_to_translate, |
| 305 |
$response, |
| 306 |
$WP_Post, |
| 307 |
$no_translation, |
| 308 |
$bulk, |
| 309 |
$bulk_action |
| 310 |
); |
| 311 |
do_action('deepl_translate_post_link_after', |
| 312 |
$response, |
| 313 |
$strings_to_translate, |
| 314 |
$WP_Post, |
| 315 |
$no_translation, |
| 316 |
$bulk, |
| 317 |
$bulk_action |
| 318 |
); |
| 319 |
return $return; |
| 320 |
} |