PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / trunk
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager vtrunk
0.0.11 0.0.10 0.0.9 0.0.8 0.0.7 0.0.6 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5
cookiez / classes / utils.php

utils.php in Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager trunk, at classes/utils.php

138 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Cookiez\Classes;
4
5 use Cookiez\Classes\Services\Client;
6 use Cookiez\Modules\Banner\Module as Banner_Module;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Utils {
13 /**
14 * get_elementor
15 *
16 * @param bool $instance
17 *
18 * @return \Elementor\Plugin|false|null
19 */
20 public static function get_elementor( bool $instance = false ) {
21 static $_instance = null;
22
23 if ( false !== $instance ) {
24 $_instance = $instance;
25
26 return $instance;
27 }
28
29 if ( null !== $_instance ) {
30 return $_instance;
31 }
32
33 if ( class_exists( 'Elementor\Plugin' ) ) {
34 return \Elementor\Plugin::instance(); // @codeCoverageIgnore
35 }
36
37 return false;
38 }
39
40 public static function is_elementor_editor(): bool {
41 if ( ! did_action( 'elementor/loaded' ) ) {
42 return false;
43 }
44
45 $elementor = self::get_elementor();
46
47 if ( ! $elementor ) {
48 return false;
49 }
50
51 if ( isset( $elementor->editor ) && $elementor->editor->is_edit_mode() ) {
52 return true;
53 }
54
55 if ( isset( $elementor->preview ) && $elementor->preview->is_preview_mode() ) {
56 return true;
57 }
58
59 return false;
60 }
61
62 public static function get_api_client(): ?Client {
63 return Client::get_instance();
64 }
65
66 public static function is_plugin_page(): bool {
67 $current_screen = get_current_screen();
68
69 return str_contains( $current_screen->id, '_page_cookiez-settings' );
70 }
71
72 public static function get_translations(): array {
73 return [
74 'cookiez' => esc_html__( 'Cookie Consent', 'cookiez' ),
75 'cookies' => esc_html__( 'Cookies', 'cookiez' ),
76 'close' => esc_html__( 'Close', 'cookiez' ),
77 'cookie' => esc_html__( 'Cookie:', 'cookiez' ),
78 'duration' => esc_html__( 'Duration:', 'cookiez' ),
79 'description' => esc_html__( 'Description:', 'cookiez' ),
80 'noCookiesInCategory' => esc_html__( 'No cookies currently in this category.', 'cookiez' ),
81 /* translators: %s is a cookie category title (e.g. "Functional"). */
82 'enableCategoryCookies' => esc_html__( 'Enable %s cookies', 'cookiez' ),
83 'alwaysActive' => esc_html__( 'Always active', 'cookiez' ),
84 ];
85 }
86
87 /**
88 * get the current page url
89 */
90 public static function get_current_page_url(): ?string {
91 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
92 $path = wp_parse_url( $request_uri, PHP_URL_PATH ); // removes query string
93 return rtrim( home_url( $path ), '/' );
94 }
95
96 public static function parse_consent_cookie(): ?array {
97 if ( empty( $_COOKIE[ Banner_Module::CONSENT_COOKIE_NAME ] ) ) {
98 return null;
99 }
100
101 $raw = sanitize_text_field( wp_unslash( $_COOKIE[ Banner_Module::CONSENT_COOKIE_NAME ] ) );
102 $decoded = json_decode( $raw, true );
103
104 if ( ! is_array( $decoded ) || empty( $decoded['data']['consent'] ) || ! is_array( $decoded['data']['consent'] ) ) {
105 return null;
106 }
107
108 return $decoded['data']['consent'];
109 }
110
111 /**
112 * Checks whether $value is a syntactically valid cookie domain.
113 */
114 public static function is_valid_cookie_domain( $value ): bool {
115 if ( ! is_string( $value ) ) {
116 return false;
117 }
118
119 $candidate = str_starts_with( $value, '.' ) ? substr( $value, 1 ) : $value;
120
121 return false !== filter_var( $candidate, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME )
122 && false !== strpos( $candidate, '.' )
123 && false === filter_var( $candidate, FILTER_VALIDATE_IP );
124 }
125
126 /**
127 * Checks whether $value is a syntactically valid cookie name per RFC 6265 §4.1.1
128 * (token from RFC 7230). Allowed characters: ALPHA / DIGIT / ! # $ % & ' * + - . ^ _ ` | ~
129 */
130 public static function is_valid_cookie_name( $value ): bool {
131 if ( ! is_string( $value ) || '' === $value ) {
132 return false;
133 }
134
135 return 1 === preg_match( "/^[!#$%&'*+\-.^_`|~A-Za-z0-9]+$/", $value );
136 }
137 }
138