Skip to content

Commit

Permalink
Merge pull request #10 from offbeatwp/feature/lesser-improvements
Browse files Browse the repository at this point in the history
Feature/lesser improvements
  • Loading branch information
maikelRAOW authored May 22, 2024
2 parents b7f4424 + 069de20 commit fe878fc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 28 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
}
],
"require": {
"offbeatwp/framework": "2.*"
"offbeatwp/framework": "^2"
},
"require-dev": {
"phpstan/phpstan": "^1.11",
"szepeviktor/phpstan-wordpress": "^1.3"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"lint:css": "wp-scripts lint-style",
"lint:js": "wp-scripts lint-js",
"packages-update": "wp-scripts packages-update",
"start": "wp-scripts start --output-path=./build/ --webpack-src-dir=src/scripts"
"start": "wp-scripts start --output-path=./build/ --webpack-src-dir=src/scripts",
"analyse": "php vendor/bin/phpstan analyse --memory-limit=3G"
},
"devDependencies": {
"@wordpress/i18n": "^4.2.4",
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# phpstan.neon
parameters:
level: 5
phpVersion: 80200
treatPhpDocTypesAsCertain: false
paths:
- src
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
11 changes: 11 additions & 0 deletions src/.phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PHPSTORM_META {
override(\offbeat(), map([
'images' => OffbeatWP\Images\Repositories\ImagesRepository::class
]));

override(\container(), map([
'images' => OffbeatWP\Images\Repositories\ImagesRepository::class
]));
}
64 changes: 39 additions & 25 deletions src/Helpers/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@

namespace OffbeatWP\Images\Helpers;

use Error;

final class ImageHelper
{
public const MIN_VIEWPORT_WIDTH = 320;
public const MAX_WIDTH_INTERVAL = 200;
public const MAX_VIEWPORT_WIDTH = 2000;

/**
* @param int $attachment
* @param int $attachmentId
* @param array{url?: string, class?: string, loading?: string, alt?: string, sizes?: string[], aspectRatio?: string, lightbox?: bool, containedMaxWidth?: string|int|float} $args
* @return string
*/
public function generateResponsiveImage(int $attachment, array $args = []): string
public function generateResponsiveImage($attachmentId, array $args = []): string
{
$args = apply_filters('offbeat/responsiveImage/args', $args, $attachment);
$args = apply_filters('offbeat/responsiveImage/args', $args, $attachmentId);

$containedMaxWidth = apply_filters('offbeat/responsiveImage/containedMaxWidth', $args['containedMaxWidth'] ?? null, $args);
$sizes = apply_filters('offbeat/responsiveImage/sizes', $args['sizes'] ?? null, $args);
/** @var array<int|string, string> $rawSizes */
$rawSizes = apply_filters('offbeat/responsiveImage/sizes', $args['sizes'] ?? null, $args);
$aspectRatio = apply_filters('offbeat/responsiveImage/aspectRatio', $args['aspectRatio'] ?? null, $args);

if (!$sizes || !is_array($sizes)) {
$sizes = [];
if (!$rawSizes || !is_array($rawSizes)) {
$sizes = [0 => '100%'];
}

$sizes = $this->cleanSizes($sizes);
$sizes = $this->transformSizes($sizes, $containedMaxWidth);

$sources = $this->generateSources($attachment, $sizes, $aspectRatio ?? null);
$sources = $this->generateSources($attachmentId, $sizes, $aspectRatio ?? null);

return $this->generateResponsiveImageTag($attachment, $sources, $args);
return $this->generateResponsiveImageTag($attachmentId, $sources, $args);
}

/**
Expand All @@ -53,10 +57,23 @@ protected function cleanSizes(array $sizes): array
return $sizes;
}

private function getViewportWidth(string $containedMaxWidth): string
{
$result = preg_replace_callback('/^(?<percentage>\d+(\.\d+)?)%$/', function ($matches) {
return floor((float)$matches['percentage']) . 'vw';
}, $containedMaxWidth);

if ($result === null) {
throw new Error(preg_last_error_msg());
}

return $result;
}

/**
* @param string[] $sizes
* @param string|int|float $containedMaxWidth
* @return string[]
* @return string[]|float[]
*/
protected function transformSizes(array $sizes, $containedMaxWidth): array
{
Expand All @@ -70,13 +87,11 @@ protected function transformSizes(array $sizes, $containedMaxWidth): array
$containedMaxWidth = '100vw';
}

// if the contained max width is percentage convert it to viewport width
// if the contained max width is percentage convert it to viewport width, otherwise convert it to an integer
if (is_numeric($containedMaxWidth)) {
$containedMaxWidth = (int)$containedMaxWidth;
$convertedMaxWidth = (int)$containedMaxWidth;
} else {
$containedMaxWidth = preg_replace_callback('/^(?<percentage>\d+(\.\d+)?)%$/', function ($matches) {
return floor((float)$matches['percentage']) . 'vw';
}, $containedMaxWidth);
$convertedMaxWidth = $this->getViewportWidth($containedMaxWidth);
}

// Remove all sizes where key is not a number
Expand All @@ -97,15 +112,15 @@ protected function transformSizes(array $sizes, $containedMaxWidth): array
$percentage = (float)$matches['percentage'];

// Make calculation when the containedMaxWidth is based on the viewport width
if (preg_match('/^(?<viewportWidth>\d+)vw$/', $containedMaxWidth, $matches)) {
if (preg_match('/^(?<viewportWidth>\d+)vw$/', $convertedMaxWidth, $matches)) {
$imageWidth = floor((int)$matches['viewportWidth'] * ($percentage / 100)) . 'vw';
} elseif (is_numeric($containedMaxWidth)) {
} elseif (is_numeric($convertedMaxWidth)) {
// if breakpoint width is smaller then the contained max width
// we add a size width a relative width otherwise an absolute width
if ($breakpointWidth < $containedMaxWidth) {
if ($breakpointWidth < $convertedMaxWidth) {
$imageWidth = floor($percentage) . 'vw';
} else {
$imageWidth = ceil((int)$containedMaxWidth * ($percentage / 100)) . 'px';
$imageWidth = ceil((int)$convertedMaxWidth * ($percentage / 100)) . 'px';
}
}
} elseif (is_numeric($imageSize)) {
Expand All @@ -123,14 +138,14 @@ protected function transformSizes(array $sizes, $containedMaxWidth): array
// 2. If there is no next breakpoint, but we didn't reached the contained max width yet

if (
is_int($containedMaxWidth) &&
is_int($convertedMaxWidth) &&
is_float($percentage) &&
(
($nextBreakpointWidth && $breakpointWidth < $containedMaxWidth && $nextBreakpointWidth > $containedMaxWidth) ||
(!$nextBreakpointWidth && $breakpointWidth < $containedMaxWidth)
($nextBreakpointWidth && $breakpointWidth < $convertedMaxWidth && $nextBreakpointWidth > $convertedMaxWidth) ||
(!$nextBreakpointWidth && $breakpointWidth < $convertedMaxWidth)
)
) {
$sizesReturn[$containedMaxWidth] = ceil($containedMaxWidth * ($percentage / 100)) . 'px';
$sizesReturn[$convertedMaxWidth] = (float)ceil($convertedMaxWidth * ($percentage / 100)) . 'px';
}
}

Expand Down Expand Up @@ -198,7 +213,7 @@ public function calculateImageWidths(array $sizes): array
* @param bool $pixelDensitySrcSet
* @return string[]
*/
public function generateSrcSet(int $attachmentId, array $sizes, $aspectRatio = null, bool $pixelDensitySrcSet = false): array
public function generateSrcSet($attachmentId, array $sizes, $aspectRatio = null, bool $pixelDensitySrcSet = false): array
{
$srcSet = [];
$imageModifier = $aspectRatio ? 'c' : '';
Expand Down Expand Up @@ -347,7 +362,7 @@ protected function generateSizesAttribute(?array $sizes): ?string
/**
* @param int $attachmentId
* @param array{sizes: string[]|null[], media_query: string, srcset?: string[]}[] $sources
* @param array{url?: string, class?: string, loading?: string, alt?: string, sizes?: string[], aspectRatio?: string, lightbox?: bool, containedMaxWidth?: string|int|float} $args
* @param array{url?: string, class?: string, loading?: string, alt?: string, sizes?: string[], aspectRatio?: string, lightbox?: bool, containedMaxWidth?: string|int|float, caption?: string} $args
* @return string
*/
protected function generateResponsiveImageTag(int $attachmentId, array $sources, array $args): string
Expand All @@ -365,7 +380,6 @@ protected function generateResponsiveImageTag(int $attachmentId, array $sources,
'decoding' => $args['decoding'] ?? null
];


$styles = [];
$aspectRatio = $args['aspectRatio'] ?? null;
$objectFit = $args['objectFit'] ?? 'cover';
Expand Down Expand Up @@ -454,7 +468,7 @@ public static function calculateAspectRatio($aspectRatio, $attachmentId)
$originalImageSize = wp_get_attachment_image_src($attachmentId, 'full');

if (is_array($originalImageSize) && !empty($originalImageSize)) {
return intval($originalImageSize[1]) / intval($originalImageSize[2]);
return (int)$originalImageSize[1] / (int)$originalImageSize[2];
}

return 3 / 2;
Expand Down
3 changes: 2 additions & 1 deletion src/Hooks/FocalPointInitAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OffbeatWP\Images\Hooks;

use Error;
use OffbeatWP\Hooks\AbstractAction;
use WP_Post;

Expand Down Expand Up @@ -97,7 +98,7 @@ public function getImageInfoForFocalPointTriggerButton(WP_Post $post): array
}

public function enqueueAdminAssets() {
$entryBuildPath = dirname(__FILE__) . '/../../build';
$entryBuildPath = __DIR__ . '/../../build';
$assetPath = $entryBuildPath . '/index.asset.php';

if ( ! file_exists( $assetPath ) ) {
Expand Down

0 comments on commit fe878fc

Please sign in to comment.