public function get_parent_title(Post $post, $content = '')
 {
     $content = $post->get_title() . ' | ' . $content;
     if ($post->get_parent() !== null) {
         $content = $this->get_parent_title($post->get_parent(), $content);
     }
     return $content;
 }
 /**
  * Get post by permalink components.
  *
  * @param Post $post
  *
  * @return Post
  *
  * @throws Exception
  */
 public function get_by_permalink(Post $post)
 {
     // Parent post ID.
     $parent_id = $post->get_parent() !== null ? $post->get_parent()->get_id() : 0;
     // Select post with a specific GUID ending.
     $query = $this->wpdb->prepare('SELECT * FROM ' . $this->wpdb->posts . ' WHERE post_parent = %s AND post_name = %s AND post_type = %s', $parent_id, $post->get_name(), $post->get_type());
     $result = $this->wpdb->get_results($query, ARRAY_A);
     if (empty($result)) {
         return null;
     }
     if (count($result) > 1) {
         // Get all post IDs.
         $ids = array_map(function ($row) {
             return $row['ID'];
         }, $result);
         // Turn array of IDs into string of IDs.
         $ids = implode(', ', $ids);
         throw new Exception(sprintf('Permalink components not unique (post_parent: %d, post_name: %s, post_type: %s). Set unique permalink for the following post IDs: %s', $parent_id, $post->get_name(), $post->get_type(), $ids));
     }
     if (isset($result[0]) && isset($result[0]['ID'])) {
         return $this->create_object($result[0]);
     }
     return null;
 }
 /**
  * Make sure parent post exist (if post has any) either in production
  * database or in batch.
  *
  * @param Post  $post
  * @param array $posts
  * @return bool True if parent post exist (or post does not have a parent), false
  *              otherwise.
  */
 private function parent_post_exists(Post $post, $posts)
 {
     // Check if the post has a parent post.
     if ($post->get_parent() === null) {
         return true;
     }
     // Check if parent post exist on production server.
     if ($this->post_dao->get_by_guid($post->get_parent()->get_guid())) {
         return true;
     }
     // Parent post is not on production, look in this batch for parent post.
     foreach ($posts as $item) {
         if ($item->get_id() == $post->get_parent()->get_id()) {
             return true;
         }
     }
     return false;
 }
 /**
  * Update the relationship between a post and its parent post.
  *
  * @param Post $post
  */
 public function update_parent_post_relation(Post $post)
 {
     $parent = $post->get_parent();
     if (!$parent) {
         return;
     }
     // Get production IDs.
     $prod_id = $this->post_dao->get_id_by_guid($post->get_guid());
     $parent_prod_id = $this->post_dao->get_id_by_guid($parent->get_guid());
     if (!$prod_id) {
         $msg = sprintf('No post with GUID %s found on production.', $post->get_guid());
         $this->api->add_deploy_message($this->batch->get_id(), $msg, 'error');
         return;
     }
     if (!$parent_prod_id) {
         $msg = sprintf('No post with GUID %s found on production.', $parent->get_guid());
         $this->api->add_deploy_message($this->batch->get_id(), $msg, 'error');
         return;
     }
     $this->post_dao->update(array('post_parent' => $parent_prod_id), array('ID' => $prod_id), array('%d'), array('%d'));
 }