| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Modules\Settings\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Content_Resolver { |
| 10 |
|
| 11 |
private const ALLOWED_TEMPLATE_TYPES = [ 'opt-in', 'opt-out' ]; |
| 12 |
|
| 13 |
private const COOKIE_CATEGORIES = [ |
| 14 |
'necessary', |
| 15 |
'functional', |
| 16 |
'analytics', |
| 17 |
'advertising', |
| 18 |
'unclassified', |
| 19 |
]; |
| 20 |
|
| 21 |
private const BANNER_STRING_DEFAULTS = [ |
| 22 |
'title' => 'We use cookies', |
| 23 |
'cookieMessage' => 'We use cookies to improve your experience on this website. You may choose which types of cookies to allow and change your preferences at any time. Disabling cookies may impact your experience on this website. You can learn more by viewing our Cookie Policy.', |
| 24 |
'acceptAllLabel' => 'Accept All', |
| 25 |
'denyLabel' => 'Deny', |
| 26 |
'customizeLabel' => 'Customize', |
| 27 |
'cookiePolicyLinkText' => '', |
| 28 |
'cookiePolicyUrl' => '', |
| 29 |
'privacyPolicyLinkText' => '', |
| 30 |
'privacyPolicyUrl' => '', |
| 31 |
'doNotSellLinkText' => 'Do Not Sell or Share My Personal Information', |
| 32 |
]; |
| 33 |
|
| 34 |
private const BANNER_BOOL_DEFAULTS = [ |
| 35 |
'cookiePolicyLink' => false, |
| 36 |
'privacyPolicyLink' => false, |
| 37 |
]; |
| 38 |
|
| 39 |
private const OPT_IN_PREFERENCES_DEFAULTS = [ |
| 40 |
'header' => 'Customize consent preference', |
| 41 |
'privacyOverview' => 'We use cookies to improve your experience on this website. You may choose which types of cookies to allow and change your preferences at any time. Disabling cookies may impact your experience on this website. You can learn more by viewing our Cookie Policy.', |
| 42 |
'acceptAllLabel' => 'Accept all', |
| 43 |
'denyLabel' => 'Deny', |
| 44 |
'savePreferencesLabel' => 'Save preferences', |
| 45 |
]; |
| 46 |
|
| 47 |
private const OPT_OUT_DIALOG_DEFAULTS = [ |
| 48 |
'header' => 'Customize consent preference', |
| 49 |
'privacyOverview' => 'We use cookies to improve your experience on this website. You may choose which types of cookies to allow and change your preferences at any time. Disabling cookies may impact your experience on this website. You can learn more by viewing our Cookie Policy.', |
| 50 |
'denyLabel' => 'Deny', |
| 51 |
'savePreferencesLabel' => 'Save preferences', |
| 52 |
]; |
| 53 |
|
| 54 |
private const CATEGORY_DEFAULTS = [ |
| 55 |
'necessary' => [ |
| 56 |
'title' => 'Necessary', |
| 57 |
'description' => 'These cookies are essential for you to browse the website and use its features, such as accessing secure areas of the site or provide the basic services.', |
| 58 |
], |
| 59 |
'functional' => [ |
| 60 |
'title' => 'Functional', |
| 61 |
'description' => 'These cookies allow a website to remember choices you have made in the past, like what language you prefer, what region you would like weather reports for, or what your user name and password are so you can automatically log in.', |
| 62 |
], |
| 63 |
'analytics' => [ |
| 64 |
'title' => 'Analytical', |
| 65 |
'description' => 'These cookies collect information about how you use a website, like which pages you visited and which links you clicked on. None of this information can be used to identify you. It is all aggregated and, therefore, anonymized. Their sole purpose is to improve website functions.', |
| 66 |
], |
| 67 |
'advertising' => [ |
| 68 |
'title' => 'Advertisement', |
| 69 |
'description' => 'These cookies track your online activity to help advertisers deliver more relevant advertising or to limit how many times you see an ad. These cookies can share that information with other organizations or advertisers.', |
| 70 |
], |
| 71 |
'unclassified' => [ |
| 72 |
'title' => 'Uncategorized', |
| 73 |
'description' => 'These are cookies that are currently in the process of being classified together with other cookies.', |
| 74 |
], |
| 75 |
]; |
| 76 |
|
| 77 |
/** |
| 78 |
* @return array<string, string|bool> |
| 79 |
*/ |
| 80 |
public static function get_active_banner_fields( ?string $template_type_override = null ): array { |
| 81 |
$template_type = self::resolve_template_type( $template_type_override ); |
| 82 |
$template = self::get_active_template_for_type( $template_type ); |
| 83 |
|
| 84 |
$buttons = isset( $template['buttons'] ) && is_array( $template['buttons'] ) ? $template['buttons'] : []; |
| 85 |
$banner_options = isset( $template['banner-options'] ) && is_array( $template['banner-options'] ) ? $template['banner-options'] : []; |
| 86 |
|
| 87 |
$string_map = [ |
| 88 |
'title' => $template['title'] ?? '', |
| 89 |
'cookieMessage' => $template['cookie-message'] ?? '', |
| 90 |
'acceptAllLabel' => $buttons['accept-all'] ?? '', |
| 91 |
'denyLabel' => $buttons['deny'] ?? '', |
| 92 |
'customizeLabel' => $buttons['customize'] ?? '', |
| 93 |
'cookiePolicyLinkText' => $banner_options['cookie-policy-link-text'] ?? '', |
| 94 |
'cookiePolicyUrl' => $banner_options['cookie-policy-url'] ?? '', |
| 95 |
'privacyPolicyLinkText' => $banner_options['privacy-policy-link-text'] ?? '', |
| 96 |
'privacyPolicyUrl' => $banner_options['privacy-policy-url'] ?? '', |
| 97 |
'doNotSellLinkText' => $banner_options['do-not-sell-link-text'] ?? '', |
| 98 |
]; |
| 99 |
|
| 100 |
$out = []; |
| 101 |
foreach ( $string_map as $key => $value ) { |
| 102 |
$stored = is_string( $value ) && '' !== $value ? $value : null; |
| 103 |
$out[ $key ] = $stored ?? self::BANNER_STRING_DEFAULTS[ $key ]; |
| 104 |
} |
| 105 |
|
| 106 |
$out['cookiePolicyLink'] = isset( $banner_options['cookie-policy-link'] ) ? (bool) $banner_options['cookie-policy-link'] : self::BANNER_BOOL_DEFAULTS['cookiePolicyLink']; |
| 107 |
$out['privacyPolicyLink'] = isset( $banner_options['privacy-policy-link'] ) ? (bool) $banner_options['privacy-policy-link'] : self::BANNER_BOOL_DEFAULTS['privacyPolicyLink']; |
| 108 |
|
| 109 |
return $out; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @return array<string, string> |
| 114 |
*/ |
| 115 |
public static function get_active_preferences_fields( ?string $template_type_override = null ): array { |
| 116 |
$template_type = self::resolve_template_type( $template_type_override ); |
| 117 |
$template = self::get_active_template_for_type( $template_type ); |
| 118 |
|
| 119 |
$is_opt_out = 'opt-out' === $template_type; |
| 120 |
$source_key = $is_opt_out ? 'opt-out-dialog' : 'preferences'; |
| 121 |
$defaults = $is_opt_out ? self::OPT_OUT_DIALOG_DEFAULTS : self::OPT_IN_PREFERENCES_DEFAULTS; |
| 122 |
|
| 123 |
$raw = isset( $template[ $source_key ] ) && is_array( $template[ $source_key ] ) |
| 124 |
? $template[ $source_key ] |
| 125 |
: []; |
| 126 |
|
| 127 |
$wire_to_output = $is_opt_out |
| 128 |
? [ |
| 129 |
'header' => 'header', |
| 130 |
'privacy-overview' => 'privacyOverview', |
| 131 |
'deny-button' => 'denyLabel', |
| 132 |
'save-preferences' => 'savePreferencesLabel', |
| 133 |
] |
| 134 |
: [ |
| 135 |
'header' => 'header', |
| 136 |
'privacy-overview' => 'privacyOverview', |
| 137 |
'accept-button' => 'acceptAllLabel', |
| 138 |
'deny-button' => 'denyLabel', |
| 139 |
'save-preferences' => 'savePreferencesLabel', |
| 140 |
]; |
| 141 |
|
| 142 |
$out = $defaults; |
| 143 |
foreach ( $wire_to_output as $wire_key => $output_key ) { |
| 144 |
if ( isset( $raw[ $wire_key ] ) && '' !== $raw[ $wire_key ] ) { |
| 145 |
$out[ $output_key ] = is_string( $raw[ $wire_key ] ) ? $raw[ $wire_key ] : (string) $raw[ $wire_key ]; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $out; |
| 150 |
} |
| 151 |
|
| 152 |
public static function get_active_template_type( ?string $template_type_override = null ): string { |
| 153 |
return self::resolve_template_type( $template_type_override ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @return array<string, array{title: string, description: string}> |
| 158 |
*/ |
| 159 |
public static function get_active_category_fields( ?string $template_type_override = null ): array { |
| 160 |
$template_type = self::resolve_template_type( $template_type_override ); |
| 161 |
$template = self::get_active_template_for_type( $template_type ); |
| 162 |
|
| 163 |
$raw = isset( $template['cookie-categories'] ) && is_array( $template['cookie-categories'] ) |
| 164 |
? $template['cookie-categories'] |
| 165 |
: []; |
| 166 |
|
| 167 |
$out = []; |
| 168 |
foreach ( self::COOKIE_CATEGORIES as $category ) { |
| 169 |
$default = self::CATEGORY_DEFAULTS[ $category ]; |
| 170 |
$category_raw = isset( $raw[ $category ] ) && is_array( $raw[ $category ] ) ? $raw[ $category ] : []; |
| 171 |
|
| 172 |
$out[ $category ] = [ |
| 173 |
'title' => isset( $category_raw['title'] ) && '' !== $category_raw['title'] |
| 174 |
? (string) $category_raw['title'] |
| 175 |
: $default['title'], |
| 176 |
'description' => isset( $category_raw['description'] ) && '' !== $category_raw['description'] |
| 177 |
? (string) $category_raw['description'] |
| 178 |
: $default['description'], |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
return $out; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @return string[] |
| 187 |
*/ |
| 188 |
public static function get_cookie_category_keys(): array { |
| 189 |
return self::COOKIE_CATEGORIES; |
| 190 |
} |
| 191 |
|
| 192 |
private static function resolve_template_type( ?string $override ): string { |
| 193 |
if ( null !== $override && in_array( $override, self::ALLOWED_TEMPLATE_TYPES, true ) ) { |
| 194 |
return $override; |
| 195 |
} |
| 196 |
|
| 197 |
$settings = Settings::get( Settings::COOKIEZ_SETTINGS ); |
| 198 |
$template_type = $settings['templateType'] ?? 'opt-in'; |
| 199 |
|
| 200 |
return in_array( $template_type, self::ALLOWED_TEMPLATE_TYPES, true ) ? $template_type : 'opt-in'; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @return array<string, mixed> |
| 205 |
*/ |
| 206 |
private static function get_active_template_for_type( string $template_type ): array { |
| 207 |
$content_root = Settings::get( Settings::COOKIEZ_CONTENT ); |
| 208 |
$by_lang = isset( $content_root['content'] ) && is_array( $content_root['content'] ) |
| 209 |
? $content_root['content'] |
| 210 |
: []; |
| 211 |
$lang_data = self::resolve_language_block( $by_lang ); |
| 212 |
|
| 213 |
return isset( $lang_data[ $template_type ] ) && is_array( $lang_data[ $template_type ] ) |
| 214 |
? $lang_data[ $template_type ] |
| 215 |
: []; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @param array<string, mixed> $by_lang |
| 220 |
* @return array<string, mixed> |
| 221 |
*/ |
| 222 |
private static function resolve_language_block( array $by_lang ): array { |
| 223 |
$locale = get_locale(); |
| 224 |
|
| 225 |
if ( isset( $by_lang[ $locale ] ) && is_array( $by_lang[ $locale ] ) ) { |
| 226 |
return $by_lang[ $locale ]; |
| 227 |
} |
| 228 |
|
| 229 |
$short = strlen( $locale ) > 2 ? substr( $locale, 0, 2 ) : $locale; |
| 230 |
if ( isset( $by_lang[ $short ] ) && is_array( $by_lang[ $short ] ) ) { |
| 231 |
return $by_lang[ $short ]; |
| 232 |
} |
| 233 |
|
| 234 |
if ( isset( $by_lang['en'] ) && is_array( $by_lang['en'] ) ) { |
| 235 |
return $by_lang['en']; |
| 236 |
} |
| 237 |
|
| 238 |
return []; |
| 239 |
} |
| 240 |
} |
| 241 |
|