PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / StyleGeneratorService.php

StyleGeneratorService.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/StyleGeneratorService.php

245 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Services;
3
4 use Averta\Core\Utility\Arr;
5 use Averta\WordPress\Utility\Sanitize;
6 use Depicter\Document\CSS\Breakpoints;
7 use Depicter\Document\CSS\Selector;
8 use Depicter\Document\Models\Traits\HasDocumentIdTrait;
9 use Depicter\Html\Tag;
10
11 class StyleGeneratorService
12 {
13 use HasDocumentIdTrait;
14
15 /**
16 * @var string
17 */
18 protected $css;
19
20 /**
21 * Document raw styles list
22 * @var array
23 */
24 private $stylesList;
25
26 /**
27 * Is filesystem writable or not
28 *
29 * @var bool
30 */
31 protected $isWritable = null;
32
33 /**
34 * Before init styles of document
35 *
36 * @var string
37 */
38 protected $beforeInitStyle = '';
39
40 /**
41 * Class extra options
42 *
43 * @var array
44 */
45 protected $args = [];
46
47
48 public function __construct( $stylesList = [], $documentID = 0, $args = [] ) {
49 $this->setDocumentId( $documentID );
50 $this->setStylesList( $stylesList );
51
52 $this->args = Arr::merge( $args, [
53 'addImportant' => false
54 ]);
55 }
56
57 public function setStylesList( $stylesList ){
58 if ( $stylesList ) {
59 $this->stylesList = $stylesList;
60 }
61 }
62
63 /**
64 * Generate Css Styles
65 *
66 * @param array $stylesList
67 *
68 * @return string
69 */
70 protected function generateCss( $stylesList = [] ) {
71
72 if ( empty( $stylesList ) ) {
73 return '';
74 }
75
76 $css = '';
77 $devices = Breakpoints::names();
78 $breakpoints = Breakpoints::all();
79
80 $default = "\n";
81 $tablet = '';
82 $mobile = '';
83 $custom_style = '';
84 $maybeImportantSuffix = $this->args['addImportant'] ? ' !important' : '';
85
86 foreach ( $stylesList as $selector => $cssProperties ) {
87
88 if ( !empty( $cssProperties['customStyle'] ) ) {
89 $custom_style .= $cssProperties['customStyle'] . "\n";
90 }
91
92 if ( !empty( $cssProperties['beforeInitStyle'] ) && is_array( $cssProperties['beforeInitStyle'] ) ) {
93 $this->beforeInitStyle = $this->generateCss( $cssProperties['beforeInitStyle'] );
94 }
95
96 foreach ( $devices as $device ) {
97 if ( !empty( $cssProperties[ $device ] ) ) {
98 $$device .= $selector . "{\n";
99 foreach ( $cssProperties[ $device ] as $property => $value ) {
100 $$device .= "\t{$property}:{$value}{$maybeImportantSuffix};\n";
101 }
102 $$device .= "}\n";
103 }
104
105 // check for hover styles
106 if ( !empty( $cssProperties['hover'][ $device ] ) ) {
107 $$device .= $selector . ":hover {\n";
108 foreach ( $cssProperties['hover'][ $device ] as $property => $value ) {
109 $$device .= "\t{$property}:{$value}{$maybeImportantSuffix};\n";
110 }
111 $$device .= "}\n";
112 }
113 }
114 }
115
116 if( $tablet ){
117 $tablet = "\n/***** Tablet *****/\n@media screen and (max-width: {$breakpoints['tablet']}px){\n\n{$tablet}\n}";
118 }
119 if( $mobile ){
120 $mobile = "\n/***** Mobile *****/\n@media screen and (max-width: {$breakpoints['mobile']}px){\n\n{$mobile}\n}";
121 }
122
123 $css = $default . $tablet . $mobile;
124
125 if( $custom_style ){
126 $css .= "\n/*** Custom styles ***/\n$custom_style";
127 }
128
129 return $css;
130 }
131
132 /**
133 * Retrieves CSS with style tag
134 *
135 * @param bool $forceRegenerateStyles
136 *
137 * @return string
138 */
139 public function getCssAndTag( $forceRegenerateStyles = false ) {
140 $attributes = [ 'id' => Selector::prefixify( $this->getDocumentID() ) . '-inline-css' ];
141 return $this->wrapStyleWithTag( $this->getCss( $forceRegenerateStyles ), $attributes );
142 }
143
144 /**
145 * Get generated CSS
146 *
147 * @param bool $forceRegenerateStyles
148 *
149 * @return string
150 */
151 public function getCss( $forceRegenerateStyles = false ) {
152 if( ! $this->css || $forceRegenerateStyles ){
153 $this->css = $this->generateCss( $this->stylesList );
154 }
155 return $this->css . "\n";
156 }
157
158 /**
159 * Get before init CSS
160 *
161 * @param bool $forceRegenerateStyles
162 *
163 * @return string
164 */
165 public function getBeforeInitCss( $forceRegenerateStyles = false ) {
166 if( ! $this->beforeInitStyle|| $forceRegenerateStyles ){
167 $this->generateCss( $this->stylesList );
168 }
169 return $this->beforeInitStyle . "\n";
170 }
171
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 /**
185 * Save CSS in upload folder
186 *
187 * @param bool $forceRegenerateStyles
188 *
189 * @return StyleGeneratorService
190 */
191 public function saveCss( $forceRegenerateStyles = false ) {
192 $this->isWritable =\Depicter::storage()->filesystem()->write( $this->getCssFilePath(), $this->getCss( $forceRegenerateStyles ) );
193
194 return $this;
195 }
196
197 /**
198 * Retrieves the path to generated css file for current document
199 *
200 * @param bool $checkExistence
201 *
202 * @return bool|string false on not founding the file
203 */
204 public function getCssFilePath( $checkExistence = false ){
205 $cssFilePath = \Depicter::storage()->getCssUploadsDirectory() . '/' . $this->getDocumentID() . '.css';
206 if( $checkExistence ){
207 return file_exists( $cssFilePath ) ? $cssFilePath : false;
208 }
209 return $cssFilePath;
210 }
211
212 /**
213 * Retrieves the url of generated css file for current document
214 *
215 * @return bool|string false on not founding the file
216 */
217 public function getCssFileUrl(){
218 $cssFilePath = $this->getCssFilePath( true );
219
220 if( $cssFilePath !== false ){
221 return \Depicter::storage()->getCssUploadsUrl() . '/' . $this->getDocumentID() . '.css';
222 }
223
224 return false;
225 }
226
227 /**
228 * Whether filesystem is writable or not
229 *
230 * @return bool
231 */
232 public function isWritable(){
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";
243 }
244 }
245