要将来自给定远程站点的帖子合并到给定类别下的一个站点中,我如何挂接导入插件以保存给定类别的帖子:
给出的示例:
接收方站点具有类别auto 我想从(例如)导入所有帖子auto.com 在下面this category. 如果远程帖子有另一个类别auto 添加为子级。
应该下载内部帖子图片,更新帖子内的所有链接,因此我已经找到了一个核心方法,下面是我尝试的方法,但我认为这可以变得更简单
<?php
if (!class_exists(\'Wp_Http\'))
    include_once(ABSPATH . WPINC . \'/class-http.php\');
require_once ABSPATH . \'wp-admin/includes/import.php\';
if (!class_exists(\'WP_Importer\')) {
    $class_importer = ABSPATH . \'wp-admin/includes/class-wp-importer.php\';
    if (file_exists($class_importer)) {
        require $class_importer;
    }
}
class WordpressMigration extends WP_Importer
{
    public $wpXML;
    public $xml;
    public $domain;
    function __construct($wpXML)
    {
        $this->wpXML = $wpXML;
        $this->xml = simplexml_load_file($this->wpXML);
        $this->domain = (string)$this->xml->channel->link;
    }
    public function getPosts()
    {
        $this->xml = simplexml_load_file($this->wpXML);
        $posts = array();
        /* import authors */
        $authors = $this->xml->channel->children(\'wp\', true);
        foreach ($authors->author as $author) {
        }
        foreach ($this->xml->channel->item as $item) {
            $categories = array();
            foreach ($item->category as $category) {
                //echo $category[\'domain\'];
                if ($category[\'nicename\'] != "uncategorized" && $category[\'domain\'] == "category") {
                    $categories[] = $category[\'nicename\'];
                }
            }
            $content = $item->children(\'content\', true);
            $doc = new DOMDocument();
            $doc->loadHTML(mb_convert_encoding(html_entity_decode($content->encoded), \'HTML-ENTITIES\', \'UTF-8\'));
            $imgs = $doc->getElementsByTagName(\'img\');
            //get the remote images and upload to media library
            if ($imgs instanceof DOMNodeList) {
                foreach ($imgs as $i => $img) {
                    $http = new WP_Http();
                    $targetImage = $img->getAttribute(\'src\');
                    $response = $http->request($targetImage);
                    if (!is_array($response) || $response[\'response\'][\'code\'] != 200) {
                        //write_log
                    }
                    if (is_array($response)) {
                        $upload = wp_upload_bits(basename($targetImage), null, $response[\'body\']);
                        if (!empty($upload[\'error\'])) {
                            //write_log
                        }
                        $img->setAttribute(\'src\', $upload[\'url\']);
                        $doc->getElementsByTagName(\'img\')->item($i)->nodeValue = $upload[\'url\'];
                    }
                }
            }
            $targetLinks = $doc->getElementsByTagName(\'a\');
            if ($targetLinks instanceof DOMNodeList) {
                foreach ($targetLinks as $i => $targetLink) {
                    var_dump($targetLink->getAttribute(\'href\'));
                    die;
                }
            }
            $posts[] = array(
                "title" => $item->title,
                "content" => htmlentities(html_entity_decode($doc->saveHTML())),
                "pubDate" => $item->pubDate,
                "categories" => implode(",", $categories),
                "slug" => str_replace("/", "", str_replace("", "", $item->guid))
            );
        }
        return $posts;
    }
}
?>