Conditions | 69 |
Paths | > 20000 |
Total Lines | 347 |
Code Lines | 225 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
65 | public static function generateSitemap(array $params): ?string |
||
66 | { |
||
67 | $groupId = $params['groupId']; |
||
68 | $type = $params['type']; |
||
69 | $handle = $params['handle']; |
||
70 | $siteId = $params['siteId']; |
||
71 | $page = $params['page']; |
||
72 | |||
73 | // Get an array of site ids for this site group |
||
74 | $groupSiteIds = []; |
||
75 | |||
76 | if (Seomatic::$settings->siteGroupsSeparate) { |
||
77 | if (empty($groupId)) { |
||
78 | try { |
||
79 | $thisSite = Craft::$app->getSites()->getSiteById($siteId); |
||
80 | if ($thisSite !== null) { |
||
81 | $group = $thisSite->getGroup(); |
||
82 | $groupId = $group->id; |
||
83 | } |
||
84 | } catch (Throwable $e) { |
||
85 | Craft::error($e->getMessage(), __METHOD__); |
||
86 | } |
||
87 | } |
||
88 | $siteGroup = Craft::$app->getSites()->getGroupById($groupId); |
||
89 | if ($siteGroup !== null) { |
||
90 | $groupSiteIds = $siteGroup->getSiteIds(); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | if (empty($groupSiteIds)) { |
||
95 | $groupSiteIds = Craft::$app->getSites()->allSiteIds; |
||
96 | } |
||
97 | |||
98 | $lines = []; |
||
99 | // Sitemap index XML header and opening tag |
||
100 | $lines[] = '<?xml version="1.0" encoding="UTF-8"?>'; |
||
101 | $lines[] = '<?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>'; |
||
102 | // One sitemap entry for each element |
||
103 | $metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle( |
||
104 | $type, |
||
105 | $handle, |
||
106 | $siteId |
||
107 | ); |
||
108 | // If it doesn't exist, exit |
||
109 | if ($metaBundle === null) { |
||
110 | return null; |
||
111 | } |
||
112 | $multiSite = count($metaBundle->sourceAltSiteSettings) > 1; |
||
113 | $totalElements = null; |
||
114 | $urlsetLine = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'; |
||
115 | if ($metaBundle->metaSitemapVars->sitemapAssets) { |
||
116 | $urlsetLine .= ' xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"'; |
||
117 | $urlsetLine .= ' xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"'; |
||
118 | } |
||
119 | if ($multiSite) { |
||
120 | $urlsetLine .= ' xmlns:xhtml="http://www.w3.org/1999/xhtml"'; |
||
121 | } |
||
122 | if ((bool)$metaBundle->metaSitemapVars->newsSitemap) { |
||
123 | $urlsetLine .= ' xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"'; |
||
124 | } |
||
125 | $urlsetLine .= '>'; |
||
126 | $lines[] = $urlsetLine; |
||
127 | |||
128 | // Get all of the elements for this meta bundle type |
||
129 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType); |
||
130 | |||
131 | if ($seoElement !== null) { |
||
132 | // Ensure `null` so that the resulting element query is correct |
||
133 | if (empty($metaBundle->metaSitemapVars->sitemapLimit)) { |
||
134 | $metaBundle->metaSitemapVars->sitemapLimit = null; |
||
135 | } |
||
136 | |||
137 | $totalElements = $seoElement::sitemapElementsQuery($metaBundle)->count(); |
||
138 | if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) { |
||
|
|||
139 | $totalElements = $metaBundle->metaSitemapVars->sitemapLimit; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | // If no elements exist, just exit |
||
144 | if (!$totalElements) { |
||
145 | return null; |
||
146 | } |
||
147 | |||
148 | // Stash the sitemap attributes so they can be modified on a per-entry basis |
||
149 | $stashedSitemapAttrs = $metaBundle->metaSitemapVars->getAttributes(); |
||
150 | $stashedGlobalVarsAttrs = $metaBundle->metaGlobalVars->getAttributes(); |
||
151 | // Use craft\db\Paginator to paginate the results so we don't exceed any memory limits |
||
152 | // See batch() and each() discussion here: https://github.com/yiisoft/yii2/issues/8420 |
||
153 | // and here: https://github.com/craftcms/cms/issues/7338 |
||
154 | |||
155 | $elementQuery = $seoElement::sitemapElementsQuery($metaBundle); |
||
156 | $sitemapPageSize = $metaBundle->metaSitemapVars->sitemapPageSize; |
||
157 | $elementQuery->limit($metaBundle->metaSitemapVars->sitemapLimit ?? null); |
||
158 | |||
159 | // If this is not a paged sitemap, go through full results |
||
160 | if (empty($sitemapPageSize)) { |
||
161 | $pagedSitemap = false; |
||
162 | $paginator = new Paginator($elementQuery, [ |
||
163 | 'pageSize' => self::SITEMAP_QUERY_PAGE_SIZE, |
||
164 | ]); |
||
165 | $elements = $paginator->getPageResults(); |
||
166 | } else { |
||
167 | $sitemapPage = empty($page) ? 1 : $page; |
||
168 | $pagedSitemap = true; |
||
169 | $elementQuery->limit($sitemapPageSize); |
||
170 | $elementQuery->offset(($sitemapPage - 1) * $sitemapPageSize); |
||
171 | $elements = $elementQuery->all(); |
||
172 | $totalElements = $sitemapPageSize; |
||
173 | $paginator = new Paginator($elementQuery, [ |
||
174 | 'pageSize' => $sitemapPageSize, |
||
175 | ]); |
||
176 | } |
||
177 | |||
178 | $currentElement = 1; |
||
179 | |||
180 | do { |
||
181 | if (Craft::$app instanceof ConsoleApplication) { |
||
182 | if ($pagedSitemap) { |
||
183 | $message = sprintf('Query %d elements', $sitemapPageSize); |
||
184 | } else { |
||
185 | $message = sprintf('Query %d / %d - elements: %d', |
||
186 | $paginator->getCurrentPage(), |
||
187 | $paginator->getTotalPages(), |
||
188 | $paginator->getTotalResults()); |
||
189 | } |
||
190 | echo $message . PHP_EOL; |
||
191 | } |
||
192 | /** @var Element $element */ |
||
193 | foreach ($elements as $element) { |
||
194 | // Output some info if this is a console app |
||
195 | if (Craft::$app instanceof ConsoleApplication) { |
||
196 | echo "Processing element {$currentElement}/{$totalElements} - {$element->title}" . PHP_EOL; |
||
197 | } |
||
198 | |||
199 | $metaBundle->metaSitemapVars->setAttributes($stashedSitemapAttrs, false); |
||
200 | $metaBundle->metaGlobalVars->setAttributes($stashedGlobalVarsAttrs, false); |
||
201 | // Combine in any per-entry type settings |
||
202 | self::combineEntryTypeSettings($seoElement, $element, $metaBundle); |
||
203 | // Make sure this entry isn't disabled |
||
204 | self::combineFieldSettings($element, $metaBundle); |
||
205 | // Special case for the __home__ URI |
||
206 | $path = ($element->uri === '__home__') ? '' : $element->uri; |
||
207 | // Check to see if robots is `none` or `no index` |
||
208 | $robotsEnabled = true; |
||
209 | if (!empty($metaBundle->metaGlobalVars->robots)) { |
||
210 | $robotsEnabled = $metaBundle->metaGlobalVars->robots !== 'none' && |
||
211 | $metaBundle->metaGlobalVars->robots !== 'noindex'; |
||
212 | } |
||
213 | $enabled = $element->getEnabledForSite($metaBundle->sourceSiteId); |
||
214 | $enabled = $enabled && $path !== null && $metaBundle->metaSitemapVars->sitemapUrls && $robotsEnabled; |
||
215 | $event = new IncludeSitemapEntryEvent([ |
||
216 | 'include' => $enabled, |
||
217 | 'element' => $element, |
||
218 | 'metaBundle' => $metaBundle, |
||
219 | ]); |
||
220 | Event::trigger(self::class, self::EVENT_INCLUDE_SITEMAP_ENTRY, $event); |
||
221 | // Only add in a sitemap entry if it meets our criteria |
||
222 | if ($event->include) { |
||
223 | // Get the url and canonicalUrl |
||
224 | try { |
||
225 | $url = UrlHelper::siteUrl($path, null, null, $metaBundle->sourceSiteId); |
||
226 | } catch (Exception $e) { |
||
227 | $url = ''; |
||
228 | } |
||
229 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||
230 | if (Seomatic::$settings->excludeNonCanonicalUrls) { |
||
231 | Seomatic::$matchedElement = $element; |
||
232 | MetaValue::cache(); |
||
233 | $path = $metaBundle->metaGlobalVars->parsedValue('canonicalUrl'); |
||
234 | try { |
||
235 | $canonicalUrl = UrlHelper::siteUrl($path, null, null, $metaBundle->sourceSiteId); |
||
236 | } catch (Exception $e) { |
||
237 | $canonicalUrl = ''; |
||
238 | } |
||
239 | $canonicalUrl = UrlHelper::absoluteUrlWithProtocol($canonicalUrl); |
||
240 | if ($url !== $canonicalUrl) { |
||
241 | Craft::info("Excluding URL: {$url} from the sitemap because it does not match the Canonical URL: {$canonicalUrl} - " . $metaBundle->metaGlobalVars->canonicalUrl . " - " . $element->uri); |
||
242 | continue; |
||
243 | } |
||
244 | } |
||
245 | $dateUpdated = $element->dateUpdated ?? $element->dateCreated ?? new DateTime(); |
||
246 | $lines[] = '<url>'; |
||
247 | // Standard sitemap key/values |
||
248 | $lines[] = '<loc>'; |
||
249 | $lines[] = Html::encode($url); |
||
250 | $lines[] = '</loc>'; |
||
251 | $lines[] = '<lastmod>'; |
||
252 | $lines[] = $dateUpdated->format(DateTime::W3C); |
||
253 | $lines[] = '</lastmod>'; |
||
254 | $lines[] = '<changefreq>'; |
||
255 | $lines[] = $metaBundle->metaSitemapVars->sitemapChangeFreq; |
||
256 | $lines[] = '</changefreq>'; |
||
257 | $lines[] = '<priority>'; |
||
258 | $lines[] = $metaBundle->metaSitemapVars->sitemapPriority; |
||
259 | $lines[] = '</priority>'; |
||
260 | // Handle alternate URLs if this is multi-site |
||
261 | if ($multiSite && $metaBundle->metaSitemapVars->sitemapAltLinks) { |
||
262 | $primarySiteId = Craft::$app->getSites()->getPrimarySite()->id; |
||
263 | foreach ($metaBundle->sourceAltSiteSettings as $altSiteSettings) { |
||
264 | if (in_array($altSiteSettings['siteId'], $groupSiteIds, false) && SiteHelper::siteEnabledWithUrls($altSiteSettings['siteId'])) { |
||
265 | $altElement = null; |
||
266 | if ($seoElement !== null) { |
||
267 | /** @var Element $altElement */ |
||
268 | $altElement = $seoElement::sitemapAltElement( |
||
269 | $metaBundle, |
||
270 | $element->id, |
||
271 | $altSiteSettings['siteId'] |
||
272 | ); |
||
273 | } |
||
274 | // Make sure to only include the `hreflang` if the element exists, |
||
275 | // and sitemaps are on for it |
||
276 | if (Seomatic::$settings->addHrefLang && $altElement && $altElement->url) { |
||
277 | list($altSourceId, $altSourceBundleType, $altSourceHandle, $altSourceSiteId, $altTypeId) |
||
278 | = Seomatic::$plugin->metaBundles->getMetaSourceFromElement($altElement); |
||
279 | $altMetaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( |
||
280 | $altSourceBundleType, |
||
281 | $altSourceId, |
||
282 | $altSourceSiteId |
||
283 | ); |
||
284 | if ($altMetaBundle) { |
||
285 | $altEnabled = $altElement->getEnabledForSite($altMetaBundle->sourceSiteId); |
||
286 | // Make sure this entry isn't disabled |
||
287 | self::combineFieldSettings($altElement, $altMetaBundle); |
||
288 | if ($altEnabled && $altMetaBundle->metaSitemapVars->sitemapUrls) { |
||
289 | try { |
||
290 | $altUrl = UrlHelper::siteUrl($altElement->url, null, null, $altSourceId); |
||
291 | } catch (Exception $e) { |
||
292 | $altUrl = $altElement->url; |
||
293 | } |
||
294 | $altUrl = UrlHelper::absoluteUrlWithProtocol($altUrl); |
||
295 | // If this is the primary site, add it as x-default, too |
||
296 | if ($primarySiteId === $altSourceSiteId && Seomatic::$settings->addXDefaultHrefLang) { |
||
297 | $lines[] = '<xhtml:link rel="alternate"' |
||
298 | . ' hreflang="x-default"' |
||
299 | . ' href="' . Html::encode($altUrl) . '"' |
||
300 | . ' />'; |
||
301 | } |
||
302 | $lines[] = '<xhtml:link rel="alternate"' |
||
303 | . ' hreflang="' . $altSiteSettings['language'] . '"' |
||
304 | . ' href="' . Html::encode($altUrl) . '"' |
||
305 | . ' />'; |
||
306 | } |
||
307 | } |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | } |
||
312 | // Handle any Assets |
||
313 | if ($metaBundle->metaSitemapVars->sitemapAssets) { |
||
314 | // Regular Assets fields |
||
315 | $assetFields = FieldHelper::fieldsOfTypeFromElement( |
||
316 | $element, |
||
317 | FieldHelper::ASSET_FIELD_CLASS_KEY, |
||
318 | true |
||
319 | ); |
||
320 | foreach ($assetFields as $assetField) { |
||
321 | $assets = $element[$assetField]->all(); |
||
322 | /** @var Asset[] $assets */ |
||
323 | foreach ($assets as $asset) { |
||
324 | self::assetSitemapItem($asset, $metaBundle, $lines); |
||
325 | } |
||
326 | } |
||
327 | // Assets embeded in Block fields |
||
328 | $blockFields = FieldHelper::fieldsOfTypeFromElement( |
||
329 | $element, |
||
330 | FieldHelper::BLOCK_FIELD_CLASS_KEY, |
||
331 | true |
||
332 | ); |
||
333 | foreach ($blockFields as $blockField) { |
||
334 | $blocks = $element[$blockField]->all(); |
||
335 | /** @var Entry[]|NeoBlock[]|object[] $blocks */ |
||
336 | foreach ($blocks as $block) { |
||
337 | $assetFields = []; |
||
338 | if ($block instanceof Entry) { |
||
339 | $assetFields = FieldHelper::matrixFieldsOfType($block, AssetsField::class); |
||
340 | } |
||
341 | if ($block instanceof NeoBlock) { |
||
342 | $assetFields = FieldHelper::neoFieldsOfType($block, AssetsField::class); |
||
343 | } |
||
344 | foreach ($assetFields as $assetField) { |
||
345 | foreach ($block[$assetField]->all() as $asset) { |
||
346 | self::assetSitemapItem($asset, $metaBundle, $lines); |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | } |
||
352 | $lines[] = '</url>'; |
||
353 | } |
||
354 | // Include links to any known file types in the assets fields |
||
355 | if ($metaBundle->metaSitemapVars->sitemapFiles) { |
||
356 | // Regular Assets fields |
||
357 | $assetFields = FieldHelper::fieldsOfTypeFromElement( |
||
358 | $element, |
||
359 | FieldHelper::ASSET_FIELD_CLASS_KEY, |
||
360 | true |
||
361 | ); |
||
362 | foreach ($assetFields as $assetField) { |
||
363 | $assets = $element[$assetField]->all(); |
||
364 | foreach ($assets as $asset) { |
||
365 | self::assetFilesSitemapLink($asset, $metaBundle, $lines); |
||
366 | } |
||
367 | } |
||
368 | // Assets embeded in Block fields |
||
369 | $blockFields = FieldHelper::fieldsOfTypeFromElement( |
||
370 | $element, |
||
371 | FieldHelper::BLOCK_FIELD_CLASS_KEY, |
||
372 | true |
||
373 | ); |
||
374 | foreach ($blockFields as $blockField) { |
||
375 | $blocks = $element[$blockField]->all(); |
||
376 | /** @var Entry[]|NeoBlock[]|object[] $blocks */ |
||
377 | foreach ($blocks as $block) { |
||
378 | $assetFields = []; |
||
379 | if ($block instanceof Entry) { |
||
380 | $assetFields = FieldHelper::matrixFieldsOfType($block, AssetsField::class); |
||
381 | } |
||
382 | if ($block instanceof NeoBlock) { |
||
383 | $assetFields = FieldHelper::neoFieldsOfType($block, AssetsField::class); |
||
384 | } |
||
385 | foreach ($assetFields as $assetField) { |
||
386 | foreach ($block[$assetField]->all() as $asset) { |
||
387 | self::assetFilesSitemapLink($asset, $metaBundle, $lines); |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | } |
||
392 | } |
||
393 | $currentElement++; |
||
394 | } |
||
395 | |||
396 | if ($pagedSitemap) { |
||
397 | break; |
||
398 | } |
||
399 | |||
400 | if ($paginator->getCurrentPage() == $paginator->getTotalPages()) { |
||
401 | break; |
||
402 | } |
||
403 | |||
404 | $paginator->currentPage++; |
||
405 | $elements = $paginator->getPageResults(); |
||
406 | } while (!empty($elements)); |
||
407 | |||
408 | // Sitemap closing tag |
||
409 | $lines[] = '</urlset>'; |
||
410 | |||
411 | return implode('', $lines); |
||
412 | } |
||
605 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: