|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Prokl\BitrixOrdinaryToolsBundle\Services\Seo; |
| 4 | + |
| 5 | +use CBitrixComponent; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class Canonical |
| 9 | + * Вывод канонических ссылок компонента из свойства. |
| 10 | + * @package Prokl\BitrixOrdinaryToolsBundle\Services\Seo |
| 11 | + */ |
| 12 | +class Canonical |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @const string TARGET_ID ID отложенной функции |
| 16 | + */ |
| 17 | + private const TARGET_ID = 'canonicalLink'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @const string PAGE_PROPERTY_CANONICAL_NAME Название свойства папки для канонических страниц. |
| 21 | + */ |
| 22 | + private const PAGE_PROPERTY_CANONICAL_NAME = 'canonical-link'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param CBitrixComponent $bitrixComponent Объект Битрикс-компонента. |
| 26 | + * @param string $canonicalLink Каноническая ссылка. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public static function passLink(CBitrixComponent $bitrixComponent, string $canonicalLink = ''): void |
| 31 | + { |
| 32 | + if ($canonicalLink) { |
| 33 | + // Канононические URL из свойства CANONICAL_URL |
| 34 | + $bitrixComponent->__parent->__template->SetViewTarget(self::TARGET_ID); ?> |
| 35 | + <link rel="canonical" href="<?php echo self::getFullUrl($canonicalLink); ?>"/> |
| 36 | + <?php $bitrixComponent->__parent->__template->EndViewTarget(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Канонические ссылки для статических страниц. Берутся из |
| 42 | + * свойства папки canonical. |
| 43 | + * |
| 44 | + * @param string $canonicalUrl URL (для списковых страниц). |
| 45 | + * |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public static function staticPage(string $canonicalUrl = ''): void |
| 49 | + { |
| 50 | + if (!$canonicalUrl) { |
| 51 | + $canonicalUrl = $GLOBALS['APPLICATION']->GetDirProperty(self::PAGE_PROPERTY_CANONICAL_NAME); |
| 52 | + } |
| 53 | + |
| 54 | + if (!$canonicalUrl) { |
| 55 | + $canonicalUrl = $GLOBALS['APPLICATION']->GetPageProperty(self::PAGE_PROPERTY_CANONICAL_NAME); |
| 56 | + } |
| 57 | + |
| 58 | + if ($canonicalUrl) { |
| 59 | + $sResultLine = sprintf('<link rel="canonical" href="%s" />', self::getFullUrl($canonicalUrl)); |
| 60 | + $GLOBALS['APPLICATION']->AddViewContent(self::TARGET_ID, $sResultLine); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Насильно выставить каноническую ссылку. |
| 66 | + * |
| 67 | + * @param string $canonicalUrl Каноническая ссылка. |
| 68 | + * |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public static function forceCanonical(string $canonicalUrl = '') : void |
| 72 | + { |
| 73 | + if (!$canonicalUrl) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $sResultLine = sprintf('<link rel="canonical" href="%s" />', $canonicalUrl); |
| 78 | + $GLOBALS['APPLICATION']->AddViewContent(self::TARGET_ID, $sResultLine); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Вывод канонической ссылки в header. |
| 83 | + * |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public static function show(): void |
| 87 | + { |
| 88 | + $GLOBALS['APPLICATION']->ShowViewContent(self::TARGET_ID); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Проверка - HTTP или HTTPS. |
| 93 | + * |
| 94 | + * @return boolean |
| 95 | + */ |
| 96 | + private static function isSecureConnection(): bool |
| 97 | + { |
| 98 | + return |
| 99 | + (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') |
| 100 | + || $_SERVER['SERVER_PORT'] == 443; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Получить полный (включая https, домен) путь к канонической странице. |
| 105 | + * |
| 106 | + * @param string $url Укороченный URL (без домена). |
| 107 | + * |
| 108 | + * @return string |
| 109 | + */ |
| 110 | + private static function getFullUrl(string $url = ''): string |
| 111 | + { |
| 112 | + $canonicalLink = $url ?: $_SERVER['REQUEST_URI']; |
| 113 | + $schema = self::isSecureConnection() ? 'https://' : 'http://'; |
| 114 | + |
| 115 | + return $schema . $_SERVER['HTTP_HOST'] . $canonicalLink; |
| 116 | + } |
| 117 | +} |
0 commit comments