PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.2
GenerateBlocks v1.8.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / blocks / class-image.php
generateblocks / includes / blocks Last commit date
class-button-container.php 2 years ago class-button.php 2 years ago class-container.php 2 years ago class-grid.php 3 years ago class-headline.php 2 years ago class-image.php 2 years ago class-query-loop.php 3 years ago
class-image.php
337 lines
1 <?php
2 /**
3 * Handles the Image block.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Add Image related functions.
14 */
15 class GenerateBlocks_Block_Image {
16 /**
17 * Keep track of all blocks of this type on the page.
18 *
19 * @var array $block_ids The current block id.
20 */
21 private static $block_ids = [];
22
23 /**
24 * Keep track of CSS we want to output once per block type.
25 *
26 * @var boolean
27 */
28 private static $singular_css_added = false;
29
30 /**
31 * Block defaults.
32 */
33 public static function defaults() {
34 return [
35 'mediaId' => '',
36 'sizeSlug' => '',
37 'width' => '',
38 'widthTablet' => '',
39 'widthMobile' => '',
40 'height' => '',
41 'heightTablet' => '',
42 'heightMobile' => '',
43 'borderColor' => '',
44 'objectFit' => '',
45 'objectFitTablet' => '',
46 'objectFitMobile' => '',
47 'align' => '',
48 'alignment' => '',
49 'alignmentTablet' => '',
50 'alignmentMobile' => '',
51 ];
52 }
53
54 /**
55 * Store our block ID in memory.
56 *
57 * @param string $id The block ID to store.
58 */
59 public static function store_block_id( $id ) {
60 self::$block_ids[] = $id;
61 }
62
63 /**
64 * Check if our block ID exists.
65 *
66 * @param string $id The block ID to store.
67 */
68 public static function block_id_exists( $id ) {
69 return in_array( $id, (array) self::$block_ids );
70 }
71
72 /**
73 * Compile our CSS data based on our block attributes.
74 *
75 * @param array $attributes Our block attributes.
76 */
77 public static function get_css_data( $attributes ) {
78 $css = new GenerateBlocks_Dynamic_CSS();
79 $desktop_css = new GenerateBlocks_Dynamic_CSS();
80 $tablet_css = new GenerateBlocks_Dynamic_CSS();
81 $tablet_only_css = new GenerateBlocks_Dynamic_CSS();
82 $mobile_css = new GenerateBlocks_Dynamic_CSS();
83 $css_data = [];
84
85 $defaults = generateblocks_get_block_defaults();
86
87 $settings = wp_parse_args(
88 $attributes,
89 $defaults['image']
90 );
91
92 $id = $attributes['uniqueId'];
93
94 // Only add this CSS once.
95 if ( ! self::$singular_css_added ) {
96 $css->set_selector( '.gb-block-image img' );
97 $css->add_property( 'vertical-align', 'middle' );
98
99 do_action(
100 'generateblocks_block_one_time_css_data',
101 'image',
102 $settings,
103 $css
104 );
105
106 self::$singular_css_added = true;
107 }
108
109 // Map deprecated settings.
110 $settings = GenerateBlocks_Map_Deprecated_Attributes::map_attributes( $settings );
111
112 $css->set_selector( '.gb-block-image-' . $id );
113 generateblocks_add_spacing_css( $css, $settings );
114
115 // Set a flag we'll update later if we disable floats.
116 $disable_float = false;
117 $has_desktop_float = 'floatLeft' === $settings['alignment'] || 'floatRight' === $settings['alignment'];
118 $has_tablet_float = 'floatLeft' === $settings['alignmentTablet'] || 'floatRight' === $settings['alignmentTablet'];
119
120 if ( $has_desktop_float ) {
121 $css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignment'] ) );
122 } else {
123 $css->add_property( 'text-align', $settings['alignment'] );
124 }
125
126 $css->set_selector( '.gb-image-' . $id );
127 generateblocks_add_border_css( $css, $settings );
128 $css->add_property( 'width', $settings['width'] );
129 $css->add_property( 'height', $settings['height'] );
130 $css->add_property( 'object-fit', $settings['objectFit'] );
131
132 $tablet_css->set_selector( '.gb-block-image-' . $id );
133 generateblocks_add_spacing_css( $tablet_css, $settings, 'Tablet' );
134
135 if ( $has_tablet_float ) {
136 $tablet_css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignmentTablet'] ) );
137 } else {
138 $tablet_css->add_property( 'text-align', $settings['alignmentTablet'] );
139
140 if ( $settings['alignmentTablet'] && $has_desktop_float ) {
141 $tablet_css->add_property( 'float', 'none' );
142 $disable_float = true;
143 }
144 }
145
146 $tablet_css->set_selector( '.gb-image-' . $id );
147 generateblocks_add_border_css( $tablet_css, $settings, 'Tablet' );
148 $tablet_css->add_property( 'width', $settings['widthTablet'] );
149 $tablet_css->add_property( 'height', $settings['heightTablet'] );
150 $tablet_css->add_property( 'object-fit', $settings['objectFitTablet'] );
151
152 $mobile_css->set_selector( '.gb-block-image-' . $id );
153 generateblocks_add_spacing_css( $mobile_css, $settings, 'Mobile' );
154
155 if ( 'floatLeft' === $settings['alignmentMobile'] || 'floatRight' === $settings['alignmentMobile'] ) {
156 $mobile_css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignmentMobile'] ) );
157 } else {
158 $mobile_css->add_property( 'text-align', $settings['alignmentMobile'] );
159
160 if (
161 $settings['alignmentMobile'] &&
162 ! $disable_float &&
163 (
164 $has_desktop_float ||
165 $has_tablet_float
166 )
167 ) {
168 $mobile_css->add_property( 'float', 'none' );
169 }
170 }
171
172 $mobile_css->set_selector( '.gb-image-' . $id );
173 generateblocks_add_border_css( $mobile_css, $settings, 'Mobile' );
174 $mobile_css->add_property( 'width', $settings['widthMobile'] );
175 $mobile_css->add_property( 'height', $settings['heightMobile'] );
176 $mobile_css->add_property( 'object-fit', $settings['objectFitMobile'] );
177
178 // Store this block ID in memory.
179 self::store_block_id( $id );
180
181 /**
182 * Do generateblocks_block_css_data hook
183 *
184 * @since 1.0
185 *
186 * @param string $name The name of our block.
187 * @param array $settings The settings for the current block.
188 * @param object $css Our desktop/main CSS data.
189 * @param object $desktop_css Our desktop only CSS data.
190 * @param object $tablet_css Our tablet CSS data.
191 * @param object $tablet_only_css Our tablet only CSS data.
192 * @param object $mobile_css Our mobile CSS data.
193 */
194 do_action(
195 'generateblocks_block_css_data',
196 'image',
197 $settings,
198 $css,
199 $desktop_css,
200 $tablet_css,
201 $tablet_only_css,
202 $mobile_css
203 );
204
205 return [
206 'main' => $css->css_output(),
207 'desktop' => $desktop_css->css_output(),
208 'tablet' => $tablet_css->css_output(),
209 'tablet_only' => $tablet_only_css->css_output(),
210 'mobile' => $mobile_css->css_output(),
211 ];
212 }
213
214 /**
215 * Wrapper function for our dynamic buttons.
216 *
217 * @since 1.6.0
218 * @param array $attributes The block attributes.
219 * @param string $content The dynamic text to display.
220 * @param WP_Block $block Block instance.
221 */
222 public static function render_block( $attributes, $content, $block ) {
223 if ( empty( $attributes['useDynamicData'] ) ) {
224 // Add styles to this block if needed.
225 $content = generateblocks_maybe_add_block_css(
226 $content,
227 [
228 'class_name' => 'GenerateBlocks_Block_Image',
229 'attributes' => $attributes,
230 'block_ids' => self::$block_ids,
231 ]
232 );
233
234 return generateblocks_filter_images( $content, $attributes );
235 }
236
237 $image = empty( $attributes['dynamicContentType'] )
238 ? generateblocks_filter_images( GenerateBlocks_Dynamic_Content::get_static_content( $content ), $attributes )
239 : GenerateBlocks_Dynamic_Content::get_dynamic_image( $attributes, $block );
240
241 if ( ! $image ) {
242 return '';
243 }
244
245 $defaults = generateblocks_get_block_defaults();
246
247 $settings = wp_parse_args(
248 $attributes,
249 $defaults['image']
250 );
251
252 // Add styles to this block if needed.
253 $output = generateblocks_maybe_add_block_css(
254 '',
255 [
256 'class_name' => 'GenerateBlocks_Block_Image',
257 'attributes' => $attributes,
258 'block_ids' => self::$block_ids,
259 ]
260 );
261
262 $output .= sprintf(
263 '<figure %s>',
264 generateblocks_attr(
265 'image-figure',
266 array(
267 'class' => implode(
268 ' ',
269 array(
270 'gb-block-image',
271 'gb-block-image-' . $settings['uniqueId'],
272 )
273 ),
274 ),
275 $settings,
276 $block
277 )
278 );
279
280 $dynamic_link = GenerateBlocks_Dynamic_Content::get_dynamic_url( $attributes, $block );
281
282 if ( $dynamic_link ) {
283 $relAttributes = array();
284
285 if ( ! empty( $settings['relNoFollow'] ) ) {
286 $relAttributes[] = 'nofollow';
287 }
288
289 if ( ! empty( $settings['openInNewWindow'] ) ) {
290 $relAttributes[] = 'noopener';
291 $relAttributes[] = 'noreferrer';
292 }
293
294 if ( ! empty( $settings['relSponsored'] ) ) {
295 $relAttributes[] = 'sponsored';
296 }
297
298 $image = sprintf(
299 '<a %s>%s</a>',
300 generateblocks_attr(
301 'image-link',
302 array(
303 'class' => '',
304 'href' => $dynamic_link,
305 'rel' => ! empty( $relAttributes ) ? implode( ' ', $relAttributes ) : null,
306 'target' => ! empty( $settings['openInNewWindow'] ) ? '_blank' : null,
307 ),
308 $settings,
309 $block
310 ),
311 $image
312 );
313 }
314
315 $output .= $image;
316
317 if ( isset( $block->parsed_block['innerBlocks'][0]['attrs'] ) ) {
318 $image_id = GenerateBlocks_Dynamic_Content::get_dynamic_image_id( $attributes );
319 $block->parsed_block['innerBlocks'][0]['attrs']['dynamicImage'] = $image_id;
320
321 $caption = (
322 new WP_Block(
323 $block->parsed_block['innerBlocks'][0]
324 )
325 )->render( array( 'dynamic' => true ) );
326
327 if ( $caption ) {
328 $output .= $caption;
329 }
330 }
331
332 $output .= '</figure>';
333
334 return $output;
335 }
336 }
337