Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBeckers committed Feb 26, 2020
1 parent 709e5a6 commit 03322c3
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 129 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Craft Cookie boss Changelog
# Release Notes for Craft Cookie boss

All notable changes to this project will be documented in this file.

Expand Down Expand Up @@ -54,4 +54,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## 1.1.3 - 2019-11-20
### Fixed
- Fixed deprecated warning 'craft.config.setting'
- Fixed deprecated warning 'craft.config.setting'

## 1.2.0 - 2020-02-26
### Added
- Added support for consent invalidation.
- Dutch translations

### Edit
- Refactoring
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Allow your visitors to set their cookie preference which you can use to enable f
Create your own cookie groups, make them required and/or set their default state.
These cookie groups can contain cookies with information which you can display to describe the purpose of each cookie.


![Create own cookie groups](resources/img/consent-groups.png)
![Add cookies to cookie groups](resources/img/cookie-descriptions.png)

Expand Down Expand Up @@ -110,6 +111,22 @@ Get all consents of the current visitor. Get default values if noting found.

---

### Get visitor consent date
`craft.cookieBoss.getConsentGiveAt(format)` is used to get the unix time or formated date of the visitor's consents.

| Attribute | Type | Required | Description |
|:----------|:-----|:---------|:------------|
|format|string|false|Format the timestamp|

#### Examples
Get the current visitor time of consent.
``craft.cookieBoss.getConsentGiveAt()``

Get the current visitor time of consent in given format.
```craft.cookieBoss.getConsentGiveAt("d-m-Y")```

---

