PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.0
GenerateBlocks v1.0
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 6 years ago class-enqueue-css.php 6 years ago class-settings.php 6 years ago dashboard.php 6 years ago defaults.php 6 years ago functions.php 6 years ago general.php 6 years ago generate-css.php 6 years ago
class-do-css.php
216 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 $this->_css .= $property . ':' . $value . ';';
98 return $this;
99 } else {
100 if ( $valueTop ) {
101 $property_top = $property . '-top';
102 $unit_top = $unit;
103
104 if ( 'border-radius' === $property ) {
105 $property_top = 'border-top-left-radius';
106 } elseif ( 'border-width' === $property ) {
107 $property_top = 'border-top-width';
108 }
109
110 if ( 0 === $value[0] || '0' === $value[0] ) {
111 $unit_top = '';
112 }
113
114 $this->_css .= $property_top . ':' . $value[0] . $unit_top . ';';
115 }
116
117 if ( $valueRight ) {
118 $property_right = $property . '-right';
119 $unit_right = $unit;
120
121 if ( 'border-radius' === $property ) {
122 $property_right = 'border-top-right-radius';
123 } elseif ( 'border-width' === $property ) {
124 $property_right = 'border-right-width';
125 }
126
127 if ( 0 === $value[1] || '0' === $value[1] ) {
128 $unit_right = '';
129 }
130
131 $this->_css .= $property_right . ':' . $value[1] . $unit_right . ';';
132 }
133
134 if ( $valueBottom ) {
135 $property_bottom = $property . '-bottom';
136 $unit_bottom = $unit;
137
138 if ( 'border-radius' === $property ) {
139 $property_bottom = 'border-bottom-right-radius';
140 } elseif ( 'border-width' === $property ) {
141 $property_bottom = 'border-bottom-width';
142 }
143
144 if ( 0 === $value[2] || '0' === $value[2] ) {
145 $unit_bottom = '';
146 }
147
148 $this->_css .= $property_bottom . ':' . $value[2] . $unit_bottom . ';';
149 }
150
151 if ( $valueLeft ) {
152 $property_left = $property . '-left';
153 $unit_left = $unit;
154
155 if ( 'border-radius' === $property ) {
156 $property_left = 'border-bottom-left-radius';
157 } elseif ( 'border-width' === $property ) {
158 $property_left = 'border-left-width';
159 }
160
161 if ( 0 === $value[3] || '0' === $value[3] ) {
162 $unit_left = '';
163 }
164
165 $this->_css .= $property_left . ':' . $value[3] . $unit_left . ';';
166 }
167
168 return $this;
169 }
170 }
171
172 // Add our unit to our value if it exists.
173 if ( $unit ) {
174 $value = $value . $unit;
175 }
176
177 $this->_css .= $property . ':' . $value . ';';
178 return $this;
179 }
180
181 /**
182 * Adds the current selector rules to the output variable
183 *
184 * @access private
185 * @since 1.0
186 *
187 * @return $this
188 */
189 private function add_selector_rules_to_output() {
190 if ( ! empty( $this->_css ) ) {
191 $this->_selector_output = $this->_selector;
192 $this->_output[ $this->_selector_output ][] = $this->_css;
193
194 // Reset the css.
195 $this->_css = '';
196 }
197
198 return $this;
199 }
200
201 /**
202 * Returns the minified css in the $_output variable
203 *
204 * @access public
205 * @since 1.0
206 *
207 * @return string
208 */
209 public function css_output() {
210 // Add current selector's rules to output.
211 $this->add_selector_rules_to_output();
212
213 return $this->_output;
214 }
215 }
216