Skip to content

Commit 5d2ece4

Browse files
committed
Removed all changes of #2105 and #2104
1 parent 34b65b4 commit 5d2ece4

File tree

7 files changed

+124
-148
lines changed

7 files changed

+124
-148
lines changed

Diff for: packages/playground/data-liberation/bin/import/blueprint-import-wxr.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"pluginPath": "data-liberation/plugin.php"
1212
},
1313
{
14-
"step": "wp-cli",
15-
"command": "wp data-liberation import /wordpress/wp-content/uploads/import-wxr"
14+
"step": "runPHP",
15+
"code": "<?php require_once 'wordpress/wp-load.php';\n$upload_dir = wp_upload_dir();\nforeach ( wp_visit_file_tree( $upload_dir['basedir'] . '/import-wxr' ) as $event ) {\nforeach ( $event->files as $file ) {\nif ( $file->isFile() && pathinfo( $file->getPathname(), PATHINFO_EXTENSION ) === 'xml' ) {\ndata_liberation_import( $file->getPathname() );\n}\n}\n};"
1616
}
1717
]
1818
}

Diff for: packages/playground/data-liberation/bootstrap.php

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
require_once __DIR__ . '/src/import/WP_Stream_Importer.php';
6666
require_once __DIR__ . '/src/import/WP_Entity_Iterator_Chain.php';
6767
require_once __DIR__ . '/src/import/WP_Retry_Frontloading_Iterator.php';
68-
require_once __DIR__ . '/src/import/WP_Logger.php';
6968

7069
require_once __DIR__ . '/src/utf8_decoder.php';
7170

Diff for: packages/playground/data-liberation/plugin.php

+32-21
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,40 @@ function () {
3939
}
4040
);
4141

