Background.php
1 year ago
BackgroundDefaults.php
2 years ago
BackgroundImage.php
1 year ago
BackgroundOverlay.php
1 year ago
BackgroundSlideshow.php
1 year ago
BackgroundVideo.php
1 year ago
Background.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Background; |
| 4 | |
| 5 | use Kubio\Config; |
| 6 | use Kubio\Core\Element; |
| 7 | use Kubio\Core\LodashBasic; |
| 8 | use Kubio\Core\Styles\Utils; |
| 9 | |
| 10 | class Background extends Element { |
| 11 | |
| 12 | private $backgroundByType = array( |
| 13 | 'video' => BackgroundVideo::class, |
| 14 | 'slideshow' => BackgroundSlideshow::class, |
| 15 | 'image' => BackgroundImage::class, |
| 16 | ); |
| 17 | |
| 18 | function __construct( $tag_name, $props, $children, $block = null ) { |
| 19 | $this->default = BackgroundDefaults::getDefaultBackground(); |
| 20 | |
| 21 | if ( $block ) { |
| 22 | $this->value = $block->backgroundByMedia(); |
| 23 | } |
| 24 | |
| 25 | $bgs = array(); |
| 26 | |
| 27 | $default = LodashBasic::get( $this->value, 'desktop', array() ); |
| 28 | foreach ( $this->value as $media => $value_on_media ) { |
| 29 | if ( $media !== 'desktop' ) { |
| 30 | $value_on_media = LodashBasic::merge( $default, $value_on_media ); |
| 31 | } |
| 32 | $bgs[] = $this->backgroundOnMedia( $media, $value_on_media ); |
| 33 | } |
| 34 | |
| 35 | parent::__construct( |
| 36 | Element::DIV, |
| 37 | LodashBasic::merge( |
| 38 | array( |
| 39 | 'className' => 'background-wrapper', |
| 40 | ), |
| 41 | $props |
| 42 | ), |
| 43 | $bgs, |
| 44 | $block |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | function backgroundOnMedia( $media, $value ) { |
| 50 | $mergedValue = LodashBasic::merge( $this->default, $value ); |
| 51 | $types = Config::value( 'props.background.enums.types' ); |
| 52 | |
| 53 | $children = array(); |
| 54 | if ( isset( $value['overlay'] ) ) { |
| 55 | $enabled = LodashBasic::get( $value, 'overlay.enabled', false ); |
| 56 | if ( $enabled ) { |
| 57 | $children[] = new BackgroundOverlay( $value['overlay'] ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | $type = LodashBasic::get( $mergedValue, 'type', $types['NONE'] ); |
| 62 | if ( $type !== $types['NONE'] ) { |
| 63 | if ( isset( $this->backgroundByType[ $type ] ) && $this->backgroundLayerIsEnabled( $mergedValue ) ) { |
| 64 | $BackgroundClass = $this->backgroundByType[ $type ]; |
| 65 | $children[] = new $BackgroundClass( LodashBasic::get( $value, $type ) ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return new Element( |
| 70 | Element::DIV, |
| 71 | array( |
| 72 | 'className' => $this->backgroundOnMediaClass( $media ), |
| 73 | ), |
| 74 | $children, |
| 75 | $this->block |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | function backgroundOnMediaClass( $media ) { |
| 80 | $className = Utils::composeClassForMedia( |
| 81 | $media, |
| 82 | '', |
| 83 | 'background-layer-media-container', |
| 84 | true |
| 85 | ); |
| 86 | |
| 87 | return array( 'background-layer', $className ); |
| 88 | } |
| 89 | |
| 90 | function backgroundLayerIsEnabled( $background ) { |
| 91 | $type = LodashBasic::get( $background, 'type' ); |
| 92 | switch ( $type ) { |
| 93 | case 'image': |
| 94 | $useParallax = LodashBasic::get( $background, 'image.0.useParallax', false ); |
| 95 | $useFeaturedImage = LodashBasic::get( $background, 'image.0.useFeaturedImage', false ); |
| 96 | $forceBackgroundLayer = LodashBasic::get( $background, 'image.0.forceBackgroundLayer', false ); |
| 97 | return ! ! $useParallax || ! ! $useFeaturedImage || ! ! $forceBackgroundLayer; |
| 98 | default: |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |