-
Notifications
You must be signed in to change notification settings - Fork 20
/
Tag.php
74 lines (60 loc) · 1.97 KB
/
Tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace V17Development\FlarumSeo\Managers;
use Flarum\Tags\TagRepository;
use V17Development\FlarumSeo\Listeners\PageListener;
/**
* Class Tag
* @package V17Development\FlarumSeo\Managers
*/
class Tag
{
// Parent and Tag Repository
protected $parent;
/**
* @var TagRepository $tagRepository
*/
protected $tagRepository;
/**
* @param TagRepository $tagRepository
*/
public function __construct(TagRepository $tagRepository)
{
$this->tagRepository = $tagRepository;
}
/**
* @param PageListener $parent
* @param $tag
*/
public function handle(PageListener $parent, $tagId)
{
// I do support it, but it didn't work
if(!is_numeric($tagId))
{
$tagId = $this->tagRepository->getIdForSlug($tagId);
}
try {
// Find tag
$tag = $this->tagRepository->findOrFail($tagId);
if(!method_exists($tag, "getAttribute")) return;
$lastPostedAt = (new \DateTime($tag->getAttribute('last_posted_at')))->format("c");
// The tag plugin does not set page titles... Then we'll do that
$parent
->setPageTitle($tag->getAttribute('name'))
->setTitle($tag->getAttribute('name'));
$parent
// Add Schema.org metadata: CollectionPage https://schema.org/CollectionPage
->setSchemaJson('@type', 'CollectionPage')
->setSchemaJson('about', $tag->getAttribute('description'))
->setUpdatedOn($lastPostedAt)
// Tag URL
->setUrl('/t/' . $tag->getAttribute('slug'))
// Description
->setDescription($tag->getAttribute('description'))
// Canonical url
->setCanonicalUrl('/t/' . $tag->getAttribute('slug'));
}
catch (\Exception $e) {
// Do nothing. It just did not work
}
}
}