| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Banner; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use Cookiez\Classes\Module_Base; |
| 10 |
use Cookiez\Classes\Utils; |
| 11 |
use Cookiez\Modules\Banner\Dynamic_Tags\Preferences_Trigger; |
| 12 |
use Cookiez\Modules\Connect\Module as Connect_Module; |
| 13 |
use Cookiez\Modules\Cookie\Database\Cookie_Entry; |
| 14 |
use Cookiez\Modules\Settings\Classes\Sanitize_Content; |
| 15 |
use Cookiez\Modules\Settings\Classes\Settings; |
| 16 |
|
| 17 |
/** |
| 18 |
* Module `Banner` |
| 19 |
*/ |
| 20 |
class Module extends Module_Base { |
| 21 |
|
| 22 |
public const CONSENT_COOKIE_NAME = 'cookiez-user-consent'; |
| 23 |
|
| 24 |
private const SCANNER_USER_AGENT_MARKERS = [ |
| 25 |
'Cookiez/', |
| 26 |
'elementor.com', |
| 27 |
]; |
| 28 |
|
| 29 |
private array $page_cookies = []; |
| 30 |
|
| 31 |
public function get_name(): string { |
| 32 |
return 'Banner'; |
| 33 |
} |
| 34 |
|
| 35 |
public static function component_list(): array { |
| 36 |
return [ |
| 37 |
'Gutenberg_Preferences_Link_Block', |
| 38 |
'Script_Blocker', |
| 39 |
'Script_Blocker_Fallback', |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get service URL |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
public static function get_service_api_url(): string { |
| 48 |
return apply_filters( 'cookiez_service_api_url', 'https://my.elementor.com/apps/api/v1/cookiez-consent' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Enqueue Scripts and Styles |
| 53 |
*/ |
| 54 |
public function enqueue_scripts(): void { |
| 55 |
if ( Utils::is_elementor_editor() ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
Utils\Assets::enqueue_app_assets( 'banner', false ); |
| 60 |
|
| 61 |
$content = Settings::get( Settings::COOKIEZ_CONTENT ); |
| 62 |
$default_language = $content['languages'][0] ?? Sanitize_Content::FALLBACK_LANGUAGE; |
| 63 |
$disabled_languages = $content['disabledLanguages'] ?? []; |
| 64 |
|
| 65 |
$banner_settings = [ |
| 66 |
'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG, |
| 67 |
'isRTL' => is_rtl(), |
| 68 |
'language' => Sanitize_Content::resolve_language( get_locale(), $default_language, $disabled_languages ), |
| 69 |
'subscription' => get_option( Settings::SUBSCRIPTION_ID ), |
| 70 |
'planData' => get_option( Settings::PLAN_DATA ), |
| 71 |
'url' => Utils::get_current_page_url(), |
| 72 |
'serviceUrl' => self::get_service_api_url(), |
| 73 |
'settings' => Settings::get( Settings::COOKIEZ_SETTINGS ), |
| 74 |
'content' => $content, |
| 75 |
'cookies' => $this->page_cookies, |
| 76 |
'translations' => Utils::get_translations(), |
| 77 |
'showBanner' => self::should_show_banner(), |
| 78 |
'cookiesHash' => self::get_cookies_hash(), |
| 79 |
]; |
| 80 |
|
| 81 |
$banner_settings = apply_filters( 'cookiez/banner/settings', $banner_settings ); |
| 82 |
|
| 83 |
wp_localize_script( |
| 84 |
'banner', |
| 85 |
'cookiezBannerSettings', |
| 86 |
$banner_settings |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
public static function should_blocker_run(): bool { |
| 92 |
if ( self::is_cookiez_scanner_request() ) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! Connect_Module::is_connected() ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$settings = Settings::get( Settings::COOKIEZ_SETTINGS ); |
| 101 |
|
| 102 |
if ( array_key_exists( 'bannerDisplayStatus', $settings ) && ! $settings['bannerDisplayStatus'] ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
$disabled_pages = $settings['disableBannerPages'] ?? []; |
| 107 |
|
| 108 |
if ( empty( $disabled_pages ) || ! is_array( $disabled_pages ) ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
$current_url = home_url( add_query_arg( null, null ) ); |
| 113 |
|
| 114 |
foreach ( $disabled_pages as $url ) { |
| 115 |
if ( trailingslashit( $current_url ) === trailingslashit( $url ) ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
private static function is_cookiez_scanner_request(): bool { |
| 124 |
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
$ua = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ); |
| 129 |
|
| 130 |
foreach ( self::SCANNER_USER_AGENT_MARKERS as $marker ) { |
| 131 |
if ( ! str_contains( $ua, $marker ) ) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
// Check if cookies or cookies category was changed |
| 140 |
private function get_cookies_hash(): string { |
| 141 |
$signature = array_map( |
| 142 |
fn( $cookie ) => $cookie->name . ':' . $cookie->category, |
| 143 |
$this->page_cookies |
| 144 |
); |
| 145 |
sort( $signature ); |
| 146 |
|
| 147 |
return md5( implode( '|', $signature ) ); |
| 148 |
} |
| 149 |
|
| 150 |
private function should_show_banner(): bool { |
| 151 |
$settings = Settings::get( Settings::COOKIEZ_SETTINGS ); |
| 152 |
|
| 153 |
if ( array_key_exists( 'bannerDisplayStatus', $settings ) && ! $settings['bannerDisplayStatus'] ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
if ( empty( $_COOKIE[ self::CONSENT_COOKIE_NAME ] ) ) { |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
$raw = sanitize_text_field( wp_unslash( $_COOKIE[ self::CONSENT_COOKIE_NAME ] ) ); |
| 162 |
$decoded = json_decode( $raw, true ); |
| 163 |
|
| 164 |
if ( ! is_array( $decoded ) || empty( $decoded['meta'] ) ) { |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
$meta = $decoded['meta']; |
| 169 |
$expiration_days = $settings['consentExpiration'] ?? 180; |
| 170 |
$consent_time = $meta['timestamp'] ?? 0; |
| 171 |
$is_expired = ( time() - $consent_time ) > ( $expiration_days * DAY_IN_SECONDS ); |
| 172 |
|
| 173 |
if ( $is_expired ) { |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
$stored_hash = $meta['cookiesHash'] ?? ''; |
| 178 |
|
| 179 |
return $stored_hash !== $this->get_cookies_hash(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @param \Elementor\Core\DynamicTags\Manager $dynamic_tags_manager Elementor dynamic tags manager. |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function register_dynamic_tag( $dynamic_tags_manager ): void { |
| 187 |
if ( ! class_exists( '\Elementor\Core\DynamicTags\Data_Tag' ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$dynamic_tags_manager->register( new Preferences_Trigger() ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Registers Elementor Pro URL action handler (inline script on a dedicated handle). |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
public function enqueue_elementor_prefs_url_action(): void { |
| 200 |
if ( is_admin() ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
wp_add_inline_script( 'banner', " |
| 205 |
(function() { |
| 206 |
const registerCookiezPreferencesAction = () => { |
| 207 |
if ( ! window?.ElementorProFrontendConfig || ! window?.elementorFrontend?.utils?.urlActions ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
elementorFrontend.utils.urlActions.addAction( 'cookiezBanner:openPreferences', () => { |
| 212 |
window?.cookiezBanner?.screenManager?.openPreferences?.(); |
| 213 |
} ); |
| 214 |
}; |
| 215 |
|
| 216 |
const waitingLimit = 30; |
| 217 |
let retryCounter = 0; |
| 218 |
|
| 219 |
const waitForElementorPro = () => { |
| 220 |
return new Promise( ( resolve ) => { |
| 221 |
const intervalId = setInterval( () => { |
| 222 |
if ( retryCounter === waitingLimit ) { |
| 223 |
resolve( null ); |
| 224 |
} |
| 225 |
|
| 226 |
retryCounter++; |
| 227 |
|
| 228 |
if ( window.elementorFrontend && window?.elementorFrontend?.utils?.urlActions ) { |
| 229 |
clearInterval( intervalId ); |
| 230 |
resolve( window.elementorFrontend ); |
| 231 |
} |
| 232 |
}, 100 ); |
| 233 |
}); |
| 234 |
}; |
| 235 |
|
| 236 |
waitForElementorPro().then( () => { registerCookiezPreferencesAction(); } ); |
| 237 |
}()); |
| 238 |
" ); |
| 239 |
} |
| 240 |
|
| 241 |
public function __construct() { |
| 242 |
if ( self::should_blocker_run() ) { |
| 243 |
$this->register_components(); |
| 244 |
|
| 245 |
add_action( 'elementor/dynamic_tags/register', [ $this, 'register_dynamic_tag' ] ); |
| 246 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_elementor_prefs_url_action' ], 20 ); |
| 247 |
|
| 248 |
$this->page_cookies = Cookie_Entry::find_all(); |
| 249 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|