Separator.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Separators; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Config; |
| 7 | use Kubio\Core\Element; |
| 8 | use Kubio\Core\LodashBasic; |
| 9 | use Kubio\Core\StyleManager\ParserUtils; |
| 10 | use Kubio\Core\Styles\Utils; |
| 11 | use function file_exists; |
| 12 | use function file_get_contents; |
| 13 | use function sanitize_file_name; |
| 14 | use const KUBIO_ROOT_DIR; |
| 15 | |
| 16 | class Separator extends Element { |
| 17 | |
| 18 | function __construct( $tag_name, $props, $children, $block ) { |
| 19 | parent::__construct( $tag_name, $props, $children, $block ); |
| 20 | |
| 21 | $position = $this->getProp( 'position' ); |
| 22 | $negative = $this->getProp( 'negative' ); |
| 23 | |
| 24 | $enabledByMedia = $this->getProp( 'enabledByMedia' ); |
| 25 | |
| 26 | $visibilityPerMedia = $this->getVisibilityPerMedia( $enabledByMedia ); |
| 27 | |
| 28 | $children = []; |
| 29 | |
| 30 | $height = ParserUtils::toValueUnitString( $this->getProp( 'height' )); |
| 31 | $style = array( |
| 32 | 'fill' => $this->getProp( 'color' ), |
| 33 | 'height' => $height, |
| 34 | ); |
| 35 | |
| 36 | if ( ! $this->getProp( 'overlap' ) ) { |
| 37 | $style['position'] = 'relative'; |
| 38 | } |
| 39 | |
| 40 | $style[ $position ] = 'calc(0px)'; |
| 41 | |
| 42 | $type = sanitize_file_name( $this->getProp( 'type' ) ); |
| 43 | |
| 44 | $top = $position === 'top'; |
| 45 | $shouldUseNegative = $negative && file_exists( KUBIO_ROOT_DIR . "lib/shapes/separators/${type}-negative.svg" ); |
| 46 | |
| 47 | $supportsNegative = file_exists( KUBIO_ROOT_DIR . "lib/shapes/separators/${type}-negative.svg" ); |
| 48 | |
| 49 | if ( ( $shouldUseNegative && $top ) || ( ! $shouldUseNegative && ! $top ) ) { |
| 50 | $style['transform'] = 'rotateX(180deg)'; |
| 51 | } |
| 52 | |
| 53 | if ( $negative && $supportsNegative ) { |
| 54 | $type = $type . '-negative'; |
| 55 | } |
| 56 | |
| 57 | $html = file_get_contents( KUBIO_ROOT_DIR . 'lib/shapes/separators/' . $type . '.svg' ); |
| 58 | $this->extendProps( |
| 59 | array( |
| 60 | 'className' => array_merge( array( 'h-separator', "h-separator--${position}" ), $visibilityPerMedia ), |
| 61 | 'style' => $style, |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | if(in_array( true, array_values( $enabledByMedia ))){ |
| 66 | $medias = $this->getMediaProps($block); |
| 67 | |
| 68 | $media_style = "<style>"; |
| 69 | foreach ( $enabledByMedia as $media => $enabled ) { |
| 70 | if($media === 'desktop') { |
| 71 | continue; |
| 72 | } |
| 73 | if($enabled){ |
| 74 | $parent_class = $this->getBlockStyleRefAsClass($block); |
| 75 | $media_height = ParserUtils::toValueUnitString($this->getHeightForMedia($medias, $media, $position)); |
| 76 | if(empty($media_height)) { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | $media_style .= $this->getStyleForMedia($parent_class, $media, $position, $media_height); |
| 81 | } |
| 82 | } |
| 83 | $media_style .= "</style>"; |
| 84 | $children[] = $media_style; |
| 85 | } |
| 86 | |
| 87 | $children[] = $html; |
| 88 | |
| 89 | $this->setChildren( $children ); |
| 90 | } |
| 91 | |
| 92 | public function getVisibilityPerMedia( $enabledByMedia = array() ) { |
| 93 | $classes = array(); |
| 94 | $prefix = 'h-separator--display'; |
| 95 | foreach ( $enabledByMedia as $media => $enabled ) { |
| 96 | $value = $enabled ? 'flex' : 'none'; |
| 97 | $mediaPrefix = utils::getMediaPrefix( $media ); |
| 98 | $values = LodashBasic::compactWithExceptions( array( $prefix, $value, $mediaPrefix ), array( '0', 0 ) ); |
| 99 | $prefixedClass = implode( '-', $values ); |
| 100 | |
| 101 | $classes[] = $prefixedClass; |
| 102 | } |
| 103 | return $classes; |
| 104 | } |
| 105 | |
| 106 | public function getMediaProps($block){ |
| 107 | $separator_element = $block->separatorElement; |
| 108 | $key = "attrs.kubio.style.descendants.${separator_element}.media"; |
| 109 | |
| 110 | return Arr::get($block->block_data, $key); |
| 111 | } |
| 112 | |
| 113 | public function getHeightForMedia($array, $media, $position){ |
| 114 | return Arr::get($array, "${media}.separators.${position}.height"); |
| 115 | } |
| 116 | |
| 117 | public function getStyleForMedia($parent_class, $media = 'desktop', $position = 'bottom', $height = '100px' ){ |
| 118 | if($media === 'desktop') { |
| 119 | return ''; |
| 120 | } |
| 121 | $height = str_replace('%', '%%', $height); |
| 122 | $style = ""; |
| 123 | |
| 124 | if($media === 'tablet'){ |
| 125 | $style = __( |
| 126 | "@media (min-width: 768px) and (max-width: 1023px){ |
| 127 | .%s > .h-separator.h-separator--%s { |
| 128 | height: ${height} !important; |
| 129 | } |
| 130 | }\n", |
| 131 | "kubio" |
| 132 | ); |
| 133 | } |
| 134 | else if($media === 'mobile'){ |
| 135 | $style = __( |
| 136 | "@media (max-width: 767px){ |
| 137 | .%s > .h-separator.h-separator--%s { |
| 138 | height: ${height} !important; |
| 139 | } |
| 140 | }\n", |
| 141 | "kubio" |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | return sprintf($style, $parent_class, $position); |
| 146 | } |
| 147 | |
| 148 | public function getBlockStyleRefAsClass($block){ |
| 149 | $style_ref = Arr::get($block->block_data, 'attrs.kubio.styleRef'); |
| 150 | $separator_element = $block->separatorElement; |
| 151 | |
| 152 | return implode('-', ["style", $style_ref, $separator_element] ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 |