| 1 |
<?php |
| 2 |
/** |
| 3 |
* The plugin helper functions |
| 4 |
* |
| 5 |
* @link https://trewknowledge.com |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package GDPR |
| 9 |
* @subpackage public |
| 10 |
* @author Fernando Claussen <fernandoclaussen@gmail.com> |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Adds a button to re-open the cookie preferences modal. |
| 15 |
* @since 1.0.0 |
| 16 |
* @author Fernando Claussen <fernandoclaussen@gmail.com> |
| 17 |
* @param string $text The button text. |
| 18 |
* @param string $type The type of preferences. Possible options are `cookies` or `consents` |
| 19 |
*/ |
| 20 |
function gdpr_preferences( $text ) { |
| 21 |
echo '<button type="button" class="gdpr-preferences">' . esc_html( $text ) . '</button>'; |
| 22 |
} |
| 23 |
|
| 24 |
function gdpr_preferences_shortcode( $atts ) { |
| 25 |
$atts = shortcode_atts( array( |
| 26 |
'text' => esc_html__( 'Privacy Preferences', 'gdpr' ), |
| 27 |
), $atts, 'gdpr_preferences' ); |
| 28 |
|
| 29 |
ob_start(); |
| 30 |
gdpr_preferences( $atts['text'] ); |
| 31 |
return ob_get_clean(); |
| 32 |
} |
| 33 |
|
| 34 |
add_shortcode( 'gdpr_preferences', 'gdpr_preferences_shortcode' ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Load the request forms. |
| 38 |
* @since 1.0.0 |
| 39 |
* @author Fernando Claussen <fernandoclaussen@gmail.com> |
| 40 |
* @param string $type The type of request. |
| 41 |
*/ |
| 42 |
function gdpr_request_form( $type ) { |
| 43 |
echo GDPR_Requests_Public::request_form( $type ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Create the request form shortcode. |
| 48 |
* @since 1.0.0 |
| 49 |
* @author Fernando Claussen <fernandoclaussen@gmail.com> |
| 50 |
* @param string $atts Shortcode attributes. |
| 51 |
*/ |
| 52 |
function gdpr_request_form_shortcode( $atts ) { |
| 53 |
$atts = shortcode_atts( array( |
| 54 |
'type' => '', |
| 55 |
), $atts, 'gdpr_request_form' ); |
| 56 |
|
| 57 |
return GDPR_Requests_Public::request_form( $atts['type'] ); |
| 58 |
} |
| 59 |
|
| 60 |
add_shortcode( 'gdpr_request_form', 'gdpr_request_form_shortcode' ); |
| 61 |
|
| 62 |
function gdpr_get_consent_checkboxes( $atts ) { |
| 63 |
$atts = shortcode_atts( array( |
| 64 |
'id' => false, |
| 65 |
), $atts, 'gdpr_consent_checkboxes' ); |
| 66 |
|
| 67 |
return GDPR::get_consent_checkboxes( $atts['id'] ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Checks if a cookie is allowed |
| 72 |
* @since 1.0.0 |
| 73 |
* @author Fernando Claussen <fernandoclaussen@gmail.com> |
| 74 |
* @param string $cookie_name The cookie name. |
| 75 |
* @return bool Whether the cookie is allowed or not. |
| 76 |
*/ |
| 77 |
function is_allowed_cookie( $cookie_name ) { |
| 78 |
if ( isset( $_COOKIE['gdpr']['allowed_cookies'] ) ) { |
| 79 |
$allowed_cookies = json_decode( wp_unslash( $_COOKIE['gdpr']['allowed_cookies'] ), true ); |
| 80 |
$name = preg_quote( $cookie_name, '~' ); |
| 81 |
$result = preg_grep( '~' . $name . '~', $allowed_cookies ); |
| 82 |
if ( in_array( $cookie_name, $allowed_cookies ) || ! empty( $result ) ) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
function gdpr_deprecated_function( $function, $version, $replacement = null ) { |
| 91 |
if ( defined( 'DOING_AJAX' ) ) { |
| 92 |
do_action( 'deprecated_function_run', $function, $replacement, $version ); |
| 93 |
$log_string = "The {$function} function is deprecated since version {$version}."; |
| 94 |
$log_string .= $replacement ? " Replace with {$replacement}." : ''; |
| 95 |
} else { |
| 96 |
_deprecated_function( $function, $version, $replacement ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Checks if a user gave consent. |
| 102 |
* @since 1.0.0 |
| 103 |
* @author Fernando Claussen <fernandoclaussen@gmail.com> |
| 104 |
* @param string $consent The consent id. |
| 105 |
* @return bool Whether the user gave consent or not. |
| 106 |
*/ |
| 107 |
function have_consent( $consent ) { |
| 108 |
gdpr_deprecated_function( 'have_consent', '1.1.0', 'has_consent' ); |
| 109 |
return has_consent( $consent ); |
| 110 |
} |
| 111 |
|
| 112 |
function has_consent( $consent ) { |
| 113 |
|
| 114 |
if ( is_user_logged_in() ) { |
| 115 |
$user = wp_get_current_user(); |
| 116 |
$consents = (array) get_user_meta( $user->ID, 'gdpr_consents' ); |
| 117 |
} else if ( isset( $_COOKIE['gdpr']['consent_types'] ) && ! empty( $_COOKIE['gdpr']['consent_types'] ) ) { |
| 118 |
$consents = array_map( 'sanitize_text_field', (array) json_decode( wp_unslash( $_COOKIE['gdpr']['consent_types'] ) ) ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( isset( $consents ) && ! empty( $consents ) ) { |
| 122 |
if ( in_array( $consent, $consents ) ) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
function is_dnt() { |
| 131 |
return ( isset( $_SERVER['HTTP_DNT'] ) && $_SERVER['HTTP_DNT'] === '1' ); |
| 132 |
} |
| 133 |
|