image( $attachment_id, $target_language, $size ); } catch ( Exception $e ) { $img_src = false; } } else { $img_src = false; } return $img_src; } /** * @param string $img_src * @param string|null $source_language * * @return int */ public function get_attachment_id_by_url( $img_src, $source_language = null ) { if ( ! $source_language ) { $source_language = $this->getLanguageByUrl( $img_src ) ?: $this->sitepress->get_current_language(); } $attachment_by_url = $this->attachment_by_url_factory->create( $img_src, $source_language, $this->media_attachment_by_url_query ); return (int) $attachment_by_url->get_id(); } /** * @param string $url * * @return null|string */ private function getLanguageByUrl( $url ) { $imageUrl = WPML_Media_Attachment_By_URL::getUrl( $url ); $imageId = Cache::get( self::URLS_TO_IDS_CACHE_KEY, $imageUrl )->getOrElse( null ); if ( ! $imageId ) { $imageId = attachment_url_to_postid( $imageUrl ); Cache::set( self::URLS_TO_IDS_CACHE_KEY, $imageUrl, HOUR_IN_SECONDS, $imageId ); } return $this->sitepress->get_language_for_element( $imageId, 'post_attachment' ); } /** * @param string $url * @param int $attachment_id * * @return string */ private function get_image_size_from_url( $url, $attachment_id ) { $media_sizes = new WPML_Media_Sizes(); return $media_sizes->get_image_size_from_url( $url, $attachment_id ); } /** * @param int $attachment_id * @param string $size * @param string $uploads_dir * * @return string */ private function get_sized_image_url( $attachment_id, $size, $uploads_dir ) { $image_url = ''; $meta_data = wp_get_attachment_metadata( $attachment_id ); $image_url_parts = array( $uploads_dir['baseurl'] ); if ( is_array( $meta_data ) && array_key_exists( 'file', $meta_data ) ) { $file_subdirectory = $meta_data['file']; $file_subdirectory_parts = explode( '/', $file_subdirectory ); $filename = array_pop( $file_subdirectory_parts ); $image_url_parts[] = implode( '/', $file_subdirectory_parts ); if ( array_key_exists( $size, $meta_data['sizes'] ) ) { $image_url_parts[] = $meta_data['sizes'][ $size ]['file']; } else { $image_url_parts[] = $filename; } $image_url = implode( '/', $image_url_parts ); } return $image_url; } }