| 1 |
<?php |
| 2 |
|
| 3 |
namespace Barn2\PTS_Lib; |
| 4 |
|
| 5 |
/** |
| 6 |
* Utility functions for building CSS. |
| 7 |
* |
| 8 |
* @package Barn2\barn2-lib |
| 9 |
* @author Barn2 Plugins <support@barn2.com> |
| 10 |
* @license GPL-3.0 |
| 11 |
* @copyright Barn2 Media Ltd |
| 12 |
* @version 1.1 |
| 13 |
*/ |
| 14 |
class CSS_Util { |
| 15 |
|
| 16 |
public static function build_background_style( $bg_color, $important = false ) { |
| 17 |
if ( ! $bg_color ) { |
| 18 |
return ''; |
| 19 |
} |
| 20 |
|
| 21 |
$important_dec = $important ? ' !important' : ''; |
| 22 |
|
| 23 |
return sprintf( 'background-color: %1$s%2$s;', $bg_color, $important_dec ); |
| 24 |
} |
| 25 |
|
| 26 |
public static function build_border_style( $option, $borders = 'all', $important = false ) { |
| 27 |
$option = wp_parse_args( |
| 28 |
$option, |
| 29 |
[ |
| 30 |
'size' => '', |
| 31 |
'color' => '' |
| 32 |
] |
| 33 |
); |
| 34 |
|
| 35 |
if ( ! is_numeric( $option['size'] ) && empty( $option['color'] ) ) { |
| 36 |
return ''; |
| 37 |
} |
| 38 |
|
| 39 |
if ( ! is_array( $borders ) ) { |
| 40 |
$borders = array_filter( (array) $borders ); |
| 41 |
} |
| 42 |
|
| 43 |
$result = ''; |
| 44 |
$border_size = is_numeric( $option['size'] ) ? $option['size'] . 'px' : ''; |
| 45 |
$border_color = $option['color']; |
| 46 |
$important_dec = $important ? ' !important' : ''; |
| 47 |
|
| 48 |
foreach ( $borders as $border ) { |
| 49 |
$border_edge = ''; |
| 50 |
|
| 51 |
if ( in_array( $border, [ 'top', 'left', 'bottom', 'right' ], true ) ) { |
| 52 |
$border_edge = $border . '-'; |
| 53 |
} |
| 54 |
|
| 55 |
if ( $border_size || $border_color ) { |
| 56 |
$result .= sprintf( 'border-%1$sstyle: solid%2$s;', $border_edge, $important_dec ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( $border_size ) { |
| 60 |
$result .= sprintf( 'border-%1$swidth: %2$s%3$s;', $border_edge, $border_size, $important_dec ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( $border_color ) { |
| 64 |
$result .= sprintf( 'border-%1$scolor: %2$s%3$s;', $border_edge, $border_color, $important_dec ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $result; |
| 69 |
} |
| 70 |
|
| 71 |
public static function build_font_style( $option, $important = false ) { |
| 72 |
$option = wp_parse_args( |
| 73 |
$option, |
| 74 |
[ |
| 75 |
'size' => '', |
| 76 |
'color' => '' |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
if ( ! is_numeric( $option['size'] ) && empty( $option['color'] ) ) { |
| 81 |
return ''; |
| 82 |
} |
| 83 |
|
| 84 |
$style = ''; |
| 85 |
$important_dec = $important ? ' !important' : ''; |
| 86 |
|
| 87 |
if ( is_numeric( $option['size'] ) ) { |
| 88 |
$style .= sprintf( 'font-size: %1$upx%2$s;', $option['size'], $important_dec ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! empty( $option['color'] ) ) { |
| 92 |
$style .= sprintf( 'color: %1$s%2$s;', $option['color'], $important_dec ); |
| 93 |
} |
| 94 |
|
| 95 |
return $style; |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|