blocks
1 year ago
dynamic-tags
3 months ago
pattern-library
1 year ago
utils
2 years ago
class-do-css.php
2 years ago
class-dynamic-content.php
6 months ago
class-dynamic-tag-security.php
6 months ago
class-enqueue-css.php
1 year ago
class-legacy-attributes.php
4 years ago
class-map-deprecated-attributes.php
2 years ago
class-meta-handler.php
3 months ago
class-plugin-update.php
1 year ago
class-query-loop.php
2 years ago
class-query-utils.php
3 months ago
class-render-blocks.php
1 year ago
class-rest.php
1 year ago
class-settings.php
1 year ago
dashboard.php
1 year ago
defaults.php
1 year ago
deprecated.php
1 year ago
functions.php
6 months ago
general.php
8 months ago
class-do-css.php
233 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 ( |
| 85 | is_array( $value ) && |
| 86 | ! array_filter( |
| 87 | $value, |
| 88 | function( $v ) { |
| 89 | return is_numeric( $v ) || $v; |
| 90 | } |
| 91 | ) |
| 92 | ) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | if ( is_array( $value ) ) { |
| 97 | $valueTop = generateblocks_has_number_value( $value[0] ); |
| 98 | $valueRight = generateblocks_has_number_value( $value[1] ); |
| 99 | $valueBottom = generateblocks_has_number_value( $value[2] ); |
| 100 | $valueLeft = generateblocks_has_number_value( $value[3] ); |
| 101 | |
| 102 | if ( $valueTop && $valueRight && $valueBottom && $valueLeft ) { |
| 103 | $value = generateblocks_get_shorthand_css( $value[0], $value[1], $value[2], $value[3], $unit ); |
| 104 | |
| 105 | if ( 'border-width' === $property ) { |
| 106 | $this->_css .= 'border-style: solid;'; |
| 107 | } |
| 108 | |
| 109 | $this->_css .= $property . ':' . $value . ';'; |
| 110 | return $this; |
| 111 | } else { |
| 112 | if ( $valueTop ) { |
| 113 | $property_top = $property . '-top'; |
| 114 | $unit_top = $unit; |
| 115 | |
| 116 | if ( 'border-radius' === $property ) { |
| 117 | $property_top = 'border-top-left-radius'; |
| 118 | } elseif ( 'border-width' === $property ) { |
| 119 | $property_top = 'border-top-width'; |
| 120 | $this->_css .= 'border-top-style: solid;'; |
| 121 | } |
| 122 | |
| 123 | if ( ! is_numeric( $value[0] ) || 0 === $value[0] || '0' === $value[0] ) { |
| 124 | $unit_top = ''; |
| 125 | } |
| 126 | |
| 127 | $this->_css .= $property_top . ':' . $value[0] . $unit_top . ';'; |
| 128 | } |
| 129 | |
| 130 | if ( $valueRight ) { |
| 131 | $property_right = $property . '-right'; |
| 132 | $unit_right = $unit; |
| 133 | |
| 134 | if ( 'border-radius' === $property ) { |
| 135 | $property_right = 'border-top-right-radius'; |
| 136 | } elseif ( 'border-width' === $property ) { |
| 137 | $property_right = 'border-right-width'; |
| 138 | $this->_css .= 'border-right-style: solid;'; |
| 139 | } |
| 140 | |
| 141 | if ( ! is_numeric( $value[1] ) || 0 === $value[1] || '0' === $value[1] ) { |
| 142 | $unit_right = ''; |
| 143 | } |
| 144 | |
| 145 | $this->_css .= $property_right . ':' . $value[1] . $unit_right . ';'; |
| 146 | } |
| 147 | |
| 148 | if ( $valueBottom ) { |
| 149 | $property_bottom = $property . '-bottom'; |
| 150 | $unit_bottom = $unit; |
| 151 | |
| 152 | if ( 'border-radius' === $property ) { |
| 153 | $property_bottom = 'border-bottom-right-radius'; |
| 154 | } elseif ( 'border-width' === $property ) { |
| 155 | $property_bottom = 'border-bottom-width'; |
| 156 | $this->_css .= 'border-bottom-style: solid;'; |
| 157 | } |
| 158 | |
| 159 | if ( ! is_numeric( $value[2] ) || 0 === $value[2] || '0' === $value[2] ) { |
| 160 | $unit_bottom = ''; |
| 161 | } |
| 162 | |
| 163 | $this->_css .= $property_bottom . ':' . $value[2] . $unit_bottom . ';'; |
| 164 | } |
| 165 | |
| 166 | if ( $valueLeft ) { |
| 167 | $property_left = $property . '-left'; |
| 168 | $unit_left = $unit; |
| 169 | |
| 170 | if ( 'border-radius' === $property ) { |
| 171 | $property_left = 'border-bottom-left-radius'; |
| 172 | } elseif ( 'border-width' === $property ) { |
| 173 | $property_left = 'border-left-width'; |
| 174 | $this->_css .= 'border-left-style: solid;'; |
| 175 | } |
| 176 | |
| 177 | if ( ! is_numeric( $value[3] ) || 0 === $value[3] || '0' === $value[3] ) { |
| 178 | $unit_left = ''; |
| 179 | } |
| 180 | |
| 181 | $this->_css .= $property_left . ':' . $value[3] . $unit_left . ';'; |
| 182 | } |
| 183 | |
| 184 | return $this; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Add our unit to our value if it exists. |
| 189 | if ( $unit && is_numeric( $value ) ) { |
| 190 | $value = $value . $unit; |
| 191 | } |
| 192 | |
| 193 | $this->_css .= $property . ':' . $value . ';'; |
| 194 | return $this; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Adds the current selector rules to the output variable |
| 199 | * |
| 200 | * @access private |
| 201 | * @since 1.0 |
| 202 | * |
| 203 | * @return $this |
| 204 | */ |
| 205 | private function add_selector_rules_to_output() { |
| 206 | if ( ! empty( $this->_css ) ) { |
| 207 | $this->_selector_output = $this->_selector; |
| 208 | $this->_output[ $this->_selector_output ][] = $this->_css; |
| 209 | $this->_output[ $this->_selector_output ] = array_unique( $this->_output[ $this->_selector_output ] ); |
| 210 | |
| 211 | // Reset the css. |
| 212 | $this->_css = ''; |
| 213 | } |
| 214 | |
| 215 | return $this; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns the minified css in the $_output variable |
| 220 | * |
| 221 | * @access public |
| 222 | * @since 1.0 |
| 223 | * |
| 224 | * @return string |
| 225 | */ |
| 226 | public function css_output() { |
| 227 | // Add current selector's rules to output. |
| 228 | $this->add_selector_rules_to_output(); |
| 229 | |
| 230 | return $this->_output; |
| 231 | } |
| 232 | } |
| 233 |