| @@ -162,9 +162,10 @@ | ||
| 162 | 162 | public function importAsset( $assetID, $forceToDownloadAgain = false ) { |
| 163 | 163 | |
| 164 | 164 | // Check if this asset id is imported before or not |
| 165 | 165 | // Useful while user publishes document during edit process multiple times |
| 166 | - if( ! $forceToDownloadAgain && $attachmentId = $this->getAttachmentForImportedAsset( $assetID ) ){ | |
| 166 | + $attachmentId = $this->getAttachmentForImportedAsset( $assetID ); | |
| 167 | + if( ! $forceToDownloadAgain && $attachmentId && get_attached_file( $attachmentId ) ){ | |
| 167 | 168 | return $attachmentId; |
| 168 | 169 | } |
| 169 | 170 | |
| 170 | 171 | $args = [ |
| @@ -204,8 +205,9 @@ | ||
| 204 | 205 | |
| 205 | 206 | $fileSystem = Depicter::storage()->filesystem(); |
| 206 | 207 | |
| 207 | 208 | $file = Depicter::storage()->uploads()->getPath() . '/' . $filename; |
| 209 | + $fileType = wp_check_filetype( $filename, null ); | |
| 208 | 210 | |
| 209 | 211 | if( $fileSystem->exists( $file ) ){ |
| 210 | 212 | $fileUrl = Depicter::storage()->uploads()->getUrl() . '/' . $filename; |
| 211 | 213 | $attachmentId = attachment_url_to_postid( $fileUrl ); |
| @@ -210,10 +212,12 @@ | ||
| 210 | 212 | $fileUrl = Depicter::storage()->uploads()->getUrl() . '/' . $filename; |
| 211 | 213 | $attachmentId = attachment_url_to_postid( $fileUrl ); |
| 212 | 214 | if ( $attachmentId ) { |
| 213 | 215 | $this->registerImportedAsset( $assetID, (int) $attachmentId ); |
| 216 | + return $attachmentId; | |
| 214 | 217 | } |
| 215 | - return false; | |
| 218 | + | |
| 219 | + return $this->insertAttachment( $assetID, $file, $fileType['type'], $assetFileName ); | |
| 216 | 220 | } |
| 217 | 221 | |
| 218 | 222 | $isFileDownloaded = $fileSystem->write( |
| 219 | 223 | $file, |
| @@ -223,42 +227,54 @@ | ||
| 223 | 227 | if ( ! $isFileDownloaded ) { |
| 224 | 228 | return false; |
| 225 | 229 | } |
| 226 | 230 | |
| 227 | - $fileType = wp_check_filetype( $filename, null ); | |
| 231 | + return $this->insertAttachment( $assetID, $file, $fileType['type'], $assetFileName ); | |
| 232 | + } | |
| 228 | 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 = '') { | |
| 229 | 245 | $attachment = array( |
| 230 | - 'post_mime_type' => $fileType['type'], | |
| 231 | - 'post_title' => $assetFileName, | |
| 246 | + 'post_mime_type' => $fileType, | |
| 247 | + 'post_title' => !empty( $fileName ) ? $fileName : basename( $filePath ), | |
| 232 | 248 | 'post_content' => '', |
| 233 | 249 | 'post_status' => 'inherit' |
| 234 | 250 | ); |
| 235 | 251 | |
| 236 | - $attachmentId = wp_insert_attachment( $attachment, $file ); | |
| 252 | + $attachmentId = wp_insert_attachment( $attachment, $filePath ); | |
| 237 | 253 | |
| 238 | 254 | if( is_wp_error( $attachmentId ) || ! $attachmentId ){ |
| 239 | - error_log( 'Error while inserting asset with ID of ' . $attachmentId, 0 ); | |
| 255 | + error_log( 'Error while inserting asset with ID of ' . $assetID, 0 ); | |
| 240 | 256 | return false; |
| 241 | 257 | } |
| 242 | 258 | |
| 243 | - wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $file ) ); | |
| 259 | + wp_update_attachment_metadata( $attachmentId, wp_generate_attachment_metadata( $attachmentId, $filePath ) ); | |
| 244 | 260 | |
| 245 | 261 | $this->registerImportedAsset( $assetID, (int) $attachmentId ); |
| 246 | 262 | |
| 247 | 263 | return $attachmentId; |
| 248 | 264 | } |
| 249 | - | |
| 250 | 265 | /** |
| 251 | 266 | * Imports list of assets to media library |
| 252 | 267 | * |
| 253 | 268 | * @param $assetIDs |
| 269 | + * @param $forceToDownloadAgain | |
| 254 | 270 | * |
| 255 | 271 | * @throws GuzzleException |
| 256 | 272 | */ |
| 257 | - public function importAssets( $assetIDs ) | |
| 273 | + public function importAssets( $assetIDs, $forceToDownloadAgain = false ) | |
| 258 | 274 | { |
| 259 | 275 | foreach( $assetIDs as $ID ){ |
| 260 | - $this->importAsset( $ID ); | |
| 276 | + $this->importAsset( $ID, $forceToDownloadAgain ); | |
| 261 | 277 | } |
| 262 | 278 | } |
| 263 | 279 | |
| 264 | 280 | /** |
| @@ -278,8 +294,31 @@ | ||
| 278 | 294 | $dictionary = $this->getImportedAssetsDictionary(); |
| 279 | 295 | return isset( $dictionary[ $assetId ] ) ? $dictionary[ $assetId ] : false; |
| 280 | 296 | } |
| 281 | 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 | + } | |
| 282 | 321 | |
| 283 | 322 | /** |
| 284 | 323 | * Registers the attachment ID and belonging asset ID in a dictionary. |
| 285 | 324 | * |