| 1 |
<?php |
| 2 |
namespace Easyel\EasyElements\Widgets; |
| 3 |
use Elementor\Controls_Manager; |
| 4 |
use Elementor\Utils; |
| 5 |
use Elementor\Widget_Base; |
| 6 |
use Elementor\Group_Control_Typography; |
| 7 |
use Elementor\Group_Control_Text_Shadow; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class Easyel_Site_Logo_Widget extends \Elementor\Widget_Base { |
| 14 |
|
| 15 |
public function get_name() { |
| 16 |
return 'eel-site-logo'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_title() { |
| 20 |
return __( 'Site Logo', 'easy-elements' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function get_icon() { |
| 24 |
return 'easyicon easyelIcon-site-logo'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_categories() { |
| 28 |
return [ 'easyelements_header_footer_category' ]; |
| 29 |
} |
| 30 |
|
| 31 |
public function get_keywords() { |
| 32 |
return [ 'site', 'logo', 'link', 'image', 'text' ]; |
| 33 |
} |
| 34 |
|
| 35 |
public function get_style_depends() { |
| 36 |
return [ |
| 37 |
'eel-site-logo', |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
protected function register_controls() { |
| 43 |
$this->start_controls_section( 'section_logo', [ |
| 44 |
'label' => __( 'Site Logo Settings', 'easy-elements' ), |
| 45 |
] ); |
| 46 |
$this->add_control( 'site_logo_fallback', [ |
| 47 |
'label' => __( 'Use Custom Logo?', 'easy-elements' ), |
| 48 |
'type' => Controls_Manager::SWITCHER, |
| 49 |
'default' => '', |
| 50 |
] ); |
| 51 |
$this->add_control( 'custom_image', [ |
| 52 |
'label' => __( 'Custom Logo Image', 'easy-elements' ), |
| 53 |
'type' => Controls_Manager::MEDIA, |
| 54 |
'dynamic' => [ 'active' => true ], |
| 55 |
'default' => [ 'url' => Utils::get_placeholder_image_src() ], |
| 56 |
'condition' => [ 'site_logo_fallback' => 'yes' ], |
| 57 |
] ); |
| 58 |
$this->add_control( 'custom_sticky_image', [ |
| 59 |
'label' => __( 'Custom Sticky Logo Image', 'easy-elements' ), |
| 60 |
'type' => Controls_Manager::MEDIA, |
| 61 |
'dynamic' => [ 'active' => true ], |
| 62 |
'default' => [ 'url' => Utils::get_placeholder_image_src() ], |
| 63 |
'condition' => [ 'site_logo_fallback' => 'yes' ], |
| 64 |
] ); |
| 65 |
$this->add_responsive_control( |
| 66 |
'logo_height', |
| 67 |
[ |
| 68 |
'label' => esc_html__( 'Logo Width', 'easy-elements' ), |
| 69 |
'type' => \Elementor\Controls_Manager::SLIDER, |
| 70 |
'size_units' => [ 'px', '%', 'em', 'vh' ], |
| 71 |
'range' => [ |
| 72 |
'px' => [ |
| 73 |
'min' => 10, |
| 74 |
'max' => 500, |
| 75 |
'step' => 1, |
| 76 |
], |
| 77 |
'%' => [ |
| 78 |
'min' => 1, |
| 79 |
'max' => 100, |
| 80 |
], |
| 81 |
'em' => [ |
| 82 |
'min' => 0.1, |
| 83 |
'max' => 10, |
| 84 |
], |
| 85 |
], |
| 86 |
'default' => [ |
| 87 |
'unit' => 'px', |
| 88 |
'size' => '', |
| 89 |
], |
| 90 |
'selectors' => [ |
| 91 |
'{{WRAPPER}} .eel-site-logo a img, {{WRAPPER}} .eel-site-logo img' => 'width: {{SIZE}}{{UNIT}}; height: auto;', |
| 92 |
], |
| 93 |
] |
| 94 |
); |
| 95 |
$this->add_responsive_control( 'align', [ |
| 96 |
'label' => __( 'Alignment', 'easy-elements' ), |
| 97 |
'type' => Controls_Manager::CHOOSE, |
| 98 |
'options' => [ |
| 99 |
'left' => [ 'title' => __( 'Left', 'easy-elements' ), 'icon' => 'eicon-h-align-left' ], |
| 100 |
'center' => [ 'title' => __( 'Center', 'easy-elements' ), 'icon' => 'eicon-h-align-center' ], |
| 101 |
'right' => [ 'title' => __( 'Right', 'easy-elements' ), 'icon' => 'eicon-h-align-right' ], |
| 102 |
], |
| 103 |
'default' => 'left', |
| 104 |
'selectors' => [ |
| 105 |
'{{WRAPPER}} .eel-site-logo' => 'justify-content: {{VALUE}};', |
| 106 |
], |
| 107 |
] ); |
| 108 |
$this->add_control( 'caption_source', [ |
| 109 |
'label' => __( 'Show Caption?', 'easy-elements' ), |
| 110 |
'type' => Controls_Manager::SWITCHER, |
| 111 |
'default' => '', |
| 112 |
] ); |
| 113 |
$this->add_control( 'caption', [ |
| 114 |
'label' => __( 'Custom Caption', 'easy-elements' ), |
| 115 |
'type' => Controls_Manager::TEXT, |
| 116 |
'dynamic' => [ 'active' => true ], |
| 117 |
'condition' => [ 'caption_source' => 'yes' ], |
| 118 |
] ); |
| 119 |
$this->add_control( 'link_to', [ |
| 120 |
'label' => __( 'Link To', 'easy-elements' ), |
| 121 |
'type' => Controls_Manager::SELECT, |
| 122 |
'default' => 'default', |
| 123 |
'options' => [ |
| 124 |
'default' => __( 'Home Page', 'easy-elements' ), |
| 125 |
'none' => __( 'None', 'easy-elements' ), |
| 126 |
'file' => __( 'Media File', 'easy-elements' ), |
| 127 |
'custom' => __( 'Custom URL', 'easy-elements' ), |
| 128 |
], |
| 129 |
] ); |
| 130 |
$this->add_control( 'link', [ |
| 131 |
'label' => __( 'Custom URL', 'easy-elements' ), |
| 132 |
'type' => Controls_Manager::URL, |
| 133 |
'dynamic' => [ 'active' => true ], |
| 134 |
'placeholder' => __( 'https://your-link.com', 'easy-elements' ), |
| 135 |
'condition' => [ 'link_to' => 'custom' ], |
| 136 |
'show_label' => false, |
| 137 |
] ); |
| 138 |
$this->end_controls_section(); |
| 139 |
|
| 140 |
// Caption Style Section |
| 141 |
$this->start_controls_section( |
| 142 |
'section_caption_style', |
| 143 |
[ |
| 144 |
'label' => __( 'Caption Style', 'easy-elements' ), |
| 145 |
'tab' => Controls_Manager::TAB_STYLE, |
| 146 |
'condition' => [ 'caption_source' => 'yes' ], |
| 147 |
] |
| 148 |
); |
| 149 |
|
| 150 |
$this->add_control( |
| 151 |
'caption_color', |
| 152 |
[ |
| 153 |
'label' => __( 'Color', 'easy-elements' ), |
| 154 |
'type' => Controls_Manager::COLOR, |
| 155 |
'selectors' => [ |
| 156 |
'{{WRAPPER}} .eel-site-logo .wp-caption-text' => 'color: {{VALUE}};', |
| 157 |
], |
| 158 |
] |
| 159 |
); |
| 160 |
|
| 161 |
$this->add_group_control( |
| 162 |
Group_Control_Typography::get_type(), |
| 163 |
[ |
| 164 |
'name' => 'caption_typography', |
| 165 |
'label' => __( 'Typography', 'easy-elements' ), |
| 166 |
'selector' => '{{WRAPPER}} .eel-site-logo .wp-caption-text', |
| 167 |
] |
| 168 |
); |
| 169 |
|
| 170 |
$this->add_group_control( |
| 171 |
Group_Control_Text_Shadow::get_type(), |
| 172 |
[ |
| 173 |
'name' => 'caption_text_shadow', |
| 174 |
'label' => __( 'Text Shadow', 'easy-elements' ), |
| 175 |
'selector' => '{{WRAPPER}} .eel-site-logo .wp-caption-text', |
| 176 |
] |
| 177 |
); |
| 178 |
|
| 179 |
$this->add_responsive_control( |
| 180 |
'caption_margin', |
| 181 |
[ |
| 182 |
'label' => __( 'Margin', 'easy-elements' ), |
| 183 |
'type' => Controls_Manager::DIMENSIONS, |
| 184 |
'size_units' => [ 'px', 'em', '%' ], |
| 185 |
'selectors' => [ |
| 186 |
'{{WRAPPER}} .eel-site-logo .wp-caption-text' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 187 |
], |
| 188 |
] |
| 189 |
); |
| 190 |
|
| 191 |
$this->end_controls_section(); |
| 192 |
} |
| 193 |
|
| 194 |
protected function get_logo_or_fallback( $settings ) { |
| 195 |
// Helper to get image URL from attachment ID |
| 196 |
$get_image_url = function( $id ) { |
| 197 |
$img = wp_get_attachment_image_src( $id, 'full' ); |
| 198 |
return $img ? $img[0] : false; |
| 199 |
}; |
| 200 |
|
| 201 |
// 1. Custom logo from widget |
| 202 |
if ( 'yes' === $settings['site_logo_fallback'] ) { |
| 203 |
if ( ! empty( $settings['custom_image']['id'] ) ) { |
| 204 |
if ( $url = $get_image_url( $settings['custom_image']['id'] ) ) { |
| 205 |
return [ 'type' => 'image', 'url' => $url ]; |
| 206 |
} |
| 207 |
} elseif ( ! empty( $settings['custom_image']['url'] ) ) { |
| 208 |
return [ 'type' => 'image', 'url' => $settings['custom_image']['url'] ]; |
| 209 |
} |
| 210 |
if ( ! empty( $settings['custom_sticky_image']['id'] ) ) { |
| 211 |
if ( $url = $get_image_url( $settings['custom_sticky_image']['id'] ) ) { |
| 212 |
return [ 'type' => 'image', 'url' => $url ]; |
| 213 |
} |
| 214 |
} elseif ( ! empty( $settings['custom_sticky_image']['url'] ) ) { |
| 215 |
return [ 'type' => 'image', 'url' => $settings['custom_sticky_image']['url'] ]; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// 2. Kirki logo (entro_logo) |
| 220 |
$kirki_logo = null; |
| 221 |
if ( class_exists( '\Kirki' ) ) { |
| 222 |
$kirki_logo = \Kirki::get_option( 'entro', 'site_logo' ); |
| 223 |
} |
| 224 |
if ( is_array( $kirki_logo ) && ! empty( $kirki_logo['url'] ) ) { |
| 225 |
return [ 'type' => 'image', 'url' => $kirki_logo['url'] ]; |
| 226 |
} |
| 227 |
if ( is_string( $kirki_logo ) && filter_var( $kirki_logo, FILTER_VALIDATE_URL ) ) { |
| 228 |
return [ 'type' => 'image', 'url' => $kirki_logo ]; |
| 229 |
} |
| 230 |
|
| 231 |
// 3. WP Site Identity logo |
| 232 |
if ( $logo_id = get_theme_mod( 'custom_logo' ) ) { |
| 233 |
if ( $url = $get_image_url( $logo_id ) ) { |
| 234 |
return [ 'type' => 'image', 'url' => $url ]; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// 4. Kirki site title and tagline fallback |
| 239 |
$kirki_title = null; |
| 240 |
$kirki_tagline = null; |
| 241 |
if ( class_exists( '\Kirki' ) ) { |
| 242 |
$kirki_title = \Kirki::get_option( 'entro', 'site_title' ); |
| 243 |
$kirki_tagline = \Kirki::get_option( 'entro', 'site_tagline' ); |
| 244 |
} |
| 245 |
if ( ! empty( $kirki_title ) ) { |
| 246 |
return [ |
| 247 |
'type' => 'text', |
| 248 |
'title' => $kirki_title, |
| 249 |
'tagline' => $kirki_tagline, |
| 250 |
]; |
| 251 |
} |
| 252 |
|
| 253 |
// 5. Site title and tagline fallback |
| 254 |
return [ |
| 255 |
'type' => 'text', |
| 256 |
'title' => get_bloginfo( 'name' ), |
| 257 |
'tagline' => get_bloginfo( 'description' ), |
| 258 |
]; |
| 259 |
} |
| 260 |
|
| 261 |
protected function render() { |
| 262 |
$settings = $this->get_settings_for_display(); |
| 263 |
$logo_data = $this->get_logo_or_fallback( $settings ); |
| 264 |
$caption = ! empty( $settings['caption_source'] ) && 'yes' === $settings['caption_source'] ? $settings['caption'] : ''; |
| 265 |
$alt_text = esc_attr( get_bloginfo( 'name' ) ); |
| 266 |
|
| 267 |
$this->add_render_attribute( 'wrapper', 'class', 'eel-site-logo' ); |
| 268 |
|
| 269 |
// Add eel-site-logo-added class if logo image is present |
| 270 |
if ( $logo_data['type'] === 'image' ) { |
| 271 |
$this->add_render_attribute( 'wrapper', 'class', 'eel-site-logo-added' ); |
| 272 |
} |
| 273 |
|
| 274 |
// Link setup |
| 275 |
$link = ''; |
| 276 |
if ( 'file' === $settings['link_to'] && isset( $logo_data['url'] ) ) { |
| 277 |
$link = $logo_data['url']; |
| 278 |
} elseif ( 'default' === $settings['link_to'] ) { |
| 279 |
$link = home_url(); |
| 280 |
} elseif ( 'custom' === $settings['link_to'] && ! empty( $settings['link']['url'] ) ) { |
| 281 |
$link = $settings['link']['url']; |
| 282 |
} |
| 283 |
|
| 284 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Safe, output contains trusted HTML attributes. |
| 285 |
echo '<div ' . $this->get_render_attribute_string( 'wrapper' ) . '>'; |
| 286 |
|
| 287 |
if ( $caption ) { |
| 288 |
echo '<figure class="wp-caption">'; |
| 289 |
} |
| 290 |
if ( $link ) { |
| 291 |
$target = ! empty( $settings['link']['is_external'] ) ? ' target="_blank"' : ''; |
| 292 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Safe, output contains trusted HTML attributes. |
| 293 |
echo '<a href="' . esc_url( $link ) . '"' . $target . '>'; |
| 294 |
} |
| 295 |
if ( $logo_data['type'] === 'image' ) { |
| 296 |
$attachment_id = 0; |
| 297 |
if ( ! empty( $settings['custom_image']['id'] ) ) { |
| 298 |
$attachment_id = $settings['custom_image']['id']; |
| 299 |
} elseif ( ! empty( $settings['custom_sticky_image']['id'] ) ) { |
| 300 |
$attachment_id = $settings['custom_sticky_image']['id']; |
| 301 |
} else { |
| 302 |
$attachment_id = attachment_url_to_postid( $logo_data['url'] ); |
| 303 |
} |
| 304 |
if ( $attachment_id ) { |
| 305 |
$sticky_logo_id = get_theme_mod( 'sticky_logo' ); |
| 306 |
if ( ! empty( $settings['custom_sticky_image']['id'] ) ) { |
| 307 |
$sticky_logo_id = $settings['custom_sticky_image']['id']; |
| 308 |
} |
| 309 |
$sticky_logo_url = is_numeric( $sticky_logo_id ) ? wp_get_attachment_image_url( $sticky_logo_id, 'full' ) : $sticky_logo_id; |
| 310 |
|
| 311 |
// Build class string |
| 312 |
$main_logo_classes = 'eel-main-logo'; |
| 313 |
if ( empty( $sticky_logo_url ) ) { |
| 314 |
$main_logo_classes .= ' eel-no-sticky'; |
| 315 |
} |
| 316 |
|
| 317 |
// Output main logo |
| 318 |
echo wp_get_attachment_image( $attachment_id, 'full', false, [ |
| 319 |
'class' => $main_logo_classes, |
| 320 |
'alt' => $alt_text, |
| 321 |
'loading' => 'lazy', |
| 322 |
'decoding' => 'async', |
| 323 |
] ); |
| 324 |
|
| 325 |
// Output sticky logo if available |
| 326 |
if ( $sticky_logo_url ) { |
| 327 |
echo '<img class="eel-sticky-logo" src="' . esc_url( $sticky_logo_url ) . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" decoding="async">'; |
| 328 |
} |
| 329 |
} else { |
| 330 |
$img_html = '<img src="' . esc_url( $logo_data['url'] ) . '" alt="' . esc_attr( $alt_text ) . '" decoding="async">'; |
| 331 |
echo wp_kses_post( apply_filters( 'easy_elements_site_logo_fallback_img_html', $img_html, $logo_data['url'], $alt_text, $settings ) ); |
| 332 |
} |
| 333 |
} else { |
| 334 |
// Show site title and tagline as fallback |
| 335 |
echo '<div class="eel-site-title-tagline">'; |
| 336 |
echo '<h1 class="eel-site-title">' . esc_html( $logo_data['title'] ) . '</h1>'; |
| 337 |
if ( ! empty( $logo_data['tagline'] ) ) { |
| 338 |
echo '<p class="eel-site-tagline">' . esc_html( $logo_data['tagline'] ) . '</p>'; |
| 339 |
} |
| 340 |
echo '</div>'; |
| 341 |
} |
| 342 |
if ( $link ) { |
| 343 |
echo '</a>'; |
| 344 |
} |
| 345 |
if ( $caption ) { |
| 346 |
echo '<figcaption class="wp-caption-text">' . esc_html( $caption ) . '</figcaption>'; |
| 347 |
echo '</figure>'; |
| 348 |
} |
| 349 |
echo '</div>'; |
| 350 |
} |
| 351 |
} |
| 352 |
|