class-import.php
9.06 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* Block Lab Importer.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab\Admin;
use Block_Lab\Component_Abstract;
/**
* Class Import
*/
class Import extends Component_Abstract {
/**
* Importer slug.
*
* @var string
*/
public $slug = 'block-lab';
/**
* Register any hooks that this component needs.
*/
public function register_hooks() {
add_action( 'admin_init', [ $this, 'register_importer' ] );
}
/**
* Register the importer for the Tools > Import admin screen
*/
public function register_importer() {
register_importer(
$this->slug,
__( 'Block Lab', 'block-lab' ),
__( 'Import custom blocks created with Block Lab.', 'block-lab' ),
[ $this, 'render_page' ]
);
}
/**
* Render the import page. Manages the three separate stages of the JSON import process.
*/
public function render_page() {
$step = filter_input( INPUT_GET, 'step', FILTER_SANITIZE_NUMBER_INT );
ob_start();
$this->render_page_header();
switch ( $step ) {
case 0:
default:
$this->render_welcome();
break;
case 1:
check_admin_referer( 'import-upload' );
$upload_dir = wp_get_upload_dir();
if ( ! isset( $upload_dir['basedir'] ) ) {
$this->render_import_error(
__( 'Sorry, there was an error uploading the file.', 'block-lab' ),
__( 'Upload base directory not set.', 'block-lab' )
);
}
$cache_dir = $upload_dir['basedir'] . '/block-lab';
$file = wp_import_handle_upload();
if ( $this->validate_upload( $file ) ) {
if ( ! file_exists( $cache_dir ) ) {
mkdir( $cache_dir, 0777, true );
}
// This is on the local filesystem, so file_get_contents() is ok to use here.
file_put_contents( $cache_dir . '/import.json', file_get_contents( $file['file'] ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions
$json = file_get_contents( $file['file'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions
$blocks = json_decode( $json, true );
$this->render_choose_blocks( $blocks );
}
break;
case 2:
$cache_dir = wp_get_upload_dir()['basedir'] . '/block-lab';
$file = [ 'file' => $cache_dir . '/import.json' ];
if ( $this->validate_upload( $file ) ) {
// This is on the local filesystem, so file_get_contents() is ok to use here.
$json = file_get_contents( $file['file'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions
$blocks = json_decode( $json, true );
$import_blocks = [];
foreach ( $blocks as $block_namespace => $block ) {
if ( 'on' === filter_input( INPUT_GET, $block_namespace, FILTER_SANITIZE_STRING ) ) {
$import_blocks[ $block_namespace ] = $block;
}
}
$this->import_blocks( $import_blocks );
}
break;
}
$html = ob_get_clean();
echo '<div class="wrap block-lab-import">' . $html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Render the Import page header.
*/
public function render_page_header() {
?>
<h2><?php esc_html_e( 'Import Block Lab Content Blocks', 'block-lab' ); ?></h2>
<?php
}
/**
* Render the welcome message.
*/
public function render_welcome() {
?>
<p><?php esc_html_e( 'Welcome! This importer processes Block Lab JSON files, adding custom blocks to this site.', 'block-lab' ); ?></p>
<p><?php esc_html_e( 'Choose a JSON (.json) file to upload, then click Upload file and import.', 'block-lab' ); ?></p>
<p>
<?php
echo wp_kses(
sprintf(
/* translators: %1$s: an opening anchor tag, %2$s: a closing anchor tag */
__( 'This JSON file should come from the export link or bulk action in the %1$sContent Blocks screen%2$s, not from the main Export tool.', 'block-lab' ),
sprintf(
'<a href="%1$s">',
esc_url(
admin_url(
add_query_arg(
[ 'post_type' => block_lab()->get_post_type_slug() ],
'edit.php'
)
)
)
),
'</a>'
),
[ 'a' => [ 'href' => [] ] ]
);
?>
</p>
<?php
wp_import_upload_form(
add_query_arg(
[
'import' => $this->slug,
'step' => 1,
]
)
);
}
/**
* Render the currently importing block title.
*
* @param string $title The title of the block.
*/
public function render_import_success( $title ) {
echo wp_kses_post(
sprintf(
'<p>%s</p>',
sprintf(
// translators: placeholder refers to title of custom block.
__( 'Successfully imported %1$s.', 'block-lab' ),
'<strong>' . esc_html( $title ) . '</strong>'
)
)
);
}
/**
* Render the currently importing block title.
*
* @param string $title The title of the block.
* @param string $error The error being reported.
*/
public function render_import_error( $title, $error ) {
echo wp_kses_post(
sprintf( '<p><strong>%s</strong></p><p>%s</p>', $title, $error )
);
}
/**
* Render the successful import message.
*/
public function render_done() {
?>
<p><?php esc_html_e( 'All done!', 'block-lab' ); ?></p>
<?php
}
/**
* Render the interface for choosing blocks to update.
*
* @param array $blocks An array of block names to choose from.
*/
public function render_choose_blocks( $blocks ) {
?>
<p><?php esc_html_e( 'Please select the blocks to import:', 'block-lab' ); ?></p>
<form>
<?php
foreach ( $blocks as $block_namespace => $block ) {
$action = __( 'Import', 'block-lab' );
if ( $this->block_exists( $block_namespace ) ) {
$action = __( 'Replace', 'block-lab' );
}
?>
<p>
<input type="checkbox" name="<?php echo esc_attr( $block_namespace ); ?>" id="<?php echo esc_attr( $block_namespace ); ?>" checked>
<label for="<?php echo esc_attr( $block_namespace ); ?>">
<?php echo esc_html( $action ); ?> <strong><?php echo esc_attr( $block['title'] ); ?></strong>
</label>
</p>
<?php
}
wp_nonce_field();
?>
<input type="hidden" name="import" value="block-lab">
<input type="hidden" name="step" value="2">
<p class="submit"><input type="submit" value="<?php esc_attr_e( 'Import Selected', 'block-lab' ); ?>" class="button button-primary"></p>
</form>
<?php
}
/**
* Handles the JSON upload and initial parsing of the file.
*
* @param array $file The file.
* @return bool False if error uploading or invalid file, true otherwise.
*/
public function validate_upload( $file ) {
if ( isset( $file['error'] ) ) {
$this->render_import_error(
__( 'Sorry, there was an error uploading the file.', 'block-lab' ),
$file['error']
);
return false;
} elseif ( ! file_exists( $file['file'] ) ) {
$this->render_import_error(
__( 'Sorry, there was an error uploading the file.', 'block-lab' ),
sprintf(
// translators: placeholder refers to a file directory.
__( 'The export file could not be found at %1$s. It is likely that this was caused by a permissions problem.', 'block-lab' ),
'<code>' . esc_html( $file['file'] ) . '</code>'
)
);
return false;
}
// This is on the local filesystem, so file_get_contents() is ok to use here.
$json = file_get_contents( $file['file'] ); // @codingStandardsIgnoreLine
$data = json_decode( $json, true );
if ( ! is_array( $data ) ) {
$this->render_import_error(
__( 'Sorry, there was an error processing the file.', 'block-lab' ),
__( 'Invalid JSON.', 'block-lab' )
);
return false;
}
return true;
}
/**
* Import data into new Block Lab posts.
*
* @param array $blocks An array of Block Lab content blocks.
*/
public function import_blocks( $blocks ) {
foreach ( $blocks as $block_namespace => $block ) {
if ( ! isset( $block['title'] ) || ! isset( $block['name'] ) ) {
continue;
}
$post_id = false;
if ( $this->block_exists( $block_namespace ) ) {
$post = get_page_by_path( $block['name'], OBJECT, block_lab()->get_post_type_slug() );
if ( $post ) {
$post_id = $post->ID;
}
}
$json = wp_json_encode( [ $block_namespace => $block ], JSON_UNESCAPED_UNICODE );
$post_data = [
'post_title' => $block['title'],
'post_name' => $block['name'],
'post_content' => wp_slash( $json ),
'post_status' => 'publish',
'post_type' => block_lab()->get_post_type_slug(),
];
if ( $post_id ) {
$post_data['ID'] = $post_id;
}
$post = wp_insert_post( $post_data );
if ( is_wp_error( $post ) ) {
$this->render_import_error(
sprintf(
// translators: placeholder refers to title of custom block.
__( 'Error importing %s.', 'block-lab' ),
$block['title']
),
$post->get_error_message()
);
} else {
$this->render_import_success( $block['title'] );
}
}
$this->render_done();
}
/**
* Check if block already exists.
*
* @param string $block_namespace The JSON key for the block. e.g. block-lab/foo.
*
* @return bool
*/
private function block_exists( $block_namespace ) {
$registered_blocks = get_dynamic_block_names();
if ( in_array( $block_namespace, $registered_blocks, true ) ) {
return true;
}
return false;
}
}