|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
|
6
|
|
|
* and flexible |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://nystudio107.com |
|
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace nystudio107\seomatic\models; |
|
13
|
|
|
|
|
14
|
|
|
use nystudio107\seomatic\Seomatic; |
|
15
|
|
|
use nystudio107\seomatic\base\FrontendTemplate; |
|
16
|
|
|
use nystudio107\seomatic\base\SitemapInterface; |
|
17
|
|
|
use nystudio107\seomatic\jobs\GenerateSitemap; |
|
18
|
|
|
|
|
19
|
|
|
use Craft; |
|
20
|
|
|
|
|
21
|
|
|
use yii\caching\TagDependency; |
|
22
|
|
|
use yii\web\NotFoundHttpException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
26
|
|
|
* @package Seomatic |
|
27
|
|
|
* @since 3.0.0 |
|
|
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
|
|
29
|
|
|
class SitemapTemplate extends FrontendTemplate implements SitemapInterface |
|
30
|
|
|
{ |
|
31
|
|
|
// Constants |
|
32
|
|
|
// ========================================================================= |
|
33
|
|
|
|
|
34
|
|
|
const TEMPLATE_TYPE = 'SitemapTemplate'; |
|
35
|
|
|
|
|
36
|
|
|
const CACHE_KEY = 'seomatic_sitemap_'; |
|
37
|
|
|
|
|
38
|
|
|
const SITEMAP_CACHE_TAG = 'seomatic_sitemap_'; |
|
39
|
|
|
|
|
40
|
|
|
const FILE_TYPES = [ |
|
41
|
|
|
'excel', |
|
42
|
|
|
'pdf', |
|
43
|
|
|
'illustrator', |
|
44
|
|
|
'powerpoint', |
|
45
|
|
|
'text', |
|
46
|
|
|
'word', |
|
47
|
|
|
'xml', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
// Static Methods |
|
51
|
|
|
// ========================================================================= |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $config |
|
|
|
|
|
|
55
|
|
|
* |
|
56
|
|
|
* @return null|SitemapTemplate |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function create(array $config = []) |
|
59
|
|
|
{ |
|
60
|
|
|
$defaults = [ |
|
61
|
|
|
'path' => 'sitemaps/<groupId:\d+>/<type:[-\w\.*]+>/<handle:[-\w\.*]+>/<siteId:\d+>/<file:[-\w\.*]+>', |
|
62
|
|
|
'template' => '', |
|
63
|
|
|
'controller' => 'sitemap', |
|
64
|
|
|
'action' => 'sitemap', |
|
65
|
|
|
]; |
|
66
|
|
|
$config = array_merge($config, $defaults); |
|
67
|
|
|
|
|
68
|
|
|
return new SitemapTemplate($config); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Public Properties |
|
72
|
|
|
// ========================================================================= |
|
73
|
|
|
|
|
74
|
|
|
// Public Methods |
|
75
|
|
|
// ========================================================================= |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function rules(): array |
|
81
|
|
|
{ |
|
82
|
|
|
$rules = parent::rules(); |
|
83
|
|
|
$rules = array_merge($rules, [ |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
return $rules; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @inheritdoc |
|
91
|
|
|
*/ |
|
92
|
|
|
public function fields(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return parent::fields(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
|
|
|
|
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
100
|
|
|
public function render(array $params = []): string |
|
101
|
|
|
{ |
|
102
|
|
|
$groupId = $params['groupId']; |
|
103
|
|
|
$type = $params['type']; |
|
104
|
|
|
$handle = $params['handle']; |
|
105
|
|
|
$siteId = $params['siteId']; |
|
106
|
|
|
|
|
107
|
|
|
$metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle($type, $handle, $siteId); |
|
108
|
|
|
// If it's disabled, just throw a 404 |
|
109
|
|
|
if ($metaBundle === null || !$metaBundle->metaSitemapVars->sitemapUrls) { |
|
110
|
|
|
throw new NotFoundHttpException(Craft::t('seomatic', 'Page not found.')); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$cache = Craft::$app->getCache(); |
|
114
|
|
|
$cacheKey = $this::CACHE_KEY.$groupId.$type.$handle.$siteId; |
|
115
|
|
|
$result = $cache->get($cacheKey); |
|
116
|
|
|
// If the sitemap isn't cached, start a job to create it |
|
117
|
|
|
if ($result === false) { |
|
118
|
|
|
$queue = Craft::$app->getQueue(); |
|
119
|
|
|
$jobId = $queue->push(new GenerateSitemap([ |
|
120
|
|
|
'groupId' => $groupId, |
|
121
|
|
|
'type' => $type, |
|
122
|
|
|
'handle' => $handle, |
|
123
|
|
|
'siteId' => $siteId, |
|
124
|
|
|
])); |
|
125
|
|
|
$queue->run(); |
|
126
|
|
|
Craft::debug( |
|
127
|
|
|
Craft::t( |
|
128
|
|
|
'seomatic', |
|
129
|
|
|
'Started GenerateSitemap queue job id: {jobId}', |
|
130
|
|
|
[ |
|
131
|
|
|
'jobId' => $jobId, |
|
132
|
|
|
] |
|
133
|
|
|
), |
|
134
|
|
|
__METHOD__ |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
// Return an empty XML document |
|
138
|
|
|
$lines[] = '<?xml version="1.0" encoding="UTF-8"?>'; |
|
|
|
|
|
|
139
|
|
|
$lines[] = '<!-- ' . Craft::t('seomatic', 'This sitemap has not been generated yet.') . ' -->'; |
|
140
|
|
|
$lines[] = '<urlset>'; |
|
141
|
|
|
$lines[] = '</urlset>'; |
|
142
|
|
|
$lines = implode("\r\n", $lines); |
|
143
|
|
|
|
|
144
|
|
|
return $lines; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $result; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Invalidate a sitemap cache |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $handle |
|
|
|
|
|
|
154
|
|
|
* @param int $siteId |
|
|
|
|
|
|
155
|
|
|
*/ |
|
156
|
|
|
public function invalidateCache(string $handle, int $siteId) |
|
157
|
|
|
{ |
|
158
|
|
|
$cache = Craft::$app->getCache(); |
|
159
|
|
|
TagDependency::invalidate($cache, $this::SITEMAP_CACHE_TAG.$handle.$siteId); |
|
160
|
|
|
Craft::info( |
|
161
|
|
|
'Sitemap cache cleared: '.$handle, |
|
162
|
|
|
__METHOD__ |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|