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-grid.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-grid.php
314 lines
1 <?php
2 /**
3 * Handles the Grid block.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Add Grid related functions.
14 */
15 class GenerateBlocks_Block_Grid {
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 'horizontalGap' => '',
36 'verticalGap' => '',
37 'verticalAlignment' => '',
38 'horizontalGapTablet' => '',
39 'verticalGapTablet' => '',
40 'verticalAlignmentTablet' => 'inherit',
41 'horizontalGapMobile' => '',
42 'verticalGapMobile' => '',
43 'verticalAlignmentMobile' => 'inherit',
44 'horizontalAlignment' => '',
45 'horizontalAlignmentTablet' => '',
46 'horizontalAlignmentMobile' => '',
47 'useLegacyRowGap' => false,
48 ];
49 }
50
51 /**
52 * Store our block ID in memory.
53 *
54 * @param string $id The block ID to store.
55 */
56 public static function store_block_id( $id ) {
57 self::$block_ids[] = $id;
58 }
59
60 /**
61 * Check if our block ID exists.
62 *
63 * @param string $id The block ID to store.
64 */
65 public static function block_id_exists( $id ) {
66 return in_array( $id, (array) self::$block_ids );
67 }
68
69 /**
70 * Compile our CSS data based on our block attributes.
71 *
72 * @param array $attributes Our block attributes.
73 */
74 public static function get_css_data( $attributes ) {
75 $css = new GenerateBlocks_Dynamic_CSS();
76 $desktop_css = new GenerateBlocks_Dynamic_CSS();
77 $tablet_css = new GenerateBlocks_Dynamic_CSS();
78 $tablet_only_css = new GenerateBlocks_Dynamic_CSS();
79 $mobile_css = new GenerateBlocks_Dynamic_CSS();
80 $css_data = [];
81
82 $defaults = generateblocks_get_block_defaults();
83
84 $settings = wp_parse_args(
85 $attributes,
86 $defaults['gridContainer']
87 );
88
89 $id = $attributes['uniqueId'];
90 $blockVersion = ! empty( $settings['blockVersion'] ) ? $settings['blockVersion'] : 1;
91
92 // Use legacy settings if needed.
93 if ( $blockVersion < 2 ) {
94 $settings = GenerateBlocks_Legacy_Attributes::get_settings( '1.4.0', 'grid', $settings, $attributes );
95 }
96
97 $gap_direction = 'left';
98
99 if ( is_rtl() ) {
100 $gap_direction = 'right';
101 }
102
103 // Don't output horizontal gap defaults if we're using global styles.
104 if ( $blockVersion < 2 && isset( $settings['useGlobalStyle'] ) && $settings['useGlobalStyle'] && isset( $settings['globalStyleId'] ) && $settings['globalStyleId'] ) {
105 if ( (string) $settings['horizontalGap'] === (string) $defaults['gridContainer']['horizontalGap'] ) {
106 $settings['horizontalGap'] = '';
107 }
108 }
109
110 // Only add this CSS once.
111 if ( ! self::$singular_css_added ) {
112 $css->set_selector( '.gb-grid-wrapper' );
113 $css->add_property( 'display', 'flex' );
114 $css->add_property( 'flex-wrap', 'wrap' );
115
116 $css->set_selector( '.gb-grid-column' );
117 $css->add_property( 'box-sizing', 'border-box' );
118
119 $css->set_selector( '.gb-grid-wrapper .wp-block-image' );
120 $css->add_property( 'margin-bottom', '0' );
121
122 do_action(
123 'generateblocks_block_one_time_css_data',
124 'grid',
125 $settings,
126 $css
127 );
128
129 self::$singular_css_added = true;
130 }
131
132 $css->set_selector( '.gb-grid-wrapper-' . $id );
133 $css->add_property( 'align-items', $settings['verticalAlignment'] );
134 $css->add_property( 'justify-content', $settings['horizontalAlignment'] );
135
136 if ( $blockVersion > 2 && ! $settings['useLegacyRowGap'] ) {
137 $css->add_property( 'row-gap', $settings['verticalGap'], 'px' );
138 }
139
140 if ( $settings['horizontalGap'] ) {
141 $css->add_property( 'margin-' . $gap_direction, '-' . $settings['horizontalGap'] . 'px' );
142 }
143
144 $css->set_selector( '.gb-grid-wrapper-' . $id . ' > .gb-grid-column' );
145 $css->add_property( 'padding-' . $gap_direction, $settings['horizontalGap'], 'px' );
146
147 if ( $blockVersion < 3 || $settings['useLegacyRowGap'] ) {
148 $css->add_property( 'padding-bottom', $settings['verticalGap'], 'px' );
149 }
150
151 $tablet_css->set_selector( '.gb-grid-wrapper-' . $id );
152
153 if ( $blockVersion > 2 && ! $settings['useLegacyRowGap'] ) {
154 $tablet_css->add_property( 'row-gap', $settings['verticalGapTablet'], 'px' );
155 }
156
157 if ( 'inherit' !== $settings['verticalAlignmentTablet'] ) {
158 $tablet_css->add_property( 'align-items', $settings['verticalAlignmentTablet'] );
159 }
160
161 if ( 'inherit' !== $settings['horizontalAlignmentTablet'] ) {
162 $tablet_css->add_property( 'justify-content', $settings['horizontalAlignmentTablet'] );
163 }
164
165 if ( $settings['horizontalGapTablet'] ) {
166 $tablet_css->add_property( 'margin-' . $gap_direction, '-' . $settings['horizontalGapTablet'] . 'px' );
167 } elseif ( 0 === $settings['horizontalGapTablet'] ) {
168 $tablet_css->add_property( 'margin-' . $gap_direction, $settings['horizontalGapTablet'] );
169 }
170
171 $tablet_css->set_selector( '.gb-grid-wrapper-' . $id . ' > .gb-grid-column' );
172 $tablet_css->add_property( 'padding-' . $gap_direction, $settings['horizontalGapTablet'], 'px' );
173
174 if ( $blockVersion < 3 || $settings['useLegacyRowGap'] ) {
175 $tablet_css->add_property( 'padding-bottom', $settings['verticalGapTablet'], 'px' );
176 }
177
178 $mobile_css->set_selector( '.gb-grid-wrapper-' . $id );
179
180 if ( $blockVersion > 2 && ! $settings['useLegacyRowGap'] ) {
181 $mobile_css->add_property( 'row-gap', $settings['verticalGapMobile'], 'px' );
182 }
183
184 if ( 'inherit' !== $settings['verticalAlignmentMobile'] ) {
185 $mobile_css->add_property( 'align-items', $settings['verticalAlignmentMobile'] );
186 }
187
188 if ( 'inherit' !== $settings['horizontalAlignmentMobile'] ) {
189 $mobile_css->add_property( 'justify-content', $settings['horizontalAlignmentMobile'] );
190 }
191
192 if ( $settings['horizontalGapMobile'] ) {
193 $mobile_css->add_property( 'margin-' . $gap_direction, '-' . $settings['horizontalGapMobile'] . 'px' );
194 } elseif ( 0 === $settings['horizontalGapMobile'] ) {
195 $mobile_css->add_property( 'margin-' . $gap_direction, $settings['horizontalGapMobile'] );
196 }
197
198 $mobile_css->set_selector( '.gb-grid-wrapper-' . $id . ' > .gb-grid-column' );
199 $mobile_css->add_property( 'padding-' . $gap_direction, $settings['horizontalGapMobile'], 'px' );
200
201 if ( $blockVersion < 3 || $settings['useLegacyRowGap'] ) {
202 $mobile_css->add_property( 'padding-bottom', $settings['verticalGapMobile'], 'px' );
203 }
204
205 // Store this block ID in memory.
206 self::store_block_id( $id );
207
208 /**
209 * Do generateblocks_block_css_data hook
210 *
211 * @since 1.0
212 *
213 * @param string $name The name of our block.
214 * @param array $settings The settings for the current block.
215 * @param object $css Our desktop/main CSS data.
216 * @param object $desktop_css Our desktop only CSS data.
217 * @param object $tablet_css Our tablet CSS data.
218 * @param object $tablet_only_css Our tablet only CSS data.
219 * @param object $mobile_css Our mobile CSS data.
220 */
221 do_action(
222 'generateblocks_block_css_data',
223 'grid',
224 $settings,
225 $css,
226 $desktop_css,
227 $tablet_css,
228 $tablet_only_css,
229 $mobile_css
230 );
231
232 return [
233 'main' => $css->css_output(),
234 'desktop' => $desktop_css->css_output(),
235 'tablet' => $tablet_css->css_output(),
236 'tablet_only' => $tablet_only_css->css_output(),
237 'mobile' => $mobile_css->css_output(),
238 ];
239 }
240
241 /**
242 * Wrapper function for our dynamic buttons.
243 *
244 * @since 1.6.0
245 * @param array $attributes The block attributes.
246 * @param string $content The dynamic text to display.
247 * @param WP_Block $block Block instance.
248 */
249 public static function render_block( $attributes, $content, $block ) {
250 if ( ! isset( $attributes['isDynamic'] ) || ! $attributes['isDynamic'] ) {
251 // Add styles to this block if needed.
252 $content = generateblocks_maybe_add_block_css(
253 $content,
254 [
255 'class_name' => 'GenerateBlocks_Block_Grid',
256 'attributes' => $attributes,
257 'block_ids' => self::$block_ids,
258 ]
259 );
260
261 return $content;
262 }
263
264 $defaults = generateblocks_get_block_defaults();
265
266 $settings = wp_parse_args(
267 $attributes,
268 $defaults['gridContainer']
269 );
270
271 $classNames = array(
272 'gb-grid-wrapper',
273 'gb-grid-wrapper-' . $settings['uniqueId'],
274 );
275
276 if ( ! empty( $settings['className'] ) ) {
277 $classNames[] = $settings['className'];
278 }
279
280 // Add styles to this block if needed.
281 $output = generateblocks_maybe_add_block_css(
282 '',
283 [
284 'class_name' => 'GenerateBlocks_Block_Grid',
285 'attributes' => $attributes,
286 'block_ids' => self::$block_ids,
287 ]
288 );
289
290 $output .= sprintf(
291 '<div %s>',
292 generateblocks_attr(
293 'grid-wrapper',
294 array(
295 'id' => isset( $settings['anchor'] ) ? $settings['anchor'] : null,
296 'class' => implode( ' ', $classNames ),
297 ),
298 $settings,
299 $block
300 )
301 );
302
303 if ( empty( $attributes['isQueryLoop'] ) ) {
304 $output .= $content;
305 } else {
306 $output .= GenerateBlocks_Block_Query_Loop::render_block( $attributes, $content, $block );
307 }
308
309 $output .= '</div>';
310
311 return $output;
312 }
313 }
314