| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Translate nodes text in dom for translated pages |
| 11 |
* |
| 12 |
* @param object $dom |
| 13 |
* @param array $args |
| 14 |
* @return object |
| 15 |
*/ |
| 16 |
function wplng_dom_translate_nodes_texts( $dom, $args ) { |
| 17 |
|
| 18 |
wplng_args_setup( $args ); |
| 19 |
|
| 20 |
if ( empty( $args['translations'] ) |
| 21 |
|| $args['load'] === 'progress' |
| 22 |
) { |
| 23 |
return $dom; |
| 24 |
} |
| 25 |
|
| 26 |
$selector = 'text'; |
| 27 |
|
| 28 |
if ( $args['mode'] === 'editor' |
| 29 |
|| $args['mode'] === 'list' |
| 30 |
) { |
| 31 |
$selector = 'head text'; |
| 32 |
} |
| 33 |
|
| 34 |
$node_text_excluded = wplng_data_excluded_node_text(); |
| 35 |
|
| 36 |
foreach ( $dom->find( $selector ) as $element ) { |
| 37 |
|
| 38 |
if ( in_array( $element->parent->tag, $node_text_excluded ) ) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
$text = wplng_text_esc( $element->innertext ); |
| 43 |
|
| 44 |
if ( ! wplng_text_is_translatable( $text ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$translated_text = wplng_get_translated_text_from_translations( |
| 49 |
$element->innertext, |
| 50 |
$args['translations'] |
| 51 |
); |
| 52 |
|
| 53 |
$element->innertext = esc_html( $translated_text ); |
| 54 |
} |
| 55 |
|
| 56 |
return $dom; |
| 57 |
} |
| 58 |
|