PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / trunk
GenerateBlocks vtrunk
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-block.php 1 year ago class-button-container.php 2 years ago class-button.php 2 years ago class-container.php 2 years ago class-element.php 1 year ago class-grid.php 2 years ago class-headline.php 1 week ago class-image.php 2 years ago class-loop-item.php 1 year ago class-looper.php 1 year ago class-media.php 1 year ago class-query-loop.php 3 years ago class-query-no-results.php 1 year ago class-query-page-numbers.php 1 year ago class-query.php 1 year ago class-shape.php 1 year ago class-text.php 1 year ago
class-image.php
335 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 do_action(
97 'generateblocks_block_one_time_css_data',
98 'image',
99 $settings,
100 $css
101 );
102
103 self::$singular_css_added = true;
104 }
105
106 // Map deprecated settings.
107 $settings = GenerateBlocks_Map_Deprecated_Attributes::map_attributes( $settings );
108
109 $css->set_selector( '.gb-block-image-' . $id );
110 generateblocks_add_spacing_css( $css, $settings );
111
112 // Set a flag we'll update later if we disable floats.
113 $disable_float = false;
114 $has_desktop_float = 'floatLeft' === $settings['alignment'] || 'floatRight' === $settings['alignment'];
115 $has_tablet_float = 'floatLeft' === $settings['alignmentTablet'] || 'floatRight' === $settings['alignmentTablet'];
116
117 if ( $has_desktop_float ) {
118 $css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignment'] ) );
119 } else {
120 $css->add_property( 'text-align', $settings['alignment'] );
121 }
122
123 $css->set_selector( '.gb-image-' . $id );
124 generateblocks_add_border_css( $css, $settings );
125 $css->add_property( 'width', $settings['width'] );
126 $css->add_property( 'height', $settings['height'] );
127 $css->add_property( 'object-fit', $settings['objectFit'] );
128 $css->add_property( 'vertical-align', 'middle' );
129
130 $tablet_css->set_selector( '.gb-block-image-' . $id );
131 generateblocks_add_spacing_css( $tablet_css, $settings, 'Tablet' );
132
133 if ( $has_tablet_float ) {
134 $tablet_css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignmentTablet'] ) );
135 } else {
136 $tablet_css->add_property( 'text-align', $settings['alignmentTablet'] );
137
138 if ( $settings['alignmentTablet'] && $has_desktop_float ) {
139 $tablet_css->add_property( 'float', 'none' );
140 $disable_float = true;
141 }
142 }
143
144 $tablet_css->set_selector( '.gb-image-' . $id );
145 generateblocks_add_border_css( $tablet_css, $settings, 'Tablet' );
146 $tablet_css->add_property( 'width', $settings['widthTablet'] );
147 $tablet_css->add_property( 'height', $settings['heightTablet'] );
148 $tablet_css->add_property( 'object-fit', $settings['objectFitTablet'] );
149
150 $mobile_css->set_selector( '.gb-block-image-' . $id );
151 generateblocks_add_spacing_css( $mobile_css, $settings, 'Mobile' );
152
153 if ( 'floatLeft' === $settings['alignmentMobile'] || 'floatRight' === $settings['alignmentMobile'] ) {
154 $mobile_css->add_property( 'float', generateblocks_get_float_alignment( $settings['alignmentMobile'] ) );
155 } else {
156 $mobile_css->add_property( 'text-align', $settings['alignmentMobile'] );
157
158 if (
159 $settings['alignmentMobile'] &&
160 ! $disable_float &&
161 (
162 $has_desktop_float ||
163 $has_tablet_float
164 )
165 ) {
166 $mobile_css->add_property( 'float', 'none' );
167 }
168 }
169
170 $mobile_css->set_selector( '.gb-image-' . $id );
171 generateblocks_add_border_css( $mobile_css, $settings, 'Mobile' );
172 $mobile_css->add_property( 'width', $settings['widthMobile'] );
173 $mobile_css->add_property( 'height', $settings['heightMobile'] );
174 $mobile_css->add_property( 'object-fit', $settings['objectFitMobile'] );
175
176 // Store this block ID in memory.
177 self::store_block_id( $id );
178
179 /**
180 * Do generateblocks_block_css_data hook
181 *
182 * @since 1.0
183 *
184 * @param string $name The name of our block.
185 * @param array $settings The settings for the current block.
186 * @param object $css Our desktop/main CSS data.
187 * @param object $desktop_css Our desktop only CSS data.
188 * @param object $tablet_css Our tablet CSS data.
189 * @param object $tablet_only_css Our tablet only CSS data.
190 * @param object $mobile_css Our mobile CSS data.
191 */
192 do_action(
193 'generateblocks_block_css_data',
194 'image',
195 $settings,
196 $css,
197 $desktop_css,
198 $tablet_css,
199 $tablet_only_css,
200 $mobile_css
201 );
202
203 return [
204 'main' => $css->css_output(),
205 'desktop' => $desktop_css->css_output(),
206 'tablet' => $tablet_css->css_output(),
207 'tablet_only' => $tablet_only_css->css_output(),
208 'mobile' => $mobile_css->css_output(),
209 ];
210 }
211
212 /**
213 * Wrapper function for our dynamic buttons.
214 *
215 * @since 1.6.0
216 * @param array $attributes The block attributes.
217 * @param string $content The dynamic text to display.
218 * @param WP_Block $block Block instance.
219 */
220 public static function render_block( $attributes, $content, $block ) {
221 if ( empty( $attributes['useDynamicData'] ) ) {
222 // Add styles to this block if needed.
223 $content = generateblocks_maybe_add_block_css(
224 $content,
225 [
226 'class_name' => 'GenerateBlocks_Block_Image',
227 'attributes' => $attributes,
228 'block_ids' => self::$block_ids,
229 ]
230 );
231
232 return generateblocks_filter_images( $content, $attributes );
233 }
234
235 $image = empty( $attributes['dynamicContentType'] )
236 ? generateblocks_filter_images( GenerateBlocks_Dynamic_Content::get_static_content( $content ), $attributes )
237 : GenerateBlocks_Dynamic_Content::get_dynamic_image( $attributes, $block );
238
239 if ( ! $image ) {
240 return '';
241 }
242
243 $defaults = generateblocks_get_block_defaults();
244
245 $settings = wp_parse_args(
246 $attributes,
247 $defaults['image']
248 );
249
250 // Add styles to this block if needed.
251 $output = generateblocks_maybe_add_block_css(
252 '',
253 [
254 'class_name' => 'GenerateBlocks_Block_Image',
255 'attributes' => $attributes,
256 'block_ids' => self::$block_ids,
257 ]
258 );
259
260 $output .= sprintf(
261 '<figure %s>',
262 generateblocks_attr(
263 'image-figure',
264 array(
265 'class' => implode(
266 ' ',
267 array(
268 'gb-block-image',
269 'gb-block-image-' . $settings['uniqueId'],
270 )
271 ),
272 ),
273 $settings,
274 $block
275 )
276 );
277
278 $dynamic_link = GenerateBlocks_Dynamic_Content::get_dynamic_url( $attributes, $block );
279
280 if ( $dynamic_link ) {
281 $relAttributes = array();
282
283 if ( ! empty( $settings['relNoFollow'] ) ) {
284 $relAttributes[] = 'nofollow';
285 }
286
287 if ( ! empty( $settings['openInNewWindow'] ) ) {
288 $relAttributes[] = 'noopener';
289 $relAttributes[] = 'noreferrer';
290 }
291
292 if ( ! empty( $settings['relSponsored'] ) ) {
293 $relAttributes[] = 'sponsored';
294 }
295
296 $image = sprintf(
297 '<a %s>%s</a>',
298 generateblocks_attr(
299 'image-link',
300 array(
301 'class' => '',
302 'href' => $dynamic_link,
303 'rel' => ! empty( $relAttributes ) ? implode( ' ', $relAttributes ) : null,
304 'target' => ! empty( $settings['openInNewWindow'] ) ? '_blank' : null,
305 ),
306 $settings,
307 $block
308 ),
309 $image
310 );
311 }
312
313 $output .= $image;
314
315 if ( isset( $block->parsed_block['innerBlocks'][0]['attrs'] ) ) {
316 $image_id = GenerateBlocks_Dynamic_Content::get_dynamic_image_id( $attributes );
317 $block->parsed_block['innerBlocks'][0]['attrs']['dynamicImage'] = $image_id;
318
319 $caption = (
320 new WP_Block(
321 $block->parsed_block['innerBlocks'][0]
322 )
323 )->render( array( 'dynamic' => true ) );
324
325 if ( $caption ) {
326 $output .= $caption;
327 }
328 }
329
330 $output .= '</figure>';
331
332 return $output;
333 }
334 }
335