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