PluginProbe
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 3.10.1
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v3.10.1
4.7.4 4.7.5 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 All 95 releases
cookiebot / addons / controller / addons / add-to-any / add-to-any.php

add-to-any.php in Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode 3.10.1, at addons/controller/addons/add-to-any/add-to-any.php

387 lines 8.5 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\add_to_any;
4
5 use DOMDocument;
6 use DOMXPath;
7 use DOMElement;
8 use cookiebot_addons\controller\addons\Cookiebot_Addons_Interface;
9 use cookiebot_addons\lib\Cookie_Consent_Interface;
10 use cookiebot_addons\lib\Settings_Service_Interface;
11 use cookiebot_addons\lib\script_loader_tag\Script_Loader_Tag_Interface;
12 use cookiebot_addons\lib\buffer\Buffer_Output_Interface;
13
14 class Add_To_Any implements Cookiebot_Addons_Interface {
15
16 /**
17 * @var Settings_Service_Interface
18 *
19 * @since 1.3.0
20 */
21 protected $settings;
22
23 /**
24 * @var Script_Loader_Tag_Interface
25 *
26 * @since 1.3.0
27 */
28 protected $script_loader_tag;
29
30 /**
31 * @var Cookie_Consent_Interface
32 *
33 * @since 1.3.0
34 */
35 public $cookie_consent;
36
37 /**
38 * @var Buffer_Output_Interface
39 *
40 * @since 1.3.0
41 */
42 protected $buffer_output;
43
44 /**
45 * Jetpack constructor.
46 *
47 * @param $settings Settings_Service_Interface
48 * @param $script_loader_tag Script_Loader_Tag_Interface
49 * @param $cookie_consent Cookie_Consent_Interface
50 * @param $buffer_output Buffer_Output_Interface
51 *
52 * @since 1.3.0
53 */
54 public function __construct( Settings_Service_Interface $settings, Script_Loader_Tag_Interface $script_loader_tag, Cookie_Consent_Interface $cookie_consent, Buffer_Output_Interface $buffer_output ) {
55 $this->settings = $settings;
56 $this->script_loader_tag = $script_loader_tag;
57 $this->cookie_consent = $cookie_consent;
58 $this->buffer_output = $buffer_output;
59 }
60
61 /**
62 * Loads addon configuration
63 *
64 * @since 1.3.0
65 */
66 public function load_configuration() {
67 add_action( 'wp_loaded', array( $this, 'cookiebot_addon_add_to_any' ), 5 );
68 }
69
70 /**
71 * Disable scripts if state not accepted
72 *
73 * @since 1.3.0
74 */
75 public function cookiebot_addon_add_to_any() {
76
77 $this->buffer_output->add_tag( 'wp_head', 10, array(
78 'a2a_config' => $this->get_cookie_types()
79 ), false );
80
81 $this->buffer_output->add_tag( 'wp_footer', 10, array(
82 'a2a_config' => $this->get_cookie_types()
83 ), false );
84
85 $this->buffer_output->add_tag( 'pre_get_posts', 10, array(
86 'GoogleAnalyticsObject' => $this->get_cookie_types(),
87 ), false );
88
89 // External js, so manipulate attributes
90 if ( has_action( 'wp_enqueue_scripts', 'A2A_SHARE_SAVE_enqueue_script' ) ) {
91 $this->script_loader_tag->add_tag( 'addtoany', $this->get_cookie_types() );
92 }
93
94 add_filter( 'the_content', array(
95 $this,
96 'cookiebot_addon_add_to_any_content',
97 ), 1000 ); //Ensure it is executed as the last filter
98
99 add_filter( 'the_excerpt', array(
100 $this,
101 'cookiebot_addon_add_to_any_content',
102 ), 1000 ); //Ensure it is executed as the last filter
103 }
104
105 /**
106 * Display a placeholder on elements with "addtoany_share_save_container" class name.
107 *
108 * @param string $content
109 *
110 * @return string
111 */
112 public function cookiebot_addon_add_to_any_content( $content ) {
113 if ( ! class_exists( 'DOMDocument' ) || ! class_exists( 'DOMXPath' ) || ! class_exists( 'DOMElement' ) ) {
114 return $content;
115 }
116
117 if ( $this->has_placeholder() && $this->is_placeholder_enabled() ) {
118 $dom = new DOMDocument();
119 $dom->loadHTML( $content );
120 $finder = new DOMXPath( $dom );
121 $nodes = $finder->query( "//*[contains(@class, 'addtoany_share_save_container')]" );
122
123 $placeholder = $this->get_placeholder();
124 $placeholder_element = $dom->createDocumentFragment();
125 $placeholder_element->appendXML( '<div class="' . cookiebot_addons_cookieconsent_optout( $this->get_cookie_types() ) . '">' . $placeholder . '</div>' );
126
127 foreach ( $nodes as $node ) {
128 /* @var DOMElement $node */
129 $node->appendChild( $placeholder_element );
130 }
131
132 $content = $dom->saveHTML();
133 }
134
135 return $content;
136 }
137
138 /**
139 * Return addon/plugin name
140 *
141 * @return string
142 *
143 * @since 1.3.0
144 */
145 public function get_addon_name() {
146 return 'addToAny Share Buttons';
147 }
148
149 /**
150 * Default placeholder content
151 *
152 * @return string
153 *
154 * @since 1.8.0
155 */
156 public function get_default_placeholder() {
157 return 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable Social Share buttons.';
158 }
159
160 /**
161 * Get placeholder content
162 *
163 * This function will check following features:
164 * - Current language
165 *
166 * @param $src
167 *
168 * @return bool|mixed
169 *
170 * @since 1.8.0
171 */
172 public function get_placeholder( $src = '' ) {
173 return $this->settings->get_placeholder( $this->get_option_name(), $this->get_default_placeholder(), cookiebot_addons_output_cookie_types( $this->get_cookie_types() ), $src );
174 }
175
176 /**
177 * Option name in the database
178 *
179 * @return string
180 *
181 * @since 1.3.0
182 */
183 public function get_option_name() {
184 return 'add_to_any';
185 }
186
187 /**
188 * Plugin file name
189 *
190 * @return string
191 *
192 * @since 1.3.0
193 */
194 public function get_plugin_file() {
195 return 'add-to-any/add-to-any.php';
196 }
197
198 /**
199 * Returns checked cookie types
200 * @return mixed
201 *
202 * @since 1.3.0
203 */
204 public function get_cookie_types() {
205 return $this->settings->get_cookie_types( $this->get_option_name(), $this->get_default_cookie_types() );
206 }
207
208 /**
209 * Returns default cookie types
210 * @return array
211 *
212 * @since 1.5.0
213 */
214 public function get_default_cookie_types() {
215 return array( 'marketing', 'statistics' );
216 }
217
218 /**
219 * Check if plugin is activated and checked in the backend
220 *
221 * @since 1.3.0
222 */
223 public function is_addon_enabled() {
224 return $this->settings->is_addon_enabled( $this->get_option_name() );
225 }
226
227 /**
228 * Checks if addon is installed
229 *
230 * @since 1.3.0
231 */
232 public function is_addon_installed() {
233 return $this->settings->is_addon_installed( $this->get_plugin_file() );
234 }
235
236 /**
237 * Checks if addon is activated
238 *
239 * @since 1.3.0
240 */
241 public function is_addon_activated() {
242 return $this->settings->is_addon_activated( $this->get_plugin_file() );
243 }
244
245 /**
246 * Retrieves current installed version of the addon
247 *
248 * @return bool
249 *
250 * @since 2.2.1
251 */
252 public function get_addon_version() {
253 return $this->settings->get_addon_version( $this->get_plugin_file() );
254 }
255
256 /**
257 * Checks if it does have custom placeholder content
258 *
259 * @return mixed
260 *
261 * @since 1.8.0
262 */
263 public function has_placeholder() {
264 return $this->settings->has_placeholder( $this->get_option_name() );
265 }
266
267 /**
268 * returns all placeholder contents
269 *
270 * @return mixed
271 *
272 * @since 1.8.0
273 */
274 public function get_placeholders() {
275 return $this->settings->get_placeholders( $this->get_option_name() );
276 }
277
278 /**
279 * Return true if the placeholder is enabled
280 *
281 * @return mixed
282 *
283 * @since 1.8.0
284 */
285 public function is_placeholder_enabled() {
286 return $this->settings->is_placeholder_enabled( $this->get_option_name() );
287 }
288
289 /**
290 * Adds extra information under the label
291 *
292 * @return string
293 *
294 * @since 1.8.0
295 */
296 public function get_extra_information() {
297 return '<p>' . esc_html__( 'Blocks embedded videos from Youtube, Twitter, Vimeo and Facebook.', 'cookiebot-addons' ) . '</p>';
298 }
299
300 /**
301 * Returns the url of WordPress SVN repository or another link where we can verify the plugin file.
302 *
303 * @return string
304 *
305 * @since 1.8.0
306 */
307 public function get_svn_url() {
308 return 'http://plugins.svn.wordpress.org/add-to-any/trunk/add-to-any.php';
309 }
310
311 /**
312 * Placeholder helper overlay in the settings page.
313 *
314 * @return string
315 *
316 * @since 1.8.0
317 */
318 public function get_placeholder_helper() {
319 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>';
320 }
321
322
323 /**
324 * Returns parent class or false
325 *
326 * @return string|bool
327 *
328 * @since 2.1.3
329 */
330 public function get_parent_class() {
331 return get_parent_class( $this );
332 }
333
334 /**
335 * Action after enabling the addon on the settings page
336 *
337 * @since 2.2.0
338 */
339 public function post_hook_after_enabling() {
340 //do nothing
341 }
342
343 /**
344 * Cookiebot plugin is deactivated
345 *
346 * @since 2.2.0
347 */
348 public function plugin_deactivated() {
349 //do nothing
350 }
351
352 /**
353 * @return mixed
354 *
355 * @since 2.4.5
356 */
357 public function extra_available_addon_option() {
358 //do nothing
359 }
360
361 /**
362 * Returns boolean to enable/disable plugin by default
363 *
364 * @return bool
365 *
366 * @since 3.6.3
367 */
368 public function enable_by_default() {
369 return false;
370 }
371
372 /**
373 * Sets default settings for this addon
374 *
375 * @return array
376 *
377 * @since 3.6.3
378 */
379 public function get_default_enable_setting() {
380 return array(
381 'enabled' => 1,
382 'cookie_type' => $this->get_default_cookie_types(),
383 'placeholder' => $this->get_default_placeholder(),
384 );
385 }
386 }
387