Backend\Modules\Blog\Engine\Model::insertCompletePost PHP Method

insertCompletePost() public static method

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.
public static insertCompletePost ( array $item, array $meta = [], array $tags = [], array $comments = [] ) : integer
$item array The data to insert.
$meta array The metadata to insert.
$tags array The tags to connect to this post.
$comments array The comments attached to this post.
return integer
    public static function insertCompletePost($item, $meta = array(), $tags = array(), $comments = array())
    {
        // Build item
        if (!isset($item['id'])) {
            $item['id'] = (int) self::getMaximumId() + 1;
        }
        if (!isset($item['user_id'])) {
            $item['user_id'] = BackendAuthentication::getUser()->getUserId();
        }
        if (!isset($item['hidden'])) {
            $item['hidden'] = 'N';
        }
        if (!isset($item['allow_comments'])) {
            $item['allow_comments'] = 'Y';
        }
        if (!isset($item['num_comments'])) {
            $item['num_comments'] = 0;
        }
        if (!isset($item['status'])) {
            $item['status'] = 'active';
        }
        if (!isset($item['language'])) {
            $item['language'] = BL::getWorkingLanguage();
        }
        if (!isset($item['publish_on'])) {
            $item['publish_on'] = BackendModel::getUTCDate();
        }
        if (!isset($item['created_on'])) {
            $item['created_on'] = BackendModel::getUTCDate();
        }
        if (!isset($item['edited_on'])) {
            $item['edited_on'] = BackendModel::getUTCDate();
        }
        if (!isset($item['category_id'])) {
            $item['category_id'] = 1;
        }
        if (!isset($item['title']) || !isset($item['text'])) {
            throw new Exception('$item should at least have a title and a text property');
        }
        // Set drafts hidden
        if (strtotime((string) $item['publish_on']) > time()) {
            $item['hidden'] = 'Y';
            $item['status'] = 'draft';
        }
        // Build meta
        if (!is_array($meta)) {
            $meta = array();
        }
        if (!isset($meta['keywords'])) {
            $meta['keywords'] = $item['title'];
        }
        if (!isset($meta['keywords_overwrite'])) {
            $meta['keywords_overwrite'] = 'N';
        }
        if (!isset($meta['description'])) {
            $meta['description'] = $item['title'];
        }
        if (!isset($meta['description_overwrite'])) {
            $meta['description_overwrite'] = 'N';
        }
        if (!isset($meta['title'])) {
            $meta['title'] = $item['title'];
        }
        if (!isset($meta['title_overwrite'])) {
            $meta['title_overwrite'] = 'N';
        }
        if (!isset($meta['url'])) {
            $meta['url'] = self::getURL($item['title']);
        }
        if (!isset($meta['url_overwrite'])) {
            $meta['url_overwrite'] = 'N';
        }
        if (!isset($meta['data'])) {
            $meta['data'] = serialize(array('seo_index' => 'index', 'seo_follow' => 'follow'));
        }
        // Write meta to db
        $item['meta_id'] = BackendModel::getContainer()->get('database')->insert('meta', $meta);
        // Write post to db
        $item['revision_id'] = self::insert($item);
        // Any tags?
        if (!empty($tags)) {
            BackendTagsModel::saveTags($item['id'], implode(',', $tags), 'blog');
        }
        // Any comments?
        foreach ($comments as $comment) {
            // We require some fields (author, email, text)
            if (!isset($comment['author']) || !isset($comment['email']) || !isset($comment['text'])) {
                continue;
            }
            // Set some defaults
            if (!isset($comment['language'])) {
                $comment['language'] = BL::getWorkingLanguage();
            }
            if (!isset($comment['created_on'])) {
                $comment['created_on'] = BackendModel::getUTCDate();
            }
            if (!isset($comment['status'])) {
                $comment['status'] = 'published';
            }
            if (!isset($comment['data'])) {
                $comment['data'] = serialize(array('server' => $_SERVER));
            }
            if (!isset($comment['website'])) {
                $comment['website'] = '';
            }
            $comment['post_id'] = $item['id'];
            $comment['data'] = serialize(array('server' => $_SERVER));
            // Insert the comment
            self::insertComment($comment);
        }
        // Return
        return $item['revision_id'];
    }

Usage Example

 /**
  * 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;
 }