This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block-library.php
237 lines (206 loc) · 7.39 KB
/
block-library.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Plugin Name: Block Library
* Description: A powerful tool for WordPress developers that enhances the Gutenberg editor by enabling the efficient creation and management of custom blocks and patterns.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 1.0.0
* Author: Roberto Di Marco
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: block-library
*
* @package block-library
*/
add_filter('block_categories_all', 'new_block_category');
add_action('admin_menu', 'create_block_block_library_options_page');
add_action('admin_init', 'create_block_block_library_settings_init');
add_action('init', 'block_library_init');
add_action('admin_enqueue_scripts', 'block_library_assets_init');
function block_library_init()
{
register_custom_block_pattern_category();
register_custom_block_patterns();
create_block_block_library_block_init();
create_block_block_library_pattern_init();
}
function block_library_assets_init($hook)
{
block_library_add_admin_scripts($hook);
block_library_add_admin_styles($hook);
}
function new_block_category($cats)
{
$new = array(
'slug' => 'block-library',
'title' => 'block Library',
);
array_unshift($cats, $new);
return $cats;
}
function create_block_block_library_options_page()
{
add_options_page(
'Block Library Options',
'Block Library',
'manage_options',
'block-library',
'create_block_block_library_options_page_html'
);
}
function create_block_block_library_options_page_html()
{
if (!current_user_can('manage_options')) {
return;
}
?>
<div class="wrap">
<h1><?= esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<p>
<label>
<input type="checkbox" id="select-all">
<?php _e('Select All / Deselect All', 'block-library'); ?>
</label>
</p>
<?php
settings_fields('block_library_options');
do_settings_sections('block_library_options');
submit_button('Save Settings');
?>
</form>
</div>
<?php
}
function create_block_block_library_settings_init()
{
register_setting('block_library_options', 'block_library_block_settings');
register_setting('block_library_options', 'block_library_pattern_settings');
$blocks = glob(plugin_dir_path(__FILE__) . 'blocks/*', GLOB_ONLYDIR);
add_settings_section(
'block_library_block_settings_section',
'Block Settings',
null,
'block_library_options'
);
foreach ($blocks as $block) {
$block_name = basename($block);
add_settings_field(
$block_name,
$block_name,
'create_block_block_library_settings_field_cb',
'block_library_options',
'block_library_block_settings_section',
[
'label_for' => $block_name,
'class' => 'block_library_row',
'block_library_custom_data' => 'custom',
]
);
}
add_settings_section(
'block_library_pattern_settings_section',
'Pattern Settings',
null,
'block_library_options'
);
$patterns = glob(plugin_dir_path(__FILE__) . 'patterns/*', GLOB_ONLYDIR);
foreach ($patterns as $pattern) {
$pattern_name = basename($pattern);
add_settings_field(
$pattern_name,
$pattern_name,
'create_block_block_library_pattern_settings_field_cb',
'block_library_options',
'block_library_pattern_settings_section',
[
'label_for' => $pattern_name,
'class' => 'block_library_row',
'block_library_custom_data' => 'custom',
]
);
}
}
function create_block_block_library_pattern_init()
{
$patterns = glob(plugin_dir_path(__FILE__) . 'patterns/*', GLOB_ONLYDIR);
$options = get_option('block_library_pattern_settings');
// if options doesn't exist, create the default settings
if ($options === false) {
$options = array_fill_keys(array_map('basename', $patterns), 1);
update_option('block_library_pattern_settings', $options);
}
foreach ($patterns as $pattern) {
$pattern_name = basename($pattern);
if (isset($options[$pattern_name]) && $options[$pattern_name]) {
$pattern_properties = include $pattern . '/index.php';
register_block_pattern($pattern_name, $pattern_properties);
}
}
}
function create_block_block_library_settings_field_cb($args)
{
$options = get_option('block_library_block_settings');
$checked = isset($options[$args['label_for']]) && $options[$args['label_for']];
?>
<input type="checkbox" id="<?= esc_attr($args['label_for']); ?>" name="block_library_block_settings[<?= esc_attr($args['label_for']); ?>]" <?php checked($checked, 1); ?> value="1">
<?php
}
function create_block_block_library_block_init()
{
$blocks = glob(plugin_dir_path(__FILE__) . 'blocks/*', GLOB_ONLYDIR);
$options = get_option('block_library_block_settings');
// if options doesn't exist, create the default settings
if ($options === false) {
$options = array_fill_keys(array_map('basename', $blocks), 1);
update_option('block_library_block_settings', $options);
}
foreach ($blocks as $block) {
$block_name = basename($block);
if (isset($options[$block_name]) && $options[$block_name]) {
register_block_type(plugin_dir_path(__FILE__) . 'blocks/' . $block_name . '/build');
}
}
}
function create_block_block_library_pattern_settings_field_cb($args)
{
$options = get_option('block_library_pattern_settings');
$checked = isset($options[$args['label_for']]) && $options[$args['label_for']];
?>
<input type="checkbox" id="<?= esc_attr($args['label_for']); ?>" name="block_library_pattern_settings[<?= esc_attr($args['label_for']); ?>]" <?php checked($checked, 1); ?> value="1">
<?php
}
function block_library_add_admin_scripts($hook)
{
if ($hook != 'settings_page_block-library') {
return;
}
wp_enqueue_script('block-library-admin', plugins_url('admin.js', __FILE__), array('jquery'), '1.0.0', true);
}
function block_library_add_admin_styles($hook)
{
if (strpos($hook, 'post.php') === false && strpos($hook, 'post-new.php') === false) {
return;
}
wp_enqueue_style('block-library-admin', plugins_url('admin.css', __FILE__));
}
function register_custom_block_patterns()
{
if (function_exists('register_block_pattern')) {
$options = get_option('block_library_pattern_settings', []);
$patterns = glob(plugin_dir_path(__FILE__) . 'patterns/*', GLOB_ONLYDIR);
foreach ($patterns as $pattern) {
$pattern_name = basename($pattern);
if (isset($options[$pattern_name]) && $options[$pattern_name]) {
$pattern_properties = include $pattern . '/index.php';
register_block_pattern($pattern_name, $pattern_properties);
}
}
}
}
function register_custom_block_pattern_category()
{
if (function_exists('register_block_pattern_category')) {
register_block_pattern_category('block-library', array('label' => __('Block Library')));
}
}