-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ilmenite-cookie-consent.php
318 lines (269 loc) · 8.83 KB
/
ilmenite-cookie-consent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/*
* Plugin Name: Ilmenite Cookie Consent
* Plugin URI: https://github.com/bernskioldmedia/Ilmenite-Cookie-Consent
* Description: A simple, developer-friendly WordPress plugin that lets visitors know that the site is using cookies.
* Author: Bernskiold Media
* Version: 3.3.0
* Author URI: http://www.bernskioldmedia.com/
* Text Domain: ilmenite-cookie-consent
* Domain Path: /languages
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Ilmenite Cookie Consent
*/
class Ilmenite_Cookie_Consent {
/**
* The Plugin Path
*
* @var string
*/
public $plugin_path;
/**
* The Plugin URL
*
* @var string
*/
public $plugin_url;
/**
* The Plugin Version
*
* @var string
*/
public $version = '3.3.0';
/**
* The single instance of the class
*
* @var Ilmenite_Cookie_Consent|null
*/
protected static $_instance = null;
/**
* Instance
*
* @return Ilmenite_Cookie_Consent
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor.
*/
public function __construct() {
// Set Developer Mode Constant.
if ( ! defined( 'ILCC_DEV_MODE' ) ) {
define( 'ILCC_DEV_MODE', apply_filters( 'ilcc_dev_mode', false ) );
}
// Set the plugin path.
$this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
// Set the plugin URL.
$this->plugin_url = untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) );
// Load classes.
$this->classes();
// Hooks to run on plugin init.
$this->init_hooks();
do_action( 'ilcc_loaded' );
}
/**
* Hooks into various necessary hooks
* at the init time.
*
* @return void
*/
public function init_hooks() {
do_action( 'before_ilcc_init' );
// Add Scripts.
add_action( 'wp_enqueue_scripts', [ $this, 'scripts' ] );
// Add Styles.
add_action( 'wp_enqueue_scripts', [ $this, 'styles' ] );
// Add Translation Loading.
add_action( 'plugins_loaded', [ $this, 'load_languages' ] );
// Boot other classes.
ILCC_Settings::hooks();
ILCC_Consent::hooks();
do_action( 'ilcc_init' );
}
/**
* Load Classes
*/
public function classes() {
require_once 'classes/class-consent.php';
require_once 'classes/class-trackers.php';
require_once 'classes/class-settings.php';
}
/**
* Load translations in the right order.
*/
public function load_languages() {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
$locale = apply_filters( 'plugin_locale', $locale, 'ilmenite-cookie-consent' );
unload_textdomain( 'ilmenite-cookie-consent' );
// Start checking in the main language dir.
load_textdomain( 'ilmenite-cookie-consent', WP_LANG_DIR . '/ilmenite-cookie-consent/ilmenite-cookie-consent-' . $locale . '.mo' );
// Otherwise, load from the plugin.
load_plugin_textdomain( 'ilmenite-cookie-consent', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Load necessary scripts for the plugin.
*/
public function scripts() {
/**
* If the banner shouldn't load on this page, bail early.
*/
if ( ! self::should_load_banner() ) {
return;
}
wp_register_script( 'ilmenite-cookie-consent', $this->plugin_url . '/assets/scripts/dist/cookie-banner.js', [ 'ilcc-vendor' ], $this->version, true );
wp_register_script( 'ilcc-vendor', $this->plugin_url . '/assets/scripts/dist/cookie-banner-vendor.js', [], $this->version, false );
/**
* We localize the script to add our texts.
* These are changeable by filters. See the functions
* that get the texts below.
*/
wp_localize_script( 'ilmenite-cookie-consent', 'ilcc', [
'cookieConsentTitle' => ILCC_Settings::get_consent_title(),
'cookieConsentText' => ILCC_Settings::get_consent_text(),
'acceptText' => ILCC_Settings::get_accept_text(),
'style' => ILCC_Settings::get_style(),
'configureSettingsText' => ILCC_Settings::get_configure_settings_text(),
'necessaryText' => ILCC_Settings::get_only_necessary_text(),
'rememberDuration' => self::get_remember_me_duration(),
'preferencesCookieName' => self::get_preferences_cookie_name(),
'consentedCategoriesCookieName' => self::get_categories_cookie_name(),
'necessaryHeading' => ILCC_Settings::get_settings_necessary_heading(),
'necessaryDescription' => ILCC_Settings::get_settings_necessary_description(),
'isAnalyticsShown' => ILCC_Settings::is_analytics_shown() ? '1' : '0',
'analyticsHeading' => ILCC_Settings::get_settings_analytics_heading(),
'analyticsDescription' => ILCC_Settings::get_settings_analytics_description(),
'isMarketingShown' => ILCC_Settings::is_marketing_shown() ? '1' : '0',
'marketingHeading' => ILCC_Settings::get_settings_marketing_heading(),
'marketingDescription' => ILCC_Settings::get_settings_marketing_description(),
'saveSettingsText' => ILCC_Settings::get_save_settings_button_title(),
'settingsTitle' => ILCC_Settings::get_settings_title(),
'settingsDescription' => ILCC_Settings::get_settings_description(),
'consentModeEnabled' => ILCC_Settings::is_consent_mode_integration_enabled() ? '1' : '0',
'debug' => $this->is_debugging(),
] );
/**
* Add the whitelist and blacklist.
*/
wp_add_inline_script( 'ilcc-vendor', $this->get_allow_and_disallowlists(), 'before' );
/**
* Add consent mode defaults.
*/
wp_add_inline_script('ilmenite-cookie-consent', ILCC_Consent::add_consent_mode_defaults(), 'before');
// Finally, enqueue!
wp_enqueue_script( 'ilcc-vendor' );
wp_enqueue_script( 'ilmenite-cookie-consent' );
}
/**
* Get the black- and whitelist HTML.
*
* @return string
*/
public function get_allow_and_disallowlists() {
$output = "window.YETT_BLACKLIST = [" . esc_js( ILCC_Trackers::get_disallow_for_js() ) . "];\n";
if ( ! empty( ILCC_Trackers::get_allowlist_for_js() ) ) {
$output .= 'window.YETT_WHITELIST = [' . esc_js( ILCC_Trackers::get_allowlist_for_js() ) . '];';
}
return $output;
}
/**
* Load the built-in styles for the plugin.
*/
public function styles() {
/**
* If the banner shouldn't load on this page, bail early.
*/
if ( ! self::should_load_banner() ) {
return;
}
/**
* Don't load anything if we are asked not
* to load the stylesheet.
*/
if ( false === apply_filters( 'ilcc_load_stylesheet', true ) || true === ILCC_DEV_MODE ) {
return;
}
/**
* Register the main stylesheet.
*/
wp_register_style( 'ilmenite-cookie-consent', $this->plugin_url . '/assets/styles/dist/cookie-banner.css', false, $this->version, 'all' );
// Finally, enqueue!
wp_enqueue_style( 'ilmenite-cookie-consent' );
}
/**
* Check if we should be loading the banner on this page.
* This hook allows overriding to hide the banner on certain pages or templates.
*
* @return bool
*/
public static function should_load_banner() {
return apply_filters( 'ilcc_is_active_on_page', true );
}
/**
* Get the name of the cookie.
*
* @return string
*/
public static function get_preferences_cookie_name() {
return apply_filters( 'ilcc_preferences_cookie_name', 'ilcc_has_preferences' );
}
/**
* Get Categories Cookie Name
*
* @return string
*/
public static function get_categories_cookie_name() {
return apply_filters( 'ilcc_categories_cookie_name', 'ilcc_consent_categories' );
}
/**
* Get how many days the user should be remembered.
*
* @return int
*/
public static function get_remember_me_duration() {
return apply_filters( 'ilcc_remember_duration', 90 );
}
/**
* Check if we are debugging or not.
*
* @return bool
*/
public function is_debugging() {
if ( defined( 'ILCC_DEBUG' ) ) {
return ILCC_DEBUG;
}
return false;
}
}
/**
* Returns an instance of the plugin class.
*
* @return Ilmenite_Cookie_Consent
*/
function ilmenite_cookie_consent() {
return Ilmenite_Cookie_Consent::instance();
}
// Initialize the class instance only once
ilmenite_cookie_consent();