-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathContentImporter.php
More file actions
418 lines (347 loc) · 10.2 KB
/
ContentImporter.php
File metadata and controls
418 lines (347 loc) · 10.2 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
namespace lloc\Msls\ContentImport;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use lloc\Msls\ContentImport\Importers\Importer;
use lloc\Msls\ContentImport\Importers\Map;
use lloc\Msls\ContentImport\Importers\WithRequestPostAttributes;
use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsMain;
use lloc\Msls\MslsOptionsPost;
use lloc\Msls\MslsRegistryInstance;
use lloc\Msls\MslsRequest;
/**
* Class ContentImporter
*
* Handles the request for a content import.
*
* @package lloc\Msls\ContentImport
*/
class ContentImporter extends MslsRegistryInstance {
use WithRequestPostAttributes;
const MSLS_BEFORE_IMPORT_ACTION = 'msls_content_import_before_import';
const MSLS_AFTER_IMPORT_ACTION = 'msls_content_import_after_import';
/**
* @var MslsMain
*/
protected MslsMain $main;
/**
* @var ImportLogger|null
*/
protected ?ImportLogger $logger = null;
/**
* @var Relations|null
*/
protected ?Relations $relations = null;
/**
* @var bool Whether the class should handle requests or not.
*/
protected bool $handle = true;
/**
* @var int The ID of the post the class created while handling the request, if any.
*/
protected int $has_created_post = 0;
/**
* ContentImporter constructor.
*
* @param ?MslsMain $main
*/
public function __construct( ?MslsMain $main = null ) {
$this->main = $main ?? MslsMain::create();
}
/**
* @return ?ImportLogger
*/
public function get_logger(): ?ImportLogger {
return $this->logger;
}
/**
* @param ImportLogger $logger
*/
public function set_logger( ImportLogger $logger ): void {
$this->logger = $logger;
}
/**
* @return ?Relations
*/
public function get_relations(): ?Relations {
return $this->relations;
}
/**
* @param Relations $relations
*/
public function set_relations( Relations $relations ): void {
$this->relations = $relations;
}
/**
* Handles an import request happening during a post save or a template redirect.
*
* @param string[] $data
*
* @return string[] The updated, if needed, data array.
*/
public function handle_import( array $data = array() ) {
$sources = $this->parse_sources();
if ( ! $this->pre_flight_check() || false === $sources ) {
return $data;
}
list( $source_blog_id, $source_post_id ) = $sources;
if ( get_current_blog_id() === $source_blog_id ) {
return $data;
}
$source_lang = MslsBlogCollection::get_blog_language( $source_blog_id );
$dest_blog_id = get_current_blog_id();
$dest_lang = MslsBlogCollection::get_blog_language( get_current_blog_id() );
$dest_post_id = $this->get_the_blog_post_ID( $dest_blog_id );
if ( empty( $dest_post_id ) ) {
return $data;
}
switch_to_blog( $source_blog_id );
$source_post = get_post( $source_post_id );
restore_current_blog();
if ( ! $source_post instanceof \WP_Post ) {
return $data;
}
$import_coordinates = new ImportCoordinates();
$import_coordinates->source_blog_id = $source_blog_id;
$import_coordinates->source_post_id = $source_post_id;
$import_coordinates->dest_blog_id = $dest_blog_id;
$import_coordinates->dest_post_id = $dest_post_id;
$import_coordinates->source_post = $source_post;
$import_coordinates->source_lang = $source_lang;
$import_coordinates->dest_lang = $dest_lang;
$import_coordinates->parse_importers_from_request();
$data = $this->import_content( $import_coordinates, $data );
if ( $this->has_created_post ) {
$this->update_inserted_blog_post_data( $dest_blog_id, $dest_post_id, $data );
$this->redirect_to_blog_post( $dest_blog_id, $dest_post_id );
}
return $data;
}
/**
* Whether the importer should run or not.
*
* @return bool
*/
protected function pre_flight_check() {
if ( ! $this->handle ) {
return false;
}
if ( ! $this->main->verify_nonce() ) {
return false;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['msls_import'] ) ) {
return false;
}
return true;
}
/**
* Parses the source blog and post IDs from the $_POST array validating them.
*
* @return array{int, int}|false
*/
public function parse_sources() {
if ( ! MslsRequest::has_var( 'msls_import' ) ) {
return false;
}
$msls_import = MslsRequest::get_var( 'msls_import' );
$import_data = array_filter( explode( '|', trim( $msls_import ) ), 'is_numeric' );
if ( count( $import_data ) !== 2 ) {
return false;
}
return array_map( 'intval', $import_data );
}
/**
* @param int $blog_id
*
* @return int
*/
protected function get_the_blog_post_ID( $blog_id ) {
switch_to_blog( $blog_id );
$id = get_the_ID();
if ( false !== $id && $id > 0 ) {
restore_current_blog();
return $id;
}
$request = MslsRequest::get_request( array( 'post' ) );
if ( ! empty( $request['post'] ) ) {
return (int) $request['post'];
}
$data = array(
'post_type' => $this->read_post_type_from_request( 'post' ),
'post_title' => 'MSLS Content Import Draft - ' . ( new \DateTimeImmutable() )->format( 'Y-m-d H:i:s' ),
);
return $this->insert_blog_post( $blog_id, $data );
}
/**
* @param int $blog_id
* @param array<string, mixed> $data
*
* @return int
*/
protected function insert_blog_post( $blog_id, array $data = array() ) {
if ( empty( $data ) ) {
return 0;
}
switch_to_blog( $blog_id );
$this->handle( false );
if ( isset( $data['ID'] ) ) {
$post_id = wp_update_post( $data );
} else {
$post_id = wp_insert_post( $data );
}
$this->handle( true );
$this->has_created_post = $post_id > 0 ? $post_id : 0;
restore_current_blog();
return $this->has_created_post;
}
/**
* @param bool $handle
*
* @return void
*/
public function handle( $handle ) {
$this->handle = $handle;
// Also, prevent MSLS from saving.
if ( false === $handle ) {
add_action( 'msls_main_save', 'msls_return_void' );
} else {
remove_action( 'msls_main_save', 'msls_return_void' );
}
}
/**
* Imports content according to the provided coordinates.
*
* @param ImportCoordinates $import_coordinates
* @param string[] $post_fields An optional array of post fields; this can be
* left empty if the method is not called as a consequence
* of filtering the `wp_insert_post_data` filter.
*
* @return string[] An array of modified post fields.
*/
public function import_content( ImportCoordinates $import_coordinates, array $post_fields = array() ) {
if ( ! $import_coordinates->validate() ) {
return $post_fields;
}
/**
* Fires before the import runs.
*
* @param ImportCoordinates $import_coordinates
*/
do_action( self::MSLS_BEFORE_IMPORT_ACTION, $import_coordinates );
/**
* Filters the data before the import runs.
*
* @param array $post_fields
* @param ImportCoordinates $import_coordinates
*
* @since TBD
*/
$post_fields = apply_filters( 'msls_content_import_data_before_import', $post_fields, $import_coordinates );
/**
* Filters the importers map before it's populated.
*
* Returning a non `null` value here will override the creation of the importers map completely
* and use the one returned in the filter.
*
* @param ?array $importers
* @param ImportCoordinates $import_coordinates
*/
$importers = apply_filters( 'msls_content_import_importers', null, $import_coordinates );
if ( is_null( $importers ) ) {
$importers = Map::instance()->make( $import_coordinates );
}
if ( is_null( $this->logger ) ) {
$this->logger = new ImportLogger( $import_coordinates );
}
if ( is_null( $this->relations ) ) {
$this->relations = new Relations( $import_coordinates );
}
if ( ! empty( $importers ) ) {
$source_post_id = $import_coordinates->source_post_id;
$dest_lang = $import_coordinates->dest_lang;
$dest_post_id = $import_coordinates->dest_post_id;
$this->relations->should_create( MslsOptionsPost::create( $source_post_id ), $dest_lang, $dest_post_id );
foreach ( $importers as $key => $importer ) {
/** @var Importer $importer */
$post_fields = $importer->import( $post_fields );
$this->logger->merge( $importer->get_logger() );
$this->relations->merge( $importer->get_relations() );
}
$this->relations->create();
$this->logger->save();
}
/**
* Fires after the import ran.
*
* @param ImportCoordinates $import_coordinates
* @param ?ImportLogger $logger
* @param ?Relations $relations
*
* @since TBD
*/
do_action( self::MSLS_AFTER_IMPORT_ACTION, $import_coordinates, $this->logger, $this->relations );
/**
* Filters the data after the import ran.
*
* @param array $post_fields
* @param ImportCoordinates $import_coordinates
* @param ?ImportLogger $logger
* @param ?Relations $relations
*/
return apply_filters(
'msls_content_import_data_after_import',
$post_fields,
$import_coordinates,
$this->logger,
$this->relations
);
}
/**
* @param int $blog_id
* @param int $post_id
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function update_inserted_blog_post_data( $blog_id, $post_id, array $data ) {
$data['ID'] = $post_id;
$data['post_status'] = empty( $data['post_status'] ) || 'auto-draft' === $data['post_status']
? 'draft'
: $data['post_status'];
$this->insert_blog_post( $blog_id, $data );
return $data;
}
/**
* @param int $dest_blog_id
* @param int $post_id
* @return void
*/
protected function redirect_to_blog_post( $dest_blog_id, $post_id ) {
switch_to_blog( $dest_blog_id );
$edit_post_link = html_entity_decode( get_edit_post_link( $post_id ) );
wp_safe_redirect( $edit_post_link );
die();
}
/**
* Filters whether the post should be considered empty or not.
*
* Empty posts would not be saved to database but it's fine if in
* the context of a content import as it will be populated.
*
* @param bool $is_empty
*
* @return bool
*/
public function filter_empty( $is_empty ) {
if ( ! $this->main->verify_nonce() ) {
return $is_empty;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['msls_import'] ) ) {
return $is_empty;
}
return false;
}
}