img_parser = $img_parser; $this->image_translator = $image_translator; $this->media_sizes = $media_sizes; } /** * @param string $text * @param string $source_language * @param string $target_language * * @return string */ public function replace_images_with_translations( $text, $target_language, $source_language = null ) { $imgs = $this->img_parser->get_imgs( $text ); foreach ( $imgs as $img ) { if ( isset( $img['attachment_id'] ) ) { $attachment_id = $img['attachment_id']; $size = $this->media_sizes->get_attachment_size( $img ); if ( $size ) { $translated_src = $this->image_translator->get_translated_image( $attachment_id, $target_language, $size ); } else { $translated_src = $this->get_translated_image_by_url( $target_language, $source_language, $img ); } } else { $translated_src = $this->get_translated_image_by_url( $target_language, $source_language, $img ); } if ( $translated_src && $translated_src !== $img['attributes']['src'] ) { $text = $this->replace_image_src( $text, $img['attributes']['src'], $translated_src ); } } return $text; } /** * @param string $text * @param string $from * @param string $to * * @return string */ private function replace_image_src( $text, $from, $to ) { return str_replace( $from, $to, $text ); } /** * @param $target_language * @param $source_language * @param $img * * @return bool|string */ private function get_translated_image_by_url( $target_language, $source_language, $img ) { if ( null === $source_language ) { $source_language = wpml_get_current_language(); } $translated_src = $this->image_translator->get_translated_image_by_url( $img['attributes']['src'], $source_language, $target_language ); return $translated_src; } }