PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.0
GenerateBlocks v1.8.0
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-container.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-container.php
1120 lines
1 <?php
2 /**
3 * Handles the Container block.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Add Container related functions.
14 */
15 class GenerateBlocks_Block_Container {
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 $container_width = generateblocks_get_option( 'container_width' );
35
36 if ( function_exists( 'generate_get_option' ) ) {
37 $container_width = generate_get_option( 'container_width' );
38 }
39
40 return [
41 'tagName' => 'div',
42 'isGrid' => false,
43 'backgroundColor' => '',
44 'gradient' => false,
45 'gradientDirection' => '',
46 'gradientColorOne' => '',
47 'gradientColorOneOpacity' => '',
48 'gradientColorStopOne' => '',
49 'gradientColorTwo' => '',
50 'gradientColorTwoOpacity' => '',
51 'gradientColorStopTwo' => '',
52 'gradientSelector' => 'element',
53 'textColor' => '',
54 'linkColor' => '',
55 'linkColorHover' => '',
56 'bgImage' => '',
57 'bgOptions' => array(
58 'selector' => 'element',
59 'opacity' => 1,
60 'overlay' => false,
61 'position' => 'center center',
62 'size' => 'cover',
63 'repeat' => 'no-repeat',
64 'attachment' => '',
65 ),
66 'bgImageSize' => 'full',
67 'bgImageInline' => false,
68 'fontFamilyFallback' => '',
69 'googleFont' => false,
70 'googleFontVariants' => '',
71 'useInnerContainer' => false,
72 'variantRole' => '',
73 // Deprecated attributes.
74 'containerWidth' => $container_width,
75 'outerContainer' => 'full',
76 'innerContainer' => 'contained',
77 'minHeight' => false,
78 'minHeightUnit' => 'px',
79 'minHeightTablet' => false,
80 'minHeightUnitTablet' => 'px',
81 'minHeightMobile' => false,
82 'minHeightUnitMobile' => 'px',
83 'width' => '',
84 'widthTablet' => '',
85 'widthMobile' => '',
86 'autoWidth' => false,
87 'autoWidthTablet' => false,
88 'autoWidthMobile' => false,
89 'flexBasisUnit' => 'px',
90 'verticalAlignment' => '',
91 'verticalAlignmentTablet' => 'inherit',
92 'verticalAlignmentMobile' => 'inherit',
93 'borderColorOpacity' => 1,
94 'backgroundColorOpacity' => 1,
95 'fontSize' => '',
96 'fontSizeTablet' => '',
97 'fontSizeMobile' => '',
98 'fontSizeUnit' => 'px',
99 'fontWeight' => '',
100 'textTransform' => '',
101 'alignment' => '',
102 'alignmentTablet' => '',
103 'alignmentMobile' => '',
104 'removeVerticalGap' => false,
105 'removeVerticalGapTablet' => false,
106 'removeVerticalGapMobile' => false,
107 'fontFamily' => '',
108 'borderColor' => '',
109 'innerZindex' => '',
110 ];
111 }
112
113 /**
114 * Store our block ID in memory.
115 *
116 * @param string $id The block ID to store.
117 */
118 public static function store_block_id( $id ) {
119 self::$block_ids[] = $id;
120 }
121
122 /**
123 * Check if our block ID exists.
124 *
125 * @param string $id The block ID to store.
126 */
127 public static function block_id_exists( $id ) {
128 return in_array( $id, (array) self::$block_ids );
129 }
130
131 /**
132 * Compile our CSS data based on our block attributes.
133 *
134 * @param array $attributes Our block attributes.
135 */
136 public static function get_css_data( $attributes ) {
137 $css = new GenerateBlocks_Dynamic_CSS();
138 $desktop_css = new GenerateBlocks_Dynamic_CSS();
139 $tablet_css = new GenerateBlocks_Dynamic_CSS();
140 $tablet_only_css = new GenerateBlocks_Dynamic_CSS();
141 $mobile_css = new GenerateBlocks_Dynamic_CSS();
142 $css_data = [];
143
144 $defaults = generateblocks_get_block_defaults();
145
146 $settings = wp_parse_args(
147 $attributes,
148 $defaults['container']
149 );
150
151 $id = $attributes['uniqueId'];
152 $blockVersion = ! empty( $settings['blockVersion'] ) ? $settings['blockVersion'] : 1;
153
154 // Use legacy settings if needed.
155 if ( $blockVersion < 2 ) {
156 $settings = GenerateBlocks_Legacy_Attributes::get_settings( '1.4.0', 'container', $settings, $attributes );
157 }
158
159 // Map deprecated settings.
160 $settings = GenerateBlocks_Map_Deprecated_Attributes::map_attributes( $settings );
161
162 $selector = generateblocks_get_css_selector( 'container', $attributes );
163 $use_visited_selector = generateblocks_use_visited_selector( 'container', $attributes );
164 $settings['useInnerContainer'] = $blockVersion < 3 || $settings['useInnerContainer'];
165 $useInnerContainer = $settings['useInnerContainer'];
166
167 if ( ! isset( $settings['bgOptions']['selector'] ) ) {
168 $settings['bgOptions']['selector'] = 'element';
169 }
170
171 $containerWidth = $settings['containerWidth'];
172
173 if ( isset( $settings['useGlobalStyle'] ) && $settings['useGlobalStyle'] ) {
174 if ( (string) $containerWidth === (string) $defaults['container']['containerWidth'] ) {
175 $containerWidth = '';
176 }
177 }
178
179 $backgroundImageValue = generateblocks_get_background_image_css( 'image', $settings );
180 $gradientValue = generateblocks_get_background_image_css( 'gradient', $settings );
181 $hasBgImage = generateblocks_has_background_image( $settings );
182
183 // Only add this CSS once.
184 if ( ! self::$singular_css_added ) {
185 $css->set_selector( '.gb-container .wp-block-image img' );
186 $css->add_property( 'vertical-align', 'middle' );
187
188 $css->set_selector( '.gb-container .gb-shape' );
189 $css->add_property( 'position', 'absolute' );
190 $css->add_property( 'overflow', 'hidden' );
191 $css->add_property( 'pointer-events', 'none' );
192 $css->add_property( 'line-height', '0' );
193
194 $css->set_selector( '.gb-container .gb-shape svg' );
195 $css->add_property( 'fill', 'currentColor' );
196
197 do_action(
198 'generateblocks_block_one_time_css_data',
199 'container',
200 $settings,
201 $css
202 );
203
204 self::$singular_css_added = true;
205 }
206
207 /**
208 * Main Container selector.
209 *
210 * Example: .gb-container-{ $uniqueId }
211 */
212 $css->set_selector( $selector );
213 generateblocks_add_sizing_css( $css, $settings );
214 generateblocks_add_layout_css( $css, $settings );
215 generateblocks_add_flex_child_css( $css, $settings );
216 generateblocks_add_typography_css( $css, $settings );
217 generateblocks_add_spacing_css( $css, $settings );
218 generateblocks_add_border_css( $css, $settings );
219 $css->add_property( 'background-color', generateblocks_hex2rgba( $settings['backgroundColor'], $settings['backgroundColorOpacity'] ) );
220 $css->add_property( 'color', $settings['textColor'] );
221
222 if ( $hasBgImage && 'element' === $settings['bgOptions']['selector'] && $backgroundImageValue ) {
223 if ( ! $settings['bgImageInline'] || ( $settings['bgImageInline'] && 'element' !== $settings['bgOptions']['selector'] ) ) {
224 $css->add_property( 'background-image', $backgroundImageValue );
225 }
226
227 $css->add_property( 'background-repeat', $settings['bgOptions']['repeat'] );
228 $css->add_property( 'background-position', $settings['bgOptions']['position'] );
229 $css->add_property( 'background-size', $settings['bgOptions']['size'] );
230 $css->add_property( 'background-attachment', $settings['bgOptions']['attachment'] );
231 } elseif ( $settings['gradient'] && 'element' === $settings['gradientSelector'] ) {
232 $css->add_property( 'background-image', $gradientValue );
233 }
234
235 if ( $useInnerContainer ) {
236 if (
237 ( $hasBgImage && 'pseudo-element' === $settings['bgOptions']['selector'] ) ||
238 $settings['zindex'] ||
239 ( $settings['gradient'] && 'pseudo-element' === $settings['gradientSelector'] )
240 ) {
241 $css->add_property( 'position', 'relative' );
242 }
243
244 if (
245 ( $hasBgImage && 'pseudo-element' === $settings['bgOptions']['selector'] ) ||
246 ( $settings['gradient'] && 'pseudo-element' === $settings['gradientSelector'] )
247 ) {
248 $css->add_property( 'overflow', 'hidden' );
249 }
250 }
251
252 if ( $blockVersion < 3 ) {
253 $css->add_property( 'min-height', $settings['minHeight'], $settings['minHeightUnit'] );
254 }
255
256 // Set flags so we don't duplicate this CSS in media queries.
257 $usingMinHeightFlex = false;
258 $usingMinHeightInnerWidth = false;
259
260 if ( $useInnerContainer ) {
261 if ( $settings['zindex'] ) {
262 $css->add_property( 'z-index', $settings['zindex'] );
263 }
264
265 if ( 'contained' === $settings['outerContainer'] && ! $settings['isGrid'] ) {
266 if ( ! empty( $containerWidth ) ) {
267 $css->add_property( 'max-width', absint( $containerWidth ), 'px' );
268 $css->add_property( 'margin-left', 'auto' );
269 $css->add_property( 'margin-right', 'auto' );
270 }
271 }
272
273 $minHeight = $blockVersion < 3 ? $settings['minHeight'] : generateblocks_get_array_attribute_value( 'minHeight', $settings['sizing'] );
274
275 if ( $minHeight && $settings['verticalAlignment'] && ! $settings['isGrid'] ) {
276 $css->add_property( 'display', 'flex' );
277 $css->add_property( 'flex-direction', 'row' );
278 $css->add_property( 'align-items', $settings['verticalAlignment'] );
279
280 $usingMinHeightFlex = true;
281 }
282 }
283
284 $innerZIndex = $settings['innerZindex'];
285
286 /**
287 * Container before pseudo selector.
288 *
289 * Example: .gb-container-{ $uniqueId }:before
290 */
291 $css->set_selector( $selector . ':before' );
292
293 if ( $hasBgImage && 'pseudo-element' === $settings['bgOptions']['selector'] ) {
294 $css->add_property( 'content', '""' );
295 $css->add_property( 'background-image', $backgroundImageValue );
296 $css->add_property( 'background-repeat', $settings['bgOptions']['repeat'] );
297 $css->add_property( 'background-position', $settings['bgOptions']['position'] );
298 $css->add_property( 'background-size', $settings['bgOptions']['size'] );
299 $css->add_property( 'background-attachment', $settings['bgOptions']['attachment'] );
300 $css->add_property( 'z-index', '0' );
301 $css->add_property( 'position', 'absolute' );
302 $css->add_property( 'top', '0' );
303 $css->add_property( 'right', '0' );
304 $css->add_property( 'bottom', '0' );
305 $css->add_property( 'left', '0' );
306 $css->add_property( 'transition', 'inherit' );
307 $css->add_property(
308 'border-radius',
309 array(
310 generateblocks_get_array_attribute_value( 'borderTopLeftRadius', $settings['borders'] ),
311 generateblocks_get_array_attribute_value( 'borderTopRightRadius', $settings['borders'] ),
312 generateblocks_get_array_attribute_value( 'borderBottomRightRadius', $settings['borders'] ),
313 generateblocks_get_array_attribute_value( 'borderBottomLeftRadius', $settings['borders'] ),
314 )
315 );
316 $css->add_property( 'pointer-events', 'none' );
317
318 if ( isset( $settings['bgOptions']['opacity'] ) && 1 !== $settings['bgOptions']['opacity'] ) {
319 $css->add_property( 'opacity', $settings['bgOptions']['opacity'] );
320 }
321
322 if ( $blockVersion < 2 && ! $innerZIndex ) {
323 $innerZIndex = 1;
324 }
325 }
326
327 /**
328 * Container after pseudo selector.
329 *
330 * Example: .gb-container-{ $uniqueId }:after
331 */
332 if ( $settings['gradient'] && 'pseudo-element' === $settings['gradientSelector'] ) {
333 $css->set_selector( $selector . ':after' );
334 $css->add_property( 'content', '""' );
335 $css->add_property( 'background-image', $gradientValue );
336 $css->add_property( 'z-index', '0' );
337 $css->add_property( 'position', 'absolute' );
338 $css->add_property( 'top', '0' );
339 $css->add_property( 'right', '0' );
340 $css->add_property( 'bottom', '0' );
341 $css->add_property( 'left', '0' );
342 $css->add_property( 'pointer-events', 'none' );
343
344 if ( $blockVersion < 2 && ! $innerZIndex ) {
345 $innerZIndex = 1;
346 }
347 }
348
349 /**
350 * Legacy inner Container selector.
351 *
352 * Example: .gb-container-{ $uniqueId } > .gb-inside-container
353 */
354 if ( $useInnerContainer ) {
355 $css->set_selector( $selector . ' > .gb-inside-container' );
356 if ( $blockVersion < 4 ) {
357 $css->add_property( 'padding', array( $settings['paddingTop'], $settings['paddingRight'], $settings['paddingBottom'], $settings['paddingLeft'] ), $settings['paddingUnit'] );
358 } else {
359 $css->add_property(
360 'padding',
361 array(
362 generateblocks_get_array_attribute_value( 'paddingTop', $settings['spacing'] ),
363 generateblocks_get_array_attribute_value( 'paddingRight', $settings['spacing'] ),
364 generateblocks_get_array_attribute_value( 'paddingBottom', $settings['spacing'] ),
365 generateblocks_get_array_attribute_value( 'paddingLeft', $settings['spacing'] ),
366 )
367 );
368 }
369
370 if ( 'contained' === $settings['innerContainer'] && ! $settings['isGrid'] ) {
371 if ( ! empty( $containerWidth ) ) {
372 $css->add_property( 'max-width', absint( $containerWidth ), 'px' );
373 $css->add_property( 'margin-left', 'auto' );
374 $css->add_property( 'margin-right', 'auto' );
375 }
376 }
377
378 if ( $usingMinHeightFlex ) {
379 $css->add_property( 'width', '100%' );
380
381 $usingMinHeightInnerWidth = true;
382 }
383
384 if ( $innerZIndex || 0 === $innerZIndex ) {
385 $css->add_property( 'z-index', $innerZIndex );
386 $css->add_property( 'position', 'relative' );
387 }
388 }
389
390 /**
391 * Container hover.
392 *
393 * Example: .gb-container-{ $uniqueId }:hover
394 */
395 $css->set_selector( $selector . ':hover' );
396 generateblocks_add_border_color_css( $css, $settings, 'Hover' );
397
398 /**
399 * Container current.
400 *
401 * Example: .gb-container-{ $uniqueId }.gb-block-is-current
402 */
403 $current_selector = sprintf(
404 '%1$s.gb-block-is-current, %1$s.gb-block-is-current:hover, %1$s.gb-block-is-current:active, %1$s.gb-block-is-current:focus',
405 $selector
406 );
407
408 $css->set_selector( $current_selector );
409 generateblocks_add_border_color_css( $css, $settings, 'Current' );
410
411 /**
412 * Container links.
413 *
414 * Example: .gb-container-{ $uniqueId } a
415 */
416 $visited_selector = $use_visited_selector
417 ? ', ' . $selector . ' a:visited'
418 : '';
419
420 $css->set_selector( $selector . ' a' . $visited_selector );
421 $css->add_property( 'color', $settings['linkColor'] );
422
423 $css->set_selector( $selector . ' a:hover' );
424 $css->add_property( 'color', $settings['linkColorHover'] );
425
426 /**
427 * Grid item selector.
428 *
429 * Example: .gb-grid-wrapper > .gb-grid-column-{ $uniqueId }
430 */
431 if ( $settings['isGrid'] ) {
432 $css->set_selector( '.gb-grid-wrapper > .gb-grid-column-' . $id );
433
434 if ( $blockVersion < 3 ) {
435 $css->add_property( 'width', $settings['width'], '%' );
436 } else {
437 $css->add_property( 'width', generateblocks_get_array_attribute_value( 'width', $settings['sizing'] ) );
438 }
439
440 $css->add_property( 'flex-grow', $settings['flexGrow'] );
441 $css->add_property( 'flex-shrink', $settings['flexShrink'] );
442
443 if ( is_numeric( $settings['flexBasis'] ) && $blockVersion < 3 ) {
444 $css->add_property( 'flex-basis', $settings['flexBasis'], $settings['flexBasisUnit'] );
445 } else {
446 $css->add_property( 'flex-basis', $settings['flexBasis'] );
447 }
448
449 if ( $settings['isGrid'] ) {
450 $css->add_property( 'order', $settings['order'] );
451 }
452 }
453
454 /**
455 * Grid item selector with tag name.
456 *
457 * This was used for the removeVerticalGap option which was deprecated
458 * in version 3 of the Grid block.
459 *
460 * Example: .gb-grid-wrapper > div.gb-grid-column-{ $uniqueId }
461 */
462 if ( $settings['removeVerticalGap'] ) {
463 $desktop_css->set_selector( '.gb-grid-wrapper > div.gb-grid-column-' . $id );
464 $desktop_css->add_property( 'padding-bottom', '0' );
465 }
466
467 /**
468 * Grid item Container selector.
469 *
470 * Example: .gb-grid-wrapper > .gb-grid-column-{ $uniqueId } > .gb-container
471 */
472 if ( $useInnerContainer ) {
473 $css->set_selector( '.gb-grid-wrapper > .gb-grid-column-' . $id . ' > .gb-container' );
474 $css->add_property( 'justify-content', $settings['verticalAlignment'] );
475 $css->add_property( 'display', 'flex' );
476 $css->add_property( 'flex-direction', 'column' );
477 $css->add_property( 'height', '100%' );
478 }
479
480 /**
481 * Container selector for shapes.
482 *
483 * Example: .gb-container-{ $uniqueId }
484 */
485 if ( ! empty( $settings['shapeDividers'] ) ) {
486 $css->set_selector( $selector );
487
488 if ( $useInnerContainer ) {
489 $css->add_property( 'position', 'relative' );
490 }
491
492 $default_styles = generateblocks_get_default_styles();
493
494 foreach ( (array) $settings['shapeDividers'] as $index => $options ) {
495 $shapeNumber = $index + 1;
496
497 $shapeOptions = wp_parse_args(
498 $options,
499 $default_styles['container']['shapeDividers']
500 );
501
502 $shapeTransforms = array();
503
504 if ( 'top' === $shapeOptions['location'] ) {
505 $shapeTransforms[] = 'scaleY(-1)';
506 }
507
508 if ( $shapeOptions['flipHorizontally'] ) {
509 $shapeTransforms[] = 'scaleX(-1)';
510 }
511
512 $css->set_selector( $selector . ' > .gb-shapes .gb-shape-' . $shapeNumber );
513 $css->add_property( 'color', generateblocks_hex2rgba( $shapeOptions['color'], $shapeOptions['colorOpacity'] ) );
514 $css->add_property( 'z-index', $shapeOptions['zindex'] );
515
516 if ( 'top' === $shapeOptions['location'] || 'bottom' === $shapeOptions['location'] ) {
517 $css->add_property( 'left', '0' );
518 $css->add_property( 'right', '0' );
519 }
520
521 if ( 'bottom' === $shapeOptions['location'] ) {
522 $css->add_property( 'bottom', '-1px' );
523 }
524
525 if ( 'top' === $shapeOptions['location'] ) {
526 $css->add_property( 'top', '-1px' );
527 }
528
529 if ( ! empty( $shapeTransforms ) ) {
530 $css->add_property( 'transform', implode( ' ', $shapeTransforms ) );
531 }
532
533 $shapeWidth = $shapeOptions['width'] . '%';
534
535 if ( 100 === (int) $shapeOptions['width'] ) {
536 $shapeWidth = 'calc(' . $shapeWidth . ' + 1.3px)';
537 }
538
539 $css->set_selector( $selector . ' > .gb-shapes .gb-shape-' . $shapeNumber . ' svg' );
540 $css->add_property( 'height', $shapeOptions['height'], 'px' );
541 $css->add_property( 'width', $shapeWidth );
542
543 if ( 'top' === $shapeOptions['location'] || 'bottom' === $shapeOptions['location'] ) {
544 $css->add_property( 'position', 'relative' );
545 $css->add_property( 'left', '50%' );
546 $css->add_property( 'transform', 'translateX(-50%)' );
547 $css->add_property( 'min-width', '100%' );
548 }
549 }
550 }
551
552 /**
553 * Main Container selector for tablet.
554 *
555 * Example: .gb-container-{ $uniqueId }
556 */
557 $tablet_css->set_selector( $selector );
558 generateblocks_add_sizing_css( $tablet_css, $settings, 'Tablet' );
559 generateblocks_add_layout_css( $tablet_css, $settings, 'Tablet' );
560 generateblocks_add_flex_child_css( $tablet_css, $settings, 'Tablet' );
561 generateblocks_add_typography_css( $tablet_css, $settings, 'Tablet' );
562 generateblocks_add_spacing_css( $tablet_css, $settings, 'Tablet' );
563 generateblocks_add_border_css( $tablet_css, $settings, 'Tablet' );
564
565 if ( $blockVersion < 3 ) {
566 $tablet_css->add_property( 'min-height', $settings['minHeightTablet'], $settings['minHeightUnitTablet'] );
567 }
568
569 if ( $useInnerContainer ) {
570 // Need to check if we're using minHeightTablet in two places below.
571 $minHeightTablet = $blockVersion < 3 ? $settings['minHeightTablet'] : generateblocks_get_array_attribute_value( 'minHeightTablet', $settings['sizing'] );
572
573 if ( ! $settings['isGrid'] ) {
574 if ( ! $usingMinHeightFlex && $minHeightTablet && 'inherit' !== $settings['verticalAlignmentTablet'] ) {
575 $tablet_css->add_property( 'display', 'flex' );
576 $tablet_css->add_property( 'flex-direction', 'row' );
577
578 $usingMinHeightFlex = true;
579 }
580
581 if ( $usingMinHeightFlex && 'inherit' !== $settings['verticalAlignmentTablet'] ) {
582 $tablet_css->add_property( 'align-items', $settings['verticalAlignmentTablet'] );
583 }
584 }
585
586 /**
587 * Legacy inner Container selector for tablet.
588 *
589 * Example: .gb-container-{ $uniqueId } > .gb-inside-container
590 */
591 $tablet_css->set_selector( $selector . ' > .gb-inside-container' );
592
593 if ( $blockVersion < 4 ) {
594 $tablet_css->add_property( 'padding', array( $settings['paddingTopTablet'], $settings['paddingRightTablet'], $settings['paddingBottomTablet'], $settings['paddingLeftTablet'] ), $settings['paddingUnit'] );
595 } else {
596 $tablet_css->add_property(
597 'padding',
598 array(
599 generateblocks_get_array_attribute_value( 'paddingTopTablet', $settings['spacing'] ),
600 generateblocks_get_array_attribute_value( 'paddingRightTablet', $settings['spacing'] ),
601 generateblocks_get_array_attribute_value( 'paddingBottomTablet', $settings['spacing'] ),
602 generateblocks_get_array_attribute_value( 'paddingLeftTablet', $settings['spacing'] ),
603 )
604 );
605 }
606
607 $usingMinHeightInnerWidthBoxSizing = false;
608
609 if ( ! $settings['isGrid'] ) {
610 // Needs 100% width if it's a flex item.
611 if ( ! $usingMinHeightInnerWidth && $minHeightTablet && 'inherit' !== $settings['verticalAlignmentTablet'] ) {
612 $tablet_css->add_property( 'width', '100%' );
613
614 $usingMinHeightInnerWidth = true;
615 } elseif ( $usingMinHeightInnerWidth ) {
616 if ( 'contained' === $settings['innerContainer'] && ! $settings['isGrid'] ) {
617 $tablet_css->add_property( 'box-sizing', 'border-box' );
618
619 $usingMinHeightInnerWidthBoxSizing = true;
620 }
621 }
622 }
623 }
624
625 /**
626 * Grid item selector for tablet.
627 *
628 * Example: .gb-grid-wrapper > .gb-grid-column-{ $uniqueId }
629 */
630 $tablet_css->set_selector( '.gb-grid-wrapper > .gb-grid-column-' . $id );
631
632 if ( $blockVersion < 3 ) {
633 if ( ! $settings['autoWidthTablet'] ) {
634 $tablet_css->add_property( 'width', $settings['widthTablet'], '%' );
635 } else {
636 $tablet_css->add_property( 'width', 'auto' );
637 }
638 } else {
639 $tablet_css->add_property( 'width', generateblocks_get_array_attribute_value( 'widthTablet', $settings['sizing'] ) );
640 }
641
642 $tablet_css->add_property( 'flex-grow', $settings['flexGrowTablet'] );
643 $tablet_css->add_property( 'flex-shrink', $settings['flexShrinkTablet'] );
644
645 if ( is_numeric( $settings['flexBasisTablet'] ) ) {
646 $tablet_css->add_property( 'flex-basis', $settings['flexBasisTablet'], $settings['flexBasisUnit'] );
647 } else {
648 $tablet_css->add_property( 'flex-basis', $settings['flexBasisTablet'] );
649 }
650
651 if ( $settings['isGrid'] ) {
652 $tablet_css->add_property( 'order', $settings['orderTablet'] );
653 }
654
655 /**
656 * Grid item selector with tag name for tablet.
657 *
658 * This was used for the removeVerticalGap option which was deprecated
659 * in version 3 of the Grid block.
660 *
661 * Example: .gb-grid-wrapper > div.gb-grid-column-{ $uniqueId }
662 */
663 if ( $settings['removeVerticalGapTablet'] ) {
664 $tablet_only_css->set_selector( '.gb-grid-wrapper > div.gb-grid-column-' . $id );
665 $tablet_only_css->add_property( 'padding-bottom', '0' );
666 }
667
668 /**
669 * Grid item Container selector for tablet.
670 *
671 * Example: .gb-grid-wrapper > .gb-grid-column-{ $uniqueId } > .gb-container
672 */
673 if ( $useInnerContainer ) {
674 $tablet_css->set_selector( '.gb-grid-wrapper > .gb-grid-column-' . $id . ' > .gb-container' );
675
676 if ( 'inherit' !== $settings['verticalAlignmentTablet'] ) {
677 $tablet_css->add_property( 'justify-content', $settings['verticalAlignmentTablet'] );
678 }
679 }
680
681 /**
682 * Container before pseudo selector for tablet.
683 *
684 * Example: .gb-container-{ $uniqueId }:before
685 */
686 if ( $hasBgImage && 'pseudo-element' === $settings['bgOptions']['selector'] ) {
687 $tablet_css->set_selector( $selector . ':before' );
688 $tablet_css->add_property(
689 'border-radius',
690 array(
691 generateblocks_get_array_attribute_value( 'borderTopLeftRadiusTablet', $settings['borders'] ),
692 generateblocks_get_array_attribute_value( 'borderTopRightRadiusTablet', $settings['borders'] ),
693 generateblocks_get_array_attribute_value( 'borderBottomRightRadiusTablet', $settings['borders'] ),
694 generateblocks_get_array_attribute_value( 'borderBottomLeftRadiusTablet', $settings['borders'] ),
695 )
696 );
697 }
698
699 /**
700 * Shape selector for tablets.
701 *
702 * Example: .gb-container-{ $uniqueId } > .gb-shapes .gb-shape-{ $shapeNumber } svg
703 */
704 if ( ! empty( $settings['shapeDividers'] ) ) {
705 $default_styles = generateblocks_get_default_styles();
706
707 foreach ( (array) $settings['shapeDividers'] as $index => $options ) {
708 $shapeNumber = $index + 1;
709
710 $shapeOptions = wp_parse_args(
711 $options,
712 $default_styles['container']['shapeDividers']
713 );
714
715 $tablet_css->set_selector( $selector . ' > .gb-shapes .gb-shape-' . $shapeNumber . ' svg' );
716 $tablet_css->add_property( 'height', $shapeOptions['heightTablet'], 'px' );
717 $tablet_css->add_property( 'width', $shapeOptions['widthTablet'], '%' );
718 }
719 }
720
721 /**
722 * Main Container selector for mobile.
723 *
724 * Example: .gb-container-{ $uniqueId }
725 */
726 $mobile_css->set_selector( $selector );
727 generateblocks_add_sizing_css( $mobile_css, $settings, 'Mobile' );
728 generateblocks_add_layout_css( $mobile_css, $settings, 'Mobile' );
729 generateblocks_add_flex_child_css( $mobile_css, $settings, 'Mobile' );
730 generateblocks_add_typography_css( $mobile_css, $settings, 'Mobile' );
731 generateblocks_add_spacing_css( $mobile_css, $settings, 'Mobile' );
732 generateblocks_add_border_css( $mobile_css, $settings, 'Mobile' );
733
734 if ( $blockVersion < 3 ) {
735 $mobile_css->add_property( 'min-height', $settings['minHeightMobile'], $settings['minHeightUnitMobile'] );
736 }
737
738 if ( $useInnerContainer ) {
739 // Need to check if we're using minHeightMobile in two places below.
740 $minHeightMobile = $blockVersion < 3 ? $settings['minHeightMobile'] : generateblocks_get_array_attribute_value( 'minHeightMobile', $settings['sizing'] );
741
742 if ( ! $settings['isGrid'] ) {
743 if ( ! $usingMinHeightFlex && $minHeightMobile && 'inherit' !== $settings['verticalAlignmentMobile'] ) {
744 $mobile_css->add_property( 'display', 'flex' );
745 $mobile_css->add_property( 'flex-direction', 'row' );
746
747 $usingMinHeightFlex = true;
748 }
749
750 if ( $usingMinHeightFlex && 'inherit' !== $settings['verticalAlignmentMobile'] ) {
751 $mobile_css->add_property( 'align-items', $settings['verticalAlignmentMobile'] );
752 }
753 }
754
755 /**
756 * Legacy inner Container selector for mobile.
757 *
758 * Example: .gb-container-{ $uniqueId } > .gb-inside-container
759 */
760 $mobile_css->set_selector( $selector . ' > .gb-inside-container' );
761
762 if ( $blockVersion < 4 ) {
763 $mobile_css->add_property( 'padding', array( $settings['paddingTopMobile'], $settings['paddingRightMobile'], $settings['paddingBottomMobile'], $settings['paddingLeftMobile'] ), $settings['paddingUnit'] );
764 } else {
765 $mobile_css->add_property(
766 'padding',
767 array(
768 generateblocks_get_array_attribute_value( 'paddingTopMobile', $settings['spacing'] ),
769 generateblocks_get_array_attribute_value( 'paddingRightMobile', $settings['spacing'] ),
770 generateblocks_get_array_attribute_value( 'paddingBottomMobile', $settings['spacing'] ),
771 generateblocks_get_array_attribute_value( 'paddingLeftMobile', $settings['spacing'] ),
772 )
773 );
774 }
775
776 if ( ! $settings['isGrid'] ) {
777 // Needs 100% width if it's a flex item.
778 if ( ! $usingMinHeightInnerWidth && $minHeightMobile && 'inherit' !== $settings['verticalAlignmentMobile'] ) {
779 $mobile_css->add_property( 'width', '100%' );
780 } elseif ( $usingMinHeightInnerWidth && ! $usingMinHeightInnerWidthBoxSizing ) {
781 if ( 'contained' === $settings['innerContainer'] && ! $settings['isGrid'] ) {
782 $mobile_css->add_property( 'box-sizing', 'border-box' );
783 }
784 }
785 }
786 }
787
788 /**
789 * Grid item selector for tablet.
790 *
791 * Example: .gb-grid-wrapper > .gb-grid-column-{ $uniqueId }
792 */
793 $mobile_css->set_selector( '.gb-grid-wrapper > .gb-grid-column-' . $id );
794
795 if ( $blockVersion < 3 ) {
796 if ( ! $settings['autoWidthMobile'] ) {
797 $mobile_css->add_property( 'width', $settings['widthMobile'], '%' );
798 }
799
800 if ( $settings['autoWidthMobile'] ) {
801 $mobile_css->add_property( 'width', 'auto' );
802 }
803 } else {
804 $mobile_css->add_property( 'width', generateblocks_get_array_attribute_value( 'widthMobile', $settings['sizing'] ) );
805 }
806
807 $mobile_css->add_property( 'flex-grow', $settings['flexGrowMobile'] );
808 $mobile_css->add_property( 'flex-shrink', $settings['flexShrinkMobile'] );
809
810 if ( is_numeric( $settings['flexBasisMobile'] ) ) {
811 $mobile_css->add_property( 'flex-basis', $settings['flexBasisMobile'], $settings['flexBasisUnit'] );
812 } else {
813 $mobile_css->add_property( 'flex-basis', $settings['flexBasisMobile'] );
814 }
815
816 if ( $settings['isGrid'] ) {
817 $mobile_css->add_property( 'order', $settings['orderMobile'] );
818 }
819
820 /**
821 * Grid item selector with tag name for mobile.
822 *
823 * This was used for the removeVerticalGap option which was deprecated
824 * in version 3 of the Grid block.
825 *
826 * Example: .gb-grid-wrapper > div.gb-grid-column-{ $uniqueId }
827 */
828 if ( $settings['removeVerticalGapMobile'] ) {
829 $mobile_css->set_selector( '.gb-grid-wrapper > div.gb-grid-column-' . $id );
830 $mobile_css->add_property( 'padding-bottom', '0' );
831 }
832
833 /**
834 * Grid item Container selector for mobile.
835 *
836 * Example: .gb-grid-wrapper > .gb-grid-column-{ $uniqueId } > .gb-container
837 */
838 if ( $useInnerContainer ) {
839 $mobile_css->set_selector( '.gb-grid-wrapper > .gb-grid-column-' . $id . ' > .gb-container' );
840
841 if ( 'inherit' !== $settings['verticalAlignmentMobile'] ) {
842 $mobile_css->add_property( 'justify-content', $settings['verticalAlignmentMobile'] );
843 }
844 }
845
846 /**
847 * Container before pseudo selector for mobile.
848 *
849 * Example: .gb-container-{ $uniqueId }:before
850 */
851 if ( $hasBgImage && 'pseudo-element' === $settings['bgOptions']['selector'] ) {
852 $mobile_css->set_selector( $selector . ':before' );
853 $mobile_css->add_property(
854 'border-radius',
855 array(
856 generateblocks_get_array_attribute_value( 'borderTopLeftRadiusMobile', $settings['borders'] ),
857 generateblocks_get_array_attribute_value( 'borderTopRightRadiusMobile', $settings['borders'] ),
858 generateblocks_get_array_attribute_value( 'borderBottomRightRadiusMobile', $settings['borders'] ),
859 generateblocks_get_array_attribute_value( 'borderBottomLeftRadiusMobile', $settings['borders'] ),
860 )
861 );
862 }
863
864 /**
865 * Shape selector for tablets.
866 *
867 * Example: .gb-container-{ $uniqueId } > .gb-shapes .gb-shape-{ $shapeNumber } svg
868 */
869 if ( ! empty( $settings['shapeDividers'] ) ) {
870 $default_styles = generateblocks_get_default_styles();
871
872 foreach ( (array) $settings['shapeDividers'] as $index => $options ) {
873 $shapeNumber = $index + 1;
874
875 $shapeOptions = wp_parse_args(
876 $options,
877 $default_styles['container']['shapeDividers']
878 );
879
880 $mobile_css->set_selector( $selector . ' > .gb-shapes .gb-shape-' . $shapeNumber . ' svg' );
881 $mobile_css->add_property( 'height', $shapeOptions['heightMobile'], 'px' );
882 $mobile_css->add_property( 'width', $shapeOptions['widthMobile'], '%' );
883 }
884 }
885
886 /**
887 * Conditional selector to disable fixed backgrounds on mobile.
888 *
889 * Example 1: .gb-container-{ $uniqueId }
890 * Example 2: .gb-container-{ $uniqueId }:before
891 */
892 if ( $hasBgImage && 'fixed' === $settings['bgOptions']['attachment'] ) {
893 if ( 'element' === $settings['bgOptions']['selector'] ) {
894 $mobile_css->set_selector( $selector );
895 }
896
897 if ( 'pseudo-element' === $settings['bgOptions']['selector'] ) {
898 $mobile_css->set_selector( $selector . ':before' );
899 }
900
901 $mobile_css->add_property( 'background-attachment', 'initial' );
902 }
903
904 // Store this block ID in memory.
905 self::store_block_id( $id );
906
907 /**
908 * Do generateblocks_block_css_data hook
909 *
910 * @since 1.0
911 *
912 * @param string $name The name of our block.
913 * @param array $settings The settings for the current block.
914 * @param object $css Our desktop/main CSS data.
915 * @param object $desktop_css Our desktop only CSS data.
916 * @param object $tablet_css Our tablet CSS data.
917 * @param object $tablet_only_css Our tablet only CSS data.
918 * @param object $mobile_css Our mobile CSS data.
919 */
920 do_action(
921 'generateblocks_block_css_data',
922 'container',
923 $settings,
924 $css,
925 $desktop_css,
926 $tablet_css,
927 $tablet_only_css,
928 $mobile_css
929 );
930
931 return [
932 'main' => $css->css_output(),
933 'desktop' => $desktop_css->css_output(),
934 'tablet' => $tablet_css->css_output(),
935 'tablet_only' => $tablet_only_css->css_output(),
936 'mobile' => $mobile_css->css_output(),
937 ];
938 }
939
940 /**
941 * Wrapper function for our dynamic buttons.
942 *
943 * @since 1.6.0
944 * @param array $attributes The block attributes.
945 * @param string $content The dynamic text to display.
946 * @param WP_Block $block Block instance.
947 */
948 public static function render_block( $attributes, $content, $block ) {
949 if ( ! isset( $attributes['isDynamic'] ) || ! $attributes['isDynamic'] ) {
950 // Add styles to this block if needed.
951 $content = generateblocks_maybe_add_block_css(
952 $content,
953 [
954 'class_name' => 'GenerateBlocks_Block_Container',
955 'attributes' => $attributes,
956 'block_ids' => self::$block_ids,
957 ]
958 );
959
960 return $content;
961 }
962
963 $defaults = generateblocks_get_block_defaults();
964
965 $settings = wp_parse_args(
966 $attributes,
967 $defaults['container']
968 );
969
970 $blockVersion = ! empty( $settings['blockVersion'] ) ? $settings['blockVersion'] : 1;
971 $useInnerContainer = $blockVersion < 3 || $settings['useInnerContainer'];
972
973 // Add styles to this block if needed.
974 $output = generateblocks_maybe_add_block_css(
975 '',
976 [
977 'class_name' => 'GenerateBlocks_Block_Container',
978 'attributes' => $attributes,
979 'block_ids' => self::$block_ids,
980 ]
981 );
982
983 if ( $settings['isGrid'] ) {
984 $gridItemClassNames = array(
985 'gb-grid-column',
986 'gb-grid-column-' . $settings['uniqueId'],
987 );
988
989 $output .= sprintf(
990 '<div %s>',
991 generateblocks_attr(
992 'grid-item',
993 array(
994 'class' => implode( ' ', $gridItemClassNames ),
995 ),
996 $settings,
997 $block
998 )
999 );
1000 }
1001
1002 $classNames = array(
1003 'gb-container',
1004 'gb-container-' . $settings['uniqueId'],
1005 );
1006
1007 if ( ! empty( $settings['className'] ) ) {
1008 $classNames[] = $settings['className'];
1009 }
1010
1011 if ( ! $settings['isGrid'] && ! empty( $settings['align'] ) ) {
1012 $classNames[] = 'align' . $settings['align'];
1013 }
1014
1015 // Pass the dynamic url to our URL attribute.
1016 if ( isset( $settings['url'] ) ) {
1017 if ( $settings['useDynamicData'] && '' !== $settings['dynamicLinkType'] ) {
1018 $attributes['url'] = GenerateBlocks_Dynamic_Content::get_dynamic_url( $settings, $block );
1019 $settings['url'] = $attributes['url'];
1020 }
1021 }
1022
1023 $tagName = apply_filters(
1024 'generateblocks_container_tagname',
1025 $settings['tagName'],
1026 $attributes,
1027 $block
1028 );
1029
1030 $allowedTagNames = apply_filters(
1031 'generateblocks_container_allowed_tagnames',
1032 array(
1033 'div',
1034 'article',
1035 'section',
1036 'header',
1037 'footer',
1038 'aside',
1039 'a',
1040 ),
1041 $attributes,
1042 $block
1043 );
1044
1045 if ( ! in_array( $tagName, $allowedTagNames ) ) {
1046 $tagName = 'div';
1047 }
1048
1049 $output = apply_filters(
1050 'generateblocks_before_container_open',
1051 $output,
1052 $attributes,
1053 $block
1054 );
1055
1056 $output .= sprintf(
1057 '<%1$s %2$s>',
1058 $tagName,
1059 generateblocks_attr(
1060 'container',
1061 array(
1062 'id' => isset( $settings['anchor'] ) ? $settings['anchor'] : null,
1063 'class' => implode( ' ', $classNames ),
1064 ),
1065 $settings,
1066 $block
1067 )
1068 );
1069
1070 $output = apply_filters(
1071 'generateblocks_after_container_open',
1072 $output,
1073 $attributes,
1074 $block
1075 );
1076
1077 if ( $useInnerContainer ) {
1078 $output .= '<div class="gb-inside-container">';
1079 }
1080
1081 $output = apply_filters(
1082 'generateblocks_inside_container',
1083 $output,
1084 $attributes,
1085 $block
1086 );
1087
1088 $output .= $content;
1089
1090 if ( $useInnerContainer ) {
1091 $output .= '</div>';
1092 }
1093
1094 $output = apply_filters(
1095 'generateblocks_before_container_close',
1096 $output,
1097 $attributes,
1098 $block
1099 );
1100
1101 $output .= sprintf(
1102 '</%s>',
1103 $tagName
1104 );
1105
1106 $output = apply_filters(
1107 'generateblocks_after_container_close',
1108 $output,
1109 $attributes,
1110 $block
1111 );
1112
1113 if ( $settings['isGrid'] ) {
1114 $output .= '</div>';
1115 }
1116
1117 return $output;
1118 }
1119 }
1120