feat: caching, optimization, legal pages & footer

- WP Super Cache enabled (PHP mode, gzip, Nginx compatible)
- Autoptimize: CSS/HTML minification + deferred JS + Google Fonts optimization
- Cookie Notice: GDPR/LOPD banner styled with brand colors (navy/burgundy/gold)
- Legal pages: Aviso Legal, Política de Privacidad, Política de Cookies (ES)
- MU-plugin: custom footer with legal links + Cloud Host credit
- Footer: copyright, legal nav, Hosted & Maintained by Cloud Host (cloudhost.es)
- Security: X-Frame-Options, X-Content-Type, Referrer-Policy headers
- Security: XML-RPC disabled, REST user enumeration blocked
- Performance: emoji scripts removed, post revisions limited to 3
This commit is contained in:
Malin
2026-05-19 19:58:11 +02:00
parent 67241f537f
commit 6daabcab65
277 changed files with 96841 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules AMP class.
*
* Compatibility since: 2.0.0
*
* @class Cookie_Notice_Modules_AMP
*/
class Cookie_Notice_Modules_AMP {
private $nonce = '';
/**
* Constructor.
*
* @return void
*/
public function __construct() {
$this->nonce = wp_create_nonce( 'cookie-compliance-amp-consent' );
add_action( 'init', [ $this, 'handle_iframe' ] );
add_action( 'wp_head', [ $this, 'load_amp_consent' ] );
}
/**
* Load AMP consent module.
*
* @return void
*/
public function load_amp_consent() {
// is banner allowed to display?
if ( ! Cookie_Notice()->frontend->maybe_display_banner( [ 'skip_amp' => true ] ) )
return;
if ( function_exists( 'amp_is_request' ) && amp_is_request() ) {
// load styles
echo '
<style amp-custom>
#cnConsentContainer {
background: none;
border: none;
box-shadow: none;
border-radius: 0;
height: 100vh;
width: 100%;
}
#cnConsentContainer amp-iframe {
height: 100vh;
width: 100%;
}
#cnConsentWidget {
height: 100vh;
width: 100%;
}
</style>';
// load scripts
echo '
<script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
<script async custom-element="amp-consent" src="https://cdn.ampproject.org/v0/amp-consent-0.1.js"></script>
<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>';
// get iframe url
$url = apply_filters( 'cn_cookie_compliance_amp_iframe_url', $this->add_subdomain_to_url( get_site_url(), 'www' ) );
// load consent iframe
echo '
<amp-consent id="cnConsentContainer" layout="nodisplay">
<script type="application/json">
{
"consentInstanceId": "cnConsent",
"consentRequired": true,
"purposeConsentRequired": [ "basic_operations", "content_personalization", "site_optimization", "ad_personalization" ],
"promptUI": "cnConsentWidget"
}
</script>
<div id="cnConsentWidget">
<amp-iframe layout="fill" sandbox="allow-scripts allow-same-origin" src="' . esc_url( $url . '/?cn-amp-consent-iframe=' . $this->nonce ) . '">
<div placeholder></div>
</amp-iframe>
</div>
</amp-consent>';
}
}
/**
* Add subdomain to url.
*
* @param string $url
* @param string $subdomain
* @return string
*/
public function add_subdomain_to_url( $url, $subdomain ) {
// parse url
$parts = parse_url( $url );
// subdomain does not exist?
if ( substr( $parts['host'], 0, strlen( $subdomain ) + 1 ) !== $subdomain . '.' ) {
// find host
$pos = strpos( $url, $parts['host'] );
// update url and add subdomain
$url = substr_replace( $url, $subdomain . '.' . $parts['host'], $pos, strlen( $parts['host'] ) );
}
return $url;
}
/**
* Generate consent iframe.
*
* @return void
*/
public function handle_iframe() {
if ( isset( $_GET['cn-amp-consent-iframe'] ) && $_GET['cn-amp-consent-iframe'] === $this->nonce ) {
wp_ob_end_flush_all();
// display iframe
echo $this->generate_iframe_html();
exit;
}
}
/**
* Generate consent iframe.
*
* @return string
*/
public function generate_iframe_html() {
// get main instance
$cn = Cookie_Notice();
// get options
$options = $cn->frontend->get_cc_options();
// get output
$cc_output = $cn->frontend->get_cc_output( $options );
// get allowed html for cookie compliance html output
$allowed_html = array_merge(
wp_kses_allowed_html( 'post' ),
[
'script' => [
'type' => true,
'src' => true
]
]
);
$html = '
<!DOCTYPE html>
<html ' . get_language_attributes( 'html' ) . '>
<head>
<meta charset="' . esc_attr( get_bloginfo( 'charset', 'display' ) ) . '">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex">
<title>' . esc_html__( 'Cookie Compliance AMP Consent', 'cookie-notice' ) . '</title>
' . wp_kses( $cc_output, $allowed_html ) . '
<script type="text/javascript" src="' . esc_url( COOKIE_NOTICE_URL . '/includes/modules/amp/iframe.js' ) . '"></script>
</head>
<body></body>
</html>';
return $html;
}
}
new Cookie_Notice_Modules_AMP();

View File

@@ -0,0 +1,58 @@
function cnDisableRevoke() {
var huObject = null;
// get valid hu object
for ( const object of [ '__hu', 'hu' ] ) {
// check global variable
if ( typeof window[object] !== 'undefined' && window[object].hasOwnProperty( 'earlyInit' ) && typeof window[object].earlyInit === 'function' ) {
huObject = window[object];
// no need to check again
break;
}
}
// force revoke button to be disabled
huObject.options.config.revokeConsent = false;
}
function cnConsentResponse( event ) {
// allow this event to run only once
if ( event.type === 'set-consent.hu' )
document.removeEventListener( 'hide.hu', cnConsentResponse );
document.addEventListener( 'save-consent-response.hu', cnConsentSet( event.detail.categories ) );
}
function cnConsentSet( categories ) {
// it has to use return function to wait for save-consent-response event
return function cnRequestFinished( event ) {
var action = 'accept';
// only basic operations?
if ( categories[1] && ! categories[2] && ! categories[3] && ! categories[4] )
action = 'reject';
// inform amp to save consent
window.parent.postMessage(
{
type: 'consent-response',
action: action,
purposeConsents: {
'basic_operations': categories[1],
'content_personalization': categories[2],
'site_optimization': categories[3],
'ad_personalization': categories[4]
}
},
'*'
);
}
}
// set consent
document.addEventListener( 'hide.hu', cnConsentResponse );
document.addEventListener( 'set-consent.hu', cnConsentResponse );
// disable revoke button
document.addEventListener( 'load.hu', cnDisableRevoke );

View File

@@ -0,0 +1,72 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Autoptimize class.
*
* Compatibility since: 2.4.0
*
* @class Cookie_Notice_Modules_Autoptimize
*/
class Cookie_Notice_Modules_Autoptimize {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'plugins_loaded', [ $this, 'load_module' ], 11 );
}
/**
* Add compatibility to Autoptimize plugin.
*
* @return void
*/
public function load_module() {
add_filter( 'autoptimize_filter_js_exclude', [ $this, 'exclude' ] );
}
/**
* Exclude JavaScript files or inline code.
*
* @param string $excludes
* @return string
*/
function exclude( $excludes ) {
if ( empty( $excludes ) )
$new_excludes = [];
else {
$new_excludes = explode( ',', $excludes );
$new_excludes = array_filter( $new_excludes );
$new_excludes = array_map( 'trim', $new_excludes );
}
// not found huOptions?
if ( strpos( $excludes, 'huOptions' ) === false )
$new_excludes[] = 'huOptions';
// get widget url
$widget_url = basename( Cookie_Notice()->get_url( 'widget' ) );
// not found widget url?
if ( strpos( $excludes, $widget_url ) === false )
$new_excludes[] = $widget_url;
// React admin asset exclusions — see Cookie_Notice::REACT_ADMIN_*.
// Keeps Autoptimize off our IIFE bundle if its "optimize admin" toggle is on.
if ( strpos( $excludes, Cookie_Notice::REACT_ADMIN_BUNDLE_BASENAME ) === false )
$new_excludes[] = Cookie_Notice::REACT_ADMIN_BUNDLE_BASENAME;
if ( strpos( $excludes, Cookie_Notice::REACT_ADMIN_INLINE_KEYWORD ) === false )
$new_excludes[] = Cookie_Notice::REACT_ADMIN_INLINE_KEYWORD;
return implode( ', ', $new_excludes );
}
}
new Cookie_Notice_Modules_Autoptimize();

View File

