-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin-interface.php
executable file
·264 lines (238 loc) · 7.22 KB
/
admin-interface.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
add_action( 'admin_menu', function () {
add_menu_page(
__( 'Bulk Update Blocks', 'my-textdomain' ),
__( 'Bulk Update Blocks', 'my-textdomain' ),
'manage_options',
'bulk-update-blocks',
'bub_page_contents',
'dashicons-schedule',
90
);
} );
function bub_page_contents() {
global $wp;
$page_id = isset($_REQUEST['sub']) ? intval($_REQUEST['sub']) : 0;
$forms = [
[
'id' => 'bub_insert_blocks',
'title' => 'Insert blocks',
'fields' => [
[
'id' => 'bub-blocks',
'label' => 'Blocks to insert',
'description' => 'Paste the raw blocks code, including the <code><!-- wp:block --></code> tags',
'type' => 'textarea',
],
[
'id' => 'bub-posts',
'label' => 'Posts to include in search<br>(optional)',
'description' => 'Comma separated list of post IDs and/or post types (use the exact slug)',
'type' => 'text',
],
[
'id' => 'bub-position',
'label' => 'Position',
'description' => 'Where to insert the block. `1` means insert as first block. `-1` means insert as last block. `3` means insert as 3th block.',
'type' => 'number',
],
],
'submit' => 'Insert Blocks!',
],
[
'id' => 'bub_replace_block',
'title' => 'Replace block',
'fields' => [
[
'id' => 'bub-block',
'label' => 'Block to find',
'description' => 'Template of block to replace. For example <code>[ "core/paragraph" , {"dropCap":true} ]</code>. Needs to be valid JSON.<br>Will replace all occurences of this block.',
'type' => 'text',
],
[
'id' => 'bub-blocks-to-replace',
'label' => 'Replace with',
'description' => 'Paste the raw blocks code, including the <code><!-- wp:xyz/block --></code> tags',
'type' => 'textarea',
],
[
'id' => 'bub-replacement-function',
'label' => 'Replacement-function',
'description' => 'You can create additional replacement functions.',
'type' => 'select',
'options' => bub_get_replacement_functions(),
],
[
'id' => 'bub-posts',
'label' => 'Posts to include in search<br>(optional)',
'description' => 'Comma separated list of post IDs and/or post types (use the exact slug)',
'type' => 'text',
],
],
'submit' => 'Replace Blocks!',
],
[
'id' => 'bub_delete_block',
'title' => 'Delete block',
'fields' => [
[
'id' => 'bub-block',
'label' => 'Block to find',
'description' => 'Template of block to delete. For example <code>[ "core/paragraph" , {"dropCap":true} ]</code>. Needs to be valid JSON.<br>Will delete all occurences of this block.',
'type' => 'text',
],
[
'id' => 'bub-posts',
'label' => 'Posts to include in search<br>(optional)',
'description' => 'Comma separated list of post IDs and/or post types (use the exact slug)',
'type' => 'text',
],
],
'submit' => 'Delete Blocks!',
],
[
'id' => 'bub_find_posts_containing_block',
'title' => 'Find posts containing block',
'fields' => [
[
'id' => 'bub-block',
'label' => 'Block to look for',
'description' => 'Template of block to look for. For example <code>[ "core/paragraph" , {"dropCap":true} ]</code>. Needs to be valid JSON.',
'type' => 'text',
],
[
'id' => 'bub-posts',
'label' => 'Posts to include in search<br>(optional)',
'description' => 'Comma separated list of post IDs and/or post types (use the exact slug)',
'type' => 'text',
],
[
'id' => 'bub-depth',
'label' => 'Depth',
'description' => "how many levels deep should we dive? -1 means inifinte (default). 0 means don't check innerBlocks.",
'type' => 'number',
],
],
'submit' => 'Find Posts!',
],
];
?>
<style>
textarea, input[type=text] {width: 100%;}
</style>
<h1><?php esc_html_e( 'Bulk Update Blocks', 'bulk-update-blocks' ); ?></h1>
<p>
<strong>MAKE A BACKUP FIRST!</strong><br>
This tool is in beta! It's quickly put together, and made it public in the hope to be useful at this very early stage.<br>
It has no validation and <strong>IT WILL BREAK YOUR WEBSITE</strong> if you don't know what you are doing.
</p>
<nav>
<?php
echo '| ';
foreach($forms as $key => $form) {
$url = site_url().'/wp-admin/admin.php?page=bulk-update-blocks&sub='.$key;
echo '<a href="'.$url.'">'.$form['title'].'</a> | ';
}
?>
</nav>
<?php
bub_render_admin_form($forms[$page_id]);
?>
<script>
(function($){
$('form.bulk-update-form').eq(0).submit(function(e){
const formData = new FormData(this);
const data = {};
for (const [key, value] of formData.entries()) {
data[key] = value;
}
$('#bub-result').html('Busy. please wait...');
$.post(ajaxurl, data, function(response){
$('#bub-result').html(JSON.stringify(response, null, 2));
},'json');
e.preventDefault();
return false;
});
})(jQuery);
</script>
<?php
}
function bub_render_admin_form($form) {
$fields = $form['fields'];
?>
<h2><?php echo $form['title']; ?></h2>
<form id="<?php echo $form['id'] ?>" class="bulk-update-form" method="POST">
<input type="hidden" name="action" value="<?php echo $form['id'] ?>" />
<table class="form-table" role="presentation">
<tbody>
<?php array_walk($fields, 'bub_render_field'); ?>
</tbody>
</table>
<pre id="bub-result"></pre>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php echo $form['submit'] ?>"></p>
</form>
<?php
}
function bub_render_field($field) {
?>
<tr>
<th scope="row"><label for="<?php echo $field['id'] ?>"><?php echo $field['label'] ?></label></th>
<td>
<?php bub_render_input($field) ?>
<p class="description" id="tagline-description"><?php echo $field['description'] ?></p>
</td>
</tr>
<?php
}
function bub_render_input($field) {
$id = $field['id'];
$type = $field['type'];
if ($type == 'select') {
$options = $field['options'];
?>
<select <?php echo "name=\"{$id}\" id=\"{$id}\"" ?>>
<?php foreach ($options as $opt) { ?>
<option value="<?php echo $opt[1] ?>"><?php echo $opt[0] ?></option>
<?php } ?>
</select>
<?php
} else if ($type == 'textarea') {
echo "<textarea name=\"{$id}\" id=\"{$id}\"></textarea>";
} else {
echo "<input type=\"{$type}\" name=\"{$id}\" id=\"{$id}\" />";
}
}
function bub_get_post_IDs($posts_str) {
if ($posts_str == '') {
// get all posts (all custom post types + post + page. Don't include any other builtin post types)
$args = [
'post_type' => array_values(array_merge(get_post_types(['_builtin' => false]), [ 'post', 'page' ])),
'post_status' => 'publish',
'numberposts' => -1,
'fields' => 'ids',
];
$post_ids = get_posts($args);
return $post_ids;
}
$values = explode(',',$posts_str);
$post_ids = [];
foreach($values as $val) {
$val = trim($val);
if (is_numeric($val)) {
$post_ids[] = intval($val);
continue;
}
$posts = get_posts([
'post_type' => $val,
'post_status' => 'publish',
'numberposts' => -1
]);
foreach($posts as $post) {
$post_ids[] = $post->ID;
}
}
return $post_ids;
}
function bub_get_replacement_functions() {
return apply_filters('bub_replacement_functions', [ ['simple', 'bub_simple_replace'] ]);
}