| 1 |
<?php |
| 2 |
namespace ABlocks\Controls; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\ControlBaseAbstract; |
| 9 |
use ABlocks\Controls\Dimensions; |
| 10 |
use ABlocks\Controls\Range; |
| 11 |
|
| 12 |
class Icon { |
| 13 |
public static function get_attribute_default_value( $is_responsive = false ) { |
| 14 |
return []; |
| 15 |
} |
| 16 |
|
| 17 |
public static function get_attribute( $attribute_prefix = 'icon', $default = [] ) { |
| 18 |
$default_args = wp_parse_args($default, [ |
| 19 |
'path' => 'M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm0 448c-110.3 0-200-89.7-200-200S137.7 56 248 56s200 89.7 200 200-89.7 200-200 200zm84-143.4c-20.8 25-51.5 39.4-84 39.4s-63.2-14.3-84-39.4c-8.5-10.2-23.6-11.5-33.8-3.1-10.2 8.5-11.5 23.6-3.1 33.8 30 36 74.1 56.6 120.9 56.6s90.9-20.6 120.9-56.6c8.5-10.2 7.1-25.3-3.1-33.8-10.2-8.4-25.3-7.1-33.8 3.1zM136.5 211c7.7-13.7 19.2-21.6 31.5-21.6s23.8 7.9 31.5 21.6l9.5 17c2.1 3.7 6.2 4.7 9.3 3.7 3.6-1.1 6-4.5 5.7-8.3-3.3-42.1-32.2-71.4-56-71.4s-52.7 29.3-56 71.4c-.3 3.7 2.1 7.2 5.7 8.3 3.4 1.1 7.4-.5 9.3-3.7l9.5-17zM328 152c-23.8 0-52.7 29.3-56 71.4-.3 3.7 2.1 7.2 5.7 8.3 3.5 1.1 7.4-.5 9.3-3.7l9.5-17c7.7-13.7 19.2-21.6 31.5-21.6s23.8 7.9 31.5 21.6l9.5 17c2.1 3.7 6.2 4.7 9.3 3.7 3.6-1.1 6-4.5 5.7-8.3-3.3-42.1-32.2-71.4-56-71.4z', |
| 20 |
'viewBox' => '0 0 496 512', |
| 21 |
'className' => 'far fa-smile-beam', |
| 22 |
'hasNoSelectorOrSource' => false, |
| 23 |
'size' => 55, |
| 24 |
'color' => '#69727d', |
| 25 |
'BgColor' => '', |
| 26 |
'iconType' => 'default', |
| 27 |
'iconShape' => 'circle', |
| 28 |
]); |
| 29 |
|
| 30 |
$svgPathKey = $attribute_prefix . 'SvgPath'; |
| 31 |
$svgViewBoxKey = $attribute_prefix . 'SvgViewBox'; |
| 32 |
$svgClassKey = $attribute_prefix . 'Class'; |
| 33 |
$customImageUrl = $attribute_prefix . 'ImageUrl'; |
| 34 |
$customImageID = $attribute_prefix . 'ImageID'; |
| 35 |
$customImageSize = $attribute_prefix . 'ImageSize'; |
| 36 |
|
| 37 |
$attribute = [ |
| 38 |
$svgPathKey => [ |
| 39 |
'type' => 'string', |
| 40 |
'default' => $default_args['path'], |
| 41 |
], |
| 42 |
$svgViewBoxKey => [ |
| 43 |
'type' => 'string', |
| 44 |
'default' => $default_args['viewBox'], |
| 45 |
], |
| 46 |
$svgClassKey => [ |
| 47 |
'type' => 'string', |
| 48 |
'default' => $default_args['className'], |
| 49 |
], |
| 50 |
$customImageUrl => [ |
| 51 |
'type' => 'string', |
| 52 |
'default' => '', |
| 53 |
], |
| 54 |
$customImageID => [ |
| 55 |
'type' => 'number', |
| 56 |
'default' => 0, |
| 57 |
], |
| 58 |
$customImageSize => [ |
| 59 |
'type' => 'string', |
| 60 |
'default' => '', |
| 61 |
], |
| 62 |
$attribute_prefix . 'Color' => [ |
| 63 |
'type' => 'string', |
| 64 |
'default' => $default_args['color'], |
| 65 |
], |
| 66 |
$attribute_prefix . 'BgColor' => [ |
| 67 |
'type' => 'string', |
| 68 |
'default' => $default_args['BgColor'], |
| 69 |
], |
| 70 |
$attribute_prefix . 'Type' => [ |
| 71 |
'type' => 'string', |
| 72 |
'default' => $default_args['iconType'], |
| 73 |
], |
| 74 |
$attribute_prefix . 'Shape' => [ |
| 75 |
'type' => 'string', |
| 76 |
'default' => $default_args['iconShape'], |
| 77 |
], |
| 78 |
]; |
| 79 |
|
| 80 |
// Add additional attributes if 'hasNoSelectorOrSource' is false |
| 81 |
if ( ! $default_args['hasNoSelectorOrSource'] ) { |
| 82 |
$attribute[ $svgPathKey ]['source'] = 'attribute'; |
| 83 |
$attribute[ $svgPathKey ]['selector'] = 'svg.ablocks-svg-icon path'; |
| 84 |
$attribute[ $svgPathKey ]['attribute'] = 'd'; |
| 85 |
|
| 86 |
$attribute[ $svgViewBoxKey ]['source'] = 'attribute'; |
| 87 |
$attribute[ $svgViewBoxKey ]['selector'] = 'svg.ablocks-svg-icon'; |
| 88 |
$attribute[ $svgViewBoxKey ]['attribute'] = 'viewBox'; |
| 89 |
} |
| 90 |
|
| 91 |
$attribute = array_merge( |
| 92 |
$attribute, |
| 93 |
Dimensions::get_attribute( $attribute_prefix . 'Padding', false ), |
| 94 |
Dimensions::get_attribute( $attribute_prefix . 'BorderRadius', false ), |
| 95 |
Dimensions::get_attribute( $attribute_prefix . 'BorderWidth', false ), |
| 96 |
Range::get_attribute([ |
| 97 |
'attributeName' => $attribute_prefix . 'Size', |
| 98 |
'isResponsive' => false, |
| 99 |
'defaultValue' => $default_args['size'], |
| 100 |
]), |
| 101 |
Range::get_attribute([ |
| 102 |
'attributeName' => $attribute_prefix . 'Rotate', |
| 103 |
'isResponsive' => false, |
| 104 |
'defaultValue' => 0, |
| 105 |
]), |
| 106 |
Range::get_attribute([ |
| 107 |
'attributeName' => $attribute_prefix . 'Sizing', |
| 108 |
'attributeObjectKey' => 'value', |
| 109 |
'isResponsive' => true, |
| 110 |
'hasUnit' => true, |
| 111 |
'unitDefaultValue' => 'px', |
| 112 |
'defaultValue' => $default_args['size'], |
| 113 |
|
| 114 |
]), |
| 115 |
Range::get_attribute([ |
| 116 |
'attributeName' => $attribute_prefix . 'Rotated', |
| 117 |
'attributeObjectKey' => 'value', |
| 118 |
'isResponsive' => true, |
| 119 |
'hasUnit' => true, |
| 120 |
'unitDefaultValue' => 'deg', |
| 121 |
'defaultValue' => 0, |
| 122 |
]), |
| 123 |
Range::get_attribute([ |
| 124 |
'attributeName' => $attribute_prefix . 'RotateHover', |
| 125 |
'attributeObjectKey' => 'value', |
| 126 |
'isResponsive' => true, |
| 127 |
'hasUnit' => true, |
| 128 |
'unitDefaultValue' => 'deg', |
| 129 |
'defaultValue' => 0, |
| 130 |
] ), |
| 131 |
Range::get_attribute([ |
| 132 |
'attributeName' => $attribute_prefix . 'transitionDuration', |
| 133 |
'isResponsive' => false, |
| 134 |
'defaultValue' => 0, |
| 135 |
] ), |
| 136 |
Range::get_attribute([ |
| 137 |
'attributeName' => $attribute_prefix . 'ImageWidth', |
| 138 |
'isResponsive' => false, |
| 139 |
'defaultValue' => 0, |
| 140 |
]) |
| 141 |
); |
| 142 |
|
| 143 |
return $attribute; |
| 144 |
} |
| 145 |
|
| 146 |
public static function get_css( $attribute_value, $property = '', $device = '' ) { |
| 147 |
return []; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static function get_wrapper_css( $attributes, $device = '', $attributePrefix = 'icon' ) { |
| 153 |
$iconType = $attributes[ $attributePrefix . 'Type' ]; |
| 154 |
$iconShape = $attributes[ $attributePrefix . 'Shape' ]; |
| 155 |
$backgroundColor = $attributes[ $attributePrefix . 'BgColor' ]; |
| 156 |
$primaryColor = $attributes[ $attributePrefix . 'Color' ]; |
| 157 |
$iconViewCSS = []; |
| 158 |
|
| 159 |
if ( $iconType !== 'default' ) { |
| 160 |
if ( $iconType === 'stacked' ) { |
| 161 |
if ( $iconShape === 'circle' ) { |
| 162 |
$iconViewCSS = [ |
| 163 |
'background' => $backgroundColor ? $backgroundColor : '#ddd', |
| 164 |
'border-radius' => '50%', |
| 165 |
'padding' => '.5em', |
| 166 |
]; |
| 167 |
} elseif ( $iconShape === 'square' ) { |
| 168 |
$iconViewCSS = [ |
| 169 |
'background' => $backgroundColor ? $backgroundColor : '#ddd', |
| 170 |
'padding' => '.5em', |
| 171 |
]; |
| 172 |
} |
| 173 |
} elseif ( $iconType === 'framed' ) { |
| 174 |
if ( $iconShape === 'circle' ) { |
| 175 |
$iconViewCSS = [ |
| 176 |
'background' => $backgroundColor ? $backgroundColor : 'transparent', |
| 177 |
'padding' => '.5em', |
| 178 |
'border-radius' => '50%', |
| 179 |
'border' => '2px solid ' . ( $primaryColor ? $primaryColor : '#69727d' ), |
| 180 |
]; |
| 181 |
} elseif ( $iconShape === 'square' ) { |
| 182 |
$iconViewCSS = [ |
| 183 |
'background' => $backgroundColor ? $backgroundColor : 'transparent', |
| 184 |
'padding' => '.5em', |
| 185 |
'border' => '2px solid ' . ( $primaryColor ? $primaryColor : '#69727d' ), |
| 186 |
]; |
| 187 |
} |
| 188 |
}//end if |
| 189 |
}//end if |
| 190 |
|
| 191 |
if ( ! empty( $attributes[ $attributePrefix . 'Sizing' ] ) && empty( $attributes[ $attributePrefix . 'ImageUrl' ] ) ) { |
| 192 |
$sizeValue = isset( $attributes[ $attributePrefix . 'Sizing' ][ 'value' . $device ] ) ? $attributes[ $attributePrefix . 'Sizing' ][ 'value' . $device ] : $attributes[ $attributePrefix . 'Sizing' ]['value']; |
| 193 |
$sizeUnit = isset( $attributes[ $attributePrefix . 'Sizing' ][ 'valueUnit' . $device ] ) ? $attributes[ $attributePrefix . 'Sizing' ][ 'valueUnit' . $device ] : ( isset( $attributes[ $attributePrefix . 'Sizing' ]['valueUnit'] ) ? $attributes[ $attributePrefix . 'Sizing' ]['valueUnit'] : '' ); |
| 194 |
|
| 195 |
if ( empty( $sizeUnit ) ) { |
| 196 |
$sizeUnit = 'px'; |
| 197 |
} |
| 198 |
|
| 199 |
$iconViewCSS['font-size'] = $sizeValue . $sizeUnit; |
| 200 |
} |
| 201 |
// Remove font size for fixing |
| 202 |
if ( isset( $attributes[ $attributePrefix . 'ImageUrl' ] ) && ! empty( $attributes[ $attributePrefix . 'ImageUrl' ] ) ) { |
| 203 |
$iconViewCSS['font-size'] = 'unset'; |
| 204 |
} |
| 205 |
|
| 206 |
return array_merge( |
| 207 |
[ 'background' => $backgroundColor ], |
| 208 |
$iconViewCSS, |
| 209 |
'default' !== $iconType ? array_merge( |
| 210 |
Dimensions::get_css( $attributes[ $attributePrefix . 'Padding' ], 'padding', $device ), |
| 211 |
Dimensions::get_css( $attributes[ $attributePrefix . 'BorderRadius' ], 'border-radius', $device ), |
| 212 |
Dimensions::get_css( $attributes[ $attributePrefix . 'BorderWidth' ], 'border-width', $device ) |
| 213 |
) : [] |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
public static function get_element_css( $attributes, $device = '', $attributePrefix = 'icon' ) { |
| 218 |
$iconType = $attributes[ $attributePrefix . 'Type' ] ?? 'default'; |
| 219 |
$iconShape = $attributes[ $attributePrefix . 'Shape' ] ?? ''; |
| 220 |
$primaryColor = $attributes[ $attributePrefix . 'Color' ] ?? '#69727d'; |
| 221 |
$rotate = ! empty( $attributes[ $attributePrefix . 'Rotated' ][ 'value' . $device ] ) ?? 0; |
| 222 |
$iconViewCSS = []; |
| 223 |
|
| 224 |
if ( ! empty( $attributes[ $attributePrefix . 'Rotated' ][ 'value' . $device ] ) ) { |
| 225 |
$rotate = $attributes[ $attributePrefix . 'Rotated' ][ 'value' . $device ]; |
| 226 |
} |
| 227 |
|
| 228 |
if ( $iconType !== 'default' ) { |
| 229 |
$iconViewCSS['fill'] = $primaryColor; |
| 230 |
} |
| 231 |
|
| 232 |
if ( $rotate ) { |
| 233 |
$iconViewCSS['transform'] = 'rotate(' . $rotate . 'deg)'; |
| 234 |
} |
| 235 |
|
| 236 |
return array_merge( |
| 237 |
[ 'fill' => $primaryColor ], |
| 238 |
$iconViewCSS |
| 239 |
); |
| 240 |
} |
| 241 |
|
| 242 |
public static function get_element_image_css( $attributes, $device = '', $attributePrefix = 'icon' ) { |
| 243 |
$rotate = ! empty( $attributes[ $attributePrefix . 'Rotated' ][ 'value' . $device ] ) ?? 0; |
| 244 |
$iconViewCSS = []; |
| 245 |
|
| 246 |
if ( isset( $attributes[ $attributePrefix . 'Rotated' ][ 'value' . $device ] ) ) { |
| 247 |
$rotate = $attributes[ $attributePrefix . 'Rotated' ][ 'value' . $device ]; |
| 248 |
} |
| 249 |
|
| 250 |
if ( $rotate ) { |
| 251 |
$iconViewCSS['transform'] = 'rotate(' . $rotate . 'deg)'; |
| 252 |
} |
| 253 |
if ( isset( $attributes[ $attributePrefix . 'ImageUrl' ] ) ) { |
| 254 |
$image_size_value = isset( $attributes[ $attributePrefix . 'Sizing' ][ 'value' . $device ] ) |
| 255 |
? $attributes[ $attributePrefix . 'Sizing' ][ 'value' . $device ] |
| 256 |
: $attributes[ $attributePrefix . 'Sizing' ]['value']; |
| 257 |
|
| 258 |
$image_size_unit = isset( $attributes[ $attributePrefix . 'Sizing' ][ 'valueUnit' . $device ] ) |
| 259 |
? $attributes[ $attributePrefix . 'Sizing' ][ 'valueUnit' . $device ] |
| 260 |
: ( isset( $attributes[ $attributePrefix . 'Sizing' ]['valueUnit'] ) |
| 261 |
? $attributes[ $attributePrefix . 'Sizing' ]['valueUnit'] |
| 262 |
: '' ); |
| 263 |
|
| 264 |
if ( empty( $image_size_unit ) ) { |
| 265 |
$image_size_unit = 'px'; |
| 266 |
} |
| 267 |
|
| 268 |
$iconViewCSS['width'] = "{$image_size_value}{$image_size_unit}"; |
| 269 |
} |
| 270 |
|
| 271 |
return array_merge( |
| 272 |
[ |
| 273 |
'height' => 'auto' |
| 274 |
], |
| 275 |
$iconViewCSS |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
public static function get_element_image_hover_css( $attributes, $device = '', $attributePrefix = 'icon' ) { |
| 280 |
$rotateHover = $attributes[ $attributePrefix . 'RotateHover' ][ 'value' . $device ] ?? 0; |
| 281 |
$transitionHover = $attributes[ $attributePrefix . 'transitionDuration' ] ?? 0; |
| 282 |
$iconViewCSS = []; |
| 283 |
if ( isset( $attributes[ $attributePrefix . 'RotateHover' ][ 'value' . $device ] ) ) { |
| 284 |
$rotateHover = $attributes[ $attributePrefix . 'RotateHover' ][ 'value' . $device ]; |
| 285 |
} |
| 286 |
if ( $rotateHover ) { |
| 287 |
$iconViewCSS['transform'] = 'rotate(' . $rotateHover . 'deg)'; |
| 288 |
} |
| 289 |
if ( $transitionHover ) { |
| 290 |
$iconViewCSS['transition'] = $transitionHover . 's'; |
| 291 |
} |
| 292 |
|
| 293 |
return array_merge( |
| 294 |
$iconViewCSS |
| 295 |
); |
| 296 |
} |
| 297 |
} |
| 298 |
|