GDPR icon indicating copy to clipboard operation
GDPR copied to clipboard

Helper function is_allowed_cookie() does inexact match

Open wizzud opened this issue 7 years ago • 1 comments

Change function to (something along the lines of) ...

function is_allowed_cookie( $cookie_name, $exactMatch = false ) {
	if ( isset( $_COOKIE['gdpr']['allowed_cookies'] ) ) {
		$allowed_cookies = array_map( 
			'sanitize_text_field',
			json_decode(
				wp_unslash( $_COOKIE['gdpr']['allowed_cookies'] ),
				true
			)
		);
		if ( in_array( $cookie_name, $allowed_cookies, true ) ) {
			return true;
		}
		if ( ! $exactMatch ) {
			$name = preg_quote( $cookie_name, '~' );
			$result = preg_grep( '~' . $name . '~', $allowed_cookies );
			return ! empty( $result );
		}
	}

	return false;
}

Note : the above is suggested without reference/regard to any other Issue raised, and is merely - without changing the functionality of existing calls to the helper - a way for developers to be able to determine an exact match for, say, '_ga' (by calling is_allowed_cookie('_ga', true);), and not risk a false match against 'online_gaming' (a made-up 'for instance'!).

wizzud avatar Jun 04 '18 14:06 wizzud

That's a good suggestion. Can you submit this as a pull request?

For your other suggestion for checking for categories, I will add as a new function so we don't break thousands of sites when changing from cookie to category based checks.

fclaussen avatar Jun 04 '18 15:06 fclaussen