Skip to content

Commit

Permalink
fix: additional image sizes aren't present when plugin loads
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Dec 5, 2022
1 parent b55ab3d commit fff7639
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Configuration/EventManagementConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function modify(Container $container)
];

if ($container['ymir_cdn_image_processing_enabled']) {
$subscribers[] = new Subscriber\ContentDeliveryNetworkImageProcessingSubscriber($container['image_sizes'], $container['is_multisite'], $container['uploads_baseurl'], $container['content_width']);
$subscribers[] = new Subscriber\ContentDeliveryNetworkImageProcessingSubscriber($container['base_image_sizes'], $container['is_multisite'], $container['uploads_baseurl'], $container['content_width']);
}

return $subscribers;
Expand Down
4 changes: 2 additions & 2 deletions src/Configuration/WordPressConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function modify(Container $container)

return new \WP_Filesystem_Direct(false);
});
$container['image_sizes'] = $container->service(function () {
$container['base_image_sizes'] = $container->service(function () {
$sizes = [
'thumb' => [
'width' => (int) get_option('thumbnail_size_w'),
Expand Down Expand Up @@ -87,7 +87,7 @@ public function modify(Container $container)
// Compatibility mapping as found in wp-includes/media.php.
$sizes['thumbnail'] = $sizes['thumb'];

return array_merge($sizes, wp_get_additional_image_sizes());
return $sizes;
});
$container['is_multisite'] = is_multisite();
$container['phpmailer'] = function () {
Expand Down
45 changes: 28 additions & 17 deletions src/Subscriber/ContentDeliveryNetworkImageProcessingSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class ContentDeliveryNetworkImageProcessingSubscriber implements SubscriberInter
*/
private const SUPPORTED_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png', 'webp'];

/**
* The base WordPress image sizes.
*
* @var array
*/
private $baseImageSizes;

/**
* "content_width" global used to constrain image sizes if present.
*
Expand All @@ -43,14 +50,6 @@ class ContentDeliveryNetworkImageProcessingSubscriber implements SubscriberInter
* @var bool
*/
private $imageDownsizeFilterEnabled;

/**
* The registered WordPress image sizes.
*
* @var array
*/
private $imageSizes;

/**
* Flag whether this is a multisite installation or not.
*
Expand All @@ -68,11 +67,11 @@ class ContentDeliveryNetworkImageProcessingSubscriber implements SubscriberInter
/**
* {@inheritdoc}
*/
public function __construct(array $imageSizes, bool $isMultisite, string $uploadsUrl, ?int $contentWidthGlobal = null)
public function __construct(array $baseImageSizes, bool $isMultisite, string $uploadsUrl, ?int $contentWidthGlobal = null)
{
$this->baseImageSizes = $baseImageSizes;
$this->contentWidthGlobal = $contentWidthGlobal;
$this->imageDownsizeFilterEnabled = true;
$this->imageSizes = $imageSizes;
$this->isMultisite = $isMultisite;
$this->uploadsUrl = $uploadsUrl;
}
Expand Down Expand Up @@ -108,7 +107,7 @@ public function generateScaledDownImage($image, $attachmentId, $size)
return $image;
}

list($width, $height, $cropped) = $this->getAttachmentImageSize((int) $attachmentId, $size);
list($width, $height, $cropped) = $this->getImageAttachmentDimensions((int) $attachmentId, $size);

return [
$this->generateImageUrl($imageUrl, $height, $width, $cropped),
Expand Down Expand Up @@ -325,7 +324,7 @@ private function generateImageUrl(string $imageUrl, ?int $height, ?int $width, b
/**
* Get the image size array of the given image attachment for the given size.
*/
private function getAttachmentImageSize(int $attachmentId, $size): array
private function getImageAttachmentDimensions(int $attachmentId, $size): array
{
$cropped = false;
$fullSizeImageMetadata = wp_get_attachment_metadata($attachmentId);
Expand Down Expand Up @@ -377,6 +376,20 @@ private function getImageSize(array $image): array
return ($height || $width) ? [$width, $height, $height && $width] : $this->determineImageSizeUsingWordPressImageSizes($image);
}

/**
* Get the image size metadata for the given image size.
*/
private function getImageSizeMetadata($size): ?array
{
if (!is_int($size) && !is_string($size)) {
return null;
}

$sizes = array_merge($this->baseImageSizes, wp_get_additional_image_sizes());

return $sizes[$size] ?? null;
}

/**
* Get the original image URL from the re-dimensioned image URL.
*/
Expand All @@ -390,11 +403,9 @@ private function getOriginalImageUrl($url)
*/
private function isImageSizeCropped($size): bool
{
if (!is_int($size) && !is_string($size)) {
return false;
}
$sizeMetadata = $this->getImageSizeMetadata($size);

return (bool) ($this->imageSizes[$size]['crop'] ?? false);
return (bool) ($sizeMetadata['crop'] ?? false);
}

/**
Expand Down Expand Up @@ -422,7 +433,7 @@ private function isProcessableImageUrl(string $imageUrl): bool
*/
private function isValidImageSize($size): bool
{
return is_array($size) || ((is_int($size) || is_string($size)) && array_key_exists($size, $this->imageSizes));
return is_array($size) || is_array($this->getImageSizeMetadata($size));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public function testGenerateScaledDownImageForFullSizeImageWithNoImageMetadata()
->with($this->identicalTo(42))
->willReturn('https://assets.com/uploads/image.jpg');

$wp_get_additional_image_sizes = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_additional_image_sizes');
$wp_get_additional_image_sizes->expects($this->exactly(2))
->willReturn([]);

$wp_get_attachment_metadata = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_attachment_metadata');
$wp_get_attachment_metadata->expects($this->once())
->with($this->identicalTo(42))
Expand All @@ -157,6 +161,10 @@ public function testGenerateScaledDownImageForThumbnailImageWithNoImageMetadata(
$is_admin->expects($this->once())
->willReturn(false);

$wp_get_additional_image_sizes = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_additional_image_sizes');
$wp_get_additional_image_sizes->expects($this->exactly(2))
->willReturn([]);

$wp_get_attachment_url = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_attachment_url');
$wp_get_attachment_url->expects($this->once())
->with($this->identicalTo(42))
Expand Down Expand Up @@ -191,6 +199,10 @@ public function testGenerateScaledDownImageWhenAttachmentUrlIsntProcessable()
$is_admin->expects($this->once())
->willReturn(false);

$wp_get_additional_image_sizes = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_additional_image_sizes');
$wp_get_additional_image_sizes->expects($this->once())
->willReturn([]);

$wp_get_attachment_url = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_attachment_url');
$wp_get_attachment_url->expects($this->once())
->with($this->identicalTo(42))
Expand All @@ -214,6 +226,10 @@ public function testGenerateScaledDownImageWhenNoAttachmentUrlFound()
$is_admin->expects($this->once())
->willReturn(false);

$wp_get_additional_image_sizes = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_additional_image_sizes');
$wp_get_additional_image_sizes->expects($this->once())
->willReturn([]);

$wp_get_attachment_url = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_attachment_url');
$wp_get_attachment_url->expects($this->once())
->with($this->identicalTo(42))
Expand All @@ -237,6 +253,10 @@ public function testGenerateScaledDownImageWhenSizeIsntInImageSizeArray()
$is_admin->expects($this->once())
->willReturn(false);

$wp_get_additional_image_sizes = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_additional_image_sizes');
$wp_get_additional_image_sizes->expects($this->once())
->willReturn([]);

$this->assertFalse((new ContentDeliveryNetworkImageProcessingSubscriber($this->getImageSizes(), true, 'https://assets.com/uploads'))->generateScaledDownImage(false, 42, 'missing'));
}

Expand Down Expand Up @@ -633,6 +653,10 @@ public function testRewriteContentImageUrlsUsingWordPress(string $content, strin
->with($this->identicalTo('42'))
->willReturn($post);

$wp_get_additional_image_sizes = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_additional_image_sizes');
$wp_get_additional_image_sizes->expects($this->any())
->willReturn([]);

$wp_get_attachment_image_src = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_attachment_image_src');
$wp_get_attachment_image_src->expects($this->once())
->with($this->identicalTo('42'), $this->identicalTo($expectedSize))
Expand Down

0 comments on commit fff7639

Please sign in to comment.