PluginProbe
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 3.1.0
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v3.1.0
4.7.3 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 All 93 releases
cookiebot / addons / controller / addons / jetpack / widget / goodreads-widget.php

goodreads-widget.php in Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode 3.1.0, at addons/controller/addons/jetpack/widget/goodreads-widget.php

304 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace cookiebot_addons\controller\addons\jetpack\widget;
4
5 use cookiebot_addons\lib\Settings_Service_Interface;
6 use cookiebot_addons\lib\script_loader_tag\Script_Loader_Tag_Interface;
7 use cookiebot_addons\lib\Cookie_Consent_Interface;
8 use cookiebot_addons\lib\buffer\Buffer_Output_Interface;
9
10 class Goodreads_Widget implements Jetpack_Widget_Interface {
11
12 protected $widget_id;
13
14 protected $transient_name;
15
16 protected $keywords;
17
18 /**
19 * @var array list of supported cookie types
20 *
21 * @since 1.3.0
22 */
23 protected $cookie_types;
24
25 /**
26 * @var Settings_Service_Interface
27 *
28 * @since 1.3.0
29 */
30 protected $settings;
31
32 /**
33 * @var Script_Loader_Tag_Interface
34 *
35 * @since 1.3.0
36 */
37 protected $script_loader_tag;
38
39 /**
40 * @var Cookie_Consent_Interface
41 *
42 * @since 1.3.0
43 */
44 protected $cookie_consent;
45
46 /**
47 * @var Buffer_Output_Interface
48 *
49 * @since 1.3.0
50 */
51 protected $buffer_output;
52
53 /**
54 * Option name for jetpack addon
55 *
56 * @var string
57 */
58 public $widget_option;
59
60 /**
61 * Goodreads_Widget constructor.
62 *
63 * @param Settings_Service_Interface $settings
64 * @param Script_Loader_Tag_Interface $script_loader_tag
65 * @param Cookie_Consent_Interface $cookie_consent
66 * @param Buffer_Output_Interface $buffer_output
67 * @param $widget_option
68 *
69 * @version 1.8.0
70 * @since 1.3.0
71 */
72 public function __construct( Settings_Service_Interface $settings, Script_Loader_Tag_Interface $script_loader_tag, Cookie_Consent_Interface $cookie_consent, Buffer_Output_Interface $buffer_output, $widget_option ) {
73 $this->settings = $settings;
74 $this->script_loader_tag = $script_loader_tag;
75 $this->cookie_consent = $cookie_consent;
76 $this->buffer_output = $buffer_output;
77 $this->widget_option = $widget_option;
78 }
79
80 public function load_configuration() {
81 /**
82 * The widget is active
83 */
84 if ( is_active_widget( false, false, 'wpcom-goodreads', true ) ) {
85 /**
86 * The widget is enabled in Prior consent
87 */
88 if ( $this->is_widget_enabled() ) {
89 /**
90 * The visitor didn't check the required cookie types
91 */
92 if ( ! $this->cookie_consent->are_cookie_states_accepted( $this->get_widget_cookie_types() ) ) {
93
94 if ( $this->is_widget_placeholder_enabled() ) {
95 add_action( 'jetpack_stats_extra', array( $this, 'cookie_consent_div' ), 10, 2 );
96 }
97
98 $this->transient_name = 'wpcom-goodreads';
99
100 $this->keywords = array( 'www.goodreads.com' => $this->get_widget_cookie_types() );
101 $this->block_javascript_file();
102 $this->output_manipulated();
103 }
104 }
105 }
106 }
107
108 public function get_label() {
109 return 'Goodreads';
110 }
111
112 /**
113 * Returns widget option name
114 *
115 * @return string
116 *
117 * @since 1.8.0
118 */
119 public function get_widget_option_name() {
120 return 'goodreads';
121 }
122
123 /**
124 * Returns cookie types for a widget
125 *
126 * @return mixed
127 *
128 * @since 1.8.0
129 */
130 public function get_widget_cookie_types() {
131 return $this->settings->get_widget_cookie_types( $this->widget_option, $this->get_widget_option_name() );
132 }
133
134 /**
135 * Checks if a widget is enabled
136 *
137 * @return mixed
138 *
139 * @since 1.8.0
140 */
141 public function is_widget_enabled() {
142 return $this->settings->is_widget_enabled( $this->widget_option, $this->get_widget_option_name() );
143 }
144
145 /**
146 * Checks if widget has existing placeholders
147 *
148 * @return mixed
149 *
150 * @since 1.8.0
151 */
152 public function widget_has_placeholder() {
153 return $this->settings->widget_has_placeholder( $this->widget_option, $this->get_widget_option_name() );
154 }
155
156 /**
157 * @return string
158 */
159 public function get_default_placeholder() {
160 return 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to watch goodreads.';
161 }
162
163 /**
164 * Returns all widget placeholders
165 *
166 * @return mixed
167 *
168 * @since 1.8.0
169 */
170 public function get_widget_placeholders() {
171 return $this->settings->get_widget_placeholders( $this->widget_option, $this->get_widget_option_name() );
172 }
173
174 /**
175 * Checks if a widget placeholder is enabled
176 *
177 * @return boolean true If widget placeholder is checked
178 * false If widget placeholder is not checked
179 *
180 * @since 1.8.0
181 */
182 public function is_widget_placeholder_enabled() {
183 return $this->settings->is_widget_placeholder_enabled( $this->widget_option, $this->get_widget_option_name() );
184 }
185
186 /**
187 * returns widget placeholder
188 *
189 * @return mixed
190 *
191 * @since 1.8.0
192 */
193 public function get_widget_placeholder() {
194 return $this->settings->get_widget_placeholder( $this->widget_option, $this->get_widget_option_name(), $this->get_default_placeholder(), cookiebot_addons_output_cookie_types( $this->get_widget_cookie_types() ) );
195 }
196
197 /**
198 * Add message to go to consent settings when marketing consent is not accepted
199 *
200 * @since 1.2.0
201 */
202 protected function block_javascript_file() {
203 add_action( 'dynamic_sidebar', array( $this, 'display_div_message_to_go_to_consent_settings' ), 10, 1 );
204 }
205
206 /**
207 * Show a messsage to go to consent settings
208 *
209 * @param $widget string
210 *
211 * @since 1.2.0
212 */
213 public function display_div_message_to_go_to_consent_settings( $widget ) {
214 $callback = $widget['callback'][0];
215
216 if ( $callback->id_base == 'wpcom-goodreads' ) {
217 ob_start( array( $this, 'manipulate_script' ) );
218 }
219 }
220
221 /**
222 * Return widget output after dynamic sidebar is fully processed
223 *
224 * @since 1.2.0
225 */
226 public function output_manipulated() {
227 add_action( 'dynamic_sidebar_after', function ( $index ) {
228 ob_end_flush();
229 } );
230 }
231
232 /**
233 * Custom manipulation of the script
234 *
235 * @param $buffer
236 *
237 * @return mixed|null|string|string[]
238 *
239 * @since 1.2.0
240 */
241 public function manipulate_script( $buffer ) {
242 /**
243 * Get wp head scripts from the cache
244 */
245 $updated_scripts = get_transient( $this->transient_name );
246
247 /**
248 * If cache is not set then build it
249 */
250 if ( $updated_scripts === false ) {
251
252 $updated_scripts = cookiebot_addons_manipulate_script( $buffer, $this->keywords );
253
254 /**
255 * Set cache for 15 minutes
256 */
257 set_transient( $this->transient_name, $updated_scripts, 60 * 15 );
258 }
259
260 return $updated_scripts;
261 }
262
263 /**
264 * Show consent message when the consent is not given.
265 *
266 * @param $view string
267 * @param $widget string
268 *
269 * @since 1.6.0
270 */
271 public function cookie_consent_div( $view, $widget ) {
272 if ( $widget == 'goodreads' && $view == 'widget_view' ) {
273 if ( is_array( $this->get_widget_cookie_types() ) && count( $this->get_widget_cookie_types() ) > 0 ) {
274 echo '<div class="cookieconsent-optout-' . cookiebot_addons_get_one_cookie_type( $this->get_widget_cookie_types() ) . '">
275 ' . $this->get_widget_placeholder() . '
276 </div>';
277 }
278 }
279 }
280
281 /**
282 * Adds extra information under the label
283 *
284 * @return string
285 *
286 * @since 1.8.0
287 */
288 public function get_extra_information() {
289 return false;
290 }
291
292 /**
293 * Placeholder helper overlay in the settings page.
294 *
295 * @return string
296 *
297 * @since 1.8.0
298 */
299 public function get_placeholder_helper() {
300 return '<p>Merge tags you can use in the placeholder text:</p><ul><li>%cookie_types - Lists required cookie types</li><li>[renew_consent]text[/renew_consent] - link to display cookie settings in frontend</li></ul>';
301 }
302
303 }
304