Last active
September 21, 2024 21:02
-
-
Save PetraMotz/d49143d37a98ad6cd6b32c119b0e06a7 to your computer and use it in GitHub Desktop.
PHP #wordpress #import #php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public function execute() { | |
| /** @var Connection $connection */ | |
| $connection = GeneralUtility::makeInstance(ObjectManager::class)->get(ConnectionPool::class)->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); | |
| if ($this->initialize() === false) { | |
| return false; | |
| } | |
| /** @var Typo3QuerySettings $querySettings */ | |
| $querySettings = $this->objectManager->get(Typo3QuerySettings::class); | |
| $querySettings->setRespectStoragePage(false); | |
| $this->postRepository->setDefaultQuerySettings($querySettings); | |
| $this->categoryRepository->setDefaultQuerySettings($querySettings); | |
| $categories = json_decode(file_get_contents('https://www.gsunde-gschichten.de/wp-json//wp/v2/categories?per_page=100'), true); | |
| $posts = []; | |
| $tags = []; | |
| for ($i = 1; $i <= 5; $i++) { | |
| $tmp = json_decode(file_get_contents('https://www.gsunde-gschichten.de/wp-json//wp/v2/tags?per_page=100&page=' . $i), true); | |
| $tags = array_merge($tags, $tmp); | |
| } | |
| for ($i = 1; $i <= 2; $i++) { | |
| $tmp = json_decode(file_get_contents('https://www.gsunde-gschichten.de/wp-json//wp/v2/posts?per_page=100&page=' . $i), true);; | |
| $posts = array_merge($posts, $tmp); | |
| } | |
| foreach ($categories as $category) { | |
| $Category = new Category(); | |
| $Category->setPid(161); | |
| $Category->setName($category['name']); | |
| $Category->setDescription($category['id']); | |
| $this->categoryRepository->add($Category); | |
| } | |
| $this->objectManager->get(PersistenceManager::class)->persistAll(); | |
| foreach ($posts as $post) { | |
| $Post = new Post(); | |
| $Post->setPid(161); | |
| $Post->setHidden(false); | |
| $Post->setTitle($post['title']['rendered']); | |
| $Post->setPublishDate(date_create_from_format('Y-m-d\TH:i:s', $post['date'])); | |
| $Post->setPreviewText($post['excerpt']['rendered']); | |
| $Post->setAuthor(3); | |
| $html = $post['content']['rendered']; | |
| preg_match_all('/<img.*src="(.*)"/imU', $html, $images); | |
| foreach ($images[1] as $key => $image) { | |
| $data = @file_get_contents($image); | |
| if (strlen($data) == 0) { | |
| $html = str_replace($images[0][$key], '', $html); | |
| } else { | |
| $path = '/uploads/t3extblog/' . basename($image); | |
| @file_put_contents($_SERVER['DOCUMENT_ROOT'] . $path, $data); | |
| $html = str_replace($image, $path, $html); | |
| } | |
| } | |
| $content = new Content(); | |
| $content->setPid(161); | |
| $content->setBodytext($html); | |
| $content->setCType('html'); | |
| $Post->addContent($content); | |
| foreach ($post['categories'] as $category) { | |
| $Post->addCategory($this->categoryRepository->findOneByDescription($category)); | |
| } | |
| $post_tags = []; | |
| foreach ($post['tags'] as $post_tag) { | |
| foreach ($tags as $tag) { | |
| if ($post_tag == $tag['id']) { | |
| $post_tags[] = $tag['name']; | |
| } | |
| } | |
| } | |
| $Post->setTagCloud(strtolower(implode(', ', $post_tags))); | |
| $this->postRepository->add($Post); | |
| $this->objectManager->get(PersistenceManager::class)->persistAll(); | |
| $connection->query('UPDATE tx_t3blog_post SET url_segment = ' . $connection->quote($post['slug']) . ' WHERE uid = ' . $Post->getUid()); | |
| } | |
| $this->objectManager->get(PersistenceManager::class)->persistAll(); | |
| return true; | |
| } | |
| Task über Planer ausführen |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension in typo3conf/ext/ einfügen (entpackte zip) | |
| In diesem Fall wurde ein Wordpress Blog über die | |
| https://www.gsunde-gschichten.de/wp-json/wp/v2/posts | |
| in die T3Blog Extension importiert | |
| Die Bilder werden aus dem html content rausgefiltert, die url wird gegen eine eigene ersetzt, das bild wird lokal gespeichert und die neue url wird eingefügt. | |
| http://jsonviewer.stack.hu/ | |
| https://developer.wordpress.org/rest-api/reference/ ->parameter für den link ACHTUNG !!! per_page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment