Airship\Cabin\Bridge\Blueprint\Blog::updatePost PHP Method

updatePost() public method

Update a blog post
public updatePost ( array $post, array $old, boolean $publish = false ) : boolean
$post array
$old array
$publish boolean
return boolean
    public function updatePost(array $post, array $old, bool $publish = false) : bool
    {
        $this->db->beginTransaction();
        $postUpdates = [];
        // First, update the hull_blog_posts entry
        if (!empty($post['author'])) {
            if ($post['author'] !== $old['author']) {
                $postUpdates['author'] = (int) $post['author'];
            }
        }
        if ($post['description'] !== $old['description']) {
            $postUpdates['description'] = (string) $post['description'];
        }
        if ($post['format'] !== $old['format']) {
            $postUpdates['format'] = (string) $post['format'];
        }
        if ($post['slug'] !== $old['slug']) {
            $bm = (string) $old['blogmonth'] < 10 ? '0' . $old['blogmonth'] : $old['blogmonth'];
            $exists = $this->db->cell('SELECT count(*) FROM view_hull_blog_list WHERE blogmonth = ? AND blogyear = ? AND slug = ?', $old['blogyear'], $bm, $post['slug']);
            if ($exists > 0) {
                // Slug collision
                return false;
            }
            $postUpdates['slug'] = (string) $post['slug'];
            if (!empty($post['redirect_slug'])) {
                $oldUrl = \implode('/', ['blog', $old['blogyear'], $bm, $old['slug']]);
                $newUrl = \implode('/', ['blog', $old['blogyear'], $bm, $post['slug']]);
                $this->db->insert('airship_custom_redirect', ['oldpath' => $oldUrl, 'newpath' => $newUrl, 'cabin' => $this->cabin, 'same_cabin' => true]);
            }
        }
        $now = new \DateTime();
        if (!empty($post['published'])) {
            try {
                $now = new \DateTime($post['published']);
            } catch (\Throwable $ex) {
            }
        }
        if (!\array_key_exists('category', $post)) {
            $post['category'] = 0;
        }
        if ($post['category'] !== $old['category']) {
            $postUpdates['category'] = (int) $post['category'];
        }
        if ($publish) {
            $postUpdates['status'] = true;
            $postUpdates['cache'] = !empty($post['cache']);
            // Let's set the publishing time.
            $postUpdates['published'] = $now->format(\AIRSHIP_DATE_FORMAT);
        }
        if ($post['title'] !== $old['title']) {
            $postUpdates['title'] = (string) $post['title'];
        }
        if (!empty($postUpdates)) {
            $this->db->update('hull_blog_posts', $postUpdates, ['postid' => $old['postid']]);
        }
        do {
            $unique = \Airship\uniqueId();
            $exists = $this->db->exists('SELECT COUNT(*) FROM hull_blog_post_versions WHERE uniqueid = ?', $unique);
        } while ($exists);
        // Second, create a new entry in hull_blog_post_versions
        $this->db->insert('hull_blog_post_versions', ['post' => $old['postid'], 'body' => $post['blog_post_body'], 'format' => $post['format'], 'live' => $publish, 'metadata' => \json_encode($post['metadata'] ?? []), 'published_by' => $publish ? $this->getActiveUserId() : null, 'uniqueid' => $unique]);
        if (empty($old['tags'])) {
            $old['tags'] = [];
        }
        if (empty($post['tags'])) {
            $post['tags'] = [];
        }
        // Now let's update the tag relationships
        $tag_ins = \array_diff($post['tags'], $old['tags']);
        $tag_del = \array_diff($old['tags'], $post['tags']);
        foreach ($tag_del as $del) {
            $this->db->delete('hull_blog_post_tags', ['postid' => $old['postid'], 'tagid' => $del]);
        }
        foreach ($tag_ins as $ins) {
            $this->db->insert('hull_blog_post_tags', ['postid' => $old['postid'], 'tagid' => $ins]);
        }
        if ($publish) {
            \Airship\clear_cache();
        }
        return $this->db->commit();
    }

Usage Example

Example #1
0
 /**
  * Update a blog post
  *
  * @param array $post
  * @param array $authorsAllowed
  * @param array $oldPost
  * @return bool
  */
 protected function processEditPost(array $post, array $authorsAllowed = [], array $oldPost = []) : bool
 {
     $required = ['author', 'blog_post_body', 'format', 'save_btn', 'title'];
     if (!\Airship\all_keys_exist($required, $post)) {
         return false;
     }
     if (!$this->isSuperUser()) {
         if (!empty($post['author'])) {
             // Only administrators can transfer ownership; block this request
             return false;
         }
         if (!\in_array((int) $oldPost['author'], $authorsAllowed)) {
             // This author is invalid.
             return false;
         }
     }
     $publish = $this->can('publish') ? $post['save_btn'] === 'publish' : false;
     return $this->blog->updatePost($post, $oldPost, $publish);
 }