| 1 |
<?php |
| 2 |
|
| 3 |
namespace AgeGate\Presentation; |
| 4 |
|
| 5 |
use Asylum\Utility\Color; |
| 6 |
use Asylum\Utility\Storage; |
| 7 |
use AgeGate\Common\Settings; |
| 8 |
|
| 9 |
class Form |
| 10 |
{ |
| 11 |
private $settings; |
| 12 |
private $custom = 'age-gate-custom'; |
| 13 |
private $options = 'age-gate-options'; |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
$this->settings = Settings::getInstance(); |
| 18 |
} |
| 19 |
|
| 20 |
public function optionStyle() |
| 21 |
{ |
| 22 |
wp_register_style($this->custom, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 23 |
wp_add_inline_style($this->custom, wp_strip_all_tags(stripslashes(html_entity_decode($this->getStyleOptions(), ENT_QUOTES)))); |
| 24 |
|
| 25 |
return $this; |
| 26 |
} |
| 27 |
|
| 28 |
public function customStyle() |
| 29 |
{ |
| 30 |
if (!trim($this->settings->css)) { |
| 31 |
return $this; |
| 32 |
} |
| 33 |
|
| 34 |
if ($this->settings->cssFile && file_exists(Storage::storageDir('css', 'age-gate') . '/custom.css')) { |
| 35 |
$deps = []; |
| 36 |
|
| 37 |
if ($this->settings->enqueueCss) { |
| 38 |
$deps = ['age-gate']; |
| 39 |
} |
| 40 |
|
| 41 |
wp_register_style($this->custom, Storage::storageUrl('css', 'age-gate') . '/custom.css', $deps, AGE_GATE_VERSION); |
| 42 |
} else { |
| 43 |
wp_register_style($this->custom, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 44 |
|
| 45 |
wp_add_inline_style($this->custom, wp_strip_all_tags(stripslashes(html_entity_decode($this->settings->css, ENT_QUOTES)))); |
| 46 |
} |
| 47 |
|
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
public function enqueue() |
| 52 |
{ |
| 53 |
wp_enqueue_style($this->custom); |
| 54 |
wp_enqueue_style($this->options); |
| 55 |
} |
| 56 |
|
| 57 |
private function getStyleOptions() |
| 58 |
{ |
| 59 |
$options = [ |
| 60 |
'backgroundColor', |
| 61 |
'backgroundOpacity', |
| 62 |
'backgroundImage', |
| 63 |
'backgroundPosition', |
| 64 |
'backgroundImageOpacity', |
| 65 |
'blur', |
| 66 |
'foregroundColor', |
| 67 |
'foregroundOpacity', |
| 68 |
'textColor', |
| 69 |
]; |
| 70 |
|
| 71 |
$styles = ''; |
| 72 |
|
| 73 |
if ($this->settings->backgroundColor) { |
| 74 |
$styles .= $this->variable('--ag-background-color', Color::hex2rgb($this->settings->backgroundColor, $this->settings->backgroundOpacity !== false ? (float) $this->settings->backgroundOpacity : 1)); |
| 75 |
} |
| 76 |
|
| 77 |
if ($this->settings->backgroundImage) { |
| 78 |
$styles .= $this->variable('--ag-background-image', 'url(' . $this->settings->backgroundImage . ')'); |
| 79 |
} |
| 80 |
|
| 81 |
if ($this->settings->backgroundPosition) { |
| 82 |
$styles .= $this->variable('--ag-background-image-position', $this->settings->backgroundPosition['x'] . ' ' . $this->settings->backgroundPosition['y']); |
| 83 |
} |
| 84 |
|
| 85 |
if ($this->settings->backgroundImageOpacity) { |
| 86 |
$styles .= $this->variable('--ag-background-image-opacity', $this->settings->backgroundImageOpacity !== false ? (float) $this->settings->backgroundImageOpacity : 1); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
if ($this->settings->foregroundColor) { |
| 91 |
$styles .= $this->variable('--ag-form-background', Color::hex2rgb($this->settings->foregroundColor, $this->settings->foregroundOpacity !== false ? (float) $this->settings->foregroundOpacity : 1)); |
| 92 |
} |
| 93 |
|
| 94 |
if ($this->settings->textColor) { |
| 95 |
$styles .= $this->variable('--ag-text-color', $this->settings->textColor); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
if ($this->settings->blur) { |
| 100 |
$styles .= $this->variable('--ag-blur', $this->settings->blur . 'px'); |
| 101 |
} |
| 102 |
|
| 103 |
$styles = trim($styles); |
| 104 |
wp_register_style($this->options, false, ['age-gate']); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 105 |
|
| 106 |
if ($styles) { |
| 107 |
$styles = sprintf(':root{%s}', $styles); |
| 108 |
|
| 109 |
wp_add_inline_style($this->options, wp_strip_all_tags(stripslashes(html_entity_decode($styles, ENT_QUOTES)))); |
| 110 |
} |
| 111 |
|
| 112 |
return $styles; |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
private function variable($key, $value) |
| 117 |
{ |
| 118 |
return sprintf('%s: %s;', $key, $value); |
| 119 |
} |
| 120 |
} |
| 121 |
|