@@ -0,0 +1,100 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Breeze class.
*
* Compatibility since: 2.0.30
*
* @class Cookie_Notice_Modules_Breeze
*/
class Cookie_Notice_Modules_Breeze {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'plugins_loaded', [ $this, 'load_module' ], 11 );
}
/**
* Add compatibility to Breeze plugin.
*
* @return void
*/
public function load_module() {
// get main instance
$cn = Cookie_Notice();
// update 2.5.7+
if ( version_compare( $cn->db_version, '2.5.7', '<=' ) )
$this->remove_excluded_external_script();
// is caching active?
if ( (int) Breeze_Options_Reader::get_option_value( 'breeze-active' ) === 1 ) {
// update 2.4.16+
if ( version_compare( $cn->db_version, '2.4.16', '<=' ) ) {
// clear cache
$this->delete_cache();
}
add_action( 'cn_configuration_updated', [ $this, 'delete_cache' ] );
// is js minification active?
if ( (int) Breeze_Options_Reader::get_option_value( 'breeze-minify-js' ) === 1 ) {
// filters
add_filter( 'cn_cookie_compliance_output', [ $this, 'update_cc_output' ] );
}
}
}
/**
* Update Cookie Compliance output.
*
* @param string $output
*
* @return string
*/
public function update_cc_output( $output ) {
// add special /breeze-extra/ comment
return preg_replace( '/<script(.*)var huOptions(.*)<\/script>/', "<script$1var huOptions$2\n//breeze-extra/</script>", $output, 1 );
}
/**
* Remove previously excluded external script from being minified/combined.
*
* @return void
*/
public function remove_excluded_external_script() {
$pattern = '(.*)/js/hu-options.js(.*)';
// get breeze file options
$file_options = breeze_get_option( 'file_settings' );
// find pattern
$key = array_search( $pattern, $file_options['breeze-exclude-js'], true );
// found pattern? remove it
if ( $key !== false )
unset( $file_options['breeze-exclude-js'][$key] );
// update breeze file options
breeze_update_option( 'file_settings', $file_options, true );
}
/**
* Delete all cache files.
*
* @return void
*/
public function delete_cache() {
do_action( 'breeze_clear_all_cache' );
}
}
new Cookie_Notice_Modules_Breeze();

View File

@@ -0,0 +1,69 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Contact Form 7 class.
*
* Compatibility since: 5.1.0 (recaptcha v3 only)
*
* @class Cookie_Notice_Modules_ContactForm7
*/
class Cookie_Notice_Modules_ContactForm7 {
private $service;
/**
* Constructor.
*
* @return void
*/
public function __construct() {
$this->service = WPCF7_RECAPTCHA::get_instance();
if ( $this->service->is_active() )
add_action( 'wp_enqueue_scripts', [ $this, 'contact_form_7_recaptcha' ], 21 );
}
/**
* Replace original recaptcha script from Contact Form 7.
*
* @return void
*/
public function contact_form_7_recaptcha() {
// deregister original script
wp_deregister_script( 'wpcf7-recaptcha' );
// register new script
wp_register_script(
'wpcf7-recaptcha',
COOKIE_NOTICE_URL . '/includes/modules/contact-form-7/recaptcha.js',
[
'google-recaptcha',
'wp-polyfill'
],
WPCF7_VERSION,
true
);
wp_enqueue_script( 'wpcf7-recaptcha' );
wp_localize_script(
'wpcf7-recaptcha',
'wpcf7_recaptcha',
[
'sitekey' => $this->service->get_sitekey(),
'actions' => apply_filters(
'wpcf7_recaptcha_actions',
[
'homepage' => 'homepage',
'contactform' => 'contactform'
]
)
]
);
}
}
new Cookie_Notice_Modules_ContactForm7();

View File

@@ -0,0 +1,246 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Contact Form 7 Privacy Consent class.
*
* Compatibility since: 5.3.0
*
* @class Cookie_Notice_Modules_ContactForm7_Privacy_Consent
*/
class Cookie_Notice_Modules_ContactForm7_Privacy_Consent {
private $defaults = [];
private $source = [];
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// get main instance
$cn = Cookie_Notice();
$this->source = [
'name' => __( 'Contact Form 7', 'cookie-notice' ),
'id' => 'contactform7',
'id_type' => 'integer',
'type' => 'dynamic',
'availability' => cn_is_plugin_active( 'contactform7', 'privacy-consent' ),
'status' => $cn->options['privacy_consent']['contactform7_active'],
'status_type' => $cn->options['privacy_consent']['contactform7_active_type'],
'forms' => []
];
// register source
$cn->privacy_consent->add_instance( $this, $this->source['id'] );
$cn->privacy_consent->add_source( $this->source );
add_action( 'admin_init', [ $this, 'register_source' ] );
// check compliance status
if ( $cn->get_status() !== 'active' )
return;
// forms
add_filter( 'do_shortcode_tag', [ $this, 'shortcode' ], 10, 3 );
}
/**
* Register source.
*
* @return void
*/
public function register_source() {
register_setting(
'cookie_notice_privacy_consent_contactform7',
'cookie_notice_privacy_consent_contactform7',
[
'type' => 'array'
]
);
}
/**
* Validate source.
*
* @param array $input
*
* @return array
*/
public function validate( $input ) {
// get main instance
$cn = Cookie_Notice();
$input['contactform7_active'] = isset( $input['contactform7_active'] );
$input['contactform7_active_type'] = isset( $input['contactform7_active_type'] ) && array_key_exists( $input['contactform7_active_type'], $cn->privacy_consent->form_active_types ) ? $input['contactform7_active_type'] : $cn->defaults['privacy_consent']['contactform7_active_type'];
return $input;
}
/**
* Check whether form exists.
*
* @param int $form_id
*
* @return bool
*/
public function form_exists( $form_id ) {
$query = new WP_Query( [
'p' => $form_id,
'post_status' => 'publish',
'post_type' => 'wpcf7_contact_form',
'fields' => 'ids',
'no_found_rows' => true
] );
return $query->have_posts();
}
/**
* Get forms.
*
* @param array $args
*
* @return array
*/
public function get_forms( $args ) {
// get only published forms
$query = new WP_Query( [
'post_status' => 'publish',
'post_type' => 'wpcf7_contact_form',
'order' => $args['order'],
'orderby' => $args['orderby'],
'fields' => 'all',
'posts_per_page' => 10,
'no_found_rows' => false,
'paged' => $args['page'],
's' => $args['search']
] );
$forms = [];
// any forms?
if ( ! empty( $query->posts ) ) {
foreach ( $query->posts as $post ) {
$forms[] = [
'id' => $post->ID,
'title' => $post->post_title,
'date' => $post->post_date,
'fields' => []
];
}
}
return [
'forms' => $forms,
'total' => $query->found_posts,
'max_pages' => $query->max_num_pages
];
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function get_form( $args ) {
// get only one form
$query = new WP_Query( [
'p' => (int) $args['form_id'],
'post_status' => 'publish',
'post_type' => 'wpcf7_contact_form',
'fields' => 'all',
'no_found_rows' => true
] );
// any forms?
if ( ! empty( $query->posts[0] ) ) {
$form = [
'source' => $this->source['id'],
'id' => $query->posts[0]->ID,
'title' => Cookie_Notice()->privacy_consent->strcut( sanitize_text_field( $query->posts[0]->post_title ), 100 ),
'fields' => [
'subject' => [
'first_name' => 'your-name',
'email' => 'your-email'
],
'preferences' => [
'terms' => 'your-consent'
]
]
];
} else
$form = [];
return $form;
}
/**
* Contact Form 7 shortcode output.
*
* @param string $output
* @param string $tag
* @param array $attr
*
* @return string
*/
public function shortcode( $output, $tag, $attr ) {
if ( $tag !== 'contact-form-7' )
return $output;
// check form id
$form_id = isset( $attr['id'] ) ? trim( $attr['id'] ) : '';
if ( empty( $form_id ) )
return $output;
$form = null;
// sanitize form id
$form_id = preg_replace( '/[^a-z0-9]/i', '', $form_id );
// get form by hash, since cf7 5.8
if ( function_exists( 'wpcf7_get_contact_form_by_hash' ) )
$form = wpcf7_get_contact_form_by_hash( $form_id );
// get form by integer id
if ( ! $form && function_exists( 'wpcf7_contact_form' ) )
$form = wpcf7_contact_form( $form_id );
// valid form?
if ( $form && is_a( $form, 'WPCF7_ContactForm' ) ) {
// get id
$form_id = (int) $form->id();
// active form?
if ( $form_id && Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id
] );
$output = (string) $output;
$output .= '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'[id^="wpcf7-f' . (int) $form_id . '-"] form\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
}
return $output;
}
}
new Cookie_Notice_Modules_ContactForm7_Privacy_Consent();

View File

