Skip to content

Commit

Permalink
extract missing image alt widget to addon
Browse files Browse the repository at this point in the history
  • Loading branch information
marcorieser committed Feb 7, 2023
1 parent 1724e29 commit b915263
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
41 changes: 41 additions & 0 deletions src/Widgets/ImagesMissingAlt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Studio1902\PeakSeo\Widgets;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Statamic\Facades\Asset;
use Statamic\Facades\AssetContainer;
use Statamic\Widgets\Widget;

class ImagesMissingAlt extends Widget
{
/**
* The HTML that should be shown in the widget.
*
* @return string|\Illuminate\View\View
*/
public function html()
{
$expiration = Carbon::now()->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(),
]);
}
}

0 comments on commit b915263

Please sign in to comment.