| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Assets package. |
| 5 |
* |
| 6 |
* (c) Inpsyde GmbH |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace Inpsyde\Assets; |
| 15 |
|
| 16 |
use Inpsyde\Assets\Handler\AssetHandler; |
| 17 |
use Inpsyde\Assets\Handler\StyleHandler; |
| 18 |
use Inpsyde\Assets\OutputFilter\AsyncStyleOutputFilter; |
| 19 |
|
| 20 |
class Style extends BaseAsset implements Asset |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-media |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $media = 'all'; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var string[]|null |
| 31 |
*/ |
| 32 |
protected $inlineStyles = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var array<string, array<string, string>> |
| 36 |
*/ |
| 37 |
protected $cssVars = []; |
| 38 |
|
| 39 |
/** |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
public function media(): string |
| 43 |
{ |
| 44 |
return $this->media; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param string $media |
| 49 |
* |
| 50 |
* @return static |
| 51 |
*/ |
| 52 |
public function forMedia(string $media): Style |
| 53 |
{ |
| 54 |
$this->media = $media; |
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @return string[]|null |
| 61 |
*/ |
| 62 |
public function inlineStyles(): ?array |
| 63 |
{ |
| 64 |
return $this->inlineStyles; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param string $inline |
| 69 |
* |
| 70 |
* @return static |
| 71 |
* |
| 72 |
* @see https://codex.wordpress.org/Function_Reference/wp_add_inline_style |
| 73 |
*/ |
| 74 |
public function withInlineStyles(string $inline): Style |
| 75 |
{ |
| 76 |
if (!$this->inlineStyles) { |
| 77 |
$this->inlineStyles = []; |
| 78 |
} |
| 79 |
|
| 80 |
$this->inlineStyles[] = $inline; |
| 81 |
|
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Add custom CSS properties (CSS vars) to an element. |
| 87 |
* Those custom CSS vars will be enqueued with inline style |
| 88 |
* to your handle. Variables will be automatically prefixed |
| 89 |
* with '--'. |
| 90 |
* |
| 91 |
* @param string $element |
| 92 |
* @param array<string, string> $vars |
| 93 |
* |
| 94 |
* @return $this |
| 95 |
* |
| 96 |
* @example Style::withCssVars('.some-element', ['--white' => '#fff']); |
| 97 |
* @example Style::withCssVars('.some-element', ['white' => '#fff']); |
| 98 |
*/ |
| 99 |
public function withCssVars(string $element, array $vars): Style |
| 100 |
{ |
| 101 |
if (!isset($this->cssVars[$element])) { |
| 102 |
$this->cssVars[$element] = []; |
| 103 |
} |
| 104 |
|
| 105 |
foreach ($vars as $key => $value) { |
| 106 |
$key = substr($key, 0, 2) === '--' |
| 107 |
? $key |
| 108 |
: '--' . $key; |
| 109 |
|
| 110 |
$this->cssVars[$element][$key] = $value; |
| 111 |
} |
| 112 |
|
| 113 |
return $this; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @return array<string, array<string, string>> |
| 118 |
*/ |
| 119 |
public function cssVars(): array |
| 120 |
{ |
| 121 |
return $this->cssVars; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public function cssVarsAsString(): string |
| 128 |
{ |
| 129 |
$return = ''; |
| 130 |
foreach ($this->cssVars() as $element => $vars) { |
| 131 |
$values = ''; |
| 132 |
foreach ($vars as $key => $value) { |
| 133 |
$values .= sprintf('%1$s:%2$s;', $key, $value); |
| 134 |
} |
| 135 |
$return .= sprintf('%1$s{%2$s}', $element, $values); |
| 136 |
} |
| 137 |
|
| 138 |
return $return; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Wrapper function to set AsyncStyleOutputFilter as filter. |
| 143 |
* |
| 144 |
* @return static |
| 145 |
*/ |
| 146 |
public function useAsyncFilter(): Style |
| 147 |
{ |
| 148 |
return $this->withFilters(AsyncStyleOutputFilter::class); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* {@inheritDoc} |
| 153 |
*/ |
| 154 |
protected function defaultHandler(): string |
| 155 |
{ |
| 156 |
return StyleHandler::class; |
| 157 |
} |
| 158 |
} |
| 159 |
|