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