| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Presentation; |
| 4 |
|
| 5 |
use AgeGate\Common\Settings; |
| 6 |
use Asylum\Utility\StringTemplate; |
| 7 |
|
| 8 |
trait ClassNames |
| 9 |
{ |
| 10 |
protected static $formatted = false; |
| 11 |
|
| 12 |
public static $attributes = [ |
| 13 |
'age-gate-html' => [ |
| 14 |
'class' => 'age-gate{element}restricted age-gate{element}restricted{modifier}standard' |
| 15 |
], |
| 16 |
'age-gate-body' => [ |
| 17 |
'class' => 'age-restriction' |
| 18 |
], |
| 19 |
'age-gate-headline' => [ |
| 20 |
'class' => 'age-gate{element}headline' |
| 21 |
], |
| 22 |
'age-gate-wrapper' => [ |
| 23 |
'class' => 'age-gate{element}wrapper' |
| 24 |
], |
| 25 |
'age-gate-background-colour' => [ |
| 26 |
'class' => 'age-gate{element}background-color' |
| 27 |
], |
| 28 |
'age-gate-background' => [ |
| 29 |
'class' => 'age-gate{element}background' |
| 30 |
], |
| 31 |
'age-gate-loader' => [ |
| 32 |
'class' => 'age-gate{element}loader' |
| 33 |
], |
| 34 |
'age-gate-loading-icon' => [ |
| 35 |
'class' => 'age-gate{element}loading-icon' |
| 36 |
], |
| 37 |
'age-gate' => [ |
| 38 |
'class' => 'age-gate' |
| 39 |
], |
| 40 |
'age-gate-shortcode' => [ |
| 41 |
'class' => 'age-gate age-gate{modifier}shortcode' |
| 42 |
], |
| 43 |
'age-gate-shortcode-inner' => [ |
| 44 |
'class' => 'age-gate{element}shortcode{element}inner' |
| 45 |
], |
| 46 |
'age-gate-form' => [ |
| 47 |
'class' => 'age-gate{element}form' |
| 48 |
], |
| 49 |
'age-gate-errors' => [ |
| 50 |
'class' => 'age-gate{element}errors' |
| 51 |
], |
| 52 |
'age-gate-error' => [ |
| 53 |
'class' => 'age-gate{element}error' |
| 54 |
], |
| 55 |
'age-gate-error-message' => [ |
| 56 |
'class' => 'age-gate{element}error-message' |
| 57 |
], |
| 58 |
'age-gate-remember-wrapper' => [ |
| 59 |
'class' => 'age-gate{element}remember-wrapper' |
| 60 |
], |
| 61 |
'age-gate-remember' => [ |
| 62 |
'class' => 'age-gate{element}remember' |
| 63 |
], |
| 64 |
'age-gate-remember-checkbox' => [ |
| 65 |
'type' => 'checkbox', |
| 66 |
'class' => 'age-gate{element}remember-field', |
| 67 |
'name' => 'age_gate[remember]', |
| 68 |
'value' => 1 |
| 69 |
], |
| 70 |
'age-gate-remember-text' => [ |
| 71 |
'class' => 'age-gate{element}remember-text', |
| 72 |
], |
| 73 |
'age-gate-submit-section' => [ |
| 74 |
'class' => 'age-gate{element}submit' |
| 75 |
], |
| 76 |
'age-gate-submit' => [ |
| 77 |
'class' => 'age-gate{element}button' |
| 78 |
], |
| 79 |
'age-gate-additional-information' => [ |
| 80 |
'class' => 'age-gate{element}additional-information' |
| 81 |
], |
| 82 |
'age-gate-form-fields' => [ |
| 83 |
'class' => 'age-gate{element}fields' |
| 84 |
], |
| 85 |
'age-gate-form-elements' => [ |
| 86 |
'class' => 'age-gate{element}form-elements' |
| 87 |
], |
| 88 |
'age-gate-form-section' => [ |
| 89 |
'class' => 'age-gate{element}form-section' |
| 90 |
], |
| 91 |
'age-gate-d-label' => [ |
| 92 |
'class' => 'age-gate{element}label age-gate{element}label{modifier}day', |
| 93 |
'for' => 'age-gate-d' |
| 94 |
], |
| 95 |
'age-gate-m-label' => [ |
| 96 |
'class' => 'age-gate{element}label age-gate{element}label{modifier}month', |
| 97 |
'for' => 'age-gate-m' |
| 98 |
], |
| 99 |
'age-gate-y-label' => [ |
| 100 |
'class' => 'age-gate{element}label age-gate{element}label{modifier}year', |
| 101 |
'for' => 'age-gate-y' |
| 102 |
], |
| 103 |
'age-gate-d-input' => [ |
| 104 |
'class' => 'age-gate{element}input age-gate{element}input{modifier}day', |
| 105 |
'type' => "text", |
| 106 |
'name' => "age_gate[d]", |
| 107 |
'id' => "age-gate-d", |
| 108 |
'maxlength' => "2", |
| 109 |
'pattern' => "[0-9]*" , |
| 110 |
'inputmode' => "numeric" , |
| 111 |
'autocomplete' => "off", |
| 112 |
], |
| 113 |
'age-gate-m-input' => [ |
| 114 |
'class' => 'age-gate{element}input age-gate{element}input{modifier}month', |
| 115 |
'type' => "text", |
| 116 |
'name' => "age_gate[m]", |
| 117 |
'id' => "age-gate-m", |
| 118 |
'maxlength' => "2", |
| 119 |
'pattern' => "[0-9]*", |
| 120 |
'inputmode' => "numeric", |
| 121 |
'autocomplete' => "off", |
| 122 |
], |
| 123 |
'age-gate-y-input' => [ |
| 124 |
'class' => 'age-gate{element}input age-gate{element}input{modifier}year', |
| 125 |
'type' => "text", |
| 126 |
'name' => "age_gate[y]", |
| 127 |
'id' => "age-gate-y", |
| 128 |
'minlength' => "4", |
| 129 |
'maxlength' => "4", |
| 130 |
'pattern' => "[0-9]*", |
| 131 |
'inputmode' => "numeric", |
| 132 |
'autocomplete' => "off", |
| 133 |
], |
| 134 |
'age-gate-d-select' => [ |
| 135 |
'class' => 'age-gate{element}select', |
| 136 |
'name' => 'age_gate[d]', |
| 137 |
'id' => 'age-gate-d', |
| 138 |
], |
| 139 |
'age-gate-m-select' => [ |
| 140 |
'class' => 'age-gate{element}select', |
| 141 |
'name' => 'age_gate[m]', |
| 142 |
'id' => 'age-gate-m', |
| 143 |
], |
| 144 |
'age-gate-y-select' => [ |
| 145 |
'class' => 'age-gate{element}select', |
| 146 |
'name' => 'age_gate[y]', |
| 147 |
'id' => 'age-gate-y', |
| 148 |
], |
| 149 |
'age-gate-challenge' => [ |
| 150 |
'class' => 'age-gate{element}challenge' |
| 151 |
], |
| 152 |
'age-gate-buttons' => [ |
| 153 |
'class' => 'age-gate{element}buttons', |
| 154 |
], |
| 155 |
'age-gate-submit-yes' => [ |
| 156 |
'class' => 'age-gate{element}submit age-gate{element}submit{modifier}yes', |
| 157 |
'data-submit' => 'yes', |
| 158 |
'value' => 1, |
| 159 |
"name" => 'age_gate[confirm]' |
| 160 |
], |
| 161 |
'age-gate-submit-no' => [ |
| 162 |
'class' => 'age-gate{element}submit age-gate{element}submit{modifier}no', |
| 163 |
'data-submit' => 'no', |
| 164 |
'value' => 0, |
| 165 |
"name" => 'age_gate[confirm]' |
| 166 |
], |
| 167 |
'age-gate-heading' => [ |
| 168 |
'class' => 'age-gate{element}heading' |
| 169 |
], |
| 170 |
'age-gate-heading-title' => [ |
| 171 |
'class' => 'age-gate{element}heading-title' |
| 172 |
], |
| 173 |
'age-gate-logo' => [ |
| 174 |
'class' => 'age-gate{element}heading-title age-gate{element}heading-title{modifier}logo' |
| 175 |
], |
| 176 |
'age-gate-logo-image' => [ |
| 177 |
'class' => 'age-gate{element}logo-image' |
| 178 |
], |
| 179 |
'age-gate-subheadline' => [ |
| 180 |
'class' => 'age-gate{element}subheadline' |
| 181 |
], |
| 182 |
'age-gate-video' => [ |
| 183 |
'class' => 'age-gate{element}video' |
| 184 |
], |
| 185 |
'age-gate-extra' => [ |
| 186 |
'class' => 'age-gate{element}extra' |
| 187 |
], |
| 188 |
'age-gate-template' => [ |
| 189 |
'class' => '', |
| 190 |
] |
| 191 |
]; |
| 192 |
|
| 193 |
public static $disallowedAttributes = [ |
| 194 |
'type', |
| 195 |
'value', |
| 196 |
'pattern', |
| 197 |
'name', |
| 198 |
'inputmode', |
| 199 |
'autocomplete', |
| 200 |
'id', |
| 201 |
'for', |
| 202 |
'minlength', |
| 203 |
'maxlength', |
| 204 |
'required', |
| 205 |
'data-error-field', |
| 206 |
'method', |
| 207 |
'action', |
| 208 |
'onclick', |
| 209 |
'onload', |
| 210 |
'onerror', |
| 211 |
'disabled', |
| 212 |
'accept', |
| 213 |
'accept-charset', |
| 214 |
'action', |
| 215 |
'align', |
| 216 |
'allow', |
| 217 |
'as', |
| 218 |
'async', |
| 219 |
'autocapitalize', |
| 220 |
'autocomplete', |
| 221 |
'autoplay', |
| 222 |
'bgcolor', |
| 223 |
'border', |
| 224 |
'capture', |
| 225 |
'charset', |
| 226 |
'checked', |
| 227 |
'cite', |
| 228 |
'color', |
| 229 |
'cols', |
| 230 |
'colspan', |
| 231 |
'content', |
| 232 |
'contenteditable', |
| 233 |
'controls', |
| 234 |
'coords', |
| 235 |
'crossorigin', |
| 236 |
'csp', |
| 237 |
'datetime', |
| 238 |
'decoding', |
| 239 |
'default', |
| 240 |
'defer', |
| 241 |
'dir', |
| 242 |
'dirname', |
| 243 |
'disabled', |
| 244 |
'download', |
| 245 |
'draggable', |
| 246 |
'enctype', |
| 247 |
'enterkeyhint', |
| 248 |
'for', |
| 249 |
'form', |
| 250 |
'formaction', |
| 251 |
'headers', |
| 252 |
'height', |
| 253 |
'hidden', |
| 254 |
'high', |
| 255 |
'href', |
| 256 |
'hreflang', |
| 257 |
'http-equiv', |
| 258 |
'id', |
| 259 |
'integrity', |
| 260 |
'intrinsicsize', |
| 261 |
'inputmode', |
| 262 |
'ismap', |
| 263 |
'itemprop', |
| 264 |
'kind', |
| 265 |
'label', |
| 266 |
'lang', |
| 267 |
'language', |
| 268 |
'loading', |
| 269 |
'list', |
| 270 |
'loop', |
| 271 |
'low', |
| 272 |
'media', |
| 273 |
'method', |
| 274 |
'multiple', |
| 275 |
'muted', |
| 276 |
'name', |
| 277 |
'novalidate', |
| 278 |
'open', |
| 279 |
'optimum', |
| 280 |
'pattern', |
| 281 |
'ping', |
| 282 |
'playsinline', |
| 283 |
'poster', |
| 284 |
'preload', |
| 285 |
'readonly', |
| 286 |
'referrerpolicy', |
| 287 |
'required', |
| 288 |
'reversed', |
| 289 |
'rows', |
| 290 |
'rowspan', |
| 291 |
'sandbox', |
| 292 |
'scope', |
| 293 |
'scoped', |
| 294 |
'selected', |
| 295 |
'shape', |
| 296 |
'size', |
| 297 |
'sizes', |
| 298 |
'slot', |
| 299 |
'span', |
| 300 |
'spellcheck', |
| 301 |
'src', |
| 302 |
'srcdoc', |
| 303 |
'srclang', |
| 304 |
'srcset', |
| 305 |
'start', |
| 306 |
'step', |
| 307 |
'style', |
| 308 |
'summary', |
| 309 |
'target', |
| 310 |
'translate', |
| 311 |
'type', |
| 312 |
'usemap', |
| 313 |
'value', |
| 314 |
'width', |
| 315 |
'wrap', |
| 316 |
'formenctype', |
| 317 |
'formmethod', |
| 318 |
'formnovalidate', |
| 319 |
'formtarget', |
| 320 |
]; |
| 321 |
|
| 322 |
protected static $validAttributes = [ |
| 323 |
'accesskey', |
| 324 |
'alt', |
| 325 |
'background', |
| 326 |
'class', |
| 327 |
'data', |
| 328 |
'data-*', |
| 329 |
'max', |
| 330 |
'maxlength', |
| 331 |
'minlength', |
| 332 |
'min', |
| 333 |
'placeholder', |
| 334 |
'rel', |
| 335 |
'role', |
| 336 |
'tabindex', |
| 337 |
'title', |
| 338 |
'aria-autocomplete', |
| 339 |
'aria-checked', |
| 340 |
'aria-disabled', |
| 341 |
'aria-errormessage', |
| 342 |
'aria-expanded', |
| 343 |
'aria-haspopup', |
| 344 |
'aria-hidden', |
| 345 |
'aria-invalid', |
| 346 |
'aria-label', |
| 347 |
'aria-level', |
| 348 |
'aria-modal', |
| 349 |
'aria-multiline', |
| 350 |
'aria-multiselectable', |
| 351 |
'aria-orientation', |
| 352 |
'aria-placeholder', |
| 353 |
'aria-pressed', |
| 354 |
'aria-readonly', |
| 355 |
'aria-required', |
| 356 |
'aria-selected', |
| 357 |
'aria-sort', |
| 358 |
'aria-valuemax', |
| 359 |
'aria-valuemin', |
| 360 |
'aria-valuenow', |
| 361 |
'aria-valuetext', |
| 362 |
'aria-busy', |
| 363 |
'aria-live', |
| 364 |
'aria-relevant', |
| 365 |
'aria-atomic', |
| 366 |
'aria-activedescendant', |
| 367 |
'aria-colcount', |
| 368 |
'aria-colindex', |
| 369 |
'aria-colspan', |
| 370 |
'aria-controls', |
| 371 |
'aria-describedby', |
| 372 |
'aria-description', |
| 373 |
'aria-details', |
| 374 |
'aria-errormessage', |
| 375 |
'aria-flowto', |
| 376 |
'aria-labelledby', |
| 377 |
'aria-owns', |
| 378 |
'aria-posinset', |
| 379 |
'aria-rowcount', |
| 380 |
'aria-rowindex', |
| 381 |
'aria-rowspan', |
| 382 |
'aria-setsize', |
| 383 |
'aria-atomic', |
| 384 |
'aria-busy', |
| 385 |
'aria-controls', |
| 386 |
'aria-current', |
| 387 |
'aria-describedby', |
| 388 |
'aria-description', |
| 389 |
'aria-details', |
| 390 |
'aria-disabled', |
| 391 |
'aria-dropeffect', |
| 392 |
'aria-errormessage', |
| 393 |
'aria-flowto', |
| 394 |
'aria-grabbed', |
| 395 |
'aria-haspopup', |
| 396 |
'aria-hidden', |
| 397 |
'aria-invalid', |
| 398 |
'aria-keyshortcuts', |
| 399 |
'aria-label', |
| 400 |
'aria-labelledby', |
| 401 |
'aria-live', |
| 402 |
'aria-owns', |
| 403 |
'aria-relevant', |
| 404 |
'aria-roledescription', |
| 405 |
]; |
| 406 |
|
| 407 |
public static $months = [ |
| 408 |
'01' => 'January', |
| 409 |
'02' => 'February', |
| 410 |
'03' => 'March', |
| 411 |
'04' => 'April', |
| 412 |
'05' => 'May', |
| 413 |
'06' => 'June', |
| 414 |
'07' => 'July', |
| 415 |
'08' => 'August', |
| 416 |
'09' => 'September', |
| 417 |
'10' => 'October', |
| 418 |
'11' => 'November', |
| 419 |
'12' => 'December' |
| 420 |
]; |
| 421 |
|
| 422 |
public static function addAttribute(string $element, $attribute, string $value): void |
| 423 |
{ |
| 424 |
if (in_array($attribute, self::$disallowedAttributes)) { |
| 425 |
_doing_it_wrong('age_gate_add_attribute', esc_html(wp_sprintf('`%s` may not be assigned', $attribute)), esc_html(AGE_GATE_VERSION)); |
| 426 |
return; |
| 427 |
// wp_die('Disallowed attribute: <b>"' . $attribute . '"</b> on <b>"' . $element . '"</b>'); |
| 428 |
} |
| 429 |
|
| 430 |
if (!in_array($attribute, self::$validAttributes) && strpos($attribute, 'data-') !== 0) { |
| 431 |
_doing_it_wrong('age_gate_add_attribute', esc_html(wp_sprintf('Invalid attribute', $attribute)), esc_html(AGE_GATE_VERSION)); |
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
if (is_array($attribute)) { |
| 436 |
foreach ($attribute as $k => $value) { |
| 437 |
self::addAttribute($element, $k, $value); |
| 438 |
} |
| 439 |
} else { |
| 440 |
|
| 441 |
if (!isset(self::$attributes[$element])) { |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
$attribute = sanitize_text_field( $attribute); |
| 446 |
|
| 447 |
if (isset(self::$attributes[$element][$attribute])) { |
| 448 |
if (strpos(self::$attributes[$element][$attribute], $value) === false) { |
| 449 |
self::$attributes[$element][$attribute] = self::$attributes[$element][$attribute] . ' ' . $value; |
| 450 |
} |
| 451 |
} else { |
| 452 |
self::$attributes[$element][$attribute] = $value; |
| 453 |
}; |
| 454 |
} |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
public static function getAttribute($element) |
| 459 |
{ |
| 460 |
if (isset(self::$attributes[$element])) { |
| 461 |
return html_build_attributes(self::$attributes[$element]); |
| 462 |
} else { |
| 463 |
return html_build_attributes(['class' => $element]); |
| 464 |
} |
| 465 |
// return self::$attributes[$element]; |
| 466 |
} |
| 467 |
|
| 468 |
public static function attr($element, $echo = true) |
| 469 |
{ |
| 470 |
if (!self::$formatted) { |
| 471 |
self::formatClasses(); |
| 472 |
} |
| 473 |
|
| 474 |
if ($echo) { |
| 475 |
echo self::getAttribute($element); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 476 |
return; |
| 477 |
} |
| 478 |
|
| 479 |
return self::getAttribute($element); |
| 480 |
} |
| 481 |
|
| 482 |
public static function formatClasses() |
| 483 |
{ |
| 484 |
$settings = Settings::getInstance(); |
| 485 |
|
| 486 |
$replacements = [ |
| 487 |
'element' => ($settings->cssType === 'v2' ? '-' : '__'), |
| 488 |
'modifier' => ($settings->cssType === 'v2' ? '-' : '--'), |
| 489 |
]; |
| 490 |
|
| 491 |
$engine = new StringTemplate(); |
| 492 |
|
| 493 |
foreach (self::$attributes as $key => $attribute) { |
| 494 |
if ($attribute['class']) { |
| 495 |
self::$attributes[$key]['class'] = $engine->render($attribute['class'], $replacements); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
self::$formatted = true; |
| 500 |
} |
| 501 |
|
| 502 |
public static function formatMessage($str, $replacements = []) |
| 503 |
{ |
| 504 |
$engine = new StringTemplate(); |
| 505 |
return $engine->render($str, $replacements); |
| 506 |
} |
| 507 |
|
| 508 |
public static function getSingleAttribute($element, $attribute) |
| 509 |
{ |
| 510 |
return self::$attributes[$element][$attribute] ?? false; |
| 511 |
} |
| 512 |
} |
| 513 |
|