Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions assets/cookie-notice/frontconsent-cookie-notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,40 @@
}
}

function defineActivateGatedHelper() {
window.frcnCookieNoticeActivateGated = window.frcnCookieNoticeActivateGated || function (allowedCategories) {
if (allowedCategories && !allowedCategories.analytics) {
return;
}

var run = function () {
var gated = Array.prototype.slice.call(document.querySelectorAll('script[data-frcn-consent=analytics]')).filter(function (node) {
return 'text/plain' === node.type;
});

var next = function () {
var inert = gated.shift();
if (!inert) {
return;
}
var live = document.createElement('script');
live.async = false;
live.onload = next;
live.onerror = next;
live.src = inert.src;
inert.parentNode.replaceChild(live, inert);
};
next();
};

if ('loading' === document.readyState) {
document.addEventListener('DOMContentLoaded', run);
} else {
run();
}
};
}

function defineInjectHelper() {
window.frcnCookieNoticeInject = window.frcnCookieNoticeInject || function (gtmId, ga4Id, trackingIntegrations, allowedCategories) {
var allowsCategory = function (category) {
Expand Down Expand Up @@ -133,6 +167,8 @@
window.frcnCookieNoticePendingIntegrations.push(integration);
}
});

window.frcnCookieNoticeActivateGated(allowedCategories);
};
}

Expand Down Expand Up @@ -230,6 +266,7 @@
return;
}

defineActivateGatedHelper();
defineInjectHelper();

var isStale = typeof window.frcnCookieNoticeIsConsentStale === 'function' && window.frcnCookieNoticeIsConsentStale();
Expand Down
2 changes: 1 addition & 1 deletion assets/cookie-notice/frontconsent-cookie-notice.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions frontconsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: FrontConsent
* Plugin URI: https://wordpress.org/plugins/frontconsent/
* Description: GDPR/ePrivacy-compliant cookie consent banner for WordPress, following the AEPD guide. Configurable banner, Google Consent Mode v2, and tracking integrations that only load after consent.
* Version: 1.0.1
* Version: 1.0.2-beta.gdpr
* Author: CloseTechnology
* Author URI: https://close.technology
* Text Domain: frontconsent
Expand All @@ -26,7 +26,7 @@

defined( 'ABSPATH' ) || die( 'No script kiddies please!' );

define( 'FRCN_VERSION', '1.0.1' );
define( 'FRCN_VERSION', '1.0.2-beta.gdpr' );
define( 'FRCN_PLUGIN', __FILE__ );
define( 'FRCN_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'FRCN_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
Expand Down
78 changes: 78 additions & 0 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace FrontConsent\Admin;

use FrontConsent\Frontend\CookieNotice;
use FrontConsent\GDPR_Cookie_Compliance_Migration;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -56,6 +57,82 @@ public function __construct() {
add_action( 'admin_menu', array( $this, 'register_menu' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'admin_post_frcn_import_gdpr_cookie_compliance', array( $this, 'handle_gdpr_cookie_compliance_import' ) );
}

/**
* Run the GDPR Cookie Compliance importer from its admin-post form.
*
* @return void
*/
public function handle_gdpr_cookie_compliance_import() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'frontconsent' ), '', array( 'response' => 403 ) );
}

check_admin_referer( 'frcn_import_gdpr_cookie_compliance' );

$report = GDPR_Cookie_Compliance_Migration::run();
if ( null !== $report ) {
set_transient( 'frcn_gdpr_import_report_' . get_current_user_id(), $report, 5 * MINUTE_IN_SECONDS );
}

wp_safe_redirect( admin_url( 'options-general.php?page=' . $this->page_slug ) );
exit;
}

