Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. //
  2. // WordpressImport Class Built for LaravelVoyager
  3. //
  4.  
  5. class WordpressImport
  6. {
  7. // store the Wordpress XML
  8. public $wpXML;
  9.  
  10. public $authors;
  11. public $attachments;
  12. public $categories;
  13. public $posts;
  14.  
  15. public $copyImages = true;
  16.  
  17. function __construct($wpXML, $copyImages)
  18. {
  19. $this->wpXML = simplexml_load_file($wpXML, 'SimpleXMLElement', LIBXML_NOCDATA);
  20.  
  21. $this->copyImages = $copyImages;
  22. $this->userDefaultPassword = 'password';
  23.  
  24. $this->saveAuthors();
  25. $this->saveCategories();
  26. $this->saveAttachments();
  27. $this->savePosts();
  28. }
  29.  
  30. // Create new users and load them into array
  31. private function saveAuthors(){
  32.  
  33. $wpData = $this->wpXML->channel->children('wp', true);
  34. $defaultUserRoleId = TCG\Voyager\Models\Role::where('name', '=', 'user')->first()->id;
  35.  
  36. foreach($wpData->author as $author){
  37. $this->authors[(string)$author->author_login] = array(
  38. 'role_id' => $defaultUserRoleId,
  39. 'name' => (string)$author->author_display_name,
  40. 'email' => (string)$author->author_email,
  41. 'password' => \Hash::make($this->userDefaultPassword),
  42. );
  43.  
  44. $new_user = TCG\Voyager\Models\User::create($this->authors[(string)$author->author_login]);
  45.  
  46. // store the new id in the array
  47. $this->authors[(string)$author->author_login]['id'] = $new_user->id;
  48. }
  49. }
  50.  
  51. // Create new categories and store them in the array
  52. private function saveCategories(){
  53.  
  54. $wpData = $this->wpXML->channel->children('wp', true);
  55.  
  56. $order = 1;
  57. foreach($wpData->category as $category){
  58.  
  59. $this->categories[(string)$category->category_nicename] = array(
  60. 'parent_id' => NULL,
  61. 'order' => $order,
  62. 'name' => (string)$category->cat_name,
  63. 'slug' => (string)$category->category_nicename
  64. );
  65.  
  66. $new_cat = TCG\Voyager\Models\Category::create($this->categories[(string)$category->category_nicename]);
  67.  
  68. $this->categories[(string)$category->category_nicename]['parent'] = (string)$category->category_parent;
  69. $this->categories[(string)$category->category_nicename]['id'] = $new_cat->id;
  70.  
  71. $order += 1;
  72. }
  73.  
  74. // Save any parent categories to their children
  75. foreach($this->categories as $category){
  76. if(!empty($category['parent'])){
  77. $parent = TCG\Voyager\Models\Category::where('slug', '=', $category['parent'])->first();
  78. if(isset($parent->id)){
  79. $category['parent_id'] = $parent->id;
  80. $this_cat = TCG\Voyager\Models\Category::find($category['id']);
  81. if(isset($this_cat->id)){
  82. $this_cat->parent_id = $parent->id;
  83. $this_cat->save();
  84. }
  85. }
  86. }
  87. }
  88. }
  89.  
  90. // Save all the attachments in an array
  91. private function saveAttachments(){
  92.  
  93. foreach($this->wpXML->channel->item as $item)
  94. {
  95. // Save The Attachments in an array
  96. $wpData = $item->children('wp', true);
  97. if($wpData->post_type == 'attachment'){
  98. $this->attachments[(string)$wpData->post_parent] = (string)$wpData->attachment_url;
  99. }
  100.  
  101. }
  102. }
  103.  
  104. private function savePosts()
  105. {
  106. foreach($this->wpXML->channel->item as $item)
  107. {
  108.  
  109. $wpData = $item->children('wp', true);
  110. $content = $item->children('content', true);
  111. $excerpt = $item->children('excerpt', true);
  112. $category = NULL;
  113. $image = isset($this->attachments[(string)$wpData->post_id]) ? $this->attachments[(string)$wpData->post_id] : '';
  114. $dc = $item->children('excerpt', true);
  115. $author = NULL;
  116.  
  117. if(isset($dc->creator)){
  118. $author = (string)$dc->creator;
  119. }
  120.  
  121. if(isset($item->category["nicename"])){
  122. $category = (string)$item->category["nicename"];
  123. }
  124.  
  125. $status = 'PUBLISHED';
  126. if(isset($wpData->status) && $wpData->status != 'publish'){
  127. $status = 'DRAFT';
  128. }
  129.  
  130. if($wpData->post_type == 'post'){
  131.  
  132.  
  133. $this->posts[] = array(
  134. "author_id" => isset($this->authors[$author]['id']) ? $this->authors[$author]['id'] : 1,
  135. "category_id" => isset($this->categories[$category]['id']) ? $this->categories[$category]['id'] : NULL,
  136. "title" => trim((string)$item->title, '"'),
  137. "seo_title" => trim((string)$item->title, '"'),
  138. "excerpt" => trim((string)$excerpt->encoded, '" \n'),
  139. "body" => trim((string)$content->encoded, '" \n'),
  140. "image" => $this->getImage($image),
  141. "slug" => (string)$wpData->post_name,
  142. "status" => $status,
  143. "featured" => 0,
  144. "created_at" => \Carbon\Carbon::parse((string)$item->pubDate),
  145. "updated_at" => \Carbon\Carbon::parse((string)$item->pubDate),
  146. );
  147.  
  148. }
  149.  
  150. }
  151.  
  152. TCG\Voyager\Models\Post::insert($this->posts);
  153.  
  154. }
  155.  
  156. private function getImage($image){
  157.  
  158. if(!empty($image) && $this->copyImages){
  159.  
  160. $resize_width = 1800;
  161. $resize_height = null;
  162. $path = 'posts/'.date('FY').'/';
  163. $filename = basename($image);
  164.  
  165. $img = Image::make($image)->resize($resize_width, $resize_height,
  166. function (Intervention\Image\Constraint $constraint) {
  167. $constraint->aspectRatio();
  168. $constraint->upsize();
  169. })->encode(pathinfo($image, PATHINFO_EXTENSION), 75);
  170. Storage::disk(config('voyager.storage.disk'))->put($path.$filename, (string) $img, 'public');
  171.  
  172. $image = $path.$filename;
  173.  
  174. }
  175.  
  176. return $image;
  177.  
  178.  
  179. }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement