| @@ -116,12 +116,18 @@ | ||
| 116 | 116 | } else { |
| 117 | 117 | $mediaSize = 'full'; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | + $mime_type = get_post_mime_type( $id ); | |
| 121 | + | |
| 120 | 122 | // If media not found |
| 121 | - if( false === $mime_type = get_post_mime_type( $id ) ){ | |
| 123 | + if( false === $mime_type ){ | |
| 122 | 124 | throw new \Exception('Media does not exists.'); |
| 123 | 125 | } |
| 126 | + // If mime type was not detected try to retrieve original attachment url | |
| 127 | + if( empty( $mime_type ) && $attachment_url = wp_get_attachment_url( $id ) ){ | |
| 128 | + return $attachment_url; | |
| 129 | + } | |
| 124 | 130 | |
| 125 | 131 | // If it was video mime type |
| 126 | 132 | if( 0 === strpos( $mime_type, 'video' ) ){ |
| 127 | 133 | $attachment_url = wp_get_attachment_url( $id ); |
| @@ -156,9 +162,10 @@ | ||
| 156 | 162 | public function importAsset( $assetID, $forceToDownloadAgain = false ) { |
| 157 | 163 | |
| 158 | 164 | // Check if this asset id is imported before or not |
| 159 | 165 | // Useful while user publishes document during edit process multiple times |
| 160 | - if( ! $forceToDownloadAgain && $attachmentId = $this->getAttachmentForImportedAsset( $assetID ) ){ | |
| 166 | + $attachmentId = $this->getAttachmentForImportedAsset( $assetID ); | |
| 167 | + if( ! $forceToDownloadAgain && $attachmentId && get_attached_file( $attachmentId ) ){ | |
| 161 | 168 | return $attachmentId; |
| 162 | 169 | } |
| 163 | 170 | |
| 164 | 171 | $args = [ |
| @@ -198,8 +205,9 @@ | ||
| 198 | 205 | |
| 199 | 206 | $fileSystem = Depicter::storage()->filesystem(); |
| 200 | 207 | |
| 201 | 208 | $file = Depicter::storage()->uploads()->getPath() . '/' . $filename; |
| 209 | + $fileType = wp_check_filetype( $filename, null ); | |
| 202 | 210 | |
| 203 | 211 | if( $fileSystem->exists( $file ) ){ |
| 204 | 212 | $fileUrl = Depicter::storage()->uploads()->getUrl() . '/' . $filename; |
| 205 | 213 | $attachmentId = attachment_url_to_postid( $fileUrl ); |
| @@ -204,10 +212,12 @@ | ||
| 204 | 212 | $fileUrl = Depicter::storage()->uploads()->getUrl() . '/' . $filename; |
| 205 | 213 | $attachmentId = attachment_url_to_postid( $fileUrl ); |
| 206 | 214 | if ( $attachmentId ) { |
| 207 | 215 | $this->registerImportedAsset( $assetID, (int) $attachmentId ); |
| 216 | + return $attachmentId; | |
| 208 | 217 | } |
| 209 | - return false; | |
| 218 | + | |
| 219 | + return $this->insertAttachment( $assetID, $file, $fileType['type'], $assetFileName ); | |
| 210 | 220 | } |
| 211 | 221 | |
| 212 | 222 | $isFileDownloaded = $fileSystem->write( |
| 213 | 223 | $file, |
| @@ -217,42 +227,54 @@ | ||
| 217 | 227 | if ( ! $isFileDownloaded ) { |
| 218 | 228 | return false; |
| 219 | 229 | } |
| 220 | 230 | |
| 221 | - $fileType = wp_check_filetype( $filename, null ); | |
| 231 | + return $this->insertAttachment( $assetID, $file, $fileType['type'], $assetFileName ); | |
| 232 | + } | |
| 222 | 233 | |
| 234 | + /** | |
| 235 | + * Insert Attachment | |
| 236 | + * | |
| 237 | + * @param string $assetID | |
| 238 | + * @param string $filePath | |
| 239 | + * @param string $fileType | |
| 240 | + * @param string $fileName | |
| 241 | + * | |
| 242 | + * @return false|int | |
| 243 | + */ | |
| 244 | + public function insertAttachment( string $assetID, string $filePath, string $fileType, string $fileName = '') { | |
| 223 | 245 | $attachment = array( |
| 224 | - 'post_mime_type' => $fileType['type'], | |
| 225 | - 'post_title' => $assetFileName, | |
| 246 | + 'post_mime_type' => $fileType, | |
| 247 | + 'post_title' => !empty( $fileName ) ? $fileName : basename( $filePath ), | |
| 226 | 248 | 'post_content' => '', |
| 227 | 249 | 'post_status' => 'inherit' |
| 228 | 250 | ); |
| 229 | 251 | |
| 230 | - $attachmentId = wp_insert_attachment( $attachment, $file ); | |
| 252 | + $attachmentId = wp_insert_attachment( $attachment, $filePath ); | |
| 231 | 253 | |
| 232 | 254 | if( is_wp_error( $attachmentId ) || ! $attachmentId ){ |
| 233 | - error_log( 'Error while inserting asset with ID of ' . $attachmentId, 0 ); | |
| 255 | + error_log( 'Error while inserting asset with ID of ' . $assetID, 0 ); | |
| 234 | 256 | return false; |
| 235 | 257 | } |
| 236 | 258 | |
| 237 | - wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $file ) ); | |
| 259 | + wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $filePath ) ); | |
| 238 | 260 | |
| 239 | 261 | $this->registerImportedAsset( $assetID, (int) $attachmentId ); |
| 240 | 262 | |
| 241 | 263 | return $attachmentId; |
| 242 | 264 | } |
| 243 | - | |
| 244 | 265 | /** |
| 245 | 266 | * Imports list of assets to media library |
| 246 | 267 | * |
| 247 | 268 | * @param $assetIDs |
| 269 | + * @param $forceToDownloadAgain | |
| 248 | 270 | * |
| 249 | 271 | * @throws GuzzleException |
| 250 | 272 | */ |
| 251 | - public function importAssets( $assetIDs ) | |
| 273 | + public function importAssets( $assetIDs, $forceToDownloadAgain = false ) | |
| 252 | 274 | { |
| 253 | 275 | foreach( $assetIDs as $ID ){ |
| 254 | - $this->importAsset( $ID ); | |
| 276 | + $this->importAsset( $ID, $forceToDownloadAgain ); | |
| 255 | 277 | } |
| 256 | 278 | } |
| 257 | 279 | |
| 258 | 280 | /** |
| @@ -272,8 +294,31 @@ | ||
| 272 | 294 | $dictionary = $this->getImportedAssetsDictionary(); |
| 273 | 295 | return isset( $dictionary[ $assetId ] ) ? $dictionary[ $assetId ] : false; |
| 274 | 296 | } |
| 275 | 297 | |
| 298 | + /** | |
| 299 | + * Retrieves supported mime types for a media type | |
| 300 | + * | |
| 301 | + * @param string $mediaType Media type. image, photo, video, audio, vector, svg | |
| 302 | + * | |
| 303 | + * @return mixed|string | |
| 304 | + */ | |
| 305 | + public function getSupportedMimeTypes( $mediaType = '' ){ | |
| 306 | + $mimeTypes = [ | |
| 307 | + 'image' => ['image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/webp'], | |
| 308 | + 'photo' => ['image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/webp'], | |
| 309 | + 'video' => 'video', | |
| 310 | + 'audio' => 'audio', | |
| 311 | + 'vector' => 'image/svg+xml', | |
| 312 | + 'svg' => 'image/svg+xml' | |
| 313 | + ]; | |
| 314 | + | |
| 315 | + if( ! empty( $mimeTypes[ $mediaType ] ) ){ | |
| 316 | + return $mimeTypes[ $mediaType ]; | |
| 317 | + } | |
| 318 | + | |
| 319 | + return ''; | |
| 320 | + } | |
| 276 | 321 | |
| 277 | 322 | /** |
| 278 | 323 | * Registers the attachment ID and belonging asset ID in a dictionary. |
| 279 | 324 | * |