-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaita-columns-block.php
218 lines (195 loc) · 5.85 KB
/
laita-columns-block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
Plugin Name: Laita Columns block
Version: 1.0.0
Author: Daniel Mejta
Author URI: https://www.mejta.net
Text Domain: laita-columns
Domain Path: /languages
*/
namespace Laita;
/**
* Helper: Zjednodušuje volání funkcí v namespace
*
* @param string $callable
* @return string
*/
function ns(string $callable)
{
return __NAMESPACE__ . '\\' . $callable;
}
/**
* Registrace custom bloků
*/
add_action('init', ns('register_column_block'));
add_action('init', ns('register_columns_block'));
/**
* Render pro core bloky a nahrazení render funkce za vlastní. Pokud
* chceš použít kontext, musíš využít toto místo render_block filteru.
*/
add_filter('register_block_type_args', ns('register_render_with_context'), 10, 2);
/**
* Registruje laita/columns block
*/
function register_columns_block()
{
// Zaregistrujeme styl pro frontend
wp_register_style(
'laita-columns-frontend',
plugin_dir_url(__FILE__) . 'assets/columns-frontend.css'
);
// Zaregistrujeme styl pro block editor
wp_register_style(
'laita-columns-block-style',
plugin_dir_url(__FILE__) . 'assets/columns-block.css'
);
// Zaregistrujeme script do block editoru
wp_register_script(
'laita-columns-block-script',
plugin_dir_url(__FILE__) . 'assets/columns-block.js',
['react', 'wp-blocks', 'wp-block-editor', 'wp-i18n']
);
// Nastavíme překlady pro blok
wp_set_script_translations('laita-columns-block-script', 'laita-columns', plugin_dir_path(__FILE__) . 'languages');
// Zaregistrujeme vlastní block type
register_block_type('laita/columns', [
'editor_script' => 'laita-columns-block-script',
'editor_style' => 'laita-columns-block-style',
'style' => 'laita-columns-frontend',
'attributes' => [
'columns' => ['type' => 'number'],
'align' => ['type' => 'string'],
],
'provides_context' => [
'laita/columns' => 'columns', // tady kontext poskytuješ pro podřízené bloky
],
]);
}
function register_column_block()
{
// Zaregistrujeme styl pro block editor
wp_register_style(
'laita-column-block-style',
plugin_dir_url(__FILE__) . 'assets/column-block.css'
);
// Zaregistrujeme script do block editoru
wp_register_script(
'laita-column-block-script',
plugin_dir_url(__FILE__) . 'assets/column-block.js',
['react', 'wp-blocks', 'wp-block-editor', 'wp-i18n']
);
// Nastavíme překlady pro blok
wp_set_script_translations('laita-column-block-script', 'laita-columns', plugin_dir_path(__FILE__) . 'languages');
// Zaregistrujeme vlastní block type
register_block_type('laita/column', [
'editor_script' => 'laita-column-block-script',
'editor_style' => 'laita-column-block-style',
'attributes' => [
'column' => ['type' => 'number'],
],
'provides_context' => [
'laita/column' => 'column', // tady kontext poskytuješ pro podřízené bloky
],
]);
}
/**
* Zaregistruje u libovolného bloku kontext, který má odebírat
* a vlastní render funkce, které můžou kontext využívat.
*
* @param $args
* @param $block_name
* @return mixed
*/
function register_render_with_context($args, $block_name)
{
// všechny bloky budou využívat context
if (!isset($args['uses_context'])) {
$args['uses_context'] = [];
}
$args['uses_context'] = array_merge($args['uses_context'], [
'laita/columns',
'laita/column',
]);
// registrujeme vlastní render funkce
$custom_renders = [
'laita/columns' => ns('render_columns'),
'laita/column' => ns('render_column'),
'core/image' => ns('render_image'),
'core/gallery' => ns('render_gallery'),
];
if (isset($custom_renders[$block_name])) {
$args['render_callback'] = $custom_renders[$block_name];
}
return $args;
}
/**
* Vypisuje wrapper pro sloupce
*
* @param array $attributes
* @param null $inner_blocks
* @return false|string
*/
function render_columns($attributes = [], $inner_blocks = null)
{
ob_start();
?>
<div class="<?php echo join(' ', ['laita-columns', 'laita-columns--' . $attributes['columns'], $attributes['className'] ?? null]); ?>">
<?php echo $inner_blocks; ?>
</div>
<?php
return ob_get_clean();
}
/**
* Vypisuje jednotlivý sloupec
*
* @param array $attributes
* @param null $inner_blocks
* @param null $block
* @return false|string
*/
function render_column($attributes = [], $inner_blocks = null, $block = null)
{
ob_start();
?>
<div class="<?php echo join(' ', ['laita-column', 'laita-column--' . $attributes['column'], 'laita-column--in-' . $block->context['laita/columns']]) ?>">
<?php echo $inner_blocks; ?>
</div>
<?php
return ob_get_clean();
}
/**
* Vlastní render obrázku
*
* @param string $block_content
* @param array $block
* @return string
*/
function render_image($attributes = [], $content = null, $block = null)
{
ob_start();
?>
<figure class="<?php echo join(' ', [
$attributes['className'] ?? null,
'image-in-columns--' . $block->context['laita/columns'], // tady už můžeš kontext používat
'image--column-' . $block->context['laita/column'],
]); ?>">
<?php echo wp_get_attachment_image($attributes['id'], $attributes['sizeSlug'], false, [
'class' => $attributes['className'] ?? null,
'alt' => $attributes['alt'],
]); ?>
</figure>
<?php
return ob_get_clean();
}
/**
* Vlastní render galerie
*
* @param array $attributes
* @param null $content
* @param null $block
* @return mixed|null
*/
function render_gallery($attributes = [], $content = null, $block = null)
{
return $content;
}