| 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 |
|