Skip to content

Commit

Permalink
Existing Feature Image IDs (#14)
Browse files Browse the repository at this point in the history
* - Fix check and clarify variables to show the existing Attachment ID is used or a new one is imported
  • Loading branch information
rleeson authored Apr 4, 2024
1 parent 0a235b0 commit c4c0716
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Processor/WordPress/FeaturedImageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,12 @@ protected function processFeaturedImage( FeaturedImagePostEntity $_entity, int $

// WordPress will scale large images, to accommodate this, use a regular expression to check
try {
$filename = pathinfo( $image->fileName, PATHINFO_FILENAME );
$extension = pathinfo( $image->fileName, PATHINFO_EXTENSION );
$currentImage = $this->attachmentService()->readByUniqueIdentifier( "{$filename}(-scaled)?\.{$extension}" );
$currentImageId = $currentImage?->indexIdentifier() ?? 0;
$_entity->updateFeaturedImageIdentifier(
1 > $currentImageId
? $this->imageService()->import( $image )
: 0
);
$filename = pathinfo( $image->fileName, PATHINFO_FILENAME );
$extension = pathinfo( $image->fileName, PATHINFO_EXTENSION );
$existingImage = $this->attachmentService()->readByUniqueIdentifier( "{$filename}(-scaled)?\.{$extension}" );
$existingImageId = $existingImage?->indexIdentifier() ?? 0;
$nextImageId = 0 < $existingImageId ? $existingImage : $this->imageService()->import( $image );
$_entity->updateFeaturedImageIdentifier( $nextImageId );
} catch ( SetReaderException ) {
$_entity->updateFeaturedImageIdentifier( $_existingId );
}
Expand Down

0 comments on commit c4c0716

Please sign in to comment.