Skip to content

Commit

Permalink
Removed all changes of #2105 and #2104
Browse files Browse the repository at this point in the history
  • Loading branch information
zaerl committed Jan 8, 2025
1 parent 9d19eb9 commit 0b68a60
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"pluginPath": "data-liberation/plugin.php"
},
{
"step": "wp-cli",
"command": "wp data-liberation import /wordpress/wp-content/uploads/import-wxr"
"step": "runPHP",
"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};"
}
]
}
53 changes: 32 additions & 21 deletions packages/playground/data-liberation/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,40 @@ function () {
}
);

function data_liberation_init() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once __DIR__ . '/src/cli/WP_Import_Command.php';
add_action(
'init',
function () {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
/**
* Import a WXR file.
*
* <file>
* : The WXR file to import.
*/
$command = function ( $args, $assoc_args ) {
$file = $args[0];
data_liberation_import( $file );
};

// Register the WP-CLI import command.
// Example usage: wp data-liberation /path/to/file.xml
WP_CLI::add_command( 'data-liberation', $command );
}

// Register the WP-CLI import command.
WP_CLI::add_command( 'data-liberation', WP_Import_Command::class );
register_post_status(
'error',
array(
'label' => _x( 'Error', 'post' ), // Label name
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false,
// translators: %s is the number of errors
'label_count' => _n_noop( 'Error <span class="count">(%s)</span>', 'Error <span class="count">(%s)</span>' ),
)
);
}

register_post_status(
'error',
array(
'label' => _x( 'Error', 'post' ), // Label name
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false,
// translators: %s is the number of errors
'label_count' => _n_noop( 'Error <span class="count">(%s)</span>', 'Error <span class="count">(%s)</span>' ),
)
);
}

add_action( 'init', 'data_liberation_init' );
);

function data_liberation_activate() {
// Create tables and option.
Expand Down
34 changes: 34 additions & 0 deletions packages/playground/data-liberation/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,37 @@ function mb_str_split( $input, $split_length = 1, $encoding = null ) {
return $result;
}
}