@@ -0,0 +1,89 @@
( function( window, document, undefined ) {
'use strict';
/**
* Initialize recaptcha.
*
* @return {void}
*/
function initRecaptcha() {
wpcf7_recaptcha = {
...( wpcf7_recaptcha ?? {} )
};
const siteKey = wpcf7_recaptcha.sitekey;
const { homepage, contactform } = wpcf7_recaptcha.actions;
const execute = options => {
const { action, func, params } = options;
grecaptcha.execute( siteKey, {
action,
} ).then( token => {
const event = new CustomEvent( 'wpcf7grecaptchaexecuted', {
detail: {
action,
token
}
} );
document.dispatchEvent( event );
} ).then( () => {
if ( typeof func === 'function' ) {
func( ...params );
}
} ).catch( error => console.error( error ) );
};
grecaptcha.ready( () => {
execute( {
action: homepage
} );
} );
document.addEventListener( 'change', event => {
execute( {
action: contactform
} );
} );
if ( typeof wpcf7 !== 'undefined' && typeof wpcf7.submit === 'function' ) {
const submit = wpcf7.submit;
wpcf7.submit = ( form, options = {} ) => {
execute( {
action: contactform,
func: submit,
params: [ form, options ]
} );
};
}
document.addEventListener( 'wpcf7grecaptchaexecuted', event => {
const fields = document.querySelectorAll( 'form.wpcf7-form input[name="_wpcf7_recaptcha_response"]' );
for ( let i = 0; i < fields.length; i++ ) {
let field = fields[ i ];
field.setAttribute( 'value', event.detail.token );
}
} );
}
/**
* Handle cookies-unblocked event.
*
* @return {void}
*/
document.addEventListener( 'cookies-unblocked.hu', function( e ) {
e.detail.data.scripts.forEach( function( script ) {
// find google recaptcha in valid category
if ( script.id === 'google-recaptcha-js' && e.detail.categories[script.dataset.huCategory] === true ) {
script.onload = initRecaptcha;
script.onreadystatechange = initRecaptcha;
}
} );
}, false );
} )( window, document );

View File

@@ -0,0 +1,34 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Divi class.
*
* Compatibility since: 2.4.19
*
* @class Cookie_Notice_Modules_Divi
*/
class Cookie_Notice_Modules_Divi {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
add_filter( 'cn_is_preview_mode', [ $this, 'is_preview_mode' ] );
}
/**
* Whether Divi builder is active.
*
* @return bool
*/
function is_preview_mode() {
return is_et_pb_preview() || isset( $_GET[ 'et_fb' ] );
}
}
new Cookie_Notice_Modules_Divi();

View File

@@ -0,0 +1,397 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Easy Digital Downloads Privacy Consent class.
*
* Compatibility since: 3.0.0
*
* @class Cookie_Notice_Modules_EasyDigitalDownloads_Privacy_Consent
*/
class Cookie_Notice_Modules_EasyDigitalDownloads_Privacy_Consent {
private $defaults = [
'edd_registration_form' => [
'status' => false
],
'edd_checkout_form' => [
'status' => false
]
];
private $source = [];
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// get main instance
$cn = Cookie_Notice();
$this->source = [
'name' => __( 'Easy Digital Downloads', 'cookie-notice' ),
'id' => 'easydigitaldownloads',
'id_type' => 'string',
'type' => 'static',
'availability' => cn_is_plugin_active( 'easydigitaldownloads', 'privacy-consent' ),
'status' => $cn->options['privacy_consent']['easydigitaldownloads_active'],
'status_type' => $cn->options['privacy_consent']['easydigitaldownloads_active_type'],
'forms' => [
'edd_registration_form' => [
'name' => __( 'Registration Form', 'cookie-notice' ),
'id' => 'edd_registration_form',
'type' => 'static',
'mode' => 'automatic',
'status' => false,
'logged_out_only' => true,
'fields' => [
'username' => '',
'email' => ''
],
'subject' => [
'email' => '#edd-user-email'
],
'preferences' => [],
'excluded' => []
],
'edd_checkout_form' => [
'name' => __( 'Checkout Form', 'cookie-notice' ),
'id' => 'edd_checkout_form',
'type' => 'static',
'mode' => 'automatic',
'status' => true,
'logged_out_only' => true,
'fields' => [
'email' => '',
'first_name' => '',
'last_name' => '',
'country' => '',
'address' => '',
'city' => '',
'postal_code' => '',
'phone' => ''
],
'subject' => [
'email' => '#edd-email',
'first_name' => '#edd-first',
'last_name' => '#edd-last'
],
'preferences' => [
'terms' => '#edd_agree_to_terms',
'privacy' => '#edd-agree-to-privacy-policy'
],
'excluded' => []
]
]
];
// register source
$cn->privacy_consent->add_instance( $this, $this->source['id'] );
$cn->privacy_consent->add_source( $this->source );
add_action( 'admin_init', [ $this, 'register_source' ] );
// check compliance status
if ( $cn->get_status() !== 'active' )
return;
// registration
add_action( 'edd_register_form_fields_after', [ $this, 'registration_form_classic' ] );
add_filter( 'render_block', [ $this, 'registration_form_blocks' ], 10, 2 );
add_filter( 'edd_errors', [ $this, 'errors' ] );
// checkout
add_action( 'edd_checkout_form_bottom', [ $this, 'checkout_form_classic' ] );
add_filter( 'render_block', [ $this, 'checkout_form_blocks' ], 10, 2 );
add_action( 'edd_built_order', [ $this, 'checkout_new_order' ], 10, 2 );
}
/**
* Register source.
*
* @return void
*/
public function register_source() {
register_setting(
'cookie_notice_privacy_consent_easydigitaldownloads',
'cookie_notice_privacy_consent_easydigitaldownloads',
[
'type' => 'array'
]
);
}
/**
* Validate source.
*
* @param array $input
*
* @return array
*/
public function validate( $input ) {
// get main instance
$cn = Cookie_Notice();
$input['easydigitaldownloads_active'] = isset( $input['easydigitaldownloads_active'] );
$input['easydigitaldownloads_active_type'] = isset( $input['easydigitaldownloads_active_type'] ) && array_key_exists( $input['easydigitaldownloads_active_type'], $cn->privacy_consent->form_active_types ) ? $input['easydigitaldownloads_active_type'] : $cn->defaults['privacy_consent']['easydigitaldownloads_active_type'];
return $input;
}
/**
* Check whether form exists.
*
* @param string $form_id
*
* @return bool
*/
public function form_exists( $form_id ) {
return array_key_exists( $form_id, $this->source['forms'] );
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function get_form( $args ) {
// invalid form?
if ( ! $this->form_exists( $args['form_id'] ) )
return [];
$form_data = $this->source['forms'][$args['form_id']];
$form = [
'source' => $this->source['id'],
'id' => $form_data['id'],
'title' => $form_data['name'],
'fields' => [
'subject' => $form_data['subject'],
'preferences' => $form_data['preferences']
]
];
return $form;
}
/**
* Registration classic form.
*
* @return void
*/
public function registration_form_classic() {
$form_id = 'edd_registration_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
if ( $this->source['forms'][ $form_id ]['logged_out_only'] && is_user_logged_in() )
return;
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id,
'form_type' => 'classic'
] );
echo '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.getElementById( \'edd_register_form\' );';
// edd 3.2.3+ uses different id for registration form in blocks
if ( defined( 'EDD_VERSION' ) && version_compare( EDD_VERSION, '3.2.3', '>=' ) ) {
echo '
if ( huFormNode === null )
huFormNode = document.getElementById( \'edd-blocks-form__register\' );';
}
echo '
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
}
/**
* Registration blocks form.
*
* @param string|mixed $block_content
* @param array $block
*
* @return string
*/
public function registration_form_blocks( $block_content, $block ) {
// edd version 3.2.3+ has native support of edd_checkout_form_bottom in blocks
if ( defined( 'EDD_VERSION' ) && version_compare( EDD_VERSION, '3.2.3', '>=' ) )
return $block_content;
// not edd checkout block?
if ( $block['blockName'] !== 'edd/register' )
return $block_content;
$form_id = 'edd_registration_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
if ( $this->source['forms'][ $form_id ]['logged_out_only'] && is_user_logged_in() )
return;
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id,
'form_type' => 'blocks'
] );
$block_content = '
<div class="wp-block" data-blockType="' . esc_attr( $block['blockName'] ) . '">
' . $block_content . '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.getElementById( \'edd-blocks-form__register\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>
</div>';
}
return $block_content;
}
/**
* Registration errors.
*
* @param array $errors
*
* @return array
*/
public function errors( $errors ) {
if ( isset( $_POST['edd_action'] ) && $_POST['edd_action'] === 'user_register' ) {
// prevent headers already sent
if ( headers_sent() )
return $errors;
// get main instance
$cn = Cookie_Notice();
// active registration form?
if ( $cn->privacy_consent->is_form_active( 'edd_registration_form', $this->source['id'] ) )
$cn->privacy_consent->set_cookie( empty( $errors ) ? 'true' : 'false' );
}
return $errors;
}
/**
* Checkout blocks form.
*
* @param string|mixed $block_content
* @param array $block
*
* @return string
*/
public function checkout_form_blocks( $block_content, $block ) {
// edd version 3.6.0+ has native support of edd_checkout_form_bottom in blocks
if ( defined( 'EDD_VERSION' ) && version_compare( EDD_VERSION, '3.6.0', '>=' ) )
return $block_content;
// not edd checkout block?
if ( $block['blockName'] !== 'edd/checkout' )
return $block_content;
$form_id = 'edd_checkout_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
if ( $this->source['forms'][ $form_id ]['logged_out_only'] && is_user_logged_in() )
return;
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id,
'form_type' => 'blocks'
] );
$block_content = '
<div class="wp-block" data-blockType="' . esc_attr( $block['blockName'] ) . '">
' . $block_content . '
' . $this->checkout_form_script( $form_data ) . '
</div>';
}
return $block_content;
}
/**
* Checkout classic form.
*
* @return void
*/
public function checkout_form_classic() {
$form_id = 'edd_checkout_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
if ( $this->source['forms'][ $form_id ]['logged_out_only'] && is_user_logged_in() )
return;
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id,
'form_type' => 'classic'
] );
echo $this->checkout_form_script( $form_data );
}
}
/**
* Add form script.
*
* @param array $form_data
*
* @return string
*/
public function checkout_form_script( $form_data ) {
return '<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.getElementById( \'edd_purchase_form\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
/**
* Checkout new order.
*
* @param int $order_id
* @param array $order_data
*
* @return void
*/
public function checkout_new_order( $order_id, $order_data ) {
// get main instance
$cn = Cookie_Notice();
// active checkout form?
if ( $cn->privacy_consent->is_form_active( 'edd_checkout_form', $this->source['id'] ) ) {
if ( ! empty( $order_data['user_email'] ) || ! empty( $order_data['user_info']['email'] ) )
$cn->privacy_consent->set_cookie( 'true' );
else
$cn->privacy_consent->set_cookie( 'false' );
}
}
}
new Cookie_Notice_Modules_EasyDigitalDownloads_Privacy_Consent();

