| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Maker\Style |
| 5 |
* Creates the CSS style for Button. |
| 6 |
* |
| 7 |
* @package ButtonGenerator |
| 8 |
* @subpackage Public |
| 9 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 10 |
* @copyright 2024 Dmytro Lobov |
| 11 |
* @license GPL-2.0+ |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace ButtonGenerator\Maker; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
class Style { |
| 19 |
/** |
| 20 |
* @var mixed |
| 21 |
*/ |
| 22 |
private $id; |
| 23 |
/** |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
private $param; |
| 27 |
|
| 28 |
public function __construct( $id, $param ) { |
| 29 |
$this->id = $id; |
| 30 |
$this->param = $param; |
| 31 |
} |
| 32 |
|
| 33 |
public function init(): string { |
| 34 |
|
| 35 |
$css = $this->main_style(); |
| 36 |
$css .= $this->icon_style(); |
| 37 |
$css .= $this->mobile_rules(); |
| 38 |
|
| 39 |
return trim( preg_replace( '~\s+~', ' ', $css ) ); |
| 40 |
} |
| 41 |
|
| 42 |
private function main_style(): string { |
| 43 |
$param = $this->param; |
| 44 |
$properties = $this->btn_properties(); |
| 45 |
$css = '.btg-button-' . absint( $this->id ) . '{'; |
| 46 |
|
| 47 |
if ( $param['type'] === 'floating' ) { |
| 48 |
$css .= $this->floating(); |
| 49 |
} |
| 50 |
|
| 51 |
foreach ( $properties as $property => $variable ) { |
| 52 |
|
| 53 |
if ( ! isset( $param[ $property ] ) ) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
if ( $param[ $property ] === $variable[1] ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
if ( $property === 'shadow' ) { |
| 62 |
$shadow = "{$param['shadow_h_offset']}px {$param['shadow_v_offset']}px {$param['shadow_blur']}px {$param['shadow_spread']}px {$param['shadow_color']}"; |
| 63 |
$inset = $param[ $property ] === 'inset' ? 'inset ' : ''; |
| 64 |
$css .= "{$variable[0]}: {$inset}{$shadow};"; |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
if ( isset( $variable[2] ) && is_string( $variable[2] ) ) { |
| 69 |
$css .= "{$variable[0]}: {$param[ $property ]}{$variable[2]};"; |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$css .= "{$variable[0]}: {$param[ $property ]};"; |
| 74 |
} |
| 75 |
|
| 76 |
$css .= '}'; |
| 77 |
|
| 78 |
return $css; |
| 79 |
} |
| 80 |
|
| 81 |
private function floating(): string { |
| 82 |
$param = $this->param; |
| 83 |
$css = '--position: fixed;'; |
| 84 |
|
| 85 |
$location = $param['location'] ?? 'topLeft'; |
| 86 |
$top = $param['location_top'] ?? '0'; |
| 87 |
$bottom = $param['location_bottom'] ?? '0'; |
| 88 |
$left = $param['location_left'] ?? '0'; |
| 89 |
$right = $param['location_right'] ?? '0'; |
| 90 |
|
| 91 |
switch ( $location ) { |
| 92 |
case 'topLeft': |
| 93 |
$css .= "top:{$top}px; left:{$left}px;"; |
| 94 |
break; |
| 95 |
case 'topCenter': |
| 96 |
$css .= "top:{$top}px;"; |
| 97 |
break; |
| 98 |
case 'topRight': |
| 99 |
$css .= "top:{$top}px; right:{$right}px;"; |
| 100 |
break; |
| 101 |
case 'bottomLeft': |
| 102 |
$css .= "bottom:{$bottom}px; left:{$left}px;"; |
| 103 |
break; |
| 104 |
case 'bottomCenter': |
| 105 |
$css .= "bottom:{$bottom}px;"; |
| 106 |
break; |
| 107 |
case 'bottomRight': |
| 108 |
$css .= "bottom:{$bottom}px; right:{$right}px;"; |
| 109 |
break; |
| 110 |
case 'left': |
| 111 |
$css .= "left:{$left}px;"; |
| 112 |
break; |
| 113 |
case 'right': |
| 114 |
$css .= "right:{$right}px;"; |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
return $css; |
| 119 |
} |
| 120 |
|
| 121 |
private function btn_properties(): array { |
| 122 |
|
| 123 |
$arg = [ |
| 124 |
'text_location' => [ '--direction', 'row' ], |
| 125 |
'gap' => [ '--gap', '8', 'px' ], |
| 126 |
'zindex' => [ '--z-index', '999' ], |
| 127 |
'width' => [ '--width', '100px' ], |
| 128 |
'height' => [ '--height', '50px' ], |
| 129 |
'rotate_button' => [ '--rotate', '0deg' ], |
| 130 |
'color' => [ '--color', '#ffffff' ], |
| 131 |
'background' => [ '--background', '#1f9ef8' ], |
| 132 |
'hover_color' => [ '--hover-color', '#ffffff' ], |
| 133 |
'hover_background' => [ '--hover-background', '#0090f7' ], |
| 134 |
'icon_hover_color' => [ '--icon-hover-color', '#ffffff' ], |
| 135 |
'border_radius' => [ '--radius', '1px' ], |
| 136 |
'border_style' => [ '--border-style', 'none' ], |
| 137 |
'border_color' => [ '--border-color', '#383838' ], |
| 138 |
'border_width' => [ '--border-width', '1', 'px' ], |
| 139 |
'shadow' => [ '--shadow', 'none' ], |
| 140 |
'font_size' => [ '--font-size', '16', 'px' ], |
| 141 |
'font_family' => [ '--font-family', 'inherit' ], |
| 142 |
'font_weight' => [ '--font-weight', 'normal' ], |
| 143 |
'font_style' => [ '--font-style', 'normal' ], |
| 144 |
'transition_duration' => [ '--transition-duration', ' 0.2', 's' ], |
| 145 |
'transition_function' => [ '--transition-function', ' ease-out' ], |
| 146 |
]; |
| 147 |
|
| 148 |
return $arg; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
private function icon_style(): string { |
| 153 |
$param = $this->param; |
| 154 |
$css = '.btg-button-' . absint( $this->id ) . ' .btg-icon, .btg-button-' . absint( $this->id ) . ' img.btg-icon{'; |
| 155 |
|
| 156 |
if ( $param['rotate_icon'] === 'custom' ) { |
| 157 |
$css .= '--rotate: ' . esc_attr( $param['rotate_icon_custom'] ) . 'deg;'; |
| 158 |
} |
| 159 |
$icon_size = $param['icon_size'] ?? 16; |
| 160 |
$icon_color = $param['icon_color'] ?? '#ffffff'; |
| 161 |
$css .= '--font-size: ' . esc_attr( $icon_size ) . 'px;'; |
| 162 |
$css .= '--color: ' . esc_attr( $icon_color ) . ';'; |
| 163 |
$css .= '}'; |
| 164 |
|
| 165 |
return $css; |
| 166 |
} |
| 167 |
private function mobile_rules(): string { |
| 168 |
$param = $this->param; |
| 169 |
$css = ''; |
| 170 |
|
| 171 |
if ( ! empty( $param['mobile_on'] ) ) { |
| 172 |
$mobile = $param['mobile'] ?? 480; |
| 173 |
$css .= "@media only screen and (max-width: {$mobile}px){ |
| 174 |
.btg-button-$this->id { |
| 175 |
display:none; |
| 176 |
} |
| 177 |
}"; |
| 178 |
} |
| 179 |
|
| 180 |
if ( ! empty( $param['desktop_on'] ) ) { |
| 181 |
$desktop = $param['desktop'] ?? 1024; |
| 182 |
$css .= "@media only screen and (min-width: {$desktop}px){ |
| 183 |
.btg-button-$this->id { |
| 184 |
display:none; |
| 185 |
} |
| 186 |
}"; |
| 187 |
} |
| 188 |
|
| 189 |
return $css; |
| 190 |
} |
| 191 |
} |