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