View File

@@ -0,0 +1,34 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Elementor class.
*
* Compatibility since: 1.3.0
*
* @class Cookie_Notice_Modules_Elementor
*/
class Cookie_Notice_Modules_Elementor {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
add_filter( 'cn_is_preview_mode', [ $this, 'is_preview_mode' ] );
}
/**
* Whether elementor editor is active.
*
* @return bool
*/
function is_preview_mode() {
return \Elementor\Plugin::$instance->preview->is_preview_mode();
}
}
new Cookie_Notice_Modules_Elementor();

View File

@@ -0,0 +1,254 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Formidable Forms Privacy Consent class.
*
* Compatibility since: 2.0.0
*
* @class Cookie_Notice_Modules_FormidableForms_Privacy_Consent
*/
class Cookie_Notice_Modules_FormidableForms_Privacy_Consent {
private $defaults = [];
private $source = [];
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// get main instance
$cn = Cookie_Notice();
$this->source = [
'name' => __( 'Formidable Forms', 'cookie-notice' ),
'id' => 'formidableforms',
'id_type' => 'integer',
'type' => 'dynamic',
'availability' => cn_is_plugin_active( 'formidableforms', 'privacy-consent' ),
'status' => $cn->options['privacy_consent']['formidableforms_active'],
'status_type' => $cn->options['privacy_consent']['formidableforms_active_type'],
'forms' => []
];
// register source
$cn->privacy_consent->add_instance( $this, $this->source['id'] );
$cn->privacy_consent->add_source( $this->source );
add_action( 'admin_init', [ $this, 'register_source' ] );
// check compliance status
if ( $cn->get_status() !== 'active' )
return;
// forms
add_filter( 'do_shortcode_tag', [ $this, 'shortcode' ], 10, 3 );
add_filter( 'frm_validate_entry', [ $this, 'handle_form' ], PHP_INT_MAX, 2 );
}
/**
* Register source.
*
* @return void
*/
public function register_source() {
register_setting(
'cookie_notice_privacy_consent_formidableforms',
'cookie_notice_privacy_consent_formidableforms',
[
'type' => 'array'
]
);
}
/**
* Validate source.
*
* @param array $input
*
* @return array
*/
public function validate( $input ) {
// get main instance
$cn = Cookie_Notice();
$input['formidableforms_active'] = isset( $input['formidableforms_active'] );
$input['formidableforms_active_type'] = isset( $input['formidableforms_active_type'] ) && array_key_exists( $input['formidableforms_active_type'], $cn->privacy_consent->form_active_types ) ? $input['formidableforms_active_type'] : $cn->defaults['privacy_consent']['formidableforms_active_type'];
return $input;
}
/**
* Check whether form exists.
*
* @param int $form_id
*
* @return bool
*/
public function form_exists( $form_id ) {
$form = FrmForm::get_published_forms( [ 'id' => $form_id ], 1, 'exclude' );
return ! empty( $form );
}
/**
* Get forms.
*
* @param array $args
*
* @return array
*/
public function get_forms( $args ) {
// default query args
$query = [
'is_template' => 0,
'parent_form_id' => array( null, 0 ),
'status' => array( null, '', 'published' )
];
// searching?
if ( $args['search'] !== '' ) {
$query[] = [
'or' => true,
'name LIKE' => $args['search'],
'description LIKE' => $args['search'],
'form_key LIKE' => $args['search']
];
}
// check orderby
if ( $args['orderby'] === 'title' )
$orderby = 'name';
elseif ( $args['orderby'] === 'date' )
$orderby = 'created_at';
// get only published forms
$frm_forms = FrmForm::getAll( $query, $orderby . ' ' . $args['order'], '10 OFFSET ' . ( ( $args['page'] - 1 ) * 10 ) );
$forms = [];
// any forms?
if ( ! empty( $frm_forms ) ) {
$total_items = FrmDb::get_count( 'frm_forms', $query );
foreach ( $frm_forms as $form ) {
$forms[] = [
'id' => $form->id,
'title' => $form->name,
'date' => $form->created_at,
'fields' => []
];
}
} else
$total_items = 0;
return [
'forms' => $forms,
'total' => $total_items,
'max_pages' => (int) ceil( $total_items / 10 )
];
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function get_form( $args ) {
// get only one form
$frm_form = FrmForm::get_published_forms( [ 'id' => (int) $args['form_id'] ], 1, 'exclude' );
// any forms?
if ( ! empty( $frm_form ) ) {
$form = [
'source' => $this->source['id'],
'id' => $frm_form->id,
'title' => Cookie_Notice()->privacy_consent->strcut( sanitize_text_field( $frm_form->name ), 100 ),
'fields' => [
'subject' => [],
'preferences' => []
]
];
} else
$form = [];
return $form;
}
/**
* Formidable Forms shortcode output.
*
* @param string $output
* @param string $tag
* @param array $attr
*
* @return string
*/
public function shortcode( $output, $tag, $attr ) {
if ( $tag !== 'formidable' )
return $output;
// check form id
$form_id = isset( $attr['id'] ) ? trim( $attr['id'] ) : '';
if ( empty( $form_id ) )
return $output;
// cast form id
$form_id = (int) $form_id;
// get form
// $form = FrmForm::getOne( $form_id );
$form = FrmForm::get_published_forms( [ 'id' => $form_id ], 1, 'exclude' );
// valid abd active form?
if ( $form && Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id
] );
$output = (string) $output;
$output .= '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'#frm_form_' . $form_id . '_container form\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
return $output;
}
/**
* Handle form submission.
*
* @param array $errors
* @param array $values
*
* @return array
*/
public function handle_form( $errors, $values ) {
// get main instance
$cn = Cookie_Notice();
// active registration form?
if ( $cn->privacy_consent->is_form_active( $values['form_id'], $this->source['id'] ) )
$cn->privacy_consent->set_cookie( empty( $errors ) ? 'true' : 'false' );
return $errors;
}
}
new Cookie_Notice_Modules_FormidableForms_Privacy_Consent();

View File

@@ -0,0 +1,60 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
use Hummingbird\Core\Utils;
/**
* Cookie Notice Modules Hummingbird class.
*
* Compatibility since: 2.1.0
*
* @class Cookie_Notice_Modules_Hummingbird
*/
class Cookie_Notice_Modules_Hummingbird {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'init', [ $this, 'load_module' ] );
}
/**
* Add compatibility to Hummingbird plugin.
*
* @return void
*/
public function load_module() {
// bail if options class is not available
if ( ! class_exists( 'Hummingbird\Core\Utils' ) )
return;
// get caching module
$mod = Utils::get_module( 'page_cache' );
// valid object?
if ( is_a( $mod, 'Hummingbird\Core\Modules\Page_Cache' ) && method_exists( $mod, 'is_active' ) ) {
// is caching enabled?
if ( $mod->is_active() ) {
// delete cache files after updating settings or status
add_action( 'cn_configuration_updated', [ $this, 'delete_cache' ] );
}
}
}
/**
* Delete all cache files.
*
* @return void
*/
public function delete_cache() {
do_action( 'wphb_clear_page_cache' );
}
}
new Cookie_Notice_Modules_Hummingbird();