/**
* Import a WXR file. Used by the CLI.
*
* @param string $path The path to the WXR file.
* @return void
*/
function data_liberation_import( $path ): bool {
$importer = WP_Stream_Importer::create_for_wxr_file( $path );

if ( ! $importer ) {
return false;
}

$is_wp_cli = defined( 'WP_CLI' ) && WP_CLI;

if ( $is_wp_cli ) {
WP_CLI::line( "Importing from {$path}" );
}

while ( $importer->next_step() ) {
// Output the current stage if running in WP-CLI.
if ( $is_wp_cli ) {
$current_stage = $importer->get_current_stage();
WP_CLI::line( "Import: stage {$current_stage}" );
}
}

if ( $is_wp_cli ) {
WP_CLI::success( 'Import ended' );
}

return true;
}
128 changes: 56 additions & 72 deletions packages/playground/data-liberation/src/import/WP_Entity_Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct( $options = array() ) {
$this->mapping['term_id'] = array();
$this->requires_remapping = $empty_types;
$this->exists = $empty_types;
$this->logger = isset( $options['logger'] ) ? $options['logger'] : new WP_Logger();
$this->logger = new Logger();

$this->options = wp_parse_args(
$options,
Expand Down Expand Up @@ -126,8 +126,6 @@ public function import_entity( WP_Imported_Entity $entity ) {
case WP_Imported_Entity::TYPE_TAG:
case WP_Imported_Entity::TYPE_CATEGORY:
return $this->import_term( $data );
case WP_Imported_Entity::TYPE_TERM_META:
return $this->import_term_meta( $data, $data['term_id'] );
case WP_Imported_Entity::TYPE_USER:
return $this->import_user( $data );
case WP_Imported_Entity::TYPE_SITE_OPTION:
Expand Down Expand Up @@ -390,40 +388,6 @@ public function import_term( $data ) {
return $term_id;
}

public function import_term_meta( $meta_item, $term_id ) {
if ( empty( $meta_item ) ) {
return true;
}

/**
* Pre-process term meta data.
*
* @param array $meta_item Meta data. (Return empty to skip.)
* @param int $term_id Term the meta is attached to.
*/
$meta_item = apply_filters( 'wxr_importer_pre_process_term_meta', $meta_item, $term_id );
if ( empty( $meta_item ) ) {
return false;
}

// Have we already processed this?
if ( isset( $element['_already_mapped'] ) ) {
$this->logger->debug( 'Skipping term meta, already processed' );
return;
}

if ( ! isset( $meta_item['term_id'] ) ) {
$meta_item['term_id'] = $term_id;
}

$value = maybe_unserialize( $meta_item['meta_value'] );
$term_meta_id = add_term_meta( $meta_item['term_id'], wp_slash( $meta_item['meta_key'] ), wp_slash_strings_only( $value ) );

do_action( 'wxr_importer_processed_term_meta', $term_meta_id, $meta_item, $meta_item['term_id'] );

return $term_meta_id;
}

/**
* Prefill existing post data.
*
Expand Down Expand Up @@ -480,8 +444,6 @@ protected function post_exists( $data ) {
* Note that new/updated terms, comments and meta are imported for the last of the above.
*/
public function import_post( $data ) {
$parent_id = isset( $data['post_parent'] ) ? (int) $data['post_parent'] : 0;

/**
* Pre-process post data.
*
Expand All @@ -490,7 +452,7 @@ public function import_post( $data ) {
* @param array $comments Comments on the post.
* @param array $terms Terms on the post.
*/
$data = apply_filters( 'wxr_importer_pre_process_post', $data, $parent_id );
$data = apply_filters( 'wxr_importer_pre_process_post', $data );
if ( empty( $data ) ) {
$this->logger->debug( 'Skipping post, empty data' );
return false;
Expand Down Expand Up @@ -659,37 +621,6 @@ public function import_post( $data ) {
}
$this->mark_post_exists( $data, $post_id );

// Add terms to the post
/*if ( ! empty( $data['terms'] ) ) {
$terms_to_set = array();
foreach ( $data['terms'] as $term ) {
// Back compat with WXR 1.0 map 'tag' to 'post_tag'
$taxonomy = ( 'tag' === $term['taxonomy'] ) ? 'post_tag' : $term['taxonomy'];
$term_exists = term_exists( $term['slug'], $taxonomy );
$term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
if ( ! $term_id ) {
// @TODO: Add a unit test with a WXR with one post and X tags without root declated tags.
$new_term = wp_insert_term( $term['slug'], $taxonomy, $term );
if ( ! is_wp_error( $new_term ) ) {
$term_id = $new_term['term_id'];
$this->topological_sorter->update_mapped_id( $new_term, $term_id );
} else {
continue;
}
}
$terms_to_set[ $taxonomy ][] = intval( $term_id );
}
foreach ( $terms_to_set as $tax => $ids ) {
// Add the post terms to the post
wp_set_post_terms( $post_id, $ids, $tax );
}
}*/

$this->logger->info(
sprintf(
/* translators: 1: post title, 2: post type name */
Expand Down Expand Up @@ -717,7 +648,6 @@ public function import_post( $data ) {
* @param array $terms Raw term data, already processed.
*/
do_action( 'wxr_importer_processed_post', $post_id, $data );

return $post_id;
}

Expand Down Expand Up @@ -1289,3 +1219,57 @@ public static function sort_comments_by_id( $a, $b ) {
return $a['comment_id'] - $b['comment_id'];
}
}

/**
* @TODO how to treat this? Should this class even exist?
* how does WordPress handle different levels? It
* seems useful for usage in wp-cli, Blueprints,
* and other non-web environments.
*/
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound
class Logger {
/**
* Log a debug message.
*
* @param string $message Message to log
*/
public function debug( $message ) {
// echo( '[DEBUG] ' . $message );
}

/**
* Log an info message.
*
* @param string $message Message to log
*/
public function info( $message ) {
// echo( '[INFO] ' . $message );
}

/**
* Log a warning message.
*
* @param string $message Message to log
*/
public function warning( $message ) {
echo( '[WARNING] ' . $message );
}

/**
* Log an error message.
*
* @param string $message Message to log
*/
public function error( $message ) {
echo( '[ERROR] ' . $message );
}

/**
* Log a notice message.
*
* @param string $message Message to log
*/
public function notice( $message ) {
// echo( '[NOTICE] ' . $message );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class WP_Imported_Entity {
const TYPE_COMMENT = 'comment';
const TYPE_COMMENT_META = 'comment_meta';
const TYPE_TERM = 'term';
const TYPE_TERM_META = 'term_meta';
const TYPE_TAG = 'tag';
const TYPE_CATEGORY = 'category';
const TYPE_USER = 'user';
Expand Down
51 changes: 0 additions & 51 deletions packages/playground/data-liberation/src/import/WP_Logger.php

This file was deleted.

0 comments on commit 0b68a60

Please sign in to comment.