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