-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
SitemapTemplate.php
212 lines (183 loc) · 6.44 KB
/
SitemapTemplate.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* SEOmatic plugin for Craft CMS 3.x
*
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
* and flexible
*
* @link https://nystudio107.com
* @copyright Copyright (c) 2017 nystudio107
*/
namespace nystudio107\seomatic\models;
use Craft;
use craft\console\Application as ConsoleApplication;
use nystudio107\fastcgicachebust\FastcgiCacheBust;
use nystudio107\seomatic\base\FrontendTemplate;
use nystudio107\seomatic\base\SitemapInterface;
use nystudio107\seomatic\helpers\SiteHelper;
use nystudio107\seomatic\helpers\Sitemap;
use nystudio107\seomatic\Seomatic;
use yii\caching\TagDependency;
use yii\web\NotFoundHttpException;
/**
* @author nystudio107
* @package Seomatic
* @since 3.0.0
*/
class SitemapTemplate extends FrontendTemplate implements SitemapInterface
{
// Constants
// =========================================================================
const TEMPLATE_TYPE = 'SitemapTemplate';
const CACHE_KEY = 'seomatic_sitemap_';
const QUEUE_JOB_CACHE_KEY = 'seomatic_sitemap_queue_job_';
const SITEMAP_CACHE_TAG = 'seomatic_sitemap_';
const FILE_TYPES = [
'excel',
'pdf',
'illustrator',
'powerpoint',
'text',
'word',
'xml',
];
// Static Methods
// =========================================================================
/**
* @param array $config
*
* @return null|SitemapTemplate
*/
public static function create(array $config = [])
{
$defaults = [
'path' => 'sitemaps-<groupId:\d+>-<type:[\w\.*]+>-<handle:[\w\.*]+>-<siteId:\d+>-<file:[-\w\.*]+>',
'template' => '',
'controller' => 'sitemap',
'action' => 'sitemap',
];
$config = array_merge($config, $defaults);
return new SitemapTemplate($config);
}
// Public Properties
// =========================================================================
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function rules(): array
{
$rules = parent::rules();
$rules = array_merge($rules, [
]);
return $rules;
}
/**
* @inheritdoc
*/
public function fields(): array
{
return parent::fields();
}
/**
* @inheritdoc
*/
public function render(array $params = []): string
{
$groupId = $params['groupId'];
$type = $params['type'];
$handle = $params['handle'];
$siteId = $params['siteId'];
$page = $params['page'] ?? 0;
$request = Craft::$app->getRequest();
$metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle($type, $handle, $siteId);
// If it doesn't exist, throw a 404
if ($metaBundle === null) {
if ($request->isCpRequest || $request->isConsoleRequest) {
return '';
}
throw new NotFoundHttpException(Craft::t('seomatic', 'Page not found.'));
}
// Check to see if robots is `none` or `no index`
$robotsEnabled = true;
if (!empty($metaBundle->metaGlobalVars->robots)) {
$robotsEnabled = $metaBundle->metaGlobalVars->robots !== 'none' &&
$metaBundle->metaGlobalVars->robots !== 'noindex';
}
$sitemapUrls = $metaBundle->metaSitemapVars->sitemapUrls;
if (Seomatic::$plugin->sitemaps->anyEntryTypeHasSitemapUrls($metaBundle)) {
$robotsEnabled = true;
$sitemapUrls = true;
}
if ($sitemapUrls && !SiteHelper::siteEnabledWithUrls($siteId)) {
$sitemapUrls = false;
}
// If it's disabled, just throw a 404
if (!$sitemapUrls || !$robotsEnabled) {
if ($request->isCpRequest || $request->isConsoleRequest) {
return '';
}
throw new NotFoundHttpException(Craft::t('seomatic', 'Page not found.'));
}
$cache = Craft::$app->getCache();
$pageCacheSuffix = 's' . (int)$metaBundle->metaSitemapVars->sitemapPageSize . 'p' . $page;
$uniqueKey = $groupId . $type . $handle . $siteId . $pageCacheSuffix;
$cacheKey = self::CACHE_KEY . $uniqueKey;
$result = $cache->get($cacheKey);
// If the sitemap isn't cached, render it immediately
if ($result === false) {
$sitemap = Sitemap::generateSitemap([
'groupId' => $groupId,
'type' => $type,
'handle' => $handle,
'siteId' => $siteId,
'page' => $page,
]);
if ($sitemap) {
$dependency = new TagDependency([
'tags' => [
self::GLOBAL_SITEMAP_CACHE_TAG,
self::SITEMAP_CACHE_TAG . $handle . $siteId,
self::SITEMAP_CACHE_TAG . $handle . $siteId . $pageCacheSuffix,
],
]);
$cacheDuration = Seomatic::$devMode
? Seomatic::DEVMODE_CACHE_DURATION
: null;
$result = $cache->set($cacheKey, $sitemap, $cacheDuration, $dependency);
// Output some info if this is a console app
if (Craft::$app instanceof ConsoleApplication) {
echo 'Sitemap cache result: ' . print_r($result, true) . ' for cache key: ' . $cacheKey . PHP_EOL;
}
// If the FastCGI Cache Bust plugin is installed, clear its caches too
/** @var FastcgiCacheBust $plugin */
$plugin = Craft::$app->getPlugins()->getPlugin('fastcgi-cache-bust');
if ($plugin !== null) {
$plugin->cache->clearAll();
}
return $sitemap;
}
} else {
if (Craft::$app instanceof ConsoleApplication) {
echo 'Found in cache' . PHP_EOL;
}
}
return $result;
}
/**
* Invalidate a sitemap cache
*
* @param string $handle
* @param int $siteId
*/
public function invalidateCache(string $handle, int $siteId)
{
$cache = Craft::$app->getCache();
TagDependency::invalidate($cache, self::SITEMAP_CACHE_TAG . $handle . $siteId);
Craft::info(
'Sitemap cache cleared: ' . $handle,
__METHOD__
);
}
}