?])#', $input, $m ) ) { return $m[1]; } return ''; } /** * The script URL to print, or '' when the plugin is not set up yet. * * @return string */ function cookieconsenthq_script_url() { $key = cookieconsenthq_parse_site_key( get_option( COOKIECONSENTHQ_OPTION, '' ) ); return '' === $key ? '' : COOKIECONSENTHQ_CDN . $key . '.js'; } /** * Whether this request renders a normal front-end page the banner belongs on. * * @return bool */ function cookieconsenthq_should_print() { if ( is_admin() || is_feed() || is_embed() ) { return false; } if ( ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || wp_is_json_request() ) { return false; } // AMP pages cannot run custom JavaScript; they need amp-consent instead. if ( function_exists( 'amp_is_request' ) && amp_is_request() ) { return false; } if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { return false; } return true; } /** * Print the banner script first in . It must stay synchronous (no async/defer) so it runs * before any tracker and can block it; the data attributes ask Cloudflare Rocket Loader and * LiteSpeed Cache not to delay or combine it. data-cchq-credit="off" tells the banner to leave out * its "Powered by CookieConsentHQ" link unless the admin turned that link on. */ function cookieconsenthq_print_script() { if ( ! cookieconsenthq_should_print() ) { return; } $url = cookieconsenthq_script_url(); if ( '' === $url ) { return; } $attributes = array( 'id' => 'cookieconsenthq', 'src' => esc_url( $url ), 'data-cfasync' => 'false', 'data-no-optimize' => '1', 'data-no-defer' => '1', ); if ( ! cookieconsenthq_show_credit() ) { $attributes['data-cchq-credit'] = 'off'; } wp_print_script_tag( $attributes ); } add_action( 'wp_head', 'cookieconsenthq_print_script', -9999 ); /** * [cchq_cookie_table]: the banner script fills this placeholder with the site's cookie declaration. * * @return string */ function cookieconsenthq_cookie_table_shortcode() { return '
'; } add_shortcode( 'cchq_cookie_table', 'cookieconsenthq_cookie_table_shortcode' ); /* * WP Consent API. The banner script sets window.wp_consent_type and calls wp_set_consent() in the * browser. Server side, fall back to opt-in so PHP consent checks fail closed until the visitor's * choice is known. */ add_filter( 'wp_consent_api_registered_' . plugin_basename( __FILE__ ), '__return_true' ); /** * Default server-side consent type when no other plugin has set one. * * @param mixed $type Consent type from other filters. * @return mixed */ function cookieconsenthq_consent_type( $type ) { return $type ? $type : 'optin'; } add_filter( 'wp_get_consent_type', 'cookieconsenthq_consent_type' ); /* ---------- Settings → CookieConsentHQ ---------- */ /** * Sanitize callback for the site key option. Invalid input keeps the saved value and shows an error. * * @param mixed $input Submitted value. * @return string */ function cookieconsenthq_sanitize_site_key( $input ) { if ( is_string( $input ) && '' === trim( $input ) ) { return ''; } $key = cookieconsenthq_parse_site_key( $input ); if ( '' === $key ) { add_settings_error( COOKIECONSENTHQ_OPTION, 'cookieconsenthq_invalid', __( 'That is not a CookieConsentHQ script URL. Copy it from the Install tab of your dashboard; it looks like https://cdn.cookieconsenthq.com/s/sk_….js', 'cookieconsenthq' ) ); return (string) get_option( COOKIECONSENTHQ_OPTION, '' ); } return $key; } /** Whether the admin opted in to the "Powered by CookieConsentHQ" link. */ function cookieconsenthq_show_credit() { return '1' === get_option( COOKIECONSENTHQ_CREDIT_OPTION, '' ); } /** * Store the credit checkbox as '1' or ''. * * @param mixed $input Submitted value ('1' when checked, missing when not). * @return string */ function cookieconsenthq_sanitize_credit( $input ) { return '1' === $input || 1 === $input || true === $input ? '1' : ''; } /** Register the options with the Settings API (which checks the nonce and manage_options on save). */ function cookieconsenthq_register_settings() { register_setting( 'cookieconsenthq', COOKIECONSENTHQ_OPTION, array( 'type' => 'string', 'sanitize_callback' => 'cookieconsenthq_sanitize_site_key', 'default' => '', 'show_in_rest' => false, ) ); register_setting( 'cookieconsenthq', COOKIECONSENTHQ_CREDIT_OPTION, array( 'type' => 'string', 'sanitize_callback' => 'cookieconsenthq_sanitize_credit', 'default' => '', 'show_in_rest' => false, ) ); } add_action( 'admin_init', 'cookieconsenthq_register_settings' ); /** Add the page under Settings. */ function cookieconsenthq_add_settings_page() { add_options_page( __( 'CookieConsentHQ', 'cookieconsenthq' ), __( 'CookieConsentHQ', 'cookieconsenthq' ), 'manage_options', 'cookieconsenthq', 'cookieconsenthq_render_settings_page' ); } add_action( 'admin_menu', 'cookieconsenthq_add_settings_page' ); /** Render Settings → CookieConsentHQ. */ function cookieconsenthq_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $url = cookieconsenthq_script_url(); ?>


[cchq_cookie_table]' ); ?>

' . esc_html__( 'Settings', 'cookieconsenthq' ) . ''; array_unshift( $links, $settings ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'cookieconsenthq_action_links' );