PluginProbe
Depicter — Popup & Slider Builder / 1.3.3
Depicter — Popup & Slider Builder v1.3.3
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 1.3.3, at app/src/Services/StyleGeneratorService.php

204 lines 4.5 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\Models\Traits\HasDocumentIdTrait;
8
9 class StyleGeneratorService
10 {
11 use HasDocumentIdTrait;
12
13 /**
14 * @var string
15 */
16 protected $css;
17
18 /**
19 * Document raw styles list
20 * @var array
21 */
22 private $stylesList;
23
24 /**
25 * Is filesystem writable or not
26 *
27 * @var bool
28 */
29 protected $isWritable = null;
30
31 /**
32 * Class extra options
33 *
34 * @var array
35 */
36 protected $args = [];
37
38
39 public function __construct( $stylesList = [], $documentID = 0, $args = [] ) {
40 $this->setDocumentId( $documentID );
41 $this->setStylesList( $stylesList );
42
43 $this->args = Arr::merge( $args, [
44 'addImportant' => false
45 ]);
46 }
47
48 public function setStylesList( $stylesList ){
49 if ( $stylesList ) {
50 $this->stylesList = $stylesList;
51 }
52 }
53
54 /**
55 * Generate Css Styles
56 *
57 * @param array $stylesList
58 *
59 * @return string
60 */
61 protected function generateCss( $stylesList = [] ) {
62
63 if ( empty( $stylesList ) ) {
64 return '';
65 }
66
67 $devices = Breakpoints::names();
68 $breakpoints = Breakpoints::all();
69
70 $default = "\n";
71 $tablet = '';
72 $mobile = '';
73 $custom_style = '';
74 $maybeImportantSuffix = $this->args['addImportant'] ? ' !important' : '';
75
76 foreach ( $stylesList as $selector => $cssProperties ) {
77
78 if ( !empty( $cssProperties['customStyle'] ) ) {
79 $custom_style .= $cssProperties['customStyle'] . "\n";
80 }
81
82 foreach ( $devices as $device ) {
83 if ( !empty( $cssProperties[ $device ] ) ) {
84 $$device .= $selector . "{\n";
85 foreach ( $cssProperties[ $device ] as $property => $value ) {
86 $$device .= "\t{$property}:{$value}{$maybeImportantSuffix};\n";
87 }
88 $$device .= "}\n";
89 }
90
91 // check for hover styles
92 if ( !empty( $cssProperties['hover'][ $device ] ) ) {
93 $$device .= $selector . ":hover {\n";
94 foreach ( $cssProperties['hover'][ $device ] as $property => $value ) {
95 $$device .= "\t{$property}:{$value}{$maybeImportantSuffix};\n";
96 }
97 $$device .= "}\n";
98 }
99 }
100 }
101
102 if( $tablet ){
103 $tablet = "\n/***** Tablet *****/\n@media screen and (max-width: {$breakpoints['tablet']}px){\n\n{$tablet}\n}";
104 }
105 if( $mobile ){
106 $mobile = "\n/***** Mobile *****/\n@media screen and (max-width: {$breakpoints['mobile']}px){\n\n{$mobile}\n}";
107 }
108
109 $this->css = $default . $tablet . $mobile;
110
111 if( $custom_style ){
112 $this->css .= "\n/*** Custom styles ***/\n$custom_style";
113 }
114
115 return $this->css;
116 }
117
118 /**
119 * Retrieves CSS with style tag
120 *
121 * @param bool $forceRegenerateStyles
122 *
123 * @return string
124 */
125 public function getCssAndTag( $forceRegenerateStyles = false ) {
126 return "<style>\n" . $this->getCss( $forceRegenerateStyles ) . "</style>\n";
127 }
128
129 /**
130 * Prints CSS with style tag
131 *
132 * @param bool $forceRegenerateStyles
133 */
134 public function printCssAndTag( $forceRegenerateStyles = false ) {
135 echo Sanitize::html( $this->getCSSAndTag( $forceRegenerateStyles ) );
136 }
137
138 /**
139 * Print CSS inline
140 *
141 * @param bool $forceRegenerateStyles
142 *
143 * @return string
144 */
145 public function getCss( $forceRegenerateStyles = false ) {
146 if( ! $this->css || $forceRegenerateStyles ){
147 $this->generateCss( $this->stylesList );
148 }
149 return $this->css . "\n";
150 }
151
152 /**
153 * Save CSS in upload folder
154 *
155 * @param bool $forceRegenerateStyles
156 *
157 * @return StyleGeneratorService
158 */
159 public function saveCss( $forceRegenerateStyles = false ) {
160 $this->isWritable =\Depicter::storage()->filesystem()->write( $this->getCssFilePath(), $this->getCss( $forceRegenerateStyles ) );
161
162 return $this;
163 }
164
165 /**
166 * Retrieves the path to generated css file for current document
167 *
168 * @param bool $checkExistence
169 *
170 * @return bool|string false on not founding the file
171 */
172 public function getCssFilePath( $checkExistence = false ){
173 $cssFilePath = \Depicter::storage()->getCssUploadsDirectory() . '/' . $this->getDocumentID() . '.css';
174 if( $checkExistence ){
175 return file_exists( $cssFilePath ) ? $cssFilePath : false;
176 }
177 return $cssFilePath;
178 }
179
180 /**
181 * Retrieves the url of generated css file for current document
182 *
183 * @return bool|string false on not founding the file
184 */
185 public function getCssFileUrl(){
186 $cssFilePath = $this->getCssFilePath( true );
187
188 if( $cssFilePath !== false ){
189 return \Depicter::storage()->getCssUploadsUrl() . '/' . $this->getDocumentID() . '.css';
190 }
191
192 return false;
193 }
194
195 /**
196 * Whether filesystem is writable or not
197 *
198 * @return bool
199 */
200 public function isWritable(){
201 return $this->isWritable;
202 }
203 }
204