woocommerce-multilingual
/
addons
/
wpml-dependencies
/
vendor
/
wpml
/
wp
/
classes
/
Attachment.php
woocommerce-multilingual
/
addons
/
wpml-dependencies
/
vendor
/
wpml
/
wp
/
classes
Last commit date
Attachment.php
1 month ago
Cache.php
1 month ago
Gutenberg.php
1 month ago
Hooks.php
1 month ago
Http.php
1 month ago
Nonce.php
1 month ago
Option.php
1 month ago
Post.php
1 month ago
PostType.php
1 month ago
Resources.php
1 month ago
Roles.php
4 years ago
Transient.php
1 month ago
Url.php
1 month ago
User.php
1 month ago
WP.php
1 month ago
WPDB.php
1 month ago
Attachment.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPML\LIB\WP; |
| 4 | |
| 5 | use WPML\FP\Logic; |
| 6 | use WPML\FP\Str; |
| 7 | use function WPML\FP\pipe; |
| 8 | |
| 9 | class Attachment { |
| 10 | private static $cache = []; |
| 11 | |
| 12 | private static $withOutSizeRegEx = '/-\d+[Xx]\d+\./'; |
| 13 | |
| 14 | private static function removeSizeFromUrl( $urlWithMaybeSize ) { |
| 15 | return Str::pregReplace( self::$withOutSizeRegEx, '.', $urlWithMaybeSize ); |
| 16 | } |
| 17 | |
| 18 | public static function extractSrcFromAttributes( $data ) { |
| 19 | if ( ! array_key_exists( 'attributes', $data ) || ! is_array( $data['attributes'] ) || ! array_key_exists( 'src', $data['attributes'] ) ) { |
| 20 | return ''; |
| 21 | } |
| 22 | |
| 23 | $src = $data['attributes']['src']; |
| 24 | |
| 25 | if ( ! is_string( $src ) || strlen( $src ) === 0 ) { |
| 26 | return ''; |
| 27 | } |
| 28 | |
| 29 | return self::removeSizeFromUrl( $src ); |
| 30 | } |
| 31 | |
| 32 | public static function idFromUrlCache( $url, $urlWithoutSize = null ) { |
| 33 | if ( array_key_exists( $url, self::$cache ) ) { |
| 34 | return self::$cache[ $url ]; |
| 35 | } |
| 36 | |
| 37 | if ( is_null( $urlWithoutSize ) ) { |
| 38 | $urlWithoutSize = self::removeSizeFromUrl( $url ); |
| 39 | } |
| 40 | |
| 41 | if ( array_key_exists( $urlWithoutSize, self::$cache ) ) { |
| 42 | return self::$cache[ $urlWithoutSize ]; |
| 43 | } |
| 44 | |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | public static function idFromUrl( $url ) { |
| 49 | $urlWithoutSize = self::removeSizeFromUrl( $url ); |
| 50 | $maybeId = self::idFromUrlCache( $url, $urlWithoutSize ); |
| 51 | |
| 52 | if ( ! is_null( $maybeId ) && is_numeric( $maybeId ) ) { |
| 53 | return $maybeId; |
| 54 | } |
| 55 | |
| 56 | if ( $url !== $urlWithoutSize && $id = attachment_url_to_postid( $urlWithoutSize ) ) { |
| 57 | self::$cache[ $url ] = $id; |
| 58 | self::$cache[ $urlWithoutSize ] = $id; |
| 59 | return $id; |
| 60 | } |
| 61 | |
| 62 | if ( $id = attachment_url_to_postid( $url ) ) { |
| 63 | self::$cache[ $url ] = $id; |
| 64 | return $id; |
| 65 | } |
| 66 | |
| 67 | if ( $id = self::idByGuid( $url ) ) { |
| 68 | self::$cache[ $url ] = $id; |
| 69 | return $id; |
| 70 | } |
| 71 | |
| 72 | if ( $url !== $urlWithoutSize && $id = self::idByGuid( $urlWithoutSize ) ) { |
| 73 | self::$cache[ $url ] = $id; |
| 74 | self::$cache[ $urlWithoutSize ] = $id; |
| 75 | return $id; |
| 76 | } |
| 77 | |
| 78 | self::$cache[ $url ] = null; |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | public static function idByGuid( $url ) { |
| 83 | if ( array_key_exists( $url , self::$cache ) ) { |
| 84 | return self::$cache[$url]; |
| 85 | } |
| 86 | |
| 87 | global $wpdb; |
| 88 | |
| 89 | $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s' LIMIT 1", $url ) ); |
| 90 | |
| 91 | return $attachment && count( $attachment ) |
| 92 | ? $attachment[0] |
| 93 | : 0; |
| 94 | } |
| 95 | |
| 96 | public static function addToCache( $media ) { |
| 97 | self::$cache = array_merge( self::$cache, $media ); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | public static function attachmentUrlsToPostIds( $urls ) { |
| 102 | global $wpdb; |
| 103 | |
| 104 | if ( count( $urls ) === 0 ) { |
| 105 | return []; |
| 106 | } |
| 107 | |
| 108 | list( $pathes, $urlsToPathes, $pathesToUrls ) = self::getPathesFromUrls( $urls ); |
| 109 | |
| 110 | $results = $wpdb->get_results( |
| 111 | "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value IN (" . wpml_prepare_in( $pathes, '%s' ) . ")" |
| 112 | ); |
| 113 | |
| 114 | $results = array_map( |
| 115 | function ( $result ) { |
| 116 | return [ |
| 117 | 'id' => $result->post_id, |
| 118 | 'value' => $result->meta_value |
| 119 | ]; |
| 120 | }, |
| 121 | $results |
| 122 | ); |
| 123 | |
| 124 | return self::mapUrlsToPostIds( $results, $urls, $pathes, $urlsToPathes, $pathesToUrls ); |
| 125 | } |
| 126 | |
| 127 | public static function getPathesFromUrls( $urls ) { |
| 128 | $urlsToPathes = []; |
| 129 | $pathesToUrls = []; |
| 130 | $pathes = []; |
| 131 | foreach ( $urls as $url ) { |
| 132 | $path = self::urlToPath( $url ); |
| 133 | $pathes[] = $path; |
| 134 | $urlsToPathes[ $url ] = $path; |
| 135 | $pathesToUrls[ $path ] = $url; |
| 136 | } |
| 137 | |
| 138 | return [ $pathes, $urlsToPathes, $pathesToUrls ]; |
| 139 | } |
| 140 | |
| 141 | public static function mapUrlsToPostIds( $results, $urls, $pathes, $urlsToPathes, $pathesToUrls ) { |
| 142 | $urlsToPostIds = []; |
| 143 | foreach ( $urls as $url ) { |
| 144 | $urlsToPostIds[ $url ] = null; |
| 145 | } |
| 146 | |
| 147 | foreach ( $results as $result ) { |
| 148 | foreach ( $pathes as $path ) { |
| 149 | if ( $path === $result['value'] ) { |
| 150 | $url = $pathesToUrls[ $path ]; |
| 151 | $urlsToPostIds[ $url ] = (int) $result['id']; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | foreach ( $urlsToPostIds as $url => $postId ) { |
| 158 | if ( ! is_null( $postId ) ) { |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | $path = strtolower( $urlsToPathes[ $url ] ); |
| 163 | foreach ( $results as $result ) { |
| 164 | if ( $path === strtolower( $result['value'] ) ) { |
| 165 | $urlsToPostIds[ $url ] = (int) $result['id']; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return $urlsToPostIds; |
| 172 | } |
| 173 | |
| 174 | public static function urlToPath( $url ) { |
| 175 | $dir = wp_get_upload_dir(); |
| 176 | $path = $url; |
| 177 | |
| 178 | $site_url = parse_url( $dir['url'] ); |
| 179 | $image_path = parse_url( $path ); |
| 180 | |
| 181 | if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) { |
| 182 | $path = str_replace( $image_path['scheme'], $site_url['scheme'], $path ); |
| 183 | } |
| 184 | |
| 185 | if ( Str::startsWith( $dir['baseurl'] . '/', $path ) ) { |
| 186 | $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); |
| 187 | } |
| 188 | |
| 189 | return $path; |
| 190 | } |
| 191 | } |
| 192 |