### Get if visitor gave consent for cookie group
`craft.cookieBoss.isConsentWith(handle)` is used to get the consent of the current visitor by handle.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dutchheight/craft-cookie-boss",
"description": "Allow your visitors to set their cookie preference.",
"type": "craft-plugin",
"version": "1.1.3",
"version": "1.2.0",
"keywords": [
"craft",
"cms",
Expand Down
10 changes: 4 additions & 6 deletions src/CookieBoss.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@

namespace dutchheight\cookieboss;

use yii\base\Event;

use Craft;
use craft\base\Plugin;
use craft\helpers\UrlHelper;
use craft\web\UrlManager;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\web\twig\variables\CraftVariable;

use craft\web\UrlManager;
use dutchheight\cookieboss\models\Settings;
use dutchheight\cookieboss\services\ConsentGroupService;
use dutchheight\cookieboss\services\CookieDescriptionService;
use dutchheight\cookieboss\variables\CookieBossVariable;
use dutchheight\cookieboss\models\Settings;
use yii\base\Event;

/**
* Craft plugins are very much like little applications in and of themselves. We’ve made
Expand Down
47 changes: 29 additions & 18 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace dutchheight\cookieboss\controllers;

use dutchheight\cookieboss\CookieBoss;

use Craft;
use craft\web\Controller;
use craft\helpers\UrlHelper;

use craft\elements\Entry;
use yii\web\Response;
use craft\helpers\UrlHelper;
use craft\web\Controller;
use dutchheight\cookieboss\CookieBoss;
use yii\web\NotFoundHttpException;
use yii\web\Response;


/**
Expand Down Expand Up @@ -82,9 +82,16 @@ public function actionSaveGeneral()
throw new NotFoundHttpException(Craft::t('app', 'Plugin not found.'));
}

$forceReconsent = (bool)Craft::$app->getRequest()->getBodyParam('reset-consent');
return $this->saveSettings($forceReconsent);
}

private function saveSettings($forceReconsent = false) {
$plugin = Craft::$app->getPlugins()->getPlugin('cookie-boss');
$cookies = Craft::$app->getRequest()->getRequiredBodyParam('cookies');
$cookies = empty($cookies) ? [] : $cookies;
$update = CookieBoss::getInstance()->cookieDescriptions->updateAll($cookies);
$update = CookieBoss::getInstance()->cookieDescriptions->updateAll($cookies);

if (is_array($update)) {
$this->error($plugin);
return null;
Expand All @@ -98,18 +105,22 @@ public function actionSaveGeneral()
return null;
}

$settings['enabled'] = (Craft::$app->getRequest()->getRequiredBodyParam('enabled') == '1');
$settings['presentGroups'] = (Craft::$app->getRequest()->getRequiredBodyParam('presentGroups') == '1');
$settings['forceAccept'] = (Craft::$app->getRequest()->getRequiredBodyParam('forceAccept') == '1');

$settings['cookieTime'] = Craft::$app->getRequest()->getRequiredBodyParam('cookieTime') * 86400;
$settings['title'] = Craft::$app->getRequest()->getRequiredBodyParam('title');
$settings['message'] = Craft::$app->getRequest()->getRequiredBodyParam('message');
$settings['messageSettings'] = Craft::$app->getRequest()->getRequiredBodyParam('messageSettings');
$settings['acceptButtonText'] = Craft::$app->getRequest()->getRequiredBodyParam('acceptButtonText');
$settings['settingsButtonText'] = Craft::$app->getRequest()->getRequiredBodyParam('settingsButtonText');
$settings['cookiesPageId'] = Craft::$app->getRequest()->getRequiredBodyParam('contactPage');
$settings['acceptAfterSeconds'] = Craft::$app->getRequest()->getRequiredBodyParam('acceptAfterSeconds');
$settings['enabled'] = (Craft::$app->getRequest()->getRequiredBodyParam('enabled') == '1');
$settings['presentGroups'] = (Craft::$app->getRequest()->getRequiredBodyParam('presentGroups') == '1');
$settings['forceAccept'] = (Craft::$app->getRequest()->getRequiredBodyParam('forceAccept') == '1');

$settings['cookieTime'] = Craft::$app->getRequest()->getRequiredBodyParam('cookieTime') * 86400;
$settings['title'] = Craft::$app->getRequest()->getRequiredBodyParam('title');
$settings['message'] = Craft::$app->getRequest()->getRequiredBodyParam('message');
$settings['messageSettings'] = Craft::$app->getRequest()->getRequiredBodyParam('messageSettings');
$settings['acceptButtonText'] = Craft::$app->getRequest()->getRequiredBodyParam('acceptButtonText');
$settings['settingsButtonText'] = Craft::$app->getRequest()->getRequiredBodyParam('settingsButtonText');
$settings['cookiesPageId'] = Craft::$app->getRequest()->getRequiredBodyParam('contactPage');
$settings['acceptAfterSeconds'] = Craft::$app->getRequest()->getRequiredBodyParam('acceptAfterSeconds');

if ($forceReconsent) {
$settings['lastSettingsUpdate'] = time();
}

$success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings);
if (!$success) {
Expand Down
7 changes: 7 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ class Settings extends Model
*/
public $acceptAfterSeconds = 0;

/**
* Last cookie settings update (Unix timestamp)
*
* @var int
*/
public $lastSettingsUpdate = null;

// Public Methods
// =========================================================================

Expand Down
39 changes: 35 additions & 4 deletions src/services/ConsentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Craft;
use craft\base\Component;
use yii\web\Cookie;

use dutchheight\cookieboss\CookieBoss;

use yii\web\Cookie;

class ConsentService extends Component {

/**
Expand All @@ -28,11 +28,33 @@ public function isConsentWith($handle, $concentIfNotSet = false) {
}

/**
*
* Check for valid consent cookies and consent given at time
* @return Boolean
*/
public function hasConsentCookie() {
return Craft::$app->getRequest()->getCookies()->has('cookie-boss');

if (!Craft::$app->getRequest()->getCookies()->has('cookie-boss') || !Craft::$app->getRequest()->getCookies()->has('cookie-boss-consent-at')) {
return false;
}

if ((int)CookieBoss::$settings->lastSettingsUpdate > (int)Craft::$app->getRequest()->getCookies()->get('cookie-boss-consent-at')->value) {
return false;
}

return true;
}

/**
* Check for valid consent cookies and consent given at time
* @return int|null
*/
public function getConsentGivenAt() {

if (Craft::$app->getRequest()->getCookies()->has('cookie-boss-consent-at')) {
return (int)Craft::$app->getRequest()->getCookies()->get('cookie-boss-consent-at')->value;
}

return null;
}

/**
Expand Down Expand Up @@ -73,10 +95,19 @@ public function generateCookieData($groups) {
public function saveConsentCookies($data) {
$cookies = Craft::$app->response->cookies;
$cookies->remove('cookie-boss');
$cookies->remove('cookie-boss-consent-at');

$cookies->add(new Cookie([
'name' => 'cookie-boss',
'value' => json_encode($data),
'expire' => time() + CookieBoss::$settings->cookieTime
]));

// Set current time
$cookies->add(new Cookie([
'name' => 'cookie-boss-consent-at',
'value' => json_encode(time()),
'expire' => time() + CookieBoss::$settings->cookieTime
]));
}
}
9 changes: 2 additions & 7 deletions src/templates/askConsent/_index.twig
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{% if settings.enabled %}
{% if craft.app.config.general.devMode %}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
{% else %}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
{% endif %}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

{% do view.registerAssetBundle("dutchheight\\cookieboss\\assetbundles\\cookieboss\\CookieBossAsset") %}
{% set baseAssetsUrl = view.getAssetManager().getPublishedUrl('@dutchheight/cookieboss/assetbundles/cookieboss/dist', true) %}
Expand Down
Loading

0 comments on commit 03322c3

Please sign in to comment.