PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
ablocks / includes / blocks / featured-image / block.php

block.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.14.0, at includes/blocks/featured-image/block.php

239 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Blocks\FeaturedImage;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use ABlocks\Classes\BlockBaseAbstract;
9 use ABlocks\Classes\CssGeneratorV2;
10 use ABlocks\Helper;
11 use ABlocks\Controls\Alignment;
12 use ABlocks\Controls\Border;
13 use ABlocks\Controls\Dimensions;
14 use ABlocks\Controls\Typography;
15 use ABlocks\Controls\CssFilter;
16 use ABlocks\Controls\BoxShadow;
17 use ABlocks\Controls\Range;
18 use ABlocks\Controls\Color;
19
20 class Block extends BlockBaseAbstract {
21 protected $block_name = 'featured-image';
22
23 public function build_css( $attributes ) {
24 $css_generator = new CssGeneratorV2( $attributes, $this->block_name );
25 // Image Wrapper CSS
26 $css_generator->add_class_styles(
27 '{{WRAPPER}}',
28 $this->get_wrapper_css( $attributes ),
29 $this->get_wrapper_css( $attributes, 'Tablet' ),
30 $this->get_wrapper_css( $attributes, 'Mobile' ),
31 $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_wrapper_css( $attributes, $device ); } )
32 );
33
34 // Image container css
35 $css_generator->add_class_styles(
36 '{{WRAPPER}}',
37 $this->get_image_container_css( $attributes ),
38 $this->get_image_container_css( $attributes, 'Tablet' ),
39 $this->get_image_container_css( $attributes, 'Mobile' ),
40 $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_image_container_css( $attributes, $device ); } )
41 );
42 // Image css
43 $css_generator->add_class_styles(
44 '{{WRAPPER}}.ablocks-block--featured-image img',
45 $this->get_image_css( $attributes ),
46 $this->get_image_css( $attributes, 'Tablet' ),
47 $this->get_image_css( $attributes, 'Mobile' ),
48 $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_image_css( $attributes, $device ); } )
49 );
50
51 // Image hover css
52 $css_generator->add_class_styles(
53 '{{WRAPPER}}.ablocks-block--featured-image img:hover',
54 $this->get_image_hover_css( $attributes ),
55 $this->get_image_hover_css( $attributes, 'Tablet' ),
56 $this->get_image_hover_css( $attributes, 'Mobile' ),
57 $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_image_hover_css( $attributes, $device ); } )
58 );
59
60 return $css_generator->generate_css();
61 }
62
63 public function get_wrapper_css( $attributes, $device = '' ) {
64 return array_merge(
65 isset( $attributes['padding'] ) ? Dimensions::get_css( $attributes['padding'], 'padding', $device ) : [],
66 );
67 }
68
69 public function get_image_css( $attributes, $device = '' ) {
70 $css = [];
71
72 if ( isset( $attributes['widthHeightWidget'][ 'width' . $device ] ) ) {
73 $css['width'] = $attributes['widthHeightWidget'][ 'width' . $device ] . 'px';
74 } elseif ( isset( $attributes['widthHeightWidget']['imgNaturalWidth'] ) ) {
75 $css['width'] = $attributes['widthHeightWidget']['imgNaturalWidth'] . 'px';
76 }
77
78 $aspectRatioValue = empty( $attributes['aspectRatio'][ 'value' . $device ] ) ? false : $attributes['aspectRatio'][ 'value' . $device ];
79 $widthValue = empty( $attributes['widthHeightWidget'][ 'width' . $device ] ) ? false : $attributes['widthHeightWidget'][ 'width' . $device ];
80 $heightValue = empty( $attributes['widthHeightWidget'][ 'height' . $device ] ) ? false : $attributes['widthHeightWidget'][ 'height' . $device ];
81 $showHeight = ! empty( $attributes['widthHeightWidget'][ 'showHeight' . $device ] );
82
83 if ( $widthValue ) {
84 $css['width'] = $widthValue . 'px';
85 }
86 if ( $heightValue ) {
87 $css['height'] = $heightValue . 'px';
88 } else {
89 $css['height'] = 'auto';
90 }
91
92 if ( isset( $attributes['objectFit'][ 'value' . $device ] ) && '' !== $attributes['objectFit'][ 'value' . $device ] && 'default' !== $attributes['objectFit'][ 'value' . $device ] ) {
93 $css['object-fit'] = $attributes['objectFit'][ 'value' . $device ];
94 }
95
96 if ( $aspectRatioValue && $aspectRatioValue !== 'original' ) {
97 $css['aspect-ratio'] = $aspectRatioValue;
98 }
99 if ( isset( $attributes['onHoverImg'] ) && 'slide' === $attributes['onHoverImg'] ) {
100 $css['transform'] = 'translate3d(-40px, 0, 0)';
101 $css['transition'] = 'transform 0.3s';
102 }
103
104 return array_merge(
105 $css,
106 Range::get_css([
107 'attributeValue' => $attributes['opacity'],
108 'defaultValue' => 1,
109 'unitDefaultValue' => '',
110 'property' => 'opacity',
111 'device' => $device,
112 ]),
113 isset( $attributes['border'] ) ? Border::get_css( $attributes['border'], '', $device ) : [],
114 isset( $attributes['cssFilter'] ) ? CssFilter::get_css( $attributes['cssFilter'], '', $device ) : [],
115 isset( $attributes['boxShadow'] ) ? BoxShadow::get_css( $attributes['boxShadow'], $device ) : []
116 );
117 }
118
119 public function get_image_hover_css( $attributes, $device = '' ) {
120 $css = [];
121 $range_css = Range::get_css([
122 'attributeValue' => $attributes['opacityH'],
123 'defaultValue' => 1,
124 'unitDefaultValue' => '',
125 'property' => 'opacity',
126 'device' => $device,
127 ]);
128 $transitionDurationRange = Range::get_css([
129 'attributeValue' => $attributes['transitionDuration'],
130 'defaultValue' => 0.5,
131 'unitDefaultValue' => '',
132 'property' => 'transition',
133 'device' => $device,
134 ]);
135 $filterTransitionDurationRange = Range::get_css([
136 'attributeValue' => $attributes['filterTransitionDuration'],
137 'defaultValue' => 0.5,
138 'unitDefaultValue' => '',
139 'property' => 'filter',
140 'device' => $device,
141 ]);
142
143 if ( isset( $attributes['onHoverImg'] ) ) {
144 $on_Hover_Img = $attributes['onHoverImg'];
145 if ( 'zoomin' === $on_Hover_Img ) {
146 $css['transform'] = 'scale(1.1)';
147 $css['transition'] = 'transform 0.3s';
148 } elseif ( 'grayscale' === $on_Hover_Img ) {
149 $css['filter'] = 'grayscale(100%)';
150 $css['transition'] = 'transform 0.3s';
151 } elseif ( 'blur' === $on_Hover_Img ) {
152 $css['filter'] = 'blur(3px)';
153 $css['transition'] = 'transform 0.3s';
154 } elseif ( 'slide' === $on_Hover_Img ) {
155 $css['transform'] = 'translate3d(0, 0, 0)';
156 $css['transition'] = 'transform 0.3s';
157 }
158 }
159 if (
160 isset( $attributes['border']['transitionDuration'] ) ||
161 isset( $attributes['boxShadow']['transitionDuration'] ) ||
162 isset( $filterTransitionDurationRange['filter'] ) ||
163 isset( $transitionDurationRange['transition'] )
164 ) {
165 $css['transition'] = sprintf(
166 'border %ss, box-shadow %ss, opacity %ss, filter %ss, transform 0.3s',
167 ! empty( $attributes['border']['transitionDuration'] ) ? $attributes['border']['transitionDuration'] : $transitionDurationRange['transition'],
168 isset( $attributes['boxShadow']['transitionDuration'] ) ? $attributes['boxShadow']['transitionDuration'] : $transitionDurationRange['transition'],
169 $filterTransitionDurationRange['filter'],
170 $filterTransitionDurationRange['filter']
171 );
172 }
173
174 $border_hover_css = Border::get_hover_css( isset( $attributes['border'] ) ? $attributes['border'] : null, $device );
175
176 return array_merge(
177 $css,
178 $range_css,
179 $border_hover_css,
180 ( isset( $attributes['boxShadow'] ) ) ? BoxShadow::get_hover_css( $attributes['boxShadow'], $device ) : [],
181 ( isset( $attributes['cssHoverFilter'] ) ) ? CssFilter::get_css( $attributes['cssHoverFilter'], '', $device ) : [],
182 );
183 }
184
185 public function get_image_container_css( $attributes, $device = '' ) {
186 $alignment_value = $attributes['alignment'][ 'value' . $device ] ?? '';
187
188 $css = [];
189 if ( ! empty( $alignment_value ) ) {
190 $css['display'] = 'flex';
191 $css['justify-content'] = $alignment_value;
192 }
193 return $css;
194 }
195
196 public function render_block_content( $attributes, $content, $block_instance ) {
197 $post_id = get_the_ID();
198
199 if ( isset( $block_instance->context['postId'] ) && ! empty( $block_instance->context['postId'] ) ) {
200 $post_id = $block_instance->context['postId'];
201 }
202
203 $featured_img_url = get_the_post_thumbnail_url( $post_id, 'full' );
204 if ( empty( $featured_img_url ) ) {
205 $featured_img_url = ABLOCKS_ASSETS_URL . 'images/placeholder-thumbnail.svg';
206 }
207
208 $featured_img_title = isset( $attributes['imgTitle'] ) ? (string) $attributes['imgTitle'] : '';
209 $featured_img_alt = isset( $attributes['imgAltText'] ) ? (string) $attributes['imgAltText'] : '';
210 $featured_img_link = isset( $attributes['imgLink']['linkDestination'] ) ? (string) $attributes['imgLink']['linkDestination'] : '';
211 $featured_img_link_href = isset( $attributes['imgLink']['href'] ) ? (string) $attributes['imgLink']['href'] : '';
212 $featured_img_link_target = ! empty( $featured_img_link_href ) ? '_blank' : '_self'; // ← semicolon added
213
214 // Linked image (custom URL)
215 if ( 'custom' === $featured_img_link && ! empty( $featured_img_link_href ) ) {
216 $rel = ( '_blank' === $featured_img_link_target ) ? ' rel="noopener noreferrer"' : '';
217 return sprintf(
218 '<a href="%s" target="%s"%s><img class="ablocks-featured-image" src="%s" alt="%s" title="%s" loading="lazy" decoding="async" /></a>',
219 esc_url( $featured_img_link_href ),
220 esc_attr( $featured_img_link_target ),
221 $rel,
222 esc_url( $featured_img_url ),
223 esc_attr( $featured_img_alt ),
224 esc_attr( $featured_img_title )
225 );
226 }
227
228 // Plain image
229 return sprintf(
230 '<img class="ablocks-featured-image" src="%s" alt="%s" title="%s" loading="lazy" decoding="async" />',
231 esc_url( $featured_img_url ),
232 esc_attr( $featured_img_alt ),
233 esc_attr( $featured_img_title )
234 );
235 }
236
237
238 }
239