| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Modify scripts in dom for translated pages |
| 11 |
* |
| 12 |
* @param object $dom |
| 13 |
* @param array $args |
| 14 |
* @return object |
| 15 |
*/ |
| 16 |
function wplng_dom_translate_script( $dom, $args ) { |
| 17 |
|
| 18 |
wplng_args_setup( $args ); |
| 19 |
|
| 20 |
foreach ( $dom->find( 'script' ) as $element ) { |
| 21 |
|
| 22 |
if ( ! empty( $element->attr['type'] ) |
| 23 |
&& ( |
| 24 |
$element->attr['type'] === 'application/ld+json' |
| 25 |
|| $element->attr['type'] === 'application/json' |
| 26 |
) |
| 27 |
) { |
| 28 |
|
| 29 |
/** |
| 30 |
* Translate JSON in JSON scripts |
| 31 |
*/ |
| 32 |
|
| 33 |
$element->innertext = wplng_translate_json( |
| 34 |
$element->innertext, |
| 35 |
$args |
| 36 |
); |
| 37 |
|
| 38 |
} else { |
| 39 |
|
| 40 |
/** |
| 41 |
* Translate JS in JS scripts |
| 42 |
*/ |
| 43 |
|
| 44 |
$element->innertext = wplng_translate_js( |
| 45 |
$element->innertext, |
| 46 |
$args |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $dom; |
| 52 |
} |
| 53 |
|