diff --git a/CHANGELOG.md b/CHANGELOG.md index 65c80d1..fc0444d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Release Notes for Cookie Consent +## 5.0.0 - 2024-04-19 + +### Deprecated +- before-body-end hook deprecated. Use after-body-start. ## 4.0.4 - 2022-09-30 @@ -206,4 +210,4 @@ ## 1.0.0 - 2019-05-14 ### Added -- Initial Plugin Release \ No newline at end of file +- Initial Plugin Release diff --git a/README.md b/README.md index 193f89b..328086f 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,6 @@ You need to add this line right after your start-body-tag in the relevant layout `{% hook 'after-body-start' %}` -~~To give you full control over where the plugin is rendering the consent template, you need to add this line before your end-body-tag in the relevant layout file(s):~~ -`{% hook 'before-body-end' %}` - -**Note:** *Both hooks will work as of v1.5.0, but please do not use both. Before-body-end will likely be deprecated in a future update.* - ## Activate the plugin for your site Navigate to the plugin inside your Control Panel. If you have the correct permissions, it should be visible in the menu. @@ -116,4 +111,4 @@ This object contains key-value pairs of consents and their status. The object will be created if the default javascript file is loaded, and a form with the ID #elc-cookie-consent-form exists and has the `data-show`-attribute has the url to the consent show endpoint(/cookie-consent/show) ## Acknowledgements -Plugin Icon designed by Trinh Ho from Flaticon \ No newline at end of file +Plugin Icon designed by Trinh Ho from Flaticon diff --git a/composer.json b/composer.json index bca92af..652b412 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "elleracompany/craft-cookie-consent", - "description": "GDPR Compliant Cookie Consent Plugin", - "version": "4.0.4", + "description": "Craft Cookie Consent Plugin", + "version": "5.0.0", "type": "craft-plugin", "keywords": ["cookies", "consent", "gdpr"], "license": "proprietary", @@ -16,7 +16,7 @@ }, "require": { "ext-json": "*", - "craftcms/cms": "^4.0" + "craftcms/cms": "^5.0" }, "autoload": { "psr-4": { @@ -24,12 +24,21 @@ } }, "extra": { - "name": "GDPR Cookie Consent", + "name": "Cookie Consent", "handle": "cookie-consent", "documentationUrl": "https://github.com/elleracompany/craft-cookie-consent/blob/master/README.md", "changelogUrl": "https://github.com/elleracompany/craft-cookie-consent/blob/master/CHANGELOG.md", "class": "elleracompany\\cookieconsent\\CookieConsent" }, "prefer-stable": true, - "minimum-stability": "dev" + "minimum-stability": "dev", + "require-dev": { + "craftcms/phpstan": "dev-main" + }, + "config": { + "allow-plugins": { + "yiisoft/yii2-composer": true, + "craftcms/plugin-installer": true + } + } } diff --git a/composer.lock b/composer.lock index 6173b1a..bdfcd82 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": "b4e83ef649ca7b0090b96af80ce46f98", + "content-hash": "b4b4a5e70028c0b879551328b120ba4e", "packages": [ { "name": "cebe/markdown", @@ -6591,15 +6591,103 @@ "time": "2022-02-10T13:42:46+00:00" } ], - "packages-dev": [], + "packages-dev": [ + { + "name": "craftcms/phpstan", + "version": "dev-main", + "source": { + "type": "git", + "url": "https://github.com/craftcms/phpstan.git", + "reference": "b61bba102b5ec8599406e6e29a28a20c915a6abc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/craftcms/phpstan/zipball/b61bba102b5ec8599406e6e29a28a20c915a6abc", + "reference": "b61bba102b5ec8599406e6e29a28a20c915a6abc", + "shasum": "" + }, + "require": { + "phpstan/phpstan": "^1.4.6" + }, + "default-branch": true, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "description": "PHPStan configuration for Craft CMS projects", + "support": { + "issues": "https://github.com/craftcms/phpstan/issues", + "source": "https://github.com/craftcms/phpstan/tree/main" + }, + "time": "2022-04-12T20:50:18+00:00" + }, + { + "name": "phpstan/phpstan", + "version": "1.10.67", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "16ddbe776f10da6a95ebd25de7c1dbed397dc493" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/16ddbe776f10da6a95ebd25de7c1dbed397dc493", + "reference": "16ddbe776f10da6a95ebd25de7c1dbed397dc493", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2024-04-16T07:22:02+00:00" + } + ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "craftcms/phpstan": 20 + }, "prefer-stable": true, "prefer-lowest": false, "platform": { "ext-json": "*" }, "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.6.0" } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..f9f81af --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +includes: + - vendor/craftcms/phpstan/phpstan.neon + +parameters: + level: 0 + paths: + - src diff --git a/src/CookieConsent.php b/src/CookieConsent.php index ba308bc..ac1dba7 100644 --- a/src/CookieConsent.php +++ b/src/CookieConsent.php @@ -2,8 +2,12 @@ namespace elleracompany\cookieconsent; use Craft; +use craft\web\Response; use craft\web\View; use elleracompany\cookieconsent\services\Variables; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; use yii\base\Event; use craft\web\UrlManager; use craft\events\RegisterUrlRulesEvent; @@ -12,6 +16,8 @@ use craft\services\UserPermissions; use craft\events\RegisterUserPermissionsEvent; use craft\console\Application as ConsoleApplication; +use yii\base\Exception; +use yii\base\InvalidRouteException; /** * Class Plugin @@ -25,39 +31,12 @@ class CookieConsent extends \craft\base\Plugin // Constants // ========================================================================= - /** - * Database Table name for SiteSettings records - */ const SITE_SETTINGS_TABLE = '{{%cookie_consent_site_settings}}'; - - /** - * Database Table name for Cookie Group records - */ const COOKIE_GROUP_TABLE = '{{%cookie_consent_group}}'; - - /** - * Database Table name for Cookie Consent records - */ const CONSENT_TABLE = '{{%cookie_consent_consent}}'; - - /** - * Default banner template location - */ const DEFAULT_TEMPLATE = 'cookie-consent/banner'; - - /** - * Default banner headline - */ const DEFAULT_HEADLINE = 'This website uses cookies'; - - /** - * Default banner description - */ const DEFAULT_DESCRIPTION = 'We use cookies to personalize content and ads, and to analyze our traffic and improve our service.'; - - /** - * Default cookie groups - */ const DEFAULT_GROUPS = [ [ 'required' => true, @@ -109,37 +88,16 @@ class CookieConsent extends \craft\base\Plugin 'cookies' => [] ] ]; - - /** - * Plugin name - */ const PLUGIN_NAME = 'Cookie Banner'; // Properties // ========================================================================= - - /** - * Enable CpNav - * - * @var bool - */ public bool $hasCpSection = true; - - /** - * Schema version - * For applying migrations etc. - * - * @var string - */ public string $schemaVersion = '1.5.0'; // Public Methods // ========================================================================= - - /** - * Plugin Initiator - */ - public function init() + public function init(): void { parent::init(); Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $e) { @@ -172,22 +130,16 @@ public function init() Craft::$app->view->hook('after-body-start', function (array &$context) { return $this->renderPluginTemplate($this->cookieConsent->getTemplate()); }); - Craft::$app->view->hook('before-body-end', function (array &$context) { - return $this->renderPluginTemplate($this->cookieConsent->getTemplate()); - }); } } } else $this->installCpEventListeners(); } - /** - * @inheritdoc - */ - public function getSettingsResponse(): mixed - { + public function getSettingsResponse(): Response + { // Just redirect to the plugin settings page - Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('cookie-consent')); + return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('cookie-consent')); } /** @@ -195,17 +147,16 @@ public function getSettingsResponse(): mixed * and JS/CSS files. * * https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-templates - * * @param $path * @param array $params * @return string - * @throws \Twig\Error\LoaderError - * @throws \Twig\Error\RuntimeError - * @throws \Twig\Error\SyntaxError - * @throws \yii\base\Exception + * @throws Exception + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ - public function renderPluginTemplate($path, $params = []) - { + public function renderPluginTemplate($path, array $params = []): string + { if($path == 'cookie-consent/banner') { $oldMode = Craft::$app->view->getTemplateMode(); @@ -221,11 +172,7 @@ public function renderPluginTemplate($path, $params = []) // Private Methods // ========================================================================= - - /** - * Install CP Event Listeners - */ - protected function installCpEventListeners() + protected function installCpEventListeners(): void { Event::on( UrlManager::class, @@ -250,10 +197,7 @@ function (RegisterUserPermissionsEvent $event) { ); } - /** - * Install Site Event Listeners - */ - protected function installSiteEventListeners() + protected function installSiteEventListeners(): void { Event::on( UrlManager::class, @@ -268,11 +212,6 @@ function (RegisterUrlRulesEvent $event) { ); } - /** - * Return the custom Control Panel routes - * - * @return array - */ protected function customAdminCpRoutes(): array { return [ @@ -286,23 +225,12 @@ protected function customAdminCpRoutes(): array ]; } - /** - * Return the custom Control Panel routes - * - * @return array - */ protected function customSiteRoutes(): array { return [ 'cookie-consent/show' => 'cookie-consent/consent/show', ]; } - - /** - * Returns the custom Control Panel user permissions. - * - * @return array - */ protected function customAdminCpPermissions(): array { return [ @@ -339,4 +267,4 @@ protected function customAdminCpPermissions(): array ], ]; } -} \ No newline at end of file +} diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 0fce766..8d1a212 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -4,6 +4,7 @@ use Craft; use craft\helpers\Db; +use craft\helpers\UrlHelper; use craft\models\Site; use craft\web\Controller; use elleracompany\cookieconsent\CookieConsent; @@ -11,7 +12,6 @@ use elleracompany\cookieconsent\records\CookieGroup; use elleracompany\cookieconsent\records\SiteSettings; use yii\web\NotFoundHttpException; -use craft\helpers\UrlHelper; class SettingsController extends Controller { @@ -47,8 +47,8 @@ public function actionEditSiteSettings(SiteSettings $model = null) $this->_prepVariables($variables); $variables['currentPage'] = 'site'; $variables['title'] = Craft::t('cookie-consent', 'Site Settings'); - $variables['invalidate_link'] = "/" . Craft::$app->config->general->cpTrigger . "/cookie-consent/site/invalidate?site=".$variables['currentSiteHandle']; - $this->_checkSiteEditPermission(Craft::$app->getSites()->currentSite->id); + $variables['invalidate_link'] = "/" . Craft::$app->config->general->cpTrigger . "/cookie-consent/site/invalidate?site=".$variables['selectedSite']->handle; + $this->_checkSiteEditPermission($variables['selectedSite']->id); $this->_prepSiteSettingsPermissionVariables($variables); return $this->renderTemplate('cookie-consent/settings/site', $variables); @@ -64,7 +64,7 @@ public function actionEditSiteSettings(SiteSettings $model = null) */ public function actionConsent($page = null) { - $pageSize = 20; + $pageSize = 10; if($page == null) $page = 1; $this->requirePermission('cookie-consent:site-settings:view-consents'); @@ -74,8 +74,8 @@ public function actionConsent($page = null) ]; $this->_prepVariables($variables); $variables['currentPage'] = 'consent'; - $variables['consents'] = Consent::find()->where(['site_id' => $variables['currentSiteId']])->orderBy('dateUpdated DESC')->limit($pageSize)->offset(($page-1)*$pageSize)->all(); - $total = Consent::find()->where(['site_id' => $variables['currentSiteId']])->count(); + $variables['consents'] = Consent::find()->where(['site_id' => $variables['selectedSite']->id])->orderBy('dateUpdated DESC')->limit($pageSize)->offset(($page-1)*$pageSize)->all(); + $total = Consent::find()->where(['site_id' => $variables['selectedSite']->id])->count(); $count = count($variables['consents']); $cpTrigger = Craft::$app->config->general->cpTrigger; @@ -190,8 +190,8 @@ public function actionEditCookieGroup(string $groupId = null, CookieGroup $group ]; $this->_prepVariables($variables); $this->_prepEditGroupVariables($variables); - if($variables['currentSiteId'] !== $variables['group']->site_id) return $this->redirect(UrlHelper::cpUrl('cookie-consent?site=' . $variables['currentSiteHandle'])); - $this->_checkSiteEditPermission(Craft::$app->getSites()->currentSite->id); + if($variables['selectedSite']->id !== $variables['group']->site_id) return $this->redirect(UrlHelper::cpUrl('cookie-consent?site=' . $variables['selectedSite']->handle)); + $this->_checkSiteEditPermission($variables['selectedSite']->id); $this->_prepGroupPermissionVariables($variables); return $this->renderTemplate('cookie-consent/settings/group', $variables); @@ -298,20 +298,47 @@ protected function insertDefaultRecord(SiteSettings &$record) private function _prepVariables(array &$variables) { $siteHandle = Craft::$app->request->get('site'); + if($siteHandle) $variables['selectedSite'] = Craft::$app->getSites()->getSiteByHandle($siteHandle); + else $variables['selectedSite'] = Craft::$app->getSites()->primarySite; + $variables['fullPageForm'] = true; + + if (empty($variables['model'])) { + $variables['model'] = SiteSettings::findOne($variables['selectedSite']->id); + if (!$variables['model']) { + $variables['model'] = new SiteSettings(); + $variables['model']->site_id = $variables['selectedSite']->id; + $this->insertDefaultRecord($variables['model']); + } + } + + // Old + /* + $siteHandle = Craft::$app->request->get('site'); /* @var $site Site */ + /* if($siteHandle) $site = Craft::$app->getSites()->getSiteByHandle($siteHandle); - else $site =Craft::$app->getSites()->primarySite; + else $site = Craft::$app->getSites()->primarySite; - $variables['site'] = $site; - $variables['currentSiteId'] = $variables['site']->id; - $variables['currentSiteHandle'] = $variables['site']->handle; + $variables['selectedSite'] = $site; $variables['cpTrigger'] = Craft::$app->config->general->cpTrigger; + if (Craft::$app->getIsMultiSite()) { + $sites = Craft::$app->getSites(); + $variables['sitesMenuLabel'] = $site->name; + $variables['showSiteMenu'] = true; + foreach ($sites->getEditableSiteIds() as $editableSiteId) { + $variables['enabledSiteIds'][] = $editableSiteId; + $variables['siteIds'][] = $editableSiteId; + } + } else { + $variables['sitesMenuLabel'] = 'Cookie Consent'; + } + if (empty($variables['model'])) { - $variables['model'] = SiteSettings::findOne($variables['currentSiteId']); + $variables['model'] = SiteSettings::findOne($variables['selectedSite']->id); if (!$variables['model']) { $variables['model'] = new SiteSettings(); - $variables['model']->site_id = $variables['currentSiteId']; + $variables['model']->site_id = $variables['selectedSite']->id; $this->insertDefaultRecord($variables['model']); } } @@ -323,10 +350,11 @@ private function _prepVariables(array &$variables) 'url' => UrlHelper::cpUrl('cookie-consent'), ], [ - 'label' => $variables['site']->name, - 'url' => UrlHelper::cpUrl('cookie-consent/site/'.$variables['site']->handle), + 'label' => $variables['selectedSite']->name, + 'url' => UrlHelper::cpUrl('cookie-consent/site/'.$variables['selectedSite']->handle), ] ]; + */ } /** @@ -348,25 +376,15 @@ private function _prepEditGroupVariables(array &$variables) } } else { $variables['group'] = new CookieGroup(); - $variables['group']->site_id = $variables['currentSiteId']; + $variables['group']->site_id = $variables['selectedSite']->id; } } $variables['group']->unstringifyCookies(); - $variables['model'] = SiteSettings::findOne($variables['currentSiteId']); + $variables['model'] = SiteSettings::findOne($variables['selectedSite']->id); $variables['currentPage'] = 'group'; $variables['title'] = $variables['group']->isNewRecord ? Craft::t('cookie-consent', 'New cookie Group') : $variables['group']->name; $variables['fullPageForm'] = true; - $variables['crumbs'] = [ - [ - 'label' => CookieConsent::PLUGIN_NAME, - 'url' => UrlHelper::cpUrl('cookie-consent'), - ], - [ - 'label' => $variables['site']->name, - 'url' => UrlHelper::cpUrl('cookie-consent/site/'.$variables['currentSiteHandle']), - ] - ]; } /** @@ -404,4 +422,4 @@ private function _prepSiteSettingsPermissionVariables(array &$variables) $variables['canCreate'] = Craft::$app->user->checkPermission('cookie-consent:cookie-groups:create-new'); $variables['canGroups'] = Craft::$app->user->checkPermission('cookie-consent:cookie-groups'); } -} \ No newline at end of file +} diff --git a/src/icon-mask.svg b/src/icon-mask.svg index bcf5507..656cfb4 100755 --- a/src/icon-mask.svg +++ b/src/icon-mask.svg @@ -1,6 +1,6 @@ - @@ -25,4 +25,4 @@ - \ No newline at end of file + diff --git a/src/templates/banner.twig b/src/templates/banner.twig index 68df7c4..ccab439 100644 --- a/src/templates/banner.twig +++ b/src/templates/banner.twig @@ -1,5 +1,5 @@