Wordpress.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model\Import;
  9. /**
  10. * Wordpress import model
  11. */
  12. class Wordpress extends AbstractImport
  13. {
  14. protected $_requiredFields = ['dbname', 'uname', 'pwd', 'dbhost', 'prefix'];
  15. public function execute()
  16. {
  17. $con = $this->_connect = mysqli_connect(
  18. $this->getData('dbhost'),
  19. $this->getData('uname'),
  20. $this->getData('pwd'),
  21. $this->getData('dbname')
  22. );
  23. if (mysqli_connect_errno()) {
  24. throw new \Exception("Failed connect to wordpress database", 1);
  25. }
  26. $_pref = mysqli_real_escape_string($con, $this->getData('prefix'));
  27. $categories = [];
  28. $oldCategories = [];
  29. /* Import categories */
  30. $sql = 'SELECT
  31. t.term_id as old_id,
  32. t.name as title,
  33. t.slug as identifier,
  34. tt.parent as parent_id
  35. FROM '.$_pref.'terms t
  36. LEFT JOIN '.$_pref.'term_taxonomy tt on t.term_id = tt.term_id
  37. WHERE tt.taxonomy = "category" AND t.slug <> "uncategorized"';
  38. $result = $this->_mysqliQuery($sql);
  39. while ($data = mysqli_fetch_assoc($result)) {
  40. /* Prepare category data */
  41. foreach (['title', 'identifier'] as $key) {
  42. $data[$key] = utf8_encode($data[$key]);
  43. }
  44. $data['store_ids'] = [$this->getStoreId()];
  45. $data['is_active'] = 1;
  46. $data['position'] = 0;
  47. $data['path'] = 0;
  48. $data['identifier'] = trim(strtolower($data['identifier']));
  49. if (strlen($data['identifier']) == 1) {
  50. $data['identifier'] .= $data['identifier'];
  51. }
  52. $category = $this->_categoryFactory->create();
  53. try {
  54. /* Initial saving */
  55. $category->setData($data)->save();
  56. $this->_importedCategoriesCount++;
  57. $categories[$category->getId()] = $category;
  58. $oldCategories[$category->getOldId()] = $category;
  59. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  60. unset($category);
  61. $this->_skippedCategories[] = $data['title'];
  62. }
  63. }
  64. /* Reindexing parent categories */
  65. foreach ($categories as $ct) {
  66. if ($oldParentId = $ct->getData('parent_id')) {
  67. if (isset($oldCategories[$oldParentId])) {
  68. $ct->setPath(
  69. $parentId = $oldCategories[$oldParentId]->getId()
  70. );
  71. }
  72. }
  73. }
  74. for ($i = 0; $i < 4; $i++) {
  75. $changed = false;
  76. foreach ($categories as $ct) {
  77. if ($ct->getPath()) {
  78. $parentId = explode('/', $ct->getPath())[0];
  79. $pt = $categories[$parentId];
  80. if ($pt->getPath()) {
  81. $ct->setPath($pt->getPath() . '/'. $ct->getPath());
  82. $changed = true;
  83. }
  84. }
  85. }
  86. if (!$changed) {
  87. break;
  88. }
  89. }
  90. /* end*/
  91. foreach($categories as $ct) {
  92. /* Final saving */
  93. $ct->save();
  94. }
  95. /* Import tags */
  96. $tags = [];
  97. $oldTags = [];
  98. $sql = 'SELECT
  99. t.term_id as old_id,
  100. t.name as title,
  101. t.slug as identifier,
  102. tt.parent as parent_id
  103. FROM '.$_pref.'terms t
  104. LEFT JOIN '.$_pref.'term_taxonomy tt on t.term_id = tt.term_id
  105. WHERE tt.taxonomy = "post_tag" AND t.slug <> "uncategorized"';
  106. $result = $this->_mysqliQuery($sql);
  107. while ($data = mysqli_fetch_assoc($result)) {
  108. /* Prepare tag data */
  109. foreach (['title', 'identifier'] as $key) {
  110. $data[$key] = utf8_encode($data[$key]);
  111. }
  112. $data['identifier'] = trim(strtolower($data['identifier']));
  113. if (strlen($data['identifier']) == 1) {
  114. $data['identifier'] .= $data['identifier'];
  115. }
  116. $tag = $this->_tagFactory->create();
  117. try {
  118. /* Initial saving */
  119. $tag->setData($data)->save();
  120. $this->_importedTagsCount++;
  121. $tags[$tag->getId()] = $tag;
  122. $oldTags[$tag->getOldId()] = $tag;
  123. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  124. unset($tag);
  125. $this->_skippedTags[] = $data['title'];
  126. }
  127. }
  128. /* Import posts */
  129. $sql = 'SELECT * FROM '.$_pref.'posts WHERE `post_type` = "post"';
  130. $result = $this->_mysqliQuery($sql);
  131. while ($data = mysqli_fetch_assoc($result)) {
  132. /* find post categories*/
  133. $postCategories = [];
  134. $sql = 'SELECT tt.term_id as term_id FROM '.$_pref.'term_relationships tr
  135. LEFT JOIN '.$_pref.'term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
  136. WHERE tr.`object_id` = "'.$data['ID'].'" AND tt.taxonomy = "category"';
  137. $result2 = $this->_mysqliQuery($sql);
  138. while ($data2 = mysqli_fetch_assoc($result2)) {
  139. $oldTermId = $data2['term_id'];
  140. if (isset($oldCategories[$oldTermId])) {
  141. $postCategories[] = $oldCategories[$oldTermId]->getId();
  142. }
  143. }
  144. /* find post tags*/
  145. $postTags = [];
  146. $sql = 'SELECT tt.term_id as term_id FROM '.$_pref.'term_relationships tr
  147. LEFT JOIN '.$_pref.'term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
  148. WHERE tr.`object_id` = "'.$data['ID'].'" AND tt.taxonomy = "post_tag"';
  149. $result2 = $this->_mysqliQuery($sql);
  150. while ($data2 = mysqli_fetch_assoc($result2)) {
  151. $oldTermId = $data2['term_id'];
  152. if (isset($oldTags[$oldTermId])) {
  153. $postTags[] = $oldTags[$oldTermId]->getId();
  154. }
  155. }
  156. $data['featured_img'] = '';
  157. $sql = 'SELECT wm2.meta_value as featured_img
  158. FROM
  159. '.$_pref.'posts p1
  160. LEFT JOIN
  161. '.$_pref.'postmeta wm1
  162. ON (
  163. wm1.post_id = p1.id
  164. AND wm1.meta_value IS NOT NULL
  165. AND wm1.meta_key = "_thumbnail_id"
  166. )
  167. LEFT JOIN
  168. '.$_pref.'postmeta wm2
  169. ON (
  170. wm1.meta_value = wm2.post_id
  171. AND wm2.meta_key = "_wp_attached_file"
  172. AND wm2.meta_value IS NOT NULL
  173. )
  174. WHERE
  175. p1.ID="'.$data['ID'].'"
  176. AND p1.post_type="post"
  177. ORDER BY
  178. p1.post_date DESC';
  179. $result2 = $this->_mysqliQuery($sql);
  180. if ($data2 = mysqli_fetch_assoc($result2)) {
  181. if ($data2['featured_img']) {
  182. $data['featured_img'] = \Magefan\Blog\Model\Post::BASE_MEDIA_PATH . '/' . $data2['featured_img'];
  183. }
  184. }
  185. /* Prepare post data */
  186. foreach (['post_title', 'post_name', 'post_content'] as $key) {
  187. $data[$key] = utf8_encode($data[$key]);
  188. }
  189. $creationTime = strtotime($data['post_date_gmt']);
  190. $content = $data['post_content'];
  191. $content = str_replace('<!--more-->', '<!-- pagebreak -->', $content);
  192. $content = preg_replace(
  193. '/((http:\/\/|https:\/\/|\/\/)(.*)|(\s|"|\')|(\/[\d\w_\-\.]*))\/wp-content\/uploads(.*)((\.jpg|\.jpeg|\.gif|\.png|\.tiff|\.tif|\.svg)|(\s|"|\'))/Ui',
  194. '$4{{media url="magefan_blog$6$8"}}$9',
  195. $content
  196. );
  197. $data = [
  198. 'store_ids' => [$this->getStoreId()],
  199. 'title' => $data['post_title'],
  200. 'meta_keywords' => '',
  201. 'meta_description' => '',
  202. 'identifier' => $data['post_name'],
  203. 'content_heading' => '',
  204. 'content' => $content,
  205. 'creation_time' => $creationTime,
  206. 'update_time' => strtotime($data['post_modified_gmt']),
  207. 'publish_time' => $creationTime,
  208. 'is_active' => (int)($data['post_status'] == 'publish'),
  209. 'categories' => $postCategories,
  210. 'tags' => $postTags,
  211. 'featured_img' => $data['featured_img'],
  212. ];
  213. $data['identifier'] = trim(strtolower($data['identifier']));
  214. if (strlen($data['identifier']) == 1) {
  215. $data['identifier'] .= $data['identifier'];
  216. }
  217. $post = $this->_postFactory->create();
  218. try {
  219. /* Post saving */
  220. $post->setData($data)->save();
  221. $this->_importedPostsCount++;
  222. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  223. $this->_skippedPosts[] = $data['title'];
  224. }
  225. unset($post);
  226. }
  227. /* end */
  228. mysqli_close($con);
  229. }
  230. }