/**
  * Import postmeta for a specific post.
  *
  * Never call before all posts has been imported! In case you do
  * relationships between post IDs on content stage and production has not
  * been established and import of postmeta will fail!
  *
  * Start by changing content staging post IDs to production IDs.
  *
  * The content staging post ID is used as a key in the post relations
  * array and the production post ID is used as value.
  *
  * @param Post $post
  */
 public function import_post_meta(Post $post)
 {
     $meta = $post->get_meta();
     // Keys in postmeta table containing relationship to another post.
     $keys = $this->batch->get_post_rel_keys();
     // Stage ID of current post.
     $stage_id = $post->get_id();
     // Get the production ID of the current post.
     $prod_id = $this->post_dao->get_id_by_guid($post->get_guid());
     // Post not found on production.
     if (!$prod_id) {
         $message = sprintf('Post not found on production. Stage ID %d, GUID: %s', $stage_id, $prod_id);
         $this->api->add_deploy_message($this->batch->get_id(), $message, 'error');
         return;
     }
     for ($i = 0; $i < count($meta); $i++) {
         // Update post ID to point at the post ID on production.
         $meta[$i]['post_id'] = $prod_id;
         // TODO Remove check for "master"
         if (in_array($meta[$i]['meta_key'], $keys) && !empty($meta[$i]['meta_value']) && $meta[$i]['meta_value'] !== 'master') {
             // Post ID this meta value is referring to.
             $referenced_post_id = $this->post_dao->get_id_by_guid($meta[$i]['meta_value']);
             // Referenced post could not be found.
             if (!$referenced_post_id) {
                 $referenced_post_id = 0;
                 $message = 'Failed updating relationship between posts (blog ID %d). The relationship is defined in the postmeta table. ';
                 $message .= '<ul>';
                 $message .= '<li>Stage ID referencing post: %d</li>';
                 $message .= '<li>Production ID referencing post: %d</li>';
                 $message .= '<li>Key holding referenced post: %s</li>';
                 $message .= '<li>GUID referenced post: %s</li>';
                 $message .= '</ul>';
                 $message = sprintf($message, get_current_blog_id(), $stage_id, $prod_id, $meta[$i]['meta_key'], $meta[$i]['meta_value']);
                 $this->api->add_deploy_message($this->batch->get_id(), $message, 'warning');
             }
             // Update meta value to point at the post ID on production.
             $meta[$i]['meta_value'] = $referenced_post_id;
         }
     }
     $this->postmeta_dao->update_postmeta_by_post($prod_id, $meta);
 }
 /**
  * Take content of a batch (attachments, users, post, custom data) and
  * inserted into database.
  *
  * @param Batch $batch
  */
 private function update_batch_content(Batch $batch)
 {
     $content = array('attachments' => $batch->get_attachments(), 'users' => $batch->get_users(), 'posts' => $batch->get_posts(), 'custom_data' => $batch->get_custom_data(), 'post_rel_keys' => $batch->get_post_rel_keys());
     $content = base64_encode(serialize($content));
     update_post_meta($batch->get_id(), '_sme_batch_content', $content);
 }
 /**
  * Add post to the batch that is referenced through the post meta of
  * another post.
  *
  * Scope of this method has been extended to also include changing the ID
  * of the referenced post into using the GUID instead.
  *
  * @param Batch $batch
  * @param array $postmeta
  *
  * @return array
  */
 private function add_related_posts(Batch $batch, $postmeta)
 {
     // Check if this post meta key is in the array of post relationship keys.
     if (!in_array($postmeta['meta_key'], $batch->get_post_rel_keys())) {
         return $postmeta;
     }
     // Find post the current post holds a reference to.
     $post = $this->post_dao->find($postmeta['meta_value']);
     if (isset($post) && $post->get_id() !== null) {
         $this->add_post($batch, $post);
         /*
          * Change meta value to post GUID instead of post ID so we can later find
          * the reference on production.
          */
         $postmeta['meta_value'] = $post->get_guid();
     }
     return $postmeta;
 }