diff --git a/composer.json b/composer.json index 2da6e2c8..e5db6ded 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "craftcms/postmark": "3.0.0", "hybridinteractive/craft-position-fieldtype": "4.0.0", "hybridinteractive/craft-width-fieldtype": "^4.0", + "jaybizzle/crawler-detect": "^1.2", "mikehaertl/php-shellcommand": "^1.6", "mmikkel/cp-field-inspect": "1.4.4", "nystudio107/craft-imageoptimize": "4.0.5", diff --git a/composer.lock b/composer.lock index bcaadb02..c72fe833 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d72ea5b9e7acc6bb2f16609526386ac4", + "content-hash": "d94306aa7c33cdcaa77bf8d3c4b71c1c", "packages": [ { "name": "cebe/markdown", @@ -2926,6 +2926,58 @@ }, "time": "2021-03-25T15:10:34+00:00" }, + { + "name": "jaybizzle/crawler-detect", + "version": "v1.2.116", + "source": { + "type": "git", + "url": "https://github.com/JayBizzle/Crawler-Detect.git", + "reference": "97e9fe30219e60092e107651abb379a38b342921" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/JayBizzle/Crawler-Detect/zipball/97e9fe30219e60092e107651abb379a38b342921", + "reference": "97e9fe30219e60092e107651abb379a38b342921", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8|^5.5|^6.5|^9.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "Jaybizzle\\CrawlerDetect\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mark Beech", + "email": "m@rkbee.ch", + "role": "Developer" + } + ], + "description": "CrawlerDetect is a PHP class for detecting bots/crawlers/spiders via the user agent", + "homepage": "https://github.com/JayBizzle/Crawler-Detect/", + "keywords": [ + "crawler", + "crawler detect", + "crawler detector", + "crawlerdetect", + "php crawler detect" + ], + "support": { + "issues": "https://github.com/JayBizzle/Crawler-Detect/issues", + "source": "https://github.com/JayBizzle/Crawler-Detect/tree/v1.2.116" + }, + "time": "2023-07-21T15:49:49+00:00" + }, { "name": "justinrainbow/json-schema", "version": "v5.2.13", diff --git a/modules/statik/src/web/twig/StatikExtension.php b/modules/statik/src/web/twig/StatikExtension.php index 10f71579..a0f749c3 100644 --- a/modules/statik/src/web/twig/StatikExtension.php +++ b/modules/statik/src/web/twig/StatikExtension.php @@ -2,14 +2,15 @@ namespace modules\statik\web\twig; +use Jaybizzle\CrawlerDetect\CrawlerDetect; use Twig\Extension\AbstractExtension; +use Twig\Extension\GlobalsInterface; use Twig\TwigFilter; use Twig\TwigFunction; -use Twig\TwigTest; use craft\elements\Entry; use craft\helpers\ElementHelper; -class StatikExtension extends AbstractExtension +class StatikExtension extends AbstractExtension implements GlobalsInterface { private const SECTIONS_NO_INDEX_NO_FOLLOW = [ 'searchResults', @@ -25,18 +26,17 @@ class StatikExtension extends AbstractExtension 'setPasswordConfirmation', ]; + private CrawlerDetect $crawlerDetect; - public function getFilters(): array + public function __construct() { - return [ - new TwigFilter('slugify', [$this, 'slugify']), - ]; + $this->crawlerDetect = new CrawlerDetect(); } - public function getTests(): array + public function getFilters(): array { return [ - new TwigTest('isBot', [$this, 'isBot']), + new TwigFilter('slugify', [$this, 'slugify']), ]; } @@ -47,19 +47,15 @@ public function getFunctions(): array ]; } - - public function isBot(string $userAgent = '/bot|crawl|facebook|google|slurp|spider|mediapartners/i'): bool + public function getGlobals(): array { - if (isset($_SERVER['HTTP_USER_AGENT'])) { - return $_SERVER['HTTP_USER_AGENT'] && preg_match($userAgent, $_SERVER['HTTP_USER_AGENT']); - } - return false; + return [ + 'isBot' => $this->crawlerDetect->isCrawler(), + ]; } /** - * Create slugs from titles in contentbuilder for the anchor link - * @param $string - * @return string + * remove all non-alphanumeric characters and replace spaces with dashes */ public function slugify(string $string): string { diff --git a/templates/_site/_layout.twig b/templates/_site/_layout.twig index fc6a4685..d55b67ce 100644 --- a/templates/_site/_layout.twig +++ b/templates/_site/_layout.twig @@ -44,11 +44,12 @@ {% endif %} {# Cookiebanner #} - {{ craft.cookieBanner.render({ - supportIE: true, - showCookieBanner: (entry.type is defined and entry.type.handle != 'cookiePolicy') - }) }} - + {% if not isBot %} + {{ craft.cookieBanner.render({ + supportIE: true, + showCookieBanner: (entry.type is defined and entry.type.handle != 'cookiePolicy'), + }) }} + {% endif %}