index.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockBase; |
| 6 | use Kubio\Core\Utils; |
| 7 | use Kubio\Core\Registry; |
| 8 | |
| 9 | class LogoBlock extends BlockBase { |
| 10 | const CONTAINER = 'container'; |
| 11 | const IMAGE = 'image'; |
| 12 | const ALTERNATE_IMAGE = 'alternateImage'; |
| 13 | const TEXT = 'text'; |
| 14 | |
| 15 | private $direction_classes = array( |
| 16 | 'image' => 'kubio-logo-direction-row kubio-logo-without-text', |
| 17 | 'text' => 'kubio-logo-direction-row', |
| 18 | 'imageLeft' => 'kubio-logo-direction-row', |
| 19 | 'imageRight' => 'kubio-logo-direction-row-reverse', |
| 20 | 'imageBelow' => 'kubio-logo-direction-column-reverse', |
| 21 | 'imageAbove' => 'kubio-logo-direction-column', |
| 22 | ); |
| 23 | |
| 24 | private $imageShowData = array( |
| 25 | 'image' => array( |
| 26 | 'showImage' => true, |
| 27 | 'showAlternateImage' => true, |
| 28 | ), |
| 29 | 'text' => array( |
| 30 | 'showImage' => false, |
| 31 | 'showAlternateImage' => false, |
| 32 | ), |
| 33 | 'imageBelow' => array( |
| 34 | 'showImage' => true, |
| 35 | 'showAlternateImage' => true, |
| 36 | ), |
| 37 | 'imageAbove' => array( |
| 38 | 'showImage' => true, |
| 39 | 'showAlternateImage' => true, |
| 40 | ), |
| 41 | 'imageRight' => array( |
| 42 | 'showImage' => true, |
| 43 | 'showAlternateImage' => true, |
| 44 | ), |
| 45 | 'imageLeft' => array( |
| 46 | 'showImage' => true, |
| 47 | 'showAlternateImage' => true, |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | public function computed() { |
| 52 | $iconEnabled = $this->getProp( 'showIcon', false ); |
| 53 | $iconPosition = $this->getProp( 'iconPosition', 'before' ); |
| 54 | $layout_type = $this->getProp( 'layoutType', 'image' ); |
| 55 | $showBeforeIcon = $iconEnabled && $iconPosition === 'before'; |
| 56 | $showAfterIcon = $iconEnabled && $iconPosition === 'after'; |
| 57 | |
| 58 | return array( |
| 59 | 'showBeforeIcon' => $showBeforeIcon, |
| 60 | 'showAfterIcon' => $showAfterIcon, |
| 61 | 'layout_type' => $layout_type, |
| 62 | 'showNormalImage' => $this->imageShowData[ $layout_type ]['showImage'], |
| 63 | 'showAlternateImage' => $this->imageShowData[ $layout_type ]['showAlternateImage'], |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | public function mapPropsToElements() { |
| 68 | $computed = $this->computed(); |
| 69 | if ( $computed['layout_type'] === 'image' ) { |
| 70 | $text = ''; |
| 71 | } else { |
| 72 | $text = \get_bloginfo( 'name' ); |
| 73 | } |
| 74 | |
| 75 | if ( $computed['layout_type'] === 'text' ) { |
| 76 | $image_src = ''; |
| 77 | $alternate_image_src = ''; |
| 78 | } else { |
| 79 | $image_src = $this->getImageLogoUrl(); |
| 80 | $alternate_image_src = $this->getAlternateImageLogoUrl(); |
| 81 | } |
| 82 | |
| 83 | $link_data = array(); |
| 84 | if ( $this->getAttribute( 'linkTo' ) === 'homePage' ) { |
| 85 | $href = ''; |
| 86 | if ( kubio_wpml_is_active() ) { |
| 87 | $href = apply_filters( 'wpml_home_url', site_url() ); |
| 88 | } else { |
| 89 | $href = site_url(); |
| 90 | } |
| 91 | $link_data = array( |
| 92 | 'href' => $href, |
| 93 | ); |
| 94 | } else { |
| 95 | $link_data = Utils::getLinkAttributes( $this->getAttribute( 'link' ) ); |
| 96 | } |
| 97 | |
| 98 | $map[ self::CONTAINER ] = array_merge( |
| 99 | array( |
| 100 | 'className' => array( |
| 101 | $this->direction_classes[ $computed['layout_type'] ], |
| 102 | $this->getAttribute( 'mode', 'default' ), |
| 103 | ), |
| 104 | ), |
| 105 | $link_data |
| 106 | ); |
| 107 | |
| 108 | if ( $this->imageShowData[ $computed['layout_type'] ]['showImage'] ) { |
| 109 | $map[ self::IMAGE ] = array( |
| 110 | 'alt' => esc_attr( $this->getAttribute( 'alt', '' ) ), |
| 111 | 'src' => esc_url( $image_src ), |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | if ( $this->imageShowData[ $computed['layout_type'] ]['showImage'] ) { |
| 116 | $map[ self::ALTERNATE_IMAGE ] = array( |
| 117 | 'alt' => esc_attr( $this->getAttribute( 'alt', '' ) ), |
| 118 | 'src' => esc_url( $alternate_image_src ), |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | $map[ self::TEXT ] = array( |
| 123 | 'innerHTML' => wp_kses_post( $text ), |
| 124 | ); |
| 125 | return $map; |
| 126 | } |
| 127 | |
| 128 | private function getImageLogoUrl() { |
| 129 | $custom_logo_id = get_theme_mod( 'custom_logo', false ); |
| 130 | if ( ! $custom_logo_id ) { |
| 131 | $placeholder = kubio_url( '/static/default-assets/logo-fallback.png' ); |
| 132 | return $placeholder; |
| 133 | } |
| 134 | return wp_get_attachment_image_url( $custom_logo_id, 'full' ); |
| 135 | } |
| 136 | |
| 137 | private function getAlternateImageLogoUrl() { |
| 138 | $alternateImage = kubio_get_global_data( 'alternateLogo' ); |
| 139 | if ( $alternateImage ) { |
| 140 | return $alternateImage; |
| 141 | } |
| 142 | return $this->getImageLogoUrl(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | Registry::registerBlock( __DIR__, LogoBlock::class ); |
| 147 |