| @@ -34,9 +34,19 @@ | ||
| 34 | 34 | public function getSourceUrl( $assetId, $size = 'large', $forcePreview = false, $args = [] ) |
| 35 | 35 | { |
| 36 | 36 | // if $assetId is an attachment ID |
| 37 | 37 | if( $attachmentId = $this->getAttachmentId( $assetId ) ) { |
| 38 | - $mediaUrl = $this->library()->getSourceURL( $attachmentId, $size, $args ); | |
| 38 | + try { | |
| 39 | + $mediaUrl = $this->library()->getSourceURL( $attachmentId, $size, $args ); | |
| 40 | + } catch( \Exception $e ) { | |
| 41 | + // if asset is imported but deleted manually after a while | |
| 42 | + if ( ! is_numeric( $assetId ) ) { | |
| 43 | + $this->library()->importAsset( $assetId, true ); | |
| 44 | + $attachmentId = $this->getAttachmentId( $assetId ); | |
| 45 | + $mediaUrl = $this->library()->getSourceURL( $attachmentId, $size, $args ); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 39 | 49 | // If asset is not imported yet |
| 40 | 50 | } else { |
| 41 | 51 | $size = is_array( $size ) ? 'large' : $size; |
| 42 | 52 | $mediaUrl = AssetsAPIService::getHotlink( $assetId, $size, ['forcePreview' => $forcePreview ] ); |
| @@ -60,9 +70,18 @@ | ||
| 60 | 70 | public function resizeSourceUrl( $assetId, $resizeW = null, $resizeH = null, $cropW = null, $cropH = null, $args = [] ){ |
| 61 | 71 | |
| 62 | 72 | // if $assetId is an attachment ID |
| 63 | 73 | if( $attachmentId = $this->getAttachmentId( $assetId ) ) { |
| 64 | - $mediaUrl = ImageEditor::process( $attachmentId, $resizeW, $resizeH, $cropW, $cropH, $args ); | |
| 74 | + try { | |
| 75 | + $mediaUrl = ImageEditor::process( $attachmentId, $resizeW, $resizeH, $cropW, $cropH, $args ); | |
| 76 | + } catch( \Exception $e ) { | |
| 77 | + // if asset is imported but deleted manually after a while | |
| 78 | + if ( ! is_numeric( $assetId ) ) { | |
| 79 | + $this->library()->importAsset( $assetId, true ); | |
| 80 | + $attachmentId = $this->getAttachmentId( $assetId ); | |
| 81 | + $mediaUrl = ImageEditor::process( $attachmentId, $resizeW, $resizeH, $cropW, $cropH, $args ); | |
| 82 | + } | |
| 83 | + } | |
| 65 | 84 | // If asset is not imported yet |
| 66 | 85 | } else { |
| 67 | 86 | $mediaUrl = AssetsAPIService::getHotlink( $assetId, 'large', [ 'forcePreview' => false ] ); |
| 68 | 87 | } |
| @@ -238,6 +257,21 @@ | ||
| 238 | 257 | return $attachmentId; |
| 239 | 258 | } |
| 240 | 259 | |
| 241 | 260 | return null; |
| 261 | + } | |
| 262 | + | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Remove attachment from imported media dictionary | |
| 266 | + * | |
| 267 | + * @param int $attachmentId | |
| 268 | + * @return void | |
| 269 | + */ | |
| 270 | + public function maybeRemoveAttachmentFromDictionary( $attachmentId ) { | |
| 271 | + $imported_media_dictionary = \Depicter::options()->get( 'imported_assets', [] ); | |
| 272 | + if ( false !== $array_key = array_search( $attachmentId, $imported_media_dictionary ) ) { | |
| 273 | + unset( $imported_media_dictionary[ $array_key ] ); | |
| 274 | + \Depicter::options()->set( 'imported_assets', $imported_media_dictionary ); | |
| 275 | + } | |
| 242 | 276 | } |
| 243 | 277 | } |