| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* wpLingua OB Callback function : Translate pages |
| 11 |
* |
| 12 |
* @param string $html |
| 13 |
* @return string |
| 14 |
*/ |
| 15 |
function wplng_ob_callback_translate( $html ) { |
| 16 |
|
| 17 |
$html = apply_filters( 'wplng_html_intercepted', $html ); |
| 18 |
|
| 19 |
if ( wplng_str_is_json( $html ) ) { |
| 20 |
$html = wplng_ob_callback_translate_json( $html ); |
| 21 |
} elseif ( wplng_str_is_html( $html ) ) { |
| 22 |
$html = wplng_ob_callback_translate_html( $html ); |
| 23 |
} |
| 24 |
|
| 25 |
$html = apply_filters( 'wplng_html_translated', $html ); |
| 26 |
|
| 27 |
return $html; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* wpLingua OB Callback function : Translate JSON |
| 33 |
* |
| 34 |
* @param string $json |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
function wplng_ob_callback_translate_json( $json ) { |
| 38 |
|
| 39 |
if ( empty( $json ) ) { |
| 40 |
return $json; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get all texts in JSON |
| 45 |
*/ |
| 46 |
|
| 47 |
$texts = wplng_parse_json( $json ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Get saved translation |
| 51 |
*/ |
| 52 |
|
| 53 |
$language_target_id = wplng_get_language_current_id(); |
| 54 |
$translations = array(); |
| 55 |
|
| 56 |
if ( ! empty( $texts ) ) { |
| 57 |
$translations = wplng_get_translations_target( $language_target_id ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get unknow texts |
| 62 |
*/ |
| 63 |
|
| 64 |
$texts_unknow = array(); |
| 65 |
|
| 66 |
foreach ( $texts as $text ) { |
| 67 |
|
| 68 |
$is_in = false; |
| 69 |
|
| 70 |
foreach ( $translations as $translation ) { |
| 71 |
if ( $text === $translation['source'] ) { |
| 72 |
$is_in = true; |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! $is_in ) { |
| 78 |
$texts_unknow[] = $text; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$texts_unknow = array_splice( |
| 83 |
$texts_unknow, |
| 84 |
0, |
| 85 |
WPLNG_MAX_TRANSLATIONS + 1 |
| 86 |
); |
| 87 |
|
| 88 |
/** |
| 89 |
* Get new translated text |
| 90 |
*/ |
| 91 |
|
| 92 |
$texts_unknow = array(); |
| 93 |
|
| 94 |
$texts_unknow_translated = wplng_api_call_translate( |
| 95 |
$texts_unknow, |
| 96 |
false, |
| 97 |
$language_target_id |
| 98 |
); |
| 99 |
|
| 100 |
/** |
| 101 |
* Save new translation as wplng_translation CPT |
| 102 |
*/ |
| 103 |
|
| 104 |
$translations_new = array(); |
| 105 |
|
| 106 |
foreach ( $texts_unknow as $key => $text_source ) { |
| 107 |
if ( isset( $texts_unknow_translated[ $key ] ) ) { |
| 108 |
$translations_new[] = array( |
| 109 |
'source' => $text_source, |
| 110 |
'translation' => $texts_unknow_translated[ $key ], |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$translations_new = wplng_save_translations( |
| 116 |
$translations_new, |
| 117 |
$language_target_id |
| 118 |
); |
| 119 |
|
| 120 |
/** |
| 121 |
* Merge know and new translations |
| 122 |
*/ |
| 123 |
|
| 124 |
$translations = array_merge( |
| 125 |
$translations_new, |
| 126 |
$translations |
| 127 |
); |
| 128 |
|
| 129 |
/** |
| 130 |
* Replace original texts by translations |
| 131 |
* Translate links |
| 132 |
* Replace locale ID in data |
| 133 |
*/ |
| 134 |
|
| 135 |
$json = wplng_translate_json( |
| 136 |
$json, |
| 137 |
$translations |
| 138 |
); |
| 139 |
|
| 140 |
return $json; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* wpLingua OB Callback function : Translate HTML |
| 146 |
* |
| 147 |
* @param string $html |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
function wplng_ob_callback_translate_html( $html ) { |
| 151 |
|
| 152 |
if ( empty( $html ) ) { |
| 153 |
return $html; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Replace excluded HTML part by tag |
| 158 |
*/ |
| 159 |
|
| 160 |
$excluded = array(); |
| 161 |
|
| 162 |
$html = wplng_html_set_exclude_tag( |
| 163 |
$html, |
| 164 |
$excluded |
| 165 |
); |
| 166 |
|
| 167 |
/** |
| 168 |
* Get all texts in HTML |
| 169 |
*/ |
| 170 |
|
| 171 |
$texts = wplng_parse_html( $html ); |
| 172 |
|
| 173 |
/** |
| 174 |
* Get saved translation |
| 175 |
*/ |
| 176 |
|
| 177 |
$language_target_id = wplng_get_language_current_id(); |
| 178 |
$translations = array(); |
| 179 |
|
| 180 |
if ( ! empty( $texts ) ) { |
| 181 |
$translations = wplng_get_translations_target( $language_target_id ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get unknow texts |
| 186 |
*/ |
| 187 |
|
| 188 |
$texts_unknow = array(); |
| 189 |
|
| 190 |
foreach ( $texts as $text ) { |
| 191 |
|
| 192 |
$is_in = false; |
| 193 |
|
| 194 |
foreach ( $translations as $translation ) { |
| 195 |
if ( $text === $translation['source'] ) { |
| 196 |
$is_in = true; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( ! $is_in ) { |
| 202 |
$texts_unknow[] = $text; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
$texts_unknow = array_splice( |
| 207 |
$texts_unknow, |
| 208 |
0, |
| 209 |
WPLNG_MAX_TRANSLATIONS + 1 |
| 210 |
); |
| 211 |
|
| 212 |
/** |
| 213 |
* Get new translated text |
| 214 |
*/ |
| 215 |
|
| 216 |
$texts_unknow_translated = wplng_api_call_translate( |
| 217 |
$texts_unknow, |
| 218 |
false, |
| 219 |
$language_target_id |
| 220 |
); |
| 221 |
|
| 222 |
/** |
| 223 |
* Save new translation as wplng_translation CPT |
| 224 |
*/ |
| 225 |
|
| 226 |
$translations_new = array(); |
| 227 |
|
| 228 |
foreach ( $texts_unknow as $key => $text_source ) { |
| 229 |
if ( isset( $texts_unknow_translated[ $key ] ) ) { |
| 230 |
$translations_new[] = array( |
| 231 |
'source' => $text_source, |
| 232 |
'translation' => $texts_unknow_translated[ $key ], |
| 233 |
); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
$translations_new = wplng_save_translations( |
| 238 |
$translations_new, |
| 239 |
$language_target_id |
| 240 |
); |
| 241 |
|
| 242 |
/** |
| 243 |
* Separate page translations |
| 244 |
*/ |
| 245 |
|
| 246 |
$translations_in_page = array(); |
| 247 |
|
| 248 |
foreach ( $translations as $translation ) { |
| 249 |
foreach ( $texts as $text ) { |
| 250 |
$text = wplng_text_esc( $text ); |
| 251 |
if ( ! empty( $translation['source'] ) |
| 252 |
&& $translation['source'] === $text |
| 253 |
) { |
| 254 |
$translations_in_page[] = $translation; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Merge know and new translations |
| 261 |
*/ |
| 262 |
|
| 263 |
$translations = array_merge( |
| 264 |
$translations_in_page, |
| 265 |
$translations_new |
| 266 |
); |
| 267 |
|
| 268 |
/** |
| 269 |
* Replace original texts by translations |
| 270 |
*/ |
| 271 |
|
| 272 |
$html = wplng_translate_html( |
| 273 |
$html, |
| 274 |
false, |
| 275 |
$language_target_id, |
| 276 |
$translations |
| 277 |
); |
| 278 |
|
| 279 |
/** |
| 280 |
* Replace tag by saved excluded HTML part |
| 281 |
*/ |
| 282 |
|
| 283 |
$html = wplng_html_replace_exclude_tag( |
| 284 |
$html, |
| 285 |
$excluded |
| 286 |
); |
| 287 |
|
| 288 |
return $html; |
| 289 |
} |
| 290 |
|