|
<?php //do not copy |
|
|
|
// Check whether the user has granted Cookiebot consent for cookies beyond necessary. |
|
function my_pmpro_determine_if_set_cookie() { |
|
if ( ! isset( $_COOKIE['CookieConsent'] ) ) { |
|
// User hasn't made a choice — only strictly necessary cookies allowed. |
|
return false; |
|
} |
|
|
|
$valid_php_json = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"',stripslashes($_COOKIE["CookieConsent"])))); |
|
|
|
$CookieConsent = json_decode( $valid_php_json ); |
|
|
|
// Handle malformed or empty cookies. |
|
if ( ! $CookieConsent ) { |
|
return false; |
|
} |
|
// Allow cookies if user accepted any of these categories. |
|
return ( |
|
( isset( $CookieConsent->preferences ) && $CookieConsent->preferences !== 'false' ) || |
|
( isset( $CookieConsent->statistics ) && $CookieConsent->statistics !== 'false' ) || |
|
( isset( $CookieConsent->marketing ) && $CookieConsent->marketing !== 'false' ) |
|
); |
|
} |
|
|
|
/** |
|
* Register PMPro tracking hooks only if Cookiebot consent allows it. |
|
*/ |
|
function my_pmpro_remove_tracking() { |
|
remove_action( 'wp', 'pmpro_report_login_wp_visits' ); // Visits |
|
remove_action( 'wp_head', 'pmpro_report_login_wp_views' ); // Views |
|
remove_action( 'wp_login', 'pmpro_report_login_wp_login', 10, 2 ); // Logins |
|
} |
|
add_action( 'init', 'my_pmpro_remove_tracking', 1 ); |
|
|
|
function my_pmpro_register_tracking_hooks() { |
|
if ( my_pmpro_determine_if_set_cookie() ) { |
|
add_action( 'wp', 'pmpro_report_login_wp_visits' ); // Visits |
|
add_action( 'wp_head', 'pmpro_report_login_wp_views' ); // Views |
|
add_action( 'wp_login', 'pmpro_report_login_wp_login', 10, 2 ); // Logins |
|
} |
|
} |
|
add_action( 'init', 'my_pmpro_register_tracking_hooks', 99 ); |