From b915263ea6bbae17ce4ed963b20d857932d052a8 Mon Sep 17 00:00:00 2001 From: Marco Rieser Date: Tue, 7 Feb 2023 15:36:43 +0100 Subject: [PATCH] extract missing image alt widget to addon --- src/ServiceProvider.php | 4 ++++ src/Widgets/ImagesMissingAlt.php | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/Widgets/ImagesMissingAlt.php diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index ac490ce..c8ef727 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -14,6 +14,10 @@ class ServiceProvider extends AddonServiceProvider 'web' => __DIR__ . '/../routes/web.php', ]; + protected $widgets = [ + \Studio1902\PeakSeo\Widgets\ImagesMissingAlt::class + ]; + public function bootAddon() { $this->registerPublishableViews(); diff --git a/src/Widgets/ImagesMissingAlt.php b/src/Widgets/ImagesMissingAlt.php new file mode 100644 index 0000000..5b9eae1 --- /dev/null +++ b/src/Widgets/ImagesMissingAlt.php @@ -0,0 +1,41 @@ +addMinutes($this->config('expiry', 0)); + + $assets = Cache::remember('widgets::ImagesMissingAlt', $expiration, function() { + return Asset::query() + ->where('container', $this->config('container', 'assets')) + ->whereNull('alt') + ->whereIn('extension', $this->config('filetypes', ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'bmp', 'tiff', 'svg'])) + ->orderBy('last_modified', 'desc') + ->limit(100) + ->get() + ->toAugmentedArray(); + }); + + $assets = collect($assets); + + return view('statamic-peak-seo::widgets.images-missing-alt', [ + 'assets' => $assets->slice(0, $this->config('limit', 5)), + 'amount' => count($assets), + 'container' => AssetContainer::findByHandle($this->config('container', 'assets'))->title(), + ]); + } +}