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