View File

@@ -0,0 +1,57 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules LiteSpeed Cache class.
*
* Compatibility since: 3.0.0
*
* @class Cookie_Notice_Modules_LiteSpeedCache
*/
class Cookie_Notice_Modules_LiteSpeedCache {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'init', [ $this, 'load_module' ], 9 );
}
/**
* Add compatibility to LiteSpeed Cache plugin.
*
* @return void
*/
public function load_module() {
add_filter( 'litespeed_optimize_js_excludes', [ $this, 'exclude_js' ] );
add_filter( 'litespeed_optm_js_defer_exc ', [ $this, 'exclude_js' ] );
}
/**
* Exclude JavaScript external file and inline code.
*
* @param array $excludes
* @return array
*/
function exclude_js( $excludes ) {
// add widget url
$excludes[] = basename( Cookie_Notice()->get_url( 'widget' ) );
// add widget inline code
$excludes[] = 'huOptions';
// React admin asset exclusions — see Cookie_Notice::REACT_ADMIN_*.
// Defense-in-depth if LiteSpeed's JS combine / defer touches admin pages.
$excludes[] = Cookie_Notice::REACT_ADMIN_BUNDLE_BASENAME;
$excludes[] = Cookie_Notice::REACT_ADMIN_INLINE_KEYWORD;
return $excludes;
}
}
new Cookie_Notice_Modules_LiteSpeedCache();

View File

@@ -0,0 +1,233 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules Mailchimp Privacy Consent class.
*
* Compatibility since: 4.0.0
*
* @class Cookie_Notice_Modules_Mailchimp_Privacy_Consent
*/
class Cookie_Notice_Modules_Mailchimp_Privacy_Consent {
private $defaults = [];
private $source = [];
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// get main instance
$cn = Cookie_Notice();
$this->source = [
'name' => __( 'Mailchimp for WP', 'cookie-notice' ),
'id' => 'mailchimp',
'id_type' => 'integer',
'type' => 'dynamic',
'availability' => cn_is_plugin_active( 'mailchimp', 'privacy-consent' ),
'status' => $cn->options['privacy_consent']['mailchimp_active'],
'status_type' => $cn->options['privacy_consent']['mailchimp_active_type'],
'forms' => []
];
// register source
$cn->privacy_consent->add_instance( $this, $this->source['id'] );
$cn->privacy_consent->add_source( $this->source );
add_action( 'admin_init', [ $this, 'register_source' ] );
// check compliance status
if ( $cn->get_status() !== 'active' )
return;
// forms
add_filter( 'mc4wp_form_after_fields', [ $this, 'form_html' ], 10, 2 );
add_action( 'mc4wp_form_success', [ $this, 'handle_form' ] );
}
/**
* Register source.
*
* @return void
*/
public function register_source() {
register_setting(
'cookie_notice_privacy_consent_mailchimp',
'cookie_notice_privacy_consent_mailchimp',
[
'type' => 'array'
]
);
}
/**
* Validate source.
*
* @param array $input
*
* @return array
*/
public function validate( $input ) {
// get main instance
$cn = Cookie_Notice();
$input['mailchimp_active'] = isset( $input['mailchimp_active'] );
$input['mailchimp_active_type'] = isset( $input['mailchimp_active_type'] ) && array_key_exists( $input['mailchimp_active_type'], $cn->privacy_consent->form_active_types ) ? $input['mailchimp_active_type'] : $cn->defaults['privacy_consent']['mailchimp_active_type'];
return $input;
}
/**
* Check whether form exists.
*
* @param int $form_id
*
* @return bool
*/
public function form_exists( $form_id ) {
$query = new WP_Query( [
'p' => $form_id,
'post_status' => 'publish',
'post_type' => 'mc4wp-form',
'fields' => 'ids',
'no_found_rows' => true
] );
return $query->have_posts();
}
/**
* Get forms.
*
* @param array $args
*
* @return array
*/
public function get_forms( $args ) {
// get only published forms
$query = new WP_Query( [
'post_status' => 'publish',
'post_type' => 'mc4wp-form',
'order' => $args['order'],
'orderby' => $args['orderby'],
'fields' => 'all',
'posts_per_page' => 10,
'no_found_rows' => false,
'paged' => $args['page'],
's' => $args['search']
] );
$forms = [];
// any forms?
if ( ! empty( $query->posts ) ) {
foreach ( $query->posts as $post ) {
$forms[] = [
'id' => $post->ID,
'title' => $post->post_title,
'date' => $post->post_date,
'fields' => []
];
}
}
return [
'forms' => $forms,
'total' => $query->found_posts,
'max_pages' => $query->max_num_pages
];
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function get_form( $args ) {
// get only one form
$query = new WP_Query( [
'p' => (int) $args['form_id'],
'post_status' => 'publish',
'post_type' => 'mc4wp-form',
'fields' => 'all',
'no_found_rows' => true
] );
// any forms?
if ( ! empty( $query->posts[0] ) ) {
$form = [
'source' => $this->source['id'],
'id' => $query->posts[0]->ID,
'title' => Cookie_Notice()->privacy_consent->strcut( sanitize_text_field( $query->posts[0]->post_title ), 100 ),
'fields' => [
'subject' => [
'first_name' => 'FNAME',
'last_name' => 'LNAME',
'email' => 'EMAIL'
],
'preferences' => [
'terms' => 'AGREE_TO_TERMS'
]
]
];
} else
$form = [];
return $form;
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function form_html( $html, $form ) {
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form->ID, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form->ID
] );
$html .= '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'form[class*="mc4wp-form-' . (int) $form->ID . '"]\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
return $html;
}
/**
* Handle form submission.
*
* @param object $form
*
* @return void
*/
public function handle_form( $form ) {
// get main instance
$cn = Cookie_Notice();
// active registration form?
if ( $cn->privacy_consent->is_form_active( $form->ID, $this->source['id'] ) )
$cn->privacy_consent->set_cookie( 'true' );
}
}
new Cookie_Notice_Modules_Mailchimp_Privacy_Consent();

View File

@@ -0,0 +1,104 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
use SiteGround_Optimizer\Options\Options;
/**
* Cookie Notice Modules Speed Optimizer class.
*
* Compatibility since: 5.5.0
*
* @class Cookie_Notice_Modules_SpeedOptimizer
*/
class Cookie_Notice_Modules_SpeedOptimizer {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'plugins_loaded', [ $this, 'load_module' ], 11 );
}
/**
* Add compatibility to Speed Optimizer plugin.
*
* @return void
*/
public function load_module() {
// bail if options class is not available
if ( ! class_exists( 'SiteGround_Optimizer\Options\Options' ) )
return;
// check caching status
$cache_active = Options::is_enabled( 'siteground_optimizer_enable_cache' ) || Options::is_enabled( 'siteground_optimizer_file_caching' );
// update 2.4.17
if ( version_compare( Cookie_Notice()->db_version, '2.4.16', '<=' ) ) {
if ( $cache_active ) {
// clear cache
$this->delete_cache();
}
}
if ( $cache_active ) {
// actions
add_action( 'cn_configuration_updated', [ $this, 'delete_cache' ] );
// filters
add_filter( 'sgo_js_minify_exclude', [ $this, 'exclude_script' ] );
add_filter( 'sgo_javascript_combine_exclude', [ $this, 'exclude_script' ] );
add_filter( 'sgo_javascript_combine_excluded_external_paths', [ $this, 'exclude_script' ] );
add_filter( 'sgo_javascript_combine_excluded_inline_content', [ $this, 'exclude_code' ] );
}
}
/**
* Exclude JavaScript file.
*
* @param array $excludes
* @return array
*/
function exclude_script( $excludes ) {
// add widget url
$excludes[] = basename( Cookie_Notice()->get_url( 'widget' ) );
// React admin asset exclusions — see Cookie_Notice::REACT_ADMIN_*.
// Defense-in-depth if SG Optimizer's JS minify / combine touches admin pages.
$excludes[] = Cookie_Notice::REACT_ADMIN_BUNDLE_BASENAME;
return $excludes;
}
/**
* Exclude JavaScript inline code.
*
* @param array $excludes
* @return array
*/
function exclude_code( $excludes ) {
// add widget inline code
$excludes[] = 'huOptions';
// React admin asset exclusions — see Cookie_Notice::REACT_ADMIN_*.
$excludes[] = Cookie_Notice::REACT_ADMIN_INLINE_KEYWORD;
return $excludes;
}
/**
* Delete all cache files.
*
* @return void
*/
public function delete_cache() {
if ( function_exists( 'sg_cachepress_purge_cache' ) )
sg_cachepress_purge_cache();
}
}
new Cookie_Notice_Modules_SpeedOptimizer();

View File

