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