| @@ -3,9 +3,11 @@ | ||
| 3 | 3 | |
| 4 | 4 | use Averta\Core\Utility\Arr; |
| 5 | 5 | use Averta\WordPress\Utility\Sanitize; |
| 6 | 6 | use Depicter\Document\CSS\Breakpoints; |
| 7 | +use Depicter\Document\CSS\Selector; | |
| 7 | 8 | use Depicter\Document\Models\Traits\HasDocumentIdTrait; |
| 9 | +use Depicter\Html\Tag; | |
| 8 | 10 | |
| 9 | 11 | class StyleGeneratorService |
| 10 | 12 | { |
| 11 | 13 | use HasDocumentIdTrait; |
| @@ -28,8 +30,15 @@ | ||
| 28 | 30 | */ |
| 29 | 31 | protected $isWritable = null; |
| 30 | 32 | |
| 31 | 33 | /** |
| 34 | + * Before init styles of document | |
| 35 | + * | |
| 36 | + * @var string | |
| 37 | + */ | |
| 38 | + protected $beforeInitStyle = ''; | |
| 39 | + | |
| 40 | + /** | |
| 32 | 41 | * Class extra options |
| 33 | 42 | * |
| 34 | 43 | * @var array |
| 35 | 44 | */ |
| @@ -63,8 +72,9 @@ | ||
| 63 | 72 | if ( empty( $stylesList ) ) { |
| 64 | 73 | return ''; |
| 65 | 74 | } |
| 66 | 75 | |
| 76 | + $css = ''; | |
| 67 | 77 | $devices = Breakpoints::names(); |
| 68 | 78 | $breakpoints = Breakpoints::all(); |
| 69 | 79 | |
| 70 | 80 | $default = "\n"; |
| @@ -78,8 +88,12 @@ | ||
| 78 | 88 | if ( !empty( $cssProperties['customStyle'] ) ) { |
| 79 | 89 | $custom_style .= $cssProperties['customStyle'] . "\n"; |
| 80 | 90 | } |
| 81 | 91 | |
| 92 | + if ( !empty( $cssProperties['beforeInitStyle'] ) && is_array( $cssProperties['beforeInitStyle'] ) ) { | |
| 93 | + $this->beforeInitStyle = $this->generateCss( $cssProperties['beforeInitStyle'] ); | |
| 94 | + } | |
| 95 | + | |
| 82 | 96 | foreach ( $devices as $device ) { |
| 83 | 97 | if ( !empty( $cssProperties[ $device ] ) ) { |
| 84 | 98 | $$device .= $selector . "{\n"; |
| 85 | 99 | foreach ( $cssProperties[ $device ] as $property => $value ) { |
| @@ -105,15 +119,15 @@ | ||
| 105 | 119 | if( $mobile ){ |
| 106 | 120 | $mobile = "\n/***** Mobile *****/\n@media screen and (max-width: {$breakpoints['mobile']}px){\n\n{$mobile}\n}"; |
| 107 | 121 | } |
| 108 | 122 | |
| 109 | - $this->css = $default . $tablet . $mobile; | |
| 123 | + $css = $default . $tablet . $mobile; | |
| 110 | 124 | |
| 111 | 125 | if( $custom_style ){ |
| 112 | - $this->css .= "\n/*** Custom styles ***/\n$custom_style"; | |
| 126 | + $css .= "\n/*** Custom styles ***/\n$custom_style"; | |
| 113 | 127 | } |
| 114 | 128 | |
| 115 | - return $this->css; | |
| 129 | + return $css; | |
| 116 | 130 | } |
| 117 | 131 | |
| 118 | 132 | /** |
| 119 | 133 | * Retrieves CSS with style tag |
| @@ -122,35 +136,53 @@ | ||
| 122 | 136 | * |
| 123 | 137 | * @return string |
| 124 | 138 | */ |
| 125 | 139 | public function getCssAndTag( $forceRegenerateStyles = false ) { |
| 126 | - return "<style>\n" . $this->getCss( $forceRegenerateStyles ) . "</style>\n"; | |
| 140 | + $attributes = [ 'id' => Selector::prefixify( $this->getDocumentID() ) . '-inline-css' ]; | |
| 141 | + return $this->wrapStyleWithTag( $this->getCss( $forceRegenerateStyles ), $attributes ); | |
| 127 | 142 | } |
| 128 | 143 | |
| 129 | 144 | /** |
| 130 | - * Prints CSS with style tag | |
| 145 | + * Get generated CSS | |
| 131 | 146 | * |
| 132 | 147 | * @param bool $forceRegenerateStyles |
| 148 | + * | |
| 149 | + * @return string | |
| 133 | 150 | */ |
| 134 | - public function printCssAndTag( $forceRegenerateStyles = false ) { | |
| 135 | - echo Sanitize::html( $this->getCSSAndTag( $forceRegenerateStyles ) ); | |
| 151 | + public function getCss( $forceRegenerateStyles = false ) { | |
| 152 | + if( ! $this->css || $forceRegenerateStyles ){ | |
| 153 | + $this->css = $this->generateCss( $this->stylesList ); | |
| 154 | + } | |
| 155 | + return $this->css . "\n"; | |
| 136 | 156 | } |
| 137 | 157 | |
| 138 | 158 | /** |
| 139 | - * Print CSS inline | |
| 159 | + * Get before init CSS | |
| 140 | 160 | * |
| 141 | 161 | * @param bool $forceRegenerateStyles |
| 142 | 162 | * |
| 143 | 163 | * @return string |
| 144 | 164 | */ |
| 145 | - public function getCss( $forceRegenerateStyles = false ) { | |
| 146 | - if( ! $this->css || $forceRegenerateStyles ){ | |
| 165 | + public function getBeforeInitCss( $forceRegenerateStyles = false ) { | |
| 166 | + if( ! $this->beforeInitStyle|| $forceRegenerateStyles ){ | |
| 147 | 167 | $this->generateCss( $this->stylesList ); |
| 148 | 168 | } |
| 149 | - return $this->css . "\n"; | |
| 169 | + return $this->beforeInitStyle . "\n"; | |
| 150 | 170 | } |
| 151 | 171 | |
| 152 | 172 | /** |
| 173 | + * Get before init CSS | |
| 174 | + * | |
| 175 | + * @param bool $forceRegenerateStyles | |
| 176 | + * | |
| 177 | + * @return string | |
| 178 | + */ | |
| 179 | + public function getBeforeInitCssAndTag( $forceRegenerateStyles = false ) { | |
| 180 | + $attributes = [ 'id' => Selector::prefixify( $this->getDocumentID() ) . '-inline-pre-css' ]; | |
| 181 | + return $this->wrapStyleWithTag( $this->getBeforeInitCss( $forceRegenerateStyles ), $attributes ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 153 | 185 | * Save CSS in upload folder |
| 154 | 186 | * |
| 155 | 187 | * @param bool $forceRegenerateStyles |
| 156 | 188 | * |
| @@ -198,6 +230,15 @@ | ||
| 198 | 230 | * @return bool |
| 199 | 231 | */ |
| 200 | 232 | public function isWritable(){ |
| 201 | 233 | return $this->isWritable; |
| 234 | + } | |
| 235 | + | |
| 236 | + /** | |
| 237 | + * Wraps style within a style tag | |
| 238 | + * | |
| 239 | + * @return string | |
| 240 | + */ | |
| 241 | + protected function wrapStyleWithTag( $style, $attributes = [] ) { | |
| 242 | + return Tag::el('style', $attributes, "\n" . $style ) . "\n"; | |
| 202 | 243 | } |
| 203 | 244 | } |