PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
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 1.1.2 All 80 releases
ablocks / includes / blocks / image / block.php

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

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