Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated CookieConsent to version 3.0.0. and some minor updates #4

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b570a9c
WIP: Migrating to CookieConsent 3. Fix consent data format sent to ba…
rahkapetteri Mar 1, 2024
0aa85af
WIP: Migrating to CookieConsent 3. Fixed: embed consent state, data s…
rahkapetteri Mar 1, 2024
469621a
Updated readme, added option to declare cookies, iframemanager consen…
rahkapetteri Mar 4, 2024
b45e823
Fixes for embeds consent changing, added google consent mode for anal…
rahkapetteri Mar 5, 2024
3c3dfb7
Something weird happened
rahkapetteri Mar 5, 2024
d0feb4d
Something weird happened
rahkapetteri Mar 5, 2024
3e597cf
Documentation fix
rahkapetteri Mar 5, 2024
57c7029
Removed GA detection script
rahkapetteri Mar 5, 2024
fba2228
Changed default style
rahkapetteri Mar 5, 2024
39c8744
Updated defaults, readme and added cc-link style from 2.9.
rahkapetteri Mar 5, 2024
3857067
pref modal default change
rahkapetteri Mar 5, 2024
470fa0a
Modal width fix + button default change
rahkapetteri Mar 6, 2024
6b33980
Removed button from string
rahkapetteri Mar 6, 2024
ccaa0b3
fix indentation
rahkapetteri Mar 6, 2024
f289561
fix indentation
rahkapetteri Mar 6, 2024
ce1baad
Deleted empty file
rahkapetteri Mar 6, 2024
04e0f78
fix
rahkapetteri Mar 7, 2024
ea2c77c
expire date fix + indendation
rahkapetteri Mar 7, 2024
37ddc11
-data-cookiecategory replaces
rahkapetteri Mar 7, 2024
f67e281
Iframe Manager required to be run every page load to detect iframe co…
rahkapetteri Mar 7, 2024
646a799
Removed miscopy
rahkapetteri Mar 7, 2024
00b1908
removed not needed cookie removal
rahkapetteri Mar 8, 2024
775c95e
Disable autoupdater and add latest change from source branch
rahkapetteri Apr 22, 2024
16a517b
Added forgotten edited info
rahkapetteri Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 75 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

Air cookie provides simple cookie banner and management.

