PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.0.0
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.0.0
4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / src / addons / config / Settings_Config.php
cookiebot / src / addons / config Last commit date
Settings_Config.php 4 years ago
Settings_Config.php
595 lines
1 <?php
2
3 namespace cybot\cookiebot\addons\config;
4
5 use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Addon;
6 use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Plugin_Addon;
7 use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Theme_Addon;
8 use cybot\cookiebot\addons\controller\addons\jetpack\Jetpack;
9 use cybot\cookiebot\addons\controller\addons\jetpack\widget\Base_Jetpack_Widget;
10 use cybot\cookiebot\lib\Settings_Page_Tab;
11 use cybot\cookiebot\lib\Settings_Service_Interface;
12 use cybot\cookiebot\lib\Cookiebot_WP;
13 use Exception;
14 use InvalidArgumentException;
15 use ReflectionClass;
16 use function cybot\cookiebot\lib\asset_url;
17 use function cybot\cookiebot\lib\cookiebot_addons_get_dropdown_languages;
18 use function cybot\cookiebot\lib\get_view_html;
19 use function cybot\cookiebot\lib\include_view;
20
21 class Settings_Config {
22
23 /**
24 * @var Settings_Service_Interface
25 */
26 protected $settings_service;
27
28 /**
29 * Settings_Config constructor.
30 *
31 * @param Settings_Service_Interface $settings_service
32 *
33 * @since 1.3.0
34 */
35 public function __construct( Settings_Service_Interface $settings_service ) {
36 $this->settings_service = $settings_service;
37 }
38
39 /**
40 * Load data for settings page
41 *
42 * @since 1.3.0
43 */
44 public function load() {
45 add_action( 'admin_menu', array( $this, 'add_submenu' ) );
46 add_action( 'admin_init', array( $this, 'register_settings' ) );
47 add_action( 'admin_enqueue_scripts', array( $this, 'add_wp_admin_style_script' ) );
48 add_action(
49 'update_option_cookiebot_available_addons',
50 array(
51 $this,
52 'post_hook_available_addons_update_option',
53 ),
54 10,
55 3
56 );
57 }
58
59 /**
60 * Registers submenu in options menu.
61 *
62 * @since 1.3.0
63 */
64 public function add_submenu() {
65 add_submenu_page(
66 'cookiebot',
67 esc_html__( 'Prior Consent', 'cookiebot' ),
68 esc_html__( 'Prior Consent', 'cookiebot' ),
69 'manage_options',
70 'cookiebot-addons',
71 array(
72 $this,
73 'setting_page',
74 ),
75 40
76 );
77 }
78
79 /**
80 * Load css styling to the settings page
81 *
82 * @throws InvalidArgumentException
83 * @since 1.3.0
84 */
85 public function add_wp_admin_style_script( $hook ) {
86 if ( $hook !== 'cookiebot_page_cookiebot-addons' ) {
87 return;
88 }
89
90 wp_enqueue_script(
91 'cookiebot_tiptip_js',
92 asset_url( 'js/backend/jquery.tipTip.js' ),
93 array( 'jquery' ),
94 '1.8',
95 true
96 );
97 wp_enqueue_script(
98 'cookiebot_addons_custom_js',
99 asset_url( 'js/backend/prior-consent-settings.js' ),
100 array( 'jquery' ),
101 '1.8',
102 true
103 );
104 wp_localize_script(
105 'cookiebot_addons_custom_js',
106 'php',
107 array( 'remove_link' => ' <a href="" class="submitdelete deletion">' . esc_html__( 'Remove language', 'cookiebot' ) . '</a>' )
108 );
109 wp_enqueue_style(
110 'cookiebot_addons_custom_css',
111 asset_url( 'css/admin_styles.css' ),
112 null,
113 Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION
114 );
115 }
116
117 /**
118 * Registers addons for settings page.
119 *
120 * @throws Exception
121 * @since 1.3.0
122 */
123 public function register_settings() {
124 global $pagenow;
125
126 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
127 if ( ( isset( $_GET['page'] ) && $_GET['page'] === 'cookiebot-addons' ) || $pagenow === 'options.php' ) {
128 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
129 if ( isset( $_GET['tab'] ) && 'unavailable_addons' === $_GET['tab'] ) {
130 $this->register_unavailable_addons();
131 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
132 } elseif ( ( isset( $_GET['tab'] ) && 'jetpack' === $_GET['tab'] ) ) {
133 $this->register_jetpack_addon();
134 } else {
135 $this->register_available_addons();
136 }
137
138 if ( $pagenow === 'options.php' ) {
139 $this->register_jetpack_addon();
140 }
141 }
142 }
143
144 /**
145 * Register available addons
146 *
147 * @throws Exception
148 * @since 1.3.0
149 */
150 private function register_available_addons() {
151 add_settings_section(
152 'available_addons',
153 'Available plugins',
154 array(
155 $this,
156 'header_available_addons',
157 ),
158 'cookiebot-addons'
159 );
160
161 /** @var Base_Cookiebot_Addon $addon */
162 foreach ( $this->settings_service->get_addons() as $addon ) {
163 if ( $addon->is_addon_installed() && $addon->is_addon_activated() ) {
164 add_settings_field(
165 $addon::OPTION_NAME,
166 get_view_html(
167 'admin/settings/prior-consent/partials/extra-information.php',
168 array(
169 'label' => $addon::ADDON_NAME,
170 'extra_information_lines' => $addon->get_extra_information(),
171 )
172 ),
173 array(
174 $this,
175 'available_addon_callback',
176 ),
177 'cookiebot-addons',
178 'available_addons',
179 array(
180 'addon' => $addon,
181 )
182 );
183
184 register_setting(
185 'cookiebot_available_addons',
186 'cookiebot_available_addons',
187 array(
188 $this,
189 'sanitize_cookiebot',
190 )
191 );
192 }
193 }
194 }
195
196 /**
197 * Register jetpack addon - new tab for jetpack specific settings
198 *
199 * @throws Exception
200 * @since 1.3.0
201 */
202 private function register_jetpack_addon() {
203 add_settings_section(
204 'jetpack_addon',
205 'Jetpack',
206 array(
207 $this,
208 'jetpack_addons_header_callback',
209 ),
210 'cookiebot-addons'
211 );
212
213 /** @var Jetpack $addon */
214 foreach ( $this->settings_service->get_addons() as $addon ) {
215 if ( 'Jetpack' === ( new ReflectionClass( $addon ) )->getShortName() ) {
216 if ( $addon->is_addon_installed() && $addon->is_addon_activated() ) {
217 foreach ( $addon->get_widgets() as $widget ) {
218 add_settings_field(
219 $widget->get_widget_option_name(),
220 get_view_html(
221 'admin/settings/prior-consent/partials/extra-information.php',
222 array(
223 'label' => $widget->get_label(),
224 'extra_information_lines' => $widget->get_extra_information(),
225 )
226 ),
227 array(
228 $this,
229 'jetpack_addon_callback',
230 ),
231 'cookiebot-addons',
232 'jetpack_addon',
233 array(
234 'widget' => $widget,
235 'addon' => $addon,
236 )
237 );
238
239 register_setting( 'cookiebot_jetpack_addon', 'cookiebot_jetpack_addon' );
240 }
241 }
242 }
243 }
244 }
245
246 /**
247 * Registers unavailabe addons
248 *
249 * @throws Exception
250 * @version 2.1.3
251 * @since 1.3.0
252 */
253 private function register_unavailable_addons() {
254 add_settings_section(
255 'unavailable_addons',
256 'Unavailable plugins',
257 array(
258 $this,
259 'unavailable_addons_header_callback',
260 ),
261 'cookiebot-addons'
262 );
263
264 $addons = $this->settings_service->get_addons();
265
266 /** @var Base_Cookiebot_Addon $addon */
267 foreach ( $addons as $addon ) {
268 if ( ! $addon->is_addon_installed() || ! $addon->is_addon_activated() ) {
269 // not installed plugins
270 add_settings_field(
271 $addon::ADDON_NAME,
272 get_view_html(
273 'admin/settings/prior-consent/partials/extra-information.php',
274 array(
275 'label' => $addon::ADDON_NAME,
276 'extra_information_lines' => $addon->get_extra_information(),
277 )
278 ),
279 array(
280 $this,
281 'unavailable_addon_settings_field_callback',
282 ),
283 'cookiebot-addons',
284 'unavailable_addons',
285 array( 'addon' => $addon )
286 );
287 register_setting( $addon::OPTION_NAME, 'cookiebot_unavailable_addons' );
288 }
289 }
290 }
291
292 /**
293 * Jetpack tab - header
294 *
295 * @throws InvalidArgumentException
296 * @since 1.3.0
297 */
298 public function jetpack_addons_header_callback() {
299 include_view( 'admin/settings/prior-consent/jetpack-widgets/tab-header.php' );
300 }
301
302 /**
303 * Jetpack tab - widget callback
304 *
305 * @param $args array Information about the widget addon and the option
306 *
307 * @throws InvalidArgumentException
308 * @since 1.3.0
309 */
310 public function jetpack_addon_callback( $args ) {
311 $widget = isset( $args['widget'] ) ? $args['widget'] : null;
312 $addon = isset( $args['addon'] ) ? $args['addon'] : null;
313
314 if ( ! is_a( $widget, Base_Jetpack_Widget::class ) ) {
315 throw new InvalidArgumentException();
316 }
317
318 if ( ! is_a( $addon, Base_Cookiebot_Addon::class ) ) {
319 throw new InvalidArgumentException();
320 }
321
322 $widget_is_enabled = $widget->is_widget_enabled();
323 $widget_placeholder_is_enabled = $widget->is_widget_placeholder_enabled();
324 $widget_default_placeholder = $widget->get_widget_default_placeholder();
325 $widget_option_name = $widget->get_widget_option_name();
326 $widget_placeholders_array = $widget->get_widget_placeholders();
327 $widget_placeholders_array_keys = array_keys( $widget_placeholders_array );
328 $first_placeholder_language = isset( $widget_placeholders_array_keys[0] )
329 ? $widget_placeholders_array_keys[0]
330 : null;
331 $site_default_languages_dropdown_html = 'cookiebot_jetpack_addon[' . $widget_option_name . '][placeholder][languages][site-default]';
332 $widget_placeholders = array_map(
333 function( $language, $placeholder ) use ( $widget_option_name, $widget_placeholders_array, $first_placeholder_language ) {
334 $removable = $first_placeholder_language !== $language;
335 $option_name = 'cookiebot_jetpack_addon[' . $widget_option_name . '][placeholder][languages][' . $language . ']';
336 $languages_dropdown_html = cookiebot_addons_get_dropdown_languages(
337 'placeholder_select_language',
338 $option_name,
339 $language
340 );
341 return array(
342 'name' => $option_name,
343 'removable' => $removable,
344 'language' => $language,
345 'placeholder' => $placeholder,
346 'languages_dropdown_html' => $languages_dropdown_html,
347 );
348 },
349 array_keys( $widget_placeholders_array ),
350 array_values( $widget_placeholders_array )
351 );
352 $placeholder_helper = $addon->get_placeholder_helper();
353 $placeholders_html = $widget->widget_has_placeholder()
354 ? get_view_html(
355 'admin/settings/prior-consent/partials/placeholder-submitboxes.php',
356 array(
357 'placeholders' => $widget_placeholders,
358 'placeholder_helper' => $placeholder_helper,
359 )
360 )
361 : get_view_html(
362 'admin/settings/prior-consent/partials/placeholder-submitbox-default.php',
363 array(
364 'site_default_languages_dropdown_html' => $site_default_languages_dropdown_html,
365 'name' => 'cookiebot_jetpack_addon[' . $widget_option_name . '][placeholder][languages][site-default]',
366 'default_placeholder' => $widget_default_placeholder,
367 'placeholder_helper' => $placeholder_helper,
368 )
369 );
370
371 $view_args = array(
372 'widget_option_name' => $widget_option_name,
373 'widget_is_enabled' => $widget_is_enabled,
374 'widget_cookie_types' => $widget->get_widget_cookie_types(),
375 'widget_placeholder_is_enabled' => $widget_placeholder_is_enabled,
376 'placeholders_html' => $placeholders_html,
377 );
378
379 include_view( 'admin/settings/prior-consent/jetpack-widgets/tab.php', $view_args );
380 }
381
382 /**
383 * Returns header for installed plugins
384 *
385 * @since 1.3.0
386 */
387 public function header_available_addons() {
388 ?>
389 <p>
390 <?php esc_html_e( 'Below is a list of addons for Cookiebot. Addons help you make installed plugins GDPR compliant.', 'cookiebot' ); ?>
391 <br/>
392 <?php esc_html_e( 'These addons are available because you have the corresponding plugins installed and activated.', 'cookiebot' ); ?>
393 <br/>
394 <?php esc_html_e( 'Deactivate an addon if you want to handle GDPR compliance yourself, or through another plugin.', 'cookiebot' ); ?>
395 </p>
396 <?php
397 }
398
399 /**
400 * Available addon callback:
401 * - checkbox to enable
402 * - select field for cookie type
403 *
404 * @param $args
405 *
406 * @throws InvalidArgumentException
407 * @since 1.3.0
408 */
409 public function available_addon_callback( $args ) {
410 $addon = isset( $args['addon'] ) ? $args['addon'] : null;
411
412 if ( ! is_a( $addon, Base_Cookiebot_Addon::class ) ) {
413 throw new InvalidArgumentException();
414 }
415
416 $site_default_languages_dropdown_html = 'cookiebot_available_addons[' . $addon::OPTION_NAME . '][placeholder][languages][site-default]';
417 $addon_placeholders_array = $addon->get_placeholders();
418 $addon_placeholders_array_keys = array_keys( $addon_placeholders_array );
419 $first_placeholder_language = isset( $addon_placeholders_array_keys[0] )
420 ? $addon_placeholders_array_keys[0]
421 : null;
422 $addon_placeholders = array_map(
423 function( $language, $placeholder ) use ( $addon, $addon_placeholders_array, $first_placeholder_language ) {
424 $removable = $first_placeholder_language !== $language;
425 $option_name = 'cookiebot_available_addons[' . $addon::OPTION_NAME . '][placeholder][languages][' . $language . ']';
426 $languages_dropdown_html = cookiebot_addons_get_dropdown_languages(
427 'placeholder_select_language',
428 $option_name,
429 $language
430 );
431 return array(
432 'name' => $option_name,
433 'removable' => $removable,
434 'language' => $language,
435 'placeholder' => $placeholder,
436 'languages_dropdown_html' => $languages_dropdown_html,
437 );
438 },
439 $addon_placeholders_array_keys,
440 $addon_placeholders_array
441 );
442 $placeholder_helper = $addon->get_placeholder_helper();
443 $addon_extra_options_html = $addon->get_extra_addon_options_html();
444 $placeholders_html = $addon->has_placeholder()
445 ? get_view_html(
446 'admin/settings/prior-consent/partials/placeholder-submitboxes.php',
447 array(
448 'placeholders' => $addon_placeholders,
449 'placeholder_helper' => $placeholder_helper,
450 )
451 )
452 : get_view_html(
453 'admin/settings/prior-consent/partials/placeholder-submitbox-default.php',
454 array(
455 'site_default_languages_dropdown_html' => $site_default_languages_dropdown_html,
456 'name' => 'cookiebot_available_addons[' . $addon::OPTION_NAME . '][placeholder][languages][site-default]',
457 'default_placeholder' => $addon::DEFAULT_PLACEHOLDER_CONTENT,
458 'placeholder_helper' => $placeholder_helper,
459 )
460 );
461
462 $view_args = array(
463 'addon_option_name' => $addon::OPTION_NAME,
464 'addon_is_enabled' => $addon->is_addon_enabled(),
465 'addon_cookie_types' => $addon->get_cookie_types(),
466 'addon_placeholder_is_enabled' => $addon->is_placeholder_enabled(),
467 'placeholders_html' => $placeholders_html,
468 'addon_extra_options_html' => $addon_extra_options_html,
469 );
470
471 include_view( 'admin/settings/prior-consent/available-addons/tab.php', $view_args );
472 }
473
474 /**
475 * Returns header for unavailable plugins
476 *
477 * @throws InvalidArgumentException
478 * @since 1.3.0
479 */
480 public function unavailable_addons_header_callback() {
481 include_view( 'admin/settings/prior-consent/unavailable-addons/tab-header.php' );
482 }
483
484 /**
485 * @param $args
486 *
487 * @throws InvalidArgumentException
488 */
489 public function unavailable_addon_settings_field_callback( $args ) {
490 $addon = $args['addon'];
491
492 if ( ! is_a( $addon, Base_Cookiebot_Addon::class ) ) {
493 throw new InvalidArgumentException();
494 }
495
496 $message = '';
497 if ( ! $addon->is_addon_installed() ) {
498 if ( is_a( $addon, Base_Cookiebot_Plugin_Addon::class ) ) {
499 $message = __( 'The plugin is not installed.', 'cookiebot' );
500 }
501 if ( is_a( $addon, Base_Cookiebot_Theme_Addon::class ) ) {
502 $message = __( 'The theme is not installed.', 'cookiebot' );
503 }
504 } elseif ( ! $addon->is_addon_activated() ) {
505 if ( is_a( $addon, Base_Cookiebot_Plugin_Addon::class ) ) {
506 $message = __( 'The plugin is not activated.', 'cookiebot' );
507 }
508 if ( is_a( $addon, Base_Cookiebot_Theme_Addon::class ) ) {
509 $message = __( 'The theme is not activated.', 'cookiebot' );
510 }
511 }
512
513 $view_args = array(
514 'message' => $message,
515 );
516 include_view( 'admin/settings/prior-consent/unavailable-addons/field.php', $view_args );
517 }
518
519 /**
520 * Build up settings page
521 *
522 * @throws InvalidArgumentException
523 * @since 1.3.0
524 */
525 public function setting_page() {
526 $available_addons_tab = new Settings_Page_Tab(
527 'available_addons',
528 esc_html__( 'Available Addons', 'cookiebot' ),
529 'cookiebot_available_addons',
530 'cookiebot-addons'
531 );
532 $unavailable_addons_tab = new Settings_Page_Tab(
533 'unavailable_addons',
534 esc_html__( 'Unavailable Addons', 'cookiebot' ),
535 'cookiebot_not_installed_options',
536 'cookiebot-addons',
537 false
538 );
539 $settings_page_tabs = array(
540 $available_addons_tab,
541 $unavailable_addons_tab,
542 );
543 if ( is_plugin_active( Jetpack::PLUGIN_FILE_PATH ) ) {
544 $settings_page_tabs[] = new Settings_Page_Tab(
545 'jetpack',
546 esc_html__( 'Jetpack', 'cookiebot' ),
547 'cookiebot_jetpack_addon',
548 'cookiebot-addons'
549 );
550 }
551 $active_tab = array_reduce(
552 $settings_page_tabs,
553 function( $active_tab, Settings_Page_Tab $settings_page_tab ) {
554 if ( ! is_null( $active_tab ) ) {
555 return $active_tab;
556 }
557 if ( $settings_page_tab->is_active() ) {
558 return $settings_page_tab;
559 }
560 return null;
561 },
562 null
563 );
564 if ( ! $active_tab ) {
565 $available_addons_tab->set_is_active( true );
566 $active_tab = $available_addons_tab;
567 }
568 $view_args = array(
569 'settings_page_tabs' => $settings_page_tabs,
570 'active_tab' => $active_tab,
571 );
572 include_view( 'admin/settings/prior-consent/page.php', $view_args );
573 }
574
575 /**
576 * Post action hook after enabling the addon on the settings page.
577 *
578 * @param $old_value
579 * @param $value
580 * @param $option_name
581 *
582 * @throws Exception
583 * @since 2.2.0
584 */
585 public function post_hook_available_addons_update_option( $old_value, $value, $option_name ) {
586 if ( is_array( $value ) ) {
587 foreach ( $value as $addon_option_name => $addon_settings ) {
588 if ( isset( $addon_settings['enabled'] ) ) {
589 $this->settings_service->post_hook_after_enabling_addon_on_settings_page( $addon_option_name );
590 }
591 }
592 }
593 }
594 }
595