PluginProbe
Twentig Supercharged Block Editor – Blocks, Patterns, Starter Sites, Portfolio / trunk
Twentig Supercharged Block Editor – Blocks, Patterns, Starter Sites, Portfolio vtrunk
2.0.5 2.0.4 2.0.3 2.0.2 trunk 0.0.1 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.9.1 0.9.2 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 All 88 releases
twentig / inc / compat / blocks.php

blocks.php in Twentig Supercharged Block Editor – Blocks, Patterns, Starter Sites, Portfolio trunk, at inc/compat/blocks.php

297 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block compatibility functions for classic themes and backward compatibility.
4 *
5 * @package twentig
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Filters the columns block output.
12 *
13 * @param string $block_content Rendered block content.
14 * @param array $block Block object.
15 * @return string Filtered block content.
16 */
17 function twentig_filter_compat_columns_block( $block_content, $block ) {
18
19 $attributes = $block['attrs'] ?? array();
20 $gap = $attributes['style']['spacing']['blockGap'] ?? null;
21 $horizontal_gap = is_array( $gap ) ? ( $gap['left'] ?? null ) : $gap;
22
23 if ( twentig_theme_supports_spacing() && $horizontal_gap && str_contains( $horizontal_gap, 'px' ) ) {
24 $gap_value = intval( $horizontal_gap );
25 if ( $gap_value > 32 ) {
26 $tag_processor = new WP_HTML_Tag_Processor( $block_content );
27 $tag_processor->next_tag();
28 $tag_processor->add_class( 'tw-large-gap' );
29 $block_content = $tag_processor->get_updated_html();
30 }
31 }
32
33 return $block_content;
34 }
35 add_filter( 'render_block_core/columns', 'twentig_filter_compat_columns_block', 10, 2 );
36
37 /**
38 * Filters the column block output to add a CSS var to store the width attribute.
39 *
40 * @param string $block_content Rendered block content.
41 * @param array $block Block object.
42 * @return string Filtered block content.
43 */
44 function twentig_filter_compat_column_block( $block_content, $block ) {
45
46 if ( wp_should_load_separate_core_block_assets() ) {
47 return $block_content;
48 }
49
50 if ( isset( $block['attrs']['width'] ) ) {
51 $tag_processor = new WP_HTML_Tag_Processor( $block_content );
52 $tag_processor->next_tag();
53
54 $style_attr = $tag_processor->get_attribute( 'style' );
55 $style = '--col-width:' . esc_attr( $block['attrs']['width'] ) . ';' . $style_attr;
56
57 $tag_processor->set_attribute( 'style', $style );
58 $block_content = $tag_processor->get_updated_html();
59 }
60
61 return $block_content;
62 }
63 add_filter( 'render_block_core/column', 'twentig_filter_compat_column_block', 10, 2 );
64
65 /**
66 * Handles deprecation for our block settings by filtering the block before it's processed.
67 *
68 * @param array $parsed_block The block being rendered.
69 * @return array Modified block data.
70 */
71 function twentig_filter_compat_render_block_data( $parsed_block ) {
72 $block_name = $parsed_block['blockName'];
73 if ( 'core/post-author' === $block_name ) {
74 if ( ! empty( $parsed_block['attrs']['twIsLink'] ) ) {
75 $parsed_block['attrs']['isLink'] = true;
76 }
77 } elseif ( 'core/post-excerpt' === $block_name ) {
78 $attributes = $parsed_block['attrs'];
79 if ( isset( $attributes['twExcerptLength'] ) ) {
80 $parsed_block['attrs']['excerptLength'] = $attributes['twExcerptLength'];
81 }
82 }
83
84 return $parsed_block;
85 }
86 add_filter( 'render_block_data', 'twentig_filter_compat_render_block_data' );
87
88 /**
89 * Filters the query block output.
90 *
91 * @param string $block_content Rendered block content.
92 * @param array $block Block object.
93 * @return string Filtered block content.
94 */
95 function twentig_filter_compat_query_block( $block_content, $block ) {
96
97 $attributes = $block['attrs'] ?? array();
98 $layout = $attributes['displayLayout']['type'] ?? null;
99
100 if ( ! $layout ) {
101 return $block_content;
102 }
103
104 $class_names = array();
105 $style = '';
106
107 if ( isset( $attributes['twBlockGapVertical'] ) ) {
108 $style .= '--tw-gap-y:' . esc_attr( $attributes['twBlockGapVertical'] ) . ';';
109 }
110
111 if ( isset( $attributes['twBlockGapHorizontal'] ) ) {
112 $style .= '--tw-gap-x:' . esc_attr( $attributes['twBlockGapHorizontal'] ) . ';';
113 }
114
115 if ( ! empty( $style ) ) {
116 $class_names[] = 'tw-custom-gap';
117 }
118
119 if ( isset( $attributes['twVerticalAlignment'] ) ) {
120 $class_names[] = 'tw-valign-' . $attributes['twVerticalAlignment'];
121 }
122
123 if ( isset( $attributes['twColumnWidth'] ) && ( 'flex' === $layout || str_contains( $block_content, 'wp-block-post-template-is-layout-grid' ) ) ) {
124 $class_names[] = 'tw-cols-' . $attributes['twColumnWidth'];
125 }
126
127 if ( $style || $class_names ) {
128 $tag_processor = new WP_HTML_Tag_Processor( $block_content );
129 $tag_processor->next_tag( array( 'class_name' => 'wp-block-post-template' ) );
130
131 if ( $style ) {
132 $style_attr = $tag_processor->get_attribute( 'style' );
133 $style .= $style_attr;
134 $tag_processor->set_attribute( 'style', $style );
135 }
136
137 if ( $class_names ) {
138 foreach ( $class_names as $class_name ) {
139 $tag_processor->add_class( sanitize_html_class( $class_name ) );
140 }
141 }
142
143 $block_content = $tag_processor->get_updated_html();
144 }
145
146 return $block_content;
147 }
148 add_filter( 'render_block_core/query', 'twentig_filter_compat_query_block', 10, 2 );
149
150 /**
151 * Filters the gallery block output.
152 *
153 * @param string $block_content Rendered block content.
154 * @param array $block Block object.
155 * @return string Filtered block content.
156 */
157 function twentig_filter_compat_gallery_block( $block_content, $block ) {
158
159 if ( twentig_theme_supports_spacing() ) {
160 $gap = $block['attrs']['style']['spacing']['blockGap'] ?? null;
161 $gap = is_array( $gap ) && isset( $gap['left'] ) ? $gap['left'] : null;
162 $gap = $gap ? $gap : '16px';
163
164 if ( $gap && str_contains( $gap, 'px' ) ) {
165 $tag_processor = new WP_HTML_Tag_Processor( $block_content );
166 $tag_processor->next_tag();
167
168 $gap_value = intval( $gap );
169
170 if ( $gap_value > 32 ) {
171 $tag_processor->add_class( 'tw-large-gap' );
172 } elseif ( $gap_value > 16 ) {
173 $tag_processor->add_class( 'tw-medium-gap' );
174 }
175
176 $block_content = $tag_processor->get_updated_html();
177 }
178 }
179
180 $block_content = str_replace( 'tw-stack-sm', 'tw-cols-large', $block_content );
181
182 return $block_content;
183 }
184 add_filter( 'render_block_core/gallery', 'twentig_filter_compat_gallery_block', 0, 2 );
185
186
187 /**
188 * Gets spacing sizes.
189 *
190 * @return array Array of spacing size presets.
191 */
192 function twentig_get_spacing_sizes() {
193
194 $sizes = array(
195 array(
196 'slug' => '0',
197 'name' => '0px',
198 'size' => '0px',
199 ),
200 array(
201 'slug' => '1',
202 'name' => '5px',
203 'size' => '5px',
204 ),
205 array(
206 'slug' => '2',
207 'name' => '10px',
208 'size' => '10px',
209 ),
210 array(
211 'slug' => '3',
212 'name' => '15px',
213 'size' => '15px',
214 ),
215 array(
216 'slug' => '4',
217 'name' => '20px',
218 'size' => '20px',
219 ),
220 array(
221 'slug' => '5',
222 'name' => '30px',
223 'size' => '30px',
224 ),
225 array(
226 'slug' => '6',
227 'name' => '40px',
228 'size' => '40px',
229 ),
230 array(
231 'slug' => '7',
232 'name' => '50px',
233 'size' => '50px',
234 ),
235 array(
236 'slug' => '8',
237 'name' => '60px',
238 'size' => '60px',
239 ),
240 array(
241 'slug' => '9',
242 'name' => '80px',
243 'size' => '80px',
244 ),
245 array(
246 'slug' => '10',
247 'name' => '100px',
248 'size' => '100px',
249 ),
250 array(
251 'slug' => 'auto',
252 'name' => 'auto',
253 'size' => 'auto',
254 ),
255 );
256 return $sizes;
257 }
258
259 /**
260 * Adds margin classes to the global styles.
261 */
262 function twentig_enqueue_compat_class_styles() {
263
264 $spacing_sizes = twentig_get_spacing_sizes();
265 $css_spacing = '';
266
267 if ( empty( $spacing_sizes ) ) {
268 return;
269 }
270
271 foreach ( $spacing_sizes as $preset ) {
272 $css_spacing .= '.tw-mt-' . esc_attr( $preset['slug'] ) . '{margin-top:' . esc_attr( $preset['size'] ) . '!important;}';
273 $css_spacing .= '.tw-mb-' . esc_attr( $preset['slug'] ) . '{margin-bottom:' . esc_attr( $preset['size'] ) . '!important;}';
274 }
275 wp_add_inline_style( 'twentig-blocks', $css_spacing );
276 }
277 add_action( 'wp_enqueue_scripts', 'twentig_enqueue_compat_class_styles' );
278
279 /**
280 * Enqueue spacing styles inside the editor.
281 */
282 function twentig_compat_block_editor_spacing_styles() {
283 $spacing_sizes = twentig_get_spacing_sizes();
284 if ( empty( $spacing_sizes ) ) {
285 return;
286 }
287
288 $css_spacing = '';
289 foreach ( $spacing_sizes as $preset ) {
290 $css_spacing .= '.tw-mt-' . esc_attr( $preset['slug'] ) . '{margin-top:' . esc_attr( $preset['size'] ) . '!important;}';
291 $css_spacing .= '.tw-mb-' . esc_attr( $preset['slug'] ) . '{margin-bottom:' . esc_attr( $preset['size'] ) . '!important;}';
292 }
293
294 wp_add_inline_style( 'wp-block-library', $css_spacing );
295 }
296 add_action( 'admin_init', 'twentig_compat_block_editor_spacing_styles' );
297