From 5c3b2393796788d3e9bf86d6d1f3f5daa6fe3885 Mon Sep 17 00:00:00 2001 From: Quentin Gabriele Date: Fri, 13 Sep 2024 16:58:59 +0200 Subject: [PATCH] add standard meta --- README.md | 10 ++- config/seo.php | 50 +++++++++++ src/SeoManager.php | 88 +++++++++++++++---- .../{StandardData.php => Standard.php} | 26 +++++- tests/Units/SeoManagerTest.php | 4 +- tests/Units/StandardTest.php | 4 +- 6 files changed, 155 insertions(+), 27 deletions(-) rename src/Standard/{StandardData.php => Standard.php} (71%) diff --git a/README.md b/README.md index 21ad86d..e18d797 100644 --- a/README.md +++ b/README.md @@ -240,7 +240,11 @@ class HomeController extends Controller hreflang: "fr", href: route('home', ['locale' => "fr"]), ), - ]); + ]) + ->setOpengraph(function(OpenGraph $opengraph){ + $opengraph->title = "Custom opengraph title"; + return $opengraph; + }); // Using the facade SeoManager::current() @@ -267,14 +271,14 @@ For more complex SEO needs, you can instantiate and configure the `SeoManager` c ```php use Elegantly\Seo\SeoManager; -use Elegantly\Seo\Standard\StandardData; +use Elegantly\Seo\Standard\Standard; use Elegantly\Seo\OpenGraph\OpenGraph; use Elegantly\Seo\Twitter\Cards\Card; use Elegantly\Seo\Schemas\Schema; use Elegantly\Seo\SeoTags; $seo = new SeoManager( - standard: new StandardData(/* ... */), + standard: new Standard(/* ... */), opengraph: new OpenGraph(/* ... */), twitter: new Card(/* ... */), webpage: new WebPage(/* ... */), diff --git a/config/seo.php b/config/seo.php index 914cbbf..67009f2 100644 --- a/config/seo.php +++ b/config/seo.php @@ -23,6 +23,26 @@ */ 'description' => null, + /* + |-------------------------------------------------------------------------- + | Default Author + |-------------------------------------------------------------------------- + | + | This is the default value used for + | + */ + 'author' => null, + + /* + |-------------------------------------------------------------------------- + | Default Generator + |-------------------------------------------------------------------------- + | + | This is the default value used for + | + */ + 'generator' => null, + /* |-------------------------------------------------------------------------- | Default Keywords @@ -34,6 +54,36 @@ */ 'keywords' => null, + /* + |-------------------------------------------------------------------------- + | Default Referrer + |-------------------------------------------------------------------------- + | + | This is the default value used for + | + */ + 'referrer' => null, + + /* + |-------------------------------------------------------------------------- + | Default Theme color + |-------------------------------------------------------------------------- + | + | This is the default value used for + | + */ + 'theme-color' => null, + + /* + |-------------------------------------------------------------------------- + | Default Color Scheme + |-------------------------------------------------------------------------- + | + | This is the default value used for + | + */ + 'color-scheme' => null, + /* |-------------------------------------------------------------------------- | Default Image path diff --git a/src/SeoManager.php b/src/SeoManager.php index 9e88201..8f82840 100644 --- a/src/SeoManager.php +++ b/src/SeoManager.php @@ -2,13 +2,14 @@ namespace Elegantly\Seo; +use Closure; use Elegantly\Seo\Contracts\Taggable; use Elegantly\Seo\OpenGraph\Locale; use Elegantly\Seo\OpenGraph\OpenGraph; use Elegantly\Seo\Schemas\Schema; use Elegantly\Seo\Schemas\WebPage; use Elegantly\Seo\Standard\Alternate; -use Elegantly\Seo\Standard\StandardData; +use Elegantly\Seo\Standard\Standard; use Elegantly\Seo\Twitter\Cards\Card; use Elegantly\Seo\Twitter\Cards\Summary; use Illuminate\Contracts\Support\Htmlable; @@ -24,7 +25,7 @@ class SeoManager implements Htmlable, Stringable, Taggable * @param null|Schema[] $schemas */ public function __construct( - public ?StandardData $standard = null, + public ?Standard $standard = null, public ?OpenGraph $opengraph = null, public ?Card $twitter = null, public ?WebPage $webpage = null, @@ -41,31 +42,61 @@ public function current(): static } /** + * @param null|Standard|(Closure(Standard):null|Standard) $value * @return $this */ - public function setOpengraph(?OpenGraph $value): static + public function setStandard(null|Standard|Closure $value): static { - $this->opengraph = $value; + if ($value instanceof Closure) { + $this->standard = $value($this->standard ?? Standard::default()); + } else { + $this->standard = $value; + } return $this; } /** + * @param null|OpenGraph|(Closure(OpenGraph):null|OpenGraph) $value * @return $this */ - public function setTwitter(?Card $value): static + public function setOpengraph(null|OpenGraph|Closure $value): static { - $this->twitter = $value; + if ($value instanceof Closure) { + $this->opengraph = $value($this->opengraph ?? OpenGraph::default()); + } else { + $this->opengraph = $value; + } return $this; } /** + * @param null|Card|(Closure(Card):null|Card) $value * @return $this */ - public function setWebpage(?WebPage $value): static + public function setTwitter(null|Card|Closure $value): static { - $this->webpage = $value; + if ($value instanceof Closure) { + $this->twitter = $value($this->twitter ?? Summary::default()); + } else { + $this->twitter = $value; + } + + return $this; + } + + /** + * @param null|WebPage|(Closure(WebPage):null|WebPage) $value + * @return $this + */ + public function setWebpage(null|WebPage|Closure $value): static + { + if ($value instanceof Closure) { + $this->webpage = $value($this->webpage ?? WebPage::default()); + } else { + $this->webpage = $value; + } return $this; } @@ -198,6 +229,31 @@ public function setAlternates(?array $value): static return $this; } + /** + * @param null|string|string[] $value + * @return $this + */ + public function setKeywords(null|string|array $value): static + { + if ($this->standard) { + $this->standard->keywords = $value; + } + + return $this; + } + + /** + * @return $this + */ + public function setAuthor(?string $value): static + { + if ($this->standard) { + $this->standard->author = $value; + } + + return $this; + } + /** * @return $this */ @@ -229,14 +285,14 @@ public static function default( ?array $alternates = null, ): self { return new self( - standard: StandardData::default( - $title, - $url, - $description, - $keywords, - $robots, - $sitemap, - $alternates + standard: Standard::default( + title: $title, + canonical: $url, + description: $description, + keywords: $keywords, + robots: $robots, + sitemap: $sitemap, + alternates: $alternates ), opengraph: OpenGraph::default( title: $title, diff --git a/src/Standard/StandardData.php b/src/Standard/Standard.php similarity index 71% rename from src/Standard/StandardData.php rename to src/Standard/Standard.php index d5d6eb3..8b8a0c9 100644 --- a/src/Standard/StandardData.php +++ b/src/Standard/Standard.php @@ -10,7 +10,10 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Request; -class StandardData implements Taggable +/** + * @see https://developer.mozilla.org/fr/docs/Web/HTML/Element/meta/name + */ +class Standard implements Taggable { /** * @param null|string|string[] $keywords @@ -19,8 +22,13 @@ class StandardData implements Taggable public function __construct( public string $title, public string $canonical, + public ?string $author = null, public ?string $description = null, + public ?string $generator = null, public null|string|array $keywords = null, + public ?string $referrer = null, + public ?string $themeColor = null, + public ?string $colorScheme = null, public ?string $robots = null, public ?string $sitemap = null, public ?array $alternates = null, @@ -35,17 +43,27 @@ public function __construct( public static function default( ?string $title = null, ?string $canonical = null, + ?string $author = null, ?string $description = null, + ?string $generator = null, null|string|array $keywords = null, + ?string $referrer = null, + ?string $themeColor = null, + ?string $colorScheme = null, ?string $robots = null, ?string $sitemap = null, ?array $alternates = null, ): self { return new self( - title: $title ?? __(config('seo.defaults.title') ?? config('app.name')), + title: $title ?? config('seo.defaults.title') ?? config('app.name'), canonical: $canonical ?? Request::url(), - description: $description ?? __(config('seo.defaults.description')), - keywords: $keywords ?? __(config('seo.defaults.keywords')), + author: $author ?? config('seo.defaults.author'), + description: $description ?? config('seo.defaults.description'), + generator: $generator ?? config('seo.defaults.generator'), + keywords: $keywords ?? config('seo.defaults.keywords'), + referrer: $referrer ?? config('seo.defaults.referrer'), + themeColor: $themeColor ?? config('seo.defaults.theme-color'), + colorScheme: $colorScheme ?? config('seo.defaults.color-scheme'), robots: $robots ?? config('seo.defaults.robots'), sitemap: $sitemap ?? config('seo.defaults.sitemap'), alternates: $alternates, diff --git a/tests/Units/SeoManagerTest.php b/tests/Units/SeoManagerTest.php index 1e95c3c..a7181af 100644 --- a/tests/Units/SeoManagerTest.php +++ b/tests/Units/SeoManagerTest.php @@ -6,13 +6,13 @@ use Elegantly\Seo\OpenGraph\Verticals\Website; use Elegantly\Seo\SeoManager; use Elegantly\Seo\Standard\Alternate; -use Elegantly\Seo\Standard\StandardData; +use Elegantly\Seo\Standard\Standard; use Elegantly\Seo\Twitter\Cards\Summary; use Elegantly\Seo\Twitter\Image as TwitterImage; it('renders all standard, opengraph and twitter tags', function () { $manager = new SeoManager( - standard: new StandardData( + standard: new Standard( title: 'Foo', canonical: 'https://example.com/standard', description: 'Bar', diff --git a/tests/Units/StandardTest.php b/tests/Units/StandardTest.php index 13683a3..6f39bba 100644 --- a/tests/Units/StandardTest.php +++ b/tests/Units/StandardTest.php @@ -1,10 +1,10 @@