| @@ -1,69 +1,75 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -// If this file is called directly, abort. | |
| 4 | -if ( ! defined( 'WPINC' ) ) { | |
| 5 | - die; | |
| 6 | -} | |
| 7 | - | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * Modify links in dom for translated pages | |
| 11 | - * | |
| 12 | - * @param object $dom | |
| 13 | - * @param array $args | |
| 14 | - * @return object | |
| 15 | - */ | |
| 16 | -function wplng_dom_replace_links( $dom, $args ) { | |
| 17 | - | |
| 18 | - wplng_args_setup( $args ); | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Translate and replace links | |
| 22 | - */ | |
| 23 | - | |
| 24 | - $attr_url_to_translate = wplng_data_attr_url_to_translate(); | |
| 25 | - | |
| 26 | - foreach ( $attr_url_to_translate as $attr ) { | |
| 27 | - | |
| 28 | - foreach ( $dom->find( $attr['selector'] ) as $element ) { | |
| 29 | - | |
| 30 | - if ( empty( $element->attr[ $attr['attr'] ] ) ) { | |
| 31 | - continue; | |
| 32 | - } | |
| 33 | - | |
| 34 | - $link = sanitize_url( $element->attr[ $attr['attr'] ] ); | |
| 35 | - | |
| 36 | - $translated_url = wplng_url_translate( | |
| 37 | - $link, | |
| 38 | - $args['language_target'] | |
| 39 | - ); | |
| 40 | - | |
| 41 | - $element->setAttribute( $attr['attr'], esc_url( $translated_url ) ); | |
| 42 | - | |
| 43 | - } | |
| 44 | - } | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * Apply "link & media" rules and replace links | |
| 48 | - */ | |
| 49 | - | |
| 50 | - foreach ( $dom->find( 'img[srcset]' ) as $element ) { | |
| 51 | - | |
| 52 | - if ( empty( $element->attr['srcset'] ) ) { | |
| 53 | - continue; | |
| 54 | - } | |
| 55 | - | |
| 56 | - $link = esc_attr( $element->attr['srcset'] ); | |
| 57 | - | |
| 58 | - $url_link_media_applied = wplng_link_media_apply_rules( | |
| 59 | - $link, | |
| 60 | - $args['language_target'] | |
| 61 | - ); | |
| 62 | - | |
| 63 | - if ( $url_link_media_applied !== $link ) { | |
| 64 | - $element->setAttribute( 'srcset', esc_attr( $url_link_media_applied ) ); | |
| 65 | - } | |
| 66 | - } | |
| 67 | - | |
| 68 | - return $dom; | |
| 69 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +// If this file is called directly, abort. | |
| 4 | +if ( ! defined( 'WPINC' ) ) { | |
| 5 | + die; | |
| 6 | +} | |
| 7 | + | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Modify links in dom for translated pages | |
| 11 | + * | |
| 12 | + * @param object $dom | |
| 13 | + * @param array $args | |
| 14 | + * @return object | |
| 15 | + */ | |
| 16 | +function wplng_dom_replace_links( $dom, $args ) { | |
| 17 | + | |
| 18 | + wplng_args_setup( $args ); | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Translate and replace links | |
| 22 | + */ | |
| 23 | + | |
| 24 | + $attr_url_to_translate = wplng_data_attr_url_to_translate(); | |
| 25 | + | |
| 26 | + foreach ( $attr_url_to_translate as $attr ) { | |
| 27 | + | |
| 28 | + foreach ( $dom->find( $attr['selector'] ) as $element ) { | |
| 29 | + | |
| 30 | + if ( empty( $element->attr[ $attr['attr'] ] ) ) { | |
| 31 | + continue; | |
| 32 | + } | |
| 33 | + | |
| 34 | + $link = sanitize_url( $element->attr[ $attr['attr'] ] ); | |
| 35 | + | |
| 36 | + $translated_url = wplng_url_translate( | |
| 37 | + $link, | |
| 38 | + $args['language_target'] | |
| 39 | + ); | |
| 40 | + | |
| 41 | + $element->setAttribute( | |
| 42 | + $attr['attr'], | |
| 43 | + esc_url( $translated_url ) | |
| 44 | + ); | |
| 45 | + | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Apply "link & media" rules and replace links | |
| 51 | + */ | |
| 52 | + | |
| 53 | + foreach ( $dom->find( 'img[srcset]' ) as $element ) { | |
| 54 | + | |
| 55 | + if ( empty( $element->attr['srcset'] ) ) { | |
| 56 | + continue; | |
| 57 | + } | |
| 58 | + | |
| 59 | + $link = $element->attr['srcset']; | |
| 60 | + | |
| 61 | + $url_link_media_applied = wplng_link_media_apply_rules( | |
| 62 | + $link, | |
| 63 | + $args['language_target'] | |
| 64 | + ); | |
| 65 | + | |
| 66 | + if ( $url_link_media_applied !== $link ) { | |
| 67 | + $element->setAttribute( | |
| 68 | + 'srcset', | |
| 69 | + esc_attr( $url_link_media_applied ) | |
| 70 | + ); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + return $dom; | |
| 75 | +} | |