zephir/kirby-cookieconsent 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zephir/kirby-cookieconsent

Composer 安装命令:

composer require zephir/kirby-cookieconsent

包简介

Kirby cookieconsent plugin

README 文档

README

cover

A plugin to implement cookieconsent in Kirby.

  • Uses the open source cookieconsent library
  • Provides default translations for 5 cookie categories
  • Multilingual support (EN/DE/FR)
  • Customizable

Table of Contents

1. Installation

The recommended way of installing is by using Composer.

1.1 Composer

composer require zephir/kirby-cookieconsent

1.2 Download

Download and copy this repository to /site/plugins/kirby-cookieconsent.

1.3 Git submodule

git submodule add https://github.com/zephir/kirby-cookieconsent.git site/plugins/kirby-cookieconsent

2. Setup

Add snippet('cookieconsentCss') to your header and snippet('cookieconsentJs') to your footer.

3. Options

3.1 Available options

Option Type Default Description
root string "document.body" Root (parent) element where the modal will be appended as a last child.
autoShow boolean true Automatically show the consent modal if consent is not valid.
revision integer 1 Manages consent revisions; useful if you'd like to ask your users again for consent after a change in your cookie/privacy policy.
autoClearCookies boolean true Clears cookies when user rejects a specific category. It requires a valid autoClear array.
hideFromBots boolean true Stops the plugin's execution when a bot/crawler is detected, to prevent them from indexing the modal's content.
disablePageInteraction boolean true Creates a dark overlay and blocks the page scroll until consent is expressed.
lazyHtmlGeneration boolean true Delays the generation of the modal's markup until they're about to become visible, to improve the TTI score. You can detect when a modal is ready/created via the onModalReady callback.
guiOptions array see below You can extensively customize both the color scheme and the layout, based on your needs.
categories array see below Use to define your cookie categories.
translations array see below An array of translations for each language.
language array see below Defines the language and direction for single language installations.
cdn boolean false Whether to load the cookieconsent assets from jsdelivr.net or use the compiled assets provided with this plugin.

3.2 Defaults

'zephir.cookieconsent' => [
    'cdn' => false,
    'revision' => 1,
    'root' => 'document.body',
    'autoClearCookies' => true, // Only works when the categories have an autoClear array
    'autoShow' => true,
    'hideFromBots' => true,
    'disablePageInteraction' => false,
    'lazyHtmlGeneration' => true,
    'guiOptions' => [
        'consentModal' => [
            'layout' => 'box',
            'position' => 'bottom right',
            'flipButtons' => false,
            'equalWeightButtons' => true
        ],
        'preferencesModal' => [
            'layout' => 'box',
            // 'position' => 'left', // only relevant with the "bar" layout
            'flipButtons' => false,
            'equalWeightButtons' => true
        ]
    ],
    'categories' => [
        'necessary' => [
            'enabled' => true,
            'readOnly' => true
        ],
        'measurement' => [],
        'functionality' => [],
        'experience' => [],
        'marketing' => []
    ],
    'translations' => [
        'de' => require_once(__DIR__ . '/translations/de.php'),
        'en' => require_once(__DIR__ . '/translations/en.php'),
        'fr' => require_once(__DIR__ . '/translations/fr.php')
    ],
    'language' => [
        'locale' => 'en',
        'direction' => 'ltr'
    ]
]

3.3 Predefined cookie categories

Each cookie category can be used to control certain scripts. To learn how to manage scripts, head over to the CookieConsent documentation.

Categories that are predefined by this plugin:

Name Enabled Description
necessary The necessary cookies, can't be disabled by the user.
functionality Cookies for basic functionality and communication.
experience Cookies to improve the quality of the user experience and enable the user to interact with external content, networks and platforms.
measurement Cookies that help to measure traffic and analyze behavior.
marketing These cookies help us to deliver personalized ads or marketing content to you, and to measure their performance.

Predefined means that there are translations in all languages for each category.

To enable/disable categories you can use the categories option of the plugin.

'zephir.cookieconsent' => [
    'categories' => [
        'necessary' => [
            'enabled' => true, // marks the category as enabled by default
            'readOnly' => true
        ],
        'measurement' => [],
        'functionality' => [],
        'experience' => [],
        'marketing' => []
    ],
]

An empty array defines the category with the default options. You can pass additional options like enabled or readOnly in the array. Learn more about those options in the CookieConsent Documentation.

3.4 Removing a category

To remove a category you have to set its value to false.

'zephir.cookieconsent' => [
    'categories' => [
        'necessary' => [
            'enabled' => true,
            'readOnly' => true
        ],
        'measurement' => [],
        'functionality' => false, // will not be shown
        'experience' => false, // will not be shown
        'marketing' => []
    ],
]

4. Language

If you have a single language setup (kirby option languages not set to true), you will have to define the language option of the plugin. For multi-language setups you don't have to do anything, the plugin automatically uses the kirby language or falls back to the first translation in the translations array.

'zephir.cookieconsent' => [
    'language' => [
        'locale' => 'de', // The translation to use, default is en
        'direction' => 'ltr' // Either ltr or rtl, default is ltr
    ]
]

5. Translations

The translations are a central part of the plugin. By default, we provide translations for EN, DE, FR and the categories mentioned in 3.3 Predefined cookie categories. To customise or override the default translations, you will need to use the translations option.

5.1 Overriding specific translations

For this example we are overriding one string and adding a new category in the sections part of the EN translation.

  1. Create a new folder in your project. For this example we call it cc-translations.
  2. In that folder, create a new file en.php.
<?php
// site/cc-translations/en.php

return array_replace_recursive(
    // We assume the plugin was installed as suggested in the installation section of the readme.
    require_once(__DIR__ . '/../plugins/kirby-cookieconsent/translations/en.php'),
    [
        'consentModal' => [
            'title' => 'Lorem ipsum dolor!', // Override consentModal title
        ],
        "preferencesModal" => [
            "sections" => [
                [ // Add a new category in sections
                    "title" => "Custom scripts",
                    "description" => "Diese Cookies helfen uns, Ihnen personalisierte Werbung oder Marketinginhalte bereitzustellen und deren Leistung zu messen.",
                    "linkedCategory" => "custom-scripts",
                ],
            ]
        ]
    ]
);
  1. Now you need to require (or include) this file in config.php. Because we want to see the new category custom-scripts we also need to update the categories option.
'zephir.cookieconsent' => [
    'categories' => [
        'custom-scripts' => []
    ],
    'translations' => [
        'en' => require_once(__DIR__ . '/../cc-translations/en.php')
    ]
]

You can override the whole translation file by copying the default file and adjusting the values. You can also require the default translation and manually alter the array by modifying, adding or deleting entries.

6. Events

You can find all available events in the CookieConsent Documentation.

The CookieConsent object is available through the window object (window.CookieConsent).

License

MIT

Credits

zephir/kirby-cookieconsent 适用场景与选型建议

zephir/kirby-cookieconsent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.12k 次下载、GitHub Stars 达 51, 最近一次更新时间为 2023 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 zephir/kirby-cookieconsent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zephir/kirby-cookieconsent 我们能提供哪些服务?
定制开发 / 二次开发

基于 zephir/kirby-cookieconsent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 51
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 51
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-07