Skip to content

Commit

Permalink
Add EmbedX support
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzipp committed Oct 27, 2023
1 parent cab2f66 commit af7b7ef
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
9 changes: 9 additions & 0 deletions config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

// <meta name="twitter:title" content="..." />
'twitter' => true,

// <meta name="embedx:title" content="..." />
'embedx' => true,
],

'description' => [
Expand All @@ -27,6 +30,9 @@

// <meta name="twitter:description" content="..." />
'twitter' => true,

// <meta name="embedx:description" content="..." />
'embedx' => true,
],

'image' => [
Expand All @@ -38,6 +44,9 @@

// <meta name="twitter:image" content="..." />
'twitter' => true,

// <meta name="embedx:image" content="..." />
'embedx' => true,
],
],

Expand Down
33 changes: 33 additions & 0 deletions src/Services/Traits/ShorthandSetterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function title(string $title = null, bool $escape = true): self
Twitter::make()->name('title')->content($title, $escape)
);

$this->addIf(
$config['embedx'] ?? true,
Meta\EmbedX::make()->name('title')->content($title, $escape)
);

return $this;
}

Expand Down Expand Up @@ -74,6 +79,11 @@ public function description(string $description = null, bool $escape = true): se
Twitter::make()->name('description')->content($description, $escape)
);

$this->addIf(
$config['embedx'] ?? true,
Meta\EmbedX::make()->name('description')->content($description, $escape)
);

return $this;
}

Expand Down Expand Up @@ -104,6 +114,11 @@ public function image(string $image = null, bool $escape = true): self
Twitter::make()->name('image')->content($image, $escape)
);

$this->addIf(
$config['embedx'] ?? true,
Meta\EmbedX::make()->name('image')->content($image, $escape)
);

return $this;
}

Expand Down Expand Up @@ -155,6 +170,24 @@ public function og(string $property, $content = null, bool $escape = true): self
);
}

/**
* Add EmbedX struct.
*
* @see https://embedx.app
*
* @param string $property
* @param mixed|null $content
* @param bool $escape
*
* @return $this
*/
public function embedx(string $property, $content = null, bool $escape = true): self
{
return $this->add(
Meta\EmbedX::make()->name($property)->content($content, $escape)
);
}

/**
* Add the meta charset struct.
*
Expand Down
29 changes: 29 additions & 0 deletions src/Structs/Meta/EmbedX.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace romanzipp\Seo\Structs\Meta;

use romanzipp\Seo\Structs\Meta;

/**
* @see https://github.com/joshbuchea/HEAD#twitter-card
* @see https://embedx.app
*/
class EmbedX extends Meta
{
protected $unique = true;

protected $uniqueAttributes = ['name'];

/**
* @param mixed|null $value
* @param bool $escape
*
* @return $this
*/
public function name($value = null, bool $escape = true)
{
$this->addAttribute('name', "embedx:{$value}", $escape);

return $this;
}
}
6 changes: 4 additions & 2 deletions tests/ArrayFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testTitleIndex()
'title' => 'Foo',
]);

$this->assertCount(3, seo()->getStructs());
$this->assertCount(4, seo()->getStructs());

$this->assertInstanceOf(Title::class, $struct = seo()->getStruct(Title::class));
$this->assertEquals('Foo', (string) $struct->getBody());
Expand All @@ -37,6 +37,7 @@ public function testTitleIndexTagOnly()
'seo.shorthand.title.tag' => true,
'seo.shorthand.title.opengraph' => false,
'seo.shorthand.title.twitter' => false,
'seo.shorthand.title.embedx' => false,
]);

seo()->addFromArray([
Expand All @@ -57,7 +58,7 @@ public function testDescriptionIndex()
'description' => 'Foo',
]);

$this->assertCount(3, seo()->getStructs());
$this->assertCount(4, seo()->getStructs());

$this->assertInstanceOf(Description::class, $struct = seo()->getStruct(Description::class));
$this->assertEquals('Foo', (string) $struct->getComputedAttribute('content'));
Expand All @@ -75,6 +76,7 @@ public function testDescriptionIndexTagOnly()
'seo.shorthand.description.tag' => true,
'seo.shorthand.description.opengraph' => false,
'seo.shorthand.description.twitter' => false,
'seo.shorthand.description.embedx' => false,
]);

seo()->addFromArray([
Expand Down
19 changes: 17 additions & 2 deletions tests/ShorthandSettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function testTitleSingleSetter()
'seo.shorthand.title.tag' => true,
'seo.shorthand.title.twitter' => false,
'seo.shorthand.title.opengraph' => false,
'seo.shorthand.title.embedx' => false,
]);

seo()->title('My Title');
Expand All @@ -32,13 +33,14 @@ public function testTitleMultipleSetter()
'seo.shorthand.title.tag' => true,
'seo.shorthand.title.twitter' => true,
'seo.shorthand.title.opengraph' => true,
'seo.shorthand.title.embedx' => true,
]);

seo()->title('My Title');

$contents = seo()->render()->toArray();

$this->assertCount(3, $contents);
$this->assertCount(4, $contents);
}

public function testDescriptionSingleSetter()
Expand All @@ -47,6 +49,7 @@ public function testDescriptionSingleSetter()
'seo.shorthand.description.meta' => true,
'seo.shorthand.description.twitter' => false,
'seo.shorthand.description.opengraph' => false,
'seo.shorthand.description.embedx' => false,
]);

seo()->description('My Description');
Expand All @@ -62,13 +65,14 @@ public function testDescriptionMultipleSetter()
'seo.shorthand.description.meta' => true,
'seo.shorthand.description.twitter' => true,
'seo.shorthand.description.opengraph' => true,
'seo.shorthand.description.embedx' => true,
]);

seo()->description('My Description');

$contents = seo()->render()->toArray();

$this->assertCount(3, $contents);
$this->assertCount(4, $contents);
}

public function testTwitterSetter()
Expand All @@ -93,6 +97,17 @@ public function testOpenGraphSetter()
$this->assertInstanceOf(OpenGraph::class, seo()->getStructs()[0]);
}

public function testEmbedXSetter()
{
seo()->embedx('title', 'My Site Name');

$contents = seo()->render()->toArray();

$this->assertCount(1, $contents);

$this->assertInstanceOf(Meta\EmbedX::class, seo()->getStructs()[0]);
}

public function testMetaSetter()
{
seo()->meta('author', 'My Little Pony');
Expand Down

0 comments on commit af7b7ef

Please sign in to comment.