PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.2
GenerateBlocks v1.5.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / class-do-css.php
generateblocks / includes Last commit date
class-do-css.php 4 years ago class-dynamic-content.php 4 years ago class-enqueue-css.php 4 years ago class-legacy-attributes.php 4 years ago class-plugin-update.php 5 years ago class-query-loop.php 4 years ago class-render-blocks.php 4 years ago class-rest.php 4 years ago class-settings.php 4 years ago dashboard.php 4 years ago defaults.php 4 years ago functions.php 4 years ago general.php 4 years ago generate-css.php 4 years ago
class-do-css.php
225 lines
1 <?php
2 /**
3 * Builds our dynamic CSS.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Creates minified css via PHP.
14 */
15 class GenerateBlocks_Dynamic_CSS {
16
17 /**
18 * The css selector that you're currently adding rules to
19 *
20 * @access protected
21 * @var string
22 */
23 protected $_selector = ''; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
24
25 /**
26 * Stores the final css output with all of its rules for the current selector.
27 *
28 * @access protected
29 * @var string
30 */
31 protected $_selector_output = ''; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
32
33 /**
34 * Stores all of the rules that will be added to the selector
35 *
36 * @access protected
37 * @var string
38 */
39 protected $_css = ''; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
40
41 /**
42 * The string that holds all of the css to output
43 *
44 * @access protected
45 * @var array
46 */
47 protected $_output = array(); // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
48
49 /**
50 * Sets a selector to the object and changes the current selector to a new one
51 *
52 * @access public
53 * @since 1.0
54 *
55 * @param string $selector - the css identifier of the html that you wish to target.
56 * @return $this
57 */
58 public function set_selector( $selector = '' ) {
59 // Render the css in the output string everytime the selector changes.
60 if ( '' !== $this->_selector ) {
61 $this->add_selector_rules_to_output();
62 }
63
64 $this->_selector = $selector;
65 return $this;
66 }
67
68 /**
69 * Adds a css property with value to the css output
70 *
71 * @access public
72 * @since 1.0
73 *
74 * @param string $property - the css property.
75 * @param string $value - the value to be placed with the property.
76 * @param string $unit - the unit for the value (px).
77 * @return $this
78 */
79 public function add_property( $property, $value, $unit = false ) {
80 if ( empty( $value ) && ! is_numeric( $value ) ) {
81 return false;
82 }
83
84 if ( is_array( $value ) && ! array_filter( $value, 'is_numeric' ) ) {
85 return false;
86 }
87
88 if ( is_array( $value ) ) {
89 $valueTop = generateblocks_has_number_value( $value[0] );
90 $valueRight = generateblocks_has_number_value( $value[1] );
91 $valueBottom = generateblocks_has_number_value( $value[2] );
92 $valueLeft = generateblocks_has_number_value( $value[3] );
93
94 if ( $valueTop && $valueRight && $valueBottom && $valueLeft ) {
95 $value = generateblocks_get_shorthand_css( $value[0], $value[1], $value[2], $value[3], $unit );
96
97 if ( 'border-width' === $property ) {
98 $this->_css .= 'border-style: solid;';
99 }
100
101 $this->_css .= $property . ':' . $value . ';';
102 return $this;
103 } else {
104 if ( $valueTop ) {
105 $property_top = $property . '-top';
106 $unit_top = $unit;
107
108 if ( 'border-radius' === $property ) {
109 $property_top = 'border-top-left-radius';
110 } elseif ( 'border-width' === $property ) {
111 $property_top = 'border-top-width';
112 $this->_css .= 'border-top-style: solid;';
113 }
114
115 if ( 0 === $value[0] || '0' === $value[0] ) {
116 $unit_top = '';
117 }
118
119 $this->_css .= $property_top . ':' . $value[0] . $unit_top . ';';
120 }
121
122 if ( $valueRight ) {
123 $property_right = $property . '-right';
124 $unit_right = $unit;
125
126 if ( 'border-radius' === $property ) {
127 $property_right = 'border-top-right-radius';
128 } elseif ( 'border-width' === $property ) {
129 $property_right = 'border-right-width';
130 $this->_css .= 'border-right-style: solid;';
131 }
132
133 if ( 0 === $value[1] || '0' === $value[1] ) {
134 $unit_right = '';
135 }
136
137 $this->_css .= $property_right . ':' . $value[1] . $unit_right . ';';
138 }
139
140 if ( $valueBottom ) {
141 $property_bottom = $property . '-bottom';
142 $unit_bottom = $unit;
143
144 if ( 'border-radius' === $property ) {
145 $property_bottom = 'border-bottom-right-radius';
146 } elseif ( 'border-width' === $property ) {
147 $property_bottom = 'border-bottom-width';
148 $this->_css .= 'border-bottom-style: solid;';
149 }
150
151 if ( 0 === $value[2] || '0' === $value[2] ) {
152 $unit_bottom = '';
153 }
154
155 $this->_css .= $property_bottom . ':' . $value[2] . $unit_bottom . ';';
156 }
157
158 if ( $valueLeft ) {
159 $property_left = $property . '-left';
160 $unit_left = $unit;
161
162 if ( 'border-radius' === $property ) {
163 $property_left = 'border-bottom-left-radius';
164 } elseif ( 'border-width' === $property ) {
165 $property_left = 'border-left-width';
166 $this->_css .= 'border-left-style: solid;';
167 }
168
169 if ( 0 === $value[3] || '0' === $value[3] ) {
170 $unit_left = '';
171 }
172
173 $this->_css .= $property_left . ':' . $value[3] . $unit_left . ';';
174 }
175
176 return $this;
177 }
178 }
179
180 // Add our unit to our value if it exists.
181 if ( $unit ) {
182 $value = $value . $unit;
183 }
184
185 $this->_css .= $property . ':' . $value . ';';
186 return $this;
187 }
188
189 /**
190 * Adds the current selector rules to the output variable
191 *
192 * @access private
193 * @since 1.0
194 *
195 * @return $this
196 */
197 private function add_selector_rules_to_output() {
198 if ( ! empty( $this->_css ) ) {
199 $this->_selector_output = $this->_selector;
200 $this->_output[ $this->_selector_output ][] = $this->_css;
201 $this->_output[ $this->_selector_output ] = array_unique( $this->_output[ $this->_selector_output ] );
202
203 // Reset the css.
204 $this->_css = '';
205 }
206
207 return $this;
208 }
209
210 /**
211 * Returns the minified css in the $_output variable
212 *
213 * @access public
214 * @since 1.0
215 *
216 * @return string
217 */
218 public function css_output() {
219 // Add current selector's rules to output.
220 $this->add_selector_rules_to_output();
221
222 return $this->_output;
223 }
224 }
225