@@ -0,0 +1,64 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules SpeedyCache class.
*
* Compatibility since: 1.0.0
*
* @class Cookie_Notice_Modules_SpeedyCache
*/
class Cookie_Notice_Modules_SpeedyCache {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'plugins_loaded', [ $this, 'load_module' ], 11 );
}
/**
* Add compatibility to SpeedyCache plugin.
*
* @global object $speedycache
*
* @return void
*/
public function load_module() {
global $speedycache;
// check caching status
$cache_active = ! empty( $speedycache->options['status'] );
// update 2.4.17
if ( version_compare( Cookie_Notice()->db_version, '2.4.16', '<=' ) ) {
if ( $cache_active ) {
// clear cache
$this->delete_cache();
}
}
if ( $cache_active ) {
// delete cache files after updating settings or status
add_action( 'cn_configuration_updated', [ $this, 'delete_cache' ] );
}
}
/**
* Delete all cache files.
*
* @return void
*/
public function delete_cache() {
// clear cache
if ( function_exists( 'speedycache_delete_cache' ) )
speedycache_delete_cache( true );
}
}
new Cookie_Notice_Modules_SpeedyCache();

View File

@@ -0,0 +1,382 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules WooCommerce Privacy Consent class.
*
* Compatibility since: 4.0.4
*
* @class Cookie_Notice_Modules_WooCommerce_Privacy_Consent
*/
class Cookie_Notice_Modules_WooCommerce_Privacy_Consent {
private $defaults = [
'wc_registration_form' => [
'status' => false
],
'wc_checkout_form' => [
'status' => false
]
];
private $source = [];
private $registration_started = false;
private $registration_ended = false;
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// get main instance
$cn = Cookie_Notice();
$this->source = [
'name' => __( 'WooCommerce', 'cookie-notice' ),
'id' => 'woocommerce',
'id_type' => 'string',
'type' => 'static',
'availability' => cn_is_plugin_active( 'woocommerce', 'privacy-consent' ),
'status' => $cn->options['privacy_consent']['woocommerce_active'],
'status_type' => $cn->options['privacy_consent']['woocommerce_active_type'],
'forms' => [
'wc_registration_form' => [
'name' => __( 'Registration Form', 'cookie-notice' ),
'id' => 'wc_registration_form',
'type' => 'static',
'mode' => 'automatic',
'status' => false,
'logged_out_only' => true,
'fields' => [
'email' => ''
],
'subject' => [
'email' => 'email'
],
'preferences' => [],
'excluded' => []
],
'wc_checkout_form' => [
'name' => __( 'Checkout Form', 'cookie-notice' ),
'id' => 'wc_checkout_form',
'type' => 'static',
'mode' => 'automatic',
'status' => true,
'logged_out_only' => true,
'fields' => [
'email' => '',
'country' => '',
'first_name' => '',
'last_name' => '',
'address_1' => '',
'address_2' => '',
'postal_code' => '',
'city' => '',
'phone' => '',
'note' => ''
],
'subject' => [
'email' => '#email',
'first_name' => '#shipping-first_name',
'last_name' => '#shipping-last_name'
],
'preferences' => [
'terms' => '#terms-and-conditions'
],
'excluded' => []
]
]
];
// register source
$cn->privacy_consent->add_instance( $this, $this->source['id'] );
$cn->privacy_consent->add_source( $this->source );
add_action( 'admin_init', [ $this, 'register_source' ] );
// check compliance status
if ( $cn->get_status() !== 'active' )
return;
// registration
add_action( 'woocommerce_register_form', [ $this, 'register_form' ] );
add_action( 'wp_loaded', [ $this, 'registration_end' ], 21 );
add_filter( 'woocommerce_process_registration_errors', [ $this, 'registration_start' ], PHP_INT_MAX );
add_filter( 'woocommerce_registration_auth_new_customer', [ $this, 'registration_auth_new_customer' ], PHP_INT_MAX );
// checkout
add_action( 'woocommerce_new_order', [ $this, 'checkout_new_order' ], 10, 2 );
add_action( 'woocommerce_checkout_after_order_review', [ $this, 'checkout_form_classic' ] );
add_filter( 'render_block', [ $this, 'checkout_form_blocks' ], 10, 2 );
}
/**
* Register source.
*
* @return void
*/
public function register_source() {
register_setting(
'cookie_notice_privacy_consent_woocommerce',
'cookie_notice_privacy_consent_woocommerce',
[
'type' => 'array'
]
);
}
/**
* Validate source.
*
* @param array $input
*
* @return array
*/
public function validate( $input ) {
// get main instance
$cn = Cookie_Notice();
$input['woocommerce_active'] = isset( $input['woocommerce_active'] );
$input['woocommerce_active_type'] = isset( $input['woocommerce_active_type'] ) && array_key_exists( $input['woocommerce_active_type'], $cn->privacy_consent->form_active_types ) ? $input['woocommerce_active_type'] : $cn->defaults['privacy_consent']['woocommerce_active_type'];
return $input;
}
/**
* Check whether form exists.
*
* @param string $form_id
*
* @return bool
*/
public function form_exists( $form_id ) {
return array_key_exists( $form_id, $this->source['forms'] );
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function get_form( $args ) {
// invalid form?
if ( ! $this->form_exists( $args['form_id'] ) )
return [];
$form_data = $this->source['forms'][$args['form_id']];
$form = [
'source' => $this->source['id'],
'id' => $form_data['id'],
'title' => $form_data['name'],
'fields' => [
'subject' => $form_data['subject'],
'preferences' => $form_data['preferences']
]
];
if ( $args['form_id'] === 'wc_checkout_form' ) {
if ( $args['form_type'] === 'blocks' ) {
// force preferences
$form['preferences']['privacy'] = true;
} elseif ( $args['form_type'] === 'classic' ) {
// set default input names
$form['fields']['subject']['email'] = 'billing_email';
$form['fields']['subject']['first_name'] = 'billing_first_name';
$form['fields']['subject']['last_name'] = 'billing_last_name';
// add terms and conditions input name if needed
if ( wc_terms_and_conditions_checkbox_enabled() )
$form['fields']['preferences']['terms'] = 'terms';
else
unset( $form['fields']['preferences']['terms'] );
// force privacy policy
if ( wc_privacy_policy_page_id() && wc_get_privacy_policy_text( 'checkout' ) !== '' )
$form['preferences']['privacy'] = true;
}
} elseif ( $args['form_id'] === 'wc_registration_form' ) {
// force privacy policy
if ( wc_privacy_policy_page_id() && wc_get_privacy_policy_text( 'registration' ) !== '' )
$form['preferences']['privacy'] = true;
}
return $form;
}
/**
* Registration form.
*
* @return void
*/
public function register_form() {
$form_id = 'wc_registration_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id
] );
echo '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'.woocommerce-form-register\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
}
/**
* Registration start point.
*
* @param object $error
*
* @return object
*/
public function registration_start( $error ) {
if ( Cookie_Notice()->privacy_consent->is_form_active( 'wc_registration_form', $this->source['id'] ) ) {
// begin registration process
$this->registration_started = true;
}
return $error;
}
/**
* Registration end point #1.
*
* @param bool $force_login
*
* @return bool
*/
public function registration_auth_new_customer( $force_login ) {
if ( $this->registration_started ) {
$this->registration_ended = true;
Cookie_Notice()->privacy_consent->set_cookie( 'true' );
}
// if $force_login is true registration_end() will not be executed
return $force_login;
}
/**
* Registration end point #2.
*
* @return void
*/
public function registration_end() {
if ( $this->registration_started && ! $this->registration_ended )
Cookie_Notice()->privacy_consent->set_cookie( 'false' );
}
/**
* Checkout form.
*
* @return void
*/
public function checkout_form_classic() {
$form_id = 'wc_checkout_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id,
'form_type' => 'classic'
] );
echo '
<script>
if ( typeof huOptions !== \'undefined\' ) {
jQuery( document.body ).on( \'init_checkout\', function() {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'form.woocommerce-checkout\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
} );
}
</script>';
}
}
/**
* Add captcha to the checkout block.
*
* @param string|mixed $block_content
* @param array $block
*
* @return string
*/
public function checkout_form_blocks( $block_content, $block ) {
$block_content = (string) $block_content;
if ( $block['blockName'] !== 'woocommerce/checkout' )
return $block_content;
$form_id = 'wc_checkout_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id,
'form_type' => 'blocks'
] );
$block_content = '
<div class="wp-block" data-blockType="' . esc_attr( $block['blockName'] ) . '">
' . $block_content . '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = null;
// set empty node
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>
</div>';
}
return $block_content;
}
/**
* Checkout new order.
*
* @param int $order_id
* @param object $order
*
* @return void
*/
public function checkout_new_order( $order_id, $order ) {
// skip new admin area orders
if ( isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'update-order_' . $order_id ) )
return;
// get main instance
$cn = Cookie_Notice();
// is checkout enabled?
if ( $cn->privacy_consent->is_form_active( 'wc_checkout_form', $this->source['id'] ) ) {
if ( $order->get_billing_email() !== '' )
$cn->privacy_consent->set_cookie( 'true' );
}
}
}
new Cookie_Notice_Modules_WooCommerce_Privacy_Consent();

