-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.php
127 lines (104 loc) · 3.55 KB
/
import.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
<?php
/**
* Script to restore posts from JSON files in the exported_posts directory.
*
* Usage: wp eval-file import.php /path/to/exported_posts
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Prevent direct script access.
}
// Dry run flag: set to true to simulate changes without modifying the database.
$dry_run = true;
// Get the directory from $args[0]
if ( empty( $args[0] ) ) {
echo "Error: Please provide the path to the exported_posts directory.\n";
exit( 1 );
}
$directory = rtrim( $args[0], '/' );
if ( ! is_dir( $directory ) ) {
echo "Error: Directory $directory does not exist.\n";
exit( 1 );
}
$json_files = glob( "$directory/site_*_posts.json" );
if ( empty( $json_files ) ) {
echo "No JSON files found in $directory.\n";
exit( 0 );
}
foreach ( $json_files as $json_file ) {
echo "Processing $json_file...\n";
// Parse the JSON file.
$data = json_decode( file_get_contents( $json_file ), true );
if ( json_last_error() !== JSON_ERROR_NONE ) {
echo "Error parsing JSON file $json_file: " . json_last_error_msg() . "\n";
continue;
}
if ( empty( $data ) ) {
echo "No posts found in $json_file.\n";
continue;
}
// Extract blog ID from the file name.
preg_match( '/site_(\d+)_posts\.json$/', $json_file, $matches );
$blog_id = isset( $matches[1] ) ? intval( $matches[1] ) : 0;
if ( ! $blog_id ) {
echo "Error: Could not determine blog ID from file name $json_file.\n";
continue;
}
// For testing.
/*
$allowed_blog_ids = [ 9930 ];
if ( ! in_array( $blog_id, $allowed_blog_ids ) ) {
echo "Skipping blog ID $blog_id.\n";
continue;
}
*/
// Switch to the blog.
switch_to_blog( $blog_id );
global $wpdb;
$imported_post_ids = [];
foreach ( $data as $post ) {
$post_id = intval( $post['ID'] );
$post_content_bad = $post['post_content_bad'];
$post_content_good = $post['post_content'];
// Fetch current post content.
$current_content = $wpdb->get_var(
$wpdb->prepare(
"SELECT post_content FROM {$wpdb->posts} WHERE ID = %d",
$post_id
)
);
if ( $current_content === null ) {
echo "Post not found: blog_id:$blog_id post_id:$post_id\n";
continue;
}
// Check if the post has been modified.
if ( $current_content !== $post_content_bad ) {
echo "Post skipped due to being modified by user: blog_id:$blog_id post_id:$post_id\n";
continue;
}
// Dry run: simulate the update.
if ( $dry_run ) {
echo "Dry run: Post would be restored: blog_id:$blog_id post_id:$post_id\n";
} else {
// Overwrite the post content with the good version.
$update_result = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_content = %s WHERE ID = %d",
$post_content_good,
$post_id
)
);
if ( $update_result !== false ) {
echo "Post restored: blog_id:$blog_id post_id:$post_id\n";
} else {
echo "Error updating post: blog_id:$blog_id post_id:$post_id\n";
}
wp_cache_delete( $post_id, 'posts' );
}
}
wp_cache_delete( 'wp_get_archives', 'general' );
wp_cache_delete( 'all_page_ids', 'posts' );
wp_cache_set_posts_last_changed();
// Restore the previous blog.
restore_current_blog();
}
echo "Import process completed.\n";