| 1 |
<?php |
| 2 |
namespace HelloPlus\Modules\Content\Classes\Render; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
use Elementor\Group_Control_Image_Size; |
| 9 |
use Elementor\Icons_Manager; |
| 10 |
use Elementor\Utils; |
| 11 |
use HelloPlus\Modules\Content\Widgets\Flex_Hero; |
| 12 |
use HelloPlus\Classes\Ehp_Button; |
| 13 |
|
| 14 |
class Widget_Flex_Hero_Render { |
| 15 |
protected Flex_Hero $widget; |
| 16 |
const LAYOUT_CLASSNAME = 'ehp-flex-hero'; |
| 17 |
const CTAS_CONTAINER_CLASSNAME = 'ehp-flex-hero__ctas-container'; |
| 18 |
const BUTTON_CLASSNAME = 'ehp-flex-hero__button'; |
| 19 |
const IMAGE_CLASSNAME = 'ehp-flex-hero__image'; |
| 20 |
|
| 21 |
protected array $settings; |
| 22 |
|
| 23 |
public function __construct( Flex_Hero $widget ) { |
| 24 |
$this->widget = $widget; |
| 25 |
$this->settings = $widget->get_settings_for_display(); |
| 26 |
} |
| 27 |
|
| 28 |
public function maybe_add_layout_responsive_classes( &$layout_classes ) { |
| 29 |
$layout_image_position_mobile = $this->settings['layout_image_position_mobile']; |
| 30 |
$layout_image_position_tablet = $this->settings['layout_image_position_tablet']; |
| 31 |
|
| 32 |
if ( ! empty( $layout_image_position_mobile ) ) { |
| 33 |
$layout_classes[] = 'has-image-position-sm-' . $layout_image_position_mobile; |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! empty( $layout_image_position_tablet ) ) { |
| 37 |
$layout_classes[] = 'has-image-position-md-' . $layout_image_position_tablet; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public function render(): void { |
| 42 |
$layout_classnames = [ self::LAYOUT_CLASSNAME ]; |
| 43 |
$layout_full_height_controls = $this->settings['box_full_screen_height_controls'] ?? ''; |
| 44 |
$layout_preset = $this->settings['layout_preset']; |
| 45 |
$image_stretch = $this->settings['image_stretch']; |
| 46 |
$layout_image_position = $this->settings['layout_image_position']; |
| 47 |
$has_border = $this->settings['show_box_border']; |
| 48 |
$box_shape = $this->settings['box_shape']; |
| 49 |
$box_shape_mobile = $this->settings['box_shape_mobile']; |
| 50 |
$box_shape_tablet = $this->settings['box_shape_tablet']; |
| 51 |
|
| 52 |
if ( ! empty( $layout_full_height_controls ) ) { |
| 53 |
foreach ( $layout_full_height_controls as $breakpoint ) { |
| 54 |
$layout_classnames[] = 'is-full-height-' . $breakpoint; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! empty( $layout_preset ) ) { |
| 59 |
$layout_classnames[] = 'has-layout-preset-' . $layout_preset; |
| 60 |
} |
| 61 |
|
| 62 |
if ( 'yes' === $image_stretch ) { |
| 63 |
$layout_classnames[] = 'has-image-stretch'; |
| 64 |
} |
| 65 |
|
| 66 |
if ( 'yes' === $has_border ) { |
| 67 |
$layout_classnames[] = 'has-border'; |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! empty( $layout_image_position ) ) { |
| 71 |
$layout_classnames[] = 'has-image-position-' . $layout_image_position; |
| 72 |
|
| 73 |
// pass by reference: |
| 74 |
$this->maybe_add_layout_responsive_classes( $layout_classnames ); |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! empty( $box_shape ) ) { |
| 78 |
$layout_classnames[] = 'has-shape-' . $box_shape; |
| 79 |
|
| 80 |
if ( ! empty( $box_shape_mobile ) ) { |
| 81 |
$layout_classnames[] = 'has-shape-sm-' . $box_shape_mobile; |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! empty( $box_shape_tablet ) ) { |
| 85 |
$layout_classnames[] = 'has-shape-md-' . $box_shape_tablet; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$this->widget->add_render_attribute( 'layout', [ |
| 90 |
'class' => $layout_classnames, |
| 91 |
] ); |
| 92 |
?> |
| 93 |
<div <?php $this->widget->print_render_attribute_string( 'layout' ); ?>> |
| 94 |
<?php |
| 95 |
$this->render_content_container(); |
| 96 |
$this->render_image(); |
| 97 |
?> |
| 98 |
</div> |
| 99 |
<?php |
| 100 |
} |
| 101 |
|
| 102 |
public function render_content_container() { |
| 103 |
?> |
| 104 |
<div class="ehp-flex-hero__overlay"></div> |
| 105 |
<div class="ehp-flex-hero__content-container"> |
| 106 |
<?php |
| 107 |
$this->render_text_container(); |
| 108 |
$this->render_ctas_container(); |
| 109 |
?> |
| 110 |
</div> |
| 111 |
<?php |
| 112 |
} |
| 113 |
|
| 114 |
public function render_text_container() { |
| 115 |
$this->maybe_render_text_html( 'intro_text', 'ehp-flex-hero__intro', $this->settings['intro_text'], $this->settings['intro_tag'] ); |
| 116 |
$this->maybe_render_text_html( 'heading_text', 'ehp-flex-hero__heading', $this->settings['heading_text'], $this->settings['heading_tag'] ); |
| 117 |
$this->maybe_render_text_html( 'subheading_text', 'ehp-flex-hero__subheading', $this->settings['subheading_text'], $this->settings['subheading_tag'] ); |
| 118 |
} |
| 119 |
|
| 120 |
public function maybe_render_text_html( $render_key, $css_class, $settings_text, $settings_tag ) { |
| 121 |
if ( '' !== $settings_text ) { |
| 122 |
$this->widget->add_render_attribute( $render_key, 'class', $css_class ); |
| 123 |
|
| 124 |
$element = wp_kses_post( $settings_text ); |
| 125 |
|
| 126 |
$element_html = sprintf( '<%1$s %2$s>%3$s</%1$s>', Utils::validate_html_tag( $settings_tag ), $this->widget->get_render_attribute_string( $render_key ), $element ); |
| 127 |
|
| 128 |
// PHPCS - the variable $element_html holds safe data. |
| 129 |
echo $element_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
protected function render_ctas_container() { |
| 134 |
$primary_cta_button_text = $this->settings['primary_cta_button_text']; |
| 135 |
$secondary_cta_button_text = $this->settings['secondary_cta_button_text']; |
| 136 |
$has_primary_button = ! empty( $primary_cta_button_text ); |
| 137 |
$has_secondary_button = ! empty( $secondary_cta_button_text ); |
| 138 |
|
| 139 |
$ctas_container_classnames = [ self::CTAS_CONTAINER_CLASSNAME ]; |
| 140 |
|
| 141 |
$this->widget->add_render_attribute( 'ctas-container', [ |
| 142 |
'class' => $ctas_container_classnames, |
| 143 |
] ); |
| 144 |
?> |
| 145 |
<div <?php $this->widget->print_render_attribute_string( 'ctas-container' ); ?>> |
| 146 |
<?php if ( $has_primary_button ) { |
| 147 |
$this->render_button( 'primary' ); |
| 148 |
} ?> |
| 149 |
<?php if ( $has_secondary_button ) { |
| 150 |
$this->render_button( 'secondary' ); |
| 151 |
} ?> |
| 152 |
</div> |
| 153 |
<?php |
| 154 |
} |
| 155 |
|
| 156 |
protected function render_button( $type ) { |
| 157 |
$button = new Ehp_Button( $this->widget, [ |
| 158 |
'type' => $type, |
| 159 |
'widget_name' => 'flex-hero', |
| 160 |
] ); |
| 161 |
$button->render(); |
| 162 |
} |
| 163 |
|
| 164 |
public function get_attachment_image_html_filter( $html ) { |
| 165 |
$image_classnames = 'ehp-flex-hero__img'; |
| 166 |
$image_shape = $this->settings['image_shape']; |
| 167 |
$image_shape_mobile = $this->settings['image_shape_mobile']; |
| 168 |
$image_shape_tablet = $this->settings['image_shape_tablet']; |
| 169 |
|
| 170 |
$has_border = $this->settings['show_image_border']; |
| 171 |
|
| 172 |
if ( 'yes' === $has_border ) { |
| 173 |
$image_classnames .= ' has-border'; |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! empty( $image_shape ) ) { |
| 177 |
$image_classnames .= ' has-shape-' . $image_shape; |
| 178 |
|
| 179 |
if ( ! empty( $image_shape_mobile ) ) { |
| 180 |
$image_classnames .= ' has-shape-sm-' . $image_shape_mobile; |
| 181 |
} |
| 182 |
|
| 183 |
if ( ! empty( $image_shape_tablet ) ) { |
| 184 |
$image_classnames .= ' has-shape-md-' . $image_shape_tablet; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
$html = str_replace( '<img ', '<img class="' . esc_attr( $image_classnames ) . '" ', $html ); |
| 189 |
return $html; |
| 190 |
} |
| 191 |
|
| 192 |
public function render_image() { |
| 193 |
$image = $this->settings['image']; |
| 194 |
$has_image = ! empty( $image['url'] ); |
| 195 |
$image_wrapper_classnames = self::IMAGE_CLASSNAME; |
| 196 |
|
| 197 |
$this->widget->add_render_attribute( 'image', [ |
| 198 |
'class' => $image_wrapper_classnames, |
| 199 |
] ); |
| 200 |
|
| 201 |
if ( $has_image ) : |
| 202 |
?> |
| 203 |
<div <?php $this->widget->print_render_attribute_string( 'image' ); ?>> |
| 204 |
<?php |
| 205 |
add_filter( 'elementor/image_size/get_attachment_image_html', [ $this, 'get_attachment_image_html_filter' ], 10, 4 ); |
| 206 |
Group_Control_Image_Size::print_attachment_image_html( $this->settings, 'image' ); |
| 207 |
remove_filter( 'elementor/image_size/get_attachment_image_html', [ $this, 'get_attachment_image_html_filter' ], 10 ); |
| 208 |
?> |
| 209 |
</div> |
| 210 |
<?php |
| 211 |
endif; //has_image |
| 212 |
} |
| 213 |
} |
| 214 |
|