View File

@@ -0,0 +1,313 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules WordPress Privacy Consent class.
*
* Compatibility since: ?
*
* @class Cookie_Notice_Modules_WordPress_Privacy_Consent
*/
class Cookie_Notice_Modules_WordPress_Privacy_Consent {
private $defaults = [
'wp_registration_form' => [
'status' => true
],
'wp_comment_form' => [
'status' => true
]
];
private $source = [];
private $comment_form_active = false;
private $comment_passed_validation = false;
private $comment_validation = false;
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// get main instance
$cn = Cookie_Notice();
$this->source = [
'name' => __( 'WordPress', 'cookie-notice' ),
'id' => 'wordpress',
'id_type' => 'string',
'type' => 'static',
'availability' => true,
'status' => $cn->options['privacy_consent']['wordpress_active'],
'status_type' => $cn->options['privacy_consent']['wordpress_active_type'],
'forms' => [
'wp_registration_form' => [
'name' => __( 'Registration Form', 'cookie-notice' ),
'id' => 'wp_registration_form',
'type' => 'static',
'mode' => 'automatic',
'status' => false,
'logged_out_only' => true,
'fields' => [
'username' => '',
'email' => ''
],
'subject' => [
'email' => 'user_email',
'first_name' => 'user_login'
],
'preferences' => [],
'excluded' => []
],
'wp_comment_form' => [
'name' => __( 'Comment Form', 'cookie-notice' ),
'id' => 'wp_comment_form',
'type' => 'static',
'mode' => 'automatic',
'status' => false,
'logged_out_only' => true,
'fields' => [
'comment' => '',
'name' => '',
'email' => '',
'website' => ''
],
'subject' => [
'email' => 'email',
'first_name' => 'author'
],
'preferences' => [
'privacy' => 'wp-comment-cookies-consent'
],
'excluded' => []
]
]
];
// register source
$cn->privacy_consent->add_instance( $this, $this->source['id'] );
$cn->privacy_consent->add_source( $this->source );
add_action( 'admin_init', [ $this, 'register_source' ] );
// check compliance status
if ( $cn->get_status() !== 'active' )
return;
// comments
add_action( 'comment_form', [ $this, 'comment_form' ] );
add_action( 'comment_post', [ $this, 'comment_post' ], 10, 3 );
add_action( 'init', [ $this, 'comment_submission_start' ] );
add_action( 'shutdown', [ $this, 'comment_submission_end' ] );
// registration
add_action( 'register_form', [ $this, 'register_form' ] );
add_filter( 'registration_errors', [ $this, 'registration_errors' ], PHP_INT_MAX, 3 );
}
/**
* Register source.
*
* @return void
*/
public function register_source() {
register_setting(
'cookie_notice_privacy_consent_wordpress',
'cookie_notice_privacy_consent_wordpress',
[
'type' => 'array'
]
);
}
/**
* Validate source.
*
* @param array $input
*
* @return array
*/
public function validate( $input ) {
// get main instance
$cn = Cookie_Notice();
$input['wordpress_active'] = isset( $input['wordpress_active'] );
$input['wordpress_active_type'] = isset( $input['wordpress_active_type'] ) && array_key_exists( $input['wordpress_active_type'], $cn->privacy_consent->form_active_types ) ? $input['wordpress_active_type'] : $cn->defaults['privacy_consent']['wordpress_active_type'];
return $input;
}
/**
* Check whether form exists.
*
* @param string $form_id
*
* @return bool
*/
public function form_exists( $form_id ) {
return array_key_exists( $form_id, $this->source['forms'] );
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function get_form( $args ) {
// invalid form?
if ( ! $this->form_exists( $args['form_id'] ) )
return [];
$form_data = $this->source['forms'][$args['form_id']];
return [
'source' => $this->source['id'],
'id' => $form_data['id'],
'title' => $form_data['name'],
'fields' => [
'subject' => $form_data['subject'],
'preferences' => $form_data['preferences']
]
];
}
/**
* Comment form.
*
* @return void
*/
public function comment_form() {
$form_id = 'wp_comment_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id
] );
echo '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'[id="commentform"]\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
}
/**
* Mark comment as valid.
*
* @param array $comment_data
*
* @return array
*/
public function comment_post( $comment_id, $comment_approved, $comment_data ) {
if ( is_admin() )
return;
if ( ! $this->comment_form_active )
return;
if ( $this->comment_validation )
$this->comment_passed_validation = true;
}
/**
* Comment validation start process.
*
* @return void
*/
public function comment_submission_start() {
if ( is_admin() )
return;
$this->comment_form_active = Cookie_Notice()->privacy_consent->is_form_active( 'wp_comment_form', $this->source['id'] );
if ( ! $this->comment_form_active )
return;
// adding comment?
if ( basename( $_SERVER['PHP_SELF'] ) === 'wp-comments-post.php' ) {
$this->comment_validation = true;
$this->comment_passed_validation = false;
Cookie_Notice()->privacy_consent->set_cookie( 'false' );
}
}
/**
* Comment validation end process.
*
* @return void
*/
public function comment_submission_end() {
if ( is_admin() )
return;
if ( ! $this->comment_form_active )
return;
// adding comment?
if ( $this->comment_validation && $this->comment_passed_validation )
Cookie_Notice()->privacy_consent->set_cookie( 'true' );
}
/**
* Registration form.
*
* @return void
*/
public function register_form() {
$form_id = 'wp_registration_form';
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $form_id, $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $form_id
] );
echo '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'[id="registerform"]\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
}
/**
* Verify registration.
*
* @param object $errors
* @param string $user_login
* @param string $user_email
*
* @return object
*/
public function registration_errors( $errors, $user_login, $user_email ) {
// get main instance
$cn = Cookie_Notice();
// active registration form?
if ( $cn->privacy_consent->is_form_active( 'wp_registration_form', $this->source['id'] ) )
$cn->privacy_consent->set_cookie( $errors->has_errors() ? 'false' : 'true' );
return $errors;
}
}
new Cookie_Notice_Modules_WordPress_Privacy_Consent();

View File

@@ -0,0 +1,53 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules WP Fastest Cache class.
*
* Compatibility since: 1.0.0
*
* @class Cookie_Notice_Modules_WPFastestCache
*/
class Cookie_Notice_Modules_WPFastestCache {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'plugins_loaded', [ $this, 'load_module' ], 11 );
}
/**
* Add compatibility to WP Fastest Cache plugin.
*
* @return void
*/
public function load_module() {
// is caching enabled?
if ( isset( $GLOBALS['wp_fastest_cache_options']->wpFastestCacheStatus ) ) {
// update 2.4.10
if ( version_compare( Cookie_Notice()->db_version, '2.4.9', '<=' ) )
$this->delete_cache();
// delete cache files after updating settings or status
add_action( 'cn_configuration_updated', [ $this, 'delete_cache' ] );
}
}
/**
* Delete all cache files.
*
* @return void
*/
public function delete_cache() {
if ( isset( $GLOBALS['wp_fastest_cache'] ) && method_exists( $GLOBALS['wp_fastest_cache'], 'deleteCache' ) )
$GLOBALS['wp_fastest_cache']->deleteCache( false );
}
}
new Cookie_Notice_Modules_WPFastestCache();

View File

@@ -0,0 +1,54 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules WP-Optimize class.
*
* Compatibility since: 3.0.12
*
* @class Cookie_Notice_Modules_WPOptimize
*/
class Cookie_Notice_Modules_WPOptimize {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// has to be executed on plugins_loaded with priority 0
$this->load_module();
}
/**
* Add compatibility to WP-Optimize plugin.
*
* @return void
*/
public function load_module() {
// get wp-optimize configuration
if ( class_exists( 'WPO_Cache_Config' ) ) {
$options = WPO_Cache_Config::instance()->get();
// is caching enabled?
if ( ! empty( $options['enable_page_caching'] ) )
add_filter( 'wpo_purge_cache_hooks', [ $this, 'add_purge_cache' ] );
}
}
/**
* Add action when cache is purged.
*
* @param array $actions
* @return array
*/
public function add_purge_cache( $actions ) {
$actions[] = 'cn_configuration_updated';
return $actions;
}
}
new Cookie_Notice_Modules_WPOptimize();

View File

