forked from Really-Simple-Plugins/wp-consent-level-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-comments.php
47 lines (37 loc) · 1.5 KB
/
wordpress-comments.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
<?php // phpcs:ignore -- Ignore the "\r\n" notice for some machines.
/**
* Instead of listening to the consent checkbox, this function checks for the "preferences" consent
*
* The consent checkbox is removed
* if preferences is set, the comments cookies function gets passed a $cookies_consent=true, so cookies can be set. Otherwise, it's false.
*/
/**
* First we remove the default comment cookies action, and replace with our own
* we add custom comment cookies action, which checks the consent, then calls the comment cookie function in wp
*/
function wp_consent_api_wordpress_comments_cookies() {
// Remove default wp action.
remove_action( 'set_comment_cookies', 'wp_set_comment_cookies', 10 );
// Add our own custom action.
add_action( 'set_comment_cookies', 'wp_consent_api_set_comment_cookies', 10, 3 );
// Remove checkbox.
add_filter( 'comment_form_default_fields', 'wp_consent_api_wordpress_comment_form_hide_cookies_consent' );
}
add_action( 'init', 'wp_consent_api_wordpress_comments_cookies' );
/**
* Custom consent function, checking consent level to decide if cookies can be set.
*/
function wp_consent_api_set_comment_cookies( $comment, $user, $cookies_consent ) {
$cookies_consent = wp_has_consent( 'preference' );
wp_set_comment_cookies( $comment, $user, $cookies_consent );
}
/**
* Remove consent checkbox
* @param $fields
*
* @return array $fields
*/
function wp_consent_api_wordpress_comment_form_hide_cookies_consent( $fields ) {
unset( $fields['cookies'] );
return $fields;
}