|
<?php // copy from below. |
|
/** |
|
* Disable pmpro_visit cookie if the user has not consented to Analytics cookies |
|
* via the Pressidium Cookie Consent plugin. |
|
* |
|
* Pressidium Cookie Consent: https://wordpress.org/plugins/pressidium-cookie-consent/ |
|
* |
|
* Add pmpro_visit to Cookie Consent > Cookies > Analytics cookies: |
|
* https://github.com/pressidium/pressidium-cookie-consent/wiki/Configuration#cookies |
|
* |
|
* You can add this recipe to your site by creating a custom plugin |
|
* or using the Code Snippets plugin available for free in the WordPress repository. |
|
* Read this companion article for step-by-step directions on either method. |
|
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ |
|
*/ |
|
|
|
/** |
|
* Function to check whether the user has granted Pressidium Cookie Consent consent for Analytics cookies. |
|
*/ |
|
function my_pressidium_analytics_cookie_consent_granted() { |
|
if ( ! isset( $_COOKIE['pressidium_cookie_consent'] ) ) { |
|
// User hasn't made a choice — cookies not allowed. |
|
return false; |
|
} |
|
|
|
// Convert the JSON string stored in the cookie to a PHP object. |
|
$pressidium_cookie_consent = json_decode( sanitize_text_field( wp_unslash( $_COOKIE['pressidium_cookie_consent'] ) ) ); |
|
|
|
// Return false if consent data is unavailable. |
|
if ( ! isset( $pressidium_cookie_consent->level ) || ! is_array( $pressidium_cookie_consent->level ) ) { |
|
return false; |
|
} |
|
|
|
// Return false if 'analytics' isn't in the array of accepted cookie categories. |
|
if ( ! in_array( 'analytics', $pressidium_cookie_consent->level ) ) { |
|
return false; |
|
} |
|
|
|
// User has accepted analytics cookies. |
|
return true; |
|
} |
|
|
|
/** |
|
* Register PMPro tracking hooks only if Pressidium Cookie Consent allows analytics cookies. |
|
*/ |
|
function my_pmpro_register_tracking_hooks() { |
|
if ( ! my_pressidium_analytics_cookie_consent_granted() ) { |
|
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_register_tracking_hooks', 20 ); |