| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\Helpers\Helper; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
|
| 9 |
class StoreLogoRenderer |
| 10 |
{ |
| 11 |
protected $storeSettings; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->storeSettings = new StoreSettings(); |
| 16 |
} |
| 17 |
|
| 18 |
public function getStoreLogo(): string |
| 19 |
{ |
| 20 |
return $this->storeSettings->get('store_logo.url', ''); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
public function getStoreName(): string |
| 25 |
{ |
| 26 |
return $this->storeSettings->get('store_name', get_bloginfo('name')); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
public function render(array $atts = []): void |
| 31 |
{ |
| 32 |
$defaults = [ |
| 33 |
'is_shortcode' => false |
| 34 |
]; |
| 35 |
|
| 36 |
$atts = wp_parse_args($atts, $defaults); |
| 37 |
|
| 38 |
$isLink = Helper::toBool(Arr::get($atts, 'is_link', true), true); |
| 39 |
$isShortcode = Helper::toBool(Arr::get($atts, 'is_shortcode', false)); |
| 40 |
|
| 41 |
// Custom logo from block attributes |
| 42 |
$customLogoUrl = Arr::get($atts, 'logo_url', ''); |
| 43 |
$atts['logo_url'] = !empty($customLogoUrl) ? $customLogoUrl : $this->getStoreLogo(); |
| 44 |
$atts['store_name'] = $this->getStoreName(); |
| 45 |
|
| 46 |
$wrapperAttributes = ''; |
| 47 |
if (!$isShortcode) { |
| 48 |
$wrapperAttributes = RenderHelper::getBlockWrapperAttributes([ |
| 49 |
'class' => 'fct-store-logo-wrapper' |
| 50 |
]); |
| 51 |
} |
| 52 |
|
| 53 |
?> |
| 54 |
<div <?php echo $wrapperAttributes; ?>> |
| 55 |
<?php |
| 56 |
if ($isLink) { |
| 57 |
$this->renderWithLink($atts); |
| 58 |
} else { |
| 59 |
$this->renderWithoutLink($atts); |
| 60 |
} |
| 61 |
?> |
| 62 |
</div> |
| 63 |
<?php |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
protected function renderWithLink(array $atts): void |
| 68 |
{ |
| 69 |
$linkTarget = Arr::get($atts, 'link_target', '_self'); |
| 70 |
$rel = $linkTarget === '_blank' ? 'noopener noreferrer' : ''; |
| 71 |
|
| 72 |
?> |
| 73 |
<a href="<?php echo esc_url(home_url('/')); ?>" |
| 74 |
target="<?php echo esc_attr($linkTarget); ?>" |
| 75 |
rel="<?php echo esc_attr($rel); ?>" |
| 76 |
class="fct-store-logo-link"> |
| 77 |
<?php $this->renderLogoContent($atts); ?> |
| 78 |
</a> |
| 79 |
<?php |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
protected function renderWithoutLink(array $atts): void |
| 84 |
{ |
| 85 |
?> |
| 86 |
<div class="fct-store-logo-without-link"> |
| 87 |
<?php $this->renderLogoContent($atts); ?> |
| 88 |
</div> |
| 89 |
<?php |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
protected function renderLogoContent(array $atts): void |
| 94 |
{ |
| 95 |
$logoUrl = Arr::get($atts, 'logo_url', ''); |
| 96 |
$storeName = Arr::get($atts, 'store_name', ''); |
| 97 |
|
| 98 |
if (empty($logoUrl)) { |
| 99 |
$this->renderTextFallback($storeName); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$this->renderImage($atts); |
| 104 |
} |
| 105 |
|
| 106 |
protected function renderImage(array $atts): void |
| 107 |
{ |
| 108 |
$maxWidth = absint(Arr::get($atts, 'max_width')); |
| 109 |
$maxHeight = absint(Arr::get($atts, 'max_height')); |
| 110 |
|
| 111 |
// Fallback to defaults if empty or invalid |
| 112 |
$maxWidth = $maxWidth > 0 ? $maxWidth : 150; |
| 113 |
$maxHeight = $maxHeight > 0 ? $maxHeight : 70; |
| 114 |
|
| 115 |
$style = sprintf( |
| 116 |
'--max-width:%dpx; --max-height:%dpx;', |
| 117 |
$maxWidth, |
| 118 |
$maxHeight |
| 119 |
); |
| 120 |
|
| 121 |
?> |
| 122 |
<img src="<?php echo esc_url($atts['logo_url']); ?>" |
| 123 |
alt="<?php echo esc_attr($atts['store_name']); ?>" |
| 124 |
class="fct-store-logo-img" |
| 125 |
style="<?php echo esc_attr($style); ?>"> |
| 126 |
<?php |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
protected function renderTextFallback(string $storeName): void |
| 131 |
{ |
| 132 |
?> |
| 133 |
<span class="fct-store-logo-text"> |
| 134 |
<?php echo esc_html($storeName); ?> |
| 135 |
</span> |
| 136 |
<?php |
| 137 |
} |
| 138 |
} |
| 139 |
|