/**
* Render the GDPR Cookie Compliance import prompt, or the report of the
* import the current user just ran.
*
* @return void
*/
private function render_gdpr_import_notice() {
$report_key = 'frcn_gdpr_import_report_' . get_current_user_id();
$report = get_transient( $report_key );

if ( is_array( $report ) ) {
delete_transient( $report_key );
$labels = array(
GDPR_Cookie_Compliance_Migration::STATUS_MIGRATED => __( 'Imported', 'frontconsent' ),
GDPR_Cookie_Compliance_Migration::STATUS_EXISTING => __( 'Kept existing', 'frontconsent' ),
GDPR_Cookie_Compliance_Migration::STATUS_UNSUPPORTED => __( 'Not supported', 'frontconsent' ),
GDPR_Cookie_Compliance_Migration::STATUS_REVIEW => __( 'Needs review', 'frontconsent' ),
);
?>
<div class="notice notice-success">
<p><strong><?php esc_html_e( 'GDPR Cookie Compliance import finished.', 'frontconsent' ); ?></strong></p>
<?php if ( $report ) : ?>
<ul>
<?php foreach ( $report as $row ) : ?>
<li>
<?php echo esc_html( ( $labels[ $row['status'] ] ?? '' ) . ': ' . $row['item'] ); ?>
<?php echo '' !== $row['note'] ? esc_html( ' — ' . $row['note'] ) : ''; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<p><?php esc_html_e( 'Nothing to import.', 'frontconsent' ); ?></p>
<?php endif; ?>
<p><?php esc_html_e( 'Check that the FrontConsent banner is enabled below, then deactivate GDPR Cookie Compliance and remove any hard-coded tracking scripts to avoid duplicate banners and scripts.', 'frontconsent' ); ?></p>
</div>
<?php
return;
}

if ( ! GDPR_Cookie_Compliance_Migration::has_source_settings() || get_option( GDPR_Cookie_Compliance_Migration::DONE_FLAG ) ) {
return;
}
?>
<div class="notice notice-info">
<p><?php esc_html_e( 'We found settings from GDPR Cookie Compliance. Import its tracking integrations and banner options into FrontConsent? Existing FrontConsent values are never overwritten and the other plugin is left untouched.', 'frontconsent' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<input type="hidden" name="action" value="frcn_import_gdpr_cookie_compliance" />
<?php wp_nonce_field( 'frcn_import_gdpr_cookie_compliance' ); ?>
<p><button type="submit" class="button button-primary"><?php esc_html_e( 'Import settings', 'frontconsent' ); ?></button></p>
</form>
</div>
<?php
}

/**
Expand Down Expand Up @@ -229,6 +306,7 @@ public function render_page() {
}

$this->render_cache_notice();
$this->render_gdpr_import_notice();

$tabs = array(
array(
Expand Down
93 changes: 93 additions & 0 deletions includes/Frontend/CookieNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function __construct() {
add_action( 'wp_head', array( $this, 'render_consent_bootstrap_script' ), 2 );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'wp_footer', array( $this, 'render_banner' ) );
add_filter( 'script_loader_tag', array( $this, 'gate_analytics_scripts' ), 10, 2 );
add_filter( 'gtm4wp_consent_mode_default_enabled', array( $this, 'filter_gtm4wp_consent_default' ) );
}

// The endpoints must stay available for logged-out and logged-in visitors alike.
Expand All @@ -99,6 +101,63 @@ public function __construct() {
add_action( 'admin_post_nopriv_frcn_log_cookie_decision', array( $this, 'log_consent_form_callback' ) );
}

/**
* Script handles that set analytics/attribution cookies on their own and
* must therefore wait for consent. WooCommerce's order attribution
* (sourcebuster) writes its `sbjs_*` cookies on every first page view.
*
* @return string[]
*/
public static function get_gated_script_handles() {
/**
* Filters the script handles held back until the visitor accepts.
*
* Return an empty array to stop FrontConsent from gating any script.
*
* @param string[] $handles Registered script handles.
*/
$handles = apply_filters( 'frcn_cookie_notice_gated_script_handles', array( 'sourcebuster-js', 'wc-order-attribution' ) );

return is_array( $handles ) ? array_map( 'strval', $handles ) : array();
}

/**
* Turn a gated script's tag into an inert one. The tag is always printed,
* identically for every visitor (cache-neutral, like the rest of the
* banner); JS runs it once consent is accepted.
*
* @param string $tag Script tag HTML.
* @param string $handle Script handle.
* @return string
*/
public function gate_analytics_scripts( $tag, $handle ) {
if ( ! in_array( $handle, self::get_gated_script_handles(), true ) ) {
return $tag;
}

$tag = preg_replace( '/\stype=(["\'])[^"\']*\1/i', '', (string) $tag );

return preg_replace( '/^<script\b/i', '<script type="text/plain" data-frcn-consent="analytics"', (string) $tag, 1 );
}

/**
* Stop GTM4WP from sending its own Consent Mode "default" command (it can
* grant analytics storage before consent): FrontConsent already sends the
* denied default first, and a second source makes the result depend on
* ordering.
*
* @param bool $enabled Whether GTM4WP outputs its consent default.
* @return bool
*/
public function filter_gtm4wp_consent_default( $enabled ) {
/**
* Filters whether FrontConsent suppresses GTM4WP's consent default.
*
* @param bool $suppress True to suppress (default).
*/
return apply_filters( 'frcn_cookie_notice_suppress_gtm4wp_consent_default', true ) ? false : $enabled;
}

/**
* Handle a saved FrontConsent settings option.
*
Expand Down Expand Up @@ -824,6 +883,38 @@ public function render_consent_bootstrap_script() {
}
}

window.frcnCookieNoticeActivateGated = window.frcnCookieNoticeActivateGated || function ( allowedCategories ) {
if ( allowedCategories && ! allowedCategories.analytics ) {
return;
}

var run = function () {
var gated = Array.prototype.slice.call( document.querySelectorAll( 'script[data-frcn-consent=analytics]' ) ).filter( function ( node ) {
return 'text/plain' === node.type;
} );

var next = function () {
var inert = gated.shift();
if ( ! inert ) {
return;
}
var live = document.createElement( 'script' );
live.async = false;
live.onload = next;
live.onerror = next;
live.src = inert.src;
inert.parentNode.replaceChild( live, inert );
};
next();
};

if ( 'loading' === document.readyState ) {
document.addEventListener( 'DOMContentLoaded', run );
} else {
run();
}
};

window.frcnCookieNoticeInject = window.frcnCookieNoticeInject || function ( gtmId, ga4Id, trackingIntegrations, allowedCategories ) {
var allowsCategory = function ( category ) {
return ! allowedCategories || !! allowedCategories[ category ];
Expand Down Expand Up @@ -913,6 +1004,8 @@ public function render_consent_bootstrap_script() {
window.frcnCookieNoticePendingIntegrations.push( integration );
}
} );

window.frcnCookieNoticeActivateGated( allowedCategories );
};

// An add-on tracking per-category consent can define this (printed
Expand Down
Loading
Loading