PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.3.12
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.3.12
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 / admin_notices / Cookiebot_Base_Notice.php
cookiebot / src / admin_notices Last commit date
Cookiebot_Base_Notice.php 1 year ago Cookiebot_Notices.php 1 year ago Cookiebot_Recommendation_Notice.php 2 years ago Cookiebot_Temp_Notice.php 2 years ago
Cookiebot_Base_Notice.php
175 lines
1 <?php
2
3 namespace cybot\cookiebot\admin_notices;
4
5 use Exception;
6 use cybot\cookiebot\lib\Cookiebot_WP;
7 use InvalidArgumentException;
8 use LogicException;
9 use function cybot\cookiebot\lib\asset_url;
10 use function cybot\cookiebot\lib\include_view;
11
12 class Cookiebot_Base_Notice {
13 const COOKIEBOT_NOTICE_OPTION_KEY = '';
14 const COOKIEBOT_NOTICE_TEMPLATE_PATH = '';
15
16 const COOKIEBOT_NOTICE_TIMES = array();
17 const COOKIEBOT_NOTICE_TIMES_REVERT = false;
18
19 public $translations = array();
20 public $hide_condition = false;
21
22 public function __construct() {
23 $this->define_translations();
24 $this->define_conditions();
25 }
26
27 public function register_hooks() {
28 add_action( 'admin_notices', array( $this, 'show_notice_if_needed' ) );
29 }
30
31 /**
32 * Set translations
33 *
34 * @version 4.3.9
35 * @since 4.3.9
36 */
37 public function define_translations() {
38 $this->translations = array(
39 'title' => '',
40 'msg' => '',
41 );
42 }
43
44 /**
45 * Set special conditions if needed
46 *
47 * @version 4.4.0
48 * @since 4.4.0
49 */
50 public function define_conditions() {
51 $this->hide_condition = false;
52 }
53
54 /**
55 * Save the user action on cookiebot recommendation link
56 *
57 * @version 4.3.9
58 * @since 2.0.5
59 */
60 private function save_notice_link() {
61 if ( isset( $_GET[ static::COOKIEBOT_NOTICE_OPTION_KEY ] ) ) {
62 foreach ( static::COOKIEBOT_NOTICE_TIMES as $item ) {
63 if ( isset( $_GET[ $item['name'] ] ) &&
64 wp_verify_nonce( $_GET[ $item['name'] ], $item['action'] ) ) {
65 $option = $item['time'];
66 if ( isset( $item['str'] ) && $item['str'] === true ) {
67 $option = strtotime( $item['time'] );
68 }
69 update_option( static::COOKIEBOT_NOTICE_OPTION_KEY, $option );
70 }
71 }
72 }
73 }
74
75 /**
76 * Validate if the last user action is valid for plugin recommendation
77 *
78 * @throws Exception
79 *
80 * @version 4.3.9
81 * @since 2.0.5
82 */
83 private function do_we_need_to_show_the_notice_message() {
84 $option = get_option( static::COOKIEBOT_NOTICE_OPTION_KEY );
85
86 if ( $option !== false ) {
87 if ( $this->hide_condition !== false ) {
88 throw new LogicException( 'Hidden' );
89 }
90 // "Never show again" is clicked
91 if ( array_key_exists( $option, static::COOKIEBOT_NOTICE_TIMES ) ) {
92 throw new LogicException( static::COOKIEBOT_NOTICE_TIMES[ $option ]['msg'] );
93 } elseif ( is_numeric( $option ) ) {
94 if ( ! self::notice_check_option_date( $option ) ) {
95 throw new LogicException( 'Show me after some time' );
96 }
97 }
98 }
99 }
100
101 private function notice_check_option_date( $option ) {
102 if ( ( strtotime( 'now' ) < $option && static::COOKIEBOT_NOTICE_TIMES_REVERT ) ||
103 ( strtotime( 'now' ) > $option && ! static::COOKIEBOT_NOTICE_TIMES_REVERT ) ) {
104 return true;
105 }
106
107 return false;
108 }
109
110 /**
111 * Return html for the link html parameter
112 *
113 * @version 4.3.9
114 * @since 4.3.9
115 */
116 public function get_link_html() {
117 return '';
118 }
119
120 /**
121 * Include notice on page
122 *
123 * @version 4.3.9
124 * @since 4.3.9
125 * @throws InvalidArgumentException
126 */
127 private function show_notice() {
128 include_view(
129 static::COOKIEBOT_NOTICE_TEMPLATE_PATH,
130 array(
131 'notice' => array(
132 'title' => $this->translations['title'],
133 'msg' => $this->translations['msg'],
134 'link_html' => $this->get_link_html(),
135 'later_link' => wp_nonce_url(
136 add_query_arg(
137 array(
138 static::COOKIEBOT_NOTICE_OPTION_KEY => static::COOKIEBOT_NOTICE_TIMES['later']['time'],
139 )
140 ),
141 static::COOKIEBOT_NOTICE_TIMES['later']['action'],
142 static::COOKIEBOT_NOTICE_TIMES['later']['name']
143 ),
144 ),
145 )
146 );
147
148 wp_enqueue_style(
149 'cookiebot-admin-notices',
150 asset_url( 'css/notice.css' ),
151 null,
152 Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION
153 );
154 }
155
156 /**
157 * Check if the notice needs to be shown
158 *
159 * @version 4.3.9
160 * @since 4.3.9
161 */
162 public function show_notice_if_needed() {
163 // Save actions when someone click on the notice message
164 $this->save_notice_link();
165
166 try {
167 $this->do_we_need_to_show_the_notice_message();
168 $this->show_notice();
169 // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
170 } catch ( Exception $e ) {
171 // If exception has been thrown, then we don't need to show the notice.
172 }
173 }
174 }
175