Guest User

Untitled

a guest
May 3rd, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. <?php
  2.  
  3. define( 'WP_POST_REVISIONS', false );
  4. define( 'BASE_URL', 'YOUR_GHOST_URL' );
  5.  
  6. include './wp-load.php';
  7.  
  8. if ( ! function_exists( 'wp_crop_image' ) ) {
  9. include( ABSPATH . 'wp-admin/includes/image.php' );
  10. }
  11.  
  12. $ghost = json_decode( file_get_contents( './ghost.json' ) );
  13.  
  14. // Users
  15. echo "USERS:\n";
  16. $ghostUsers = $ghost->db[0]->data->users;
  17.  
  18. $ghostUserLookup = [];
  19.  
  20. foreach ( $ghostUsers as $ghostUser ) {
  21. $userId = username_exists( $ghostUser->slug );
  22. if ( ! $userId and email_exists( $ghostUser->email ) == false ) {
  23. $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
  24. $userId = wp_create_user( $ghostUser->slug, $random_password, $ghostUser->email );
  25. echo $ghostUser->slug . " (ID " . $userId . "), password = " . $random_password . "\n";
  26. } else {
  27. echo $ghostUser->slug . " (ID " . $userId . ") already migrated.\n";
  28. }
  29. $ghostUserLookup[ $ghostUser->id ] = $userId;
  30.  
  31. wp_update_user( array( 'ID' => $userId, 'user_url' => $ghostUser->website ) );
  32. update_user_meta( $userId, 'description', $ghostUser->bio );
  33. }
  34.  
  35. // Roles
  36. $ghostRoles = $ghost->db[0]->data->roles;
  37.  
  38. $ghostRoleLookup = [];
  39.  
  40. foreach ( $ghostRoles as $ghostRole ) {
  41. switch ( $ghostRole->name ) {
  42. case 'Owner':
  43. $ghostRoleLookup[ $ghostRole->id ] = 'administrator';
  44. break;
  45. case 'Administrator':
  46. $ghostRoleLookup[ $ghostRole->id ] = 'administrator';
  47. break;
  48. case 'Editor':
  49. $ghostRoleLookup[ $ghostRole->id ] = 'editor';
  50. break;
  51. case 'Author':
  52. $ghostRoleLookup[ $ghostRole->id ] = 'author';
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58.  
  59. // Match roles to users
  60. echo "ROLES_USERS:\n";
  61. $ghostRolesUsers = $ghost->db[0]->data->roles_users;
  62. foreach ( $ghostRolesUsers as $ghostRoleUser ) {
  63. $userId = $ghostUserLookup[ $ghostRoleUser->user_id ];
  64.  
  65. if ( $userId === null ) {
  66. continue;
  67. }
  68.  
  69. $user = new WP_User( $userId );
  70.  
  71. $role = $ghostRoleLookup[ $ghostRoleUser->role_id ];
  72.  
  73. echo "Assigning " . $user->user_login . " to role " . $role . "\n";
  74. $user->set_role( $role );
  75. }
  76.  
  77. // Tags
  78. echo "TAGS:\n";
  79. $ghostTags = $ghost->db[0]->data->tags;
  80.  
  81. $ghostTagLookup = [];
  82.  
  83. foreach ( $ghostTags as $ghostTag ) {
  84.  
  85. $term = term_exists( $ghostTag->name, 'post_tag' );
  86. if ( ! $term ) {
  87. $term = wp_insert_term( $ghostTag->name, 'post_tag', array(
  88. 'description' => $ghostTag->description,
  89. 'slug' => $ghostTag->slug,
  90. ) );
  91. }
  92. $ghostTagLookup[ $ghostTag->id ] = $term['term_id'];
  93. echo $ghostTag->name . " => " . $term['term_id'] . "\n";
  94. }
  95.  
  96. // Post Tags
  97. echo "POST_TAGS:\n";
  98. $ghostPostTags = $ghost->db[0]->data->posts_tags;
  99.  
  100. $ghostPostTagLookup = [];
  101.  
  102. foreach( $ghostPostTags as $ghostPostTag ) {
  103. if ( ! isset( $ghostPostTagLookup[ $ghostPostTag->post_id ] ) ) {
  104. $ghostPostTagLookup[ $ghostPostTag->post_id ] = array();
  105. }
  106. if ( isset( $ghostTagLookup[ $ghostPostTag->tag_id ] ) ) {
  107. $ghostPostTagLookup[ $ghostPostTag->post_id ][] = $ghostTagLookup[ $ghostPostTag->tag_id ];
  108. }
  109. }
  110.  
  111. // Posts
  112. echo "POSTS:\n";
  113. $ghostPosts = $ghost->db[0]->data->posts;
  114.  
  115. $ghostPostLookup = [];
  116.  
  117. foreach ( $ghostPosts as $ghostPost ) {
  118. echo $ghostPost->title . "\n";
  119. $args = array(
  120. 'post_author' => $ghostUserLookup[ $ghostPost->author_id ],
  121. 'post_date' => $ghostPost->published_at,
  122. 'post_content' => $ghostPost->html,
  123. 'post_content_filtered' => $ghostPost->plaintext,
  124. 'post_title' => $ghostPost->title,
  125. 'post_status' => $ghostPost->status === 'published' ? 'publish' : 'draft',
  126. 'post_name' => $ghostPost->slug,
  127. // 'tags_input' => $ghostPostTagLookup[ $ghostPost->id ],
  128. );
  129. $postId = wp_insert_post( $args );
  130. wp_set_object_terms( $postId, $ghostPostTagLookup[ $ghostPost->id ], 'post_tag', false );
  131.  
  132. $text = $ghostPost->html;
  133.  
  134. // find images
  135. $result = preg_match_all( '/<img.+?src="(.+?)".+?>/', $text, $matches );
  136. if ( $result && count( $matches ) >= 2 ) {
  137.  
  138. $urls = $matches[1];
  139.  
  140. foreach ( $urls as $k => $url ) {
  141. if ( strpos( $url, '/blog/content' ) !== 0 ) {
  142. continue;
  143. }
  144.  
  145. $downloadUrl = BASE_URL . $url;
  146. echo $downloadUrl . "... ";
  147.  
  148. $filename = basename( $url );
  149.  
  150. $uploadDir = wp_upload_dir( $ghostPost->published_at );
  151. $uploadFile = $uploadDir['path'] . '/' . $filename;
  152.  
  153. $contents = file_get_contents( $downloadUrl );
  154. $saveFile = fopen( $uploadFile, 'w' );
  155. fwrite( $saveFile, $contents );
  156. fclose( $saveFile );
  157.  
  158. $wp_filetype = wp_check_filetype( basename( $filename ), null );
  159.  
  160. $attachment = array(
  161. 'post_author' => $ghostUserLookup[ $ghostPost->author_id ],
  162. 'post_mime_type' => $wp_filetype['type'],
  163. 'post_title' => $filename,
  164. 'post_content' => '',
  165. 'post_status' => 'inherit',
  166. 'post_date' => $ghostPost->published_at,
  167. 'post_parent' => $postId,
  168. );
  169.  
  170. $attachId = wp_insert_attachment( $attachment, $uploadFile );
  171.  
  172. $imagenew = get_post( $attachId );
  173. $fullsizepath = get_attached_file( $imagenew->ID );
  174. $attach_data = wp_generate_attachment_metadata( $attachId, $fullsizepath );
  175. wp_update_attachment_metadata( $attachId, $attach_data );
  176.  
  177. $text = str_replace( $url, wp_get_attachment_url( $attachId ), $text );
  178.  
  179. if ( $k == 0 ) {
  180. set_post_thumbnail( $postId, $attachId );
  181. }
  182.  
  183. echo "done \n";
  184. }
  185. }
  186.  
  187. // replace links
  188. $text = str_replace( BASE_URL . '/blog', get_home_url( '/' ), $text );
  189.  
  190. wp_update_post( array(
  191. 'ID' => $postId,
  192. 'post_content' => $text,
  193. ) );
  194. }
Add Comment
Please, Sign In to add comment