PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
ablocks / addons / cookie-consent / cookie-consent.php

cookie-consent.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.14.0, at addons/cookie-consent/cookie-consent.php

100 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocksCookieConsent;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 use ABlocks\Interfaces\AddonInterface;
10
11 /**
12 * Cookie Consent addon.
13 *
14 * A consent banner is the visible tenth of this feature. The other nine tenths
15 * is prior blocking — making sure a third-party tag has not already run by the
16 * time the visitor is asked. That is what the two gating layers do, and it is
17 * why this addon exists inside aBlocks rather than as a styled popup: the
18 * plugin already owns a script-gating mechanism (see `ABlocks\Performance\
19 * ScriptGate`) and this re-keys it from "first interaction" to "consent for
20 * category X".
21 *
22 * One constraint shapes everything: nothing here may branch the rendered HTML
23 * on the visitor's consent cookie. Page caches serve one document per URL, so a
24 * PHP-side branch would hand the first visitor's consent state to everyone
25 * after them. The banner therefore ships on every page, hidden, and the client
26 * decides what to show and what to release.
27 */
28 final class CookieConsent implements AddonInterface {
29
30 private $addon_name = 'cookie-consent';
31
32 private function __construct() {
33 $this->define_constants();
34 $this->init_addon();
35 }
36
37 public function define_constants() {
38 define( 'ABLOCKS_COOKIE_CONSENT_VERSION', '1.0' );
39 define( 'ABLOCKS_COOKIE_CONSENT_ADDON_NAME', $this->addon_name );
40 define( 'ABLOCKS_COOKIE_CONSENT_SETTINGS_NAME', 'ablocks_cookie_consent' );
41 define( 'ABLOCKS_COOKIE_CONSENT_DIR_PATH', ABLOCKS_ADDONS_DIR_PATH . 'cookie-consent/' );
42 }
43
44 public function init_addon() {
45 add_action( "ablocks/addons/activated_{$this->addon_name}", [ $this, 'addon_activation_hook' ] );
46
47 if ( ! \ABlocks\Helper::get_addon_active_status( $this->addon_name ) ) {
48 return;
49 }
50
51 // The admin screen and the recording endpoint are always available; the
52 // gating and the banner only exist on the front end.
53 Ajax::init();
54 Rest::init();
55 Record::init();
56 // Outside the front-end block below: a form can be submitted through the
57 // REST controller or through admin-ajax, and only one of those counts as
58 // the front end.
59 FormConsent::init();
60
61 if ( ! is_admin() ) {
62 ConsentMode::init();
63 Tags::init();
64 Frontend::init();
65 // Gating asks whether this request is a feed, and a conditional
66 // query tag has no answer before the query has run. Registering on
67 // `wp` still lands ahead of `template_redirect` and of any script
68 // being printed, so nothing is missed by waiting.
69 add_action(
70 'wp',
71 function () {
72 Gating::init();
73 Buffer::init();
74 }
75 );
76 }
77
78 Admin::init();
79 }
80
81 public static function init() {
82 static $instance = false;
83
84 if ( ! $instance ) {
85 $instance = new self();
86 }
87
88 return $instance;
89 }
90
91 /**
92 * Create the consent record table the first time the addon is switched on.
93 * Doing it here rather than in the plugin installer keeps the table out of
94 * sites that never enable the feature.
95 */
96 public function addon_activation_hook() {
97 Database::create_table();
98 }
99 }
100