Uses the [CookieConsent](https://orestbida.com/demo-projects/cookieconsent/) javascript plugin as a base, making its usage with WordPress easier.
Uses the [CookieConsent V3.0.0](https://playground.cookieconsent.orestbida.com/) javascript plugin as a base, making its usage with WordPress easier.

# Features

- Simple and lightweight cookie banner
- Third party embeds blocking until cookies accepted
- Allows to remove cookies after changing consent
- Easy to load scripts and execute custom javascript when cookies are accepted
- Support for multiple different cookie categories
- Polylang support for multilingual websites
Expand All @@ -27,15 +28,15 @@ Uses the [CookieConsent](https://orestbida.com/demo-projects/cookieconsent/) jav
Remember to add a link into the footer, which allows opening the cookie settings anytime!

```html
<a href="#" data-cc="c-settings" class="cc-link">Cookie settings</a>
<button type="button" data-cc="show-preferencesModal">View cookie settings</button>
```

If you have Polylang installed and active, use

```html
<a href="#" data-cc="c-settings" class="cc-link">
<button type="button" data-cc="show-preferencesModal">
<?php echo pll_translate_string( 'Evästeasetukset', pll_current_language() ); ?>
</a>
</button>
```

## Cookie categories
Expand All @@ -50,7 +51,7 @@ function my_add_cookie_category( $categories ) {
'enabled' => false, // it is advised to have categories disabled by default
'readonly' => false, // user should have always control over categories
'title' => 'Ads',
'description' => 'This site uses external services to display ads, and they might set some cookies.',
'description' => 'This site uses external services to display ads, and they might set some cookies.', // Check how to remove cookies from example below.
];

return $categories;
Expand All @@ -60,13 +61,68 @@ function my_add_cookie_category( $categories ) {
When adding new categories, the function itself is responsile for handling the translations for title and description.

There is also `air_cookie\categories\{category-key}` filter available to change the settings of indivual category.
```php
add_filter( 'air_cookie\categories\{analytics}', 'my_change_category_analytics' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be air_cookie\categories\analytics, without the braces.

Copy link
Author

@rahkapetteri rahkapetteri May 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I can´t get it to work without brackets. Only way to use the filter without brackets would require to change in settings.php:

$categories[ $key ] = apply_filters( "air_cookie\categories\{$category_key}", $category );

to $categories[ $key ] = apply_filters( "air_cookie\categories\\{$category_key}", $category );

Atleast that´s how PHP-docs say to handle backslashed variables 🤔

// Won't work, outputs: C:\folder\{fantastic}.txt
echo "C:\folder\{$great}.txt"

// Works, outputs: C:\folder\fantastic.txt
echo "C:\\folder\\{$great}.txt"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in the code, I meant in the example 😄

If user wants to filter analytics they need to create a filter add_filter ( 'air_cookie\categories\analytics', 'my_change_analytics' );, which is then called by apply_filters( "air_cookie\categories\{$category_key}", $category );.

"air_cookie\categories\{$category_key}" gets resolved to "air_cookie\categories\analytics as {$category_key} is a string template.

Copy link
Member

@raikasdev raikasdev Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed a bug. #10

function my_change_category_analytics( $edited_categoy ) {
$edited_category = [
'key' => 'analytics',
'enabled' => false,
'readonly' => false,
'title' => 'Analytics',
'description' => 'This site uses Google Analytics and it set some cookies. Read more about those from privacy policy.',
'autoClear' => [ // Optional: autoclear allows you to define cookies, which will be removed after changing consent. List all cookies to correct categories. Possible to use string or regex format (format is a bit different than official docs points out! https://cookieconsent.orestbida.com/reference/configuration-reference.html#category-autoclear).
'cookies' => [
[
'name' => '^(_ga)', // Match all cookies starting with '_ga',
],
[
'name' => '_gid',
],
],
],
];

return $edited_category;
}
```

ℹ If you add Google Analytics, remember [Google consent mode](https://developers.google.com/tag-platform/security/guides/consent?consentmode=advanced#upgrade-consent-v2) scripts.

```javascript
<script
type="text/plain"
data-category="analytics">
// Executed when the "analytics" category is enabled. Use this snippet after defining GA.
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'granted',
'ad_user_data': 'denied',
'ad_personalization': 'denied'
});
</script>

<script
type="text/plain"
data-category="!analytics">
// Executed when the "analytics" category is disabled. Use this snippet after defining GA.
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('consent', 'update', {
'ad_storage': 'denied',
'analytics_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied'
});
</script>
```
## Loading scripts after cookies have been accepted

The easiest way to load external script is by altering the `script` tag to be:

```html
<script type="text/plain" data-src="<uri-to-script>" data-cookiecategory="analytics" defer>
<script type="text/plain" data-src="<uri-to-script>" data-category="analytics">
```

The example above works only, if the script does not require any extra javascript to be executed after the script has been loaded. If you need to execute extra javascript, use the example below.
Expand All @@ -75,7 +131,7 @@ The example above works only, if the script does not require any extra javascrip
add_action( 'air_cookie_js_analytics', 'my_add_js_for_analytics' );
function my_add_js_for_analytics() {
ob_start(); ?>
cc.loadScript( 'https://www.google-analytics.com/analytics.js', function() {
CookieConsent.loadScript( 'https://www.google-analytics.com/analytics.js', function() {
ga('create', 'UA-XXXXXXXX-Y', 'auto'); //replace UA-XXXXXXXX-Y with your tracking code
ga('send', 'pageview');
} );
Expand Down Expand Up @@ -109,7 +165,7 @@ function my_add_js_for_analytics() {
add_action( 'air_cookie_js_analytics', 'my_add_js_for_analytics' );
function my_add_js_for_analytics() {
ob_start(); ?>
cc.loadScript( 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X', function() {
CookieConsent.loadScript( 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X', function() {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
Expand All @@ -125,7 +181,7 @@ function my_add_js_for_analytics() {
add_action( 'air_cookie_js_analytics', 'my_add_js_for_analytics' );
function my_add_js_for_analytics() {
ob_start(); ?>
cc.loadScript( 'https://www.google-analytics.com/analytics.js', function() {
CookieConsent.loadScript( 'https://www.google-analytics.com/analytics.js', function() {
ga('create', 'UA-XXXXXXXX-Y', 'auto'); //replace UA-XXXXXXXX-Y with your tracking code
ga('send', 'pageview');
} );
Expand Down Expand Up @@ -153,7 +209,7 @@ add_action( 'air_cookie_js_functional', function() {
}

ob_start(); ?>
cc.loadScript( '<?php echo esc_url( $stampedio_url ) ?>' );
CookieConsent.loadScript( '<?php echo esc_url( $stampedio_url ) ?>' );
<?php echo ob_get_clean(); // phpcs:ignore
} ); // end woocommerce_ga_integration_script_for_air_cookie

Expand Down Expand Up @@ -182,7 +238,7 @@ function woocommerce_stampedio_script_for_air_cookie() {
}

ob_start(); ?>
cc.loadScript( '<?php echo esc_url( $stampedio_url ) ?>' );
CookieConsent.loadScript( '<?php echo esc_url( $stampedio_url ) ?>' );
<?php echo ob_get_clean(); // phpcs:ignore
} // end woocommerce_ga_integration_script_for_air_cookie
```
Expand All @@ -207,7 +263,7 @@ function my_add_js_for_<category-key>() {
If you wish to use your own `script` tag, it is possible with example below

```javascript
<script type="text/plain" data-cookiecategory="<category-key>">
<script type="text/plain" data-category="<category-key>">
console.log( 'Hello world!' );
</script>
```
Expand All @@ -234,28 +290,28 @@ document.addEventListener( 'air_cookie', (event) => {

## Changing settings

Setting names do follow the [CookieConsents option](https://github.com/orestbida/cookieconsent#apis--configuration-parameters) names. Some settings defaults are set to be different than the CookieConsent defaults:
Setting names do follow the [CookieConsents option](https://cookieconsent.orestbida.com/reference/configuration-reference.html#configuration-reference) names. Some settings defaults are set to be different than the CookieConsent defaults:

Setting | Value
--- | ---
`cookie_name` | air_cookie
`current_lang` | _value from polylang or locale option_
cookie/name | air_cookie
`revision` | _automatically calculated from cookie categories_
`page_scripts` | true
gui_options/consent_modal/layout | box
gui_options/consent_modal/position | bottom left
settings/language/default | _value from polylang or locale option_
guiOptions/consentModal/layout | cloud inline
guiOptions/consentModal/position | bottom center

You may change the settings with `air_cookie\settings` filter which contains all settings or `air_cookie\settings\{setting-name}` filter for indivual setting.

```php
add_filter( 'air_cookie\settings', 'my_modify_cc_settings' );
function my_modify_cc_settings( $settings ) {
$settings['page_scripts'] = false;
$settings['guiOptions']['consentModal']['position'] = "top right";
return $settings;
}
```

```php
// page_scripts not used in CC 3.0.0
add_filter( 'air_cookie\settings\page_scripts', 'my_modify_cc_setting_page_scripts' );
function my_modify_cc_setting_page_scripts( $setting ) {
return false;
Expand Down
10 changes: 6 additions & 4 deletions air-cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Tested up to: 6.4.3
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* @Modified by: Jesse Raitapuro (Digiaargh)
* @Modified time: 2024-04-22 16:30:00
*
* @package air-cookie
*/
Expand Down Expand Up @@ -46,7 +48,7 @@ function get_database_version() {
* @since 0.1.0
*/
function get_script_version() {
return '2.9.1';
return '3.0.0';
} // end get_script_version

/**
Expand All @@ -57,12 +59,12 @@ function get_script_version() {
require 'plugin-helpers.php';

/**
* Github updater.
* Github updater. Disabled by commenting out.
*
* @since 0.1.0
*/
require plugin_base_path() . '/plugin-update-checker/plugin-update-checker.php';
$update_checker = \Puc_v4_Factory::buildUpdateChecker( 'https://github.com/digitoimistodude/air-cookie', __FILE__, 'air-cookie' );
// require plugin_base_path() . '/plugin-update-checker/plugin-update-checker.php';
// $update_checker = \Puc_v4_Factory::buildUpdateChecker( 'https://github.com/digitoimistodude/air-cookie', __FILE__, 'air-cookie' );
Comment on lines 61 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These kinds of changes shouldn't be included in PRs 😃

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was meant for local only 🤦‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, happens to the best of us.


/**
* Compile settings for the script.
Expand Down
2 changes: 1 addition & 1 deletion assets/cookieconsent.css

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions assets/cookieconsent.js

Large diffs are not rendered by default.

Loading