|
<?php |
|
|
|
define( 'WP_POST_REVISIONS', false ); |
|
define( 'BASE_URL', 'YOUR_GHOST_URL' ); |
|
|
|
include './wp-load.php'; |
|
|
|
if ( ! function_exists( 'wp_crop_image' ) ) { |
|
include( ABSPATH . 'wp-admin/includes/image.php' ); |
|
} |
|
|
|
$ghost = json_decode( file_get_contents( './ghost.json' ) ); |
|
|
|
// Users |
|
echo "USERS:\n"; |
|
$ghostUsers = $ghost->db[0]->data->users; |
|
|
|
$ghostUserLookup = []; |
|
|
|
foreach ( $ghostUsers as $ghostUser ) { |
|
$userId = username_exists( $ghostUser->slug ); |
|
if ( ! $userId and email_exists( $ghostUser->email ) == false ) { |
|
$random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false ); |
|
$userId = wp_create_user( $ghostUser->slug, $random_password, $ghostUser->email ); |
|
echo $ghostUser->slug . " (ID " . $userId . "), password = " . $random_password . "\n"; |
|
} else { |
|
echo $ghostUser->slug . " (ID " . $userId . ") already migrated.\n"; |
|
} |
|
$ghostUserLookup[ $ghostUser->id ] = $userId; |
|
|
|
wp_update_user( array( 'ID' => $userId, 'user_url' => $ghostUser->website ) ); |
|
update_user_meta( $userId, 'description', $ghostUser->bio ); |
|
} |
|
|
|
// Roles |
|
$ghostRoles = $ghost->db[0]->data->roles; |
|
|
|
$ghostRoleLookup = []; |
|
|
|
foreach ( $ghostRoles as $ghostRole ) { |
|
switch ( $ghostRole->name ) { |
|
case 'Owner': |
|
$ghostRoleLookup[ $ghostRole->id ] = 'administrator'; |
|
break; |
|
case 'Administrator': |
|
$ghostRoleLookup[ $ghostRole->id ] = 'administrator'; |
|
break; |
|
case 'Editor': |
|
$ghostRoleLookup[ $ghostRole->id ] = 'editor'; |
|
break; |
|
case 'Author': |
|
$ghostRoleLookup[ $ghostRole->id ] = 'author'; |
|
break; |
|
default: |
|
break; |
|
} |
|
} |
|
|
|
// Match roles to users |
|
echo "ROLES_USERS:\n"; |
|
$ghostRolesUsers = $ghost->db[0]->data->roles_users; |
|
foreach ( $ghostRolesUsers as $ghostRoleUser ) { |
|
$userId = $ghostUserLookup[ $ghostRoleUser->user_id ]; |
|
|
|
if ( $userId === null ) { |
|
continue; |
|
} |
|
|
|
$user = new WP_User( $userId ); |
|
|
|
$role = $ghostRoleLookup[ $ghostRoleUser->role_id ]; |
|
|
|
echo "Assigning " . $user->user_login . " to role " . $role . "\n"; |
|
$user->set_role( $role ); |
|
} |
|
|
|
// Tags |
|
echo "TAGS:\n"; |
|
$ghostTags = $ghost->db[0]->data->tags; |
|
|
|
$ghostTagLookup = []; |
|
|
|
foreach ( $ghostTags as $ghostTag ) { |
|
|
|
$term = term_exists( $ghostTag->name, 'post_tag' ); |
|
if ( ! $term ) { |
|
$term = wp_insert_term( $ghostTag->name, 'post_tag', array( |
|
'description' => $ghostTag->description, |
|
'slug' => $ghostTag->slug, |
|
) ); |
|
} |
|
$ghostTagLookup[ $ghostTag->id ] = $term['term_id']; |
|
echo $ghostTag->name . " => " . $term['term_id'] . "\n"; |
|
} |
|
|
|
// Post Tags |
|
echo "POST_TAGS:\n"; |
|
$ghostPostTags = $ghost->db[0]->data->posts_tags; |
|
|
|
$ghostPostTagLookup = []; |
|
|
|
foreach( $ghostPostTags as $ghostPostTag ) { |
|
if ( ! isset( $ghostPostTagLookup[ $ghostPostTag->post_id ] ) ) { |
|
$ghostPostTagLookup[ $ghostPostTag->post_id ] = array(); |
|
} |
|
if ( isset( $ghostTagLookup[ $ghostPostTag->tag_id ] ) ) { |
|
$ghostPostTagLookup[ $ghostPostTag->post_id ][] = $ghostTagLookup[ $ghostPostTag->tag_id ]; |
|
} |
|
} |
|
|
|
// Posts |
|
echo "POSTS:\n"; |
|
$ghostPosts = $ghost->db[0]->data->posts; |
|
|
|
$ghostPostLookup = []; |
|
|
|
foreach ( $ghostPosts as $ghostPost ) { |
|
echo $ghostPost->title . "\n"; |
|
$args = array( |
|
'post_author' => $ghostUserLookup[ $ghostPost->author_id ], |
|
'post_date' => $ghostPost->published_at, |
|
'post_content' => $ghostPost->html, |
|
'post_content_filtered' => $ghostPost->plaintext, |
|
'post_title' => $ghostPost->title, |
|
'post_status' => $ghostPost->status === 'published' ? 'publish' : 'draft', |
|
'post_name' => $ghostPost->slug, |
|
// 'tags_input' => $ghostPostTagLookup[ $ghostPost->id ], |
|
); |
|
$postId = wp_insert_post( $args ); |
|
wp_set_object_terms( $postId, $ghostPostTagLookup[ $ghostPost->id ], 'post_tag', false ); |
|
|
|
$text = $ghostPost->html; |
|
|
|
// find images |
|
$result = preg_match_all( '/<img.+?src="(.+?)".+?>/', $text, $matches ); |
|
if ( $result && count( $matches ) >= 2 ) { |
|
|
|
$urls = $matches[1]; |
|
|
|
foreach ( $urls as $k => $url ) { |
|
if ( strpos( $url, '/blog/content' ) !== 0 ) { |
|
continue; |
|
} |
|
|
|
$downloadUrl = BASE_URL . $url; |
|
echo $downloadUrl . "... "; |
|
|
|
$filename = basename( $url ); |
|
|
|
$uploadDir = wp_upload_dir( $ghostPost->published_at ); |
|
$uploadFile = $uploadDir['path'] . '/' . $filename; |
|
|
|
$contents = file_get_contents( $downloadUrl ); |
|
$saveFile = fopen( $uploadFile, 'w' ); |
|
fwrite( $saveFile, $contents ); |
|
fclose( $saveFile ); |
|
|
|
$wp_filetype = wp_check_filetype( basename( $filename ), null ); |
|
|
|
$attachment = array( |
|
'post_author' => $ghostUserLookup[ $ghostPost->author_id ], |
|
'post_mime_type' => $wp_filetype['type'], |
|
'post_title' => $filename, |
|
'post_content' => '', |
|
'post_status' => 'inherit', |
|
'post_date' => $ghostPost->published_at, |
|
'post_parent' => $postId, |
|
); |
|
|
|
$attachId = wp_insert_attachment( $attachment, $uploadFile ); |
|
|
|
$imagenew = get_post( $attachId ); |
|
$fullsizepath = get_attached_file( $imagenew->ID ); |
|
$attach_data = wp_generate_attachment_metadata( $attachId, $fullsizepath ); |
|
wp_update_attachment_metadata( $attachId, $attach_data ); |
|
|
|
$text = str_replace( $url, wp_get_attachment_url( $attachId ), $text ); |
|
|
|
if ( $k == 0 ) { |
|
set_post_thumbnail( $postId, $attachId ); |
|
} |
|
|
|
echo "done \n"; |
|
} |
|
} |
|
|
|
// replace links |
|
$text = str_replace( BASE_URL . '/blog', get_home_url( '/' ), $text ); |
|
|
|
wp_update_post( array( |
|
'ID' => $postId, |
|
'post_content' => $text, |
|
) ); |
|
} |