Skip to content

Commit

Permalink
Merge pull request #473 from hydephp/471-bug-ogtitle-and-twittertitle…
Browse files Browse the repository at this point in the history
…-should-use-the-page-title-and-only-use-config-one-as-fallback

Fix Bug #471: og:title and twitter:title should use the page title, and only use config one as fallback
  • Loading branch information
caendesilva authored May 30, 2022
2 parents b3d2779 + 49af0e0 commit c182c00
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Concerns/HasPageMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Hyde\Framework\Services\SitemapService;

/**
* @todo Move logic into service class to make it easier to test.
*
* @see \Tests\Feature\Concerns\HasPageMetadataTest
*/
trait HasPageMetadata
Expand Down Expand Up @@ -38,6 +40,15 @@ public function getDynamicMetadata(): array
.'" />';
}

if (isset($this->title)) {
if ($this->hasTwitterTitleInConfig()) {
$array[] = '<meta name="twitter:title" content="'.config('hyde.name', 'HydePHP').' - '.$this->title.'" />';
}
if ($this->hasOpenGraphTitleInConfig()) {
$array[] = '<meta property="og:title" content="'.config('hyde.name', 'HydePHP').' - '.$this->title.'" />';
}
}

if ($this instanceof MarkdownPost) {
// Temporarily merge data with GeneratesPageMetadata trait for compatibility
$array[] = "\n<!-- Blog Post Meta Tags -->";
Expand Down Expand Up @@ -89,4 +100,14 @@ public function canUseRssFeedLink(): bool

return false;
}

public function hasTwitterTitleInConfig(): bool
{
return str_contains(json_encode(config('hyde.meta', [])), 'twitter:title');
}

public function hasOpenGraphTitleInConfig(): bool
{
return str_contains(json_encode(config('hyde.meta', [])), 'og:title');
}
}
75 changes: 75 additions & 0 deletions tests/Feature/Concerns/HasPageMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,79 @@ public function test_get_dynamic_metadata_does_not_add_sitemap_link_when_conditi
$page->getDynamicMetadata()
);
}

public function test_has_twitter_title_in_config_returns_true_when_present_in_config()
{
config(['hyde.meta' => [
Meta::name('twitter:title', 'foo'),
]]);

$page = new class
{
use HasPageMetadata;
};

$this->assertTrue($page->hasTwitterTitleInConfig());
}

public function test_has_twitter_title_in_config_returns_false_when_not_present_in_config()
{
config(['hyde.meta' => []]);

$page = new class
{
use HasPageMetadata;
};

$this->assertFalse($page->hasTwitterTitleInConfig());
}

public function test_has_open_graph_title_in_config_returns_true_when_present_in_config()
{
config(['hyde.meta' => [
Meta::property('title', 'foo'),
]]);

$page = new class
{
use HasPageMetadata;
};

$this->assertTrue($page->hasOpenGraphTitleInConfig());
}

public function test_has_open_graph_title_in_config_returns_false_when_not_present_in_config()
{
config(['hyde.meta' => []]);

$page = new class
{
use HasPageMetadata;
};

$this->assertFalse($page->hasOpenGraphTitleInConfig());
}

// Test meta titles can be dynamically added and override defaults
public function test_get_dynamic_metadata_adds_twitter_and_open_graph_title_when_conditions_are_met()
{
config(['hyde.meta' => [
Meta::name('twitter:title', 'foo'),
Meta::property('title', 'foo'),
]]);

$page = new class extends AbstractPage
{
use HasPageMetadata;

public string $title = 'bar';
};

$this->assertEquals([
'<meta name="twitter:title" content="HydePHP - bar" />',
'<meta property="og:title" content="HydePHP - bar" />',
],
$page->getDynamicMetadata()
);
}
}

0 comments on commit c182c00

Please sign in to comment.