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