@@ -0,0 +1,97 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules WP Rocket class.
*
* Compatibility since: 3.8.0
*
* @class Cookie_Notice_Modules_WPRocket
*/
class Cookie_Notice_Modules_WPRocket {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'plugins_loaded', [ $this, 'load_module' ], 11 );
}
/**
* Add compatibility to WP Rocket plugin.
*
* @return void
*/
public function load_module() {
// update 2.4.17
if ( version_compare( Cookie_Notice()->db_version, '2.4.16', '<=' ) )
$this->delete_cache();
// delete cache files after updating settings or status
add_action( 'cn_configuration_updated', [ $this, 'delete_cache' ] );
// filters
add_filter( 'rocket_exclude_defer_js', [ $this, 'exclude_script' ] );
add_filter( 'rocket_exclude_js', [ $this, 'exclude_script' ] );
add_filter( 'rocket_delay_js_exclusions', [ $this, 'exclude_script' ] );
add_filter( 'rocket_delay_js_exclusions', [ $this, 'exclude_code' ] );
add_filter( 'rocket_defer_inline_exclusions', [ $this, 'exclude_code' ] );
add_filter( 'rocket_excluded_inline_js_content', [ $this, 'exclude_code' ] );
}
/**
* Delete all cache files.
*
* @return void
*/
public function delete_cache() {
// clear cache
if ( function_exists( 'rocket_clean_domain' ) )
rocket_clean_domain();
// clear minified css and js files
if ( function_exists( 'rocket_clean_minify' ) )
rocket_clean_minify( [ 'js', 'css' ] );
}
/**
* Exclude JavaScript file.
*
* @param array $excludes
* @return array
*/
function exclude_script( $excludes ) {
// add widget url
$excludes[] = basename( Cookie_Notice()->get_url( 'widget' ) );
// React admin asset exclusions — see Cookie_Notice::REACT_ADMIN_*.
// Defense-in-depth if Rocket's delay-JS / minify touches admin pages
// (e.g. via Cloudflare APO integrations).
$excludes[] = Cookie_Notice::REACT_ADMIN_BUNDLE_BASENAME;
return $excludes;
}
/**
* Exclude JavaScript inline code.
*
* @param array $excludes
* @return array
*/
function exclude_code( $excludes ) {
// add widget inline code
$excludes[] = 'huOptions';
// React admin asset exclusions — see Cookie_Notice::REACT_ADMIN_*.
$excludes[] = Cookie_Notice::REACT_ADMIN_INLINE_KEYWORD;
return $excludes;
}
}
new Cookie_Notice_Modules_WPRocket();

View File

@@ -0,0 +1,76 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules WP Super Cache class.
*
* Compatibility since: 1.6.3
*
* @class Cookie_Notice_Modules_WPSuperCache
*/
class Cookie_Notice_Modules_WPSuperCache {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// actions
add_action( 'init', [ $this, 'add_cookie' ] );
add_action( 'admin_init', [ $this, 'load_module' ] );
add_action( 'deactivated_cookie-notice/cookie-notice.php', [ $this, 'delete_cookie' ] );
}
/**
* Add compatibility to WP Super Cache plugin.
*
* @return void
*/
public function load_module() {
// bail if function is not available
if ( ! function_exists( 'wp_cache_is_enabled' ) )
return;
// is caching enabled?
if ( wp_cache_is_enabled() ) {
// delete cache files after updating settings or status
add_action( 'cn_configuration_updated', [ $this, 'delete_cache' ] );
}
}
/**
* Add hu-consent cookie.
*
* @return void
*/
public function add_cookie() {
do_action( 'wpsc_add_cookie', 'hu-consent' );
}
/**
* Delete hu-consent cookie.
*
* @return void
*/
public function delete_cookie() {
do_action( 'wpsc_delete_cookie', 'hu-consent' );
}
/**
* Delete all cache files.
*
* @return void
*/
public function delete_cache() {
if ( function_exists( 'wp_cache_clean_cache' ) ) {
global $file_prefix;
wp_cache_clean_cache( $file_prefix, true );
}
}
}
new Cookie_Notice_Modules_WPSuperCache();

View File

@@ -0,0 +1,225 @@
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* Cookie Notice Modules WPForms Privacy Consent class.
*
* Compatibility since: 1.6.0
*
* @class Cookie_Notice_Modules_WPForms_Privacy_Consent
*/
class Cookie_Notice_Modules_WPForms_Privacy_Consent {
private $defaults = [];
private $source = [];
/**
* Class constructor.
*
* @return void
*/
public function __construct() {
// get main instance
$cn = Cookie_Notice();
$this->source = [
'name' => __( 'WPForms', 'cookie-notice' ),
'id' => 'wpforms',
'id_type' => 'integer',
'type' => 'dynamic',
'availability' => cn_is_plugin_active( 'wpforms', 'privacy-consent' ),
'status' => $cn->options['privacy_consent']['wpforms_active'],
'status_type' => $cn->options['privacy_consent']['wpforms_active_type'],
'forms' => []
];
// register source
$cn->privacy_consent->add_instance( $this, $this->source['id'] );
$cn->privacy_consent->add_source( $this->source );
add_action( 'admin_init', [ $this, 'register_source' ] );
// check compliance status
if ( $cn->get_status() !== 'active' )
return;
// forms
add_action( 'wpforms_frontend_output', [ $this, 'wpforms_shortcode' ], 19, 5 );
}
/**
* Register source.
*
* @return void
*/
public function register_source() {
register_setting(
'cookie_notice_privacy_consent_wpforms',
'cookie_notice_privacy_consent_wpforms',
[
'type' => 'array'
]
);
}
/**
* Validate source.
*
* @param array $input
*
* @return array
*/
public function validate( $input ) {
// get main instance
$cn = Cookie_Notice();
$input['wpforms_active'] = isset( $input['wpforms_active'] );
$input['wpforms_active_type'] = isset( $input['wpforms_active_type'] ) && array_key_exists( $input['wpforms_active_type'], $cn->privacy_consent->form_active_types ) ? $input['wpforms_active_type'] : $cn->defaults['privacy_consent']['wpforms_active_type'];
return $input;
}
/**
* Check whether form exists.
*
* @param int $form_id
*
* @return bool
*/
public function form_exists( $form_id ) {
$query = new WP_Query( [
'p' => $form_id,
'post_status' => 'publish',
'post_type' => 'wpforms',
'fields' => 'ids',
'no_found_rows' => true
] );
return $query->have_posts();
}
/**
* Get forms.
*
* @param array $args
*
* @return array
*/
public function get_forms( $args ) {
// get only published forms
$query = new WP_Query( [
'post_status' => 'publish',
'post_type' => 'wpforms',
'order' => $args['order'],
'orderby' => $args['orderby'],
'fields' => 'all',
'posts_per_page' => 10,
'no_found_rows' => false,
'paged' => $args['page'],
's' => $args['search']
] );
$forms = [];
// any forms?
if ( ! empty( $query->posts ) ) {
foreach ( $query->posts as $post ) {
$forms[] = [
'id' => $post->ID,
'title' => $post->post_title,
'date' => $post->post_date,
'fields' => []
];
}
}
return [
'forms' => $forms,
'total' => $query->found_posts,
'max_pages' => $query->max_num_pages
];
}
/**
* Get form.
*
* @param array $args
*
* @return array
*/
public function get_form( $args ) {
// get only one form
$query = new WP_Query( [
'p' => (int) $args['form_id'],
'post_status' => 'publish',
'post_type' => 'wpforms',
'fields' => 'all',
'no_found_rows' => true
] );
// any forms?
if ( ! empty( $query->posts[0] ) ) {
$form = [
'source' => $this->source['id'],
'id' => $query->posts[0]->ID,
'title' => Cookie_Notice()->privacy_consent->strcut( sanitize_text_field( $query->posts[0]->post_title ), 100 ),
'fields' => [
'subject' => [
'first_name' => '',
'last_name' => ''
]
]
];
} else
$form = [];
return $form;
}
/**
* WPForms shortcode output.
*
* @param array|mixed $data
* @param null $deprecated
* @param bool $title
* @param bool $description
* @param array $errors
*
* @return void
*/
public function wpforms_shortcode( $data, $deprecated, $title, $description, $errors ) {
$data = (array) $data;
// active form?
if ( Cookie_Notice()->privacy_consent->is_form_active( $data['id'], $this->source['id'] ) ) {
// get form data
$form_data = $this->get_form( [
'form_id' => $data['id']
] );
echo '
<script>
if ( typeof huOptions !== \'undefined\' ) {
var huFormData = ' . wp_json_encode( $form_data ) . ';
var huFormNode = document.querySelector( \'[id="wpforms-' . (int) $data['id'] . '"] form\' );
var firstName = huFormNode.querySelector( \'input.wpforms-field-name-first\' );
var lastName = huFormNode.querySelector( \'input.wpforms-field-name-last\' );
if ( firstName )
huFormData[\'fields\'][\'subject\'][\'first_name\'] = firstName.getAttribute( \'name\' );
if ( lastName )
huFormData[\'fields\'][\'subject\'][\'last_name\'] = lastName.getAttribute( \'name\' );
huFormData[\'node\'] = huFormNode;
huOptions[\'forms\'].push( huFormData );
}
</script>';
}
}
}
new Cookie_Notice_Modules_WPForms_Privacy_Consent();