| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Translate JSON in attributes in dom for translated pages |
| 11 |
* |
| 12 |
* @param object $dom |
| 13 |
* @param array $args |
| 14 |
* @return object |
| 15 |
*/ |
| 16 |
function wplng_dom_translate_json_attr( $dom, $args ) { |
| 17 |
|
| 18 |
wplng_args_setup( $args ); |
| 19 |
|
| 20 |
/** |
| 21 |
* Translate JSON in attributes |
| 22 |
*/ |
| 23 |
|
| 24 |
$attr_json_to_translate = wplng_data_attr_json_to_translate(); |
| 25 |
|
| 26 |
foreach ( $attr_json_to_translate as $attr ) { |
| 27 |
foreach ( $dom->find( $attr['selector'] ) as $element ) { |
| 28 |
|
| 29 |
if ( empty( $element->attr[ $attr['attr'] ] ) ) { |
| 30 |
continue; |
| 31 |
} |
| 32 |
|
| 33 |
// Prepare arguments for translation |
| 34 |
$args['parents'] = array( $attr['attr'] ); |
| 35 |
|
| 36 |
$translated_json = wplng_translate_json( |
| 37 |
wp_specialchars_decode( |
| 38 |
$element->attr[ $attr['attr'] ], |
| 39 |
ENT_QUOTES | ENT_HTML5 |
| 40 |
), |
| 41 |
$args |
| 42 |
); |
| 43 |
|
| 44 |
$element->attr[ $attr['attr'] ] = esc_attr( $translated_json ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return $dom; |
| 49 |
} |
| 50 |
|