This method's purpose is to be able to insert a post (possibly with all its metadata, tags, and comments)
in one method call. As much data as possible has been made optional, to be able to do imports where only
fractions of the data we need are known.
The item array should have at least a 'title' and a 'text' property other properties are optional.
The meta array has only optional properties. You can use these to override the defaults.
The tags array is just a list of tagnames as string.
The comments array is an array of arrays with comment properties. A comment should have
at least 'author', 'email', and 'text' properties.
/** * Import a blog post * * @param \SimpleXMLElement $xml * @return bool */ private function processPost($xml) { // Are we really working with a post? if ($xml->children('wp', true)->post_type != 'post') { return false; } // This is a deleted post, don't import if ($xml->children('wp', true)->status == 'trash') { return false; } // Mapping for wordpress status => fork status $statusses = array('draft' => 'draft', 'pending' => 'draft', 'private' => 'private', 'publish' => 'active', 'future' => 'publish'); $commentStatusses = array('open' => 'Y', 'closed' => 'N'); // Prepare item $item = array(); $item['user_id'] = $this->handleUser((string) $xml->children('dc', true)->creator); $item['title'] = (string) $xml->title; $item['text'] = $this->handleUrls((string) $xml->children('content', true)->encoded, $this->frm->getField('filter')->getValue()); $item['created_on'] = (string) $xml->children('wp', true)->post_date; $item['publish_on'] = (string) $xml->children('wp', true)->post_date; $item['edited_on'] = (string) $xml->children('wp', true)->post_date; $item['status'] = $statusses[(string) $xml->children('wp', true)->status]; $item['allow_comments'] = $commentStatusses[(string) $xml->children('wp', true)->comment_status]; // Some status corrections if ($item['status'] == 'draft') { $item['hidden'] = 'Y'; } elseif ($item['status'] == 'private') { $item['status'] = 'publish'; $item['hidden'] = 'Y'; } // Prepare meta $meta = array(); $meta['url'] = (string) $xml->children('wp', true)->post_name; // Prepare tags $tags = array(); // Walk through wp categories foreach ($xml->category as $category) { /* @var \SimpleXMLElement $category */ switch ($category->attributes()->domain) { case 'category': $item['category_id'] = $this->handleCategory((string) $category); break; case 'post_tag': $tags[] = (string) $category; break; default: // Do nothing break; } } // Prepare comments $comments = array(); // Walk through wp comments foreach ($xml->children('wp', true)->comment as $comment) { /* @var \SimpleXMLElement $comment */ $comments[] = array('author' => (string) $comment->children('wp', true)->comment_author, 'email' => (string) $comment->children('wp', true)->comment_author_email, 'text' => filter_var((string) $comment->children('wp', true)->comment_content, FILTER_SANITIZE_STRING), 'created_on' => (string) $comment->children('wp', true)->comment_date, 'status' => (string) $comment->children('wp', true)->comment_approved == '1' ? 'published' : 'moderation'); } // Make the call Model::insertCompletePost($item, $meta, $tags, $comments); return true; }