42-
function data_liberation_init() {
43-
if ( defined( 'WP_CLI' ) && WP_CLI ) {
44-
require_once __DIR__ . '/src/cli/WP_Import_Command.php';
42+
add_action(
43+
'init',
44+
function () {
45+
if ( defined( 'WP_CLI' ) && WP_CLI ) {
46+
/**
47+
* Import a WXR file.
48+
*
49+
* <file>
50+
* : The WXR file to import.
51+
*/
52+
$command = function ( $args, $assoc_args ) {
53+
$file = $args[0];
54+
data_liberation_import( $file );
55+
};
56+
57+
// Register the WP-CLI import command.
58+
// Example usage: wp data-liberation /path/to/file.xml
59+
WP_CLI::add_command( 'data-liberation', $command );
60+
}
4561

46-
// Register the WP-CLI import command.
47-
WP_CLI::add_command( 'data-liberation', WP_Import_Command::class );
62+
register_post_status(
63+
'error',
64+
array(
65+
'label' => _x( 'Error', 'post' ), // Label name
66+
'public' => false,
67+
'exclude_from_search' => false,
68+
'show_in_admin_all_list' => false,
69+
'show_in_admin_status_list' => false,
70+
// translators: %s is the number of errors
71+
'label_count' => _n_noop( 'Error <span class="count">(%s)</span>', 'Error <span class="count">(%s)</span>' ),
72+
)
73+
);
4874
}
49-
50-
register_post_status(
51-
'error',
52-
array(
53-
'label' => _x( 'Error', 'post' ), // Label name
54-
'public' => false,
55-
'exclude_from_search' => false,
56-
'show_in_admin_all_list' => false,
57-
'show_in_admin_status_list' => false,
58-
// translators: %s is the number of errors
59-
'label_count' => _n_noop( 'Error <span class="count">(%s)</span>', 'Error <span class="count">(%s)</span>' ),
60-
)
61-
);
62-
}
63-
64-
add_action( 'init', 'data_liberation_init' );
75+
);
6576

6677
function data_liberation_activate() {
6778
// Create tables and option.

Diff for: packages/playground/data-liberation/src/functions.php

+34
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,37 @@ function mb_str_split( $input, $split_length = 1, $encoding = null ) {
255255
return $result;
256256
}
257257
}
258+
259+
/**
260+
* Import a WXR file. Used by the CLI.
261+
*
262+
* @param string $path The path to the WXR file.
263+
* @return void
264+
*/
265+
function data_liberation_import( $path ): bool {
266+
$importer = WP_Stream_Importer::create_for_wxr_file( $path );
267+
268+
if ( ! $importer ) {
269+
return false;
270+
}
271+
272+
$is_wp_cli = defined( 'WP_CLI' ) && WP_CLI;
273+
274+
if ( $is_wp_cli ) {
275+
WP_CLI::line( "Importing from {$path}" );
276+
}
277+
278+
while ( $importer->next_step() ) {
279+
// Output the current stage if running in WP-CLI.
280+
if ( $is_wp_cli ) {
281+
$current_stage = $importer->get_current_stage();
282+
WP_CLI::line( "Import: stage {$current_stage}" );
283+
}
284+
}
285+
286+
if ( $is_wp_cli ) {
287+
WP_CLI::success( 'Import ended' );
288+
}
289+
290+
return true;
291+
}

Diff for: packages/playground/data-liberation/src/import/WP_Entity_Importer.php

+56-72
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct( $options = array() ) {
9595
$this->mapping['term_id'] = array();
9696
$this->requires_remapping = $empty_types;
9797
$this->exists = $empty_types;
98-
$this->logger = isset( $options['logger'] ) ? $options['logger'] : new WP_Logger();
98+
$this->logger = new Logger();
9999

100100
$this->options = wp_parse_args(
101101
$options,
@@ -126,8 +126,6 @@ public function import_entity( WP_Imported_Entity $entity ) {
126126
case WP_Imported_Entity::TYPE_TAG:
127127
case WP_Imported_Entity::TYPE_CATEGORY:
128128
return $this->import_term( $data );
129-
case WP_Imported_Entity::TYPE_TERM_META:
130-
return $this->import_term_meta( $data, $data['term_id'] );
131129
case WP_Imported_Entity::TYPE_USER:
132130
return $this->import_user( $data );
133131
case WP_Imported_Entity::TYPE_SITE_OPTION:
@@ -390,40 +388,6 @@ public function import_term( $data ) {
390388
return $term_id;
391389
}
392390

393-
public function import_term_meta( $meta_item, $term_id ) {
394-
if ( empty( $meta_item ) ) {
395-
return true;
396-
}
397-
398-
/**
399-
* Pre-process term meta data.
400-
*
401-
* @param array $meta_item Meta data. (Return empty to skip.)
402-
* @param int $term_id Term the meta is attached to.
403-
*/
404-
$meta_item = apply_filters( 'wxr_importer_pre_process_term_meta', $meta_item, $term_id );
405-
if ( empty( $meta_item ) ) {
406-
return false;
407-
}
408-
409-
// Have we already processed this?
410-
if ( isset( $element['_already_mapped'] ) ) {
411-
$this->logger->debug( 'Skipping term meta, already processed' );
412-
return;
413-
}
414-
415-
if ( ! isset( $meta_item['term_id'] ) ) {
416-
$meta_item['term_id'] = $term_id;
417-
}
418-
419-
$value = maybe_unserialize( $meta_item['meta_value'] );
420-
$term_meta_id = add_term_meta( $meta_item['term_id'], wp_slash( $meta_item['meta_key'] ), wp_slash_strings_only( $value ) );
421-
422-
do_action( 'wxr_importer_processed_term_meta', $term_meta_id, $meta_item, $meta_item['term_id'] );
423-
424-
return $term_meta_id;
425-
}
426-
427391
/**
428392
* Prefill existing post data.
429393
*
@@ -480,8 +444,6 @@ protected function post_exists( $data ) {
480444
* Note that new/updated terms, comments and meta are imported for the last of the above.
481445
*/
482446
public function import_post( $data ) {
483-
$parent_id = isset( $data['post_parent'] ) ? (int) $data['post_parent'] : 0;
484-
485447
/**
486448
* Pre-process post data.
487449
*
@@ -490,7 +452,7 @@ public function import_post( $data ) {
490452
* @param array $comments Comments on the post.
491453
* @param array $terms Terms on the post.
492454
*/
493-
$data = apply_filters( 'wxr_importer_pre_process_post', $data, $parent_id );
455+
$data = apply_filters( 'wxr_importer_pre_process_post', $data );
494456
if ( empty( $data ) ) {
495457
$this->logger->debug( 'Skipping post, empty data' );
496458
return false;
@@ -659,37 +621,6 @@ public function import_post( $data ) {
659621
}
660622
$this->mark_post_exists( $data, $post_id );
661623

662-
// Add terms to the post
663-
/*if ( ! empty( $data['terms'] ) ) {
664-
$terms_to_set = array();
665-
666-
foreach ( $data['terms'] as $term ) {
667-
// Back compat with WXR 1.0 map 'tag' to 'post_tag'
668-
$taxonomy = ( 'tag' === $term['taxonomy'] ) ? 'post_tag' : $term['taxonomy'];
669-
$term_exists = term_exists( $term['slug'], $taxonomy );
670-
$term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
671-
672-
if ( ! $term_id ) {
673-
// @TODO: Add a unit test with a WXR with one post and X tags without root declated tags.
674-
$new_term = wp_insert_term( $term['slug'], $taxonomy, $term );
675-
676-
if ( ! is_wp_error( $new_term ) ) {
677-
$term_id = $new_term['term_id'];
678-
679-
$this->topological_sorter->update_mapped_id( $new_term, $term_id );
680-
} else {
681-
continue;
682-
}
683-
}
684-
$terms_to_set[ $taxonomy ][] = intval( $term_id );
685-
}
686-
687-
foreach ( $terms_to_set as $tax => $ids ) {
688-
// Add the post terms to the post
689-
wp_set_post_terms( $post_id, $ids, $tax );
690-
}
691-
}*/
692-
693624
$this->logger->info(
694625
sprintf(
695626
/* translators: 1: post title, 2: post type name */
@@ -717,7 +648,6 @@ public function import_post( $data ) {
717648
* @param array $terms Raw term data, already processed.
718649
*/
719650
do_action( 'wxr_importer_processed_post', $post_id, $data );
720-
721651
return $post_id;
722652
}
723653

@@ -1289,3 +1219,57 @@ public static function sort_comments_by_id( $a, $b ) {
12891219
return $a['comment_id'] - $b['comment_id'];
12901220
}
12911221
}
1222+
1223+
/**
1224+
* @TODO how to treat this? Should this class even exist?
1225+
* how does WordPress handle different levels? It
1226+
* seems useful for usage in wp-cli, Blueprints,
1227+
* and other non-web environments.
1228+
*/
1229+
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound
1230+
class Logger {
1231+
/**
1232+
* Log a debug message.
1233+
*
1234+
* @param string $message Message to log
1235+
*/
1236+
public function debug( $message ) {
1237+
// echo( '[DEBUG] ' . $message );
1238+
}
1239+
1240+
/**
1241+
* Log an info message.
1242+
*
1243+
* @param string $message Message to log
1244+
*/
1245+
public function info( $message ) {
1246+
// echo( '[INFO] ' . $message );
1247+
}
1248+
1249+
/**
1250+
* Log a warning message.
1251+
*
1252+
* @param string $message Message to log
1253+
*/
1254+
public function warning( $message ) {
1255+
echo( '[WARNING] ' . $message );
1256+
}
1257+
1258+
/**
1259+
* Log an error message.
1260+
*
1261+
* @param string $message Message to log
1262+
*/
1263+
public function error( $message ) {
1264+
echo( '[ERROR] ' . $message );
1265+
}
1266+
1267+
/**
1268+
* Log a notice message.
1269+
*
1270+
* @param string $message Message to log
1271+
*/
1272+
public function notice( $message ) {
1273+
// echo( '[NOTICE] ' . $message );
1274+
}
1275+
}

Diff for: packages/playground/data-liberation/src/import/WP_Imported_Entity.php

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class WP_Imported_Entity {
77
const TYPE_COMMENT = 'comment';
88
const TYPE_COMMENT_META = 'comment_meta';
99
const TYPE_TERM = 'term';
10-
const TYPE_TERM_META = 'term_meta';
1110
const TYPE_TAG = 'tag';
1211
const TYPE_CATEGORY = 'category';
1312
const TYPE_USER = 'user';

Diff for: packages/playground/data-liberation/src/import/WP_Logger.php

-51
This file was deleted.

0 commit comments

Comments
 (0)