/** * Writes the post to the disk and updates the metadata store * By default, posts stay in "draft" status. * If $status is "published", and $publish is null, current datetime is used * * @param $type * @param $postInfo * @param string $status * @param null $publish */ public function addPost($type, $postInfo, $status = 'draft', $publish = null) { if ($this->rootFolder != '') { // make sure that required content folders exist if (!file_exists($this->rootFolder . '/contents')) { mkdir($this->rootFolder . '/contents'); } if (!file_exists($this->rootFolder . '/contents/' . $type)) { mkdir($this->rootFolder . '/contents/' . $type); } // add more fields automatically $id = $this->getNewID(); $postInfo['id'] = $id; $postInfo['type'] = $type; $postInfo['status'] = $status; $postInfo['created'] = \Temple\DateTimeUtil::now()->format('Y-m-d H:i:s'); $postInfo['creator'] = $this->currentUser; if ($status == 'published') { $postInfo['published'] = !is_null($publish) ? $publish : $postInfo['created']; } else { $postInfo['published'] = ''; } // write out the JSON file $postFilePath = $this->rootFolder . '/contents/' . $type . '/' . $id . '.json'; file_put_contents($postFilePath, json_encode($postInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); // refresh metadata on the disk $this->refreshMetadata($postInfo); } }